@prisma-next/sql-relational-core 0.5.0-dev.4 → 0.5.0-dev.41
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 +67 -1
- package/dist/codec-types-DJEaWT36.d.mts +313 -0
- package/dist/codec-types-DJEaWT36.d.mts.map +1 -0
- package/dist/{errors-ChY_dHam.d.mts → errors-BRt5yHo9.d.mts} +2 -2
- package/dist/errors-BRt5yHo9.d.mts.map +1 -0
- package/dist/{errors-D3xmG4h-.mjs → errors-D6kqqjHM.mjs} +1 -1
- package/dist/{errors-D3xmG4h-.mjs.map → errors-D6kqqjHM.mjs.map} +1 -1
- package/dist/exports/ast.d.mts +27 -12
- package/dist/exports/ast.d.mts.map +1 -1
- package/dist/exports/ast.mjs +63 -1089
- package/dist/exports/ast.mjs.map +1 -1
- package/dist/exports/errors.d.mts +4 -4
- package/dist/exports/errors.mjs +1 -1
- package/dist/exports/expression.d.mts +79 -0
- package/dist/exports/expression.d.mts.map +1 -0
- package/dist/exports/expression.mjs +41 -0
- package/dist/exports/expression.mjs.map +1 -0
- package/dist/exports/plan.d.mts +3 -2
- package/dist/exports/plan.mjs +1 -17
- package/dist/exports/query-lane-context.d.mts +3 -3
- package/dist/exports/types.d.mts +5 -4
- package/dist/index.d.mts +11 -9
- package/dist/index.mjs +6 -4
- package/dist/plan-C7SiEWkN.d.mts +25 -0
- package/dist/plan-C7SiEWkN.d.mts.map +1 -0
- package/dist/{query-lane-context-UlR8vOkd.d.mts → query-lane-context-BF-wuc0r.d.mts} +53 -3
- package/dist/query-lane-context-BF-wuc0r.d.mts.map +1 -0
- package/dist/sql-execution-plan-Dgx7BGin.d.mts +33 -0
- package/dist/sql-execution-plan-Dgx7BGin.d.mts.map +1 -0
- package/dist/{types-C3Hg-CVz.d.mts → types-B4dL4lc3.d.mts} +17 -22
- package/dist/types-B4dL4lc3.d.mts.map +1 -0
- package/dist/types-BUlUvdIU.d.mts +24 -0
- package/dist/types-BUlUvdIU.d.mts.map +1 -0
- package/dist/{types-k9pir8XY.d.mts → types-BWOCTYd8.d.mts} +12 -19
- package/dist/types-BWOCTYd8.d.mts.map +1 -0
- package/dist/types-DUL-3vy6.mjs +1064 -0
- package/dist/types-DUL-3vy6.mjs.map +1 -0
- package/package.json +9 -8
- package/src/ast/adapter-types.ts +8 -0
- package/src/ast/codec-types.ts +251 -45
- package/src/ast/sql-codecs.ts +20 -3
- package/src/ast/types.ts +142 -172
- package/src/ast/util.ts +23 -0
- package/src/exports/expression.ts +1 -0
- package/src/exports/plan.ts +1 -0
- package/src/exports/types.ts +1 -0
- package/src/expression.ts +117 -0
- package/src/index.ts +1 -0
- package/src/plan.ts +11 -30
- package/src/query-lane-context.ts +52 -1
- package/src/runtime-scope.ts +20 -0
- package/src/sql-execution-plan.ts +28 -0
- package/src/types.ts +9 -22
- package/dist/codec-types-DcEITed4.d.mts +0 -144
- package/dist/codec-types-DcEITed4.d.mts.map +0 -1
- package/dist/errors-ChY_dHam.d.mts.map +0 -1
- package/dist/exports/plan.mjs.map +0 -1
- package/dist/plan-Cs65hb-E.d.mts +0 -28
- package/dist/plan-Cs65hb-E.d.mts.map +0 -1
- package/dist/query-lane-context-UlR8vOkd.d.mts.map +0 -1
- package/dist/types-C3Hg-CVz.d.mts.map +0 -1
- package/dist/types-k9pir8XY.d.mts.map +0 -1
package/dist/exports/ast.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { A as UpdateAst, C as OrExpr, D as SelectAst, E as ProjectionItem, M as isWhereExpr, N as queryAstKinds, O as SubqueryExpr, P as whereExprKinds, S as OperationExpr, T as ParamRef, _ as JsonObjectExpr, a as DefaultValueExpr, b as NotExpr, c as DoNothingConflictAction, d as ExistsExpr, f as IdentifierRef, g as JsonArrayAggExpr, h as JoinAst, i as ColumnRef, j as isQueryAst, k as TableSource, l as DoUpdateSetConflictAction, m as InsertOnConflict, n as AndExpr, o as DeleteAst, p as InsertAst, r as BinaryExpr, s as DerivedTableSource, t as AggregateExpr, u as EqColJoinOn, v as ListExpression, w as OrderByItem, x as NullCheckExpr, y as LiteralExpr } from "../types-DUL-3vy6.mjs";
|
|
1
2
|
import { ifDefined } from "@prisma-next/utils/defined";
|
|
2
3
|
import { type } from "arktype";
|
|
3
4
|
|
|
@@ -73,11 +74,25 @@ var CodecRegistryImpl = class {
|
|
|
73
74
|
}
|
|
74
75
|
};
|
|
75
76
|
/**
|
|
76
|
-
*
|
|
77
|
-
*
|
|
77
|
+
* Construct a SQL codec from author functions and optional metadata.
|
|
78
|
+
*
|
|
79
|
+
* Author `encode` and `decode` as sync or async functions; the factory
|
|
80
|
+
* produces a {@link Codec} whose query-time methods follow the boundary
|
|
81
|
+
* contract documented on `Codec`. Authors receive a second `ctx` options
|
|
82
|
+
* argument carrying the SQL-family per-call context; ignore it if you
|
|
83
|
+
* don't need it.
|
|
84
|
+
*
|
|
85
|
+
* Both `encode` and `decode` are required so `TInput` and `TWire` are
|
|
86
|
+
* always covered by an explicit author function — the factory installs
|
|
87
|
+
* no identity fallback. `encodeJson` and `decodeJson` default to identity
|
|
88
|
+
* **only when `TInput` is assignable to `JsonValue`**; otherwise both are
|
|
89
|
+
* required so the contract artifact stays JSON-safe.
|
|
78
90
|
*/
|
|
79
91
|
function codec(config) {
|
|
80
92
|
const identity = (v) => v;
|
|
93
|
+
const userEncode = config.encode;
|
|
94
|
+
const userDecode = config.decode;
|
|
95
|
+
const widenedConfig = config;
|
|
81
96
|
return {
|
|
82
97
|
id: config.typeId,
|
|
83
98
|
targetTypes: config.targetTypes,
|
|
@@ -86,10 +101,22 @@ function codec(config) {
|
|
|
86
101
|
...ifDefined("init", config.init),
|
|
87
102
|
...ifDefined("traits", config.traits ? Object.freeze([...config.traits]) : void 0),
|
|
88
103
|
...ifDefined("renderOutputType", config.renderOutputType),
|
|
89
|
-
encode:
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
104
|
+
encode: (value, ctx) => {
|
|
105
|
+
try {
|
|
106
|
+
return Promise.resolve(userEncode(value, ctx));
|
|
107
|
+
} catch (error) {
|
|
108
|
+
return Promise.reject(error);
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
decode: (wire, ctx) => {
|
|
112
|
+
try {
|
|
113
|
+
return Promise.resolve(userDecode(wire, ctx));
|
|
114
|
+
} catch (error) {
|
|
115
|
+
return Promise.reject(error);
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
encodeJson: widenedConfig.encodeJson ?? identity,
|
|
119
|
+
decodeJson: widenedConfig.decodeJson ?? identity
|
|
93
120
|
};
|
|
94
121
|
}
|
|
95
122
|
/**
|
|
@@ -244,8 +271,15 @@ const sqlTimestampCodec = codec({
|
|
|
244
271
|
typeId: SQL_TIMESTAMP_CODEC_ID,
|
|
245
272
|
targetTypes: ["timestamp"],
|
|
246
273
|
traits: ["equality", "order"],
|
|
247
|
-
encode: (value) => value
|
|
248
|
-
decode: (wire) => wire
|
|
274
|
+
encode: (value) => value,
|
|
275
|
+
decode: (wire) => wire,
|
|
276
|
+
encodeJson: (value) => value.toISOString(),
|
|
277
|
+
decodeJson: (json) => {
|
|
278
|
+
if (typeof json !== "string") throw new Error(`Expected ISO date string for sql/timestamp@1, got ${typeof json}`);
|
|
279
|
+
const date = new Date(json);
|
|
280
|
+
if (Number.isNaN(date.getTime())) throw new Error(`Invalid ISO date string for sql/timestamp@1: ${json}`);
|
|
281
|
+
return date;
|
|
282
|
+
},
|
|
249
283
|
paramsSchema: precisionParamsSchema,
|
|
250
284
|
renderOutputType: (typeParams) => {
|
|
251
285
|
const precision = typeParams["precision"];
|
|
@@ -258,1086 +292,6 @@ const codecs = defineCodecs().add("char", sqlCharCodec).add("varchar", sqlVarcha
|
|
|
258
292
|
const sqlCodecDefinitions = codecs.codecDefinitions;
|
|
259
293
|
const sqlDataTypes = codecs.dataTypes;
|
|
260
294
|
|
|
261
|
-
//#endregion
|
|
262
|
-
//#region src/ast/types.ts
|
|
263
|
-
function frozenArrayCopy(values) {
|
|
264
|
-
return Object.freeze([...values]);
|
|
265
|
-
}
|
|
266
|
-
function frozenOptionalRecordCopy(value) {
|
|
267
|
-
return value === void 0 ? void 0 : Object.freeze({ ...value });
|
|
268
|
-
}
|
|
269
|
-
function frozenRecordCopy(record) {
|
|
270
|
-
return Object.freeze({ ...record });
|
|
271
|
-
}
|
|
272
|
-
function freezeRows(rows) {
|
|
273
|
-
return Object.freeze(rows.map((row) => Object.freeze({ ...row })));
|
|
274
|
-
}
|
|
275
|
-
function combineAll(folder, thunks) {
|
|
276
|
-
let result = folder.empty;
|
|
277
|
-
for (const thunk of thunks) {
|
|
278
|
-
if (folder.isAbsorbing?.(result)) return result;
|
|
279
|
-
result = folder.combine(result, thunk());
|
|
280
|
-
}
|
|
281
|
-
return result;
|
|
282
|
-
}
|
|
283
|
-
function rewriteComparable(value, rewriter) {
|
|
284
|
-
switch (value.kind) {
|
|
285
|
-
case "param-ref": return rewriter.paramRef ? rewriter.paramRef(value) : value;
|
|
286
|
-
case "literal": return rewriter.literal ? rewriter.literal(value) : value;
|
|
287
|
-
case "list":
|
|
288
|
-
if (rewriter.list) return rewriter.list(value);
|
|
289
|
-
return value.rewrite(rewriter);
|
|
290
|
-
default: return value.rewrite(rewriter);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
function foldComparable(value, folder) {
|
|
294
|
-
switch (value.kind) {
|
|
295
|
-
case "param-ref": return folder.paramRef ? folder.paramRef(value) : folder.empty;
|
|
296
|
-
case "literal": return folder.literal ? folder.literal(value) : folder.empty;
|
|
297
|
-
case "list": return value.fold(folder);
|
|
298
|
-
default: return value.fold(folder);
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
function collectColumnRefsWith(node) {
|
|
302
|
-
return node.fold({
|
|
303
|
-
empty: [],
|
|
304
|
-
combine: (a, b) => [...a, ...b],
|
|
305
|
-
columnRef: (columnRef) => [columnRef],
|
|
306
|
-
select: (ast) => ast.collectColumnRefs()
|
|
307
|
-
});
|
|
308
|
-
}
|
|
309
|
-
function collectParamRefsWith(node) {
|
|
310
|
-
return node.fold({
|
|
311
|
-
empty: [],
|
|
312
|
-
combine: (a, b) => [...a, ...b],
|
|
313
|
-
paramRef: (paramRef) => [paramRef],
|
|
314
|
-
select: (ast) => ast.collectParamRefs()
|
|
315
|
-
});
|
|
316
|
-
}
|
|
317
|
-
function sortRefs(tables, columns) {
|
|
318
|
-
return {
|
|
319
|
-
tables: [...tables].sort((a, b) => a.localeCompare(b)),
|
|
320
|
-
columns: [...columns.values()].sort((a, b) => {
|
|
321
|
-
const tableCompare = a.table.localeCompare(b.table);
|
|
322
|
-
if (tableCompare !== 0) return tableCompare;
|
|
323
|
-
return a.column.localeCompare(b.column);
|
|
324
|
-
})
|
|
325
|
-
};
|
|
326
|
-
}
|
|
327
|
-
function addColumnRefToRefSets(columnRef, tables, columns) {
|
|
328
|
-
if (columnRef.table === "excluded") return;
|
|
329
|
-
tables.add(columnRef.table);
|
|
330
|
-
const key = `${columnRef.table}.${columnRef.column}`;
|
|
331
|
-
if (!columns.has(key)) columns.set(key, {
|
|
332
|
-
table: columnRef.table,
|
|
333
|
-
column: columnRef.column
|
|
334
|
-
});
|
|
335
|
-
}
|
|
336
|
-
function mergeRefsInto(refs, tables, columns) {
|
|
337
|
-
for (const table of refs.tables ?? []) tables.add(table);
|
|
338
|
-
for (const column of refs.columns ?? []) addColumnRefToRefSets(new ColumnRef(column.table, column.column), tables, columns);
|
|
339
|
-
}
|
|
340
|
-
var AstNode = class {
|
|
341
|
-
freeze() {
|
|
342
|
-
Object.freeze(this);
|
|
343
|
-
}
|
|
344
|
-
};
|
|
345
|
-
var QueryAst = class extends AstNode {
|
|
346
|
-
collectColumnRefs() {
|
|
347
|
-
return (this.collectRefs().columns ?? []).map((ref) => new ColumnRef(ref.table, ref.column));
|
|
348
|
-
}
|
|
349
|
-
};
|
|
350
|
-
var FromSource = class extends AstNode {};
|
|
351
|
-
var Expression = class extends AstNode {
|
|
352
|
-
collectColumnRefs() {
|
|
353
|
-
return collectColumnRefsWith(this);
|
|
354
|
-
}
|
|
355
|
-
collectParamRefs() {
|
|
356
|
-
return collectParamRefsWith(this);
|
|
357
|
-
}
|
|
358
|
-
baseColumnRef() {
|
|
359
|
-
throw new Error(`${this.constructor.name} does not expose a base column reference`);
|
|
360
|
-
}
|
|
361
|
-
toExpr() {
|
|
362
|
-
return this;
|
|
363
|
-
}
|
|
364
|
-
not() {
|
|
365
|
-
return new NotExpr(this);
|
|
366
|
-
}
|
|
367
|
-
};
|
|
368
|
-
var TableSource = class TableSource extends FromSource {
|
|
369
|
-
kind = "table-source";
|
|
370
|
-
name;
|
|
371
|
-
alias;
|
|
372
|
-
constructor(name, alias) {
|
|
373
|
-
super();
|
|
374
|
-
this.name = name;
|
|
375
|
-
this.alias = alias;
|
|
376
|
-
this.freeze();
|
|
377
|
-
}
|
|
378
|
-
static named(name, alias) {
|
|
379
|
-
return new TableSource(name, alias);
|
|
380
|
-
}
|
|
381
|
-
rewrite(rewriter) {
|
|
382
|
-
return rewriter.tableSource ? rewriter.tableSource(this) : this;
|
|
383
|
-
}
|
|
384
|
-
toFromSource() {
|
|
385
|
-
return this;
|
|
386
|
-
}
|
|
387
|
-
collectRefs() {
|
|
388
|
-
return {
|
|
389
|
-
tables: [this.name],
|
|
390
|
-
columns: []
|
|
391
|
-
};
|
|
392
|
-
}
|
|
393
|
-
};
|
|
394
|
-
var DerivedTableSource = class DerivedTableSource extends FromSource {
|
|
395
|
-
kind = "derived-table-source";
|
|
396
|
-
alias;
|
|
397
|
-
query;
|
|
398
|
-
constructor(alias, query) {
|
|
399
|
-
super();
|
|
400
|
-
this.alias = alias;
|
|
401
|
-
this.query = query;
|
|
402
|
-
this.freeze();
|
|
403
|
-
}
|
|
404
|
-
static as(alias, query) {
|
|
405
|
-
return new DerivedTableSource(alias, query);
|
|
406
|
-
}
|
|
407
|
-
rewrite(rewriter) {
|
|
408
|
-
return new DerivedTableSource(this.alias, this.query.rewrite(rewriter));
|
|
409
|
-
}
|
|
410
|
-
toFromSource() {
|
|
411
|
-
return this;
|
|
412
|
-
}
|
|
413
|
-
collectRefs() {
|
|
414
|
-
return this.query.collectRefs();
|
|
415
|
-
}
|
|
416
|
-
};
|
|
417
|
-
var ColumnRef = class ColumnRef extends Expression {
|
|
418
|
-
kind = "column-ref";
|
|
419
|
-
table;
|
|
420
|
-
column;
|
|
421
|
-
constructor(table, column) {
|
|
422
|
-
super();
|
|
423
|
-
this.table = table;
|
|
424
|
-
this.column = column;
|
|
425
|
-
this.freeze();
|
|
426
|
-
}
|
|
427
|
-
static of(table, column) {
|
|
428
|
-
return new ColumnRef(table, column);
|
|
429
|
-
}
|
|
430
|
-
accept(visitor) {
|
|
431
|
-
return visitor.columnRef(this);
|
|
432
|
-
}
|
|
433
|
-
rewrite(rewriter) {
|
|
434
|
-
return rewriter.columnRef ? rewriter.columnRef(this) : this;
|
|
435
|
-
}
|
|
436
|
-
fold(folder) {
|
|
437
|
-
return folder.columnRef ? folder.columnRef(this) : folder.empty;
|
|
438
|
-
}
|
|
439
|
-
baseColumnRef() {
|
|
440
|
-
return this;
|
|
441
|
-
}
|
|
442
|
-
};
|
|
443
|
-
var IdentifierRef = class IdentifierRef extends Expression {
|
|
444
|
-
kind = "identifier-ref";
|
|
445
|
-
name;
|
|
446
|
-
constructor(name) {
|
|
447
|
-
super();
|
|
448
|
-
this.name = name;
|
|
449
|
-
this.freeze();
|
|
450
|
-
}
|
|
451
|
-
static of(name) {
|
|
452
|
-
return new IdentifierRef(name);
|
|
453
|
-
}
|
|
454
|
-
accept(visitor) {
|
|
455
|
-
return visitor.identifierRef(this);
|
|
456
|
-
}
|
|
457
|
-
rewrite(rewriter) {
|
|
458
|
-
return rewriter.identifierRef ? rewriter.identifierRef(this) : this;
|
|
459
|
-
}
|
|
460
|
-
fold(folder) {
|
|
461
|
-
return folder.identifierRef ? folder.identifierRef(this) : folder.empty;
|
|
462
|
-
}
|
|
463
|
-
};
|
|
464
|
-
var ParamRef = class ParamRef extends Expression {
|
|
465
|
-
kind = "param-ref";
|
|
466
|
-
value;
|
|
467
|
-
name;
|
|
468
|
-
codecId;
|
|
469
|
-
constructor(value, options) {
|
|
470
|
-
super();
|
|
471
|
-
this.value = value;
|
|
472
|
-
this.name = options?.name;
|
|
473
|
-
this.codecId = options?.codecId;
|
|
474
|
-
this.freeze();
|
|
475
|
-
}
|
|
476
|
-
static of(value, options) {
|
|
477
|
-
return new ParamRef(value, options);
|
|
478
|
-
}
|
|
479
|
-
accept(visitor) {
|
|
480
|
-
return visitor.param(this);
|
|
481
|
-
}
|
|
482
|
-
rewrite(rewriter) {
|
|
483
|
-
return rewriter.paramRef ? rewriter.paramRef(this) : this;
|
|
484
|
-
}
|
|
485
|
-
fold(folder) {
|
|
486
|
-
return folder.paramRef ? folder.paramRef(this) : folder.empty;
|
|
487
|
-
}
|
|
488
|
-
};
|
|
489
|
-
var DefaultValueExpr = class extends AstNode {
|
|
490
|
-
kind = "default-value";
|
|
491
|
-
constructor() {
|
|
492
|
-
super();
|
|
493
|
-
this.freeze();
|
|
494
|
-
}
|
|
495
|
-
};
|
|
496
|
-
var LiteralExpr = class LiteralExpr extends Expression {
|
|
497
|
-
kind = "literal";
|
|
498
|
-
value;
|
|
499
|
-
constructor(value) {
|
|
500
|
-
super();
|
|
501
|
-
this.value = value;
|
|
502
|
-
this.freeze();
|
|
503
|
-
}
|
|
504
|
-
static of(value) {
|
|
505
|
-
return new LiteralExpr(value);
|
|
506
|
-
}
|
|
507
|
-
accept(visitor) {
|
|
508
|
-
return visitor.literal(this);
|
|
509
|
-
}
|
|
510
|
-
rewrite(rewriter) {
|
|
511
|
-
return rewriter.literal ? rewriter.literal(this) : this;
|
|
512
|
-
}
|
|
513
|
-
fold(folder) {
|
|
514
|
-
return folder.literal ? folder.literal(this) : folder.empty;
|
|
515
|
-
}
|
|
516
|
-
};
|
|
517
|
-
var SubqueryExpr = class SubqueryExpr extends Expression {
|
|
518
|
-
kind = "subquery";
|
|
519
|
-
query;
|
|
520
|
-
constructor(query) {
|
|
521
|
-
super();
|
|
522
|
-
this.query = query;
|
|
523
|
-
this.freeze();
|
|
524
|
-
}
|
|
525
|
-
static of(query) {
|
|
526
|
-
return new SubqueryExpr(query);
|
|
527
|
-
}
|
|
528
|
-
accept(visitor) {
|
|
529
|
-
return visitor.subquery(this);
|
|
530
|
-
}
|
|
531
|
-
rewrite(rewriter) {
|
|
532
|
-
return new SubqueryExpr(this.query.rewrite(rewriter));
|
|
533
|
-
}
|
|
534
|
-
fold(folder) {
|
|
535
|
-
return folder.select ? folder.select(this.query) : folder.empty;
|
|
536
|
-
}
|
|
537
|
-
};
|
|
538
|
-
var OperationExpr = class OperationExpr extends Expression {
|
|
539
|
-
kind = "operation";
|
|
540
|
-
method;
|
|
541
|
-
self;
|
|
542
|
-
args;
|
|
543
|
-
returns;
|
|
544
|
-
lowering;
|
|
545
|
-
constructor(options) {
|
|
546
|
-
super();
|
|
547
|
-
this.method = options.method;
|
|
548
|
-
this.self = options.self;
|
|
549
|
-
this.args = frozenArrayCopy(options.args ?? []);
|
|
550
|
-
this.returns = options.returns;
|
|
551
|
-
this.lowering = options.lowering;
|
|
552
|
-
this.freeze();
|
|
553
|
-
}
|
|
554
|
-
accept(visitor) {
|
|
555
|
-
return visitor.operation(this);
|
|
556
|
-
}
|
|
557
|
-
rewrite(rewriter) {
|
|
558
|
-
return new OperationExpr({
|
|
559
|
-
method: this.method,
|
|
560
|
-
self: this.self.rewrite(rewriter),
|
|
561
|
-
args: this.args.map((arg) => rewriteComparable(arg, rewriter)),
|
|
562
|
-
returns: this.returns,
|
|
563
|
-
lowering: this.lowering
|
|
564
|
-
});
|
|
565
|
-
}
|
|
566
|
-
fold(folder) {
|
|
567
|
-
return combineAll(folder, [() => this.self.fold(folder), ...this.args.map((arg) => () => foldComparable(arg, folder))]);
|
|
568
|
-
}
|
|
569
|
-
baseColumnRef() {
|
|
570
|
-
return this.self.baseColumnRef();
|
|
571
|
-
}
|
|
572
|
-
};
|
|
573
|
-
var AggregateExpr = class AggregateExpr extends Expression {
|
|
574
|
-
kind = "aggregate";
|
|
575
|
-
fn;
|
|
576
|
-
expr;
|
|
577
|
-
constructor(fn, expr) {
|
|
578
|
-
super();
|
|
579
|
-
if (fn !== "count" && expr === void 0) throw new Error(`Aggregate function "${fn}" requires an expression`);
|
|
580
|
-
this.fn = fn;
|
|
581
|
-
this.expr = expr;
|
|
582
|
-
this.freeze();
|
|
583
|
-
}
|
|
584
|
-
static count(expr) {
|
|
585
|
-
return new AggregateExpr("count", expr);
|
|
586
|
-
}
|
|
587
|
-
static sum(expr) {
|
|
588
|
-
return new AggregateExpr("sum", expr);
|
|
589
|
-
}
|
|
590
|
-
static avg(expr) {
|
|
591
|
-
return new AggregateExpr("avg", expr);
|
|
592
|
-
}
|
|
593
|
-
static min(expr) {
|
|
594
|
-
return new AggregateExpr("min", expr);
|
|
595
|
-
}
|
|
596
|
-
static max(expr) {
|
|
597
|
-
return new AggregateExpr("max", expr);
|
|
598
|
-
}
|
|
599
|
-
accept(visitor) {
|
|
600
|
-
return visitor.aggregate(this);
|
|
601
|
-
}
|
|
602
|
-
rewrite(rewriter) {
|
|
603
|
-
return this.expr === void 0 ? this : new AggregateExpr(this.fn, this.expr.rewrite(rewriter));
|
|
604
|
-
}
|
|
605
|
-
fold(folder) {
|
|
606
|
-
return this.expr ? this.expr.fold(folder) : folder.empty;
|
|
607
|
-
}
|
|
608
|
-
};
|
|
609
|
-
var JsonObjectExpr = class JsonObjectExpr extends Expression {
|
|
610
|
-
kind = "json-object";
|
|
611
|
-
entries;
|
|
612
|
-
constructor(entries) {
|
|
613
|
-
super();
|
|
614
|
-
this.entries = frozenArrayCopy(entries.map((entry) => Object.freeze({ ...entry })));
|
|
615
|
-
this.freeze();
|
|
616
|
-
}
|
|
617
|
-
static entry(key, value) {
|
|
618
|
-
return {
|
|
619
|
-
key,
|
|
620
|
-
value
|
|
621
|
-
};
|
|
622
|
-
}
|
|
623
|
-
static fromEntries(entries) {
|
|
624
|
-
return new JsonObjectExpr(entries);
|
|
625
|
-
}
|
|
626
|
-
accept(visitor) {
|
|
627
|
-
return visitor.jsonObject(this);
|
|
628
|
-
}
|
|
629
|
-
rewrite(rewriter) {
|
|
630
|
-
return new JsonObjectExpr(this.entries.map((entry) => ({
|
|
631
|
-
key: entry.key,
|
|
632
|
-
value: entry.value.kind === "literal" ? rewriter.literal ? rewriter.literal(entry.value) : entry.value : entry.value.rewrite(rewriter)
|
|
633
|
-
})));
|
|
634
|
-
}
|
|
635
|
-
fold(folder) {
|
|
636
|
-
return combineAll(folder, this.entries.map((entry) => () => entry.value.kind === "literal" ? folder.literal ? folder.literal(entry.value) : folder.empty : entry.value.fold(folder)));
|
|
637
|
-
}
|
|
638
|
-
};
|
|
639
|
-
var OrderByItem = class OrderByItem extends AstNode {
|
|
640
|
-
kind = "order-by-item";
|
|
641
|
-
expr;
|
|
642
|
-
dir;
|
|
643
|
-
constructor(expr, dir) {
|
|
644
|
-
super();
|
|
645
|
-
this.expr = expr;
|
|
646
|
-
this.dir = dir;
|
|
647
|
-
this.freeze();
|
|
648
|
-
}
|
|
649
|
-
static asc(expr) {
|
|
650
|
-
return new OrderByItem(expr, "asc");
|
|
651
|
-
}
|
|
652
|
-
static desc(expr) {
|
|
653
|
-
return new OrderByItem(expr, "desc");
|
|
654
|
-
}
|
|
655
|
-
rewrite(rewriter) {
|
|
656
|
-
return new OrderByItem(this.expr.rewrite(rewriter), this.dir);
|
|
657
|
-
}
|
|
658
|
-
};
|
|
659
|
-
var JsonArrayAggExpr = class JsonArrayAggExpr extends Expression {
|
|
660
|
-
kind = "json-array-agg";
|
|
661
|
-
expr;
|
|
662
|
-
onEmpty;
|
|
663
|
-
orderBy;
|
|
664
|
-
constructor(expr, onEmpty = "null", orderBy) {
|
|
665
|
-
super();
|
|
666
|
-
this.expr = expr;
|
|
667
|
-
this.onEmpty = onEmpty;
|
|
668
|
-
this.orderBy = orderBy && orderBy.length > 0 ? frozenArrayCopy(orderBy) : void 0;
|
|
669
|
-
this.freeze();
|
|
670
|
-
}
|
|
671
|
-
static of(expr, onEmpty = "null", orderBy) {
|
|
672
|
-
return new JsonArrayAggExpr(expr, onEmpty, orderBy);
|
|
673
|
-
}
|
|
674
|
-
accept(visitor) {
|
|
675
|
-
return visitor.jsonArrayAgg(this);
|
|
676
|
-
}
|
|
677
|
-
rewrite(rewriter) {
|
|
678
|
-
return new JsonArrayAggExpr(this.expr.rewrite(rewriter), this.onEmpty, this.orderBy?.map((orderItem) => orderItem.rewrite(rewriter)));
|
|
679
|
-
}
|
|
680
|
-
fold(folder) {
|
|
681
|
-
return combineAll(folder, [() => this.expr.fold(folder), ...(this.orderBy ?? []).map((orderItem) => () => orderItem.expr.fold(folder))]);
|
|
682
|
-
}
|
|
683
|
-
};
|
|
684
|
-
var ListExpression = class ListExpression extends Expression {
|
|
685
|
-
kind = "list";
|
|
686
|
-
values;
|
|
687
|
-
constructor(values) {
|
|
688
|
-
super();
|
|
689
|
-
this.values = frozenArrayCopy(values);
|
|
690
|
-
this.freeze();
|
|
691
|
-
}
|
|
692
|
-
static of(values) {
|
|
693
|
-
return new ListExpression(values);
|
|
694
|
-
}
|
|
695
|
-
static fromValues(values) {
|
|
696
|
-
return new ListExpression(values.map((value) => new LiteralExpr(value)));
|
|
697
|
-
}
|
|
698
|
-
accept(visitor) {
|
|
699
|
-
return visitor.list(this);
|
|
700
|
-
}
|
|
701
|
-
rewrite(rewriter) {
|
|
702
|
-
if (rewriter.list) return rewriter.list(this);
|
|
703
|
-
return new ListExpression(this.values.map((value) => value.rewrite(rewriter)));
|
|
704
|
-
}
|
|
705
|
-
fold(folder) {
|
|
706
|
-
if (folder.list) return folder.list(this);
|
|
707
|
-
return combineAll(folder, this.values.map((value) => () => value.fold(folder)));
|
|
708
|
-
}
|
|
709
|
-
};
|
|
710
|
-
var BinaryExpr = class BinaryExpr extends Expression {
|
|
711
|
-
kind = "binary";
|
|
712
|
-
op;
|
|
713
|
-
left;
|
|
714
|
-
right;
|
|
715
|
-
constructor(op, left, right) {
|
|
716
|
-
super();
|
|
717
|
-
this.op = op;
|
|
718
|
-
this.left = left;
|
|
719
|
-
this.right = right;
|
|
720
|
-
this.freeze();
|
|
721
|
-
}
|
|
722
|
-
static eq(left, right) {
|
|
723
|
-
return new BinaryExpr("eq", left, right);
|
|
724
|
-
}
|
|
725
|
-
static neq(left, right) {
|
|
726
|
-
return new BinaryExpr("neq", left, right);
|
|
727
|
-
}
|
|
728
|
-
static gt(left, right) {
|
|
729
|
-
return new BinaryExpr("gt", left, right);
|
|
730
|
-
}
|
|
731
|
-
static lt(left, right) {
|
|
732
|
-
return new BinaryExpr("lt", left, right);
|
|
733
|
-
}
|
|
734
|
-
static gte(left, right) {
|
|
735
|
-
return new BinaryExpr("gte", left, right);
|
|
736
|
-
}
|
|
737
|
-
static lte(left, right) {
|
|
738
|
-
return new BinaryExpr("lte", left, right);
|
|
739
|
-
}
|
|
740
|
-
static like(left, right) {
|
|
741
|
-
return new BinaryExpr("like", left, right);
|
|
742
|
-
}
|
|
743
|
-
static in(left, right) {
|
|
744
|
-
return new BinaryExpr("in", left, right);
|
|
745
|
-
}
|
|
746
|
-
static notIn(left, right) {
|
|
747
|
-
return new BinaryExpr("notIn", left, right);
|
|
748
|
-
}
|
|
749
|
-
accept(visitor) {
|
|
750
|
-
return visitor.binary(this);
|
|
751
|
-
}
|
|
752
|
-
rewrite(rewriter) {
|
|
753
|
-
return new BinaryExpr(this.op, rewriteComparable(this.left, rewriter), rewriteComparable(this.right, rewriter));
|
|
754
|
-
}
|
|
755
|
-
fold(folder) {
|
|
756
|
-
return combineAll(folder, [() => foldComparable(this.left, folder), () => foldComparable(this.right, folder)]);
|
|
757
|
-
}
|
|
758
|
-
};
|
|
759
|
-
var AndExpr = class AndExpr extends Expression {
|
|
760
|
-
kind = "and";
|
|
761
|
-
exprs;
|
|
762
|
-
constructor(exprs) {
|
|
763
|
-
super();
|
|
764
|
-
this.exprs = frozenArrayCopy(exprs);
|
|
765
|
-
this.freeze();
|
|
766
|
-
}
|
|
767
|
-
static of(exprs) {
|
|
768
|
-
return new AndExpr(exprs);
|
|
769
|
-
}
|
|
770
|
-
static true() {
|
|
771
|
-
return new AndExpr([]);
|
|
772
|
-
}
|
|
773
|
-
accept(visitor) {
|
|
774
|
-
return visitor.and(this);
|
|
775
|
-
}
|
|
776
|
-
rewrite(rewriter) {
|
|
777
|
-
return new AndExpr(this.exprs.map((expr) => expr.rewrite(rewriter)));
|
|
778
|
-
}
|
|
779
|
-
fold(folder) {
|
|
780
|
-
return combineAll(folder, this.exprs.map((expr) => () => expr.fold(folder)));
|
|
781
|
-
}
|
|
782
|
-
};
|
|
783
|
-
var OrExpr = class OrExpr extends Expression {
|
|
784
|
-
kind = "or";
|
|
785
|
-
exprs;
|
|
786
|
-
constructor(exprs) {
|
|
787
|
-
super();
|
|
788
|
-
this.exprs = frozenArrayCopy(exprs);
|
|
789
|
-
this.freeze();
|
|
790
|
-
}
|
|
791
|
-
static of(exprs) {
|
|
792
|
-
return new OrExpr(exprs);
|
|
793
|
-
}
|
|
794
|
-
static false() {
|
|
795
|
-
return new OrExpr([]);
|
|
796
|
-
}
|
|
797
|
-
accept(visitor) {
|
|
798
|
-
return visitor.or(this);
|
|
799
|
-
}
|
|
800
|
-
rewrite(rewriter) {
|
|
801
|
-
return new OrExpr(this.exprs.map((expr) => expr.rewrite(rewriter)));
|
|
802
|
-
}
|
|
803
|
-
fold(folder) {
|
|
804
|
-
return combineAll(folder, this.exprs.map((expr) => () => expr.fold(folder)));
|
|
805
|
-
}
|
|
806
|
-
};
|
|
807
|
-
var ExistsExpr = class ExistsExpr extends Expression {
|
|
808
|
-
kind = "exists";
|
|
809
|
-
notExists;
|
|
810
|
-
subquery;
|
|
811
|
-
constructor(subquery, notExists = false) {
|
|
812
|
-
super();
|
|
813
|
-
this.notExists = notExists;
|
|
814
|
-
this.subquery = subquery;
|
|
815
|
-
this.freeze();
|
|
816
|
-
}
|
|
817
|
-
static exists(subquery) {
|
|
818
|
-
return new ExistsExpr(subquery, false);
|
|
819
|
-
}
|
|
820
|
-
static notExists(subquery) {
|
|
821
|
-
return new ExistsExpr(subquery, true);
|
|
822
|
-
}
|
|
823
|
-
accept(visitor) {
|
|
824
|
-
return visitor.exists(this);
|
|
825
|
-
}
|
|
826
|
-
rewrite(rewriter) {
|
|
827
|
-
return new ExistsExpr(this.subquery.rewrite(rewriter), this.notExists);
|
|
828
|
-
}
|
|
829
|
-
fold(folder) {
|
|
830
|
-
return folder.select ? folder.select(this.subquery) : folder.empty;
|
|
831
|
-
}
|
|
832
|
-
};
|
|
833
|
-
var NullCheckExpr = class NullCheckExpr extends Expression {
|
|
834
|
-
kind = "null-check";
|
|
835
|
-
expr;
|
|
836
|
-
isNull;
|
|
837
|
-
constructor(expr, isNull) {
|
|
838
|
-
super();
|
|
839
|
-
this.expr = expr;
|
|
840
|
-
this.isNull = isNull;
|
|
841
|
-
this.freeze();
|
|
842
|
-
}
|
|
843
|
-
static isNull(expr) {
|
|
844
|
-
return new NullCheckExpr(expr, true);
|
|
845
|
-
}
|
|
846
|
-
static isNotNull(expr) {
|
|
847
|
-
return new NullCheckExpr(expr, false);
|
|
848
|
-
}
|
|
849
|
-
accept(visitor) {
|
|
850
|
-
return visitor.nullCheck(this);
|
|
851
|
-
}
|
|
852
|
-
rewrite(rewriter) {
|
|
853
|
-
return new NullCheckExpr(this.expr.rewrite(rewriter), this.isNull);
|
|
854
|
-
}
|
|
855
|
-
fold(folder) {
|
|
856
|
-
return this.expr.fold(folder);
|
|
857
|
-
}
|
|
858
|
-
};
|
|
859
|
-
var NotExpr = class NotExpr extends Expression {
|
|
860
|
-
kind = "not";
|
|
861
|
-
expr;
|
|
862
|
-
constructor(expr) {
|
|
863
|
-
super();
|
|
864
|
-
this.expr = expr;
|
|
865
|
-
this.freeze();
|
|
866
|
-
}
|
|
867
|
-
toWhereExpr() {
|
|
868
|
-
return this;
|
|
869
|
-
}
|
|
870
|
-
accept(visitor) {
|
|
871
|
-
return visitor.not(this);
|
|
872
|
-
}
|
|
873
|
-
rewrite(rewriter) {
|
|
874
|
-
return new NotExpr(this.expr.rewrite(rewriter));
|
|
875
|
-
}
|
|
876
|
-
fold(folder) {
|
|
877
|
-
return this.expr.fold(folder);
|
|
878
|
-
}
|
|
879
|
-
};
|
|
880
|
-
var EqColJoinOn = class EqColJoinOn extends AstNode {
|
|
881
|
-
kind = "eq-col-join-on";
|
|
882
|
-
left;
|
|
883
|
-
right;
|
|
884
|
-
constructor(left, right) {
|
|
885
|
-
super();
|
|
886
|
-
this.left = left;
|
|
887
|
-
this.right = right;
|
|
888
|
-
this.freeze();
|
|
889
|
-
}
|
|
890
|
-
static of(left, right) {
|
|
891
|
-
return new EqColJoinOn(left, right);
|
|
892
|
-
}
|
|
893
|
-
rewrite(rewriter) {
|
|
894
|
-
return rewriter.eqColJoinOn ? rewriter.eqColJoinOn(this) : this;
|
|
895
|
-
}
|
|
896
|
-
};
|
|
897
|
-
var JoinAst = class JoinAst extends AstNode {
|
|
898
|
-
kind = "join";
|
|
899
|
-
joinType;
|
|
900
|
-
source;
|
|
901
|
-
lateral;
|
|
902
|
-
on;
|
|
903
|
-
constructor(joinType, source, on, lateral = false) {
|
|
904
|
-
super();
|
|
905
|
-
this.joinType = joinType;
|
|
906
|
-
this.source = source;
|
|
907
|
-
this.lateral = lateral;
|
|
908
|
-
this.on = on;
|
|
909
|
-
this.freeze();
|
|
910
|
-
}
|
|
911
|
-
static inner(source, on, lateral = false) {
|
|
912
|
-
return new JoinAst("inner", source, on, lateral);
|
|
913
|
-
}
|
|
914
|
-
static left(source, on, lateral = false) {
|
|
915
|
-
return new JoinAst("left", source, on, lateral);
|
|
916
|
-
}
|
|
917
|
-
static right(source, on, lateral = false) {
|
|
918
|
-
return new JoinAst("right", source, on, lateral);
|
|
919
|
-
}
|
|
920
|
-
static full(source, on, lateral = false) {
|
|
921
|
-
return new JoinAst("full", source, on, lateral);
|
|
922
|
-
}
|
|
923
|
-
rewrite(rewriter) {
|
|
924
|
-
return new JoinAst(this.joinType, this.source.rewrite(rewriter), this.on.kind === "eq-col-join-on" ? this.on.rewrite(rewriter) : this.on.rewrite(rewriter), this.lateral);
|
|
925
|
-
}
|
|
926
|
-
};
|
|
927
|
-
var ProjectionItem = class ProjectionItem extends AstNode {
|
|
928
|
-
kind = "projection-item";
|
|
929
|
-
alias;
|
|
930
|
-
expr;
|
|
931
|
-
constructor(alias, expr) {
|
|
932
|
-
super();
|
|
933
|
-
this.alias = alias;
|
|
934
|
-
this.expr = expr;
|
|
935
|
-
this.freeze();
|
|
936
|
-
}
|
|
937
|
-
static of(alias, expr) {
|
|
938
|
-
return new ProjectionItem(alias, expr);
|
|
939
|
-
}
|
|
940
|
-
};
|
|
941
|
-
var SelectAst = class SelectAst extends QueryAst {
|
|
942
|
-
kind = "select";
|
|
943
|
-
from;
|
|
944
|
-
joins;
|
|
945
|
-
projection;
|
|
946
|
-
where;
|
|
947
|
-
orderBy;
|
|
948
|
-
distinct;
|
|
949
|
-
distinctOn;
|
|
950
|
-
groupBy;
|
|
951
|
-
having;
|
|
952
|
-
limit;
|
|
953
|
-
offset;
|
|
954
|
-
selectAllIntent;
|
|
955
|
-
constructor(options) {
|
|
956
|
-
super();
|
|
957
|
-
this.from = options.from;
|
|
958
|
-
this.joins = options.joins && options.joins.length > 0 ? frozenArrayCopy(options.joins) : void 0;
|
|
959
|
-
this.projection = frozenArrayCopy(options.projection);
|
|
960
|
-
this.where = options.where;
|
|
961
|
-
this.orderBy = options.orderBy && options.orderBy.length > 0 ? frozenArrayCopy(options.orderBy) : void 0;
|
|
962
|
-
this.distinct = options.distinct;
|
|
963
|
-
this.distinctOn = options.distinctOn && options.distinctOn.length > 0 ? frozenArrayCopy(options.distinctOn) : void 0;
|
|
964
|
-
this.groupBy = options.groupBy && options.groupBy.length > 0 ? frozenArrayCopy(options.groupBy) : void 0;
|
|
965
|
-
this.having = options.having;
|
|
966
|
-
this.limit = options.limit;
|
|
967
|
-
this.offset = options.offset;
|
|
968
|
-
this.selectAllIntent = frozenOptionalRecordCopy(options.selectAllIntent);
|
|
969
|
-
this.freeze();
|
|
970
|
-
}
|
|
971
|
-
static from(from) {
|
|
972
|
-
return new SelectAst({
|
|
973
|
-
from,
|
|
974
|
-
joins: void 0,
|
|
975
|
-
projection: [],
|
|
976
|
-
where: void 0,
|
|
977
|
-
orderBy: void 0,
|
|
978
|
-
distinct: void 0,
|
|
979
|
-
distinctOn: void 0,
|
|
980
|
-
groupBy: void 0,
|
|
981
|
-
having: void 0,
|
|
982
|
-
limit: void 0,
|
|
983
|
-
offset: void 0,
|
|
984
|
-
selectAllIntent: void 0
|
|
985
|
-
});
|
|
986
|
-
}
|
|
987
|
-
withFrom(from) {
|
|
988
|
-
return new SelectAst({
|
|
989
|
-
...this,
|
|
990
|
-
from
|
|
991
|
-
});
|
|
992
|
-
}
|
|
993
|
-
withJoins(joins) {
|
|
994
|
-
return new SelectAst({
|
|
995
|
-
...this,
|
|
996
|
-
joins: joins.length > 0 ? joins : void 0
|
|
997
|
-
});
|
|
998
|
-
}
|
|
999
|
-
withProjection(projection) {
|
|
1000
|
-
return new SelectAst({
|
|
1001
|
-
...this,
|
|
1002
|
-
projection
|
|
1003
|
-
});
|
|
1004
|
-
}
|
|
1005
|
-
addProjection(alias, expr) {
|
|
1006
|
-
return new SelectAst({
|
|
1007
|
-
...this,
|
|
1008
|
-
projection: [...this.projection, new ProjectionItem(alias, expr)]
|
|
1009
|
-
});
|
|
1010
|
-
}
|
|
1011
|
-
withWhere(where) {
|
|
1012
|
-
return new SelectAst({
|
|
1013
|
-
...this,
|
|
1014
|
-
where
|
|
1015
|
-
});
|
|
1016
|
-
}
|
|
1017
|
-
withOrderBy(orderBy) {
|
|
1018
|
-
return new SelectAst({
|
|
1019
|
-
...this,
|
|
1020
|
-
orderBy: orderBy.length > 0 ? orderBy : void 0
|
|
1021
|
-
});
|
|
1022
|
-
}
|
|
1023
|
-
withDistinct(enabled = true) {
|
|
1024
|
-
return new SelectAst({
|
|
1025
|
-
...this,
|
|
1026
|
-
distinct: enabled ? true : void 0
|
|
1027
|
-
});
|
|
1028
|
-
}
|
|
1029
|
-
withDistinctOn(distinctOn) {
|
|
1030
|
-
return new SelectAst({
|
|
1031
|
-
...this,
|
|
1032
|
-
distinctOn: distinctOn.length > 0 ? distinctOn : void 0
|
|
1033
|
-
});
|
|
1034
|
-
}
|
|
1035
|
-
withGroupBy(groupBy) {
|
|
1036
|
-
return new SelectAst({
|
|
1037
|
-
...this,
|
|
1038
|
-
groupBy: groupBy.length > 0 ? groupBy : void 0
|
|
1039
|
-
});
|
|
1040
|
-
}
|
|
1041
|
-
withHaving(having) {
|
|
1042
|
-
return new SelectAst({
|
|
1043
|
-
...this,
|
|
1044
|
-
having
|
|
1045
|
-
});
|
|
1046
|
-
}
|
|
1047
|
-
withLimit(limit) {
|
|
1048
|
-
return new SelectAst({
|
|
1049
|
-
...this,
|
|
1050
|
-
limit
|
|
1051
|
-
});
|
|
1052
|
-
}
|
|
1053
|
-
withOffset(offset) {
|
|
1054
|
-
return new SelectAst({
|
|
1055
|
-
...this,
|
|
1056
|
-
offset
|
|
1057
|
-
});
|
|
1058
|
-
}
|
|
1059
|
-
withSelectAllIntent(selectAllIntent) {
|
|
1060
|
-
return new SelectAst({
|
|
1061
|
-
...this,
|
|
1062
|
-
selectAllIntent
|
|
1063
|
-
});
|
|
1064
|
-
}
|
|
1065
|
-
rewrite(rewriter) {
|
|
1066
|
-
const rewritten = new SelectAst({
|
|
1067
|
-
from: this.from.rewrite(rewriter),
|
|
1068
|
-
joins: this.joins?.map((join) => join.rewrite(rewriter)),
|
|
1069
|
-
projection: this.projection.map((projection) => new ProjectionItem(projection.alias, projection.expr.kind === "literal" ? rewriter.literal ? rewriter.literal(projection.expr) : projection.expr : projection.expr.rewrite(rewriter))),
|
|
1070
|
-
where: this.where?.rewrite(rewriter),
|
|
1071
|
-
orderBy: this.orderBy?.map((orderItem) => orderItem.rewrite(rewriter)),
|
|
1072
|
-
distinct: this.distinct,
|
|
1073
|
-
distinctOn: this.distinctOn?.map((expr) => expr.rewrite(rewriter)),
|
|
1074
|
-
groupBy: this.groupBy?.map((expr) => expr.rewrite(rewriter)),
|
|
1075
|
-
having: this.having?.rewrite(rewriter),
|
|
1076
|
-
limit: this.limit,
|
|
1077
|
-
offset: this.offset,
|
|
1078
|
-
selectAllIntent: this.selectAllIntent
|
|
1079
|
-
});
|
|
1080
|
-
return rewriter.select ? rewriter.select(rewritten) : rewritten;
|
|
1081
|
-
}
|
|
1082
|
-
collectColumnRefs() {
|
|
1083
|
-
const refs = [];
|
|
1084
|
-
const pushRefs = (columns) => {
|
|
1085
|
-
refs.push(...columns);
|
|
1086
|
-
};
|
|
1087
|
-
if (this.from.kind === "derived-table-source") pushRefs(this.from.query.collectColumnRefs());
|
|
1088
|
-
for (const projection of this.projection) if (!(projection.expr.kind === "literal")) pushRefs(projection.expr.collectColumnRefs());
|
|
1089
|
-
if (this.where) pushRefs(this.where.collectColumnRefs());
|
|
1090
|
-
if (this.having) pushRefs(this.having.collectColumnRefs());
|
|
1091
|
-
for (const orderItem of this.orderBy ?? []) pushRefs(orderItem.expr.collectColumnRefs());
|
|
1092
|
-
for (const expr of this.distinctOn ?? []) pushRefs(expr.collectColumnRefs());
|
|
1093
|
-
for (const expr of this.groupBy ?? []) pushRefs(expr.collectColumnRefs());
|
|
1094
|
-
for (const join of this.joins ?? []) {
|
|
1095
|
-
if (join.source.kind === "derived-table-source") pushRefs(join.source.query.collectColumnRefs());
|
|
1096
|
-
if (join.on.kind === "eq-col-join-on") refs.push(join.on.left, join.on.right);
|
|
1097
|
-
else pushRefs(join.on.collectColumnRefs());
|
|
1098
|
-
}
|
|
1099
|
-
return refs;
|
|
1100
|
-
}
|
|
1101
|
-
collectParamRefs() {
|
|
1102
|
-
const refs = [];
|
|
1103
|
-
const pushRefs = (params) => {
|
|
1104
|
-
refs.push(...params);
|
|
1105
|
-
};
|
|
1106
|
-
if (this.from.kind === "derived-table-source") pushRefs(this.from.query.collectParamRefs());
|
|
1107
|
-
for (const projection of this.projection) if (!(projection.expr.kind === "literal")) pushRefs(projection.expr.collectParamRefs());
|
|
1108
|
-
if (this.where) pushRefs(this.where.collectParamRefs());
|
|
1109
|
-
if (this.having) pushRefs(this.having.collectParamRefs());
|
|
1110
|
-
for (const orderItem of this.orderBy ?? []) pushRefs(orderItem.expr.collectParamRefs());
|
|
1111
|
-
for (const expr of this.distinctOn ?? []) pushRefs(expr.collectParamRefs());
|
|
1112
|
-
for (const expr of this.groupBy ?? []) pushRefs(expr.collectParamRefs());
|
|
1113
|
-
for (const join of this.joins ?? []) {
|
|
1114
|
-
if (join.source.kind === "derived-table-source") pushRefs(join.source.query.collectParamRefs());
|
|
1115
|
-
if (!(join.on.kind === "eq-col-join-on")) pushRefs(join.on.collectParamRefs());
|
|
1116
|
-
}
|
|
1117
|
-
return refs;
|
|
1118
|
-
}
|
|
1119
|
-
collectRefs() {
|
|
1120
|
-
const tables = /* @__PURE__ */ new Set();
|
|
1121
|
-
const columns = /* @__PURE__ */ new Map();
|
|
1122
|
-
const addSource = (source) => {
|
|
1123
|
-
mergeRefsInto(source.collectRefs(), tables, columns);
|
|
1124
|
-
};
|
|
1125
|
-
addSource(this.from);
|
|
1126
|
-
for (const join of this.joins ?? []) {
|
|
1127
|
-
addSource(join.source);
|
|
1128
|
-
if (join.on.kind === "eq-col-join-on") {
|
|
1129
|
-
addColumnRefToRefSets(join.on.left, tables, columns);
|
|
1130
|
-
addColumnRefToRefSets(join.on.right, tables, columns);
|
|
1131
|
-
} else for (const columnRef of join.on.collectColumnRefs()) addColumnRefToRefSets(columnRef, tables, columns);
|
|
1132
|
-
}
|
|
1133
|
-
for (const columnRef of this.collectColumnRefs()) addColumnRefToRefSets(columnRef, tables, columns);
|
|
1134
|
-
return sortRefs(tables, columns);
|
|
1135
|
-
}
|
|
1136
|
-
toQueryAst() {
|
|
1137
|
-
return this;
|
|
1138
|
-
}
|
|
1139
|
-
};
|
|
1140
|
-
var InsertOnConflictAction = class extends AstNode {};
|
|
1141
|
-
var DoNothingConflictAction = class extends InsertOnConflictAction {
|
|
1142
|
-
kind = "do-nothing";
|
|
1143
|
-
constructor() {
|
|
1144
|
-
super();
|
|
1145
|
-
this.freeze();
|
|
1146
|
-
}
|
|
1147
|
-
toInsertOnConflictAction() {
|
|
1148
|
-
return this;
|
|
1149
|
-
}
|
|
1150
|
-
};
|
|
1151
|
-
var DoUpdateSetConflictAction = class extends InsertOnConflictAction {
|
|
1152
|
-
kind = "do-update-set";
|
|
1153
|
-
set;
|
|
1154
|
-
constructor(set) {
|
|
1155
|
-
super();
|
|
1156
|
-
this.set = frozenRecordCopy(set);
|
|
1157
|
-
this.freeze();
|
|
1158
|
-
}
|
|
1159
|
-
toInsertOnConflictAction() {
|
|
1160
|
-
return this;
|
|
1161
|
-
}
|
|
1162
|
-
};
|
|
1163
|
-
var InsertOnConflict = class InsertOnConflict extends AstNode {
|
|
1164
|
-
kind = "insert-on-conflict";
|
|
1165
|
-
columns;
|
|
1166
|
-
action;
|
|
1167
|
-
constructor(columns, action) {
|
|
1168
|
-
super();
|
|
1169
|
-
this.columns = frozenArrayCopy(columns);
|
|
1170
|
-
this.action = action;
|
|
1171
|
-
this.freeze();
|
|
1172
|
-
}
|
|
1173
|
-
static on(columns) {
|
|
1174
|
-
return new InsertOnConflict(columns, new DoNothingConflictAction());
|
|
1175
|
-
}
|
|
1176
|
-
doNothing() {
|
|
1177
|
-
return new InsertOnConflict(this.columns, new DoNothingConflictAction());
|
|
1178
|
-
}
|
|
1179
|
-
doUpdateSet(set) {
|
|
1180
|
-
return new InsertOnConflict(this.columns, new DoUpdateSetConflictAction(set));
|
|
1181
|
-
}
|
|
1182
|
-
};
|
|
1183
|
-
var InsertAst = class InsertAst extends QueryAst {
|
|
1184
|
-
kind = "insert";
|
|
1185
|
-
table;
|
|
1186
|
-
rows;
|
|
1187
|
-
onConflict;
|
|
1188
|
-
returning;
|
|
1189
|
-
constructor(table, rows = [{}], onConflict, returning) {
|
|
1190
|
-
super();
|
|
1191
|
-
this.table = table;
|
|
1192
|
-
this.rows = freezeRows(rows);
|
|
1193
|
-
this.onConflict = onConflict;
|
|
1194
|
-
this.returning = returning && returning.length > 0 ? frozenArrayCopy(returning) : void 0;
|
|
1195
|
-
this.freeze();
|
|
1196
|
-
}
|
|
1197
|
-
static into(table) {
|
|
1198
|
-
return new InsertAst(table);
|
|
1199
|
-
}
|
|
1200
|
-
withValues(values) {
|
|
1201
|
-
return new InsertAst(this.table, [{ ...values }], this.onConflict, this.returning);
|
|
1202
|
-
}
|
|
1203
|
-
withRows(rows) {
|
|
1204
|
-
return new InsertAst(this.table, rows.map((row) => ({ ...row })), this.onConflict, this.returning);
|
|
1205
|
-
}
|
|
1206
|
-
withReturning(returning) {
|
|
1207
|
-
return new InsertAst(this.table, this.rows.map((row) => ({ ...row })), this.onConflict, returning);
|
|
1208
|
-
}
|
|
1209
|
-
withOnConflict(onConflict) {
|
|
1210
|
-
return new InsertAst(this.table, this.rows.map((row) => ({ ...row })), onConflict, this.returning);
|
|
1211
|
-
}
|
|
1212
|
-
collectParamRefs() {
|
|
1213
|
-
const refs = [];
|
|
1214
|
-
for (const row of this.rows) for (const value of Object.values(row)) if (value.kind === "param-ref") refs.push(value);
|
|
1215
|
-
if (this.onConflict?.action.kind === "do-update-set") {
|
|
1216
|
-
for (const value of Object.values(this.onConflict.action.set)) if (value.kind === "param-ref") refs.push(value);
|
|
1217
|
-
}
|
|
1218
|
-
return refs;
|
|
1219
|
-
}
|
|
1220
|
-
collectRefs() {
|
|
1221
|
-
const tables = new Set([this.table.name]);
|
|
1222
|
-
const columns = /* @__PURE__ */ new Map();
|
|
1223
|
-
const addColumn = (columnRef) => addColumnRefToRefSets(columnRef, tables, columns);
|
|
1224
|
-
const addValue = (value) => {
|
|
1225
|
-
if (value.kind === "column-ref") addColumn(value);
|
|
1226
|
-
};
|
|
1227
|
-
for (const row of this.rows) for (const value of Object.values(row)) addValue(value);
|
|
1228
|
-
for (const columnRef of this.returning ?? []) addColumn(columnRef);
|
|
1229
|
-
if (this.onConflict) {
|
|
1230
|
-
for (const columnRef of this.onConflict.columns) addColumn(columnRef);
|
|
1231
|
-
if (this.onConflict.action.kind === "do-update-set") {
|
|
1232
|
-
for (const value of Object.values(this.onConflict.action.set)) if (value.kind === "column-ref") addColumn(value);
|
|
1233
|
-
}
|
|
1234
|
-
}
|
|
1235
|
-
return sortRefs(tables, columns);
|
|
1236
|
-
}
|
|
1237
|
-
toQueryAst() {
|
|
1238
|
-
return this;
|
|
1239
|
-
}
|
|
1240
|
-
};
|
|
1241
|
-
var UpdateAst = class UpdateAst extends QueryAst {
|
|
1242
|
-
kind = "update";
|
|
1243
|
-
table;
|
|
1244
|
-
set;
|
|
1245
|
-
where;
|
|
1246
|
-
returning;
|
|
1247
|
-
constructor(table, set = {}, where, returning) {
|
|
1248
|
-
super();
|
|
1249
|
-
this.table = table;
|
|
1250
|
-
this.set = frozenRecordCopy(set);
|
|
1251
|
-
this.where = where;
|
|
1252
|
-
this.returning = returning && returning.length > 0 ? frozenArrayCopy(returning) : void 0;
|
|
1253
|
-
this.freeze();
|
|
1254
|
-
}
|
|
1255
|
-
static table(table) {
|
|
1256
|
-
return new UpdateAst(table);
|
|
1257
|
-
}
|
|
1258
|
-
withSet(set) {
|
|
1259
|
-
return new UpdateAst(this.table, set, this.where, this.returning);
|
|
1260
|
-
}
|
|
1261
|
-
withWhere(where) {
|
|
1262
|
-
return new UpdateAst(this.table, this.set, where, this.returning);
|
|
1263
|
-
}
|
|
1264
|
-
withReturning(returning) {
|
|
1265
|
-
return new UpdateAst(this.table, this.set, this.where, returning);
|
|
1266
|
-
}
|
|
1267
|
-
collectParamRefs() {
|
|
1268
|
-
const refs = [];
|
|
1269
|
-
for (const value of Object.values(this.set)) if (value.kind === "param-ref") refs.push(value);
|
|
1270
|
-
if (this.where) refs.push(...this.where.collectParamRefs());
|
|
1271
|
-
return refs;
|
|
1272
|
-
}
|
|
1273
|
-
collectRefs() {
|
|
1274
|
-
const tables = new Set([this.table.name]);
|
|
1275
|
-
const columns = /* @__PURE__ */ new Map();
|
|
1276
|
-
for (const value of Object.values(this.set)) if (value.kind === "column-ref") addColumnRefToRefSets(value, tables, columns);
|
|
1277
|
-
for (const columnRef of this.where?.collectColumnRefs() ?? []) addColumnRefToRefSets(columnRef, tables, columns);
|
|
1278
|
-
for (const columnRef of this.returning ?? []) addColumnRefToRefSets(columnRef, tables, columns);
|
|
1279
|
-
return sortRefs(tables, columns);
|
|
1280
|
-
}
|
|
1281
|
-
toQueryAst() {
|
|
1282
|
-
return this;
|
|
1283
|
-
}
|
|
1284
|
-
};
|
|
1285
|
-
var DeleteAst = class DeleteAst extends QueryAst {
|
|
1286
|
-
kind = "delete";
|
|
1287
|
-
table;
|
|
1288
|
-
where;
|
|
1289
|
-
returning;
|
|
1290
|
-
constructor(table, where, returning) {
|
|
1291
|
-
super();
|
|
1292
|
-
this.table = table;
|
|
1293
|
-
this.where = where;
|
|
1294
|
-
this.returning = returning && returning.length > 0 ? frozenArrayCopy(returning) : void 0;
|
|
1295
|
-
this.freeze();
|
|
1296
|
-
}
|
|
1297
|
-
static from(table) {
|
|
1298
|
-
return new DeleteAst(table);
|
|
1299
|
-
}
|
|
1300
|
-
withWhere(where) {
|
|
1301
|
-
return new DeleteAst(this.table, where, this.returning);
|
|
1302
|
-
}
|
|
1303
|
-
withReturning(returning) {
|
|
1304
|
-
return new DeleteAst(this.table, this.where, returning);
|
|
1305
|
-
}
|
|
1306
|
-
collectParamRefs() {
|
|
1307
|
-
return this.where?.collectParamRefs() ?? [];
|
|
1308
|
-
}
|
|
1309
|
-
collectRefs() {
|
|
1310
|
-
const tables = new Set([this.table.name]);
|
|
1311
|
-
const columns = /* @__PURE__ */ new Map();
|
|
1312
|
-
for (const columnRef of this.where?.collectColumnRefs() ?? []) addColumnRefToRefSets(columnRef, tables, columns);
|
|
1313
|
-
for (const columnRef of this.returning ?? []) addColumnRefToRefSets(columnRef, tables, columns);
|
|
1314
|
-
return sortRefs(tables, columns);
|
|
1315
|
-
}
|
|
1316
|
-
toQueryAst() {
|
|
1317
|
-
return this;
|
|
1318
|
-
}
|
|
1319
|
-
};
|
|
1320
|
-
const queryAstKinds = new Set([
|
|
1321
|
-
"select",
|
|
1322
|
-
"insert",
|
|
1323
|
-
"update",
|
|
1324
|
-
"delete"
|
|
1325
|
-
]);
|
|
1326
|
-
const whereExprKinds = new Set([
|
|
1327
|
-
"binary",
|
|
1328
|
-
"and",
|
|
1329
|
-
"or",
|
|
1330
|
-
"exists",
|
|
1331
|
-
"null-check",
|
|
1332
|
-
"not"
|
|
1333
|
-
]);
|
|
1334
|
-
function isQueryAst(value) {
|
|
1335
|
-
return typeof value === "object" && value !== null && "kind" in value && queryAstKinds.has(value.kind);
|
|
1336
|
-
}
|
|
1337
|
-
function isWhereExpr(value) {
|
|
1338
|
-
return typeof value === "object" && value !== null && "kind" in value && whereExprKinds.has(value.kind);
|
|
1339
|
-
}
|
|
1340
|
-
|
|
1341
295
|
//#endregion
|
|
1342
296
|
//#region src/ast/util.ts
|
|
1343
297
|
function compact(o) {
|
|
@@ -1349,7 +303,27 @@ function compact(o) {
|
|
|
1349
303
|
}
|
|
1350
304
|
return out;
|
|
1351
305
|
}
|
|
306
|
+
/**
|
|
307
|
+
* Walks an AST's parameter references in first-encounter order and dedupes
|
|
308
|
+
* by ParamRef identity. The single canonical helper used by every consumer
|
|
309
|
+
* that aligns `plan.params` with metadata-by-index — the SQL builder lane,
|
|
310
|
+
* the SQL ORM client, the SQL runtime encoder, and the Postgres renderer's
|
|
311
|
+
* `$N` index map — so the four walks cannot drift in dedupe semantics.
|
|
312
|
+
*
|
|
313
|
+
* SQLite's `?`-placeholder renderer intentionally does NOT use this helper
|
|
314
|
+
* because it needs one params entry per occurrence in the SQL.
|
|
315
|
+
*/
|
|
316
|
+
function collectOrderedParamRefs(ast) {
|
|
317
|
+
const seen = /* @__PURE__ */ new Set();
|
|
318
|
+
const ordered = [];
|
|
319
|
+
for (const ref of ast.collectParamRefs()) {
|
|
320
|
+
if (seen.has(ref)) continue;
|
|
321
|
+
seen.add(ref);
|
|
322
|
+
ordered.push(ref);
|
|
323
|
+
}
|
|
324
|
+
return Object.freeze(ordered);
|
|
325
|
+
}
|
|
1352
326
|
|
|
1353
327
|
//#endregion
|
|
1354
|
-
export { AggregateExpr, AndExpr, BinaryExpr, ColumnRef, DefaultValueExpr, DeleteAst, DerivedTableSource, DoNothingConflictAction, DoUpdateSetConflictAction, EqColJoinOn, ExistsExpr, IdentifierRef, InsertAst, InsertOnConflict, JoinAst, JsonArrayAggExpr, JsonObjectExpr, ListExpression, LiteralExpr, NotExpr, NullCheckExpr, OperationExpr, OrExpr, OrderByItem, ParamRef, ProjectionItem, SQL_CHAR_CODEC_ID, SQL_FLOAT_CODEC_ID, SQL_INT_CODEC_ID, SQL_TEXT_CODEC_ID, SQL_TIMESTAMP_CODEC_ID, SQL_VARCHAR_CODEC_ID, SelectAst, SubqueryExpr, TableSource, UpdateAst, codec, compact, createCodecRegistry, defineCodecs, isQueryAst, isWhereExpr, queryAstKinds, sqlCodecDefinitions, sqlDataTypes, whereExprKinds };
|
|
328
|
+
export { AggregateExpr, AndExpr, BinaryExpr, ColumnRef, DefaultValueExpr, DeleteAst, DerivedTableSource, DoNothingConflictAction, DoUpdateSetConflictAction, EqColJoinOn, ExistsExpr, IdentifierRef, InsertAst, InsertOnConflict, JoinAst, JsonArrayAggExpr, JsonObjectExpr, ListExpression, LiteralExpr, NotExpr, NullCheckExpr, OperationExpr, OrExpr, OrderByItem, ParamRef, ProjectionItem, SQL_CHAR_CODEC_ID, SQL_FLOAT_CODEC_ID, SQL_INT_CODEC_ID, SQL_TEXT_CODEC_ID, SQL_TIMESTAMP_CODEC_ID, SQL_VARCHAR_CODEC_ID, SelectAst, SubqueryExpr, TableSource, UpdateAst, codec, collectOrderedParamRefs, compact, createCodecRegistry, defineCodecs, isQueryAst, isWhereExpr, queryAstKinds, sqlCodecDefinitions, sqlDataTypes, whereExprKinds };
|
|
1355
329
|
//# sourceMappingURL=ast.mjs.map
|