Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 551x 551x 267x 267x 267x 267x 267x | import type { CteCycle, CteNode, CteSearch, ModifyingNode, QueryNode, WithClause } from './ast.ts';
import { ChunkBuilder, type Chunks } from './sql-chunks.ts';
/** Compiles a CTE body: a query, or a data-modifying statement. */
export type CteBodyCompiler = (node: QueryNode | ModifyingNode) => Chunks;
/** Compiles `WITH [RECURSIVE]` clauses with `MATERIALIZED`, `SEARCH` and `CYCLE`. */
export class WithCompiler extends ChunkBuilder {
readonly #body: CteBodyCompiler;
/**
* @param body - Compiler of CTE bodies.
*/
constructor(body: CteBodyCompiler) {
super();
this.#body = body;
}
/**
* Compiles a WITH clause.
*
* @param clause - WITH clause.
* @returns `WITH ... ` including the trailing space.
*/
with(clause: WithClause): Chunks {
return [
clause.recursive ? 'WITH RECURSIVE ' : 'WITH ',
...this.joinAll(
clause.ctes.map((cte) => this.#cte(cte)),
', ',
),
' ',
];
}
#cte(cte: CteNode): Chunks {
return [
`${this.quote(cte.name)} AS `,
cte.materialized === undefined
? ''
: cte.materialized
? 'MATERIALIZED '
: 'NOT MATERIALIZED ',
...this.wrap(this.#body(cte.query)),
this.#search(cte.search),
this.#cycle(cte.cycle),
];
}
#search(search: CteSearch | undefined): string {
return search === undefined
? ''
: ` SEARCH ${search.mode} FIRST BY ${search.by.map(this.quote).join(', ')} SET ${this.quote(search.set)}`;
}
#cycle(cycle: CteCycle | undefined): string {
return cycle === undefined
? ''
: ` CYCLE ${cycle.columns.map(this.quote).join(', ')} SET ${this.quote(cycle.set)} USING ${this.quote(cycle.using)}`;
}
}
|