@nongt/visql 0.3.0 → 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.
- package/dist/{PipelineEditor-DNZkDSaY.js → PipelineEditor-BoFPDUmi.js} +1 -1
- package/dist/{core-api-CBN6rAQG.js → core-api-DcJQGdVP.js} +401 -348
- package/dist/core.js +2 -2
- package/dist/index.js +4 -4
- package/dist/{issueLocation-Cw5sttQL.js → issueLocation-CWUdTtCh.js} +1 -1
- package/dist/mui.js +1 -1
- package/dist/types/core/pipelineFlow.d.ts +1 -0
- package/dist/types/core/scopeQuery.d.ts +12 -6
- package/package.json +1 -1
|
@@ -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: c } = Nt(
|
|
42
|
-
t,
|
|
43
|
-
{
|
|
44
|
-
id: "",
|
|
45
|
-
from: e.from,
|
|
46
|
-
joins: e.joins,
|
|
47
|
-
select: []
|
|
48
|
-
},
|
|
49
|
-
r.condition
|
|
50
|
-
), { where: u } = e;
|
|
51
|
-
let l = r.condition;
|
|
52
|
-
return (u == null ? void 0 : u.type) === "logical" && u.operator === "AND" ? l = { ...u, operands: [...u.operands, r.condition] } : u && (l = { type: "logical", operator: "AND", operands: [u, r.condition] }), { ...e, joins: c, 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);
|
|
100
|
+
}
|
|
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);
|
|
57
104
|
}
|
|
58
|
-
function
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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;
|
|
64
113
|
}
|
|
65
|
-
const
|
|
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 =
|
|
172
|
-
if (s && i.some((
|
|
219
|
+
function Pt(t, e, r, n = {}) {
|
|
220
|
+
const s = It(e), i = K(r);
|
|
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
|
-
return s.query.union.forEach((
|
|
176
|
-
const
|
|
177
|
-
if (
|
|
178
|
-
throw new Error(`addCondition: "${
|
|
179
|
-
}), s.query.union.reduce((
|
|
180
|
-
|
|
181
|
-
[
|
|
182
|
-
($) =>
|
|
224
|
+
return s.query.union.forEach((u) => {
|
|
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(
|
|
229
|
+
u,
|
|
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,79 +305,83 @@ 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) {
|
|
321
370
|
a && s.add(a);
|
|
322
371
|
return;
|
|
323
372
|
}
|
|
324
|
-
|
|
373
|
+
if (r.set(i, i), a) {
|
|
374
|
+
r.set(a, i), n.add(a), s.delete(a);
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
n.delete(i), s.delete(i);
|
|
325
378
|
}), { tableOf: r, aliases: n, derived: s };
|
|
326
379
|
}
|
|
327
|
-
function
|
|
328
|
-
const n = t.on &&
|
|
329
|
-
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) };
|
|
330
383
|
}
|
|
331
|
-
function
|
|
384
|
+
function L(t, e, r) {
|
|
332
385
|
if (t.type === "field") {
|
|
333
386
|
if (!t.table || e.derived.has(t.table)) return t;
|
|
334
387
|
const n = e.tableOf.get(t.table) ?? t.table;
|
|
@@ -338,27 +391,27 @@ function S(t, e, r) {
|
|
|
338
391
|
...t.column ? { column: r(n, t.column) } : {}
|
|
339
392
|
};
|
|
340
393
|
}
|
|
341
|
-
if (t.type === "subquery") return { ...t, query:
|
|
394
|
+
if (t.type === "subquery") return { ...t, query: U(t.query, r) };
|
|
342
395
|
if (t.type === "exists") {
|
|
343
|
-
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);
|
|
344
397
|
return {
|
|
345
398
|
...t,
|
|
346
|
-
link:
|
|
399
|
+
link: L(t.link, a, r),
|
|
347
400
|
query: {
|
|
348
401
|
...n,
|
|
349
402
|
from: { ...n.from, table: r(n.from.table) },
|
|
350
|
-
joins: n.joins.map((o) =>
|
|
351
|
-
...n.where ? { where:
|
|
403
|
+
joins: n.joins.map((o) => Kt(o, i, r)),
|
|
404
|
+
...n.where ? { where: L(n.where, i, r) } : {}
|
|
352
405
|
}
|
|
353
406
|
};
|
|
354
407
|
}
|
|
355
|
-
return
|
|
408
|
+
return Y(t, (n) => L(n, e, r));
|
|
356
409
|
}
|
|
357
|
-
function
|
|
358
|
-
const r =
|
|
410
|
+
function U(t, e) {
|
|
411
|
+
const r = j([t.from, ...t.joins]), n = J(
|
|
359
412
|
t,
|
|
360
|
-
(s) =>
|
|
361
|
-
(s) =>
|
|
413
|
+
(s) => L(s, r, e),
|
|
414
|
+
(s) => U(s, e)
|
|
362
415
|
);
|
|
363
416
|
return {
|
|
364
417
|
...n,
|
|
@@ -366,11 +419,11 @@ function M(t, e) {
|
|
|
366
419
|
joins: n.joins.map((s) => s.table === void 0 ? s : { ...s, table: e(s.table) })
|
|
367
420
|
};
|
|
368
421
|
}
|
|
369
|
-
function
|
|
370
|
-
if (Array.isArray(t)) return t.map((s) =>
|
|
422
|
+
function _(t, e) {
|
|
423
|
+
if (Array.isArray(t)) return t.map((s) => _(s, e));
|
|
371
424
|
if (typeof t != "object" || t === null || t instanceof Date) return t;
|
|
372
425
|
const r = Object.fromEntries(Object.entries(t).map(
|
|
373
|
-
([s, i]) => [s,
|
|
426
|
+
([s, i]) => [s, _(i, e)]
|
|
374
427
|
));
|
|
375
428
|
if (!("from" in r) || !Array.isArray(r.joins)) return r;
|
|
376
429
|
const n = (s) => s.table !== void 0 && !s.alias && e(s.table) !== s.table ? { ...s, alias: s.table } : s;
|
|
@@ -380,70 +433,70 @@ function v(t, e) {
|
|
|
380
433
|
joins: r.joins.map(n)
|
|
381
434
|
};
|
|
382
435
|
}
|
|
383
|
-
function
|
|
384
|
-
return
|
|
436
|
+
function zt(t, e) {
|
|
437
|
+
return U(_(t, e), e);
|
|
385
438
|
}
|
|
386
|
-
const
|
|
387
|
-
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) {
|
|
388
441
|
let r;
|
|
389
|
-
const n = typeof t == "string" ?
|
|
442
|
+
const n = typeof t == "string" ? Zt.exec(t.trim()) : null;
|
|
390
443
|
if (n)
|
|
391
444
|
r = { HH: n[1], mm: n[2], ss: n[3] ?? "00" };
|
|
392
445
|
else {
|
|
393
|
-
const a = typeof t == "string" ?
|
|
446
|
+
const a = typeof t == "string" ? qt(t, "dateTime") : t;
|
|
394
447
|
a instanceof Date && (r = {
|
|
395
|
-
YYYY:
|
|
396
|
-
MM:
|
|
397
|
-
DD:
|
|
398
|
-
HH:
|
|
399
|
-
mm:
|
|
400
|
-
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())
|
|
401
454
|
});
|
|
402
455
|
}
|
|
403
456
|
if (!r) return t;
|
|
404
457
|
const s = r;
|
|
405
|
-
return (e.match(
|
|
458
|
+
return (e.match(X) ?? []).some((a) => s[a] === void 0) ? t : e.replace(X, (a) => s[a]);
|
|
406
459
|
}
|
|
407
|
-
function
|
|
460
|
+
function it(t, e) {
|
|
408
461
|
const r = /* @__PURE__ */ new Map(), n = (s) => {
|
|
409
462
|
const i = /* @__PURE__ */ new Map();
|
|
410
|
-
|
|
411
|
-
const a = (
|
|
412
|
-
if ((
|
|
413
|
-
const
|
|
463
|
+
b(s).forEach((u) => i.set(W(u), u.table));
|
|
464
|
+
const a = (u) => {
|
|
465
|
+
if ((u == null ? void 0 : u.type) !== "field" || !u.table || !u.column) return;
|
|
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);
|
|
414
467
|
return $ == null ? void 0 : $.format;
|
|
415
|
-
}, o = (
|
|
416
|
-
switch (
|
|
468
|
+
}, o = (u, c) => {
|
|
469
|
+
switch (u.type) {
|
|
417
470
|
case "literal":
|
|
418
|
-
return { ...
|
|
471
|
+
return { ...u, value: st(u.value, c) };
|
|
419
472
|
case "list":
|
|
420
|
-
return { ...
|
|
473
|
+
return { ...u, items: u.items.map((l) => o(l, c)) };
|
|
421
474
|
case "param":
|
|
422
|
-
return r.has(
|
|
475
|
+
return r.has(u.name) || r.set(u.name, c), u;
|
|
423
476
|
default:
|
|
424
|
-
return
|
|
477
|
+
return u;
|
|
425
478
|
}
|
|
426
|
-
}, f = (
|
|
427
|
-
if (
|
|
428
|
-
const
|
|
429
|
-
if (
|
|
430
|
-
const l = a(
|
|
479
|
+
}, f = (u) => {
|
|
480
|
+
if (u.type === "subquery") return { ...u, query: n(u.query) };
|
|
481
|
+
const c = Y(u, f);
|
|
482
|
+
if (c.type !== "binary" || !c.right) return c;
|
|
483
|
+
const l = a(c.left), $ = a(c.right);
|
|
431
484
|
return {
|
|
432
|
-
...
|
|
433
|
-
left: $ ? o(
|
|
434
|
-
right: l ? o(
|
|
485
|
+
...c,
|
|
486
|
+
left: $ ? o(c.left, $) : c.left,
|
|
487
|
+
right: l ? o(c.right, l) : c.right
|
|
435
488
|
};
|
|
436
489
|
};
|
|
437
|
-
return
|
|
490
|
+
return J(s, f, (u) => n(u));
|
|
438
491
|
};
|
|
439
492
|
return { query: n(t), params: r };
|
|
440
493
|
}
|
|
441
|
-
const
|
|
494
|
+
const qe = ["mysql", "mssql", "postgres"], Ue = {
|
|
442
495
|
mysql: "MySQL",
|
|
443
496
|
mssql: "SQL Server",
|
|
444
497
|
postgres: "PostgreSQL"
|
|
445
498
|
};
|
|
446
|
-
function
|
|
499
|
+
function g(t, e) {
|
|
447
500
|
const r = t.replace(/[\uE000-\uE003]/g, "");
|
|
448
501
|
switch (e) {
|
|
449
502
|
case "mysql":
|
|
@@ -456,27 +509,27 @@ function m(t, e) {
|
|
|
456
509
|
return `"${r.replace(/"/g, '""')}"`;
|
|
457
510
|
}
|
|
458
511
|
}
|
|
459
|
-
const
|
|
460
|
-
let
|
|
461
|
-
function
|
|
462
|
-
|
|
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: [] };
|
|
463
516
|
try {
|
|
464
517
|
return e();
|
|
465
518
|
} finally {
|
|
466
|
-
|
|
519
|
+
O = void 0;
|
|
467
520
|
}
|
|
468
521
|
}
|
|
469
|
-
function
|
|
522
|
+
function at(t, e, r) {
|
|
470
523
|
if (typeof t != "object" || t === null || t instanceof Date) return;
|
|
471
524
|
let n;
|
|
472
525
|
return Object.entries(t).some(([s, i]) => {
|
|
473
526
|
const a = [...r, Array.isArray(t) ? Number(s) : s];
|
|
474
|
-
return n = i === e ? a :
|
|
527
|
+
return n = i === e ? a : at(i, e, a), n !== void 0;
|
|
475
528
|
}), n;
|
|
476
529
|
}
|
|
477
|
-
function
|
|
478
|
-
if (!
|
|
479
|
-
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);
|
|
480
533
|
if (!i) return e();
|
|
481
534
|
r.push({ value: t, path: i });
|
|
482
535
|
try {
|
|
@@ -485,7 +538,7 @@ function T(t, e) {
|
|
|
485
538
|
r.pop();
|
|
486
539
|
}
|
|
487
540
|
}
|
|
488
|
-
const
|
|
541
|
+
const re = {
|
|
489
542
|
mysql: {
|
|
490
543
|
text: "CHAR",
|
|
491
544
|
integer: "SIGNED",
|
|
@@ -514,24 +567,24 @@ const Vt = {
|
|
|
514
567
|
boolean: "boolean"
|
|
515
568
|
}
|
|
516
569
|
};
|
|
517
|
-
function
|
|
570
|
+
function ne(t, e) {
|
|
518
571
|
var n;
|
|
519
|
-
const r = (n =
|
|
572
|
+
const r = (n = re[e]) == null ? void 0 : n[t];
|
|
520
573
|
return r ? e === "postgres" ? `NULL::${r}` : `CAST(NULL AS ${r})` : "NULL";
|
|
521
574
|
}
|
|
522
|
-
function
|
|
575
|
+
function ut(t, e, r) {
|
|
523
576
|
if (t == null) return "NULL";
|
|
524
577
|
if (r)
|
|
525
|
-
return r.values.push(t),
|
|
578
|
+
return r.values.push(t), ot(r.values.length - 1);
|
|
526
579
|
if (typeof t == "boolean")
|
|
527
580
|
return e === "mssql" ? t ? "1" : "0" : t ? "TRUE" : "FALSE";
|
|
528
581
|
if (typeof t == "string") {
|
|
529
|
-
const n =
|
|
582
|
+
const n = O ? t.replace(/[\uE001-\uE003]/g, "") : t;
|
|
530
583
|
return n === "" ? "''" : `'${n.replace(/'/g, "''")}'`;
|
|
531
584
|
}
|
|
532
|
-
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);
|
|
533
586
|
}
|
|
534
|
-
function
|
|
587
|
+
function se(t, e) {
|
|
535
588
|
switch (t) {
|
|
536
589
|
case "!=":
|
|
537
590
|
return e === "mssql" ? "<>" : "!=";
|
|
@@ -543,10 +596,10 @@ function Jt(t, e) {
|
|
|
543
596
|
return t;
|
|
544
597
|
}
|
|
545
598
|
}
|
|
546
|
-
function
|
|
599
|
+
function D(t) {
|
|
547
600
|
return (t == null ? void 0 : t.type) === "literal" && typeof t.value == "string" && /^[A-Za-z_]+$/.test(t.value) ? t.value.toUpperCase() : "DAY";
|
|
548
601
|
}
|
|
549
|
-
const
|
|
602
|
+
const v = {
|
|
550
603
|
YEAR: "year",
|
|
551
604
|
QUARTER: "quarter",
|
|
552
605
|
MONTH: "month",
|
|
@@ -556,43 +609,43 @@ const w = {
|
|
|
556
609
|
MINUTE: "minute",
|
|
557
610
|
SECOND: "second",
|
|
558
611
|
MILLISECOND: "millisecond"
|
|
559
|
-
},
|
|
612
|
+
}, ie = {
|
|
560
613
|
// ---- Date arithmetic ----
|
|
561
614
|
DATE_ADD: ([t, e], r, n, s) => {
|
|
562
615
|
var a;
|
|
563
|
-
const i =
|
|
616
|
+
const i = D(r[2]);
|
|
564
617
|
switch (n) {
|
|
565
618
|
case "mysql":
|
|
566
619
|
return i === "MILLISECOND" ? `DATE_ADD(${t}, INTERVAL (${e}) * 1000 MICROSECOND)` : `DATE_ADD(${t}, INTERVAL ${e} ${i})`;
|
|
567
620
|
case "postgres": {
|
|
568
|
-
const o =
|
|
621
|
+
const o = v[i] ?? i.toLowerCase();
|
|
569
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}'))`;
|
|
570
623
|
}
|
|
571
624
|
case "mssql":
|
|
572
625
|
return `DATEADD(${i}, ${e}, ${t})`;
|
|
573
626
|
default:
|
|
574
|
-
return `DATE_ADD(${t}, ${e}, ${
|
|
627
|
+
return `DATE_ADD(${t}, ${e}, ${D(r[2])})`;
|
|
575
628
|
}
|
|
576
629
|
},
|
|
577
630
|
DATE_SUB: ([t, e], r, n, s) => {
|
|
578
631
|
var a;
|
|
579
|
-
const i =
|
|
632
|
+
const i = D(r[2]);
|
|
580
633
|
switch (n) {
|
|
581
634
|
case "mysql":
|
|
582
635
|
return i === "MILLISECOND" ? `DATE_SUB(${t}, INTERVAL (${e}) * 1000 MICROSECOND)` : `DATE_SUB(${t}, INTERVAL ${e} ${i})`;
|
|
583
636
|
case "postgres": {
|
|
584
|
-
const o =
|
|
637
|
+
const o = v[i] ?? i.toLowerCase();
|
|
585
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}'))`;
|
|
586
639
|
}
|
|
587
640
|
case "mssql":
|
|
588
641
|
return `DATEADD(${i}, -(${e}), ${t})`;
|
|
589
642
|
default:
|
|
590
|
-
return `DATE_SUB(${t}, ${e}, ${
|
|
643
|
+
return `DATE_SUB(${t}, ${e}, ${D(r[2])})`;
|
|
591
644
|
}
|
|
592
645
|
},
|
|
593
646
|
// ---- Date diff ----
|
|
594
647
|
DATEDIFF: ([t, e], r, n) => {
|
|
595
|
-
const s =
|
|
648
|
+
const s = D(r[2]);
|
|
596
649
|
switch (n) {
|
|
597
650
|
case "mysql":
|
|
598
651
|
return `TIMESTAMPDIFF(${s}, ${t}, ${e})`;
|
|
@@ -631,7 +684,7 @@ const w = {
|
|
|
631
684
|
},
|
|
632
685
|
// ---- Date extract ----
|
|
633
686
|
EXTRACT: ([t], e, r) => {
|
|
634
|
-
const n =
|
|
687
|
+
const n = D(e[1]);
|
|
635
688
|
switch (r) {
|
|
636
689
|
case "mysql":
|
|
637
690
|
return n === "MILLISECOND" ? `FLOOR(MICROSECOND(${t}) / 1000)` : `EXTRACT(${n} FROM ${t})`;
|
|
@@ -645,7 +698,7 @@ const w = {
|
|
|
645
698
|
},
|
|
646
699
|
// ---- Date truncation ----
|
|
647
700
|
DATE_TRUNC: ([t], e, r) => {
|
|
648
|
-
const n =
|
|
701
|
+
const n = D(e[1]);
|
|
649
702
|
switch (r) {
|
|
650
703
|
case "mysql":
|
|
651
704
|
switch (n) {
|
|
@@ -671,7 +724,7 @@ const w = {
|
|
|
671
724
|
return `/* unknown unit: ${n} */`;
|
|
672
725
|
}
|
|
673
726
|
case "postgres":
|
|
674
|
-
return `DATE_TRUNC('${
|
|
727
|
+
return `DATE_TRUNC('${v[n] ?? n.toLowerCase()}', ${t})`;
|
|
675
728
|
case "mssql":
|
|
676
729
|
switch (n) {
|
|
677
730
|
case "YEAR":
|
|
@@ -716,68 +769,68 @@ const w = {
|
|
|
716
769
|
// The separator goes into the SQL as a literal even in parameterized SQL:
|
|
717
770
|
// MySQL's SEPARATOR takes nothing else.
|
|
718
771
|
STRING_AGG: ([t = "??"], e, r, n, s, i) => {
|
|
719
|
-
const a = e[1] ?? { type: "literal", value: ", " }, o = a.type === "literal" && typeof a.value == "string" ?
|
|
720
|
-
var
|
|
721
|
-
return (
|
|
722
|
-
},
|
|
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(", ")}` : "";
|
|
775
|
+
}, c = ($) => i($.expression), l = ($) => $ ? ` ${$}` : "";
|
|
723
776
|
switch (r) {
|
|
724
777
|
case "mysql":
|
|
725
|
-
return `GROUP_CONCAT(${f}${t}${l(c
|
|
778
|
+
return `GROUP_CONCAT(${f}${t}${l(u(c))} SEPARATOR ${o})`;
|
|
726
779
|
case "postgres": {
|
|
727
|
-
const $ = `CAST(${t} AS TEXT)`,
|
|
728
|
-
if (!s.distinct) return
|
|
729
|
-
if (
|
|
780
|
+
const $ = `CAST(${t} AS TEXT)`, T = u((A) => {
|
|
781
|
+
if (!s.distinct) return c(A);
|
|
782
|
+
if (w(A.expression, e[0])) return $;
|
|
730
783
|
throw new Error("PostgreSQL orders STRING_AGG of different values by the value only");
|
|
731
784
|
});
|
|
732
|
-
return `STRING_AGG(${f}${$}, ${o}${l(
|
|
785
|
+
return `STRING_AGG(${f}${$}, ${o}${l(T)})`;
|
|
733
786
|
}
|
|
734
787
|
case "mssql": {
|
|
735
788
|
if (s.distinct)
|
|
736
789
|
throw new Error("SQL Server's STRING_AGG can't join only different values");
|
|
737
|
-
const $ = c
|
|
738
|
-
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}`;
|
|
739
792
|
}
|
|
740
793
|
default:
|
|
741
|
-
return `STRING_AGG(${f}${t}, ${o}${l(c
|
|
794
|
+
return `STRING_AGG(${f}${t}, ${o}${l(u(c))})`;
|
|
742
795
|
}
|
|
743
796
|
}
|
|
744
797
|
};
|
|
745
|
-
function
|
|
798
|
+
function C(t) {
|
|
746
799
|
return t.type === "binary" || t.type === "logical" || t.type === "if" || t.type === "case";
|
|
747
800
|
}
|
|
748
801
|
function E(t, e, r, n) {
|
|
749
|
-
return
|
|
802
|
+
return y(t, () => oe(t, e, r, n));
|
|
750
803
|
}
|
|
751
|
-
function
|
|
804
|
+
function V(t, e, r, n) {
|
|
752
805
|
const s = (i) => {
|
|
753
806
|
if (!i) return "??";
|
|
754
807
|
const a = E(i, e, r, n);
|
|
755
|
-
return
|
|
808
|
+
return C(i) ? `(${a})` : a;
|
|
756
809
|
};
|
|
757
|
-
return
|
|
810
|
+
return y(t, () => {
|
|
758
811
|
const [i, a] = t.type === "list" ? t.items : [t];
|
|
759
812
|
return `${s(i)} AND ${s(a)}`;
|
|
760
813
|
});
|
|
761
814
|
}
|
|
762
|
-
function
|
|
815
|
+
function oe(t, e, r, n) {
|
|
763
816
|
var a;
|
|
764
817
|
const s = " ".repeat(r), i = " ".repeat(r + 1);
|
|
765
818
|
switch (t.type) {
|
|
766
819
|
case "literal":
|
|
767
|
-
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);
|
|
768
821
|
case "param": {
|
|
769
|
-
if (!n) return
|
|
822
|
+
if (!n) return te.test(t.name) ? `:${t.name}` : "??";
|
|
770
823
|
if (!n.named.has(t.name))
|
|
771
824
|
throw new Error(`no value for parameter "${t.name}"`);
|
|
772
|
-
return n.values.push(n.named.get(t.name)),
|
|
825
|
+
return n.values.push(n.named.get(t.name)), ot(n.values.length - 1);
|
|
773
826
|
}
|
|
774
827
|
case "field": {
|
|
775
828
|
if (!t.column) return "??";
|
|
776
|
-
const o =
|
|
777
|
-
return t.table ? `${
|
|
829
|
+
const o = g(t.column, e);
|
|
830
|
+
return t.table ? `${g(t.table, e)}.${o}` : o;
|
|
778
831
|
}
|
|
779
832
|
case "function": {
|
|
780
|
-
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()];
|
|
781
834
|
if (f)
|
|
782
835
|
return f(
|
|
783
836
|
o,
|
|
@@ -787,21 +840,21 @@ function zt(t, e, r, n) {
|
|
|
787
840
|
t,
|
|
788
841
|
(l) => E(l, e, r, n)
|
|
789
842
|
);
|
|
790
|
-
const
|
|
791
|
-
if (o.length === 0 &&
|
|
792
|
-
const
|
|
793
|
-
return `${
|
|
843
|
+
const u = /^[A-Za-z_][A-Za-z0-9_.]*$/.test(t.name) || t.name === "" ? t.name : "??";
|
|
844
|
+
if (o.length === 0 && u.toUpperCase() === "COUNT") return `${u}(*)`;
|
|
845
|
+
const c = t.distinct && o.length > 0 ? "DISTINCT " : "";
|
|
846
|
+
return `${u}(${c}${o.join(", ")})`;
|
|
794
847
|
}
|
|
795
848
|
case "binary": {
|
|
796
|
-
const o = t.operator ?
|
|
797
|
-
if (
|
|
798
|
-
if (!t.right) return `${
|
|
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}`;
|
|
851
|
+
if (!t.right) return `${u} ${o} ??`;
|
|
799
852
|
if (o === "BETWEEN")
|
|
800
|
-
return `${
|
|
853
|
+
return `${u} BETWEEN ${V(t.right, e, r, n)}`;
|
|
801
854
|
if (o === "IN")
|
|
802
|
-
return t.right.type === "list" ? `${
|
|
803
|
-
const
|
|
804
|
-
return `${
|
|
855
|
+
return t.right.type === "list" ? `${u} IN ${E(t.right, e, r, n)}` : `${u} IN (${E(t.right, e, r, n)})`;
|
|
856
|
+
const c = E(t.right, e, r, n), l = C(t.right) ? `(${c})` : c;
|
|
857
|
+
return `${u} ${o} ${l}`;
|
|
805
858
|
}
|
|
806
859
|
case "logical": {
|
|
807
860
|
if (t.operands.length === 0) return "/* empty */";
|
|
@@ -811,9 +864,9 @@ function zt(t, e, r, n) {
|
|
|
811
864
|
${s}${t.operator} `;
|
|
812
865
|
return t.operands.map((f) => {
|
|
813
866
|
if (f.type === "logical") {
|
|
814
|
-
const
|
|
867
|
+
const u = E(f, e, r + 1, n);
|
|
815
868
|
return `(
|
|
816
|
-
${i}${
|
|
869
|
+
${i}${u}
|
|
817
870
|
${s})`;
|
|
818
871
|
}
|
|
819
872
|
return E(f, e, r, n);
|
|
@@ -823,11 +876,11 @@ ${s})`;
|
|
|
823
876
|
const { operand: o } = t;
|
|
824
877
|
if (o.type === "binary" && o.operator === "IN" && ((a = o.right) == null ? void 0 : a.type) === "subquery") {
|
|
825
878
|
const f = E(o.left, e, r, n);
|
|
826
|
-
return `${
|
|
879
|
+
return `${C(o.left) ? `(${f})` : f} NOT IN (${E(o.right, e, r, n)})`;
|
|
827
880
|
}
|
|
828
881
|
if (o.type === "binary" && o.operator === "BETWEEN" && o.right) {
|
|
829
882
|
const f = E(o.left, e, r, n);
|
|
830
|
-
return `${
|
|
883
|
+
return `${C(o.left) ? `(${f})` : f} NOT BETWEEN ${V(o.right, e, r, n)}`;
|
|
831
884
|
}
|
|
832
885
|
if (o.type === "logical" || o.type === "not") {
|
|
833
886
|
const f = E(o, e, r + 1, n);
|
|
@@ -838,34 +891,34 @@ ${s})`;
|
|
|
838
891
|
return `NOT ${E(o, e, r, n)}`;
|
|
839
892
|
}
|
|
840
893
|
case "if": {
|
|
841
|
-
const o = t.condition ? E(t.condition, e, r, n) : "??", f = t.then ? E(t.then, e, r, n) : "??",
|
|
842
|
-
return e === "mysql" ? `IF(${o}, ${f}, ${
|
|
894
|
+
const o = t.condition ? E(t.condition, e, r, n) : "??", f = t.then ? E(t.then, e, r, n) : "??", u = t.else ? E(t.else, e, r, n) : "NULL";
|
|
895
|
+
return e === "mysql" ? `IF(${o}, ${f}, ${u})` : e === "mssql" ? `IIF(${o}, ${f}, ${u})` : `CASE WHEN ${o} THEN ${f} ELSE ${u} END`;
|
|
843
896
|
}
|
|
844
897
|
case "case": {
|
|
845
|
-
const o = t.subject ? ` ${E(t.subject, e, r, n)}` : "", f = t.branches.map((
|
|
846
|
-
const l = E(
|
|
898
|
+
const o = t.subject ? ` ${E(t.subject, e, r, n)}` : "", f = t.branches.map((c) => {
|
|
899
|
+
const l = E(c.when, e, r + 1, n), $ = E(c.then, e, r + 1, n);
|
|
847
900
|
return `${i}WHEN ${l} THEN ${$}`;
|
|
848
901
|
}).join(`
|
|
849
|
-
`),
|
|
902
|
+
`), u = t.else ? `
|
|
850
903
|
${i}ELSE ${E(t.else, e, r + 1, n)}` : "";
|
|
851
904
|
return `CASE${o}
|
|
852
|
-
${f}${
|
|
905
|
+
${f}${u}
|
|
853
906
|
${s}END`;
|
|
854
907
|
}
|
|
855
908
|
// EXISTS (SELECT 1 FROM … WHERE <link> AND <where>): the subquery written
|
|
856
909
|
// in place, one level in, so its params fall in reading order.
|
|
857
910
|
case "exists": {
|
|
858
|
-
const { query: o, link: f } = t,
|
|
911
|
+
const { query: o, link: f } = t, u = ($) => $.type === "logical" && $.operator === "AND" ? $.operands : [$], c = o.where ? { type: "logical", operator: "AND", operands: [...u(f), ...u(o.where)] } : f;
|
|
859
912
|
return `EXISTS (
|
|
860
913
|
${[
|
|
861
914
|
"SELECT 1",
|
|
862
915
|
// The clauses below write their expressions with formatNode.
|
|
863
916
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
864
|
-
|
|
917
|
+
Et(o.from, e),
|
|
865
918
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
866
|
-
...o.joins.map(($) =>
|
|
919
|
+
...o.joins.map(($) => Tt($, e, n)),
|
|
867
920
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
868
|
-
|
|
921
|
+
ct(c, e, n)
|
|
869
922
|
].join(`
|
|
870
923
|
`).replace(/^/gm, i)}
|
|
871
924
|
${s})`;
|
|
@@ -877,129 +930,129 @@ ${s})`;
|
|
|
877
930
|
return `(${t.items.map((o) => E(o, e, r, n)).join(", ") || "??"})`;
|
|
878
931
|
case "subquery":
|
|
879
932
|
return `
|
|
880
|
-
${
|
|
933
|
+
${G(t.query, e, n).join(`
|
|
881
934
|
`).replace(/^/gm, i)}
|
|
882
935
|
${s}`;
|
|
883
936
|
default:
|
|
884
937
|
return "/* unknown */";
|
|
885
938
|
}
|
|
886
939
|
}
|
|
887
|
-
function
|
|
940
|
+
function ct(t, e, r) {
|
|
888
941
|
return `WHERE
|
|
889
942
|
${E(t, e, 1, r)}`;
|
|
890
943
|
}
|
|
891
|
-
const
|
|
944
|
+
const ae = {
|
|
892
945
|
inner: "INNER JOIN",
|
|
893
946
|
left: "LEFT JOIN",
|
|
894
947
|
right: "RIGHT JOIN",
|
|
895
948
|
full: "FULL JOIN",
|
|
896
949
|
cross: "CROSS JOIN"
|
|
897
950
|
};
|
|
898
|
-
let
|
|
899
|
-
function
|
|
900
|
-
return t.shared !== void 0 ?
|
|
901
|
-
${
|
|
902
|
-
)` :
|
|
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);
|
|
903
956
|
}
|
|
904
|
-
function
|
|
905
|
-
return
|
|
906
|
-
`)).join(
|
|
957
|
+
function $t(t, e, r) {
|
|
958
|
+
return St(t).map((n) => G(n, e, r).join(`
|
|
959
|
+
`)).join(Rt(t) && !t.all ? `
|
|
907
960
|
UNION
|
|
908
961
|
` : `
|
|
909
962
|
UNION ALL
|
|
910
963
|
`);
|
|
911
964
|
}
|
|
912
|
-
function
|
|
965
|
+
function ue(t, e, r) {
|
|
913
966
|
return t.length === 0 ? [] : [`WITH
|
|
914
|
-
${t.map((s) =>
|
|
915
|
-
const i =
|
|
916
|
-
return ` ${
|
|
967
|
+
${t.map((s) => y(s, () => {
|
|
968
|
+
const i = $t(s.query, e, r).replace(/^/gm, " ");
|
|
969
|
+
return ` ${g(s.name, e)} AS (
|
|
917
970
|
${i}
|
|
918
971
|
)`;
|
|
919
972
|
})).join(`,
|
|
920
973
|
`)}`];
|
|
921
974
|
}
|
|
922
|
-
function
|
|
923
|
-
const n = t.alias ? ` AS ${
|
|
924
|
-
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}`)}`;
|
|
925
978
|
}
|
|
926
|
-
function
|
|
927
|
-
return
|
|
928
|
-
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)}` : "";
|
|
929
982
|
if (t.joinType === "cross") return `CROSS JOIN ${n}${s}`;
|
|
930
983
|
const i = t.on ? E(t.on, e, 1, r) : "/* unconfigured */";
|
|
931
|
-
return `${
|
|
984
|
+
return `${ae[t.joinType]} ${n}${s}
|
|
932
985
|
ON ${i}`;
|
|
933
986
|
});
|
|
934
987
|
}
|
|
935
|
-
function
|
|
936
|
-
if (t.length === 0) return `${n} ${
|
|
937
|
-
const s = t.map((i) =>
|
|
938
|
-
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)}` : "";
|
|
939
992
|
return `${a}${o}`;
|
|
940
993
|
}));
|
|
941
994
|
return `${n}
|
|
942
995
|
${s.join(`,
|
|
943
996
|
`)}`;
|
|
944
997
|
}
|
|
945
|
-
function
|
|
998
|
+
function fe(t, e, r) {
|
|
946
999
|
return `GROUP BY
|
|
947
|
-
${t.map((s) =>
|
|
1000
|
+
${t.map((s) => y(
|
|
948
1001
|
s,
|
|
949
1002
|
() => E(s.expression, e, 1, r)
|
|
950
1003
|
)).join(`,
|
|
951
1004
|
`)}`;
|
|
952
1005
|
}
|
|
953
|
-
function
|
|
1006
|
+
function le(t, e, r) {
|
|
954
1007
|
return `HAVING
|
|
955
1008
|
${E(t, e, 1, r)}`;
|
|
956
1009
|
}
|
|
957
|
-
function
|
|
1010
|
+
function $e(t, e, r, n) {
|
|
958
1011
|
return `ORDER BY
|
|
959
|
-
${t.map((i) =>
|
|
1012
|
+
${t.map((i) => y(i, () => `${E(n(i.expression), e, 1, r)} ${i.direction === "desc" ? "DESC" : "ASC"}`)).join(`,
|
|
960
1013
|
`)}`;
|
|
961
1014
|
}
|
|
962
|
-
const
|
|
963
|
-
function
|
|
964
|
-
const r = e.resolveName ?
|
|
965
|
-
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;
|
|
966
1019
|
}
|
|
967
|
-
const
|
|
968
|
-
function
|
|
1020
|
+
const Ee = "18446744073709551615";
|
|
1021
|
+
function G(t, e, r, n = `SELECT${t.distinct ? " DISTINCT" : ""}`) {
|
|
969
1022
|
const s = [
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
...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))
|
|
973
1026
|
];
|
|
974
|
-
t.where && s.push(
|
|
1027
|
+
t.where && s.push(ct(t.where, e, r));
|
|
975
1028
|
const i = t.groupBy ?? [];
|
|
976
|
-
if (i.length > 0 && s.push(
|
|
977
|
-
const a = e === "mysql" ?
|
|
978
|
-
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));
|
|
979
1032
|
}
|
|
980
1033
|
return s;
|
|
981
1034
|
}
|
|
982
|
-
function
|
|
983
|
-
|
|
984
|
-
const n =
|
|
985
|
-
if (a.length > 0 &&
|
|
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") {
|
|
986
1039
|
if (i !== void 0) {
|
|
987
|
-
a.length === 0 &&
|
|
988
|
-
const
|
|
989
|
-
|
|
1040
|
+
a.length === 0 && u.push("ORDER BY (SELECT NULL)");
|
|
1041
|
+
const c = s !== void 0 ? ` ${y("limit", () => `FETCH NEXT ${s} ROWS ONLY`)}` : "";
|
|
1042
|
+
u.push(`${y("offset", () => `OFFSET ${i} ROWS`)}${c}`);
|
|
990
1043
|
}
|
|
991
1044
|
} else if (s !== void 0 || i !== void 0) {
|
|
992
|
-
const
|
|
993
|
-
|
|
1045
|
+
const c = s !== void 0 || e === "mysql" ? y(s !== void 0 ? "limit" : {}, () => `LIMIT ${s ?? Ee}`) : "", l = i !== void 0 ? y("offset", () => `OFFSET ${i}`) : "";
|
|
1046
|
+
u.push([c, l].filter(Boolean).join(" "));
|
|
994
1047
|
}
|
|
995
|
-
return [...n, ...
|
|
1048
|
+
return [...n, ...u].join(`
|
|
996
1049
|
`);
|
|
997
1050
|
}
|
|
998
|
-
function
|
|
999
|
-
const n = r.values ?
|
|
1000
|
-
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);
|
|
1001
1054
|
}
|
|
1002
|
-
function
|
|
1055
|
+
function Te(t, e) {
|
|
1003
1056
|
switch (t) {
|
|
1004
1057
|
case "mysql":
|
|
1005
1058
|
return "?";
|
|
@@ -1011,42 +1064,42 @@ function oe(t, e) {
|
|
|
1011
1064
|
return "?";
|
|
1012
1065
|
}
|
|
1013
1066
|
}
|
|
1014
|
-
function
|
|
1015
|
-
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 = {
|
|
1016
1069
|
values: [],
|
|
1017
|
-
named: new Map(Object.entries(r.values ?? {}).map(([
|
|
1018
|
-
const l = n.params.get(
|
|
1019
|
-
return [
|
|
1070
|
+
named: new Map(Object.entries(r.values ?? {}).map(([u, c]) => {
|
|
1071
|
+
const l = n.params.get(u);
|
|
1072
|
+
return [u, l ? st(c, l) : c];
|
|
1020
1073
|
}))
|
|
1021
|
-
}, i =
|
|
1022
|
-
return { sql: a.replace(
|
|
1023
|
-
const l = s.values[Number(
|
|
1024
|
-
return o.push(l instanceof Date ?
|
|
1074
|
+
}, i = yt(n.query, r), a = pt(i, e, s), o = [];
|
|
1075
|
+
return { sql: a.replace(ee, (u, c) => {
|
|
1076
|
+
const l = s.values[Number(c)];
|
|
1077
|
+
return o.push(l instanceof Date ? Z(l) : l), Te(e, o.length);
|
|
1025
1078
|
}), params: o };
|
|
1026
1079
|
}
|
|
1027
1080
|
export {
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
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
|
|
1052
1105
|
};
|