@nongt/visql-core 0.3.1 → 0.4.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.
|
@@ -1,68 +1,119 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var b = (t, e, r) =>
|
|
4
|
-
import {
|
|
5
|
-
import { ag as D, aA as
|
|
6
|
-
import { s as
|
|
7
|
-
class
|
|
8
|
-
constructor(r, n, s) {
|
|
9
|
-
|
|
1
|
+
var pt = Object.defineProperty;
|
|
2
|
+
var At = (t, e, r) => e in t ? pt(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
|
|
3
|
+
var b = (t, e, r) => At(t, typeof e != "symbol" ? e + "" : e, r);
|
|
4
|
+
import { q as L, F as mt, G as gt, g as M, o as H, d as G, t as V, h as ht, w as W, H as Dt, k as Nt, b as Ot, i as St } from "./schema-z95dxLxu.js";
|
|
5
|
+
import { ag as D, aA as J, aB as bt, aC as Rt, aD as It, aj as Ct, V as Lt, ap as Mt, aE as A, al as wt, a7 as Ft, aF as qt, aG as K, $ as Ut } from "./issueLocation-CWUdTtCh.js";
|
|
6
|
+
import { s as z } from "./calendarDate-CYlr9TvX.js";
|
|
7
|
+
class vt extends Error {
|
|
8
|
+
constructor(r, n, s, i) {
|
|
9
|
+
const a = i ? `"${n}" (as "${i}")` : `"${n}"`;
|
|
10
|
+
super(`scopeQuery: the query at ${r.length ? r.join(".") : "the top"} reads ${a} but can't reach "${s}", which the condition names`);
|
|
10
11
|
/** Where the body is, as keys into the query (validateQuery's
|
|
11
12
|
* `pathSegments` style). */
|
|
12
13
|
b(this, "path");
|
|
13
14
|
/** The table it reads that needs scope. */
|
|
14
15
|
b(this, "table");
|
|
15
|
-
|
|
16
|
+
/** The alias that copy of the table goes by, when it has one (a self
|
|
17
|
+
* join reads a table more than once, issue 216). */
|
|
18
|
+
b(this, "alias");
|
|
19
|
+
this.name = "ScopeError", this.path = r, this.table = n, i && (this.alias = i);
|
|
16
20
|
}
|
|
17
21
|
}
|
|
18
|
-
const
|
|
19
|
-
function
|
|
22
|
+
const Y = (t) => Object.prototype.toString.call(t) === "[object Object]", dt = (t) => Array.isArray(t.joins) && Y(t.from);
|
|
23
|
+
function w(t, e, r) {
|
|
20
24
|
if (Array.isArray(t)) {
|
|
21
|
-
const a = t.map((o, f) =>
|
|
25
|
+
const a = t.map((o, f) => w(o, [...e, f], r));
|
|
22
26
|
return a.every((o, f) => o === t[f]) ? t : a;
|
|
23
27
|
}
|
|
24
|
-
if (!
|
|
28
|
+
if (!Y(t)) return t;
|
|
25
29
|
let n = !1;
|
|
26
30
|
const s = Object.entries(t).map(([a, o]) => {
|
|
27
|
-
const f =
|
|
31
|
+
const f = w(o, [...e, a], r);
|
|
28
32
|
return f !== o && (n = !0), [a, f];
|
|
29
33
|
}), i = n ? Object.fromEntries(s) : t;
|
|
30
|
-
return
|
|
31
|
-
}
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
return
|
|
35
|
-
}
|
|
36
|
-
function
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
34
|
+
return dt(i) ? r(i, e) : i;
|
|
35
|
+
}
|
|
36
|
+
const Z = (t, e) => L(t).flatMap((r) => r.table !== void 0 && e.requires(r.table) ? [{ table: r.table, ...r.alias ? { alias: r.alias } : {}, ref: r.alias ?? r.table }] : []), Bt = (t) => L(t).flatMap((e) => {
|
|
37
|
+
const r = e.alias ?? e.table;
|
|
38
|
+
return r === void 0 ? [] : [r];
|
|
39
|
+
});
|
|
40
|
+
function v(t, e) {
|
|
41
|
+
if (t.type === "field") {
|
|
42
|
+
const r = t.table && e.get(t.table);
|
|
43
|
+
return r ? { ...t, table: r } : t;
|
|
44
|
+
}
|
|
45
|
+
return H(t, (r) => v(r, e));
|
|
46
|
+
}
|
|
47
|
+
const tt = (t) => t ? t.type === "logical" && t.operator === "AND" ? t.operands : [t] : [];
|
|
48
|
+
function jt(t, e) {
|
|
49
|
+
var s, i;
|
|
50
|
+
if (e.type !== "binary" || ((s = e.left) == null ? void 0 : s.type) !== "field" || ((i = e.right) == null ? void 0 : i.type) !== "field")
|
|
51
|
+
return !1;
|
|
52
|
+
const r = [e.left.table, e.right.table], n = { ...e, left: e.right, right: e.left };
|
|
53
|
+
return t.joins.some((a) => r.includes(a.alias ?? a.table) && tt(a.on).some((o) => M(o, e) || M(o, n)));
|
|
54
|
+
}
|
|
55
|
+
function et(t, e, r, n) {
|
|
56
|
+
const s = /* @__PURE__ */ new Map([[r.table, r.ref]]), i = [], a = new Set(Bt(e));
|
|
57
|
+
let o;
|
|
58
|
+
return J(n.condition).forEach((f) => {
|
|
59
|
+
if (o || s.has(f)) return;
|
|
60
|
+
const u = mt(t, [...s.keys()], f);
|
|
61
|
+
if (!u) {
|
|
62
|
+
o = f;
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
u.forEach((c) => {
|
|
66
|
+
const l = gt(t, [...s.keys()], c);
|
|
67
|
+
if (!l) return;
|
|
68
|
+
const $ = (m) => v(l.on, new Map([...s, [c, m]])), y = L(e).flatMap((m) => m.table === c ? [m.alias ?? c] : []).find((m) => jt(e, $(m)));
|
|
69
|
+
if (y) {
|
|
70
|
+
s.set(c, y);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
let T = c;
|
|
74
|
+
for (let m = 2; a.has(T); m += 1) T = `${c}_${m}`;
|
|
75
|
+
a.add(T), s.set(c, T), i.push({
|
|
76
|
+
id: bt(),
|
|
77
|
+
table: c,
|
|
78
|
+
...T !== c ? { alias: T } : {},
|
|
79
|
+
joinType: l.joinType,
|
|
80
|
+
on: $(T)
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
}), { joins: i, condition: v(n.condition, s), ...o ? { missing: o } : {} };
|
|
84
|
+
}
|
|
85
|
+
function rt(t, e) {
|
|
86
|
+
return tt(t).some((r) => M(r, e));
|
|
87
|
+
}
|
|
88
|
+
function _t(t, e) {
|
|
89
|
+
return (t == null ? void 0 : t.type) === "logical" && t.operator === "AND" ? { ...t, operands: [...t.operands, e] } : t ? { type: "logical", operator: "AND", operands: [t, e] } : e;
|
|
90
|
+
}
|
|
91
|
+
function x(t, e, r, n) {
|
|
92
|
+
return Z(e, r).reduce((s, i) => {
|
|
93
|
+
const a = et(t, s, i, r);
|
|
94
|
+
if (a.missing) throw new vt(n, i.table, a.missing, i.alias);
|
|
95
|
+
const o = rt(s.where, a.condition);
|
|
96
|
+
return a.joins.length === 0 && o ? s : {
|
|
97
|
+
...s,
|
|
98
|
+
joins: [...s.joins, ...a.joins],
|
|
99
|
+
where: o ? s.where : _t(s.where, a.condition)
|
|
100
|
+
};
|
|
101
|
+
}, e);
|
|
102
|
+
}
|
|
103
|
+
function me(t, e, r) {
|
|
104
|
+
const n = x(t, e, r, []), s = D(t, e, n), i = w(s, [], (a, o) => o.length === 0 ? a : x(t, a, r, o));
|
|
56
105
|
return D(t, s, i);
|
|
57
106
|
}
|
|
58
|
-
function
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
107
|
+
function ge(t, e, r) {
|
|
108
|
+
if (!Y(t) || !Array.isArray(t.tables))
|
|
109
|
+
throw new TypeError("unscopedBodies takes (schema, query, rule) since 0.4.0");
|
|
110
|
+
const n = [];
|
|
111
|
+
return w(e, [], (s, i) => (Z(s, r).forEach((a) => {
|
|
112
|
+
const o = et(t, s, a, r);
|
|
113
|
+
(o.missing || o.joins.length > 0 || !rt(s.where, o.condition)) && n.push({ path: i, table: a.table, ...a.alias ? { alias: a.alias } : {} });
|
|
114
|
+
}), s)), n;
|
|
64
115
|
}
|
|
65
|
-
const
|
|
116
|
+
const Ht = (t) => typeof t == "object" && t !== null && !(t instanceof Date), Q = {
|
|
66
117
|
/** A column: `expr.field('orders', 'status')`. */
|
|
67
118
|
field: (t, e) => ({ type: "field", table: t, column: e }),
|
|
68
119
|
/** A literal value, written into the SQL (as a bound parameter by
|
|
@@ -129,7 +180,7 @@ const qt = (t) => typeof t == "object" && t !== null && !(t instanceof Date), Y
|
|
|
129
180
|
type: "binary",
|
|
130
181
|
operator: "IN",
|
|
131
182
|
left: t,
|
|
132
|
-
right: Array.isArray(e) ?
|
|
183
|
+
right: Array.isArray(e) ? Q.list(...e) : e
|
|
133
184
|
}),
|
|
134
185
|
/** `left BETWEEN low AND high` (issue 169), both bounds included: each a
|
|
135
186
|
* value or an expression (a param, a relative date). NOT BETWEEN:
|
|
@@ -138,13 +189,13 @@ const qt = (t) => typeof t == "object" && t !== null && !(t instanceof Date), Y
|
|
|
138
189
|
type: "binary",
|
|
139
190
|
operator: "BETWEEN",
|
|
140
191
|
left: t,
|
|
141
|
-
right:
|
|
192
|
+
right: Q.list(e, r)
|
|
142
193
|
}),
|
|
143
194
|
/** IN's values: each a value (`expr.list(1, 2)`) or an expression
|
|
144
195
|
* (`expr.list(expr.param('a'), expr.param('b'))`). */
|
|
145
196
|
list: (...t) => ({
|
|
146
197
|
type: "list",
|
|
147
|
-
items: t.map((e) =>
|
|
198
|
+
items: t.map((e) => Ht(e) ? e : { type: "literal", value: e })
|
|
148
199
|
}),
|
|
149
200
|
/** A query of its own for IN to compare with, selecting `select` — one
|
|
150
201
|
* column. Its tables are its own: it sees nothing of the query around
|
|
@@ -153,91 +204,91 @@ const qt = (t) => typeof t == "object" && t !== null && !(t instanceof Date), Y
|
|
|
153
204
|
* 'customer_id') }))`. */
|
|
154
205
|
subquery: ({ select: t, ...e }) => ({
|
|
155
206
|
type: "subquery",
|
|
156
|
-
id:
|
|
157
|
-
query: { joins: [], ...e, select: [{ id:
|
|
207
|
+
id: G(),
|
|
208
|
+
query: { joins: [], ...e, select: [{ id: G(), expression: t }] }
|
|
158
209
|
})
|
|
159
210
|
};
|
|
160
|
-
function
|
|
161
|
-
return
|
|
211
|
+
function he(t, e, r) {
|
|
212
|
+
return A(t, e, { type: "setBaseTable", table: r });
|
|
162
213
|
}
|
|
163
|
-
function
|
|
214
|
+
function De(t, e, r, n = {}) {
|
|
164
215
|
const s = { type: "addJoin", table: r, ...n };
|
|
165
|
-
return
|
|
216
|
+
return A(t, e, s);
|
|
166
217
|
}
|
|
167
|
-
function
|
|
168
|
-
return
|
|
218
|
+
function Ne(t, e, r) {
|
|
219
|
+
return A(t, e, { type: "removeJoin", joinId: r });
|
|
169
220
|
}
|
|
170
|
-
function
|
|
171
|
-
const s =
|
|
221
|
+
function Yt(t, e, r, n = {}) {
|
|
222
|
+
const s = Rt(e), i = J(r);
|
|
172
223
|
if (s && i.some((u) => u !== s.alias)) {
|
|
173
224
|
if (i.includes(s.alias))
|
|
174
225
|
throw new Error("addCondition: a condition names both the union and tables of its branches: add it in two parts");
|
|
175
226
|
return s.query.union.forEach((u) => {
|
|
176
|
-
const c = u.from.table, l = new Set(
|
|
177
|
-
if (
|
|
178
|
-
throw new Error(`addCondition: "${
|
|
179
|
-
}), s.query.union.reduce((u, c, l) =>
|
|
227
|
+
const c = u.from.table, l = new Set(L(u).map(V)), $ = It(t, c), y = i.find((T) => !l.has(T) && !$.has(T));
|
|
228
|
+
if (y)
|
|
229
|
+
throw new Error(`addCondition: "${y}" can't be reached from "${c}", one of the union's tables: a condition must reach every branch`);
|
|
230
|
+
}), s.query.union.reduce((u, c, l) => Ct(
|
|
180
231
|
u,
|
|
181
|
-
[
|
|
182
|
-
($) =>
|
|
232
|
+
[Lt(s.id, l)],
|
|
233
|
+
($) => Yt(t, $, r, n)
|
|
183
234
|
), e);
|
|
184
235
|
}
|
|
185
236
|
const a = n.operator ?? "AND", { where: o } = e;
|
|
186
237
|
let f;
|
|
187
|
-
return o ? o.type === "logical" && o.operator === a ? f = { ...o, operands: [...o.operands, r] } : f = { type: "logical", operator: a, operands: [o, r] } : f = r, D(t, e,
|
|
238
|
+
return o ? o.type === "logical" && o.operator === a ? f = { ...o, operands: [...o.operands, r] } : f = { type: "logical", operator: a, operands: [o, r] } : f = r, D(t, e, Mt(t, e, f));
|
|
188
239
|
}
|
|
189
|
-
function
|
|
190
|
-
const s =
|
|
240
|
+
function Oe(t, e, r, n) {
|
|
241
|
+
const s = A(t, e, { type: "addSelectItem" }), i = s.select[s.select.length - 1].id, a = A(t, s, {
|
|
191
242
|
type: "setSelectExpression",
|
|
192
243
|
selectId: i,
|
|
193
244
|
expression: r
|
|
194
|
-
}), o = n === void 0 ? a :
|
|
245
|
+
}), o = n === void 0 ? a : A(t, a, { type: "setSelectAlias", selectId: i, alias: n });
|
|
195
246
|
return D(t, e, o);
|
|
196
247
|
}
|
|
197
|
-
function
|
|
198
|
-
const n =
|
|
248
|
+
function Se(t, e, r) {
|
|
249
|
+
const n = A(t, e, { type: "addGroupByItem" }), { id: s } = n.groupBy[n.groupBy.length - 1], i = A(t, n, {
|
|
199
250
|
type: "setGroupByExpression",
|
|
200
251
|
groupById: s,
|
|
201
252
|
expression: r
|
|
202
253
|
});
|
|
203
254
|
return D(t, e, i);
|
|
204
255
|
}
|
|
205
|
-
function
|
|
206
|
-
return D(t, e,
|
|
256
|
+
function be(t, e, r) {
|
|
257
|
+
return D(t, e, wt(t, e, r));
|
|
207
258
|
}
|
|
208
|
-
function
|
|
209
|
-
const s =
|
|
259
|
+
function Re(t, e, r, n = "asc") {
|
|
260
|
+
const s = A(t, e, { type: "addOrderByItem" }), { id: i } = s.orderBy[s.orderBy.length - 1], a = A(t, s, {
|
|
210
261
|
type: "setOrderByExpression",
|
|
211
262
|
orderById: i,
|
|
212
263
|
expression: r
|
|
213
|
-
}), o =
|
|
264
|
+
}), o = A(t, a, {
|
|
214
265
|
type: "setOrderByDirection",
|
|
215
266
|
orderById: i,
|
|
216
267
|
direction: n
|
|
217
268
|
});
|
|
218
269
|
return D(t, e, o);
|
|
219
270
|
}
|
|
220
|
-
function
|
|
271
|
+
function Ie(t, e, r) {
|
|
221
272
|
let n = e;
|
|
222
|
-
return "limit" in r && (n =
|
|
273
|
+
return "limit" in r && (n = A(t, n, { type: "setLimit", limit: r.limit })), "offset" in r && (n = A(t, n, { type: "setOffset", offset: r.offset })), n;
|
|
223
274
|
}
|
|
224
|
-
function
|
|
225
|
-
return
|
|
275
|
+
function Ce(t, e, r) {
|
|
276
|
+
return A(t, e, { type: "setDistinct", distinct: r });
|
|
226
277
|
}
|
|
227
|
-
function
|
|
228
|
-
if (!
|
|
278
|
+
function Le(t) {
|
|
279
|
+
if (!ht(t)) throw new Error(`query "${t.id}" has no base table yet`);
|
|
229
280
|
return t;
|
|
230
281
|
}
|
|
231
|
-
const
|
|
232
|
-
class
|
|
282
|
+
const d = 4;
|
|
283
|
+
class S extends Error {
|
|
233
284
|
constructor(r, n) {
|
|
234
285
|
super(n);
|
|
235
286
|
b(this, "kind");
|
|
236
287
|
this.name = "QueryFormatError", this.kind = r;
|
|
237
288
|
}
|
|
238
289
|
}
|
|
239
|
-
const
|
|
240
|
-
function
|
|
290
|
+
const C = (t) => typeof t == "object" && t !== null && !Array.isArray(t);
|
|
291
|
+
function Pt(t) {
|
|
241
292
|
const e = [];
|
|
242
293
|
let r = 0;
|
|
243
294
|
for (; r < t.length; ) {
|
|
@@ -256,65 +307,65 @@ function vt(t) {
|
|
|
256
307
|
}
|
|
257
308
|
return e;
|
|
258
309
|
}
|
|
259
|
-
function
|
|
310
|
+
function Gt(t) {
|
|
260
311
|
const e = (r) => {
|
|
261
312
|
if (Array.isArray(r)) return r.map(e);
|
|
262
|
-
if (!
|
|
313
|
+
if (!C(r)) return r;
|
|
263
314
|
const n = Object.fromEntries(Object.entries(r).map(([a, o]) => [a, e(o)])), { right: s } = n;
|
|
264
|
-
if (n.type !== "binary" || n.operator !== "IN" || !
|
|
265
|
-
const i = s.type === "literal" && typeof s.value == "string" ?
|
|
315
|
+
if (n.type !== "binary" || n.operator !== "IN" || !C(s) || s.type === "subquery" || s.type === "list") return n;
|
|
316
|
+
const i = s.type === "literal" && typeof s.value == "string" ? Pt(s.value).map((a) => ({ type: "literal", value: a })) : [s];
|
|
266
317
|
return { ...n, right: { type: "list", items: i } };
|
|
267
318
|
};
|
|
268
319
|
return e(t);
|
|
269
320
|
}
|
|
270
|
-
const
|
|
271
|
-
function
|
|
321
|
+
const xt = (t) => t, Qt = (t) => t, Xt = [Gt, xt, Qt];
|
|
322
|
+
function kt(t, e, r) {
|
|
272
323
|
return r.slice(e - 1).reduce((n, s) => s(n), t);
|
|
273
324
|
}
|
|
274
|
-
function
|
|
275
|
-
return Array.isArray(t) ? t.map((r) =>
|
|
325
|
+
function F(t, e) {
|
|
326
|
+
return Array.isArray(t) ? t.map((r) => F(r, e)) : C(t) ? t.type === "literal" ? e(t) : Object.fromEntries(Object.entries(t).map(([r, n]) => [r, F(n, e)])) : t;
|
|
276
327
|
}
|
|
277
|
-
function
|
|
328
|
+
function Vt(t) {
|
|
278
329
|
return t.value instanceof Date ? { ...t, value: t.value.toISOString(), valueType: "date" } : typeof t.value == "bigint" ? { ...t, value: String(t.value), valueType: "bigint" } : t;
|
|
279
330
|
}
|
|
280
|
-
function
|
|
331
|
+
function Wt(t) {
|
|
281
332
|
if (t.valueType === void 0) return t;
|
|
282
333
|
const { valueType: e, ...r } = t, n = String(r.value);
|
|
283
334
|
let s;
|
|
284
335
|
if (e === "date") {
|
|
285
336
|
if (s = new Date(n), Number.isNaN(s.getTime()))
|
|
286
|
-
throw new
|
|
337
|
+
throw new S("invalid", `not a date: ${JSON.stringify(n)}`);
|
|
287
338
|
} else if (e === "bigint" && /^-?\d+$/.test(n))
|
|
288
339
|
s = BigInt(n);
|
|
289
340
|
else {
|
|
290
341
|
const i = String(e);
|
|
291
|
-
throw new
|
|
342
|
+
throw new S("invalid", `can't read a ${i} from ${JSON.stringify(n)}`);
|
|
292
343
|
}
|
|
293
344
|
return { ...r, value: s };
|
|
294
345
|
}
|
|
295
|
-
function
|
|
346
|
+
function Me(t) {
|
|
296
347
|
return {
|
|
297
|
-
version:
|
|
298
|
-
query:
|
|
348
|
+
version: d,
|
|
349
|
+
query: F(t, Vt)
|
|
299
350
|
};
|
|
300
351
|
}
|
|
301
|
-
function
|
|
302
|
-
if (!
|
|
303
|
-
throw new
|
|
352
|
+
function we(t) {
|
|
353
|
+
if (!C(t))
|
|
354
|
+
throw new S("invalid", "a stored query is an object");
|
|
304
355
|
const { version: e } = t;
|
|
305
356
|
if (typeof e != "number" || !Number.isInteger(e) || e < 1)
|
|
306
|
-
throw new
|
|
307
|
-
if (e >
|
|
308
|
-
throw new
|
|
357
|
+
throw new S("invalid", "a stored query has a whole-number version from 1");
|
|
358
|
+
if (e > d)
|
|
359
|
+
throw new S(
|
|
309
360
|
"newerVersion",
|
|
310
|
-
`stored in format ${e}; this version reads up to ${
|
|
361
|
+
`stored in format ${e}; this version reads up to ${d}`
|
|
311
362
|
);
|
|
312
|
-
const r =
|
|
313
|
-
if (!
|
|
314
|
-
throw new
|
|
315
|
-
return
|
|
363
|
+
const r = kt(t.query, e, Xt);
|
|
364
|
+
if (!C(r) || typeof r.id != "string")
|
|
365
|
+
throw new S("invalid", "a stored query has a query with a string id");
|
|
366
|
+
return F(r, Wt);
|
|
316
367
|
}
|
|
317
|
-
function
|
|
368
|
+
function B(t, e) {
|
|
318
369
|
const r = new Map(e == null ? void 0 : e.tableOf), n = new Set(e == null ? void 0 : e.aliases), s = new Set(e == null ? void 0 : e.derived);
|
|
319
370
|
return t.forEach(({ table: i, alias: a }) => {
|
|
320
371
|
if (i === void 0) {
|
|
@@ -328,11 +379,11 @@ function U(t, e) {
|
|
|
328
379
|
n.delete(i), s.delete(i);
|
|
329
380
|
}), { tableOf: r, aliases: n, derived: s };
|
|
330
381
|
}
|
|
331
|
-
function
|
|
332
|
-
const n = t.on &&
|
|
333
|
-
return t.query ? { ...t, on: n, query:
|
|
382
|
+
function Jt(t, e, r) {
|
|
383
|
+
const n = t.on && I(t.on, e, r);
|
|
384
|
+
return t.query ? { ...t, on: n, query: Dt(t.query, (s) => q(s, r)) } : t.shared !== void 0 ? { ...t, on: n } : { ...t, on: n, table: r(t.table) };
|
|
334
385
|
}
|
|
335
|
-
function
|
|
386
|
+
function I(t, e, r) {
|
|
336
387
|
if (t.type === "field") {
|
|
337
388
|
if (!t.table || e.derived.has(t.table)) return t;
|
|
338
389
|
const n = e.tableOf.get(t.table) ?? t.table;
|
|
@@ -342,27 +393,27 @@ function S(t, e, r) {
|
|
|
342
393
|
...t.column ? { column: r(n, t.column) } : {}
|
|
343
394
|
};
|
|
344
395
|
}
|
|
345
|
-
if (t.type === "subquery") return { ...t, query:
|
|
396
|
+
if (t.type === "subquery") return { ...t, query: q(t.query, r) };
|
|
346
397
|
if (t.type === "exists") {
|
|
347
|
-
const { query: n } = t, s = [n.from, ...n.joins], i =
|
|
398
|
+
const { query: n } = t, s = [n.from, ...n.joins], i = B(s), a = B(s, e);
|
|
348
399
|
return {
|
|
349
400
|
...t,
|
|
350
|
-
link:
|
|
401
|
+
link: I(t.link, a, r),
|
|
351
402
|
query: {
|
|
352
403
|
...n,
|
|
353
404
|
from: { ...n.from, table: r(n.from.table) },
|
|
354
|
-
joins: n.joins.map((o) =>
|
|
355
|
-
...n.where ? { where:
|
|
405
|
+
joins: n.joins.map((o) => Jt(o, i, r)),
|
|
406
|
+
...n.where ? { where: I(n.where, i, r) } : {}
|
|
356
407
|
}
|
|
357
408
|
};
|
|
358
409
|
}
|
|
359
|
-
return
|
|
410
|
+
return H(t, (n) => I(n, e, r));
|
|
360
411
|
}
|
|
361
|
-
function
|
|
362
|
-
const r =
|
|
412
|
+
function q(t, e) {
|
|
413
|
+
const r = B([t.from, ...t.joins]), n = W(
|
|
363
414
|
t,
|
|
364
|
-
(s) =>
|
|
365
|
-
(s) =>
|
|
415
|
+
(s) => I(s, r, e),
|
|
416
|
+
(s) => q(s, e)
|
|
366
417
|
);
|
|
367
418
|
return {
|
|
368
419
|
...n,
|
|
@@ -370,11 +421,11 @@ function M(t, e) {
|
|
|
370
421
|
joins: n.joins.map((s) => s.table === void 0 ? s : { ...s, table: e(s.table) })
|
|
371
422
|
};
|
|
372
423
|
}
|
|
373
|
-
function
|
|
374
|
-
if (Array.isArray(t)) return t.map((s) =>
|
|
424
|
+
function j(t, e) {
|
|
425
|
+
if (Array.isArray(t)) return t.map((s) => j(s, e));
|
|
375
426
|
if (typeof t != "object" || t === null || t instanceof Date) return t;
|
|
376
427
|
const r = Object.fromEntries(Object.entries(t).map(
|
|
377
|
-
([s, i]) => [s,
|
|
428
|
+
([s, i]) => [s, j(i, e)]
|
|
378
429
|
));
|
|
379
430
|
if (!("from" in r) || !Array.isArray(r.joins)) return r;
|
|
380
431
|
const n = (s) => s.table !== void 0 && !s.alias && e(s.table) !== s.table ? { ...s, alias: s.table } : s;
|
|
@@ -384,17 +435,17 @@ function v(t, e) {
|
|
|
384
435
|
joins: r.joins.map(n)
|
|
385
436
|
};
|
|
386
437
|
}
|
|
387
|
-
function
|
|
388
|
-
return
|
|
438
|
+
function Kt(t, e) {
|
|
439
|
+
return q(j(t, e), e);
|
|
389
440
|
}
|
|
390
|
-
const
|
|
391
|
-
function
|
|
441
|
+
const zt = /^(\d{2}):(\d{2})(?::(\d{2}))?$/, X = /YYYY|MM|DD|HH|mm|ss/g, O = (t, e = 2) => String(t).padStart(e, "0");
|
|
442
|
+
function nt(t, e) {
|
|
392
443
|
let r;
|
|
393
|
-
const n = typeof t == "string" ?
|
|
444
|
+
const n = typeof t == "string" ? zt.exec(t.trim()) : null;
|
|
394
445
|
if (n)
|
|
395
446
|
r = { HH: n[1], mm: n[2], ss: n[3] ?? "00" };
|
|
396
447
|
else {
|
|
397
|
-
const a = typeof t == "string" ?
|
|
448
|
+
const a = typeof t == "string" ? Ft(t, "dateTime") : t;
|
|
398
449
|
a instanceof Date && (r = {
|
|
399
450
|
YYYY: O(a.getUTCFullYear(), 4),
|
|
400
451
|
MM: O(a.getUTCMonth() + 1),
|
|
@@ -406,20 +457,20 @@ function tt(t, e) {
|
|
|
406
457
|
}
|
|
407
458
|
if (!r) return t;
|
|
408
459
|
const s = r;
|
|
409
|
-
return (e.match(
|
|
460
|
+
return (e.match(X) ?? []).some((a) => s[a] === void 0) ? t : e.replace(X, (a) => s[a]);
|
|
410
461
|
}
|
|
411
|
-
function
|
|
462
|
+
function st(t, e) {
|
|
412
463
|
const r = /* @__PURE__ */ new Map(), n = (s) => {
|
|
413
464
|
const i = /* @__PURE__ */ new Map();
|
|
414
|
-
|
|
465
|
+
L(s).forEach((u) => i.set(V(u), u.table));
|
|
415
466
|
const a = (u) => {
|
|
416
467
|
if ((u == null ? void 0 : u.type) !== "field" || !u.table || !u.column) return;
|
|
417
|
-
const c = i.has(u.table) ? i.get(u.table) : u.table, l = c === void 0 ? void 0 :
|
|
468
|
+
const c = i.has(u.table) ? i.get(u.table) : u.table, l = c === void 0 ? void 0 : Nt(e, c), $ = l == null ? void 0 : l.columns.find((y) => y.name === u.column);
|
|
418
469
|
return $ == null ? void 0 : $.format;
|
|
419
470
|
}, o = (u, c) => {
|
|
420
471
|
switch (u.type) {
|
|
421
472
|
case "literal":
|
|
422
|
-
return { ...u, value:
|
|
473
|
+
return { ...u, value: nt(u.value, c) };
|
|
423
474
|
case "list":
|
|
424
475
|
return { ...u, items: u.items.map((l) => o(l, c)) };
|
|
425
476
|
case "param":
|
|
@@ -429,7 +480,7 @@ function et(t, e) {
|
|
|
429
480
|
}
|
|
430
481
|
}, f = (u) => {
|
|
431
482
|
if (u.type === "subquery") return { ...u, query: n(u.query) };
|
|
432
|
-
const c =
|
|
483
|
+
const c = H(u, f);
|
|
433
484
|
if (c.type !== "binary" || !c.right) return c;
|
|
434
485
|
const l = a(c.left), $ = a(c.right);
|
|
435
486
|
return {
|
|
@@ -438,16 +489,16 @@ function et(t, e) {
|
|
|
438
489
|
right: l ? o(c.right, l) : c.right
|
|
439
490
|
};
|
|
440
491
|
};
|
|
441
|
-
return
|
|
492
|
+
return W(s, f, (u) => n(u));
|
|
442
493
|
};
|
|
443
494
|
return { query: n(t), params: r };
|
|
444
495
|
}
|
|
445
|
-
const
|
|
496
|
+
const Fe = ["mysql", "mssql", "postgres"], qe = {
|
|
446
497
|
mysql: "MySQL",
|
|
447
498
|
mssql: "SQL Server",
|
|
448
499
|
postgres: "PostgreSQL"
|
|
449
500
|
};
|
|
450
|
-
function
|
|
501
|
+
function g(t, e) {
|
|
451
502
|
const r = t.replace(/[\uE000-\uE003]/g, "");
|
|
452
503
|
switch (e) {
|
|
453
504
|
case "mysql":
|
|
@@ -460,9 +511,9 @@ function m(t, e) {
|
|
|
460
511
|
return `"${r.replace(/"/g, '""')}"`;
|
|
461
512
|
}
|
|
462
513
|
}
|
|
463
|
-
const
|
|
514
|
+
const Zt = /^[A-Za-z_][A-Za-z0-9_]*$/, it = (t) => `${t}`, te = /\uE000(\d+)\uE000/g;
|
|
464
515
|
let N;
|
|
465
|
-
function
|
|
516
|
+
function Ue(t, e) {
|
|
466
517
|
N = { ...t, writing: [] };
|
|
467
518
|
try {
|
|
468
519
|
return e();
|
|
@@ -470,17 +521,17 @@ function be(t, e) {
|
|
|
470
521
|
N = void 0;
|
|
471
522
|
}
|
|
472
523
|
}
|
|
473
|
-
function
|
|
524
|
+
function ot(t, e, r) {
|
|
474
525
|
if (typeof t != "object" || t === null || t instanceof Date) return;
|
|
475
526
|
let n;
|
|
476
527
|
return Object.entries(t).some(([s, i]) => {
|
|
477
528
|
const a = [...r, Array.isArray(t) ? Number(s) : s];
|
|
478
|
-
return n = i === e ? a :
|
|
529
|
+
return n = i === e ? a : ot(i, e, a), n !== void 0;
|
|
479
530
|
}), n;
|
|
480
531
|
}
|
|
481
|
-
function
|
|
532
|
+
function p(t, e) {
|
|
482
533
|
if (!N) return e();
|
|
483
|
-
const { writing: r, paths: n } = N, s = r[r.length - 1], i = typeof t == "string" ? [t] :
|
|
534
|
+
const { writing: r, paths: n } = N, s = r[r.length - 1], i = typeof t == "string" ? [t] : ot(s.value, t, s.path);
|
|
484
535
|
if (!i) return e();
|
|
485
536
|
r.push({ value: t, path: i });
|
|
486
537
|
try {
|
|
@@ -489,7 +540,7 @@ function T(t, e) {
|
|
|
489
540
|
r.pop();
|
|
490
541
|
}
|
|
491
542
|
}
|
|
492
|
-
const
|
|
543
|
+
const ee = {
|
|
493
544
|
mysql: {
|
|
494
545
|
text: "CHAR",
|
|
495
546
|
integer: "SIGNED",
|
|
@@ -518,24 +569,24 @@ const Vt = {
|
|
|
518
569
|
boolean: "boolean"
|
|
519
570
|
}
|
|
520
571
|
};
|
|
521
|
-
function
|
|
572
|
+
function re(t, e) {
|
|
522
573
|
var n;
|
|
523
|
-
const r = (n =
|
|
574
|
+
const r = (n = ee[e]) == null ? void 0 : n[t];
|
|
524
575
|
return r ? e === "postgres" ? `NULL::${r}` : `CAST(NULL AS ${r})` : "NULL";
|
|
525
576
|
}
|
|
526
|
-
function
|
|
577
|
+
function at(t, e, r) {
|
|
527
578
|
if (t == null) return "NULL";
|
|
528
579
|
if (r)
|
|
529
|
-
return r.values.push(t),
|
|
580
|
+
return r.values.push(t), it(r.values.length - 1);
|
|
530
581
|
if (typeof t == "boolean")
|
|
531
582
|
return e === "mssql" ? t ? "1" : "0" : t ? "TRUE" : "FALSE";
|
|
532
583
|
if (typeof t == "string") {
|
|
533
584
|
const n = N ? t.replace(/[\uE001-\uE003]/g, "") : t;
|
|
534
585
|
return n === "" ? "''" : `'${n.replace(/'/g, "''")}'`;
|
|
535
586
|
}
|
|
536
|
-
return typeof t == "number" || typeof t == "bigint" ? String(t) : t instanceof Date ? `'${
|
|
587
|
+
return typeof t == "number" || typeof t == "bigint" ? String(t) : t instanceof Date ? `'${z(t)}'` : String(t);
|
|
537
588
|
}
|
|
538
|
-
function
|
|
589
|
+
function ne(t, e) {
|
|
539
590
|
switch (t) {
|
|
540
591
|
case "!=":
|
|
541
592
|
return e === "mssql" ? "<>" : "!=";
|
|
@@ -547,10 +598,10 @@ function Jt(t, e) {
|
|
|
547
598
|
return t;
|
|
548
599
|
}
|
|
549
600
|
}
|
|
550
|
-
function
|
|
601
|
+
function h(t) {
|
|
551
602
|
return (t == null ? void 0 : t.type) === "literal" && typeof t.value == "string" && /^[A-Za-z_]+$/.test(t.value) ? t.value.toUpperCase() : "DAY";
|
|
552
603
|
}
|
|
553
|
-
const
|
|
604
|
+
const U = {
|
|
554
605
|
YEAR: "year",
|
|
555
606
|
QUARTER: "quarter",
|
|
556
607
|
MONTH: "month",
|
|
@@ -560,43 +611,43 @@ const w = {
|
|
|
560
611
|
MINUTE: "minute",
|
|
561
612
|
SECOND: "second",
|
|
562
613
|
MILLISECOND: "millisecond"
|
|
563
|
-
},
|
|
614
|
+
}, se = {
|
|
564
615
|
// ---- Date arithmetic ----
|
|
565
616
|
DATE_ADD: ([t, e], r, n, s) => {
|
|
566
617
|
var a;
|
|
567
|
-
const i =
|
|
618
|
+
const i = h(r[2]);
|
|
568
619
|
switch (n) {
|
|
569
620
|
case "mysql":
|
|
570
621
|
return i === "MILLISECOND" ? `DATE_ADD(${t}, INTERVAL (${e}) * 1000 MICROSECOND)` : `DATE_ADD(${t}, INTERVAL ${e} ${i})`;
|
|
571
622
|
case "postgres": {
|
|
572
|
-
const o =
|
|
623
|
+
const o = U[i] ?? i.toLowerCase();
|
|
573
624
|
return !s && ((a = r[1]) == null ? void 0 : a.type) === "literal" && typeof r[1].value == "number" ? `(${t} + INTERVAL '${r[1].value} ${o}')` : `(${t} + (${e} * INTERVAL '1 ${o}'))`;
|
|
574
625
|
}
|
|
575
626
|
case "mssql":
|
|
576
627
|
return `DATEADD(${i}, ${e}, ${t})`;
|
|
577
628
|
default:
|
|
578
|
-
return `DATE_ADD(${t}, ${e}, ${
|
|
629
|
+
return `DATE_ADD(${t}, ${e}, ${h(r[2])})`;
|
|
579
630
|
}
|
|
580
631
|
},
|
|
581
632
|
DATE_SUB: ([t, e], r, n, s) => {
|
|
582
633
|
var a;
|
|
583
|
-
const i =
|
|
634
|
+
const i = h(r[2]);
|
|
584
635
|
switch (n) {
|
|
585
636
|
case "mysql":
|
|
586
637
|
return i === "MILLISECOND" ? `DATE_SUB(${t}, INTERVAL (${e}) * 1000 MICROSECOND)` : `DATE_SUB(${t}, INTERVAL ${e} ${i})`;
|
|
587
638
|
case "postgres": {
|
|
588
|
-
const o =
|
|
639
|
+
const o = U[i] ?? i.toLowerCase();
|
|
589
640
|
return !s && ((a = r[1]) == null ? void 0 : a.type) === "literal" && typeof r[1].value == "number" ? `(${t} - INTERVAL '${r[1].value} ${o}')` : `(${t} - (${e} * INTERVAL '1 ${o}'))`;
|
|
590
641
|
}
|
|
591
642
|
case "mssql":
|
|
592
643
|
return `DATEADD(${i}, -(${e}), ${t})`;
|
|
593
644
|
default:
|
|
594
|
-
return `DATE_SUB(${t}, ${e}, ${
|
|
645
|
+
return `DATE_SUB(${t}, ${e}, ${h(r[2])})`;
|
|
595
646
|
}
|
|
596
647
|
},
|
|
597
648
|
// ---- Date diff ----
|
|
598
649
|
DATEDIFF: ([t, e], r, n) => {
|
|
599
|
-
const s =
|
|
650
|
+
const s = h(r[2]);
|
|
600
651
|
switch (n) {
|
|
601
652
|
case "mysql":
|
|
602
653
|
return `TIMESTAMPDIFF(${s}, ${t}, ${e})`;
|
|
@@ -635,7 +686,7 @@ const w = {
|
|
|
635
686
|
},
|
|
636
687
|
// ---- Date extract ----
|
|
637
688
|
EXTRACT: ([t], e, r) => {
|
|
638
|
-
const n =
|
|
689
|
+
const n = h(e[1]);
|
|
639
690
|
switch (r) {
|
|
640
691
|
case "mysql":
|
|
641
692
|
return n === "MILLISECOND" ? `FLOOR(MICROSECOND(${t}) / 1000)` : `EXTRACT(${n} FROM ${t})`;
|
|
@@ -649,7 +700,7 @@ const w = {
|
|
|
649
700
|
},
|
|
650
701
|
// ---- Date truncation ----
|
|
651
702
|
DATE_TRUNC: ([t], e, r) => {
|
|
652
|
-
const n =
|
|
703
|
+
const n = h(e[1]);
|
|
653
704
|
switch (r) {
|
|
654
705
|
case "mysql":
|
|
655
706
|
switch (n) {
|
|
@@ -675,7 +726,7 @@ const w = {
|
|
|
675
726
|
return `/* unknown unit: ${n} */`;
|
|
676
727
|
}
|
|
677
728
|
case "postgres":
|
|
678
|
-
return `DATE_TRUNC('${
|
|
729
|
+
return `DATE_TRUNC('${U[n] ?? n.toLowerCase()}', ${t})`;
|
|
679
730
|
case "mssql":
|
|
680
731
|
switch (n) {
|
|
681
732
|
case "YEAR":
|
|
@@ -720,26 +771,26 @@ const w = {
|
|
|
720
771
|
// The separator goes into the SQL as a literal even in parameterized SQL:
|
|
721
772
|
// MySQL's SEPARATOR takes nothing else.
|
|
722
773
|
STRING_AGG: ([t = "??"], e, r, n, s, i) => {
|
|
723
|
-
const a = e[1] ?? { type: "literal", value: ", " }, o = a.type === "literal" && typeof a.value == "string" ?
|
|
724
|
-
var
|
|
725
|
-
return (
|
|
774
|
+
const a = e[1] ?? { type: "literal", value: ", " }, o = a.type === "literal" && typeof a.value == "string" ? at(a.value, r) : "??", f = s.distinct ? "DISTINCT " : "", u = ($) => {
|
|
775
|
+
var y;
|
|
776
|
+
return (y = s.orderBy) != null && y.length ? `ORDER BY ${s.orderBy.map((T) => `${$(T)} ${T.direction === "desc" ? "DESC" : "ASC"}`).join(", ")}` : "";
|
|
726
777
|
}, c = ($) => i($.expression), l = ($) => $ ? ` ${$}` : "";
|
|
727
778
|
switch (r) {
|
|
728
779
|
case "mysql":
|
|
729
780
|
return `GROUP_CONCAT(${f}${t}${l(u(c))} SEPARATOR ${o})`;
|
|
730
781
|
case "postgres": {
|
|
731
|
-
const $ = `CAST(${t} AS TEXT)`,
|
|
732
|
-
if (!s.distinct) return c(
|
|
733
|
-
if (
|
|
782
|
+
const $ = `CAST(${t} AS TEXT)`, y = u((T) => {
|
|
783
|
+
if (!s.distinct) return c(T);
|
|
784
|
+
if (M(T.expression, e[0])) return $;
|
|
734
785
|
throw new Error("PostgreSQL orders STRING_AGG of different values by the value only");
|
|
735
786
|
});
|
|
736
|
-
return `STRING_AGG(${f}${$}, ${o}${l(
|
|
787
|
+
return `STRING_AGG(${f}${$}, ${o}${l(y)})`;
|
|
737
788
|
}
|
|
738
789
|
case "mssql": {
|
|
739
790
|
if (s.distinct)
|
|
740
791
|
throw new Error("SQL Server's STRING_AGG can't join only different values");
|
|
741
|
-
const $ = u(c),
|
|
742
|
-
return `STRING_AGG(CAST(${t} AS NVARCHAR(MAX)), ${o})${
|
|
792
|
+
const $ = u(c), y = $ ? ` WITHIN GROUP (${$})` : "";
|
|
793
|
+
return `STRING_AGG(CAST(${t} AS NVARCHAR(MAX)), ${o})${y}`;
|
|
743
794
|
}
|
|
744
795
|
default:
|
|
745
796
|
return `STRING_AGG(${f}${t}, ${o}${l(u(c))})`;
|
|
@@ -750,38 +801,38 @@ function R(t) {
|
|
|
750
801
|
return t.type === "binary" || t.type === "logical" || t.type === "if" || t.type === "case";
|
|
751
802
|
}
|
|
752
803
|
function E(t, e, r, n) {
|
|
753
|
-
return
|
|
804
|
+
return p(t, () => ie(t, e, r, n));
|
|
754
805
|
}
|
|
755
|
-
function
|
|
806
|
+
function k(t, e, r, n) {
|
|
756
807
|
const s = (i) => {
|
|
757
808
|
if (!i) return "??";
|
|
758
809
|
const a = E(i, e, r, n);
|
|
759
810
|
return R(i) ? `(${a})` : a;
|
|
760
811
|
};
|
|
761
|
-
return
|
|
812
|
+
return p(t, () => {
|
|
762
813
|
const [i, a] = t.type === "list" ? t.items : [t];
|
|
763
814
|
return `${s(i)} AND ${s(a)}`;
|
|
764
815
|
});
|
|
765
816
|
}
|
|
766
|
-
function
|
|
817
|
+
function ie(t, e, r, n) {
|
|
767
818
|
var a;
|
|
768
819
|
const s = " ".repeat(r), i = " ".repeat(r + 1);
|
|
769
820
|
switch (t.type) {
|
|
770
821
|
case "literal":
|
|
771
|
-
return t.value === "" && !t.empty ? "??" : t.value === null && t.as ?
|
|
822
|
+
return t.value === "" && !t.empty ? "??" : t.value === null && t.as ? re(t.as, e) : at(t.value, e, n);
|
|
772
823
|
case "param": {
|
|
773
|
-
if (!n) return
|
|
824
|
+
if (!n) return Zt.test(t.name) ? `:${t.name}` : "??";
|
|
774
825
|
if (!n.named.has(t.name))
|
|
775
826
|
throw new Error(`no value for parameter "${t.name}"`);
|
|
776
|
-
return n.values.push(n.named.get(t.name)),
|
|
827
|
+
return n.values.push(n.named.get(t.name)), it(n.values.length - 1);
|
|
777
828
|
}
|
|
778
829
|
case "field": {
|
|
779
830
|
if (!t.column) return "??";
|
|
780
|
-
const o =
|
|
781
|
-
return t.table ? `${
|
|
831
|
+
const o = g(t.column, e);
|
|
832
|
+
return t.table ? `${g(t.table, e)}.${o}` : o;
|
|
782
833
|
}
|
|
783
834
|
case "function": {
|
|
784
|
-
const o = t.args.map((l) => E(l, e, r, n)), f =
|
|
835
|
+
const o = t.args.map((l) => E(l, e, r, n)), f = se[t.name.toUpperCase()];
|
|
785
836
|
if (f)
|
|
786
837
|
return f(
|
|
787
838
|
o,
|
|
@@ -797,11 +848,11 @@ function zt(t, e, r, n) {
|
|
|
797
848
|
return `${u}(${c}${o.join(", ")})`;
|
|
798
849
|
}
|
|
799
850
|
case "binary": {
|
|
800
|
-
const o = t.operator ?
|
|
801
|
-
if (
|
|
851
|
+
const o = t.operator ? ne(t.operator, e) : "=", f = E(t.left, e, r, n), u = R(t.left) ? `(${f})` : f;
|
|
852
|
+
if (Ut(t.operator)) return `${u} ${o}`;
|
|
802
853
|
if (!t.right) return `${u} ${o} ??`;
|
|
803
854
|
if (o === "BETWEEN")
|
|
804
|
-
return `${u} BETWEEN ${
|
|
855
|
+
return `${u} BETWEEN ${k(t.right, e, r, n)}`;
|
|
805
856
|
if (o === "IN")
|
|
806
857
|
return t.right.type === "list" ? `${u} IN ${E(t.right, e, r, n)}` : `${u} IN (${E(t.right, e, r, n)})`;
|
|
807
858
|
const c = E(t.right, e, r, n), l = R(t.right) ? `(${c})` : c;
|
|
@@ -831,7 +882,7 @@ ${s})`;
|
|
|
831
882
|
}
|
|
832
883
|
if (o.type === "binary" && o.operator === "BETWEEN" && o.right) {
|
|
833
884
|
const f = E(o.left, e, r, n);
|
|
834
|
-
return `${R(o.left) ? `(${f})` : f} NOT BETWEEN ${
|
|
885
|
+
return `${R(o.left) ? `(${f})` : f} NOT BETWEEN ${k(o.right, e, r, n)}`;
|
|
835
886
|
}
|
|
836
887
|
if (o.type === "logical" || o.type === "not") {
|
|
837
888
|
const f = E(o, e, r + 1, n);
|
|
@@ -865,11 +916,11 @@ ${[
|
|
|
865
916
|
"SELECT 1",
|
|
866
917
|
// The clauses below write their expressions with formatNode.
|
|
867
918
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
868
|
-
|
|
919
|
+
$t(o.from, e),
|
|
869
920
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
870
|
-
...o.joins.map(($) =>
|
|
921
|
+
...o.joins.map(($) => Et($, e, n)),
|
|
871
922
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
872
|
-
|
|
923
|
+
ut(c, e, n)
|
|
873
924
|
].join(`
|
|
874
925
|
`).replace(/^/gm, i)}
|
|
875
926
|
${s})`;
|
|
@@ -881,129 +932,129 @@ ${s})`;
|
|
|
881
932
|
return `(${t.items.map((o) => E(o, e, r, n)).join(", ") || "??"})`;
|
|
882
933
|
case "subquery":
|
|
883
934
|
return `
|
|
884
|
-
${
|
|
935
|
+
${P(t.query, e, n).join(`
|
|
885
936
|
`).replace(/^/gm, i)}
|
|
886
937
|
${s}`;
|
|
887
938
|
default:
|
|
888
939
|
return "/* unknown */";
|
|
889
940
|
}
|
|
890
941
|
}
|
|
891
|
-
function
|
|
942
|
+
function ut(t, e, r) {
|
|
892
943
|
return `WHERE
|
|
893
944
|
${E(t, e, 1, r)}`;
|
|
894
945
|
}
|
|
895
|
-
const
|
|
946
|
+
const oe = {
|
|
896
947
|
inner: "INNER JOIN",
|
|
897
948
|
left: "LEFT JOIN",
|
|
898
949
|
right: "RIGHT JOIN",
|
|
899
950
|
full: "FULL JOIN",
|
|
900
951
|
cross: "CROSS JOIN"
|
|
901
952
|
};
|
|
902
|
-
let
|
|
903
|
-
function
|
|
904
|
-
return t.shared !== void 0 ?
|
|
905
|
-
${
|
|
906
|
-
)` :
|
|
953
|
+
let ct = /* @__PURE__ */ new Map();
|
|
954
|
+
function ft(t, e, r) {
|
|
955
|
+
return t.shared !== void 0 ? g(ct.get(t.shared) ?? "??", e) : t.query ? `(
|
|
956
|
+
${lt(t.query, e, r).replace(/^/gm, " ")}
|
|
957
|
+
)` : g(t.table, e);
|
|
907
958
|
}
|
|
908
|
-
function
|
|
909
|
-
return
|
|
910
|
-
`)).join(
|
|
959
|
+
function lt(t, e, r) {
|
|
960
|
+
return Ot(t).map((n) => P(n, e, r).join(`
|
|
961
|
+
`)).join(St(t) && !t.all ? `
|
|
911
962
|
UNION
|
|
912
963
|
` : `
|
|
913
964
|
UNION ALL
|
|
914
965
|
`);
|
|
915
966
|
}
|
|
916
|
-
function
|
|
967
|
+
function ae(t, e, r) {
|
|
917
968
|
return t.length === 0 ? [] : [`WITH
|
|
918
|
-
${t.map((s) =>
|
|
919
|
-
const i =
|
|
920
|
-
return ` ${
|
|
969
|
+
${t.map((s) => p(s, () => {
|
|
970
|
+
const i = lt(s.query, e, r).replace(/^/gm, " ");
|
|
971
|
+
return ` ${g(s.name, e)} AS (
|
|
921
972
|
${i}
|
|
922
973
|
)`;
|
|
923
974
|
})).join(`,
|
|
924
975
|
`)}`];
|
|
925
976
|
}
|
|
926
|
-
function
|
|
927
|
-
const n = t.alias ? ` AS ${
|
|
928
|
-
return `FROM ${
|
|
977
|
+
function $t(t, e, r) {
|
|
978
|
+
const n = t.alias ? ` AS ${g(t.alias, e)}` : "";
|
|
979
|
+
return `FROM ${p(t, () => `${ft(t, e, r)}${n}`)}`;
|
|
929
980
|
}
|
|
930
|
-
function
|
|
931
|
-
return
|
|
932
|
-
const n =
|
|
981
|
+
function Et(t, e, r) {
|
|
982
|
+
return p(t, () => {
|
|
983
|
+
const n = ft(t, e, r), s = t.alias ? ` AS ${g(t.alias, e)}` : "";
|
|
933
984
|
if (t.joinType === "cross") return `CROSS JOIN ${n}${s}`;
|
|
934
985
|
const i = t.on ? E(t.on, e, 1, r) : "/* unconfigured */";
|
|
935
|
-
return `${
|
|
986
|
+
return `${oe[t.joinType]} ${n}${s}
|
|
936
987
|
ON ${i}`;
|
|
937
988
|
});
|
|
938
989
|
}
|
|
939
|
-
function
|
|
940
|
-
if (t.length === 0) return `${n} ${
|
|
941
|
-
const s = t.map((i) =>
|
|
942
|
-
const a = E(i.expression, e, 1, r), o = i.alias ? ` AS ${
|
|
990
|
+
function ue(t, e, r, n = "SELECT") {
|
|
991
|
+
if (t.length === 0) return `${n} ${p(t, () => "*")}`;
|
|
992
|
+
const s = t.map((i) => p(i, () => {
|
|
993
|
+
const a = E(i.expression, e, 1, r), o = i.alias ? ` AS ${g(i.alias, e)}` : "";
|
|
943
994
|
return `${a}${o}`;
|
|
944
995
|
}));
|
|
945
996
|
return `${n}
|
|
946
997
|
${s.join(`,
|
|
947
998
|
`)}`;
|
|
948
999
|
}
|
|
949
|
-
function
|
|
1000
|
+
function ce(t, e, r) {
|
|
950
1001
|
return `GROUP BY
|
|
951
|
-
${t.map((s) =>
|
|
1002
|
+
${t.map((s) => p(
|
|
952
1003
|
s,
|
|
953
1004
|
() => E(s.expression, e, 1, r)
|
|
954
1005
|
)).join(`,
|
|
955
1006
|
`)}`;
|
|
956
1007
|
}
|
|
957
|
-
function
|
|
1008
|
+
function fe(t, e, r) {
|
|
958
1009
|
return `HAVING
|
|
959
1010
|
${E(t, e, 1, r)}`;
|
|
960
1011
|
}
|
|
961
|
-
function
|
|
1012
|
+
function le(t, e, r, n) {
|
|
962
1013
|
return `ORDER BY
|
|
963
|
-
${t.map((i) =>
|
|
1014
|
+
${t.map((i) => p(i, () => `${E(n(i.expression), e, 1, r)} ${i.direction === "desc" ? "DESC" : "ASC"}`)).join(`,
|
|
964
1015
|
`)}`;
|
|
965
1016
|
}
|
|
966
|
-
const
|
|
967
|
-
function
|
|
968
|
-
const r = e.resolveName ?
|
|
969
|
-
return e.defaultLimit !== void 0 &&
|
|
1017
|
+
const _ = (t) => t !== void 0 && Number.isInteger(t) && t >= 0 ? t : void 0;
|
|
1018
|
+
function Tt(t, e) {
|
|
1019
|
+
const r = e.resolveName ? Kt(t, e.resolveName) : t;
|
|
1020
|
+
return e.defaultLimit !== void 0 && _(r.limit) === void 0 ? { ...r, limit: e.defaultLimit } : r;
|
|
970
1021
|
}
|
|
971
|
-
const
|
|
972
|
-
function
|
|
1022
|
+
const $e = "18446744073709551615";
|
|
1023
|
+
function P(t, e, r, n = `SELECT${t.distinct ? " DISTINCT" : ""}`) {
|
|
973
1024
|
const s = [
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
...t.joins.map((a) =>
|
|
1025
|
+
ue(t.select, e, r, n),
|
|
1026
|
+
$t(t.from, e, r),
|
|
1027
|
+
...t.joins.map((a) => Et(a, e, r))
|
|
977
1028
|
];
|
|
978
|
-
t.where && s.push(
|
|
1029
|
+
t.where && s.push(ut(t.where, e, r));
|
|
979
1030
|
const i = t.groupBy ?? [];
|
|
980
|
-
if (i.length > 0 && s.push(
|
|
981
|
-
const a = e === "mysql" ?
|
|
982
|
-
s.push(
|
|
1031
|
+
if (i.length > 0 && s.push(ce(i, e, r)), t.having) {
|
|
1032
|
+
const a = e === "mysql" ? K(t.having, t) : t.having;
|
|
1033
|
+
s.push(fe(a, e, r));
|
|
983
1034
|
}
|
|
984
1035
|
return s;
|
|
985
1036
|
}
|
|
986
|
-
function
|
|
987
|
-
N && (N.writing = [{ value: t, path: [] }]),
|
|
988
|
-
const n =
|
|
989
|
-
if (a.length > 0 && u.push(
|
|
1037
|
+
function yt(t, e, r) {
|
|
1038
|
+
N && (N.writing = [{ value: t, path: [] }]), ct = new Map((t.shared ?? []).map((c) => [c.id, c.name]));
|
|
1039
|
+
const n = ae(t.shared ?? [], e, r), s = _(t.limit), i = _(t.offset) || void 0, a = t.orderBy ?? [], o = e === "mssql" && s !== void 0 && i === void 0, f = `SELECT${t.distinct ? " DISTINCT" : ""}${o ? ` ${p("limit", () => `TOP (${s})`)}` : ""}`, u = P(t, e, r, f);
|
|
1040
|
+
if (a.length > 0 && u.push(le(a, e, r, (c) => e === "mysql" ? K(c, t) : c)), e === "mssql") {
|
|
990
1041
|
if (i !== void 0) {
|
|
991
1042
|
a.length === 0 && u.push("ORDER BY (SELECT NULL)");
|
|
992
|
-
const c = s !== void 0 ? ` ${
|
|
993
|
-
u.push(`${
|
|
1043
|
+
const c = s !== void 0 ? ` ${p("limit", () => `FETCH NEXT ${s} ROWS ONLY`)}` : "";
|
|
1044
|
+
u.push(`${p("offset", () => `OFFSET ${i} ROWS`)}${c}`);
|
|
994
1045
|
}
|
|
995
1046
|
} else if (s !== void 0 || i !== void 0) {
|
|
996
|
-
const c = s !== void 0 || e === "mysql" ?
|
|
1047
|
+
const c = s !== void 0 || e === "mysql" ? p(s !== void 0 ? "limit" : {}, () => `LIMIT ${s ?? $e}`) : "", l = i !== void 0 ? p("offset", () => `OFFSET ${i}`) : "";
|
|
997
1048
|
u.push([c, l].filter(Boolean).join(" "));
|
|
998
1049
|
}
|
|
999
1050
|
return [...n, ...u].join(`
|
|
1000
1051
|
`);
|
|
1001
1052
|
}
|
|
1002
|
-
function
|
|
1003
|
-
const n = r.values ?
|
|
1004
|
-
return
|
|
1053
|
+
function ve(t, e, r = {}) {
|
|
1054
|
+
const n = r.values ? qt(t, r.values) : t, s = r.schema ? st(n, r.schema).query : n;
|
|
1055
|
+
return yt(Tt(s, r), e);
|
|
1005
1056
|
}
|
|
1006
|
-
function
|
|
1057
|
+
function Ee(t, e) {
|
|
1007
1058
|
switch (t) {
|
|
1008
1059
|
case "mysql":
|
|
1009
1060
|
return "?";
|
|
@@ -1015,42 +1066,42 @@ function oe(t, e) {
|
|
|
1015
1066
|
return "?";
|
|
1016
1067
|
}
|
|
1017
1068
|
}
|
|
1018
|
-
function
|
|
1019
|
-
const n = r.schema ?
|
|
1069
|
+
function de(t, e, r = {}) {
|
|
1070
|
+
const n = r.schema ? st(t, r.schema) : { query: t, params: /* @__PURE__ */ new Map() }, s = {
|
|
1020
1071
|
values: [],
|
|
1021
1072
|
named: new Map(Object.entries(r.values ?? {}).map(([u, c]) => {
|
|
1022
1073
|
const l = n.params.get(u);
|
|
1023
|
-
return [u, l ?
|
|
1074
|
+
return [u, l ? nt(c, l) : c];
|
|
1024
1075
|
}))
|
|
1025
|
-
}, i =
|
|
1026
|
-
return { sql: a.replace(
|
|
1076
|
+
}, i = Tt(n.query, r), a = yt(i, e, s), o = [];
|
|
1077
|
+
return { sql: a.replace(te, (u, c) => {
|
|
1027
1078
|
const l = s.values[Number(c)];
|
|
1028
|
-
return o.push(l instanceof Date ?
|
|
1079
|
+
return o.push(l instanceof Date ? z(l) : l), Ee(e, o.length);
|
|
1029
1080
|
}), params: o };
|
|
1030
1081
|
}
|
|
1031
1082
|
export {
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1083
|
+
Fe as D,
|
|
1084
|
+
d as Q,
|
|
1085
|
+
vt as S,
|
|
1086
|
+
de as a,
|
|
1087
|
+
ve as b,
|
|
1088
|
+
Yt as c,
|
|
1089
|
+
Se as d,
|
|
1090
|
+
De as e,
|
|
1091
|
+
Re as f,
|
|
1092
|
+
Oe as g,
|
|
1093
|
+
Q as h,
|
|
1094
|
+
Le as i,
|
|
1095
|
+
he as j,
|
|
1096
|
+
Ce as k,
|
|
1097
|
+
be as l,
|
|
1098
|
+
Ie as m,
|
|
1099
|
+
S as n,
|
|
1100
|
+
we as o,
|
|
1101
|
+
Me as p,
|
|
1102
|
+
qe as q,
|
|
1103
|
+
Ne as r,
|
|
1104
|
+
me as s,
|
|
1105
|
+
ge as u,
|
|
1106
|
+
Ue as w
|
|
1056
1107
|
};
|
package/dist/core.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { D as e, E as r, M as o, b as n, f as i, a as d, h as t, i as u, v as l } from "./schema-z95dxLxu.js";
|
|
2
|
-
import { M as m, a as E, b as T, Q as y, I as c, S as h, d as p, p as Q, e as _, f as U, g as b, c as A, h as M, K as N, B as D, q as I, k as B, t as f, v, m as L, G as C, H as R, w as F, r as G, x as q, i as x, C as H, E as P, F as g, y as k, s as O, z as X, A as Y, j, o as J, l as w, n as K, u as z, J as W } from "./issueLocation-
|
|
2
|
+
import { M as m, a as E, b as T, Q as y, I as c, S as h, d as p, p as Q, e as _, f as U, g as b, c as A, h as M, K as N, B as D, q as I, k as B, t as f, v, m as L, G as C, H as R, w as F, r as G, x as q, i as x, C as H, E as P, F as g, y as k, s as O, z as X, A as Y, j, o as J, l as w, n as K, u as z, J as W } from "./issueLocation-CWUdTtCh.js";
|
|
3
3
|
import { c as Z, f as $, i as aa } from "./calendarDate-CYlr9TvX.js";
|
|
4
|
-
import { D as ea, q as ra, Q as oa, n as na, S as ia, c as da, d as ta, e as ua, f as la, g as Sa, a as ma, b as Ea, h as Ta, o as ya, r as ca, i as ha, p as pa, s as Qa, j as _a, k as Ua, l as ba, m as Aa, u as Ma } from "./core-api-
|
|
4
|
+
import { D as ea, q as ra, Q as oa, n as na, S as ia, c as da, d as ta, e as ua, f as la, g as Sa, a as ma, b as Ea, h as Ta, o as ya, r as ca, i as ha, p as pa, s as Qa, j as _a, k as Ua, l as ba, m as Aa, u as Ma } from "./core-api-BfLqMVQf.js";
|
|
5
5
|
export {
|
|
6
6
|
e as DEFAULT_FUNCTIONS,
|
|
7
7
|
ea as DIALECTS,
|
|
@@ -131,6 +131,7 @@ export type PipelineAction = {
|
|
|
131
131
|
} | {
|
|
132
132
|
type: 'clearPaging';
|
|
133
133
|
};
|
|
134
|
+
export declare function generateJoinId(): string;
|
|
134
135
|
/** Every distinct table referenced by a field in `ast` (deduplicated, in first-seen
|
|
135
136
|
* order) — used to auto-add tables to the pipeline as a WHERE condition references
|
|
136
137
|
* them. Only fields with an explicit `.table` count — an unqualified field doesn't
|
|
@@ -18,7 +18,10 @@ export declare class ScopeError extends Error {
|
|
|
18
18
|
readonly path: (string | number)[];
|
|
19
19
|
/** The table it reads that needs scope. */
|
|
20
20
|
readonly table: string;
|
|
21
|
-
|
|
21
|
+
/** The alias that copy of the table goes by, when it has one (a self
|
|
22
|
+
* join reads a table more than once, issue 216). */
|
|
23
|
+
readonly alias?: string;
|
|
24
|
+
constructor(path: (string | number)[], table: string, missing: string, alias?: string);
|
|
22
25
|
}
|
|
23
26
|
/** `query` with `rule.condition` in **every** body that reads a table
|
|
24
27
|
* `rule.requires` accepts (issue 151): the query itself, each derived
|
|
@@ -35,11 +38,14 @@ export declare class ScopeError extends Error {
|
|
|
35
38
|
* left out is exactly the leak.
|
|
36
39
|
* Call it after validateQuery, on the checked query. */
|
|
37
40
|
export declare function scopeQuery(schema: DatabaseSchema, query: QueryPipeline, rule: ScopeRule): QueryPipeline;
|
|
38
|
-
/** Every
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
|
|
41
|
+
/** Every copy of a table `rule.requires` accepts, in every body of `query`,
|
|
42
|
+
* that isn't scoped: the condition's tables not joined through it, or the
|
|
43
|
+
* condition naming them not ANDed at the top of its WHERE — where it is,
|
|
44
|
+
* the table, and the alias that copy goes by (a self join reads a table
|
|
45
|
+
* more than once, issue 216). Empty: `query` is scoped everywhere (see
|
|
46
|
+
* scopeQuery). For a query built or received some other way. */
|
|
47
|
+
export declare function unscopedBodies(schema: DatabaseSchema, query: QueryPipeline, rule: ScopeRule): {
|
|
43
48
|
path: (string | number)[];
|
|
44
49
|
table: string;
|
|
50
|
+
alias?: string;
|
|
45
51
|
}[];
|
package/package.json
CHANGED