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