Skip to content

Commit e2dd95d

Browse files
committed
Allow for one-line CREATE TABLE statements
Fixes #46
1 parent d92185d commit e2dd95d

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

src/PrintFn.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ export type PrintFn<T, K = PrintableKey<T>> = {
2323
separated(separator: Doc, path: K[]): Doc[];
2424

2525
// Returns either:
26-
// - line: when the node contains a newline
27-
// - hardline: when the original source contains no newlines
26+
// - line: when the node contains no newlines
27+
// - hardline: when the original source contains a newline
2828
dynamicLine(): Doc;
2929
};
3030

src/syntax/expr.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ export const exprMap: CstToDocMap<AllExprNodes> = {
5757
return print("expr");
5858
}
5959
const parent = path.getParentNode() as Node;
60-
const lineStyle = isCreateTableStmt(parent) ? hardline : softline;
60+
const lineStyle =
61+
isCreateTableStmt(parent) && print.dynamicLine() === hardline
62+
? hardline
63+
: softline;
6164
return group(["(", indent([lineStyle, print("expr")]), lineStyle, ")"]);
6265
},
6366
binary_expr: (print, node) => {

test/canonical_syntax.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ describe("with sqlCanonicalSyntax enabled", () => {
2121
sqlCanonicalSyntax: true,
2222
}),
2323
).toBe(dedent`
24-
CREATE TEMPORARY TABLE foo (
25-
id INT
26-
)
24+
CREATE TEMPORARY TABLE foo (id INT)
2725
`);
2826
});
2927

test/ddl/create_table.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import dedent from "dedent-js";
22
import { test, testBigquery, testMysql, testPostgresql } from "../test_utils";
33

44
describe("create table", () => {
5-
it(`formats CREATE TABLE always on multiple lines`, async () => {
5+
it(`formats short CREATE TABLE on single line if it fits`, async () => {
6+
await test(dedent`
7+
CREATE TABLE client (id INT, name VARCHAR(100), org_id INT)
8+
`);
9+
});
10+
11+
it(`formats short CREATE TABLE on multiple lines if user prefers`, async () => {
612
await test(dedent`
713
CREATE TABLE client (
814
id INT,

0 commit comments

Comments
 (0)