Skip to content

Commit

Permalink
More dynamic formatting of CREATE INDEX
Browse files Browse the repository at this point in the history
  • Loading branch information
nene committed Jan 20, 2024
1 parent ba811ee commit b86c1d4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
30 changes: 16 additions & 14 deletions src/syntax/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { AllIndexNodes } from "sql-parser-cst";
import { hardline, join } from "../print_utils";
import { group, join } from "../print_utils";
import { CstToDocMap } from "../CstToDocMap";

export const indexMap: CstToDocMap<AllIndexNodes> = {
create_index_stmt: (print, node) =>
join(hardline, [
print.spaced([
"createKw",
"indexTypeKw",
"indexKw",
"ifNotExistsKw",
"name",
"onKw",
"table",
"columns",
create_index_stmt: (print) =>
group(
join(print.dynamicLine(), [
print.spaced([
"createKw",
"indexTypeKw",
"indexKw",
"ifNotExistsKw",
"name",
"onKw",
"table",
"columns",
]),
...print("clauses"),
]),
...(node.clauses.length > 0 ? [join(hardline, print("clauses"))] : []),
]),
),
drop_index_stmt: (print) =>
print.spaced([
"dropKw",
Expand Down
8 changes: 7 additions & 1 deletion test/ddl/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ describe("index", () => {
`);
});

it(`formats WHERE clause on separate line`, async () => {
it(`formats WHERE clause on same line (if user prefers)`, async () => {
await test(dedent`
CREATE INDEX my_index ON my_table (col) WHERE col > 10
`);
});

it(`formats WHERE clause on separate line (if user prefers)`, async () => {
await test(dedent`
CREATE INDEX my_index ON my_table (col)
WHERE col > 10
Expand Down

0 comments on commit b86c1d4

Please sign in to comment.