@nongt/visql 0.10.1 → 0.11.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/{MuiTimePicker-DRqT6ByZ.js → MuiTimePicker-CROJXSCL.js} +1 -1
- package/dist/{PipelineEditor-CGTZGbmf.js → PipelineEditor-DEiKHDrr.js} +3646 -3514
- package/dist/core.js +137 -119
- package/dist/explain.js +3 -3
- package/dist/fixtures.js +1 -1
- package/dist/index.js +102 -110
- package/dist/issueLocation-Yn71qJyP.js +1141 -0
- package/dist/mui-date-pickers-host.js +1 -1
- package/dist/mui-date-pickers.js +1 -1
- package/dist/mui.js +2 -2
- package/dist/{params-DkVFbMQn.js → params-DVkfcD6D.js} +245 -229
- package/dist/{schema-CgL10bid.js → schema-CzKt6cN2.js} +230 -152
- package/dist/sql-import.js +855 -821
- package/dist/{sqlGen-C1kZwvWI.js → sqlGen-BoJXaULd.js} +331 -309
- package/dist/{tokens-DV5rTGMj.js → tokens-3SURLAkL.js} +15 -14
- package/dist/types/core/FunctionsContext.d.ts +8 -2
- package/dist/types/core/ast.d.ts +9 -1
- package/dist/types/core/components/flow/WindowKeys.d.ts +10 -0
- package/dist/types/core/expressionFlow.d.ts +8 -0
- package/dist/types/core/functionRegistry.d.ts +15 -2
- package/dist/types/core/grouping.d.ts +6 -1
- package/dist/types/core/i18n/defaultMessages.d.ts +16 -0
- package/dist/types/core/i18n/messages.d.ts +12 -0
- package/dist/types/core/issueLocation.d.ts +5 -0
- package/dist/types/core/queryHelpers.d.ts +9 -1
- package/dist/types/core/queryValidation.d.ts +11 -1
- package/dist/types/core/storedQuery.d.ts +1 -1
- package/dist/types/core/theme/appearance.d.ts +4 -1
- package/dist/types/core/theme/looks.d.ts +6 -5
- package/dist/types/core/theme/tokens.d.ts +6 -3
- package/dist/types/sqlImport/syntax.d.ts +11 -2
- package/package.json +1 -1
- package/dist/issueLocation-BlQ280IE.js +0 -1108
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
var
|
|
2
|
-
var Mt = (t, r, e) => r in t ?
|
|
1
|
+
var Lt = Object.defineProperty;
|
|
2
|
+
var Mt = (t, r, e) => r in t ? Lt(t, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[r] = e;
|
|
3
3
|
var w = (t, r, e) => Mt(t, typeof r != "symbol" ? r + "" : r, e);
|
|
4
|
-
import { q as
|
|
5
|
-
import { p as K, t as Z, v as Ht, w as
|
|
6
|
-
import { s as
|
|
7
|
-
class
|
|
8
|
-
constructor(e, n, s,
|
|
9
|
-
const u =
|
|
4
|
+
import { q as Y, d as F, a as wt, s as Ft, m as k, e as ot, g as Ut, t as qt, j as Yt, b as _t, i as jt } from "./schema-CzKt6cN2.js";
|
|
5
|
+
import { p as K, t as Z, v as Ht, w as vt, x as Bt, y as it, z as Pt, A as Gt } from "./params-DVkfcD6D.js";
|
|
6
|
+
import { s as ut } from "./calendarDate-CYlr9TvX.js";
|
|
7
|
+
class xt extends Error {
|
|
8
|
+
constructor(e, n, s, o) {
|
|
9
|
+
const u = o ? `"${n}" (as "${o}")` : `"${n}"`;
|
|
10
10
|
super(`scopeQuery: the query at ${e.length ? e.join(".") : "the top"} reads ${u} 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). */
|
|
@@ -16,156 +16,156 @@ class Gt extends Error {
|
|
|
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
|
w(this, "alias");
|
|
19
|
-
this.name = "ScopeError", this.path = e, this.table = n,
|
|
19
|
+
this.name = "ScopeError", this.path = e, this.table = n, o && (this.alias = o);
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
|
-
const
|
|
22
|
+
const _ = (t) => Object.prototype.toString.call(t) === "[object Object]", ct = (t) => Array.isArray(t.joins) && _(t.from);
|
|
23
23
|
function U(t, r, e) {
|
|
24
24
|
if (Array.isArray(t)) {
|
|
25
|
-
const u = t.map((
|
|
26
|
-
return u.every((
|
|
25
|
+
const u = t.map((i, a) => U(i, [...r, a], e));
|
|
26
|
+
return u.every((i, a) => i === t[a]) ? t : u;
|
|
27
27
|
}
|
|
28
|
-
if (!
|
|
28
|
+
if (!_(t)) return t;
|
|
29
29
|
let n = !1;
|
|
30
|
-
const s = Object.entries(t).map(([u,
|
|
31
|
-
const a = U(
|
|
32
|
-
return a !==
|
|
33
|
-
}),
|
|
34
|
-
return
|
|
30
|
+
const s = Object.entries(t).map(([u, i]) => {
|
|
31
|
+
const a = U(i, [...r, u], e);
|
|
32
|
+
return a !== i && (n = !0), [u, a];
|
|
33
|
+
}), o = n ? Object.fromEntries(s) : t;
|
|
34
|
+
return ct(o) ? e(o, r) : o;
|
|
35
35
|
}
|
|
36
|
-
const
|
|
36
|
+
const at = (t, r) => Y(t).flatMap((e) => e.table !== void 0 && r.requires(e.table) ? [{ table: e.table, ...e.alias ? { alias: e.alias } : {}, ref: e.alias ?? e.table }] : []), ft = (t) => Y(t).flatMap((r) => {
|
|
37
37
|
const e = r.alias ?? r.table;
|
|
38
38
|
return e === void 0 ? [] : [e];
|
|
39
39
|
});
|
|
40
|
-
function
|
|
40
|
+
function P(t, r) {
|
|
41
41
|
if (t.type === "field") {
|
|
42
42
|
const e = t.table && r.get(t.table);
|
|
43
43
|
return e ? { ...t, table: e } : t;
|
|
44
44
|
}
|
|
45
|
-
return
|
|
45
|
+
return k(t, (e) => P(e, r));
|
|
46
46
|
}
|
|
47
|
-
const
|
|
47
|
+
const $t = (t) => t ? t.type === "logical" && t.operator === "AND" ? t.operands : [t] : [], Xt = (t) => t.joinType === "inner" || t.joinType === "left";
|
|
48
48
|
function kt(t, r, e, n, s) {
|
|
49
|
-
const
|
|
50
|
-
return t.joins.forEach((
|
|
51
|
-
const a =
|
|
52
|
-
if (a !== e && a !== n || !
|
|
53
|
-
const c =
|
|
49
|
+
const o = r.type === "binary" && r.left && r.right ? { ...r, left: r.right, right: r.left } : r, u = { forward: !1, back: !1 };
|
|
50
|
+
return t.joins.forEach((i) => {
|
|
51
|
+
const a = i.alias ?? i.table;
|
|
52
|
+
if (a !== e && a !== n || !Xt(i) || !$t(i.on).some((f) => F(f, r) || F(f, o))) return;
|
|
53
|
+
const c = i.joinType === "inner";
|
|
54
54
|
a === e ? (u.forward = !0, u.back || (u.back = c)) : (u.back = !0, u.forward || (u.forward = c || s));
|
|
55
55
|
}), u;
|
|
56
56
|
}
|
|
57
57
|
const d = (t) => t.some((r, e) => t.slice(0, e).every((n) => n.forward) && t.slice(e).every((n) => n.back)) || t.every((r) => r.forward);
|
|
58
|
-
function
|
|
59
|
-
const
|
|
60
|
-
const c = /* @__PURE__ */ new Map([[e.table, e.ref]]), f = /* @__PURE__ */ new Map(),
|
|
58
|
+
function lt(t, r, e, n, s = new Set(ft(r))) {
|
|
59
|
+
const o = Z(n.condition), u = Y(r), i = (a) => {
|
|
60
|
+
const c = /* @__PURE__ */ new Map([[e.table, e.ref]]), f = /* @__PURE__ */ new Map(), E = [], $ = new Set(s), A = (g) => {
|
|
61
61
|
const O = f.get(g);
|
|
62
62
|
return O ? [...A(O.ref), O.tie] : [];
|
|
63
|
-
}, y = (g, O,
|
|
64
|
-
if (O === g.length) return d(A(c.get(
|
|
65
|
-
const D = g[O],
|
|
66
|
-
if (!
|
|
67
|
-
const Q = c.get(Z(
|
|
63
|
+
}, y = (g, O, H) => {
|
|
64
|
+
if (O === g.length) return d(A(c.get(H)));
|
|
65
|
+
const D = g[O], L = Ft(t, [...c.keys()], D, { anyShape: !0 });
|
|
66
|
+
if (!L) return !1;
|
|
67
|
+
const Q = c.get(Z(L.on).find((T) => T !== D)), V = (T) => P(L.on, new Map([...c, [D, T]])), J = o.includes(D), z = (T, I) => {
|
|
68
68
|
c.set(D, T), f.set(T, { ref: Q, tie: I });
|
|
69
|
-
},
|
|
69
|
+
}, bt = (T, I) => (z(T, I), d(A(T)) && y(g, O + 1, H) ? !0 : (c.delete(D), f.delete(T), !1));
|
|
70
70
|
if (u.some((T) => {
|
|
71
71
|
if (T.table !== D) return !1;
|
|
72
|
-
const I = T.alias ?? D,
|
|
73
|
-
return (
|
|
72
|
+
const I = T.alias ?? D, v = kt(r, V(I), Q, I, J);
|
|
73
|
+
return (v.forward || v.back) && bt(I, v);
|
|
74
74
|
})) return !0;
|
|
75
75
|
if (!a) return !1;
|
|
76
76
|
let p = D;
|
|
77
|
-
for (let T = 2;
|
|
78
|
-
return
|
|
77
|
+
for (let T = 2; $.has(p); T += 1) p = `${D}_${T}`;
|
|
78
|
+
return $.add(p), z(p, { back: !0, forward: L.joinType === "inner" || J }), E.push({
|
|
79
79
|
id: Ht(),
|
|
80
80
|
table: D,
|
|
81
81
|
...p !== D ? { alias: p } : {},
|
|
82
|
-
joinType:
|
|
82
|
+
joinType: L.joinType,
|
|
83
83
|
on: V(p)
|
|
84
|
-
}), y(g, O + 1,
|
|
84
|
+
}), y(g, O + 1, H);
|
|
85
85
|
};
|
|
86
86
|
let R;
|
|
87
|
-
if (
|
|
87
|
+
if (o.every((g) => {
|
|
88
88
|
if (R || c.has(g)) return !0;
|
|
89
|
-
const O =
|
|
89
|
+
const O = wt(t, [...c.keys()], g, { anyShape: !0 });
|
|
90
90
|
return O ? y(O, 0, g) : (R = g, !0);
|
|
91
91
|
}))
|
|
92
|
-
return
|
|
92
|
+
return $.forEach((g) => s.add(g)), { joins: E, condition: P(n.condition, c), ...R ? { missing: R } : {} };
|
|
93
93
|
};
|
|
94
|
-
return
|
|
94
|
+
return i(!1) ?? i(!0);
|
|
95
95
|
}
|
|
96
|
-
function
|
|
97
|
-
return
|
|
96
|
+
function Et(t, r) {
|
|
97
|
+
return $t(t).some((e) => F(e, r));
|
|
98
98
|
}
|
|
99
|
-
function
|
|
99
|
+
function Wt(t, r) {
|
|
100
100
|
return (t == null ? void 0 : t.type) === "logical" && t.operator === "AND" ? { ...t, operands: [...t.operands, r] } : t ? { type: "logical", operator: "AND", operands: [t, r] } : r;
|
|
101
101
|
}
|
|
102
102
|
function tt(t, r, e, n, s) {
|
|
103
|
-
return
|
|
104
|
-
const
|
|
105
|
-
if (
|
|
106
|
-
const a =
|
|
107
|
-
return
|
|
108
|
-
...
|
|
109
|
-
joins: [...
|
|
110
|
-
where: a ?
|
|
103
|
+
return at(r, e).reduce((o, u) => {
|
|
104
|
+
const i = lt(t, o, u, e, s);
|
|
105
|
+
if (i.missing) throw new xt(n, u.table, i.missing, u.alias);
|
|
106
|
+
const a = Et(o.where, i.condition);
|
|
107
|
+
return i.joins.length === 0 && a ? o : {
|
|
108
|
+
...o,
|
|
109
|
+
joins: [...o.joins, ...i.joins],
|
|
110
|
+
where: a ? o.where : Wt(o.where, i.condition)
|
|
111
111
|
};
|
|
112
112
|
}, r);
|
|
113
113
|
}
|
|
114
114
|
function rt(t) {
|
|
115
|
-
const r = /* @__PURE__ */ new Map(), e = (n, s,
|
|
115
|
+
const r = /* @__PURE__ */ new Map(), e = (n, s, o, u) => {
|
|
116
116
|
if (Array.isArray(n)) {
|
|
117
117
|
n.forEach((a, c) => e(a, [...s, c], void 0, u));
|
|
118
118
|
return;
|
|
119
119
|
}
|
|
120
|
-
if (!
|
|
121
|
-
let
|
|
122
|
-
|
|
120
|
+
if (!_(n)) return;
|
|
121
|
+
let i = u;
|
|
122
|
+
ct(n) && (i = o ?? /* @__PURE__ */ new Set(), r.set(JSON.stringify(s), i), ft(n).forEach((a) => i.add(a))), Object.entries(n).forEach(([a, c]) => e(
|
|
123
123
|
c,
|
|
124
124
|
[...s, a],
|
|
125
|
-
n.type === "exists" && a === "query" ?
|
|
126
|
-
|
|
125
|
+
n.type === "exists" && a === "query" ? i : void 0,
|
|
126
|
+
i
|
|
127
127
|
));
|
|
128
128
|
};
|
|
129
129
|
return e(t, [], void 0, /* @__PURE__ */ new Set()), r;
|
|
130
130
|
}
|
|
131
|
-
function
|
|
131
|
+
function yr(t, r, e) {
|
|
132
132
|
const n = tt(
|
|
133
133
|
t,
|
|
134
134
|
r,
|
|
135
135
|
e,
|
|
136
136
|
[],
|
|
137
137
|
rt(r).get("[]")
|
|
138
|
-
), s = K(t, r, n),
|
|
138
|
+
), s = K(t, r, n), o = rt(s), u = U(s, [], (i, a) => a.length === 0 ? i : tt(t, i, e, a, o.get(JSON.stringify(a))));
|
|
139
139
|
return K(t, s, u);
|
|
140
140
|
}
|
|
141
|
-
function
|
|
142
|
-
if (!
|
|
141
|
+
function Dr(t, r, e) {
|
|
142
|
+
if (!_(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 U(r, [], (s,
|
|
146
|
-
const
|
|
147
|
-
(
|
|
145
|
+
return U(r, [], (s, o) => (at(s, e).forEach((u) => {
|
|
146
|
+
const i = lt(t, s, u, e);
|
|
147
|
+
(i.missing || i.joins.length > 0 || !Et(s.where, i.condition)) && n.push({ path: o, table: u.table, ...u.alias ? { alias: u.alias } : {} });
|
|
148
148
|
}), s)), n;
|
|
149
149
|
}
|
|
150
150
|
function G(t, r) {
|
|
151
151
|
const e = new Map(r == null ? void 0 : r.tableOf), n = new Set(r == null ? void 0 : r.aliases), s = new Set(r == null ? void 0 : r.derived);
|
|
152
|
-
return t.forEach(({ table:
|
|
153
|
-
if (
|
|
152
|
+
return t.forEach(({ table: o, alias: u }) => {
|
|
153
|
+
if (o === void 0) {
|
|
154
154
|
u && s.add(u);
|
|
155
155
|
return;
|
|
156
156
|
}
|
|
157
|
-
if (e.set(
|
|
158
|
-
e.set(u,
|
|
157
|
+
if (e.set(o, o), u) {
|
|
158
|
+
e.set(u, o), n.add(u), s.delete(u);
|
|
159
159
|
return;
|
|
160
160
|
}
|
|
161
|
-
n.delete(
|
|
161
|
+
n.delete(o), s.delete(o);
|
|
162
162
|
}), { tableOf: e, aliases: n, derived: s };
|
|
163
163
|
}
|
|
164
|
-
function
|
|
165
|
-
const n = t.on &&
|
|
166
|
-
return t.query ? { ...t, on: n, query:
|
|
164
|
+
function Qt(t, r, e) {
|
|
165
|
+
const n = t.on && M(t.on, r, e);
|
|
166
|
+
return t.query ? { ...t, on: n, query: Ut(t.query, (s) => j(s, e)) } : t.shared !== void 0 ? { ...t, on: n } : { ...t, on: n, table: e(t.table) };
|
|
167
167
|
}
|
|
168
|
-
function
|
|
168
|
+
function M(t, r, e) {
|
|
169
169
|
if (t.type === "field") {
|
|
170
170
|
if (!t.table || r.derived.has(t.table)) return t;
|
|
171
171
|
const n = r.tableOf.get(t.table) ?? t.table;
|
|
@@ -175,27 +175,27 @@ function L(t, r, e) {
|
|
|
175
175
|
...t.column ? { column: e(n, t.column) } : {}
|
|
176
176
|
};
|
|
177
177
|
}
|
|
178
|
-
if (t.type === "subquery") return { ...t, query:
|
|
178
|
+
if (t.type === "subquery") return { ...t, query: j(t.query, e) };
|
|
179
179
|
if (t.type === "exists") {
|
|
180
|
-
const { query: n } = t, s = [n.from, ...n.joins],
|
|
180
|
+
const { query: n } = t, s = [n.from, ...n.joins], o = G(s), u = G(s, r);
|
|
181
181
|
return {
|
|
182
182
|
...t,
|
|
183
|
-
link:
|
|
183
|
+
link: M(t.link, u, e),
|
|
184
184
|
query: {
|
|
185
185
|
...n,
|
|
186
186
|
from: { ...n.from, table: e(n.from.table) },
|
|
187
|
-
joins: n.joins.map((
|
|
188
|
-
...n.where ? { where:
|
|
187
|
+
joins: n.joins.map((i) => Qt(i, o, e)),
|
|
188
|
+
...n.where ? { where: M(n.where, o, e) } : {}
|
|
189
189
|
}
|
|
190
190
|
};
|
|
191
191
|
}
|
|
192
|
-
return
|
|
192
|
+
return k(t, (n) => M(n, r, e));
|
|
193
193
|
}
|
|
194
|
-
function
|
|
195
|
-
const e = G([t.from, ...t.joins]), n =
|
|
194
|
+
function j(t, r) {
|
|
195
|
+
const e = G([t.from, ...t.joins]), n = ot(
|
|
196
196
|
t,
|
|
197
|
-
(s) =>
|
|
198
|
-
(s) =>
|
|
197
|
+
(s) => M(s, e, r),
|
|
198
|
+
(s) => j(s, r)
|
|
199
199
|
);
|
|
200
200
|
return {
|
|
201
201
|
...n,
|
|
@@ -207,7 +207,7 @@ function x(t, r) {
|
|
|
207
207
|
if (Array.isArray(t)) return t.map((s) => x(s, r));
|
|
208
208
|
if (typeof t != "object" || t === null || t instanceof Date) return t;
|
|
209
209
|
const e = Object.fromEntries(Object.entries(t).map(
|
|
210
|
-
([s,
|
|
210
|
+
([s, o]) => [s, x(o, r)]
|
|
211
211
|
));
|
|
212
212
|
if (!("from" in e) || !Array.isArray(e.joins)) return e;
|
|
213
213
|
const n = (s) => s.table !== void 0 && !s.alias && r(s.table) !== s.table ? { ...s, alias: s.table } : s;
|
|
@@ -217,17 +217,17 @@ function x(t, r) {
|
|
|
217
217
|
joins: e.joins.map(n)
|
|
218
218
|
};
|
|
219
219
|
}
|
|
220
|
-
function
|
|
221
|
-
return
|
|
220
|
+
function Vt(t, r) {
|
|
221
|
+
return j(x(t, r), r);
|
|
222
222
|
}
|
|
223
|
-
const
|
|
224
|
-
function
|
|
223
|
+
const Jt = /^(\d{2}):(\d{2})(?::(\d{2}))?$/, et = /YYYY|MM|DD|HH|mm|ss/g, b = (t, r = 2) => String(t).padStart(r, "0");
|
|
224
|
+
function Tt(t, r) {
|
|
225
225
|
let e;
|
|
226
|
-
const n = typeof t == "string" ?
|
|
226
|
+
const n = typeof t == "string" ? Jt.exec(t.trim()) : null;
|
|
227
227
|
if (n)
|
|
228
228
|
e = { HH: n[1], mm: n[2], ss: n[3] ?? "00" };
|
|
229
229
|
else {
|
|
230
|
-
const u = typeof t == "string" ?
|
|
230
|
+
const u = typeof t == "string" ? vt(t, "dateTime") : t;
|
|
231
231
|
u instanceof Date && (e = {
|
|
232
232
|
YYYY: b(u.getUTCFullYear(), 4),
|
|
233
233
|
MM: b(u.getUTCMonth() + 1),
|
|
@@ -241,20 +241,20 @@ function Et(t, r) {
|
|
|
241
241
|
const s = e;
|
|
242
242
|
return (r.match(et) ?? []).some((u) => s[u] === void 0) ? t : r.replace(et, (u) => s[u]);
|
|
243
243
|
}
|
|
244
|
-
function
|
|
244
|
+
function At(t, r) {
|
|
245
245
|
const e = /* @__PURE__ */ new Map(), n = (s) => {
|
|
246
|
-
const
|
|
247
|
-
|
|
246
|
+
const o = /* @__PURE__ */ new Map();
|
|
247
|
+
Y(s).forEach((c) => o.set(qt(c), c.table));
|
|
248
248
|
const u = (c) => {
|
|
249
249
|
if ((c == null ? void 0 : c.type) !== "field" || !c.table || !c.column) return;
|
|
250
|
-
const f =
|
|
251
|
-
return
|
|
252
|
-
},
|
|
250
|
+
const f = o.has(c.table) ? o.get(c.table) : c.table, E = f === void 0 ? void 0 : Yt(r, f), $ = E == null ? void 0 : E.columns.find((A) => A.name === c.column);
|
|
251
|
+
return $ == null ? void 0 : $.format;
|
|
252
|
+
}, i = (c, f) => {
|
|
253
253
|
switch (c.type) {
|
|
254
254
|
case "literal":
|
|
255
|
-
return { ...c, value:
|
|
255
|
+
return { ...c, value: Tt(c.value, f) };
|
|
256
256
|
case "list":
|
|
257
|
-
return { ...c, items: c.items.map((
|
|
257
|
+
return { ...c, items: c.items.map((E) => i(E, f)) };
|
|
258
258
|
case "param":
|
|
259
259
|
return e.has(c.name) || e.set(c.name, f), c;
|
|
260
260
|
default:
|
|
@@ -262,20 +262,20 @@ function Tt(t, r) {
|
|
|
262
262
|
}
|
|
263
263
|
}, a = (c) => {
|
|
264
264
|
if (c.type === "subquery") return { ...c, query: n(c.query) };
|
|
265
|
-
const f =
|
|
265
|
+
const f = k(c, a);
|
|
266
266
|
if (f.type !== "binary" || !f.right) return f;
|
|
267
|
-
const
|
|
267
|
+
const E = u(f.left), $ = u(f.right);
|
|
268
268
|
return {
|
|
269
269
|
...f,
|
|
270
|
-
left:
|
|
271
|
-
right:
|
|
270
|
+
left: $ ? i(f.left, $) : f.left,
|
|
271
|
+
right: E ? i(f.right, E) : f.right
|
|
272
272
|
};
|
|
273
273
|
};
|
|
274
|
-
return
|
|
274
|
+
return ot(s, a, (c) => n(c));
|
|
275
275
|
};
|
|
276
276
|
return { query: n(t), params: e };
|
|
277
277
|
}
|
|
278
|
-
const
|
|
278
|
+
const Or = ["mysql", "mssql", "postgres"], Sr = {
|
|
279
279
|
mysql: "MySQL",
|
|
280
280
|
mssql: "SQL Server",
|
|
281
281
|
postgres: "PostgreSQL"
|
|
@@ -293,9 +293,9 @@ function N(t, r) {
|
|
|
293
293
|
return `"${e.replace(/"/g, '""')}"`;
|
|
294
294
|
}
|
|
295
295
|
}
|
|
296
|
-
const
|
|
296
|
+
const zt = /^[A-Za-z_][A-Za-z0-9_]*$/, mt = (t) => `${t}`, Kt = /\uE000(\d+)\uE000/g;
|
|
297
297
|
let C;
|
|
298
|
-
function
|
|
298
|
+
function Nr(t, r) {
|
|
299
299
|
C = { ...t, writing: [] };
|
|
300
300
|
try {
|
|
301
301
|
return r();
|
|
@@ -303,26 +303,26 @@ function Dr(t, r) {
|
|
|
303
303
|
C = void 0;
|
|
304
304
|
}
|
|
305
305
|
}
|
|
306
|
-
function
|
|
306
|
+
function gt(t, r, e) {
|
|
307
307
|
if (typeof t != "object" || t === null || t instanceof Date) return;
|
|
308
308
|
let n;
|
|
309
|
-
return Object.entries(t).some(([s,
|
|
309
|
+
return Object.entries(t).some(([s, o]) => {
|
|
310
310
|
const u = [...e, Array.isArray(t) ? Number(s) : s];
|
|
311
|
-
return n =
|
|
311
|
+
return n = o === r ? u : gt(o, r, u), n !== void 0;
|
|
312
312
|
}), n;
|
|
313
313
|
}
|
|
314
314
|
function m(t, r) {
|
|
315
315
|
if (!C) return r();
|
|
316
|
-
const { writing: e, paths: n } = C, s = e[e.length - 1],
|
|
317
|
-
if (!
|
|
318
|
-
e.push({ value: t, path:
|
|
316
|
+
const { writing: e, paths: n } = C, s = e[e.length - 1], o = typeof t == "string" ? [t] : gt(s.value, t, s.path);
|
|
317
|
+
if (!o) return r();
|
|
318
|
+
e.push({ value: t, path: o });
|
|
319
319
|
try {
|
|
320
|
-
return `${n.push(
|
|
320
|
+
return `${n.push(o) - 1}${r()}`;
|
|
321
321
|
} finally {
|
|
322
322
|
e.pop();
|
|
323
323
|
}
|
|
324
324
|
}
|
|
325
|
-
const
|
|
325
|
+
const Zt = {
|
|
326
326
|
mysql: {
|
|
327
327
|
text: "CHAR",
|
|
328
328
|
integer: "SIGNED",
|
|
@@ -351,25 +351,25 @@ const Kt = {
|
|
|
351
351
|
boolean: "boolean"
|
|
352
352
|
}
|
|
353
353
|
};
|
|
354
|
-
function
|
|
354
|
+
function dt(t, r) {
|
|
355
355
|
var n;
|
|
356
|
-
const e = (n =
|
|
356
|
+
const e = (n = Zt[r]) == null ? void 0 : n[t];
|
|
357
357
|
return e ? r === "postgres" ? `NULL::${e}` : `CAST(NULL AS ${e})` : "NULL";
|
|
358
358
|
}
|
|
359
|
-
function
|
|
359
|
+
function yt(t, r, e) {
|
|
360
360
|
if (t == null) return "NULL";
|
|
361
361
|
if (e)
|
|
362
|
-
return e.values.push(t),
|
|
362
|
+
return e.values.push(t), mt(e.values.length - 1);
|
|
363
363
|
if (typeof t == "boolean")
|
|
364
364
|
return r === "mssql" ? t ? "1" : "0" : t ? "TRUE" : "FALSE";
|
|
365
365
|
if (typeof t == "string") {
|
|
366
366
|
const n = C ? t.replace(/[\uE001-\uE003]/g, "") : t;
|
|
367
367
|
return n === "" ? "''" : `'${n.replace(/'/g, "''")}'`;
|
|
368
368
|
}
|
|
369
|
-
return typeof t == "number" || typeof t == "bigint" ? String(t) : t instanceof Date ? `'${
|
|
369
|
+
return typeof t == "number" || typeof t == "bigint" ? String(t) : t instanceof Date ? `'${ut(t)}'` : String(t);
|
|
370
370
|
}
|
|
371
371
|
let q = "native";
|
|
372
|
-
function
|
|
372
|
+
function Dt(t, r) {
|
|
373
373
|
q = t ?? "native";
|
|
374
374
|
try {
|
|
375
375
|
return r();
|
|
@@ -377,7 +377,7 @@ function yt(t, r) {
|
|
|
377
377
|
q = "native";
|
|
378
378
|
}
|
|
379
379
|
}
|
|
380
|
-
function
|
|
380
|
+
function tr(t, r) {
|
|
381
381
|
switch (t) {
|
|
382
382
|
case "!=":
|
|
383
383
|
return r === "mssql" ? "<>" : "!=";
|
|
@@ -388,7 +388,7 @@ function dt(t, r) {
|
|
|
388
388
|
if (r === "mssql") throw new Error("REGEXP isn't available in SQL Server");
|
|
389
389
|
return "REGEXP";
|
|
390
390
|
default:
|
|
391
|
-
if (!
|
|
391
|
+
if (!Gt.includes(t))
|
|
392
392
|
throw new Error(`unknown operator ${JSON.stringify(t)}`);
|
|
393
393
|
return t;
|
|
394
394
|
}
|
|
@@ -396,7 +396,7 @@ function dt(t, r) {
|
|
|
396
396
|
function h(t) {
|
|
397
397
|
return (t == null ? void 0 : t.type) === "literal" && typeof t.value == "string" && /^[A-Za-z_]+$/.test(t.value) ? t.value.toUpperCase() : "DAY";
|
|
398
398
|
}
|
|
399
|
-
const
|
|
399
|
+
const B = {
|
|
400
400
|
YEAR: "year",
|
|
401
401
|
QUARTER: "quarter",
|
|
402
402
|
MONTH: "month",
|
|
@@ -406,36 +406,42 @@ const v = {
|
|
|
406
406
|
MINUTE: "minute",
|
|
407
407
|
SECOND: "second",
|
|
408
408
|
MILLISECOND: "millisecond"
|
|
409
|
-
}
|
|
409
|
+
};
|
|
410
|
+
function nt(t, [r = "??", , e], n) {
|
|
411
|
+
if (n.length < 2) return `${t}(${r})`;
|
|
412
|
+
const s = n[1], o = s.type === "literal" && Number.isInteger(s.value) && s.value >= 0 ? String(s.value) : "??";
|
|
413
|
+
return `${t}(${r}, ${o}${e === void 0 ? "" : `, ${e}`})`;
|
|
414
|
+
}
|
|
415
|
+
const rr = {
|
|
410
416
|
// ---- Date arithmetic ----
|
|
411
417
|
DATE_ADD: ([t, r], e, n, s) => {
|
|
412
418
|
var u;
|
|
413
|
-
const
|
|
419
|
+
const o = h(e[2]);
|
|
414
420
|
switch (n) {
|
|
415
421
|
case "mysql":
|
|
416
|
-
return
|
|
422
|
+
return o === "MILLISECOND" ? `DATE_ADD(${t}, INTERVAL (${r}) * 1000 MICROSECOND)` : `DATE_ADD(${t}, INTERVAL ${r} ${o})`;
|
|
417
423
|
case "postgres": {
|
|
418
|
-
const
|
|
419
|
-
return !s && ((u = e[1]) == null ? void 0 : u.type) === "literal" && typeof e[1].value == "number" ? `(${t} + INTERVAL '${e[1].value} ${
|
|
424
|
+
const i = B[o] ?? o.toLowerCase();
|
|
425
|
+
return !s && ((u = e[1]) == null ? void 0 : u.type) === "literal" && typeof e[1].value == "number" ? `(${t} + INTERVAL '${e[1].value} ${i}')` : `(${t} + (${r} * INTERVAL '1 ${i}'))`;
|
|
420
426
|
}
|
|
421
427
|
case "mssql":
|
|
422
|
-
return `DATEADD(${
|
|
428
|
+
return `DATEADD(${o}, ${r}, ${t})`;
|
|
423
429
|
default:
|
|
424
430
|
return `DATE_ADD(${t}, ${r}, ${h(e[2])})`;
|
|
425
431
|
}
|
|
426
432
|
},
|
|
427
433
|
DATE_SUB: ([t, r], e, n, s) => {
|
|
428
434
|
var u;
|
|
429
|
-
const
|
|
435
|
+
const o = h(e[2]);
|
|
430
436
|
switch (n) {
|
|
431
437
|
case "mysql":
|
|
432
|
-
return
|
|
438
|
+
return o === "MILLISECOND" ? `DATE_SUB(${t}, INTERVAL (${r}) * 1000 MICROSECOND)` : `DATE_SUB(${t}, INTERVAL ${r} ${o})`;
|
|
433
439
|
case "postgres": {
|
|
434
|
-
const
|
|
435
|
-
return !s && ((u = e[1]) == null ? void 0 : u.type) === "literal" && typeof e[1].value == "number" ? `(${t} - INTERVAL '${e[1].value} ${
|
|
440
|
+
const i = B[o] ?? o.toLowerCase();
|
|
441
|
+
return !s && ((u = e[1]) == null ? void 0 : u.type) === "literal" && typeof e[1].value == "number" ? `(${t} - INTERVAL '${e[1].value} ${i}')` : `(${t} - (${r} * INTERVAL '1 ${i}'))`;
|
|
436
442
|
}
|
|
437
443
|
case "mssql":
|
|
438
|
-
return `DATEADD(${
|
|
444
|
+
return `DATEADD(${o}, -(${r}), ${t})`;
|
|
439
445
|
default:
|
|
440
446
|
return `DATE_SUB(${t}, ${r}, ${h(e[2])})`;
|
|
441
447
|
}
|
|
@@ -453,12 +459,12 @@ const v = {
|
|
|
453
459
|
case "YEAR":
|
|
454
460
|
return `EXTRACT(YEAR FROM age(${r}, ${t}))::integer`;
|
|
455
461
|
case "QUARTER": {
|
|
456
|
-
const
|
|
457
|
-
return `FLOOR((${
|
|
462
|
+
const o = `EXTRACT(YEAR FROM age(${r}, ${t}))`, u = `EXTRACT(MONTH FROM age(${r}, ${t}))`;
|
|
463
|
+
return `FLOOR((${o} * 12 + ${u}) / 3)::integer`;
|
|
458
464
|
}
|
|
459
465
|
case "MONTH": {
|
|
460
|
-
const
|
|
461
|
-
return `(${
|
|
466
|
+
const o = `EXTRACT(YEAR FROM age(${r}, ${t}))`, u = `EXTRACT(MONTH FROM age(${r}, ${t}))`;
|
|
467
|
+
return `(${o} * 12 + ${u})::integer`;
|
|
462
468
|
}
|
|
463
469
|
case "WEEK":
|
|
464
470
|
return `FLOOR((${r}::date - ${t}::date) / 7)`;
|
|
@@ -521,7 +527,7 @@ const v = {
|
|
|
521
527
|
return `/* unknown unit: ${n} */`;
|
|
522
528
|
}
|
|
523
529
|
case "postgres":
|
|
524
|
-
return `DATE_TRUNC('${
|
|
530
|
+
return `DATE_TRUNC('${B[n] ?? n.toLowerCase()}', ${t})`;
|
|
525
531
|
case "mssql":
|
|
526
532
|
switch (n) {
|
|
527
533
|
case "YEAR":
|
|
@@ -562,308 +568,324 @@ const v = {
|
|
|
562
568
|
const e = r[1], n = (e == null ? void 0 : e.type) === "literal" && typeof e.value == "string" && /^[A-Za-z][A-Za-z0-9_ ]*(\(\s*\d+\s*(,\s*\d+\s*)?\))?$/.test(e.value) ? e.value : "??";
|
|
563
569
|
return `CAST(${t} AS ${n})`;
|
|
564
570
|
},
|
|
571
|
+
// ---- Window functions (issue 20) ----
|
|
572
|
+
// The offset goes into the SQL as a whole number even in parameterized
|
|
573
|
+
// SQL: MySQL takes nothing else there. OVER is added by formatNode.
|
|
574
|
+
LAG: (t, r) => nt("LAG", t, r),
|
|
575
|
+
LEAD: (t, r) => nt("LEAD", t, r),
|
|
565
576
|
// ---- String aggregation (issue 137) ----
|
|
566
577
|
// The separator goes into the SQL as a literal even in parameterized SQL:
|
|
567
578
|
// MySQL's SEPARATOR takes nothing else.
|
|
568
|
-
STRING_AGG: ([t = "??"], r, e, n, s,
|
|
569
|
-
const u = r[1] ?? { type: "literal", value: ", " },
|
|
579
|
+
STRING_AGG: ([t = "??"], r, e, n, s, o) => {
|
|
580
|
+
const u = r[1] ?? { type: "literal", value: ", " }, i = u.type === "literal" && typeof u.value == "string" ? yt(u.value, e) : "??", a = s.distinct ? "DISTINCT " : "", c = ($) => {
|
|
570
581
|
var A;
|
|
571
|
-
return (A = s.orderBy) != null && A.length ? `ORDER BY ${s.orderBy.map((y) => `${
|
|
572
|
-
}, f = (
|
|
582
|
+
return (A = s.orderBy) != null && A.length ? `ORDER BY ${s.orderBy.map((y) => `${$(y)} ${y.direction === "desc" ? "DESC" : "ASC"}`).join(", ")}` : "";
|
|
583
|
+
}, f = ($) => o($.expression), E = ($) => $ ? ` ${$}` : "";
|
|
573
584
|
switch (e) {
|
|
574
585
|
case "mysql":
|
|
575
|
-
return `GROUP_CONCAT(${a}${t}${
|
|
586
|
+
return `GROUP_CONCAT(${a}${t}${E(c(f))} SEPARATOR ${i})`;
|
|
576
587
|
case "postgres": {
|
|
577
|
-
const
|
|
588
|
+
const $ = `CAST(${t} AS TEXT)`, A = c((y) => {
|
|
578
589
|
if (!s.distinct) return f(y);
|
|
579
|
-
if (F(y.expression, r[0])) return
|
|
590
|
+
if (F(y.expression, r[0])) return $;
|
|
580
591
|
throw new Error("PostgreSQL orders STRING_AGG of different values by the value only");
|
|
581
592
|
});
|
|
582
|
-
return `STRING_AGG(${a}${
|
|
593
|
+
return `STRING_AGG(${a}${$}, ${i}${E(A)})`;
|
|
583
594
|
}
|
|
584
595
|
case "mssql": {
|
|
585
596
|
if (s.distinct)
|
|
586
597
|
throw new Error("SQL Server's STRING_AGG can't join only different values");
|
|
587
|
-
const
|
|
588
|
-
return `STRING_AGG(CAST(${t} AS NVARCHAR(MAX)), ${
|
|
598
|
+
const $ = c(f), A = $ ? ` WITHIN GROUP (${$})` : "";
|
|
599
|
+
return `STRING_AGG(CAST(${t} AS NVARCHAR(MAX)), ${i})${A}`;
|
|
589
600
|
}
|
|
590
601
|
default:
|
|
591
|
-
return `STRING_AGG(${a}${t}, ${
|
|
602
|
+
return `STRING_AGG(${a}${t}, ${i}${E(c(f))})`;
|
|
592
603
|
}
|
|
593
604
|
}
|
|
594
605
|
};
|
|
606
|
+
function er(t, r) {
|
|
607
|
+
var n, s;
|
|
608
|
+
return `OVER (${[
|
|
609
|
+
...(n = t.partitionBy) != null && n.length ? [`PARTITION BY ${t.partitionBy.map(r).join(", ")}`] : [],
|
|
610
|
+
...(s = t.orderBy) != null && s.length ? [`ORDER BY ${t.orderBy.map((o) => `${r(o.expression)} ${o.direction === "desc" ? "DESC" : "ASC"}`).join(", ")}`] : []
|
|
611
|
+
].join(" ")})`;
|
|
612
|
+
}
|
|
595
613
|
function S(t) {
|
|
596
614
|
return t.type === "binary" || t.type === "logical" || t.type === "if" || t.type === "case";
|
|
597
615
|
}
|
|
598
|
-
function
|
|
599
|
-
return m(t, () =>
|
|
600
|
-
}
|
|
601
|
-
function
|
|
602
|
-
const s = (
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
616
|
+
function l(t, r, e, n) {
|
|
617
|
+
return m(t, () => sr(t, r, e, n));
|
|
618
|
+
}
|
|
619
|
+
function nr(t, r, e, n) {
|
|
620
|
+
const s = t.args.map((a) => l(a, r, e, n)), o = rr[t.name.toUpperCase()];
|
|
621
|
+
if (o)
|
|
622
|
+
return o(
|
|
623
|
+
s,
|
|
624
|
+
t.args,
|
|
625
|
+
r,
|
|
626
|
+
n !== void 0,
|
|
627
|
+
t,
|
|
628
|
+
(a) => l(a, r, e, n)
|
|
629
|
+
);
|
|
630
|
+
const u = /^[A-Za-z_][A-Za-z0-9_.]*$/.test(t.name) || t.name === "" ? t.name : "??";
|
|
631
|
+
if (s.length === 0 && u.toUpperCase() === "COUNT") return `${u}(*)`;
|
|
632
|
+
const i = t.distinct && s.length > 0 ? "DISTINCT " : "";
|
|
633
|
+
return `${u}(${i}${s.join(", ")})`;
|
|
634
|
+
}
|
|
635
|
+
function st(t, r, e, n) {
|
|
636
|
+
const s = (o) => {
|
|
637
|
+
if (!o) return "??";
|
|
638
|
+
const u = l(o, r, e, n);
|
|
639
|
+
return S(o) ? `(${u})` : u;
|
|
606
640
|
};
|
|
607
641
|
return m(t, () => {
|
|
608
|
-
const [
|
|
609
|
-
return `${s(
|
|
642
|
+
const [o, u] = t.type === "list" ? t.items : [t];
|
|
643
|
+
return `${s(o)} AND ${s(u)}`;
|
|
610
644
|
});
|
|
611
645
|
}
|
|
612
|
-
function
|
|
646
|
+
function sr(t, r, e, n) {
|
|
613
647
|
var u;
|
|
614
|
-
const s = " ".repeat(e),
|
|
648
|
+
const s = " ".repeat(e), o = " ".repeat(e + 1);
|
|
615
649
|
switch (t.type) {
|
|
616
650
|
case "literal":
|
|
617
|
-
return t.value === "" && !t.empty ? "??" : t.value === null && t.as ?
|
|
651
|
+
return t.value === "" && !t.empty ? "??" : t.value === null && t.as ? dt(t.as, r) : yt(t.value, r, n);
|
|
618
652
|
case "param": {
|
|
619
|
-
if (!n) return
|
|
653
|
+
if (!n) return zt.test(t.name) ? `:${t.name}` : "??";
|
|
620
654
|
if (!n.named.has(t.name))
|
|
621
655
|
throw new Error(`no value for parameter "${t.name}"`);
|
|
622
|
-
return n.values.push(n.named.get(t.name)),
|
|
656
|
+
return n.values.push(n.named.get(t.name)), mt(n.values.length - 1);
|
|
623
657
|
}
|
|
624
658
|
case "field": {
|
|
625
659
|
if (!t.column) return "??";
|
|
626
|
-
const
|
|
627
|
-
return t.table ? `${N(t.table, r)}.${
|
|
660
|
+
const i = N(t.column, r);
|
|
661
|
+
return t.table ? `${N(t.table, r)}.${i}` : i;
|
|
628
662
|
}
|
|
629
663
|
case "function": {
|
|
630
|
-
const
|
|
631
|
-
|
|
632
|
-
return a(
|
|
633
|
-
o,
|
|
634
|
-
t.args,
|
|
635
|
-
r,
|
|
636
|
-
n !== void 0,
|
|
637
|
-
t,
|
|
638
|
-
($) => E($, r, e, n)
|
|
639
|
-
);
|
|
640
|
-
const c = /^[A-Za-z_][A-Za-z0-9_.]*$/.test(t.name) || t.name === "" ? t.name : "??";
|
|
641
|
-
if (o.length === 0 && c.toUpperCase() === "COUNT") return `${c}(*)`;
|
|
642
|
-
const f = t.distinct && o.length > 0 ? "DISTINCT " : "";
|
|
643
|
-
return `${c}(${f}${o.join(", ")})`;
|
|
664
|
+
const i = nr(t, r, e, n);
|
|
665
|
+
return t.over ? `${i} ${er(t, (a) => l(a, r, e, n))}` : i;
|
|
644
666
|
}
|
|
645
667
|
case "binary": {
|
|
646
|
-
const
|
|
647
|
-
if (
|
|
648
|
-
if (!t.right) return `${c} ${
|
|
649
|
-
if (
|
|
650
|
-
return `${c} BETWEEN ${
|
|
651
|
-
if (
|
|
652
|
-
return t.right.type === "list" ? `${c} IN ${
|
|
653
|
-
const f =
|
|
668
|
+
const i = t.operator ? tr(t.operator, r) : "=", a = l(t.left, r, e, n), c = S(t.left) ? `(${a})` : a;
|
|
669
|
+
if (Pt(t.operator)) return `${c} ${i}`;
|
|
670
|
+
if (!t.right) return `${c} ${i} ??`;
|
|
671
|
+
if (i === "BETWEEN")
|
|
672
|
+
return `${c} BETWEEN ${st(t.right, r, e, n)}`;
|
|
673
|
+
if (i === "IN")
|
|
674
|
+
return t.right.type === "list" ? `${c} IN ${l(t.right, r, e, n)}` : `${c} IN (${l(t.right, r, e, n)})`;
|
|
675
|
+
const f = l(t.right, r, e, n), E = S(t.right) ? `(${f})` : f;
|
|
654
676
|
if (t.operator === "IS NOT DISTINCT FROM" && q === "expanded") {
|
|
655
|
-
const
|
|
656
|
-
const R =
|
|
677
|
+
const $ = (y) => {
|
|
678
|
+
const R = l(y, r, e, n);
|
|
657
679
|
return S(y) ? `(${R})` : R;
|
|
658
|
-
}, A = `${
|
|
659
|
-
return `(${c} = ${
|
|
680
|
+
}, A = `${$(t.left)} IS NULL AND ${$(t.right)} IS NULL`;
|
|
681
|
+
return `(${c} = ${E} OR (${A}))`;
|
|
660
682
|
}
|
|
661
|
-
return `${c} ${
|
|
683
|
+
return `${c} ${i} ${E}`;
|
|
662
684
|
}
|
|
663
685
|
case "logical": {
|
|
664
686
|
if (t.operands.length === 0) return "/* empty */";
|
|
665
687
|
if (t.operands.length === 1)
|
|
666
|
-
return
|
|
688
|
+
return l(t.operands[0], r, e, n);
|
|
667
689
|
if (t.operator !== "AND" && t.operator !== "OR")
|
|
668
690
|
throw new Error(`unknown operator ${JSON.stringify(t.operator)}`);
|
|
669
|
-
const
|
|
691
|
+
const i = `
|
|
670
692
|
${s}${t.operator} `;
|
|
671
693
|
return t.operands.map((a) => {
|
|
672
694
|
if (a.type === "logical") {
|
|
673
|
-
const c =
|
|
695
|
+
const c = l(a, r, e + 1, n);
|
|
674
696
|
return `(
|
|
675
|
-
${
|
|
697
|
+
${o}${c}
|
|
676
698
|
${s})`;
|
|
677
699
|
}
|
|
678
|
-
return
|
|
679
|
-
}).join(
|
|
700
|
+
return l(a, r, e, n);
|
|
701
|
+
}).join(i);
|
|
680
702
|
}
|
|
681
703
|
case "not": {
|
|
682
|
-
const { operand:
|
|
683
|
-
if (
|
|
684
|
-
const a =
|
|
685
|
-
return `${S(
|
|
704
|
+
const { operand: i } = t;
|
|
705
|
+
if (i.type === "binary" && i.operator === "IN" && ((u = i.right) == null ? void 0 : u.type) === "subquery") {
|
|
706
|
+
const a = l(i.left, r, e, n);
|
|
707
|
+
return `${S(i.left) ? `(${a})` : a} NOT IN (${l(i.right, r, e, n)})`;
|
|
686
708
|
}
|
|
687
|
-
if (
|
|
688
|
-
const a =
|
|
689
|
-
return `${S(
|
|
709
|
+
if (i.type === "binary" && i.operator === "BETWEEN" && i.right) {
|
|
710
|
+
const a = l(i.left, r, e, n);
|
|
711
|
+
return `${S(i.left) ? `(${a})` : a} NOT BETWEEN ${st(i.right, r, e, n)}`;
|
|
690
712
|
}
|
|
691
|
-
if (
|
|
692
|
-
if (r === "mysql") return `NOT (${
|
|
693
|
-
const a =
|
|
694
|
-
return `${c} IS DISTINCT FROM ${
|
|
713
|
+
if (i.type === "binary" && i.operator === "IS NOT DISTINCT FROM" && i.right && q === "native") {
|
|
714
|
+
if (r === "mysql") return `NOT (${l(i, r, e, n)})`;
|
|
715
|
+
const a = l(i.left, r, e, n), c = S(i.left) ? `(${a})` : a, f = l(i.right, r, e, n), E = S(i.right) ? `(${f})` : f;
|
|
716
|
+
return `${c} IS DISTINCT FROM ${E}`;
|
|
695
717
|
}
|
|
696
|
-
if (
|
|
697
|
-
const a =
|
|
718
|
+
if (i.type === "logical" || i.type === "not") {
|
|
719
|
+
const a = l(i, r, e + 1, n);
|
|
698
720
|
return `NOT (
|
|
699
|
-
${
|
|
721
|
+
${o}${a}
|
|
700
722
|
${s})`;
|
|
701
723
|
}
|
|
702
|
-
return `NOT ${
|
|
724
|
+
return `NOT ${l(i, r, e, n)}`;
|
|
703
725
|
}
|
|
704
726
|
case "if": {
|
|
705
|
-
const
|
|
706
|
-
return r === "mysql" ? `IF(${
|
|
727
|
+
const i = t.condition ? l(t.condition, r, e, n) : "??", a = t.then ? l(t.then, r, e, n) : "??", c = t.else ? l(t.else, r, e, n) : "NULL";
|
|
728
|
+
return r === "mysql" ? `IF(${i}, ${a}, ${c})` : r === "mssql" ? `IIF(${i}, ${a}, ${c})` : `CASE WHEN ${i} THEN ${a} ELSE ${c} END`;
|
|
707
729
|
}
|
|
708
730
|
case "case": {
|
|
709
|
-
const
|
|
710
|
-
const
|
|
711
|
-
return `${
|
|
731
|
+
const i = t.subject ? ` ${l(t.subject, r, e, n)}` : "", a = t.branches.map((f) => {
|
|
732
|
+
const E = l(f.when, r, e + 1, n), $ = l(f.then, r, e + 1, n);
|
|
733
|
+
return `${o}WHEN ${E} THEN ${$}`;
|
|
712
734
|
}).join(`
|
|
713
735
|
`), c = t.else ? `
|
|
714
|
-
${
|
|
715
|
-
return `CASE${
|
|
736
|
+
${o}ELSE ${l(t.else, r, e + 1, n)}` : "";
|
|
737
|
+
return `CASE${i}
|
|
716
738
|
${a}${c}
|
|
717
739
|
${s}END`;
|
|
718
740
|
}
|
|
719
741
|
// EXISTS (SELECT 1 FROM … WHERE <link> AND <where>): the subquery written
|
|
720
742
|
// in place, one level in, so its params fall in reading order.
|
|
721
743
|
case "exists": {
|
|
722
|
-
const { query:
|
|
744
|
+
const { query: i, link: a } = t, c = ($) => $.type === "logical" && $.operator === "AND" ? $.operands : [$], f = i.where ? { type: "logical", operator: "AND", operands: [...c(a), ...c(i.where)] } : a;
|
|
723
745
|
return `EXISTS (
|
|
724
746
|
${[
|
|
725
747
|
"SELECT 1",
|
|
726
748
|
// The clauses below write their expressions with formatNode.
|
|
727
749
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
728
|
-
|
|
750
|
+
pt(i.from, r),
|
|
729
751
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
730
|
-
...
|
|
752
|
+
...i.joins.map(($) => It($, r, n)),
|
|
731
753
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
732
|
-
|
|
754
|
+
Ot(f, r, n)
|
|
733
755
|
].join(`
|
|
734
|
-
`).replace(/^/gm,
|
|
756
|
+
`).replace(/^/gm, o)}
|
|
735
757
|
${s})`;
|
|
736
758
|
}
|
|
737
759
|
// IN's right side (issue 122): its query, on lines of its own, inside the
|
|
738
760
|
// parentheses IN writes around it.
|
|
739
761
|
// An empty list is unfinished, like a value not filled in.
|
|
740
762
|
case "list":
|
|
741
|
-
return `(${t.items.map((
|
|
763
|
+
return `(${t.items.map((i) => l(i, r, e, n)).join(", ") || "??"})`;
|
|
742
764
|
case "subquery":
|
|
743
765
|
return `
|
|
744
766
|
${W(t.query, r, n).join(`
|
|
745
|
-
`).replace(/^/gm,
|
|
767
|
+
`).replace(/^/gm, o)}
|
|
746
768
|
${s}`;
|
|
747
769
|
default:
|
|
748
770
|
return "/* unknown */";
|
|
749
771
|
}
|
|
750
772
|
}
|
|
751
|
-
function
|
|
773
|
+
function Ot(t, r, e) {
|
|
752
774
|
return `WHERE
|
|
753
|
-
${
|
|
775
|
+
${l(t, r, 1, e)}`;
|
|
754
776
|
}
|
|
755
|
-
const
|
|
777
|
+
const or = {
|
|
756
778
|
inner: "INNER JOIN",
|
|
757
779
|
left: "LEFT JOIN",
|
|
758
780
|
right: "RIGHT JOIN",
|
|
759
781
|
full: "FULL JOIN",
|
|
760
782
|
cross: "CROSS JOIN"
|
|
761
783
|
};
|
|
762
|
-
let
|
|
763
|
-
function
|
|
764
|
-
return t.shared !== void 0 ? N(
|
|
765
|
-
${
|
|
784
|
+
let St = /* @__PURE__ */ new Map();
|
|
785
|
+
function Nt(t, r, e) {
|
|
786
|
+
return t.shared !== void 0 ? N(St.get(t.shared) ?? "??", r) : t.query ? `(
|
|
787
|
+
${Rt(t.query, r, e).replace(/^/gm, " ")}
|
|
766
788
|
)` : N(t.table, r);
|
|
767
789
|
}
|
|
768
|
-
function
|
|
790
|
+
function Rt(t, r, e) {
|
|
769
791
|
return _t(t).map((n) => W(n, r, e).join(`
|
|
770
|
-
`)).join(
|
|
792
|
+
`)).join(jt(t) && !t.all ? `
|
|
771
793
|
UNION
|
|
772
794
|
` : `
|
|
773
795
|
UNION ALL
|
|
774
796
|
`);
|
|
775
797
|
}
|
|
776
|
-
function
|
|
798
|
+
function ir(t, r, e) {
|
|
777
799
|
return t.length === 0 ? [] : [`WITH
|
|
778
800
|
${t.map((s) => m(s, () => {
|
|
779
|
-
const
|
|
801
|
+
const o = Rt(s.query, r, e).replace(/^/gm, " ");
|
|
780
802
|
return ` ${N(s.name, r)} AS (
|
|
781
|
-
${
|
|
803
|
+
${o}
|
|
782
804
|
)`;
|
|
783
805
|
})).join(`,
|
|
784
806
|
`)}`];
|
|
785
807
|
}
|
|
786
|
-
function
|
|
808
|
+
function pt(t, r, e) {
|
|
787
809
|
const n = t.alias ? ` AS ${N(t.alias, r)}` : "";
|
|
788
|
-
return `FROM ${m(t, () => `${
|
|
810
|
+
return `FROM ${m(t, () => `${Nt(t, r, e)}${n}`)}`;
|
|
789
811
|
}
|
|
790
|
-
function
|
|
812
|
+
function It(t, r, e) {
|
|
791
813
|
return m(t, () => {
|
|
792
|
-
const n =
|
|
814
|
+
const n = Nt(t, r, e), s = t.alias ? ` AS ${N(t.alias, r)}` : "";
|
|
793
815
|
if (t.joinType === "cross") return `CROSS JOIN ${n}${s}`;
|
|
794
|
-
const
|
|
795
|
-
return `${
|
|
796
|
-
ON ${
|
|
816
|
+
const o = t.on ? l(t.on, r, 1, e) : "/* unconfigured */";
|
|
817
|
+
return `${or[t.joinType]} ${n}${s}
|
|
818
|
+
ON ${o}`;
|
|
797
819
|
});
|
|
798
820
|
}
|
|
799
|
-
function
|
|
821
|
+
function ur(t, r, e, n = "SELECT") {
|
|
800
822
|
if (t.length === 0) return `${n} ${m(t, () => "*")}`;
|
|
801
|
-
const s = t.map((
|
|
802
|
-
const u =
|
|
803
|
-
return `${u}${
|
|
823
|
+
const s = t.map((o) => m(o, () => {
|
|
824
|
+
const u = l(o.expression, r, 1, e), i = o.alias ? ` AS ${N(o.alias, r)}` : "";
|
|
825
|
+
return `${u}${i}`;
|
|
804
826
|
}));
|
|
805
827
|
return `${n}
|
|
806
828
|
${s.join(`,
|
|
807
829
|
`)}`;
|
|
808
830
|
}
|
|
809
|
-
function
|
|
831
|
+
function cr(t, r, e) {
|
|
810
832
|
return `GROUP BY
|
|
811
833
|
${t.map((s) => m(
|
|
812
834
|
s,
|
|
813
|
-
() =>
|
|
835
|
+
() => l(s.expression, r, 1, e)
|
|
814
836
|
)).join(`,
|
|
815
837
|
`)}`;
|
|
816
838
|
}
|
|
817
|
-
function
|
|
839
|
+
function ar(t, r, e) {
|
|
818
840
|
return `HAVING
|
|
819
|
-
${
|
|
841
|
+
${l(t, r, 1, e)}`;
|
|
820
842
|
}
|
|
821
|
-
function
|
|
843
|
+
function fr(t, r, e, n) {
|
|
822
844
|
return `ORDER BY
|
|
823
|
-
${t.map((
|
|
845
|
+
${t.map((o) => m(o, () => `${l(n(o.expression), r, 1, e)} ${o.direction === "desc" ? "DESC" : "ASC"}`)).join(`,
|
|
824
846
|
`)}`;
|
|
825
847
|
}
|
|
826
|
-
const
|
|
827
|
-
function
|
|
828
|
-
const e = r.resolveName ?
|
|
829
|
-
return r.defaultLimit !== void 0 &&
|
|
848
|
+
const X = (t) => t !== void 0 && Number.isInteger(t) && t >= 0 ? t : void 0;
|
|
849
|
+
function ht(t, r) {
|
|
850
|
+
const e = r.resolveName ? Vt(t, r.resolveName) : t;
|
|
851
|
+
return r.defaultLimit !== void 0 && X(e.limit) === void 0 ? { ...e, limit: r.defaultLimit } : e;
|
|
830
852
|
}
|
|
831
|
-
const
|
|
853
|
+
const $r = "18446744073709551615";
|
|
832
854
|
function W(t, r, e, n = `SELECT${t.distinct ? " DISTINCT" : ""}`) {
|
|
833
855
|
const s = [
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
...t.joins.map((u) =>
|
|
856
|
+
ur(t.select, r, e, n),
|
|
857
|
+
pt(t.from, r, e),
|
|
858
|
+
...t.joins.map((u) => It(u, r, e))
|
|
837
859
|
];
|
|
838
|
-
t.where && s.push(
|
|
839
|
-
const
|
|
840
|
-
if (
|
|
841
|
-
const u = r === "mysql" ?
|
|
842
|
-
s.push(
|
|
860
|
+
t.where && s.push(Ot(t.where, r, e));
|
|
861
|
+
const o = t.groupBy ?? [];
|
|
862
|
+
if (o.length > 0 && s.push(cr(o, r, e)), t.having) {
|
|
863
|
+
const u = r === "mysql" ? it(t.having, t) : t.having;
|
|
864
|
+
s.push(ar(u, r, e));
|
|
843
865
|
}
|
|
844
866
|
return s;
|
|
845
867
|
}
|
|
846
|
-
function
|
|
847
|
-
C && (C.writing = [{ value: t, path: [] }]),
|
|
848
|
-
const n =
|
|
849
|
-
if (u.length > 0 && c.push(
|
|
850
|
-
if (
|
|
868
|
+
function Ct(t, r, e) {
|
|
869
|
+
C && (C.writing = [{ value: t, path: [] }]), St = new Map((t.shared ?? []).map((f) => [f.id, f.name]));
|
|
870
|
+
const n = ir(t.shared ?? [], r, e), s = X(t.limit), o = X(t.offset) || void 0, u = t.orderBy ?? [], i = r === "mssql" && s !== void 0 && o === void 0, a = `SELECT${t.distinct ? " DISTINCT" : ""}${i ? ` ${m("limit", () => `TOP (${s})`)}` : ""}`, c = W(t, r, e, a);
|
|
871
|
+
if (u.length > 0 && c.push(fr(u, r, e, (f) => r === "mysql" ? it(f, t) : f)), r === "mssql") {
|
|
872
|
+
if (o !== void 0) {
|
|
851
873
|
u.length === 0 && c.push("ORDER BY (SELECT NULL)");
|
|
852
874
|
const f = s !== void 0 ? ` ${m("limit", () => `FETCH NEXT ${s} ROWS ONLY`)}` : "";
|
|
853
|
-
c.push(`${m("offset", () => `OFFSET ${
|
|
875
|
+
c.push(`${m("offset", () => `OFFSET ${o} ROWS`)}${f}`);
|
|
854
876
|
}
|
|
855
|
-
} else if (s !== void 0 ||
|
|
856
|
-
const f = s !== void 0 || r === "mysql" ? m(s !== void 0 ? "limit" : {}, () => `LIMIT ${s ??
|
|
857
|
-
c.push([f,
|
|
877
|
+
} else if (s !== void 0 || o !== void 0) {
|
|
878
|
+
const f = s !== void 0 || r === "mysql" ? m(s !== void 0 ? "limit" : {}, () => `LIMIT ${s ?? $r}`) : "", E = o !== void 0 ? m("offset", () => `OFFSET ${o}`) : "";
|
|
879
|
+
c.push([f, E].filter(Boolean).join(" "));
|
|
858
880
|
}
|
|
859
881
|
return [...n, ...c].join(`
|
|
860
882
|
`);
|
|
861
883
|
}
|
|
862
|
-
function
|
|
863
|
-
const n = e.values ?
|
|
864
|
-
return
|
|
884
|
+
function Rr(t, r, e = {}) {
|
|
885
|
+
const n = e.values ? Bt(t, e.values) : t, s = e.schema ? At(n, e.schema).query : n, o = ht(s, e);
|
|
886
|
+
return Dt(e.nullSafeEqual, () => Ct(o, r));
|
|
865
887
|
}
|
|
866
|
-
function
|
|
888
|
+
function lr(t, r) {
|
|
867
889
|
switch (t) {
|
|
868
890
|
case "mysql":
|
|
869
891
|
return "?";
|
|
@@ -875,29 +897,29 @@ function ar(t, r) {
|
|
|
875
897
|
return "?";
|
|
876
898
|
}
|
|
877
899
|
}
|
|
878
|
-
function
|
|
879
|
-
const n = e.schema ?
|
|
900
|
+
function pr(t, r, e = {}) {
|
|
901
|
+
const n = e.schema ? At(t, e.schema) : { query: t, params: /* @__PURE__ */ new Map() }, s = {
|
|
880
902
|
values: [],
|
|
881
903
|
named: new Map(Object.entries(e.values ?? {}).map(([c, f]) => {
|
|
882
|
-
const
|
|
883
|
-
return [c,
|
|
904
|
+
const E = n.params.get(c);
|
|
905
|
+
return [c, E ? Tt(f, E) : f];
|
|
884
906
|
}))
|
|
885
|
-
},
|
|
907
|
+
}, o = ht(n.query, e), u = Dt(
|
|
886
908
|
e.nullSafeEqual,
|
|
887
|
-
() =>
|
|
888
|
-
),
|
|
889
|
-
return { sql: u.replace(
|
|
890
|
-
const
|
|
891
|
-
return
|
|
892
|
-
}), params:
|
|
909
|
+
() => Ct(o, r, s)
|
|
910
|
+
), i = [];
|
|
911
|
+
return { sql: u.replace(Kt, (c, f) => {
|
|
912
|
+
const E = s.values[Number(f)];
|
|
913
|
+
return i.push(E instanceof Date ? ut(E) : E), lr(r, i.length);
|
|
914
|
+
}), params: i };
|
|
893
915
|
}
|
|
894
916
|
export {
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
917
|
+
Or as D,
|
|
918
|
+
xt as S,
|
|
919
|
+
pr as a,
|
|
920
|
+
Rr as b,
|
|
921
|
+
Sr as c,
|
|
922
|
+
yr as s,
|
|
923
|
+
Dr as u,
|
|
924
|
+
Nr as w
|
|
903
925
|
};
|