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