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