sumak 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +409 -5
- package/dist/ast/expression.d.mts +26 -0
- package/dist/ast/expression.mjs +140 -0
- package/dist/ast/nodes.d.mts +309 -0
- package/dist/ast/nodes.mjs +59 -0
- package/dist/ast/transformer.d.mts +10 -0
- package/dist/ast/transformer.mjs +140 -0
- package/dist/ast/typed-expression.d.mts +37 -0
- package/dist/ast/typed-expression.mjs +77 -0
- package/dist/ast/visitor.d.mts +13 -0
- package/dist/ast/visitor.mjs +11 -0
- package/dist/builder/delete.d.mts +20 -0
- package/dist/builder/delete.mjs +113 -0
- package/dist/builder/eb.d.mts +312 -0
- package/dist/builder/eb.mjs +641 -0
- package/dist/builder/expression.d.mts +5 -0
- package/dist/builder/expression.mjs +10 -0
- package/dist/builder/insert.d.mts +40 -0
- package/dist/builder/insert.mjs +146 -0
- package/dist/builder/merge.d.mts +20 -0
- package/dist/builder/merge.mjs +100 -0
- package/dist/builder/raw.d.mts +2 -0
- package/dist/builder/raw.mjs +4 -0
- package/dist/builder/select.d.mts +41 -0
- package/dist/builder/select.mjs +280 -0
- package/dist/builder/typed-delete.d.mts +49 -0
- package/dist/builder/typed-delete.mjs +89 -0
- package/dist/builder/typed-insert.d.mts +86 -0
- package/dist/builder/typed-insert.mjs +159 -0
- package/dist/builder/typed-merge.d.mts +31 -0
- package/dist/builder/typed-merge.mjs +93 -0
- package/dist/builder/typed-select.d.mts +164 -0
- package/dist/builder/typed-select.mjs +309 -0
- package/dist/builder/typed-update.d.mts +59 -0
- package/dist/builder/typed-update.mjs +110 -0
- package/dist/builder/update.d.mts +20 -0
- package/dist/builder/update.mjs +121 -0
- package/dist/dialect/mssql.d.mts +2 -0
- package/dist/dialect/mssql.mjs +9 -0
- package/dist/dialect/mysql.d.mts +2 -0
- package/dist/dialect/mysql.mjs +9 -0
- package/dist/dialect/pg.d.mts +2 -0
- package/dist/dialect/pg.mjs +9 -0
- package/dist/dialect/sqlite.d.mts +2 -0
- package/dist/dialect/sqlite.mjs +9 -0
- package/dist/dialect/types.d.mts +6 -0
- package/dist/dialect/types.mjs +1 -0
- package/dist/errors.d.mts +12 -0
- package/dist/errors.mjs +24 -0
- package/dist/index.d.mts +50 -806
- package/dist/index.mjs +48 -3
- package/dist/mssql.d.mts +2 -2
- package/dist/mssql.mjs +2 -1
- package/dist/mysql.d.mts +2 -2
- package/dist/mysql.mjs +2 -1
- package/dist/pg.d.mts +2 -2
- package/dist/pg.mjs +2 -1
- package/dist/plugin/camel-case.d.mts +11 -0
- package/dist/plugin/camel-case.mjs +16 -0
- package/dist/plugin/hooks.d.mts +72 -0
- package/dist/plugin/hooks.mjs +49 -0
- package/dist/plugin/plugin-manager.d.mts +17 -0
- package/dist/plugin/plugin-manager.mjs +37 -0
- package/dist/plugin/soft-delete.d.mts +27 -0
- package/dist/plugin/soft-delete.mjs +52 -0
- package/dist/plugin/types.d.mts +19 -0
- package/dist/plugin/types.mjs +1 -0
- package/dist/plugin/with-schema.d.mts +21 -0
- package/dist/plugin/with-schema.mjs +53 -0
- package/dist/printer/base.d.mts +49 -0
- package/dist/printer/base.mjs +472 -0
- package/dist/printer/document.d.mts +45 -0
- package/dist/printer/document.mjs +153 -0
- package/dist/printer/formatter.d.mts +5 -0
- package/dist/printer/formatter.mjs +134 -0
- package/dist/printer/mssql.d.mts +10 -0
- package/dist/printer/mssql.mjs +161 -0
- package/dist/printer/mysql.d.mts +8 -0
- package/dist/printer/mysql.mjs +41 -0
- package/dist/printer/pg.d.mts +6 -0
- package/dist/printer/pg.mjs +9 -0
- package/dist/printer/sqlite.d.mts +8 -0
- package/dist/printer/sqlite.mjs +29 -0
- package/dist/printer/types.d.mts +11 -0
- package/dist/printer/types.mjs +1 -0
- package/dist/schema/column.d.mts +52 -0
- package/dist/schema/column.mjs +120 -0
- package/dist/schema/index.d.mts +6 -0
- package/dist/schema/index.mjs +4 -0
- package/dist/schema/table.d.mts +37 -0
- package/dist/schema/table.mjs +7 -0
- package/dist/schema/type-utils.d.mts +46 -0
- package/dist/schema/type-utils.mjs +1 -0
- package/dist/schema/types.d.mts +64 -0
- package/dist/schema/types.mjs +1 -0
- package/dist/schema.d.mts +2 -2
- package/dist/schema.mjs +1 -1
- package/dist/sqlite.d.mts +2 -2
- package/dist/sqlite.mjs +2 -1
- package/dist/sumak.d.mts +110 -0
- package/dist/sumak.mjs +141 -0
- package/dist/types.d.mts +14 -0
- package/dist/types.mjs +1 -0
- package/dist/utils/identifier.d.mts +3 -0
- package/dist/utils/identifier.mjs +14 -0
- package/dist/utils/param.d.mts +2 -0
- package/dist/utils/param.mjs +8 -0
- package/package.json +1 -1
- package/dist/_chunks/base.mjs +0 -1
- package/dist/_chunks/errors.mjs +0 -1
- package/dist/_chunks/index.d.mts +0 -136
- package/dist/_chunks/mssql.d.mts +0 -11
- package/dist/_chunks/mssql.mjs +0 -1
- package/dist/_chunks/mysql.d.mts +0 -9
- package/dist/_chunks/mysql.mjs +0 -1
- package/dist/_chunks/pg.d.mts +0 -7
- package/dist/_chunks/pg.mjs +0 -1
- package/dist/_chunks/schema.mjs +0 -1
- package/dist/_chunks/sqlite.d.mts +0 -9
- package/dist/_chunks/sqlite.mjs +0 -1
- package/dist/_chunks/types.d.mts +0 -338
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
import { quoteIdentifier, quoteTableRef } from "../utils/identifier.mjs";
|
|
2
|
+
import { formatParam } from "../utils/param.mjs";
|
|
3
|
+
export class BasePrinter {
|
|
4
|
+
params = [];
|
|
5
|
+
dialect;
|
|
6
|
+
constructor(dialect) {
|
|
7
|
+
this.dialect = dialect;
|
|
8
|
+
}
|
|
9
|
+
print(node) {
|
|
10
|
+
this.params = [];
|
|
11
|
+
const sql = this.printNode(node);
|
|
12
|
+
return {
|
|
13
|
+
sql,
|
|
14
|
+
params: [...this.params]
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
printNode(node) {
|
|
18
|
+
switch (node.type) {
|
|
19
|
+
case "select": return this.printSelect(node);
|
|
20
|
+
case "insert": return this.printInsert(node);
|
|
21
|
+
case "update": return this.printUpdate(node);
|
|
22
|
+
case "delete": return this.printDelete(node);
|
|
23
|
+
case "merge": return this.printMerge(node);
|
|
24
|
+
case "explain": return this.printExplain(node);
|
|
25
|
+
default: return this.printExpression(node);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
printSelect(node) {
|
|
29
|
+
const parts = [];
|
|
30
|
+
if (node.ctes.length > 0) {
|
|
31
|
+
parts.push(this.printCTEs(node.ctes));
|
|
32
|
+
}
|
|
33
|
+
parts.push("SELECT");
|
|
34
|
+
if (node.distinctOn && node.distinctOn.length > 0) {
|
|
35
|
+
parts.push(`DISTINCT ON (${node.distinctOn.map((e) => this.printExpression(e)).join(", ")})`);
|
|
36
|
+
} else if (node.distinct) {
|
|
37
|
+
parts.push("DISTINCT");
|
|
38
|
+
}
|
|
39
|
+
if (node.columns.length === 0) {
|
|
40
|
+
parts.push("*");
|
|
41
|
+
} else {
|
|
42
|
+
parts.push(node.columns.map((c) => this.printExpression(c)).join(", "));
|
|
43
|
+
}
|
|
44
|
+
if (node.from) {
|
|
45
|
+
parts.push("FROM");
|
|
46
|
+
if (node.from.type === "subquery") {
|
|
47
|
+
parts.push(this.printSubquery(node.from));
|
|
48
|
+
} else {
|
|
49
|
+
parts.push(this.printTableRef(node.from));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
for (const join of node.joins) {
|
|
53
|
+
parts.push(this.printJoin(join));
|
|
54
|
+
}
|
|
55
|
+
if (node.where) {
|
|
56
|
+
parts.push("WHERE", this.printExpression(node.where));
|
|
57
|
+
}
|
|
58
|
+
if (node.groupBy.length > 0) {
|
|
59
|
+
parts.push("GROUP BY", node.groupBy.map((g) => this.printExpression(g)).join(", "));
|
|
60
|
+
}
|
|
61
|
+
if (node.having) {
|
|
62
|
+
parts.push("HAVING", this.printExpression(node.having));
|
|
63
|
+
}
|
|
64
|
+
if (node.orderBy.length > 0) {
|
|
65
|
+
parts.push("ORDER BY", node.orderBy.map((o) => this.printOrderBy(o)).join(", "));
|
|
66
|
+
}
|
|
67
|
+
if (node.limit) {
|
|
68
|
+
parts.push("LIMIT", this.printExpression(node.limit));
|
|
69
|
+
}
|
|
70
|
+
if (node.offset) {
|
|
71
|
+
parts.push("OFFSET", this.printExpression(node.offset));
|
|
72
|
+
}
|
|
73
|
+
if (node.setOp) {
|
|
74
|
+
parts.push(node.setOp.op, this.printSelect(node.setOp.query));
|
|
75
|
+
}
|
|
76
|
+
if (node.lock) {
|
|
77
|
+
parts.push(`FOR ${node.lock.mode}`);
|
|
78
|
+
if (node.lock.noWait) {
|
|
79
|
+
parts.push("NOWAIT");
|
|
80
|
+
} else if (node.lock.skipLocked) {
|
|
81
|
+
parts.push("SKIP LOCKED");
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return parts.join(" ");
|
|
85
|
+
}
|
|
86
|
+
printInsert(node) {
|
|
87
|
+
const parts = [];
|
|
88
|
+
if (node.ctes.length > 0) {
|
|
89
|
+
parts.push(this.printCTEs(node.ctes));
|
|
90
|
+
}
|
|
91
|
+
const insertKeyword = node.insertMode ?? "INSERT";
|
|
92
|
+
parts.push(`${insertKeyword} INTO`, this.printTableRef(node.table));
|
|
93
|
+
if (node.columns.length > 0) {
|
|
94
|
+
parts.push(`(${node.columns.map((c) => quoteIdentifier(c, this.dialect)).join(", ")})`);
|
|
95
|
+
}
|
|
96
|
+
if (node.defaultValues) {
|
|
97
|
+
parts.push("DEFAULT VALUES");
|
|
98
|
+
} else if (node.source) {
|
|
99
|
+
parts.push(this.printSelect(node.source));
|
|
100
|
+
} else {
|
|
101
|
+
parts.push("VALUES");
|
|
102
|
+
const rows = node.values.map((row) => `(${row.map((v) => this.printExpression(v)).join(", ")})`);
|
|
103
|
+
parts.push(rows.join(", "));
|
|
104
|
+
}
|
|
105
|
+
if (node.onConflict) {
|
|
106
|
+
parts.push(this.printOnConflict(node.onConflict));
|
|
107
|
+
}
|
|
108
|
+
if (node.returning.length > 0) {
|
|
109
|
+
parts.push("RETURNING", node.returning.map((r) => this.printExpression(r)).join(", "));
|
|
110
|
+
}
|
|
111
|
+
return parts.join(" ");
|
|
112
|
+
}
|
|
113
|
+
printOnConflict(node) {
|
|
114
|
+
const parts = ["ON CONFLICT"];
|
|
115
|
+
if (node.constraint) {
|
|
116
|
+
parts.push(`ON CONSTRAINT ${quoteIdentifier(node.constraint, this.dialect)}`);
|
|
117
|
+
} else if (node.columns.length > 0) {
|
|
118
|
+
parts.push(`(${node.columns.map((c) => quoteIdentifier(c, this.dialect)).join(", ")})`);
|
|
119
|
+
}
|
|
120
|
+
if (node.action === "nothing") {
|
|
121
|
+
parts.push("DO NOTHING");
|
|
122
|
+
} else {
|
|
123
|
+
parts.push("DO UPDATE SET");
|
|
124
|
+
const sets = node.action.set.map((s) => `${quoteIdentifier(s.column, this.dialect)} = ${this.printExpression(s.value)}`);
|
|
125
|
+
parts.push(sets.join(", "));
|
|
126
|
+
}
|
|
127
|
+
if (node.where) {
|
|
128
|
+
parts.push("WHERE", this.printExpression(node.where));
|
|
129
|
+
}
|
|
130
|
+
return parts.join(" ");
|
|
131
|
+
}
|
|
132
|
+
printUpdate(node) {
|
|
133
|
+
const parts = [];
|
|
134
|
+
if (node.ctes.length > 0) {
|
|
135
|
+
parts.push(this.printCTEs(node.ctes));
|
|
136
|
+
}
|
|
137
|
+
parts.push("UPDATE", this.printTableRef(node.table), "SET");
|
|
138
|
+
const sets = node.set.map((s) => `${quoteIdentifier(s.column, this.dialect)} = ${this.printExpression(s.value)}`);
|
|
139
|
+
parts.push(sets.join(", "));
|
|
140
|
+
for (const join of node.joins) {
|
|
141
|
+
parts.push(this.printJoin(join));
|
|
142
|
+
}
|
|
143
|
+
if (node.from) {
|
|
144
|
+
parts.push("FROM", this.printTableRef(node.from));
|
|
145
|
+
}
|
|
146
|
+
if (node.where) {
|
|
147
|
+
parts.push("WHERE", this.printExpression(node.where));
|
|
148
|
+
}
|
|
149
|
+
if (node.orderBy && node.orderBy.length > 0) {
|
|
150
|
+
parts.push("ORDER BY", node.orderBy.map((o) => this.printOrderBy(o)).join(", "));
|
|
151
|
+
}
|
|
152
|
+
if (node.limit) {
|
|
153
|
+
parts.push("LIMIT", this.printExpression(node.limit));
|
|
154
|
+
}
|
|
155
|
+
if (node.returning.length > 0) {
|
|
156
|
+
parts.push("RETURNING", node.returning.map((r) => this.printExpression(r)).join(", "));
|
|
157
|
+
}
|
|
158
|
+
return parts.join(" ");
|
|
159
|
+
}
|
|
160
|
+
printDelete(node) {
|
|
161
|
+
const parts = [];
|
|
162
|
+
if (node.ctes.length > 0) {
|
|
163
|
+
parts.push(this.printCTEs(node.ctes));
|
|
164
|
+
}
|
|
165
|
+
parts.push("DELETE FROM", this.printTableRef(node.table));
|
|
166
|
+
if (node.using) {
|
|
167
|
+
parts.push("USING", this.printTableRef(node.using));
|
|
168
|
+
}
|
|
169
|
+
for (const join of node.joins) {
|
|
170
|
+
parts.push(this.printJoin(join));
|
|
171
|
+
}
|
|
172
|
+
if (node.where) {
|
|
173
|
+
parts.push("WHERE", this.printExpression(node.where));
|
|
174
|
+
}
|
|
175
|
+
if (node.orderBy && node.orderBy.length > 0) {
|
|
176
|
+
parts.push("ORDER BY", node.orderBy.map((o) => this.printOrderBy(o)).join(", "));
|
|
177
|
+
}
|
|
178
|
+
if (node.limit) {
|
|
179
|
+
parts.push("LIMIT", this.printExpression(node.limit));
|
|
180
|
+
}
|
|
181
|
+
if (node.returning.length > 0) {
|
|
182
|
+
parts.push("RETURNING", node.returning.map((r) => this.printExpression(r)).join(", "));
|
|
183
|
+
}
|
|
184
|
+
return parts.join(" ");
|
|
185
|
+
}
|
|
186
|
+
printExpression(node) {
|
|
187
|
+
switch (node.type) {
|
|
188
|
+
case "column_ref": return this.printColumnRef(node);
|
|
189
|
+
case "literal": return this.printLiteral(node);
|
|
190
|
+
case "binary_op": return this.printBinaryOp(node);
|
|
191
|
+
case "unary_op": return this.printUnaryOp(node);
|
|
192
|
+
case "function_call": return this.printFunctionCall(node);
|
|
193
|
+
case "param": return this.printParam(node);
|
|
194
|
+
case "raw": return this.printRaw(node);
|
|
195
|
+
case "subquery": return this.printSubquery(node);
|
|
196
|
+
case "between": return this.printBetween(node);
|
|
197
|
+
case "in": return this.printIn(node);
|
|
198
|
+
case "is_null": return this.printIsNull(node);
|
|
199
|
+
case "cast": return this.printCast(node);
|
|
200
|
+
case "exists": return this.printExists(node);
|
|
201
|
+
case "star": return this.printStar(node);
|
|
202
|
+
case "case": return this.printCase(node);
|
|
203
|
+
case "json_access": return this.printJsonAccess(node);
|
|
204
|
+
case "array_expr": return this.printArrayExpr(node);
|
|
205
|
+
case "window_function": return this.printWindowFunction(node);
|
|
206
|
+
case "aliased_expr": return this.printAliasedExpr(node);
|
|
207
|
+
case "full_text_search": return this.printFullTextSearch(node);
|
|
208
|
+
case "tuple": return this.printTuple(node);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
printColumnRef(node) {
|
|
212
|
+
let result = node.table ? `${quoteIdentifier(node.table, this.dialect)}.${quoteIdentifier(node.column, this.dialect)}` : quoteIdentifier(node.column, this.dialect);
|
|
213
|
+
if (node.alias) {
|
|
214
|
+
result += ` AS ${quoteIdentifier(node.alias, this.dialect)}`;
|
|
215
|
+
}
|
|
216
|
+
return result;
|
|
217
|
+
}
|
|
218
|
+
printLiteral(node) {
|
|
219
|
+
if (node.value === null) return "NULL";
|
|
220
|
+
if (typeof node.value === "boolean") return node.value ? "TRUE" : "FALSE";
|
|
221
|
+
if (typeof node.value === "number") return String(node.value);
|
|
222
|
+
return `'${String(node.value).replaceAll("'", "''")}'`;
|
|
223
|
+
}
|
|
224
|
+
printBinaryOp(node) {
|
|
225
|
+
return `(${this.printExpression(node.left)} ${node.op} ${this.printExpression(node.right)})`;
|
|
226
|
+
}
|
|
227
|
+
printUnaryOp(node) {
|
|
228
|
+
if (node.position === "postfix") {
|
|
229
|
+
return `(${this.printExpression(node.operand)} ${node.op})`;
|
|
230
|
+
}
|
|
231
|
+
return `(${node.op} ${this.printExpression(node.operand)})`;
|
|
232
|
+
}
|
|
233
|
+
printFunctionCall(node) {
|
|
234
|
+
const distinctPrefix = node.distinct ? "DISTINCT " : "";
|
|
235
|
+
let inner = `${distinctPrefix}${node.args.map((a) => this.printExpression(a)).join(", ")}`;
|
|
236
|
+
if (node.orderBy && node.orderBy.length > 0) {
|
|
237
|
+
inner += ` ORDER BY ${node.orderBy.map((o) => this.printOrderBy(o)).join(", ")}`;
|
|
238
|
+
}
|
|
239
|
+
let result = `${node.name}(${inner})`;
|
|
240
|
+
if (node.filter) {
|
|
241
|
+
result += ` FILTER (WHERE ${this.printExpression(node.filter)})`;
|
|
242
|
+
}
|
|
243
|
+
if (node.alias) {
|
|
244
|
+
result += ` AS ${quoteIdentifier(node.alias, this.dialect)}`;
|
|
245
|
+
}
|
|
246
|
+
return result;
|
|
247
|
+
}
|
|
248
|
+
printParam(node) {
|
|
249
|
+
this.params.push(node.value);
|
|
250
|
+
return formatParam(this.params.length - 1, this.dialect);
|
|
251
|
+
}
|
|
252
|
+
printRaw(node) {
|
|
253
|
+
this.params.push(...node.params);
|
|
254
|
+
return node.sql;
|
|
255
|
+
}
|
|
256
|
+
printSubquery(node) {
|
|
257
|
+
let result = `(${this.printSelect(node.query)})`;
|
|
258
|
+
if (node.alias) {
|
|
259
|
+
result += ` AS ${quoteIdentifier(node.alias, this.dialect)}`;
|
|
260
|
+
}
|
|
261
|
+
return result;
|
|
262
|
+
}
|
|
263
|
+
printBetween(node) {
|
|
264
|
+
const neg = node.negated ? "NOT " : "";
|
|
265
|
+
const sym = node.symmetric ? " SYMMETRIC" : "";
|
|
266
|
+
return `(${this.printExpression(node.expr)} ${neg}BETWEEN${sym} ${this.printExpression(node.low)} AND ${this.printExpression(node.high)})`;
|
|
267
|
+
}
|
|
268
|
+
printIn(node) {
|
|
269
|
+
const neg = node.negated ? "NOT " : "";
|
|
270
|
+
if (Array.isArray(node.values)) {
|
|
271
|
+
return `(${this.printExpression(node.expr)} ${neg}IN (${node.values.map((v) => this.printExpression(v)).join(", ")}))`;
|
|
272
|
+
}
|
|
273
|
+
return `(${this.printExpression(node.expr)} ${neg}IN (${this.printSelect(node.values)}))`;
|
|
274
|
+
}
|
|
275
|
+
printIsNull(node) {
|
|
276
|
+
const neg = node.negated ? " NOT" : "";
|
|
277
|
+
return `(${this.printExpression(node.expr)} IS${neg} NULL)`;
|
|
278
|
+
}
|
|
279
|
+
printCase(node) {
|
|
280
|
+
const parts = ["CASE"];
|
|
281
|
+
if (node.operand) {
|
|
282
|
+
parts.push(this.printExpression(node.operand));
|
|
283
|
+
}
|
|
284
|
+
for (const when of node.whens) {
|
|
285
|
+
parts.push("WHEN", this.printExpression(when.condition), "THEN", this.printExpression(when.result));
|
|
286
|
+
}
|
|
287
|
+
if (node.else_) {
|
|
288
|
+
parts.push("ELSE", this.printExpression(node.else_));
|
|
289
|
+
}
|
|
290
|
+
parts.push("END");
|
|
291
|
+
return parts.join(" ");
|
|
292
|
+
}
|
|
293
|
+
printCast(node) {
|
|
294
|
+
return `CAST(${this.printExpression(node.expr)} AS ${node.dataType})`;
|
|
295
|
+
}
|
|
296
|
+
printExists(node) {
|
|
297
|
+
const neg = node.negated ? "NOT " : "";
|
|
298
|
+
return `(${neg}EXISTS (${this.printSelect(node.query)}))`;
|
|
299
|
+
}
|
|
300
|
+
printStar(node) {
|
|
301
|
+
return node.table ? `${quoteIdentifier(node.table, this.dialect)}.*` : "*";
|
|
302
|
+
}
|
|
303
|
+
printTableRef(ref) {
|
|
304
|
+
let result = quoteTableRef(ref.name, this.dialect, ref.schema);
|
|
305
|
+
if (ref.temporal) {
|
|
306
|
+
result += ` ${this.printTemporalClause(ref.temporal)}`;
|
|
307
|
+
}
|
|
308
|
+
if (ref.alias) {
|
|
309
|
+
result += ` AS ${quoteIdentifier(ref.alias, this.dialect)}`;
|
|
310
|
+
}
|
|
311
|
+
return result;
|
|
312
|
+
}
|
|
313
|
+
printTemporalClause(clause) {
|
|
314
|
+
switch (clause.kind) {
|
|
315
|
+
case "as_of": return `FOR SYSTEM_TIME AS OF ${this.printExpression(clause.timestamp)}`;
|
|
316
|
+
case "from_to": return `FOR SYSTEM_TIME FROM ${this.printExpression(clause.start)} TO ${this.printExpression(clause.end)}`;
|
|
317
|
+
case "between": return `FOR SYSTEM_TIME BETWEEN ${this.printExpression(clause.start)} AND ${this.printExpression(clause.end)}`;
|
|
318
|
+
case "contained_in": return `FOR SYSTEM_TIME CONTAINED IN (${this.printExpression(clause.start)}, ${this.printExpression(clause.end)})`;
|
|
319
|
+
case "all": return "FOR SYSTEM_TIME ALL";
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
printJoin(node) {
|
|
323
|
+
const parts = [];
|
|
324
|
+
const lateral = node.lateral ? " LATERAL" : "";
|
|
325
|
+
parts.push(`${node.joinType} JOIN${lateral}`);
|
|
326
|
+
if (node.table.type === "subquery") {
|
|
327
|
+
parts.push(this.printSubquery(node.table));
|
|
328
|
+
} else {
|
|
329
|
+
parts.push(this.printTableRef(node.table));
|
|
330
|
+
}
|
|
331
|
+
if (node.on) {
|
|
332
|
+
parts.push("ON", this.printExpression(node.on));
|
|
333
|
+
}
|
|
334
|
+
return parts.join(" ");
|
|
335
|
+
}
|
|
336
|
+
printOrderBy(node) {
|
|
337
|
+
let result = `${this.printExpression(node.expr)} ${node.direction}`;
|
|
338
|
+
if (node.nulls) {
|
|
339
|
+
result += ` NULLS ${node.nulls}`;
|
|
340
|
+
}
|
|
341
|
+
return result;
|
|
342
|
+
}
|
|
343
|
+
printCTEs(ctes) {
|
|
344
|
+
const hasRecursive = ctes.some((c) => c.recursive);
|
|
345
|
+
const prefix = hasRecursive ? "WITH RECURSIVE" : "WITH";
|
|
346
|
+
const cteParts = ctes.map((c) => `${quoteIdentifier(c.name, this.dialect)} AS (${this.printSelect(c.query)})`);
|
|
347
|
+
return `${prefix} ${cteParts.join(", ")}`;
|
|
348
|
+
}
|
|
349
|
+
printJsonAccess(node) {
|
|
350
|
+
let result = `${this.printExpression(node.expr)}${node.operator}${this.printLiteral({
|
|
351
|
+
type: "literal",
|
|
352
|
+
value: node.path
|
|
353
|
+
})}`;
|
|
354
|
+
if (node.alias) {
|
|
355
|
+
result += ` AS ${quoteIdentifier(node.alias, this.dialect)}`;
|
|
356
|
+
}
|
|
357
|
+
return result;
|
|
358
|
+
}
|
|
359
|
+
printArrayExpr(node) {
|
|
360
|
+
return `ARRAY[${node.elements.map((e) => this.printExpression(e)).join(", ")}]`;
|
|
361
|
+
}
|
|
362
|
+
printWindowFunction(node) {
|
|
363
|
+
const parts = [];
|
|
364
|
+
parts.push(this.printFunctionCall(node.fn));
|
|
365
|
+
parts.push("OVER");
|
|
366
|
+
const overParts = [];
|
|
367
|
+
if (node.partitionBy.length > 0) {
|
|
368
|
+
overParts.push(`PARTITION BY ${node.partitionBy.map((p) => this.printExpression(p)).join(", ")}`);
|
|
369
|
+
}
|
|
370
|
+
if (node.orderBy.length > 0) {
|
|
371
|
+
overParts.push(`ORDER BY ${node.orderBy.map((o) => this.printOrderBy(o)).join(", ")}`);
|
|
372
|
+
}
|
|
373
|
+
if (node.frame) {
|
|
374
|
+
overParts.push(this.printFrameSpec(node.frame));
|
|
375
|
+
}
|
|
376
|
+
parts.push(`(${overParts.join(" ")})`);
|
|
377
|
+
if (node.alias) {
|
|
378
|
+
parts.push("AS", quoteIdentifier(node.alias, this.dialect));
|
|
379
|
+
}
|
|
380
|
+
return parts.join(" ");
|
|
381
|
+
}
|
|
382
|
+
printFrameSpec(frame) {
|
|
383
|
+
const start = this.printFrameBound(frame.start);
|
|
384
|
+
if (frame.end) {
|
|
385
|
+
return `${frame.kind} BETWEEN ${start} AND ${this.printFrameBound(frame.end)}`;
|
|
386
|
+
}
|
|
387
|
+
return `${frame.kind} ${start}`;
|
|
388
|
+
}
|
|
389
|
+
printFrameBound(bound) {
|
|
390
|
+
switch (bound.type) {
|
|
391
|
+
case "unbounded_preceding": return "UNBOUNDED PRECEDING";
|
|
392
|
+
case "preceding": return `${bound.value} PRECEDING`;
|
|
393
|
+
case "current_row": return "CURRENT ROW";
|
|
394
|
+
case "following": return `${bound.value} FOLLOWING`;
|
|
395
|
+
case "unbounded_following": return "UNBOUNDED FOLLOWING";
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
printMerge(node) {
|
|
399
|
+
const parts = [];
|
|
400
|
+
if (node.ctes.length > 0) {
|
|
401
|
+
parts.push(this.printCTEs(node.ctes));
|
|
402
|
+
}
|
|
403
|
+
parts.push("MERGE INTO", this.printTableRef(node.target));
|
|
404
|
+
parts.push("USING");
|
|
405
|
+
if (node.source.type === "subquery") {
|
|
406
|
+
parts.push(this.printSubquery(node.source));
|
|
407
|
+
} else {
|
|
408
|
+
parts.push(this.printTableRef(node.source));
|
|
409
|
+
}
|
|
410
|
+
parts.push("AS", quoteIdentifier(node.sourceAlias, this.dialect));
|
|
411
|
+
parts.push("ON", this.printExpression(node.on));
|
|
412
|
+
for (const when of node.whens) {
|
|
413
|
+
if (when.type === "matched") {
|
|
414
|
+
parts.push(this.printMergeWhenMatched(when));
|
|
415
|
+
} else {
|
|
416
|
+
parts.push(this.printMergeWhenNotMatched(when));
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return parts.join(" ");
|
|
420
|
+
}
|
|
421
|
+
printMergeWhenMatched(when) {
|
|
422
|
+
const parts = ["WHEN MATCHED"];
|
|
423
|
+
if (when.condition) {
|
|
424
|
+
parts.push("AND", this.printExpression(when.condition));
|
|
425
|
+
}
|
|
426
|
+
if (when.action === "delete") {
|
|
427
|
+
parts.push("THEN DELETE");
|
|
428
|
+
} else {
|
|
429
|
+
parts.push("THEN UPDATE SET");
|
|
430
|
+
const sets = (when.set ?? []).map((s) => `${quoteIdentifier(s.column, this.dialect)} = ${this.printExpression(s.value)}`);
|
|
431
|
+
parts.push(sets.join(", "));
|
|
432
|
+
}
|
|
433
|
+
return parts.join(" ");
|
|
434
|
+
}
|
|
435
|
+
printMergeWhenNotMatched(when) {
|
|
436
|
+
const parts = ["WHEN NOT MATCHED"];
|
|
437
|
+
if (when.condition) {
|
|
438
|
+
parts.push("AND", this.printExpression(when.condition));
|
|
439
|
+
}
|
|
440
|
+
parts.push("THEN INSERT");
|
|
441
|
+
parts.push(`(${when.columns.map((c) => quoteIdentifier(c, this.dialect)).join(", ")})`);
|
|
442
|
+
parts.push(`VALUES (${when.values.map((v) => this.printExpression(v)).join(", ")})`);
|
|
443
|
+
return parts.join(" ");
|
|
444
|
+
}
|
|
445
|
+
printAliasedExpr(node) {
|
|
446
|
+
return `${this.printExpression(node.expr)} AS ${quoteIdentifier(node.alias, this.dialect)}`;
|
|
447
|
+
}
|
|
448
|
+
printTuple(node) {
|
|
449
|
+
return `(${node.elements.map((e) => this.printExpression(e)).join(", ")})`;
|
|
450
|
+
}
|
|
451
|
+
printExplain(node) {
|
|
452
|
+
const parts = ["EXPLAIN"];
|
|
453
|
+
if (node.analyze) {
|
|
454
|
+
parts.push("ANALYZE");
|
|
455
|
+
}
|
|
456
|
+
if (node.format) {
|
|
457
|
+
parts.push(`(FORMAT ${node.format})`);
|
|
458
|
+
}
|
|
459
|
+
parts.push(this.printNode(node.statement));
|
|
460
|
+
return parts.join(" ");
|
|
461
|
+
}
|
|
462
|
+
printFullTextSearch(node) {
|
|
463
|
+
|
|
464
|
+
const cols = node.columns.map((c) => this.printExpression(c)).join(" || ' ' || ");
|
|
465
|
+
const lang = node.language ? `'${node.language}', ` : "";
|
|
466
|
+
let result = `(to_tsvector(${lang}${cols}) @@ to_tsquery(${lang}${this.printExpression(node.query)}))`;
|
|
467
|
+
if (node.alias) {
|
|
468
|
+
result += ` AS ${quoteIdentifier(node.alias, this.dialect)}`;
|
|
469
|
+
}
|
|
470
|
+
return result;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wadler-style pretty printer document algebra.
|
|
3
|
+
*
|
|
4
|
+
* Based on "A prettier printer" (Wadler 1998) and "Strictly Pretty" (Lindig 2000).
|
|
5
|
+
* Used to format SQL output with width-sensitive line breaking.
|
|
6
|
+
*/
|
|
7
|
+
export type Doc = {
|
|
8
|
+
tag: "empty";
|
|
9
|
+
} | {
|
|
10
|
+
tag: "text";
|
|
11
|
+
text: string;
|
|
12
|
+
} | {
|
|
13
|
+
tag: "line";
|
|
14
|
+
} | {
|
|
15
|
+
tag: "nest";
|
|
16
|
+
indent: number;
|
|
17
|
+
doc: Doc;
|
|
18
|
+
} | {
|
|
19
|
+
tag: "group";
|
|
20
|
+
doc: Doc;
|
|
21
|
+
} | {
|
|
22
|
+
tag: "concat";
|
|
23
|
+
docs: Doc[];
|
|
24
|
+
};
|
|
25
|
+
export declare function empty(): Doc;
|
|
26
|
+
export declare function text(s: string): Doc;
|
|
27
|
+
/** A line break. In flat mode, renders as a single space. */
|
|
28
|
+
export declare function line(): Doc;
|
|
29
|
+
/** Increase indentation for the nested document. */
|
|
30
|
+
export declare function nest(indent: number, doc: Doc): Doc;
|
|
31
|
+
/** Try to render flat; if too wide, break lines. */
|
|
32
|
+
export declare function group(doc: Doc): Doc;
|
|
33
|
+
/** Concatenate documents. */
|
|
34
|
+
export declare function concat(...docs: Doc[]): Doc;
|
|
35
|
+
/** Join documents with a separator. */
|
|
36
|
+
export declare function join(sep: Doc, docs: Doc[]): Doc;
|
|
37
|
+
/** Convenience: text + line */
|
|
38
|
+
export declare function textLine(s: string): Doc;
|
|
39
|
+
/**
|
|
40
|
+
* Render a document to a string.
|
|
41
|
+
*
|
|
42
|
+
* Uses Wadler/Lindig's algorithm: maintain a stack of (indent, mode, doc) triples.
|
|
43
|
+
* mode = "flat" (try single line) or "break" (use line breaks).
|
|
44
|
+
*/
|
|
45
|
+
export declare function render(doc: Doc, width?: number): string;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
export function empty() {
|
|
2
|
+
return { tag: "empty" };
|
|
3
|
+
}
|
|
4
|
+
export function text(s) {
|
|
5
|
+
return {
|
|
6
|
+
tag: "text",
|
|
7
|
+
text: s
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function line() {
|
|
12
|
+
return { tag: "line" };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function nest(indent, doc) {
|
|
16
|
+
return {
|
|
17
|
+
tag: "nest",
|
|
18
|
+
indent,
|
|
19
|
+
doc
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function group(doc) {
|
|
24
|
+
return {
|
|
25
|
+
tag: "group",
|
|
26
|
+
doc
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function concat(...docs) {
|
|
31
|
+
const filtered = docs.filter((d) => d.tag !== "empty");
|
|
32
|
+
if (filtered.length === 0) return empty();
|
|
33
|
+
if (filtered.length === 1) return filtered[0];
|
|
34
|
+
return {
|
|
35
|
+
tag: "concat",
|
|
36
|
+
docs: filtered
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function join(sep, docs) {
|
|
41
|
+
if (docs.length === 0) return empty();
|
|
42
|
+
const result = [];
|
|
43
|
+
for (let i = 0; i < docs.length; i++) {
|
|
44
|
+
if (i > 0) result.push(sep);
|
|
45
|
+
result.push(docs[i]);
|
|
46
|
+
}
|
|
47
|
+
return concat(...result);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function textLine(s) {
|
|
51
|
+
return concat(text(s), line());
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function render(doc, width = 80) {
|
|
55
|
+
let output = "";
|
|
56
|
+
let col = 0;
|
|
57
|
+
const stack = [[
|
|
58
|
+
0,
|
|
59
|
+
"break",
|
|
60
|
+
doc
|
|
61
|
+
]];
|
|
62
|
+
while (stack.length > 0) {
|
|
63
|
+
const [indent, mode, d] = stack.pop();
|
|
64
|
+
switch (d.tag) {
|
|
65
|
+
case "empty": break;
|
|
66
|
+
case "text":
|
|
67
|
+
output += d.text;
|
|
68
|
+
col += d.text.length;
|
|
69
|
+
break;
|
|
70
|
+
case "line":
|
|
71
|
+
if (mode === "flat") {
|
|
72
|
+
output += " ";
|
|
73
|
+
col += 1;
|
|
74
|
+
} else {
|
|
75
|
+
output += "\n" + " ".repeat(indent);
|
|
76
|
+
col = indent;
|
|
77
|
+
}
|
|
78
|
+
break;
|
|
79
|
+
case "nest":
|
|
80
|
+
stack.push([
|
|
81
|
+
indent + d.indent,
|
|
82
|
+
mode,
|
|
83
|
+
d.doc
|
|
84
|
+
]);
|
|
85
|
+
break;
|
|
86
|
+
case "group":
|
|
87
|
+
if (mode === "flat") {
|
|
88
|
+
stack.push([
|
|
89
|
+
indent,
|
|
90
|
+
"flat",
|
|
91
|
+
d.doc
|
|
92
|
+
]);
|
|
93
|
+
} else {
|
|
94
|
+
|
|
95
|
+
const flat = measureFlat(d.doc);
|
|
96
|
+
if (flat !== null && col + flat <= width) {
|
|
97
|
+
stack.push([
|
|
98
|
+
indent,
|
|
99
|
+
"flat",
|
|
100
|
+
d.doc
|
|
101
|
+
]);
|
|
102
|
+
} else {
|
|
103
|
+
stack.push([
|
|
104
|
+
indent,
|
|
105
|
+
"break",
|
|
106
|
+
d.doc
|
|
107
|
+
]);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
break;
|
|
111
|
+
case "concat":
|
|
112
|
+
|
|
113
|
+
for (let i = d.docs.length - 1; i >= 0; i--) {
|
|
114
|
+
stack.push([
|
|
115
|
+
indent,
|
|
116
|
+
mode,
|
|
117
|
+
d.docs[i]
|
|
118
|
+
]);
|
|
119
|
+
}
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return output;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function measureFlat(doc) {
|
|
127
|
+
let width = 0;
|
|
128
|
+
const stack = [doc];
|
|
129
|
+
while (stack.length > 0) {
|
|
130
|
+
const d = stack.pop();
|
|
131
|
+
switch (d.tag) {
|
|
132
|
+
case "empty": break;
|
|
133
|
+
case "text":
|
|
134
|
+
width += d.text.length;
|
|
135
|
+
break;
|
|
136
|
+
case "line":
|
|
137
|
+
width += 1;
|
|
138
|
+
break;
|
|
139
|
+
case "nest":
|
|
140
|
+
stack.push(d.doc);
|
|
141
|
+
break;
|
|
142
|
+
case "group":
|
|
143
|
+
stack.push(d.doc);
|
|
144
|
+
break;
|
|
145
|
+
case "concat":
|
|
146
|
+
for (let i = d.docs.length - 1; i >= 0; i--) {
|
|
147
|
+
stack.push(d.docs[i]);
|
|
148
|
+
}
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return width;
|
|
153
|
+
}
|