sumak 0.0.3 → 0.0.5
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 +650 -56
- package/dist/ast/expression.d.mts +26 -0
- package/dist/ast/expression.mjs +140 -0
- package/dist/ast/nodes.d.mts +298 -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 +18 -0
- package/dist/builder/delete.mjs +94 -0
- package/dist/builder/eb.d.mts +210 -0
- package/dist/builder/eb.mjs +399 -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 +38 -0
- package/dist/builder/select.mjs +242 -0
- package/dist/builder/typed-delete.d.mts +43 -0
- package/dist/builder/typed-delete.mjs +77 -0
- package/dist/builder/typed-insert.d.mts +74 -0
- package/dist/builder/typed-insert.mjs +136 -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 +125 -0
- package/dist/builder/typed-select.mjs +217 -0
- package/dist/builder/typed-update.d.mts +55 -0
- package/dist/builder/typed-update.mjs +102 -0
- package/dist/builder/update.d.mts +18 -0
- package/dist/builder/update.mjs +102 -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 +49 -660
- package/dist/index.mjs +46 -3
- package/dist/mssql.d.mts +2 -0
- package/dist/mssql.mjs +2 -0
- 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 +48 -0
- package/dist/printer/base.mjs +450 -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 +98 -0
- package/dist/sumak.mjs +132 -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 +7 -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/mysql.d.mts +0 -8
- 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 -8
- package/dist/_chunks/sqlite.mjs +0 -1
- package/dist/_chunks/types.d.mts +0 -274
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import { col as rawCol, lit as rawLit, param as rawParam, and as rawAnd, or as rawOr, fn as rawFn, star as rawStar, exists as rawExists, cast as rawCast, not as rawNot } from "../ast/expression.mjs";
|
|
2
|
+
|
|
3
|
+
export class Col {
|
|
4
|
+
|
|
5
|
+
_node;
|
|
6
|
+
constructor(column, table) {
|
|
7
|
+
this._node = rawCol(column, table);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
eq(value) {
|
|
11
|
+
return wrap(binOp("=", this._node, autoParam(value)));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
neq(value) {
|
|
15
|
+
return wrap(binOp("!=", this._node, autoParam(value)));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
gt(value) {
|
|
19
|
+
return wrap(binOp(">", this._node, autoParam(value)));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
gte(value) {
|
|
23
|
+
return wrap(binOp(">=", this._node, autoParam(value)));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
lt(value) {
|
|
27
|
+
return wrap(binOp("<", this._node, autoParam(value)));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
lte(value) {
|
|
31
|
+
return wrap(binOp("<=", this._node, autoParam(value)));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
like(pattern) {
|
|
35
|
+
return wrap(binOp("LIKE", this._node, rawLit(pattern)));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
in(values) {
|
|
39
|
+
return wrap({
|
|
40
|
+
type: "in",
|
|
41
|
+
expr: this._node,
|
|
42
|
+
values: values.map((v) => autoParam(v)),
|
|
43
|
+
negated: false
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
notIn(values) {
|
|
48
|
+
return wrap({
|
|
49
|
+
type: "in",
|
|
50
|
+
expr: this._node,
|
|
51
|
+
values: values.map((v) => autoParam(v)),
|
|
52
|
+
negated: true
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
isNull() {
|
|
57
|
+
return wrap({
|
|
58
|
+
type: "is_null",
|
|
59
|
+
expr: this._node,
|
|
60
|
+
negated: false
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
isNotNull() {
|
|
65
|
+
return wrap({
|
|
66
|
+
type: "is_null",
|
|
67
|
+
expr: this._node,
|
|
68
|
+
negated: true
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
between(low, high) {
|
|
73
|
+
return wrap({
|
|
74
|
+
type: "between",
|
|
75
|
+
expr: this._node,
|
|
76
|
+
low: autoParam(low),
|
|
77
|
+
high: autoParam(high),
|
|
78
|
+
negated: false
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
eqCol(other) {
|
|
83
|
+
return wrap(binOp("=", this._node, other._node));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
toExpr() {
|
|
87
|
+
return wrap(this._node);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
let _paramIdx = 0;
|
|
92
|
+
export function resetParams() {
|
|
93
|
+
_paramIdx = 0;
|
|
94
|
+
}
|
|
95
|
+
function autoParam(value) {
|
|
96
|
+
return rawParam(_paramIdx++, value);
|
|
97
|
+
}
|
|
98
|
+
function binOp(op, left, right) {
|
|
99
|
+
return {
|
|
100
|
+
type: "binary_op",
|
|
101
|
+
op,
|
|
102
|
+
left,
|
|
103
|
+
right
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function wrap(node) {
|
|
107
|
+
return { node };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function createColumnProxies(_table) {
|
|
111
|
+
return new Proxy({}, { get(_target, prop) {
|
|
112
|
+
return new Col(prop, undefined);
|
|
113
|
+
} });
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
export function and(left, right) {
|
|
118
|
+
return wrap(rawAnd(left.node, right.node));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function or(left, right) {
|
|
122
|
+
return wrap(rawOr(left.node, right.node));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function val(value) {
|
|
126
|
+
return wrap(rawLit(value));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function sqlFn(name, ...args) {
|
|
130
|
+
return wrap(rawFn(name, args.map((a) => a.node)));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function count() {
|
|
134
|
+
return wrap(rawFn("COUNT", [rawStar()]));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function countDistinct(expr) {
|
|
138
|
+
const node = {
|
|
139
|
+
type: "function_call",
|
|
140
|
+
name: "COUNT",
|
|
141
|
+
args: [expr.node],
|
|
142
|
+
distinct: true
|
|
143
|
+
};
|
|
144
|
+
return wrap(node);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function sum(expr) {
|
|
148
|
+
return wrap(rawFn("SUM", [expr.node]));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function avg(expr) {
|
|
152
|
+
return wrap(rawFn("AVG", [expr.node]));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function min(expr) {
|
|
156
|
+
return wrap(rawFn("MIN", [expr.node]));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function max(expr) {
|
|
160
|
+
return wrap(rawFn("MAX", [expr.node]));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function coalesce(expr, fallback) {
|
|
164
|
+
return wrap(rawFn("COALESCE", [expr.node, fallback.node]));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function not(expr) {
|
|
168
|
+
return wrap(rawNot(expr.node));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function exists(query) {
|
|
172
|
+
return wrap(rawExists(query));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function notExists(query) {
|
|
176
|
+
return wrap(rawExists(query, true));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function cast(expr, dataType) {
|
|
180
|
+
return wrap(rawCast(expr.node, dataType));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export function jsonRef(expr, path, operator = "->") {
|
|
184
|
+
const node = {
|
|
185
|
+
type: "json_access",
|
|
186
|
+
expr: expr.node,
|
|
187
|
+
path,
|
|
188
|
+
operator
|
|
189
|
+
};
|
|
190
|
+
return wrap(node);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function textSearch(columns, query, options) {
|
|
194
|
+
const node = {
|
|
195
|
+
type: "full_text_search",
|
|
196
|
+
columns: columns.map((c) => c.node),
|
|
197
|
+
query: query.node,
|
|
198
|
+
mode: options?.mode,
|
|
199
|
+
language: options?.language
|
|
200
|
+
};
|
|
201
|
+
return wrap(node);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function case_(operand) {
|
|
205
|
+
return new CaseBuilder(operand ? operand.node : undefined, []);
|
|
206
|
+
}
|
|
207
|
+
export class CaseBuilder {
|
|
208
|
+
|
|
209
|
+
_operand;
|
|
210
|
+
|
|
211
|
+
_whens;
|
|
212
|
+
|
|
213
|
+
_else;
|
|
214
|
+
|
|
215
|
+
constructor(operand, whens, else_) {
|
|
216
|
+
this._operand = operand;
|
|
217
|
+
this._whens = whens;
|
|
218
|
+
this._else = else_;
|
|
219
|
+
}
|
|
220
|
+
when(condition, result) {
|
|
221
|
+
return new CaseBuilder(this._operand, [...this._whens, {
|
|
222
|
+
condition: condition.node,
|
|
223
|
+
result: result.node
|
|
224
|
+
}]);
|
|
225
|
+
}
|
|
226
|
+
else_(result) {
|
|
227
|
+
return new CaseBuilder(this._operand, this._whens, result.node);
|
|
228
|
+
}
|
|
229
|
+
end() {
|
|
230
|
+
const node = {
|
|
231
|
+
type: "case",
|
|
232
|
+
operand: this._operand,
|
|
233
|
+
whens: this._whens,
|
|
234
|
+
else_: this._else
|
|
235
|
+
};
|
|
236
|
+
return wrap(node);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
export function over(fn, build) {
|
|
242
|
+
const builder = build(new WindowBuilder());
|
|
243
|
+
const fnNode = fn.node;
|
|
244
|
+
const node = {
|
|
245
|
+
type: "window_function",
|
|
246
|
+
fn: fnNode,
|
|
247
|
+
partitionBy: builder._partitionBy,
|
|
248
|
+
orderBy: builder._orderBy,
|
|
249
|
+
frame: builder._frame
|
|
250
|
+
};
|
|
251
|
+
return wrap(node);
|
|
252
|
+
}
|
|
253
|
+
export class WindowBuilder {
|
|
254
|
+
|
|
255
|
+
_partitionBy = [];
|
|
256
|
+
|
|
257
|
+
_orderBy = [];
|
|
258
|
+
|
|
259
|
+
_frame;
|
|
260
|
+
partitionBy(...columns) {
|
|
261
|
+
const b = new WindowBuilder();
|
|
262
|
+
b._partitionBy = columns.map((c) => rawCol(c));
|
|
263
|
+
b._orderBy = this._orderBy;
|
|
264
|
+
b._frame = this._frame;
|
|
265
|
+
return b;
|
|
266
|
+
}
|
|
267
|
+
orderBy(column, direction = "ASC") {
|
|
268
|
+
const b = new WindowBuilder();
|
|
269
|
+
b._partitionBy = this._partitionBy;
|
|
270
|
+
b._orderBy = [...this._orderBy, {
|
|
271
|
+
expr: rawCol(column),
|
|
272
|
+
direction
|
|
273
|
+
}];
|
|
274
|
+
b._frame = this._frame;
|
|
275
|
+
return b;
|
|
276
|
+
}
|
|
277
|
+
rows(start, end) {
|
|
278
|
+
return this._withFrame("ROWS", start, end);
|
|
279
|
+
}
|
|
280
|
+
range(start, end) {
|
|
281
|
+
return this._withFrame("RANGE", start, end);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
_withFrame(kind, start, end) {
|
|
285
|
+
const b = new WindowBuilder();
|
|
286
|
+
b._partitionBy = this._partitionBy;
|
|
287
|
+
b._orderBy = this._orderBy;
|
|
288
|
+
b._frame = {
|
|
289
|
+
kind,
|
|
290
|
+
start,
|
|
291
|
+
end
|
|
292
|
+
};
|
|
293
|
+
return b;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
export function rowNumber() {
|
|
299
|
+
return wrap(rawFn("ROW_NUMBER", []));
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export function rank() {
|
|
303
|
+
return wrap(rawFn("RANK", []));
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
export function denseRank() {
|
|
307
|
+
return wrap(rawFn("DENSE_RANK", []));
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
export function lag(expr, offset, defaultValue) {
|
|
311
|
+
const args = [expr.node];
|
|
312
|
+
if (offset !== undefined) args.push(rawLit(offset));
|
|
313
|
+
if (defaultValue !== undefined) args.push(defaultValue.node);
|
|
314
|
+
return wrap(rawFn("LAG", args));
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
export function lead(expr, offset, defaultValue) {
|
|
318
|
+
const args = [expr.node];
|
|
319
|
+
if (offset !== undefined) args.push(rawLit(offset));
|
|
320
|
+
if (defaultValue !== undefined) args.push(defaultValue.node);
|
|
321
|
+
return wrap(rawFn("LEAD", args));
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
export function ntile(n) {
|
|
325
|
+
return wrap(rawFn("NTILE", [rawLit(n)]));
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
export function upper(expr) {
|
|
330
|
+
return wrap(rawFn("UPPER", [expr.node]));
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export function lower(expr) {
|
|
334
|
+
return wrap(rawFn("LOWER", [expr.node]));
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
export function concat(...args) {
|
|
338
|
+
return wrap(rawFn("CONCAT", args.map((a) => a.node)));
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export function substring(expr, start, length) {
|
|
342
|
+
const args = [expr.node, rawLit(start)];
|
|
343
|
+
if (length !== undefined) args.push(rawLit(length));
|
|
344
|
+
return wrap(rawFn("SUBSTRING", args));
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
export function trim(expr) {
|
|
348
|
+
return wrap(rawFn("TRIM", [expr.node]));
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
export function length(expr) {
|
|
352
|
+
return wrap(rawFn("LENGTH", [expr.node]));
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
export function now() {
|
|
356
|
+
return wrap(rawFn("NOW", []));
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
export function currentTimestamp() {
|
|
360
|
+
return wrap(rawFn("CURRENT_TIMESTAMP", []));
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
export function nullif(a, b) {
|
|
364
|
+
return wrap(rawFn("NULLIF", [a.node, b.node]));
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export function greatest(...args) {
|
|
368
|
+
return wrap(rawFn("GREATEST", args.map((a) => a.node)));
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
export function least(...args) {
|
|
372
|
+
return wrap(rawFn("LEAST", args.map((a) => a.node)));
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
export function abs(expr) {
|
|
376
|
+
return wrap(rawFn("ABS", [expr.node]));
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export function round(expr, precision) {
|
|
380
|
+
const args = [expr.node];
|
|
381
|
+
if (precision !== undefined) args.push(rawLit(precision));
|
|
382
|
+
return wrap(rawFn("ROUND", args));
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
export function ceil(expr) {
|
|
386
|
+
return wrap(rawFn("CEIL", [expr.node]));
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
export function floor(expr) {
|
|
390
|
+
return wrap(rawFn("FLOOR", [expr.node]));
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
export function filter(agg, condition) {
|
|
394
|
+
const fnNode = agg.node;
|
|
395
|
+
return wrap({
|
|
396
|
+
...fnNode,
|
|
397
|
+
filter: condition.node
|
|
398
|
+
});
|
|
399
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { and, between, binOp, col, eq, exists, gt, gte, inList, isNull, like, lit, lt, lte, neq, not, or } from "../ast/expression.mjs";
|
|
2
|
+
import type { ExpressionNode } from "../ast/nodes.mjs";
|
|
3
|
+
export declare function resetParamCounter(): void;
|
|
4
|
+
export declare function val(value: unknown): ExpressionNode;
|
|
5
|
+
export { and, between, binOp, col, eq, exists, gt, gte, inList, isNull, like, lit, lt, lte, neq, not, or };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { and, between, binOp, col, eq, exists, gt, gte, inList, isNull, like, lit, lt, lte, neq, not, or, param } from "../ast/expression.mjs";
|
|
2
|
+
let paramCounter = 0;
|
|
3
|
+
export function resetParamCounter() {
|
|
4
|
+
paramCounter = 0;
|
|
5
|
+
}
|
|
6
|
+
export function val(value) {
|
|
7
|
+
const index = paramCounter++;
|
|
8
|
+
return param(index, value);
|
|
9
|
+
}
|
|
10
|
+
export { and, between, binOp, col, eq, exists, gt, gte, inList, isNull, like, lit, lt, lte, neq, not, or };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ExpressionNode, InsertMode, InsertNode, SelectNode, TableRefNode } from "../ast/nodes.mjs";
|
|
2
|
+
export declare class InsertBuilder {
|
|
3
|
+
private node;
|
|
4
|
+
private paramIndex;
|
|
5
|
+
constructor(node?: InsertNode, paramIndex?: number);
|
|
6
|
+
into(table: string | TableRefNode): InsertBuilder;
|
|
7
|
+
columns(...cols: string[]): InsertBuilder;
|
|
8
|
+
values(...vals: unknown[]): InsertBuilder;
|
|
9
|
+
returning(...exprs: ExpressionNode[]): InsertBuilder;
|
|
10
|
+
onConflictDoNothing(...columns: string[]): InsertBuilder;
|
|
11
|
+
onConflictDoUpdate(columns: string[], set: {
|
|
12
|
+
column: string;
|
|
13
|
+
value: ExpressionNode;
|
|
14
|
+
}[], where?: ExpressionNode): InsertBuilder;
|
|
15
|
+
/** ON CONFLICT ON CONSTRAINT name DO NOTHING */
|
|
16
|
+
onConflictConstraintDoNothing(constraint: string): InsertBuilder;
|
|
17
|
+
/** ON CONFLICT ON CONSTRAINT name DO UPDATE SET ... */
|
|
18
|
+
onConflictConstraintDoUpdate(constraint: string, set: {
|
|
19
|
+
column: string;
|
|
20
|
+
value: ExpressionNode;
|
|
21
|
+
}[], where?: ExpressionNode): InsertBuilder;
|
|
22
|
+
/** Set insert mode (SQLite: INSERT OR IGNORE, INSERT OR REPLACE, etc.) */
|
|
23
|
+
mode(mode: InsertMode): InsertBuilder;
|
|
24
|
+
/** Shorthand: INSERT OR IGNORE (SQLite) */
|
|
25
|
+
orIgnore(): InsertBuilder;
|
|
26
|
+
/** Shorthand: INSERT OR REPLACE (SQLite) */
|
|
27
|
+
orReplace(): InsertBuilder;
|
|
28
|
+
/** MySQL: ON DUPLICATE KEY UPDATE */
|
|
29
|
+
onDuplicateKeyUpdate(set: {
|
|
30
|
+
column: string;
|
|
31
|
+
value: ExpressionNode;
|
|
32
|
+
}[]): InsertBuilder;
|
|
33
|
+
/** INSERT INTO ... SELECT ... */
|
|
34
|
+
fromSelect(query: SelectNode): InsertBuilder;
|
|
35
|
+
/** INSERT INTO ... DEFAULT VALUES */
|
|
36
|
+
defaultValues(): InsertBuilder;
|
|
37
|
+
with(name: string, query: SelectNode, recursive?: boolean): InsertBuilder;
|
|
38
|
+
build(): InsertNode;
|
|
39
|
+
}
|
|
40
|
+
export declare function insert(table: string | TableRefNode): InsertBuilder;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { param } from "../ast/expression.mjs";
|
|
2
|
+
import { createInsertNode } from "../ast/nodes.mjs";
|
|
3
|
+
export class InsertBuilder {
|
|
4
|
+
node;
|
|
5
|
+
paramIndex;
|
|
6
|
+
constructor(node, paramIndex = 0) {
|
|
7
|
+
this.node = node ?? createInsertNode({
|
|
8
|
+
type: "table_ref",
|
|
9
|
+
name: ""
|
|
10
|
+
});
|
|
11
|
+
this.paramIndex = paramIndex;
|
|
12
|
+
}
|
|
13
|
+
into(table) {
|
|
14
|
+
const ref = typeof table === "string" ? {
|
|
15
|
+
type: "table_ref",
|
|
16
|
+
name: table
|
|
17
|
+
} : table;
|
|
18
|
+
return new InsertBuilder({
|
|
19
|
+
...this.node,
|
|
20
|
+
table: ref
|
|
21
|
+
}, this.paramIndex);
|
|
22
|
+
}
|
|
23
|
+
columns(...cols) {
|
|
24
|
+
return new InsertBuilder({
|
|
25
|
+
...this.node,
|
|
26
|
+
columns: [...this.node.columns, ...cols]
|
|
27
|
+
}, this.paramIndex);
|
|
28
|
+
}
|
|
29
|
+
values(...vals) {
|
|
30
|
+
let idx = this.paramIndex;
|
|
31
|
+
const row = vals.map((v) => {
|
|
32
|
+
const p = param(idx, v);
|
|
33
|
+
idx++;
|
|
34
|
+
return p;
|
|
35
|
+
});
|
|
36
|
+
return new InsertBuilder({
|
|
37
|
+
...this.node,
|
|
38
|
+
values: [...this.node.values, row]
|
|
39
|
+
}, idx);
|
|
40
|
+
}
|
|
41
|
+
returning(...exprs) {
|
|
42
|
+
return new InsertBuilder({
|
|
43
|
+
...this.node,
|
|
44
|
+
returning: [...this.node.returning, ...exprs]
|
|
45
|
+
}, this.paramIndex);
|
|
46
|
+
}
|
|
47
|
+
onConflictDoNothing(...columns) {
|
|
48
|
+
const conflict = {
|
|
49
|
+
columns,
|
|
50
|
+
action: "nothing"
|
|
51
|
+
};
|
|
52
|
+
return new InsertBuilder({
|
|
53
|
+
...this.node,
|
|
54
|
+
onConflict: conflict
|
|
55
|
+
}, this.paramIndex);
|
|
56
|
+
}
|
|
57
|
+
onConflictDoUpdate(columns, set, where) {
|
|
58
|
+
const conflict = {
|
|
59
|
+
columns,
|
|
60
|
+
action: { set },
|
|
61
|
+
where
|
|
62
|
+
};
|
|
63
|
+
return new InsertBuilder({
|
|
64
|
+
...this.node,
|
|
65
|
+
onConflict: conflict
|
|
66
|
+
}, this.paramIndex);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
onConflictConstraintDoNothing(constraint) {
|
|
70
|
+
const conflict = {
|
|
71
|
+
columns: [],
|
|
72
|
+
constraint,
|
|
73
|
+
action: "nothing"
|
|
74
|
+
};
|
|
75
|
+
return new InsertBuilder({
|
|
76
|
+
...this.node,
|
|
77
|
+
onConflict: conflict
|
|
78
|
+
}, this.paramIndex);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
onConflictConstraintDoUpdate(constraint, set, where) {
|
|
82
|
+
const conflict = {
|
|
83
|
+
columns: [],
|
|
84
|
+
constraint,
|
|
85
|
+
action: { set },
|
|
86
|
+
where
|
|
87
|
+
};
|
|
88
|
+
return new InsertBuilder({
|
|
89
|
+
...this.node,
|
|
90
|
+
onConflict: conflict
|
|
91
|
+
}, this.paramIndex);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
mode(mode) {
|
|
95
|
+
return new InsertBuilder({
|
|
96
|
+
...this.node,
|
|
97
|
+
insertMode: mode
|
|
98
|
+
}, this.paramIndex);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
orIgnore() {
|
|
102
|
+
return this.mode("INSERT OR IGNORE");
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
orReplace() {
|
|
106
|
+
return this.mode("INSERT OR REPLACE");
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
onDuplicateKeyUpdate(set) {
|
|
110
|
+
return new InsertBuilder({
|
|
111
|
+
...this.node,
|
|
112
|
+
onDuplicateKeyUpdate: set
|
|
113
|
+
}, this.paramIndex);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
fromSelect(query) {
|
|
117
|
+
return new InsertBuilder({
|
|
118
|
+
...this.node,
|
|
119
|
+
source: query
|
|
120
|
+
}, this.paramIndex);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
defaultValues() {
|
|
124
|
+
return new InsertBuilder({
|
|
125
|
+
...this.node,
|
|
126
|
+
defaultValues: true
|
|
127
|
+
}, this.paramIndex);
|
|
128
|
+
}
|
|
129
|
+
with(name, query, recursive = false) {
|
|
130
|
+
const cte = {
|
|
131
|
+
name,
|
|
132
|
+
query,
|
|
133
|
+
recursive
|
|
134
|
+
};
|
|
135
|
+
return new InsertBuilder({
|
|
136
|
+
...this.node,
|
|
137
|
+
ctes: [...this.node.ctes, cte]
|
|
138
|
+
}, this.paramIndex);
|
|
139
|
+
}
|
|
140
|
+
build() {
|
|
141
|
+
return { ...this.node };
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
export function insert(table) {
|
|
145
|
+
return new InsertBuilder().into(table);
|
|
146
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ExpressionNode, MergeNode, MergeWhenMatched, MergeWhenNotMatched, SelectNode, SubqueryNode, TableRefNode } from "../ast/nodes.mjs";
|
|
2
|
+
export declare class MergeBuilder {
|
|
3
|
+
private node;
|
|
4
|
+
private paramIndex;
|
|
5
|
+
constructor(node?: MergeNode, paramIndex?: number);
|
|
6
|
+
into(table: string | TableRefNode): MergeBuilder;
|
|
7
|
+
using(source: string | TableRefNode | SubqueryNode, alias: string): MergeBuilder;
|
|
8
|
+
on(expr: ExpressionNode): MergeBuilder;
|
|
9
|
+
whenMatched(action: MergeWhenMatched): MergeBuilder;
|
|
10
|
+
whenNotMatched(action: MergeWhenNotMatched): MergeBuilder;
|
|
11
|
+
whenMatchedUpdate(set: {
|
|
12
|
+
column: string;
|
|
13
|
+
value: ExpressionNode;
|
|
14
|
+
}[], condition?: ExpressionNode): MergeBuilder;
|
|
15
|
+
whenMatchedDelete(condition?: ExpressionNode): MergeBuilder;
|
|
16
|
+
whenNotMatchedInsert(columns: string[], values: ExpressionNode[], condition?: ExpressionNode): MergeBuilder;
|
|
17
|
+
with(name: string, query: SelectNode, recursive?: boolean): MergeBuilder;
|
|
18
|
+
build(): MergeNode;
|
|
19
|
+
}
|
|
20
|
+
export declare function merge(target: string | TableRefNode): MergeBuilder;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { createMergeNode } from "../ast/nodes.mjs";
|
|
2
|
+
export class MergeBuilder {
|
|
3
|
+
node;
|
|
4
|
+
paramIndex;
|
|
5
|
+
constructor(node, paramIndex = 0) {
|
|
6
|
+
this.node = node ?? createMergeNode({
|
|
7
|
+
type: "table_ref",
|
|
8
|
+
name: ""
|
|
9
|
+
}, {
|
|
10
|
+
type: "table_ref",
|
|
11
|
+
name: ""
|
|
12
|
+
}, "src", {
|
|
13
|
+
type: "literal",
|
|
14
|
+
value: true
|
|
15
|
+
});
|
|
16
|
+
this.paramIndex = paramIndex;
|
|
17
|
+
}
|
|
18
|
+
into(table) {
|
|
19
|
+
const ref = typeof table === "string" ? {
|
|
20
|
+
type: "table_ref",
|
|
21
|
+
name: table
|
|
22
|
+
} : table;
|
|
23
|
+
return new MergeBuilder({
|
|
24
|
+
...this.node,
|
|
25
|
+
target: ref
|
|
26
|
+
}, this.paramIndex);
|
|
27
|
+
}
|
|
28
|
+
using(source, alias) {
|
|
29
|
+
const src = typeof source === "string" ? {
|
|
30
|
+
type: "table_ref",
|
|
31
|
+
name: source
|
|
32
|
+
} : source;
|
|
33
|
+
return new MergeBuilder({
|
|
34
|
+
...this.node,
|
|
35
|
+
source: src,
|
|
36
|
+
sourceAlias: alias
|
|
37
|
+
}, this.paramIndex);
|
|
38
|
+
}
|
|
39
|
+
on(expr) {
|
|
40
|
+
return new MergeBuilder({
|
|
41
|
+
...this.node,
|
|
42
|
+
on: expr
|
|
43
|
+
}, this.paramIndex);
|
|
44
|
+
}
|
|
45
|
+
whenMatched(action) {
|
|
46
|
+
return new MergeBuilder({
|
|
47
|
+
...this.node,
|
|
48
|
+
whens: [...this.node.whens, action]
|
|
49
|
+
}, this.paramIndex);
|
|
50
|
+
}
|
|
51
|
+
whenNotMatched(action) {
|
|
52
|
+
return new MergeBuilder({
|
|
53
|
+
...this.node,
|
|
54
|
+
whens: [...this.node.whens, action]
|
|
55
|
+
}, this.paramIndex);
|
|
56
|
+
}
|
|
57
|
+
whenMatchedUpdate(set, condition) {
|
|
58
|
+
const when = {
|
|
59
|
+
type: "matched",
|
|
60
|
+
action: "update",
|
|
61
|
+
set,
|
|
62
|
+
condition
|
|
63
|
+
};
|
|
64
|
+
return this.whenMatched(when);
|
|
65
|
+
}
|
|
66
|
+
whenMatchedDelete(condition) {
|
|
67
|
+
const when = {
|
|
68
|
+
type: "matched",
|
|
69
|
+
action: "delete",
|
|
70
|
+
condition
|
|
71
|
+
};
|
|
72
|
+
return this.whenMatched(when);
|
|
73
|
+
}
|
|
74
|
+
whenNotMatchedInsert(columns, values, condition) {
|
|
75
|
+
const when = {
|
|
76
|
+
type: "not_matched",
|
|
77
|
+
columns,
|
|
78
|
+
values,
|
|
79
|
+
condition
|
|
80
|
+
};
|
|
81
|
+
return this.whenNotMatched(when);
|
|
82
|
+
}
|
|
83
|
+
with(name, query, recursive = false) {
|
|
84
|
+
const cte = {
|
|
85
|
+
name,
|
|
86
|
+
query,
|
|
87
|
+
recursive
|
|
88
|
+
};
|
|
89
|
+
return new MergeBuilder({
|
|
90
|
+
...this.node,
|
|
91
|
+
ctes: [...this.node.ctes, cte]
|
|
92
|
+
}, this.paramIndex);
|
|
93
|
+
}
|
|
94
|
+
build() {
|
|
95
|
+
return { ...this.node };
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
export function merge(target) {
|
|
99
|
+
return new MergeBuilder().into(target);
|
|
100
|
+
}
|