Skip to content

Commit

Permalink
Elaborate formatting of long CYCLE and SEARCH clauses
Browse files Browse the repository at this point in the history
  • Loading branch information
nene committed Jan 7, 2024
1 parent 414e2bb commit decb74d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/syntax/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,29 @@ export const selectMap: Partial<CstToDocMap<AllSelectNodes>> = {
]),
],
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",
group(
join(line, [
[print.spaced("searchKw"), group(indent([line, print("columns")]))],
print.spaced(["setKw", "resultColumn"]),
]),
]),
),
cte_cycle_clause: (print, node) =>
group(
join(
line,
[
[print("cycleKw"), group(indent([line, print("columns")]))],
print.spaced(["setKw", "resultColumn"]),
node.values ? print("values") : undefined,
print.spaced(["usingKw", "pathColumn"]),
].filter(isDefined),
),
),
cte_cycle_clause_values: (print) =>
print.spaced(["toKw", "markValue", "defaultKw", "defaultValue"]),
join(line, [
print.spaced(["toKw", "markValue"]),
print.spaced(["defaultKw", "defaultValue"]),
]),

// SELECT clause
select_clause: (print, node, path, opts) =>
Expand Down
30 changes: 30 additions & 0 deletions test/select/with.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,35 @@ describe("select with", () => {
cte2
`);
});

it(`formats long CYCLE and SEARCH clauses in WITH`, async () => {
await testPostgresql(dedent`
WITH RECURSIVE
cte1 AS (SELECT * FROM tbl)
CYCLE
first_long_column_name,
second_really_long_column_name,
third_column_name_as_well
SET target_column_name
USING path_column_name,
cte2 AS (SELECT * FROM tbl)
CYCLE col1, col2
SET target_column_name
TO 'Found it here in the cycle'
DEFAULT 'No cycle found'
USING path_column_name,
cte3 AS (SELECT * FROM tbl)
SEARCH DEPTH FIRST BY
first_long_column_name,
second_really_long_column_name,
third_column_name_as_well
SET target_column_name
SELECT *
FROM
cte1,
cte2,
cte3
`);
});
});
});

0 comments on commit decb74d

Please sign in to comment.