@prisma-next/sql-relational-core 0.3.0-pr.99.6 → 0.4.0-dev.1
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/LICENSE +201 -0
- package/README.md +20 -6
- package/dist/codec-types-DcEITed4.d.mts +144 -0
- package/dist/codec-types-DcEITed4.d.mts.map +1 -0
- package/dist/errors-Cs52upp3.d.mts +8 -0
- package/dist/errors-Cs52upp3.d.mts.map +1 -0
- package/dist/errors-D3xmG4h-.mjs +35 -0
- package/dist/errors-D3xmG4h-.mjs.map +1 -0
- package/dist/exports/ast.d.mts +158 -0
- package/dist/exports/ast.d.mts.map +1 -0
- package/dist/exports/ast.mjs +1358 -0
- package/dist/exports/ast.mjs.map +1 -0
- package/dist/exports/errors.d.mts +5 -0
- package/dist/exports/errors.mjs +3 -0
- package/dist/exports/plan.d.mts +2 -0
- package/dist/exports/plan.mjs +17 -0
- package/dist/exports/plan.mjs.map +1 -0
- package/dist/exports/query-lane-context.d.mts +3 -0
- package/dist/exports/query-lane-context.mjs +1 -0
- package/dist/exports/types.d.mts +4 -0
- package/dist/exports/types.mjs +1 -0
- package/dist/index.d.mts +11 -0
- package/dist/index.mjs +7 -0
- package/dist/plan-5qUWdlJM.d.mts +28 -0
- package/dist/plan-5qUWdlJM.d.mts.map +1 -0
- package/dist/query-lane-context-UlR8vOkd.d.mts +89 -0
- package/dist/query-lane-context-UlR8vOkd.d.mts.map +1 -0
- package/dist/types-CLCtwVWx.d.mts +485 -0
- package/dist/types-CLCtwVWx.d.mts.map +1 -0
- package/dist/types-V1qiC5DO.d.mts +200 -0
- package/dist/types-V1qiC5DO.d.mts.map +1 -0
- package/package.json +30 -52
- package/src/ast/adapter-types.ts +11 -0
- package/src/ast/codec-types.ts +101 -53
- package/src/ast/driver-types.ts +20 -3
- package/src/ast/sql-codecs.ts +142 -0
- package/src/ast/types.ts +1678 -88
- package/src/errors.ts +47 -1
- package/src/exports/ast.ts +1 -8
- package/src/index.ts +0 -3
- package/src/plan.ts +4 -4
- package/src/query-lane-context.ts +74 -11
- package/src/types.ts +102 -404
- package/dist/ast/adapter-types.d.ts +0 -28
- package/dist/ast/adapter-types.d.ts.map +0 -1
- package/dist/ast/codec-types.d.ts +0 -141
- package/dist/ast/codec-types.d.ts.map +0 -1
- package/dist/ast/common.d.ts +0 -7
- package/dist/ast/common.d.ts.map +0 -1
- package/dist/ast/delete.d.ts +0 -8
- package/dist/ast/delete.d.ts.map +0 -1
- package/dist/ast/driver-types.d.ts +0 -20
- package/dist/ast/driver-types.d.ts.map +0 -1
- package/dist/ast/insert.d.ts +0 -8
- package/dist/ast/insert.d.ts.map +0 -1
- package/dist/ast/join.d.ts +0 -6
- package/dist/ast/join.d.ts.map +0 -1
- package/dist/ast/order.d.ts +0 -6
- package/dist/ast/order.d.ts.map +0 -1
- package/dist/ast/predicate.d.ts +0 -4
- package/dist/ast/predicate.d.ts.map +0 -1
- package/dist/ast/select.d.ts +0 -18
- package/dist/ast/select.d.ts.map +0 -1
- package/dist/ast/types.d.ts +0 -130
- package/dist/ast/types.d.ts.map +0 -1
- package/dist/ast/update.d.ts +0 -9
- package/dist/ast/update.d.ts.map +0 -1
- package/dist/ast/util.d.ts +0 -2
- package/dist/ast/util.d.ts.map +0 -1
- package/dist/chunk-2BWK6XEY.js +0 -13
- package/dist/chunk-2BWK6XEY.js.map +0 -1
- package/dist/chunk-2F7DSEOU.js +0 -8
- package/dist/chunk-2F7DSEOU.js.map +0 -1
- package/dist/chunk-2MAKNVCP.js +0 -86
- package/dist/chunk-2MAKNVCP.js.map +0 -1
- package/dist/chunk-36WJWNHT.js +0 -1
- package/dist/chunk-36WJWNHT.js.map +0 -1
- package/dist/chunk-3F4RFQIB.js +0 -169
- package/dist/chunk-3F4RFQIB.js.map +0 -1
- package/dist/chunk-7I3EMQID.js +0 -16
- package/dist/chunk-7I3EMQID.js.map +0 -1
- package/dist/chunk-G52ENULI.js +0 -1
- package/dist/chunk-G52ENULI.js.map +0 -1
- package/dist/chunk-HV334QHG.js +0 -320
- package/dist/chunk-HV334QHG.js.map +0 -1
- package/dist/chunk-KYSP7L5C.js +0 -16
- package/dist/chunk-KYSP7L5C.js.map +0 -1
- package/dist/chunk-U7AXAUJA.js +0 -1
- package/dist/chunk-U7AXAUJA.js.map +0 -1
- package/dist/chunk-YXD25S5I.js +0 -171
- package/dist/chunk-YXD25S5I.js.map +0 -1
- package/dist/errors.d.ts +0 -2
- package/dist/errors.d.ts.map +0 -1
- package/dist/exports/ast.d.ts +0 -14
- package/dist/exports/ast.d.ts.map +0 -1
- package/dist/exports/ast.js +0 -46
- package/dist/exports/ast.js.map +0 -1
- package/dist/exports/errors.d.ts +0 -2
- package/dist/exports/errors.d.ts.map +0 -1
- package/dist/exports/errors.js +0 -9
- package/dist/exports/errors.js.map +0 -1
- package/dist/exports/guards.d.ts +0 -2
- package/dist/exports/guards.d.ts.map +0 -1
- package/dist/exports/guards.js +0 -31
- package/dist/exports/guards.js.map +0 -1
- package/dist/exports/operations-registry.d.ts +0 -2
- package/dist/exports/operations-registry.d.ts.map +0 -1
- package/dist/exports/operations-registry.js +0 -9
- package/dist/exports/operations-registry.js.map +0 -1
- package/dist/exports/param.d.ts +0 -3
- package/dist/exports/param.d.ts.map +0 -1
- package/dist/exports/param.js +0 -7
- package/dist/exports/param.js.map +0 -1
- package/dist/exports/plan.d.ts +0 -2
- package/dist/exports/plan.d.ts.map +0 -1
- package/dist/exports/plan.js +0 -7
- package/dist/exports/plan.js.map +0 -1
- package/dist/exports/query-lane-context.d.ts +0 -2
- package/dist/exports/query-lane-context.d.ts.map +0 -1
- package/dist/exports/query-lane-context.js +0 -2
- package/dist/exports/query-lane-context.js.map +0 -1
- package/dist/exports/schema.d.ts +0 -3
- package/dist/exports/schema.d.ts.map +0 -1
- package/dist/exports/schema.js +0 -13
- package/dist/exports/schema.js.map +0 -1
- package/dist/exports/types.d.ts +0 -2
- package/dist/exports/types.d.ts.map +0 -1
- package/dist/exports/types.js +0 -10
- package/dist/exports/types.js.map +0 -1
- package/dist/index.d.ts +0 -9
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -81
- package/dist/index.js.map +0 -1
- package/dist/operations-registry.d.ts +0 -5
- package/dist/operations-registry.d.ts.map +0 -1
- package/dist/param.d.ts +0 -4
- package/dist/param.d.ts.map +0 -1
- package/dist/plan.d.ts +0 -23
- package/dist/plan.d.ts.map +0 -1
- package/dist/query-lane-context.d.ts +0 -27
- package/dist/query-lane-context.d.ts.map +0 -1
- package/dist/schema.d.ts +0 -83
- package/dist/schema.d.ts.map +0 -1
- package/dist/types.d.ts +0 -371
- package/dist/types.d.ts.map +0 -1
- package/dist/utils/guards.d.ts +0 -82
- package/dist/utils/guards.d.ts.map +0 -1
- package/src/ast/common.ts +0 -36
- package/src/ast/delete.ts +0 -17
- package/src/ast/insert.ts +0 -17
- package/src/ast/join.ts +0 -54
- package/src/ast/order.ts +0 -11
- package/src/ast/predicate.ts +0 -22
- package/src/ast/select.ts +0 -39
- package/src/ast/update.ts +0 -19
- package/src/exports/guards.ts +0 -15
- package/src/exports/operations-registry.ts +0 -1
- package/src/exports/param.ts +0 -2
- package/src/exports/schema.ts +0 -6
- package/src/operations-registry.ts +0 -276
- package/src/param.ts +0 -15
- package/src/schema.ts +0 -406
- package/src/utils/guards.ts +0 -193
|
@@ -0,0 +1,1358 @@
|
|
|
1
|
+
import { ifDefined } from "@prisma-next/utils/defined";
|
|
2
|
+
import { type } from "arktype";
|
|
3
|
+
|
|
4
|
+
//#region src/ast/codec-types.ts
|
|
5
|
+
/**
|
|
6
|
+
* Implementation of CodecRegistry.
|
|
7
|
+
*/
|
|
8
|
+
var CodecRegistryImpl = class {
|
|
9
|
+
_byId = /* @__PURE__ */ new Map();
|
|
10
|
+
_byScalar = /* @__PURE__ */ new Map();
|
|
11
|
+
/**
|
|
12
|
+
* Map-like interface for codec lookup by ID.
|
|
13
|
+
* Example: registry.get('pg/text@1')
|
|
14
|
+
*/
|
|
15
|
+
get(id) {
|
|
16
|
+
return this._byId.get(id);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Check if a codec with the given ID is registered.
|
|
20
|
+
*/
|
|
21
|
+
has(id) {
|
|
22
|
+
return this._byId.has(id);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Get all codecs that handle a given scalar type.
|
|
26
|
+
* Returns an empty frozen array if no codecs are found.
|
|
27
|
+
* Example: registry.getByScalar('text') → [codec1, codec2, ...]
|
|
28
|
+
*/
|
|
29
|
+
getByScalar(scalar) {
|
|
30
|
+
return this._byScalar.get(scalar) ?? Object.freeze([]);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Get the default codec for a scalar type (first registered codec).
|
|
34
|
+
* Returns undefined if no codec handles this scalar type.
|
|
35
|
+
*/
|
|
36
|
+
getDefaultCodec(scalar) {
|
|
37
|
+
return this._byScalar.get(scalar)?.[0];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Register a codec in the registry.
|
|
41
|
+
* Throws an error if a codec with the same ID is already registered.
|
|
42
|
+
*
|
|
43
|
+
* @param codec - The codec to register
|
|
44
|
+
* @throws Error if a codec with the same ID already exists
|
|
45
|
+
*/
|
|
46
|
+
register(codec$1) {
|
|
47
|
+
if (this._byId.has(codec$1.id)) throw new Error(`Codec with ID '${codec$1.id}' is already registered`);
|
|
48
|
+
this._byId.set(codec$1.id, codec$1);
|
|
49
|
+
for (const scalarType of codec$1.targetTypes) {
|
|
50
|
+
const existing = this._byScalar.get(scalarType);
|
|
51
|
+
if (existing) existing.push(codec$1);
|
|
52
|
+
else this._byScalar.set(scalarType, [codec$1]);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
hasTrait(codecId, trait) {
|
|
56
|
+
return this._byId.get(codecId)?.traits?.includes(trait) ?? false;
|
|
57
|
+
}
|
|
58
|
+
traitsOf(codecId) {
|
|
59
|
+
return this._byId.get(codecId)?.traits ?? [];
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Returns an iterator over all registered codecs.
|
|
63
|
+
* Useful for iterating through codecs from another registry.
|
|
64
|
+
*/
|
|
65
|
+
*[Symbol.iterator]() {
|
|
66
|
+
for (const codec$1 of this._byId.values()) yield codec$1;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Returns an iterable of all registered codecs.
|
|
70
|
+
*/
|
|
71
|
+
values() {
|
|
72
|
+
return this._byId.values();
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Codec factory - creates a codec with typeId and encode/decode functions.
|
|
77
|
+
* Provides identity defaults for encodeJson/decodeJson when not supplied.
|
|
78
|
+
*/
|
|
79
|
+
function codec(config) {
|
|
80
|
+
const identity = (v) => v;
|
|
81
|
+
return {
|
|
82
|
+
id: config.typeId,
|
|
83
|
+
targetTypes: config.targetTypes,
|
|
84
|
+
...ifDefined("meta", config.meta),
|
|
85
|
+
...ifDefined("paramsSchema", config.paramsSchema),
|
|
86
|
+
...ifDefined("init", config.init),
|
|
87
|
+
...ifDefined("traits", config.traits ? Object.freeze([...config.traits]) : void 0),
|
|
88
|
+
...ifDefined("renderOutputType", config.renderOutputType),
|
|
89
|
+
encode: config.encode,
|
|
90
|
+
decode: config.decode,
|
|
91
|
+
encodeJson: config.encodeJson ?? identity,
|
|
92
|
+
decodeJson: config.decodeJson ?? identity
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Implementation of CodecDefBuilder.
|
|
97
|
+
*/
|
|
98
|
+
var CodecDefBuilderImpl = class CodecDefBuilderImpl {
|
|
99
|
+
_codecs;
|
|
100
|
+
CodecTypes;
|
|
101
|
+
dataTypes;
|
|
102
|
+
constructor(codecs$1) {
|
|
103
|
+
this._codecs = codecs$1;
|
|
104
|
+
const codecTypes = {};
|
|
105
|
+
for (const [, codecImpl] of Object.entries(this._codecs)) {
|
|
106
|
+
const codecImplTyped = codecImpl;
|
|
107
|
+
codecTypes[codecImplTyped.id] = {
|
|
108
|
+
input: void 0,
|
|
109
|
+
output: void 0,
|
|
110
|
+
traits: void 0
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
this.CodecTypes = codecTypes;
|
|
114
|
+
const dataTypes = {};
|
|
115
|
+
for (const key in this._codecs) if (Object.hasOwn(this._codecs, key)) dataTypes[key] = this._codecs[key].id;
|
|
116
|
+
this.dataTypes = dataTypes;
|
|
117
|
+
}
|
|
118
|
+
add(scalarName, codecImpl) {
|
|
119
|
+
return new CodecDefBuilderImpl({
|
|
120
|
+
...this._codecs,
|
|
121
|
+
[scalarName]: codecImpl
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Derive codecDefinitions structure.
|
|
126
|
+
*/
|
|
127
|
+
get codecDefinitions() {
|
|
128
|
+
const result = {};
|
|
129
|
+
for (const [scalarName, codecImpl] of Object.entries(this._codecs)) {
|
|
130
|
+
const codec$1 = codecImpl;
|
|
131
|
+
result[scalarName] = {
|
|
132
|
+
typeId: codec$1.id,
|
|
133
|
+
scalar: scalarName,
|
|
134
|
+
codec: codec$1,
|
|
135
|
+
input: void 0,
|
|
136
|
+
output: void 0,
|
|
137
|
+
jsType: void 0
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
return result;
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
/**
|
|
144
|
+
* Create a new codec registry.
|
|
145
|
+
*/
|
|
146
|
+
function createCodecRegistry() {
|
|
147
|
+
return new CodecRegistryImpl();
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Create a new codec definition builder.
|
|
151
|
+
*/
|
|
152
|
+
function defineCodecs() {
|
|
153
|
+
return new CodecDefBuilderImpl({});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
//#endregion
|
|
157
|
+
//#region src/ast/sql-codecs.ts
|
|
158
|
+
const SQL_CHAR_CODEC_ID = "sql/char@1";
|
|
159
|
+
const SQL_VARCHAR_CODEC_ID = "sql/varchar@1";
|
|
160
|
+
const SQL_INT_CODEC_ID = "sql/int@1";
|
|
161
|
+
const SQL_FLOAT_CODEC_ID = "sql/float@1";
|
|
162
|
+
const SQL_TEXT_CODEC_ID = "sql/text@1";
|
|
163
|
+
const SQL_TIMESTAMP_CODEC_ID = "sql/timestamp@1";
|
|
164
|
+
const lengthParamsSchema = type({ length: "number.integer > 0" });
|
|
165
|
+
const precisionParamsSchema = type({ "precision?": "number.integer >= 0 & number.integer <= 6" });
|
|
166
|
+
function createLengthTypeHelper(kind) {
|
|
167
|
+
return (params) => ({
|
|
168
|
+
kind,
|
|
169
|
+
maxLength: params["length"]
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
const sqlCharCodec = codec({
|
|
173
|
+
typeId: SQL_CHAR_CODEC_ID,
|
|
174
|
+
targetTypes: ["char"],
|
|
175
|
+
traits: [
|
|
176
|
+
"equality",
|
|
177
|
+
"order",
|
|
178
|
+
"textual"
|
|
179
|
+
],
|
|
180
|
+
encode: (value) => value,
|
|
181
|
+
decode: (wire) => wire.trimEnd(),
|
|
182
|
+
paramsSchema: lengthParamsSchema,
|
|
183
|
+
init: createLengthTypeHelper("fixed"),
|
|
184
|
+
renderOutputType: (typeParams) => {
|
|
185
|
+
const length = typeParams["length"];
|
|
186
|
+
if (length === void 0) return void 0;
|
|
187
|
+
if (typeof length !== "number" || !Number.isFinite(length) || !Number.isInteger(length)) throw new Error(`renderOutputType: expected integer "length" in typeParams for Char, got ${String(length)}`);
|
|
188
|
+
return `Char<${length}>`;
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
const sqlVarcharCodec = codec({
|
|
192
|
+
typeId: SQL_VARCHAR_CODEC_ID,
|
|
193
|
+
targetTypes: ["varchar"],
|
|
194
|
+
traits: [
|
|
195
|
+
"equality",
|
|
196
|
+
"order",
|
|
197
|
+
"textual"
|
|
198
|
+
],
|
|
199
|
+
encode: (value) => value,
|
|
200
|
+
decode: (wire) => wire,
|
|
201
|
+
paramsSchema: lengthParamsSchema,
|
|
202
|
+
init: createLengthTypeHelper("variable"),
|
|
203
|
+
renderOutputType: (typeParams) => {
|
|
204
|
+
const length = typeParams["length"];
|
|
205
|
+
if (length === void 0) return void 0;
|
|
206
|
+
if (typeof length !== "number" || !Number.isFinite(length) || !Number.isInteger(length)) throw new Error(`renderOutputType: expected integer "length" in typeParams for Varchar, got ${String(length)}`);
|
|
207
|
+
return `Varchar<${length}>`;
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
const sqlIntCodec = codec({
|
|
211
|
+
typeId: SQL_INT_CODEC_ID,
|
|
212
|
+
targetTypes: ["int"],
|
|
213
|
+
traits: [
|
|
214
|
+
"equality",
|
|
215
|
+
"order",
|
|
216
|
+
"numeric"
|
|
217
|
+
],
|
|
218
|
+
encode: (value) => value,
|
|
219
|
+
decode: (wire) => wire
|
|
220
|
+
});
|
|
221
|
+
const sqlFloatCodec = codec({
|
|
222
|
+
typeId: SQL_FLOAT_CODEC_ID,
|
|
223
|
+
targetTypes: ["float"],
|
|
224
|
+
traits: [
|
|
225
|
+
"equality",
|
|
226
|
+
"order",
|
|
227
|
+
"numeric"
|
|
228
|
+
],
|
|
229
|
+
encode: (value) => value,
|
|
230
|
+
decode: (wire) => wire
|
|
231
|
+
});
|
|
232
|
+
const sqlTextCodec = codec({
|
|
233
|
+
typeId: SQL_TEXT_CODEC_ID,
|
|
234
|
+
targetTypes: ["text"],
|
|
235
|
+
traits: [
|
|
236
|
+
"equality",
|
|
237
|
+
"order",
|
|
238
|
+
"textual"
|
|
239
|
+
],
|
|
240
|
+
encode: (value) => value,
|
|
241
|
+
decode: (wire) => wire
|
|
242
|
+
});
|
|
243
|
+
const sqlTimestampCodec = codec({
|
|
244
|
+
typeId: SQL_TIMESTAMP_CODEC_ID,
|
|
245
|
+
targetTypes: ["timestamp"],
|
|
246
|
+
traits: ["equality", "order"],
|
|
247
|
+
encode: (value) => value instanceof Date ? value.toISOString() : value,
|
|
248
|
+
decode: (wire) => wire instanceof Date ? wire.toISOString() : wire,
|
|
249
|
+
paramsSchema: precisionParamsSchema,
|
|
250
|
+
renderOutputType: (typeParams) => {
|
|
251
|
+
const precision = typeParams["precision"];
|
|
252
|
+
if (precision === void 0) return "Timestamp";
|
|
253
|
+
if (typeof precision !== "number" || !Number.isFinite(precision) || !Number.isInteger(precision)) throw new Error(`renderOutputType: expected integer "precision" in typeParams for Timestamp, got ${String(precision)}`);
|
|
254
|
+
return `Timestamp<${precision}>`;
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
const codecs = defineCodecs().add("char", sqlCharCodec).add("varchar", sqlVarcharCodec).add("int", sqlIntCodec).add("float", sqlFloatCodec).add("text", sqlTextCodec).add("timestamp", sqlTimestampCodec);
|
|
258
|
+
const sqlCodecDefinitions = codecs.codecDefinitions;
|
|
259
|
+
const sqlDataTypes = codecs.dataTypes;
|
|
260
|
+
|
|
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 ilike(left, right) {
|
|
744
|
+
return new BinaryExpr("ilike", left, right);
|
|
745
|
+
}
|
|
746
|
+
static in(left, right) {
|
|
747
|
+
return new BinaryExpr("in", left, right);
|
|
748
|
+
}
|
|
749
|
+
static notIn(left, right) {
|
|
750
|
+
return new BinaryExpr("notIn", left, right);
|
|
751
|
+
}
|
|
752
|
+
accept(visitor) {
|
|
753
|
+
return visitor.binary(this);
|
|
754
|
+
}
|
|
755
|
+
rewrite(rewriter) {
|
|
756
|
+
return new BinaryExpr(this.op, rewriteComparable(this.left, rewriter), rewriteComparable(this.right, rewriter));
|
|
757
|
+
}
|
|
758
|
+
fold(folder) {
|
|
759
|
+
return combineAll(folder, [() => foldComparable(this.left, folder), () => foldComparable(this.right, folder)]);
|
|
760
|
+
}
|
|
761
|
+
};
|
|
762
|
+
var AndExpr = class AndExpr extends Expression {
|
|
763
|
+
kind = "and";
|
|
764
|
+
exprs;
|
|
765
|
+
constructor(exprs) {
|
|
766
|
+
super();
|
|
767
|
+
this.exprs = frozenArrayCopy(exprs);
|
|
768
|
+
this.freeze();
|
|
769
|
+
}
|
|
770
|
+
static of(exprs) {
|
|
771
|
+
return new AndExpr(exprs);
|
|
772
|
+
}
|
|
773
|
+
static true() {
|
|
774
|
+
return new AndExpr([]);
|
|
775
|
+
}
|
|
776
|
+
accept(visitor) {
|
|
777
|
+
return visitor.and(this);
|
|
778
|
+
}
|
|
779
|
+
rewrite(rewriter) {
|
|
780
|
+
return new AndExpr(this.exprs.map((expr) => expr.rewrite(rewriter)));
|
|
781
|
+
}
|
|
782
|
+
fold(folder) {
|
|
783
|
+
return combineAll(folder, this.exprs.map((expr) => () => expr.fold(folder)));
|
|
784
|
+
}
|
|
785
|
+
};
|
|
786
|
+
var OrExpr = class OrExpr extends Expression {
|
|
787
|
+
kind = "or";
|
|
788
|
+
exprs;
|
|
789
|
+
constructor(exprs) {
|
|
790
|
+
super();
|
|
791
|
+
this.exprs = frozenArrayCopy(exprs);
|
|
792
|
+
this.freeze();
|
|
793
|
+
}
|
|
794
|
+
static of(exprs) {
|
|
795
|
+
return new OrExpr(exprs);
|
|
796
|
+
}
|
|
797
|
+
static false() {
|
|
798
|
+
return new OrExpr([]);
|
|
799
|
+
}
|
|
800
|
+
accept(visitor) {
|
|
801
|
+
return visitor.or(this);
|
|
802
|
+
}
|
|
803
|
+
rewrite(rewriter) {
|
|
804
|
+
return new OrExpr(this.exprs.map((expr) => expr.rewrite(rewriter)));
|
|
805
|
+
}
|
|
806
|
+
fold(folder) {
|
|
807
|
+
return combineAll(folder, this.exprs.map((expr) => () => expr.fold(folder)));
|
|
808
|
+
}
|
|
809
|
+
};
|
|
810
|
+
var ExistsExpr = class ExistsExpr extends Expression {
|
|
811
|
+
kind = "exists";
|
|
812
|
+
notExists;
|
|
813
|
+
subquery;
|
|
814
|
+
constructor(subquery, notExists = false) {
|
|
815
|
+
super();
|
|
816
|
+
this.notExists = notExists;
|
|
817
|
+
this.subquery = subquery;
|
|
818
|
+
this.freeze();
|
|
819
|
+
}
|
|
820
|
+
static exists(subquery) {
|
|
821
|
+
return new ExistsExpr(subquery, false);
|
|
822
|
+
}
|
|
823
|
+
static notExists(subquery) {
|
|
824
|
+
return new ExistsExpr(subquery, true);
|
|
825
|
+
}
|
|
826
|
+
accept(visitor) {
|
|
827
|
+
return visitor.exists(this);
|
|
828
|
+
}
|
|
829
|
+
rewrite(rewriter) {
|
|
830
|
+
return new ExistsExpr(this.subquery.rewrite(rewriter), this.notExists);
|
|
831
|
+
}
|
|
832
|
+
fold(folder) {
|
|
833
|
+
return folder.select ? folder.select(this.subquery) : folder.empty;
|
|
834
|
+
}
|
|
835
|
+
};
|
|
836
|
+
var NullCheckExpr = class NullCheckExpr extends Expression {
|
|
837
|
+
kind = "null-check";
|
|
838
|
+
expr;
|
|
839
|
+
isNull;
|
|
840
|
+
constructor(expr, isNull) {
|
|
841
|
+
super();
|
|
842
|
+
this.expr = expr;
|
|
843
|
+
this.isNull = isNull;
|
|
844
|
+
this.freeze();
|
|
845
|
+
}
|
|
846
|
+
static isNull(expr) {
|
|
847
|
+
return new NullCheckExpr(expr, true);
|
|
848
|
+
}
|
|
849
|
+
static isNotNull(expr) {
|
|
850
|
+
return new NullCheckExpr(expr, false);
|
|
851
|
+
}
|
|
852
|
+
accept(visitor) {
|
|
853
|
+
return visitor.nullCheck(this);
|
|
854
|
+
}
|
|
855
|
+
rewrite(rewriter) {
|
|
856
|
+
return new NullCheckExpr(this.expr.rewrite(rewriter), this.isNull);
|
|
857
|
+
}
|
|
858
|
+
fold(folder) {
|
|
859
|
+
return this.expr.fold(folder);
|
|
860
|
+
}
|
|
861
|
+
};
|
|
862
|
+
var NotExpr = class NotExpr extends Expression {
|
|
863
|
+
kind = "not";
|
|
864
|
+
expr;
|
|
865
|
+
constructor(expr) {
|
|
866
|
+
super();
|
|
867
|
+
this.expr = expr;
|
|
868
|
+
this.freeze();
|
|
869
|
+
}
|
|
870
|
+
toWhereExpr() {
|
|
871
|
+
return this;
|
|
872
|
+
}
|
|
873
|
+
accept(visitor) {
|
|
874
|
+
return visitor.not(this);
|
|
875
|
+
}
|
|
876
|
+
rewrite(rewriter) {
|
|
877
|
+
return new NotExpr(this.expr.rewrite(rewriter));
|
|
878
|
+
}
|
|
879
|
+
fold(folder) {
|
|
880
|
+
return this.expr.fold(folder);
|
|
881
|
+
}
|
|
882
|
+
};
|
|
883
|
+
var EqColJoinOn = class EqColJoinOn extends AstNode {
|
|
884
|
+
kind = "eq-col-join-on";
|
|
885
|
+
left;
|
|
886
|
+
right;
|
|
887
|
+
constructor(left, right) {
|
|
888
|
+
super();
|
|
889
|
+
this.left = left;
|
|
890
|
+
this.right = right;
|
|
891
|
+
this.freeze();
|
|
892
|
+
}
|
|
893
|
+
static of(left, right) {
|
|
894
|
+
return new EqColJoinOn(left, right);
|
|
895
|
+
}
|
|
896
|
+
rewrite(rewriter) {
|
|
897
|
+
return rewriter.eqColJoinOn ? rewriter.eqColJoinOn(this) : this;
|
|
898
|
+
}
|
|
899
|
+
};
|
|
900
|
+
var JoinAst = class JoinAst extends AstNode {
|
|
901
|
+
kind = "join";
|
|
902
|
+
joinType;
|
|
903
|
+
source;
|
|
904
|
+
lateral;
|
|
905
|
+
on;
|
|
906
|
+
constructor(joinType, source, on, lateral = false) {
|
|
907
|
+
super();
|
|
908
|
+
this.joinType = joinType;
|
|
909
|
+
this.source = source;
|
|
910
|
+
this.lateral = lateral;
|
|
911
|
+
this.on = on;
|
|
912
|
+
this.freeze();
|
|
913
|
+
}
|
|
914
|
+
static inner(source, on, lateral = false) {
|
|
915
|
+
return new JoinAst("inner", source, on, lateral);
|
|
916
|
+
}
|
|
917
|
+
static left(source, on, lateral = false) {
|
|
918
|
+
return new JoinAst("left", source, on, lateral);
|
|
919
|
+
}
|
|
920
|
+
static right(source, on, lateral = false) {
|
|
921
|
+
return new JoinAst("right", source, on, lateral);
|
|
922
|
+
}
|
|
923
|
+
static full(source, on, lateral = false) {
|
|
924
|
+
return new JoinAst("full", source, on, lateral);
|
|
925
|
+
}
|
|
926
|
+
rewrite(rewriter) {
|
|
927
|
+
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);
|
|
928
|
+
}
|
|
929
|
+
};
|
|
930
|
+
var ProjectionItem = class ProjectionItem extends AstNode {
|
|
931
|
+
kind = "projection-item";
|
|
932
|
+
alias;
|
|
933
|
+
expr;
|
|
934
|
+
constructor(alias, expr) {
|
|
935
|
+
super();
|
|
936
|
+
this.alias = alias;
|
|
937
|
+
this.expr = expr;
|
|
938
|
+
this.freeze();
|
|
939
|
+
}
|
|
940
|
+
static of(alias, expr) {
|
|
941
|
+
return new ProjectionItem(alias, expr);
|
|
942
|
+
}
|
|
943
|
+
};
|
|
944
|
+
var SelectAst = class SelectAst extends QueryAst {
|
|
945
|
+
kind = "select";
|
|
946
|
+
from;
|
|
947
|
+
joins;
|
|
948
|
+
projection;
|
|
949
|
+
where;
|
|
950
|
+
orderBy;
|
|
951
|
+
distinct;
|
|
952
|
+
distinctOn;
|
|
953
|
+
groupBy;
|
|
954
|
+
having;
|
|
955
|
+
limit;
|
|
956
|
+
offset;
|
|
957
|
+
selectAllIntent;
|
|
958
|
+
constructor(options) {
|
|
959
|
+
super();
|
|
960
|
+
this.from = options.from;
|
|
961
|
+
this.joins = options.joins && options.joins.length > 0 ? frozenArrayCopy(options.joins) : void 0;
|
|
962
|
+
this.projection = frozenArrayCopy(options.projection);
|
|
963
|
+
this.where = options.where;
|
|
964
|
+
this.orderBy = options.orderBy && options.orderBy.length > 0 ? frozenArrayCopy(options.orderBy) : void 0;
|
|
965
|
+
this.distinct = options.distinct;
|
|
966
|
+
this.distinctOn = options.distinctOn && options.distinctOn.length > 0 ? frozenArrayCopy(options.distinctOn) : void 0;
|
|
967
|
+
this.groupBy = options.groupBy && options.groupBy.length > 0 ? frozenArrayCopy(options.groupBy) : void 0;
|
|
968
|
+
this.having = options.having;
|
|
969
|
+
this.limit = options.limit;
|
|
970
|
+
this.offset = options.offset;
|
|
971
|
+
this.selectAllIntent = frozenOptionalRecordCopy(options.selectAllIntent);
|
|
972
|
+
this.freeze();
|
|
973
|
+
}
|
|
974
|
+
static from(from) {
|
|
975
|
+
return new SelectAst({
|
|
976
|
+
from,
|
|
977
|
+
joins: void 0,
|
|
978
|
+
projection: [],
|
|
979
|
+
where: void 0,
|
|
980
|
+
orderBy: void 0,
|
|
981
|
+
distinct: void 0,
|
|
982
|
+
distinctOn: void 0,
|
|
983
|
+
groupBy: void 0,
|
|
984
|
+
having: void 0,
|
|
985
|
+
limit: void 0,
|
|
986
|
+
offset: void 0,
|
|
987
|
+
selectAllIntent: void 0
|
|
988
|
+
});
|
|
989
|
+
}
|
|
990
|
+
withFrom(from) {
|
|
991
|
+
return new SelectAst({
|
|
992
|
+
...this,
|
|
993
|
+
from
|
|
994
|
+
});
|
|
995
|
+
}
|
|
996
|
+
withJoins(joins) {
|
|
997
|
+
return new SelectAst({
|
|
998
|
+
...this,
|
|
999
|
+
joins: joins.length > 0 ? joins : void 0
|
|
1000
|
+
});
|
|
1001
|
+
}
|
|
1002
|
+
withProjection(projection) {
|
|
1003
|
+
return new SelectAst({
|
|
1004
|
+
...this,
|
|
1005
|
+
projection
|
|
1006
|
+
});
|
|
1007
|
+
}
|
|
1008
|
+
addProjection(alias, expr) {
|
|
1009
|
+
return new SelectAst({
|
|
1010
|
+
...this,
|
|
1011
|
+
projection: [...this.projection, new ProjectionItem(alias, expr)]
|
|
1012
|
+
});
|
|
1013
|
+
}
|
|
1014
|
+
withWhere(where) {
|
|
1015
|
+
return new SelectAst({
|
|
1016
|
+
...this,
|
|
1017
|
+
where
|
|
1018
|
+
});
|
|
1019
|
+
}
|
|
1020
|
+
withOrderBy(orderBy) {
|
|
1021
|
+
return new SelectAst({
|
|
1022
|
+
...this,
|
|
1023
|
+
orderBy: orderBy.length > 0 ? orderBy : void 0
|
|
1024
|
+
});
|
|
1025
|
+
}
|
|
1026
|
+
withDistinct(enabled = true) {
|
|
1027
|
+
return new SelectAst({
|
|
1028
|
+
...this,
|
|
1029
|
+
distinct: enabled ? true : void 0
|
|
1030
|
+
});
|
|
1031
|
+
}
|
|
1032
|
+
withDistinctOn(distinctOn) {
|
|
1033
|
+
return new SelectAst({
|
|
1034
|
+
...this,
|
|
1035
|
+
distinctOn: distinctOn.length > 0 ? distinctOn : void 0
|
|
1036
|
+
});
|
|
1037
|
+
}
|
|
1038
|
+
withGroupBy(groupBy) {
|
|
1039
|
+
return new SelectAst({
|
|
1040
|
+
...this,
|
|
1041
|
+
groupBy: groupBy.length > 0 ? groupBy : void 0
|
|
1042
|
+
});
|
|
1043
|
+
}
|
|
1044
|
+
withHaving(having) {
|
|
1045
|
+
return new SelectAst({
|
|
1046
|
+
...this,
|
|
1047
|
+
having
|
|
1048
|
+
});
|
|
1049
|
+
}
|
|
1050
|
+
withLimit(limit) {
|
|
1051
|
+
return new SelectAst({
|
|
1052
|
+
...this,
|
|
1053
|
+
limit
|
|
1054
|
+
});
|
|
1055
|
+
}
|
|
1056
|
+
withOffset(offset) {
|
|
1057
|
+
return new SelectAst({
|
|
1058
|
+
...this,
|
|
1059
|
+
offset
|
|
1060
|
+
});
|
|
1061
|
+
}
|
|
1062
|
+
withSelectAllIntent(selectAllIntent) {
|
|
1063
|
+
return new SelectAst({
|
|
1064
|
+
...this,
|
|
1065
|
+
selectAllIntent
|
|
1066
|
+
});
|
|
1067
|
+
}
|
|
1068
|
+
rewrite(rewriter) {
|
|
1069
|
+
const rewritten = new SelectAst({
|
|
1070
|
+
from: this.from.rewrite(rewriter),
|
|
1071
|
+
joins: this.joins?.map((join) => join.rewrite(rewriter)),
|
|
1072
|
+
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))),
|
|
1073
|
+
where: this.where?.rewrite(rewriter),
|
|
1074
|
+
orderBy: this.orderBy?.map((orderItem) => orderItem.rewrite(rewriter)),
|
|
1075
|
+
distinct: this.distinct,
|
|
1076
|
+
distinctOn: this.distinctOn?.map((expr) => expr.rewrite(rewriter)),
|
|
1077
|
+
groupBy: this.groupBy?.map((expr) => expr.rewrite(rewriter)),
|
|
1078
|
+
having: this.having?.rewrite(rewriter),
|
|
1079
|
+
limit: this.limit,
|
|
1080
|
+
offset: this.offset,
|
|
1081
|
+
selectAllIntent: this.selectAllIntent
|
|
1082
|
+
});
|
|
1083
|
+
return rewriter.select ? rewriter.select(rewritten) : rewritten;
|
|
1084
|
+
}
|
|
1085
|
+
collectColumnRefs() {
|
|
1086
|
+
const refs = [];
|
|
1087
|
+
const pushRefs = (columns) => {
|
|
1088
|
+
refs.push(...columns);
|
|
1089
|
+
};
|
|
1090
|
+
if (this.from.kind === "derived-table-source") pushRefs(this.from.query.collectColumnRefs());
|
|
1091
|
+
for (const projection of this.projection) if (!(projection.expr.kind === "literal")) pushRefs(projection.expr.collectColumnRefs());
|
|
1092
|
+
if (this.where) pushRefs(this.where.collectColumnRefs());
|
|
1093
|
+
if (this.having) pushRefs(this.having.collectColumnRefs());
|
|
1094
|
+
for (const orderItem of this.orderBy ?? []) pushRefs(orderItem.expr.collectColumnRefs());
|
|
1095
|
+
for (const expr of this.distinctOn ?? []) pushRefs(expr.collectColumnRefs());
|
|
1096
|
+
for (const expr of this.groupBy ?? []) pushRefs(expr.collectColumnRefs());
|
|
1097
|
+
for (const join of this.joins ?? []) {
|
|
1098
|
+
if (join.source.kind === "derived-table-source") pushRefs(join.source.query.collectColumnRefs());
|
|
1099
|
+
if (join.on.kind === "eq-col-join-on") refs.push(join.on.left, join.on.right);
|
|
1100
|
+
else pushRefs(join.on.collectColumnRefs());
|
|
1101
|
+
}
|
|
1102
|
+
return refs;
|
|
1103
|
+
}
|
|
1104
|
+
collectParamRefs() {
|
|
1105
|
+
const refs = [];
|
|
1106
|
+
const pushRefs = (params) => {
|
|
1107
|
+
refs.push(...params);
|
|
1108
|
+
};
|
|
1109
|
+
if (this.from.kind === "derived-table-source") pushRefs(this.from.query.collectParamRefs());
|
|
1110
|
+
for (const projection of this.projection) if (!(projection.expr.kind === "literal")) pushRefs(projection.expr.collectParamRefs());
|
|
1111
|
+
if (this.where) pushRefs(this.where.collectParamRefs());
|
|
1112
|
+
if (this.having) pushRefs(this.having.collectParamRefs());
|
|
1113
|
+
for (const orderItem of this.orderBy ?? []) pushRefs(orderItem.expr.collectParamRefs());
|
|
1114
|
+
for (const expr of this.distinctOn ?? []) pushRefs(expr.collectParamRefs());
|
|
1115
|
+
for (const expr of this.groupBy ?? []) pushRefs(expr.collectParamRefs());
|
|
1116
|
+
for (const join of this.joins ?? []) {
|
|
1117
|
+
if (join.source.kind === "derived-table-source") pushRefs(join.source.query.collectParamRefs());
|
|
1118
|
+
if (!(join.on.kind === "eq-col-join-on")) pushRefs(join.on.collectParamRefs());
|
|
1119
|
+
}
|
|
1120
|
+
return refs;
|
|
1121
|
+
}
|
|
1122
|
+
collectRefs() {
|
|
1123
|
+
const tables = /* @__PURE__ */ new Set();
|
|
1124
|
+
const columns = /* @__PURE__ */ new Map();
|
|
1125
|
+
const addSource = (source) => {
|
|
1126
|
+
mergeRefsInto(source.collectRefs(), tables, columns);
|
|
1127
|
+
};
|
|
1128
|
+
addSource(this.from);
|
|
1129
|
+
for (const join of this.joins ?? []) {
|
|
1130
|
+
addSource(join.source);
|
|
1131
|
+
if (join.on.kind === "eq-col-join-on") {
|
|
1132
|
+
addColumnRefToRefSets(join.on.left, tables, columns);
|
|
1133
|
+
addColumnRefToRefSets(join.on.right, tables, columns);
|
|
1134
|
+
} else for (const columnRef of join.on.collectColumnRefs()) addColumnRefToRefSets(columnRef, tables, columns);
|
|
1135
|
+
}
|
|
1136
|
+
for (const columnRef of this.collectColumnRefs()) addColumnRefToRefSets(columnRef, tables, columns);
|
|
1137
|
+
return sortRefs(tables, columns);
|
|
1138
|
+
}
|
|
1139
|
+
toQueryAst() {
|
|
1140
|
+
return this;
|
|
1141
|
+
}
|
|
1142
|
+
};
|
|
1143
|
+
var InsertOnConflictAction = class extends AstNode {};
|
|
1144
|
+
var DoNothingConflictAction = class extends InsertOnConflictAction {
|
|
1145
|
+
kind = "do-nothing";
|
|
1146
|
+
constructor() {
|
|
1147
|
+
super();
|
|
1148
|
+
this.freeze();
|
|
1149
|
+
}
|
|
1150
|
+
toInsertOnConflictAction() {
|
|
1151
|
+
return this;
|
|
1152
|
+
}
|
|
1153
|
+
};
|
|
1154
|
+
var DoUpdateSetConflictAction = class extends InsertOnConflictAction {
|
|
1155
|
+
kind = "do-update-set";
|
|
1156
|
+
set;
|
|
1157
|
+
constructor(set) {
|
|
1158
|
+
super();
|
|
1159
|
+
this.set = frozenRecordCopy(set);
|
|
1160
|
+
this.freeze();
|
|
1161
|
+
}
|
|
1162
|
+
toInsertOnConflictAction() {
|
|
1163
|
+
return this;
|
|
1164
|
+
}
|
|
1165
|
+
};
|
|
1166
|
+
var InsertOnConflict = class InsertOnConflict extends AstNode {
|
|
1167
|
+
kind = "insert-on-conflict";
|
|
1168
|
+
columns;
|
|
1169
|
+
action;
|
|
1170
|
+
constructor(columns, action) {
|
|
1171
|
+
super();
|
|
1172
|
+
this.columns = frozenArrayCopy(columns);
|
|
1173
|
+
this.action = action;
|
|
1174
|
+
this.freeze();
|
|
1175
|
+
}
|
|
1176
|
+
static on(columns) {
|
|
1177
|
+
return new InsertOnConflict(columns, new DoNothingConflictAction());
|
|
1178
|
+
}
|
|
1179
|
+
doNothing() {
|
|
1180
|
+
return new InsertOnConflict(this.columns, new DoNothingConflictAction());
|
|
1181
|
+
}
|
|
1182
|
+
doUpdateSet(set) {
|
|
1183
|
+
return new InsertOnConflict(this.columns, new DoUpdateSetConflictAction(set));
|
|
1184
|
+
}
|
|
1185
|
+
};
|
|
1186
|
+
var InsertAst = class InsertAst extends QueryAst {
|
|
1187
|
+
kind = "insert";
|
|
1188
|
+
table;
|
|
1189
|
+
rows;
|
|
1190
|
+
onConflict;
|
|
1191
|
+
returning;
|
|
1192
|
+
constructor(table, rows = [{}], onConflict, returning) {
|
|
1193
|
+
super();
|
|
1194
|
+
this.table = table;
|
|
1195
|
+
this.rows = freezeRows(rows);
|
|
1196
|
+
this.onConflict = onConflict;
|
|
1197
|
+
this.returning = returning && returning.length > 0 ? frozenArrayCopy(returning) : void 0;
|
|
1198
|
+
this.freeze();
|
|
1199
|
+
}
|
|
1200
|
+
static into(table) {
|
|
1201
|
+
return new InsertAst(table);
|
|
1202
|
+
}
|
|
1203
|
+
withValues(values) {
|
|
1204
|
+
return new InsertAst(this.table, [{ ...values }], this.onConflict, this.returning);
|
|
1205
|
+
}
|
|
1206
|
+
withRows(rows) {
|
|
1207
|
+
return new InsertAst(this.table, rows.map((row) => ({ ...row })), this.onConflict, this.returning);
|
|
1208
|
+
}
|
|
1209
|
+
withReturning(returning) {
|
|
1210
|
+
return new InsertAst(this.table, this.rows.map((row) => ({ ...row })), this.onConflict, returning);
|
|
1211
|
+
}
|
|
1212
|
+
withOnConflict(onConflict) {
|
|
1213
|
+
return new InsertAst(this.table, this.rows.map((row) => ({ ...row })), onConflict, this.returning);
|
|
1214
|
+
}
|
|
1215
|
+
collectParamRefs() {
|
|
1216
|
+
const refs = [];
|
|
1217
|
+
for (const row of this.rows) for (const value of Object.values(row)) if (value.kind === "param-ref") refs.push(value);
|
|
1218
|
+
if (this.onConflict?.action.kind === "do-update-set") {
|
|
1219
|
+
for (const value of Object.values(this.onConflict.action.set)) if (value.kind === "param-ref") refs.push(value);
|
|
1220
|
+
}
|
|
1221
|
+
return refs;
|
|
1222
|
+
}
|
|
1223
|
+
collectRefs() {
|
|
1224
|
+
const tables = new Set([this.table.name]);
|
|
1225
|
+
const columns = /* @__PURE__ */ new Map();
|
|
1226
|
+
const addColumn = (columnRef) => addColumnRefToRefSets(columnRef, tables, columns);
|
|
1227
|
+
const addValue = (value) => {
|
|
1228
|
+
if (value.kind === "column-ref") addColumn(value);
|
|
1229
|
+
};
|
|
1230
|
+
for (const row of this.rows) for (const value of Object.values(row)) addValue(value);
|
|
1231
|
+
for (const columnRef of this.returning ?? []) addColumn(columnRef);
|
|
1232
|
+
if (this.onConflict) {
|
|
1233
|
+
for (const columnRef of this.onConflict.columns) addColumn(columnRef);
|
|
1234
|
+
if (this.onConflict.action.kind === "do-update-set") {
|
|
1235
|
+
for (const value of Object.values(this.onConflict.action.set)) if (value.kind === "column-ref") addColumn(value);
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
return sortRefs(tables, columns);
|
|
1239
|
+
}
|
|
1240
|
+
toQueryAst() {
|
|
1241
|
+
return this;
|
|
1242
|
+
}
|
|
1243
|
+
};
|
|
1244
|
+
var UpdateAst = class UpdateAst extends QueryAst {
|
|
1245
|
+
kind = "update";
|
|
1246
|
+
table;
|
|
1247
|
+
set;
|
|
1248
|
+
where;
|
|
1249
|
+
returning;
|
|
1250
|
+
constructor(table, set = {}, where, returning) {
|
|
1251
|
+
super();
|
|
1252
|
+
this.table = table;
|
|
1253
|
+
this.set = frozenRecordCopy(set);
|
|
1254
|
+
this.where = where;
|
|
1255
|
+
this.returning = returning && returning.length > 0 ? frozenArrayCopy(returning) : void 0;
|
|
1256
|
+
this.freeze();
|
|
1257
|
+
}
|
|
1258
|
+
static table(table) {
|
|
1259
|
+
return new UpdateAst(table);
|
|
1260
|
+
}
|
|
1261
|
+
withSet(set) {
|
|
1262
|
+
return new UpdateAst(this.table, set, this.where, this.returning);
|
|
1263
|
+
}
|
|
1264
|
+
withWhere(where) {
|
|
1265
|
+
return new UpdateAst(this.table, this.set, where, this.returning);
|
|
1266
|
+
}
|
|
1267
|
+
withReturning(returning) {
|
|
1268
|
+
return new UpdateAst(this.table, this.set, this.where, returning);
|
|
1269
|
+
}
|
|
1270
|
+
collectParamRefs() {
|
|
1271
|
+
const refs = [];
|
|
1272
|
+
for (const value of Object.values(this.set)) if (value.kind === "param-ref") refs.push(value);
|
|
1273
|
+
if (this.where) refs.push(...this.where.collectParamRefs());
|
|
1274
|
+
return refs;
|
|
1275
|
+
}
|
|
1276
|
+
collectRefs() {
|
|
1277
|
+
const tables = new Set([this.table.name]);
|
|
1278
|
+
const columns = /* @__PURE__ */ new Map();
|
|
1279
|
+
for (const value of Object.values(this.set)) if (value.kind === "column-ref") addColumnRefToRefSets(value, tables, columns);
|
|
1280
|
+
for (const columnRef of this.where?.collectColumnRefs() ?? []) addColumnRefToRefSets(columnRef, tables, columns);
|
|
1281
|
+
for (const columnRef of this.returning ?? []) addColumnRefToRefSets(columnRef, tables, columns);
|
|
1282
|
+
return sortRefs(tables, columns);
|
|
1283
|
+
}
|
|
1284
|
+
toQueryAst() {
|
|
1285
|
+
return this;
|
|
1286
|
+
}
|
|
1287
|
+
};
|
|
1288
|
+
var DeleteAst = class DeleteAst extends QueryAst {
|
|
1289
|
+
kind = "delete";
|
|
1290
|
+
table;
|
|
1291
|
+
where;
|
|
1292
|
+
returning;
|
|
1293
|
+
constructor(table, where, returning) {
|
|
1294
|
+
super();
|
|
1295
|
+
this.table = table;
|
|
1296
|
+
this.where = where;
|
|
1297
|
+
this.returning = returning && returning.length > 0 ? frozenArrayCopy(returning) : void 0;
|
|
1298
|
+
this.freeze();
|
|
1299
|
+
}
|
|
1300
|
+
static from(table) {
|
|
1301
|
+
return new DeleteAst(table);
|
|
1302
|
+
}
|
|
1303
|
+
withWhere(where) {
|
|
1304
|
+
return new DeleteAst(this.table, where, this.returning);
|
|
1305
|
+
}
|
|
1306
|
+
withReturning(returning) {
|
|
1307
|
+
return new DeleteAst(this.table, this.where, returning);
|
|
1308
|
+
}
|
|
1309
|
+
collectParamRefs() {
|
|
1310
|
+
return this.where?.collectParamRefs() ?? [];
|
|
1311
|
+
}
|
|
1312
|
+
collectRefs() {
|
|
1313
|
+
const tables = new Set([this.table.name]);
|
|
1314
|
+
const columns = /* @__PURE__ */ new Map();
|
|
1315
|
+
for (const columnRef of this.where?.collectColumnRefs() ?? []) addColumnRefToRefSets(columnRef, tables, columns);
|
|
1316
|
+
for (const columnRef of this.returning ?? []) addColumnRefToRefSets(columnRef, tables, columns);
|
|
1317
|
+
return sortRefs(tables, columns);
|
|
1318
|
+
}
|
|
1319
|
+
toQueryAst() {
|
|
1320
|
+
return this;
|
|
1321
|
+
}
|
|
1322
|
+
};
|
|
1323
|
+
const queryAstKinds = new Set([
|
|
1324
|
+
"select",
|
|
1325
|
+
"insert",
|
|
1326
|
+
"update",
|
|
1327
|
+
"delete"
|
|
1328
|
+
]);
|
|
1329
|
+
const whereExprKinds = new Set([
|
|
1330
|
+
"binary",
|
|
1331
|
+
"and",
|
|
1332
|
+
"or",
|
|
1333
|
+
"exists",
|
|
1334
|
+
"null-check",
|
|
1335
|
+
"not"
|
|
1336
|
+
]);
|
|
1337
|
+
function isQueryAst(value) {
|
|
1338
|
+
return typeof value === "object" && value !== null && "kind" in value && queryAstKinds.has(value.kind);
|
|
1339
|
+
}
|
|
1340
|
+
function isWhereExpr(value) {
|
|
1341
|
+
return typeof value === "object" && value !== null && "kind" in value && whereExprKinds.has(value.kind);
|
|
1342
|
+
}
|
|
1343
|
+
|
|
1344
|
+
//#endregion
|
|
1345
|
+
//#region src/ast/util.ts
|
|
1346
|
+
function compact(o) {
|
|
1347
|
+
const out = {};
|
|
1348
|
+
for (const [k, v] of Object.entries(o)) {
|
|
1349
|
+
if (v === void 0 || v === null) continue;
|
|
1350
|
+
if (Array.isArray(v) && v.length === 0) continue;
|
|
1351
|
+
out[k] = v;
|
|
1352
|
+
}
|
|
1353
|
+
return out;
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1356
|
+
//#endregion
|
|
1357
|
+
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 };
|
|
1358
|
+
//# sourceMappingURL=ast.mjs.map
|