Skip to content

Commit

Permalink
Format SEARCH and CYCLE clauses inside WITH clause
Browse files Browse the repository at this point in the history
  • Loading branch information
nene committed Jan 7, 2024
1 parent be0f771 commit 414e2bb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/syntax/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,32 @@ export const selectMap: Partial<CstToDocMap<AllSelectNodes>> = {
print.spaced(["withKw", "recursiveKw"]),
indent([line, print("tables")]),
]),
common_table_expr: (print) => [
common_table_expr: (print, node) => [
print(["table", "columns"]),
" ",
print.spaced(["asKw", "materializedKw", "expr"]),
group([
node.search || node.cycle
? indent([line, join(line, print(["search", "cycle"]))])
: [],
]),
],
cte_search_clause: (print) =>
group([print.spaced(["searchKw", "columns", "setKw", "resultColumn"])]),
cte_cycle_clause: (print) =>
group([
print.spaced([
"cycleKw",
"columns",
"setKw",
"resultColumn",
"values",
"usingKw",
"pathColumn",
]),
]),
cte_cycle_clause_values: (print) =>
print.spaced(["toKw", "markValue", "defaultKw", "defaultValue"]),

// SELECT clause
select_clause: (print, node, path, opts) =>
Expand Down
21 changes: 20 additions & 1 deletion test/select/with.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dedent from "dedent-js";
import { test } from "../test_utils";
import { test, testPostgresql } from "../test_utils";

describe("select with", () => {
it(`formats tiny WITH on same line as the rest of SELECT`, async () => {
Expand Down Expand Up @@ -56,4 +56,23 @@ describe("select with", () => {
FROM oldies
`);
});

describe("PostgreSQL", () => {
it(`formats CYCLE and SEARCH clauses in WITH`, async () => {
await testPostgresql(dedent`
WITH RECURSIVE
cte1 AS (SELECT * FROM my_table WHERE x > 0)
CYCLE a, b SET a TO 1 DEFAULT 0 USING pathcol,
cte2 AS (
SELECT *
FROM client
WHERE age > 100
) SEARCH BREADTH FIRST BY a, b SET target_col
SELECT *
FROM
cte1,
cte2
`);
});
});
});

0 comments on commit 414e2bb

Please sign in to comment.