@nongt/visql 0.7.0 → 0.9.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/README.md +23 -359
- package/dist/{MuiTimePicker-BjywwrRm.js → MuiTimePicker-DRqT6ByZ.js} +1 -1
- package/dist/PipelineEditor-C10lng6j.js +10544 -0
- package/dist/core.js +336 -78
- package/dist/explain.js +165 -0
- package/dist/fixtures.js +1 -1
- package/dist/index.js +114 -98
- package/dist/issueLocation-BOSdd1JX.js +1106 -0
- package/dist/mui-date-pickers-host.js +1 -1
- package/dist/mui-date-pickers.js +1 -1
- package/dist/mui.js +95 -84
- package/dist/params-MMOdKWnW.js +2217 -0
- package/dist/{schema-DFC26AhZ.js → schema-dP_ISylQ.js} +270 -242
- package/dist/sql-import.js +613 -627
- package/dist/sqlGen-B7-2-i9V.js +903 -0
- package/dist/style.css +1 -1
- package/dist/tokens-DV5rTGMj.js +171 -0
- package/dist/types/core/ast.d.ts +1 -1
- package/dist/types/core/components/flow/NodeActionFrame.d.ts +20 -0
- package/dist/types/core/components/flow/useNodeActions.d.ts +18 -0
- package/dist/types/core/i18n/defaultMessages.d.ts +15 -0
- package/dist/types/core/i18n/messages.d.ts +11 -0
- package/dist/types/core/params.d.ts +10 -0
- package/dist/types/core/planFindings.d.ts +27 -0
- package/dist/types/core/queryHelpers.d.ts +1 -1
- package/dist/types/core/schema.d.ts +35 -20
- package/dist/types/core/scopeQuery.d.ts +2 -2
- package/dist/types/core/sqlGen.d.ts +10 -0
- package/dist/types/core/theme/appearance.d.ts +27 -0
- package/dist/types/core/theme/looks.d.ts +14 -0
- package/dist/types/core/theme/tokens.d.ts +9 -1
- package/dist/types/core-api.d.ts +3 -2
- package/dist/types/explain/explainQuery.d.ts +53 -0
- package/dist/types/explain/postgresPlan.d.ts +12 -0
- package/dist/types/explain.d.ts +2 -0
- package/dist/types/features/queryEditor/CanvasBackground.d.ts +2 -0
- package/dist/types/features/queryEditor/ExplainButton.d.ts +13 -0
- package/dist/types/features/queryEditor/IssuesButton.d.ts +14 -6
- package/dist/types/features/queryEditor/PipelineEditor.d.ts +15 -0
- package/dist/types/features/queryEditor/planText.d.ts +8 -0
- package/dist/types/index.d.ts +2 -0
- package/package.json +14 -1
- package/dist/PipelineEditor-9-9xOJcB.js +0 -10214
- package/dist/core-api-BsEMnDQ1.js +0 -1141
- package/dist/issueLocation-732-e0B8.js +0 -3281
- package/dist/tokens-BTaVMSOx.js +0 -95
|
@@ -0,0 +1,903 @@
|
|
|
1
|
+
var bt = Object.defineProperty;
|
|
2
|
+
var Mt = (t, r, e) => r in t ? bt(t, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[r] = e;
|
|
3
|
+
var w = (t, r, e) => Mt(t, typeof r != "symbol" ? r + "" : r, e);
|
|
4
|
+
import { q as _, d as F, e as Lt, s as wt, m as X, g as st, j as Ft, t as Ut, k as qt, b as _t, i as Yt } from "./schema-dP_ISylQ.js";
|
|
5
|
+
import { t as K, v as Z, w as Ht, x as jt, y as Pt, z as ot, A as vt, B as Bt } from "./params-MMOdKWnW.js";
|
|
6
|
+
import { s as it } from "./calendarDate-CYlr9TvX.js";
|
|
7
|
+
class Gt extends Error {
|
|
8
|
+
constructor(e, n, s, i) {
|
|
9
|
+
const u = i ? `"${n}" (as "${i}")` : `"${n}"`;
|
|
10
|
+
super(`scopeQuery: the query at ${e.length ? e.join(".") : "the top"} reads ${u} but can't reach "${s}", which the condition names`);
|
|
11
|
+
/** Where the body is, as keys into the query (validateQuery's
|
|
12
|
+
* `pathSegments` style). */
|
|
13
|
+
w(this, "path");
|
|
14
|
+
/** The table it reads that needs scope. */
|
|
15
|
+
w(this, "table");
|
|
16
|
+
/** The alias that copy of the table goes by, when it has one (a self
|
|
17
|
+
* join reads a table more than once, issue 216). */
|
|
18
|
+
w(this, "alias");
|
|
19
|
+
this.name = "ScopeError", this.path = e, this.table = n, i && (this.alias = i);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const Y = (t) => Object.prototype.toString.call(t) === "[object Object]", ut = (t) => Array.isArray(t.joins) && Y(t.from);
|
|
23
|
+
function U(t, r, e) {
|
|
24
|
+
if (Array.isArray(t)) {
|
|
25
|
+
const u = t.map((o, a) => U(o, [...r, a], e));
|
|
26
|
+
return u.every((o, a) => o === t[a]) ? t : u;
|
|
27
|
+
}
|
|
28
|
+
if (!Y(t)) return t;
|
|
29
|
+
let n = !1;
|
|
30
|
+
const s = Object.entries(t).map(([u, o]) => {
|
|
31
|
+
const a = U(o, [...r, u], e);
|
|
32
|
+
return a !== o && (n = !0), [u, a];
|
|
33
|
+
}), i = n ? Object.fromEntries(s) : t;
|
|
34
|
+
return ut(i) ? e(i, r) : i;
|
|
35
|
+
}
|
|
36
|
+
const ct = (t, r) => _(t).flatMap((e) => e.table !== void 0 && r.requires(e.table) ? [{ table: e.table, ...e.alias ? { alias: e.alias } : {}, ref: e.alias ?? e.table }] : []), at = (t) => _(t).flatMap((r) => {
|
|
37
|
+
const e = r.alias ?? r.table;
|
|
38
|
+
return e === void 0 ? [] : [e];
|
|
39
|
+
});
|
|
40
|
+
function B(t, r) {
|
|
41
|
+
if (t.type === "field") {
|
|
42
|
+
const e = t.table && r.get(t.table);
|
|
43
|
+
return e ? { ...t, table: e } : t;
|
|
44
|
+
}
|
|
45
|
+
return X(t, (e) => B(e, r));
|
|
46
|
+
}
|
|
47
|
+
const ft = (t) => t ? t.type === "logical" && t.operator === "AND" ? t.operands : [t] : [], xt = (t) => t.joinType === "inner" || t.joinType === "left";
|
|
48
|
+
function kt(t, r, e, n, s) {
|
|
49
|
+
const i = 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((o) => {
|
|
51
|
+
const a = o.alias ?? o.table;
|
|
52
|
+
if (a !== e && a !== n || !xt(o) || !ft(o.on).some((f) => F(f, r) || F(f, i))) return;
|
|
53
|
+
const c = o.joinType === "inner";
|
|
54
|
+
a === e ? (u.forward = !0, u.back || (u.back = c)) : (u.back = !0, u.forward || (u.forward = c || s));
|
|
55
|
+
}), u;
|
|
56
|
+
}
|
|
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 $t(t, r, e, n, s = new Set(at(r))) {
|
|
59
|
+
const i = Z(n.condition), u = _(r), o = (a) => {
|
|
60
|
+
const c = /* @__PURE__ */ new Map([[e.table, e.ref]]), f = /* @__PURE__ */ new Map(), $ = [], l = new Set(s), A = (g) => {
|
|
61
|
+
const O = f.get(g);
|
|
62
|
+
return O ? [...A(O.ref), O.tie] : [];
|
|
63
|
+
}, D = (g, O, j) => {
|
|
64
|
+
if (O === g.length) return d(A(c.get(j)));
|
|
65
|
+
const y = g[O], M = wt(t, [...c.keys()], y);
|
|
66
|
+
if (!M) return !1;
|
|
67
|
+
const Q = c.get(Z(M.on).find((T) => T !== y)), V = (T) => B(M.on, new Map([...c, [y, T]])), J = i.includes(y), z = (T, I) => {
|
|
68
|
+
c.set(y, T), f.set(T, { ref: Q, tie: I });
|
|
69
|
+
}, Ct = (T, I) => (z(T, I), d(A(T)) && D(g, O + 1, j) ? !0 : (c.delete(y), f.delete(T), !1));
|
|
70
|
+
if (u.some((T) => {
|
|
71
|
+
if (T.table !== y) return !1;
|
|
72
|
+
const I = T.alias ?? y, P = kt(r, V(I), Q, I, J);
|
|
73
|
+
return (P.forward || P.back) && Ct(I, P);
|
|
74
|
+
})) return !0;
|
|
75
|
+
if (!a) return !1;
|
|
76
|
+
let p = y;
|
|
77
|
+
for (let T = 2; l.has(p); T += 1) p = `${y}_${T}`;
|
|
78
|
+
return l.add(p), z(p, { back: !0, forward: M.joinType === "inner" || J }), $.push({
|
|
79
|
+
id: Ht(),
|
|
80
|
+
table: y,
|
|
81
|
+
...p !== y ? { alias: p } : {},
|
|
82
|
+
joinType: M.joinType,
|
|
83
|
+
on: V(p)
|
|
84
|
+
}), D(g, O + 1, j);
|
|
85
|
+
};
|
|
86
|
+
let R;
|
|
87
|
+
if (i.every((g) => {
|
|
88
|
+
if (R || c.has(g)) return !0;
|
|
89
|
+
const O = Lt(t, [...c.keys()], g);
|
|
90
|
+
return O ? D(O, 0, g) : (R = g, !0);
|
|
91
|
+
}))
|
|
92
|
+
return l.forEach((g) => s.add(g)), { joins: $, condition: B(n.condition, c), ...R ? { missing: R } : {} };
|
|
93
|
+
};
|
|
94
|
+
return o(!1) ?? o(!0);
|
|
95
|
+
}
|
|
96
|
+
function lt(t, r) {
|
|
97
|
+
return ft(t).some((e) => F(e, r));
|
|
98
|
+
}
|
|
99
|
+
function Xt(t, r) {
|
|
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
|
+
}
|
|
102
|
+
function tt(t, r, e, n, s) {
|
|
103
|
+
return ct(r, e).reduce((i, u) => {
|
|
104
|
+
const o = $t(t, i, u, e, s);
|
|
105
|
+
if (o.missing) throw new Gt(n, u.table, o.missing, u.alias);
|
|
106
|
+
const a = lt(i.where, o.condition);
|
|
107
|
+
return o.joins.length === 0 && a ? i : {
|
|
108
|
+
...i,
|
|
109
|
+
joins: [...i.joins, ...o.joins],
|
|
110
|
+
where: a ? i.where : Xt(i.where, o.condition)
|
|
111
|
+
};
|
|
112
|
+
}, r);
|
|
113
|
+
}
|
|
114
|
+
function rt(t) {
|
|
115
|
+
const r = /* @__PURE__ */ new Map(), e = (n, s, i, u) => {
|
|
116
|
+
if (Array.isArray(n)) {
|
|
117
|
+
n.forEach((a, c) => e(a, [...s, c], void 0, u));
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (!Y(n)) return;
|
|
121
|
+
let o = u;
|
|
122
|
+
ut(n) && (o = i ?? /* @__PURE__ */ new Set(), r.set(JSON.stringify(s), o), at(n).forEach((a) => o.add(a))), Object.entries(n).forEach(([a, c]) => e(
|
|
123
|
+
c,
|
|
124
|
+
[...s, a],
|
|
125
|
+
n.type === "exists" && a === "query" ? o : void 0,
|
|
126
|
+
o
|
|
127
|
+
));
|
|
128
|
+
};
|
|
129
|
+
return e(t, [], void 0, /* @__PURE__ */ new Set()), r;
|
|
130
|
+
}
|
|
131
|
+
function Ar(t, r, e) {
|
|
132
|
+
const n = tt(
|
|
133
|
+
t,
|
|
134
|
+
r,
|
|
135
|
+
e,
|
|
136
|
+
[],
|
|
137
|
+
rt(r).get("[]")
|
|
138
|
+
), s = K(t, r, n), i = rt(s), u = U(s, [], (o, a) => a.length === 0 ? o : tt(t, o, e, a, i.get(JSON.stringify(a))));
|
|
139
|
+
return K(t, s, u);
|
|
140
|
+
}
|
|
141
|
+
function mr(t, r, e) {
|
|
142
|
+
if (!Y(t) || !Array.isArray(t.tables))
|
|
143
|
+
throw new TypeError("unscopedBodies takes (schema, query, rule) since 0.4.0");
|
|
144
|
+
const n = [];
|
|
145
|
+
return U(r, [], (s, i) => (ct(s, e).forEach((u) => {
|
|
146
|
+
const o = $t(t, s, u, e);
|
|
147
|
+
(o.missing || o.joins.length > 0 || !lt(s.where, o.condition)) && n.push({ path: i, table: u.table, ...u.alias ? { alias: u.alias } : {} });
|
|
148
|
+
}), s)), n;
|
|
149
|
+
}
|
|
150
|
+
function G(t, r) {
|
|
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: i, alias: u }) => {
|
|
153
|
+
if (i === void 0) {
|
|
154
|
+
u && s.add(u);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (e.set(i, i), u) {
|
|
158
|
+
e.set(u, i), n.add(u), s.delete(u);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
n.delete(i), s.delete(i);
|
|
162
|
+
}), { tableOf: e, aliases: n, derived: s };
|
|
163
|
+
}
|
|
164
|
+
function Wt(t, r, e) {
|
|
165
|
+
const n = t.on && L(t.on, r, e);
|
|
166
|
+
return t.query ? { ...t, on: n, query: Ft(t.query, (s) => H(s, e)) } : t.shared !== void 0 ? { ...t, on: n } : { ...t, on: n, table: e(t.table) };
|
|
167
|
+
}
|
|
168
|
+
function L(t, r, e) {
|
|
169
|
+
if (t.type === "field") {
|
|
170
|
+
if (!t.table || r.derived.has(t.table)) return t;
|
|
171
|
+
const n = r.tableOf.get(t.table) ?? t.table;
|
|
172
|
+
return {
|
|
173
|
+
...t,
|
|
174
|
+
table: r.aliases.has(t.table) ? t.table : e(n),
|
|
175
|
+
...t.column ? { column: e(n, t.column) } : {}
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
if (t.type === "subquery") return { ...t, query: H(t.query, e) };
|
|
179
|
+
if (t.type === "exists") {
|
|
180
|
+
const { query: n } = t, s = [n.from, ...n.joins], i = G(s), u = G(s, r);
|
|
181
|
+
return {
|
|
182
|
+
...t,
|
|
183
|
+
link: L(t.link, u, e),
|
|
184
|
+
query: {
|
|
185
|
+
...n,
|
|
186
|
+
from: { ...n.from, table: e(n.from.table) },
|
|
187
|
+
joins: n.joins.map((o) => Wt(o, i, e)),
|
|
188
|
+
...n.where ? { where: L(n.where, i, e) } : {}
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
return X(t, (n) => L(n, r, e));
|
|
193
|
+
}
|
|
194
|
+
function H(t, r) {
|
|
195
|
+
const e = G([t.from, ...t.joins]), n = st(
|
|
196
|
+
t,
|
|
197
|
+
(s) => L(s, e, r),
|
|
198
|
+
(s) => H(s, r)
|
|
199
|
+
);
|
|
200
|
+
return {
|
|
201
|
+
...n,
|
|
202
|
+
from: n.from.table === void 0 ? n.from : { ...n.from, table: r(n.from.table) },
|
|
203
|
+
joins: n.joins.map((s) => s.table === void 0 ? s : { ...s, table: r(s.table) })
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
function x(t, r) {
|
|
207
|
+
if (Array.isArray(t)) return t.map((s) => x(s, r));
|
|
208
|
+
if (typeof t != "object" || t === null || t instanceof Date) return t;
|
|
209
|
+
const e = Object.fromEntries(Object.entries(t).map(
|
|
210
|
+
([s, i]) => [s, x(i, r)]
|
|
211
|
+
));
|
|
212
|
+
if (!("from" in e) || !Array.isArray(e.joins)) return e;
|
|
213
|
+
const n = (s) => s.table !== void 0 && !s.alias && r(s.table) !== s.table ? { ...s, alias: s.table } : s;
|
|
214
|
+
return {
|
|
215
|
+
...e,
|
|
216
|
+
from: n(e.from),
|
|
217
|
+
joins: e.joins.map(n)
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
function Qt(t, r) {
|
|
221
|
+
return H(x(t, r), r);
|
|
222
|
+
}
|
|
223
|
+
const Vt = /^(\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 Et(t, r) {
|
|
225
|
+
let e;
|
|
226
|
+
const n = typeof t == "string" ? Vt.exec(t.trim()) : null;
|
|
227
|
+
if (n)
|
|
228
|
+
e = { HH: n[1], mm: n[2], ss: n[3] ?? "00" };
|
|
229
|
+
else {
|
|
230
|
+
const u = typeof t == "string" ? jt(t, "dateTime") : t;
|
|
231
|
+
u instanceof Date && (e = {
|
|
232
|
+
YYYY: b(u.getUTCFullYear(), 4),
|
|
233
|
+
MM: b(u.getUTCMonth() + 1),
|
|
234
|
+
DD: b(u.getUTCDate()),
|
|
235
|
+
HH: b(u.getUTCHours()),
|
|
236
|
+
mm: b(u.getUTCMinutes()),
|
|
237
|
+
ss: b(u.getUTCSeconds())
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
if (!e) return t;
|
|
241
|
+
const s = e;
|
|
242
|
+
return (r.match(et) ?? []).some((u) => s[u] === void 0) ? t : r.replace(et, (u) => s[u]);
|
|
243
|
+
}
|
|
244
|
+
function Tt(t, r) {
|
|
245
|
+
const e = /* @__PURE__ */ new Map(), n = (s) => {
|
|
246
|
+
const i = /* @__PURE__ */ new Map();
|
|
247
|
+
_(s).forEach((c) => i.set(Ut(c), c.table));
|
|
248
|
+
const u = (c) => {
|
|
249
|
+
if ((c == null ? void 0 : c.type) !== "field" || !c.table || !c.column) return;
|
|
250
|
+
const f = i.has(c.table) ? i.get(c.table) : c.table, $ = f === void 0 ? void 0 : qt(r, f), l = $ == null ? void 0 : $.columns.find((A) => A.name === c.column);
|
|
251
|
+
return l == null ? void 0 : l.format;
|
|
252
|
+
}, o = (c, f) => {
|
|
253
|
+
switch (c.type) {
|
|
254
|
+
case "literal":
|
|
255
|
+
return { ...c, value: Et(c.value, f) };
|
|
256
|
+
case "list":
|
|
257
|
+
return { ...c, items: c.items.map(($) => o($, f)) };
|
|
258
|
+
case "param":
|
|
259
|
+
return e.has(c.name) || e.set(c.name, f), c;
|
|
260
|
+
default:
|
|
261
|
+
return c;
|
|
262
|
+
}
|
|
263
|
+
}, a = (c) => {
|
|
264
|
+
if (c.type === "subquery") return { ...c, query: n(c.query) };
|
|
265
|
+
const f = X(c, a);
|
|
266
|
+
if (f.type !== "binary" || !f.right) return f;
|
|
267
|
+
const $ = u(f.left), l = u(f.right);
|
|
268
|
+
return {
|
|
269
|
+
...f,
|
|
270
|
+
left: l ? o(f.left, l) : f.left,
|
|
271
|
+
right: $ ? o(f.right, $) : f.right
|
|
272
|
+
};
|
|
273
|
+
};
|
|
274
|
+
return st(s, a, (c) => n(c));
|
|
275
|
+
};
|
|
276
|
+
return { query: n(t), params: e };
|
|
277
|
+
}
|
|
278
|
+
const gr = ["mysql", "mssql", "postgres"], Dr = {
|
|
279
|
+
mysql: "MySQL",
|
|
280
|
+
mssql: "SQL Server",
|
|
281
|
+
postgres: "PostgreSQL"
|
|
282
|
+
};
|
|
283
|
+
function S(t, r) {
|
|
284
|
+
const e = t.replace(/[\uE000-\uE003]/g, "");
|
|
285
|
+
switch (r) {
|
|
286
|
+
case "mysql":
|
|
287
|
+
return `\`${e.replace(/`/g, "``")}\``;
|
|
288
|
+
case "mssql":
|
|
289
|
+
return `[${e.replace(/]/g, "]]")}]`;
|
|
290
|
+
case "postgres":
|
|
291
|
+
return `"${e.replace(/"/g, '""')}"`;
|
|
292
|
+
default:
|
|
293
|
+
return `"${e.replace(/"/g, '""')}"`;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
const Jt = /^[A-Za-z_][A-Za-z0-9_]*$/, At = (t) => `${t}`, zt = /\uE000(\d+)\uE000/g;
|
|
297
|
+
let C;
|
|
298
|
+
function yr(t, r) {
|
|
299
|
+
C = { ...t, writing: [] };
|
|
300
|
+
try {
|
|
301
|
+
return r();
|
|
302
|
+
} finally {
|
|
303
|
+
C = void 0;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
function mt(t, r, e) {
|
|
307
|
+
if (typeof t != "object" || t === null || t instanceof Date) return;
|
|
308
|
+
let n;
|
|
309
|
+
return Object.entries(t).some(([s, i]) => {
|
|
310
|
+
const u = [...e, Array.isArray(t) ? Number(s) : s];
|
|
311
|
+
return n = i === r ? u : mt(i, r, u), n !== void 0;
|
|
312
|
+
}), n;
|
|
313
|
+
}
|
|
314
|
+
function m(t, r) {
|
|
315
|
+
if (!C) return r();
|
|
316
|
+
const { writing: e, paths: n } = C, s = e[e.length - 1], i = typeof t == "string" ? [t] : mt(s.value, t, s.path);
|
|
317
|
+
if (!i) return r();
|
|
318
|
+
e.push({ value: t, path: i });
|
|
319
|
+
try {
|
|
320
|
+
return `${n.push(i) - 1}${r()}`;
|
|
321
|
+
} finally {
|
|
322
|
+
e.pop();
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
const Kt = {
|
|
326
|
+
mysql: {
|
|
327
|
+
text: "CHAR",
|
|
328
|
+
integer: "SIGNED",
|
|
329
|
+
number: "DECIMAL(38, 10)",
|
|
330
|
+
date: "DATE",
|
|
331
|
+
dateTime: "DATETIME",
|
|
332
|
+
time: "TIME",
|
|
333
|
+
boolean: "SIGNED"
|
|
334
|
+
},
|
|
335
|
+
mssql: {
|
|
336
|
+
text: "NVARCHAR(MAX)",
|
|
337
|
+
integer: "BIGINT",
|
|
338
|
+
number: "DECIMAL(38, 10)",
|
|
339
|
+
date: "DATE",
|
|
340
|
+
dateTime: "DATETIME2",
|
|
341
|
+
time: "TIME",
|
|
342
|
+
boolean: "BIT"
|
|
343
|
+
},
|
|
344
|
+
postgres: {
|
|
345
|
+
text: "text",
|
|
346
|
+
integer: "bigint",
|
|
347
|
+
number: "numeric",
|
|
348
|
+
date: "date",
|
|
349
|
+
dateTime: "timestamp",
|
|
350
|
+
time: "time",
|
|
351
|
+
boolean: "boolean"
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
function Zt(t, r) {
|
|
355
|
+
var n;
|
|
356
|
+
const e = (n = Kt[r]) == null ? void 0 : n[t];
|
|
357
|
+
return e ? r === "postgres" ? `NULL::${e}` : `CAST(NULL AS ${e})` : "NULL";
|
|
358
|
+
}
|
|
359
|
+
function gt(t, r, e) {
|
|
360
|
+
if (t == null) return "NULL";
|
|
361
|
+
if (e)
|
|
362
|
+
return e.values.push(t), At(e.values.length - 1);
|
|
363
|
+
if (typeof t == "boolean")
|
|
364
|
+
return r === "mssql" ? t ? "1" : "0" : t ? "TRUE" : "FALSE";
|
|
365
|
+
if (typeof t == "string") {
|
|
366
|
+
const n = C ? t.replace(/[\uE001-\uE003]/g, "") : t;
|
|
367
|
+
return n === "" ? "''" : `'${n.replace(/'/g, "''")}'`;
|
|
368
|
+
}
|
|
369
|
+
return typeof t == "number" || typeof t == "bigint" ? String(t) : t instanceof Date ? `'${it(t)}'` : String(t);
|
|
370
|
+
}
|
|
371
|
+
let q = "native";
|
|
372
|
+
function Dt(t, r) {
|
|
373
|
+
q = t ?? "native";
|
|
374
|
+
try {
|
|
375
|
+
return r();
|
|
376
|
+
} finally {
|
|
377
|
+
q = "native";
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
function dt(t, r) {
|
|
381
|
+
switch (t) {
|
|
382
|
+
case "!=":
|
|
383
|
+
return r === "mssql" ? "<>" : "!=";
|
|
384
|
+
case "IS NOT DISTINCT FROM":
|
|
385
|
+
return r === "mysql" ? "<=>" : t;
|
|
386
|
+
case "REGEXP":
|
|
387
|
+
if (r === "postgres") return "~";
|
|
388
|
+
if (r === "mssql") throw new Error("REGEXP isn't available in SQL Server");
|
|
389
|
+
return "REGEXP";
|
|
390
|
+
default:
|
|
391
|
+
if (!Bt.includes(t))
|
|
392
|
+
throw new Error(`unknown operator ${JSON.stringify(t)}`);
|
|
393
|
+
return t;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
function h(t) {
|
|
397
|
+
return (t == null ? void 0 : t.type) === "literal" && typeof t.value == "string" && /^[A-Za-z_]+$/.test(t.value) ? t.value.toUpperCase() : "DAY";
|
|
398
|
+
}
|
|
399
|
+
const v = {
|
|
400
|
+
YEAR: "year",
|
|
401
|
+
QUARTER: "quarter",
|
|
402
|
+
MONTH: "month",
|
|
403
|
+
WEEK: "week",
|
|
404
|
+
DAY: "day",
|
|
405
|
+
HOUR: "hour",
|
|
406
|
+
MINUTE: "minute",
|
|
407
|
+
SECOND: "second",
|
|
408
|
+
MILLISECOND: "millisecond"
|
|
409
|
+
}, tr = {
|
|
410
|
+
// ---- Date arithmetic ----
|
|
411
|
+
DATE_ADD: ([t, r], e, n, s) => {
|
|
412
|
+
var u;
|
|
413
|
+
const i = h(e[2]);
|
|
414
|
+
switch (n) {
|
|
415
|
+
case "mysql":
|
|
416
|
+
return i === "MILLISECOND" ? `DATE_ADD(${t}, INTERVAL (${r}) * 1000 MICROSECOND)` : `DATE_ADD(${t}, INTERVAL ${r} ${i})`;
|
|
417
|
+
case "postgres": {
|
|
418
|
+
const o = v[i] ?? i.toLowerCase();
|
|
419
|
+
return !s && ((u = e[1]) == null ? void 0 : u.type) === "literal" && typeof e[1].value == "number" ? `(${t} + INTERVAL '${e[1].value} ${o}')` : `(${t} + (${r} * INTERVAL '1 ${o}'))`;
|
|
420
|
+
}
|
|
421
|
+
case "mssql":
|
|
422
|
+
return `DATEADD(${i}, ${r}, ${t})`;
|
|
423
|
+
default:
|
|
424
|
+
return `DATE_ADD(${t}, ${r}, ${h(e[2])})`;
|
|
425
|
+
}
|
|
426
|
+
},
|
|
427
|
+
DATE_SUB: ([t, r], e, n, s) => {
|
|
428
|
+
var u;
|
|
429
|
+
const i = h(e[2]);
|
|
430
|
+
switch (n) {
|
|
431
|
+
case "mysql":
|
|
432
|
+
return i === "MILLISECOND" ? `DATE_SUB(${t}, INTERVAL (${r}) * 1000 MICROSECOND)` : `DATE_SUB(${t}, INTERVAL ${r} ${i})`;
|
|
433
|
+
case "postgres": {
|
|
434
|
+
const o = v[i] ?? i.toLowerCase();
|
|
435
|
+
return !s && ((u = e[1]) == null ? void 0 : u.type) === "literal" && typeof e[1].value == "number" ? `(${t} - INTERVAL '${e[1].value} ${o}')` : `(${t} - (${r} * INTERVAL '1 ${o}'))`;
|
|
436
|
+
}
|
|
437
|
+
case "mssql":
|
|
438
|
+
return `DATEADD(${i}, -(${r}), ${t})`;
|
|
439
|
+
default:
|
|
440
|
+
return `DATE_SUB(${t}, ${r}, ${h(e[2])})`;
|
|
441
|
+
}
|
|
442
|
+
},
|
|
443
|
+
// ---- Date diff ----
|
|
444
|
+
DATEDIFF: ([t, r], e, n) => {
|
|
445
|
+
const s = h(e[2]);
|
|
446
|
+
switch (n) {
|
|
447
|
+
case "mysql":
|
|
448
|
+
return `TIMESTAMPDIFF(${s}, ${t}, ${r})`;
|
|
449
|
+
case "mssql":
|
|
450
|
+
return `DATEDIFF(${s}, ${t}, ${r})`;
|
|
451
|
+
case "postgres":
|
|
452
|
+
switch (s) {
|
|
453
|
+
case "YEAR":
|
|
454
|
+
return `EXTRACT(YEAR FROM age(${r}, ${t}))::integer`;
|
|
455
|
+
case "QUARTER": {
|
|
456
|
+
const i = `EXTRACT(YEAR FROM age(${r}, ${t}))`, u = `EXTRACT(MONTH FROM age(${r}, ${t}))`;
|
|
457
|
+
return `FLOOR((${i} * 12 + ${u}) / 3)::integer`;
|
|
458
|
+
}
|
|
459
|
+
case "MONTH": {
|
|
460
|
+
const i = `EXTRACT(YEAR FROM age(${r}, ${t}))`, u = `EXTRACT(MONTH FROM age(${r}, ${t}))`;
|
|
461
|
+
return `(${i} * 12 + ${u})::integer`;
|
|
462
|
+
}
|
|
463
|
+
case "WEEK":
|
|
464
|
+
return `FLOOR((${r}::date - ${t}::date) / 7)`;
|
|
465
|
+
case "DAY":
|
|
466
|
+
return `(${r}::date - ${t}::date)`;
|
|
467
|
+
case "HOUR":
|
|
468
|
+
return `FLOOR(EXTRACT(EPOCH FROM (${r} - ${t})) / 3600)::integer`;
|
|
469
|
+
case "MINUTE":
|
|
470
|
+
return `FLOOR(EXTRACT(EPOCH FROM (${r} - ${t})) / 60)::integer`;
|
|
471
|
+
case "SECOND":
|
|
472
|
+
return `FLOOR(EXTRACT(EPOCH FROM (${r} - ${t})))::integer`;
|
|
473
|
+
case "MILLISECOND":
|
|
474
|
+
return `FLOOR(EXTRACT(EPOCH FROM (${r} - ${t})) * 1000)::integer`;
|
|
475
|
+
default:
|
|
476
|
+
return `/* unknown unit: ${s} */`;
|
|
477
|
+
}
|
|
478
|
+
default:
|
|
479
|
+
return `DATEDIFF(${t}, ${r}, ${s})`;
|
|
480
|
+
}
|
|
481
|
+
},
|
|
482
|
+
// ---- Date extract ----
|
|
483
|
+
EXTRACT: ([t], r, e) => {
|
|
484
|
+
const n = h(r[1]);
|
|
485
|
+
switch (e) {
|
|
486
|
+
case "mysql":
|
|
487
|
+
return n === "MILLISECOND" ? `FLOOR(MICROSECOND(${t}) / 1000)` : `EXTRACT(${n} FROM ${t})`;
|
|
488
|
+
case "postgres":
|
|
489
|
+
return n === "MILLISECOND" ? `EXTRACT(MILLISECONDS FROM ${t})::integer` : `EXTRACT(${n} FROM ${t})`;
|
|
490
|
+
case "mssql":
|
|
491
|
+
return `DATEPART(${n}, ${t})`;
|
|
492
|
+
default:
|
|
493
|
+
return `EXTRACT(${t}, ${n})`;
|
|
494
|
+
}
|
|
495
|
+
},
|
|
496
|
+
// ---- Date truncation ----
|
|
497
|
+
DATE_TRUNC: ([t], r, e) => {
|
|
498
|
+
const n = h(r[1]);
|
|
499
|
+
switch (e) {
|
|
500
|
+
case "mysql":
|
|
501
|
+
switch (n) {
|
|
502
|
+
case "YEAR":
|
|
503
|
+
return `DATE_FORMAT(${t}, '%Y-01-01')`;
|
|
504
|
+
case "QUARTER":
|
|
505
|
+
return `MAKEDATE(YEAR(${t}), 1) + INTERVAL (QUARTER(${t}) - 1) QUARTER`;
|
|
506
|
+
case "MONTH":
|
|
507
|
+
return `DATE_FORMAT(${t}, '%Y-%m-01')`;
|
|
508
|
+
case "WEEK":
|
|
509
|
+
return `DATE_SUB(DATE(${t}), INTERVAL WEEKDAY(${t}) DAY)`;
|
|
510
|
+
case "DAY":
|
|
511
|
+
return `DATE(${t})`;
|
|
512
|
+
case "HOUR":
|
|
513
|
+
return `DATE_FORMAT(${t}, '%Y-%m-%d %H:00:00')`;
|
|
514
|
+
case "MINUTE":
|
|
515
|
+
return `DATE_FORMAT(${t}, '%Y-%m-%d %H:%i:00')`;
|
|
516
|
+
case "SECOND":
|
|
517
|
+
return `DATE_FORMAT(${t}, '%Y-%m-%d %H:%i:%s')`;
|
|
518
|
+
case "MILLISECOND":
|
|
519
|
+
return `DATE_FORMAT(${t}, '%Y-%m-%d %H:%i:%s.000')`;
|
|
520
|
+
default:
|
|
521
|
+
return `/* unknown unit: ${n} */`;
|
|
522
|
+
}
|
|
523
|
+
case "postgres":
|
|
524
|
+
return `DATE_TRUNC('${v[n] ?? n.toLowerCase()}', ${t})`;
|
|
525
|
+
case "mssql":
|
|
526
|
+
switch (n) {
|
|
527
|
+
case "YEAR":
|
|
528
|
+
return `DATEFROMPARTS(YEAR(${t}), 1, 1)`;
|
|
529
|
+
case "QUARTER":
|
|
530
|
+
return `DATEFROMPARTS(YEAR(${t}), (DATEPART(QUARTER, ${t}) - 1) * 3 + 1, 1)`;
|
|
531
|
+
case "MONTH":
|
|
532
|
+
return `DATEFROMPARTS(YEAR(${t}), MONTH(${t}), 1)`;
|
|
533
|
+
case "WEEK": {
|
|
534
|
+
const s = `CAST(${t} AS DATE)`;
|
|
535
|
+
return `DATEADD(DAY, -(DATEDIFF(DAY, 0, ${s}) % 7), ${s})`;
|
|
536
|
+
}
|
|
537
|
+
case "DAY":
|
|
538
|
+
return `CAST(${t} AS DATE)`;
|
|
539
|
+
case "HOUR":
|
|
540
|
+
return `DATEADD(HOUR, DATEDIFF(HOUR, 0, ${t}), 0)`;
|
|
541
|
+
case "MINUTE":
|
|
542
|
+
return `DATEADD(MINUTE, DATEDIFF(MINUTE, 0, ${t}), 0)`;
|
|
543
|
+
case "SECOND":
|
|
544
|
+
return `DATEADD(SECOND, DATEDIFF(SECOND, '2000-01-01', ${t}), '2000-01-01')`;
|
|
545
|
+
case "MILLISECOND": {
|
|
546
|
+
const s = `CAST(${t} AS DATE)`;
|
|
547
|
+
return `DATEADD(MILLISECOND, DATEDIFF(MILLISECOND, ${s}, ${t}), ${s})`;
|
|
548
|
+
}
|
|
549
|
+
default:
|
|
550
|
+
return `/* unknown unit: ${n} */`;
|
|
551
|
+
}
|
|
552
|
+
default:
|
|
553
|
+
return `DATE_TRUNC(${t}, ${n})`;
|
|
554
|
+
}
|
|
555
|
+
},
|
|
556
|
+
// ---- Type cast ----
|
|
557
|
+
/**
|
|
558
|
+
* CAST(expr, type_name) — type_name is stored as a string literal.
|
|
559
|
+
* Renders as CAST(expr AS type_name) for all dialects.
|
|
560
|
+
*/
|
|
561
|
+
CAST: ([t], r) => {
|
|
562
|
+
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
|
+
return `CAST(${t} AS ${n})`;
|
|
564
|
+
},
|
|
565
|
+
// ---- String aggregation (issue 137) ----
|
|
566
|
+
// The separator goes into the SQL as a literal even in parameterized SQL:
|
|
567
|
+
// MySQL's SEPARATOR takes nothing else.
|
|
568
|
+
STRING_AGG: ([t = "??"], r, e, n, s, i) => {
|
|
569
|
+
const u = r[1] ?? { type: "literal", value: ", " }, o = u.type === "literal" && typeof u.value == "string" ? gt(u.value, e) : "??", a = s.distinct ? "DISTINCT " : "", c = (l) => {
|
|
570
|
+
var A;
|
|
571
|
+
return (A = s.orderBy) != null && A.length ? `ORDER BY ${s.orderBy.map((D) => `${l(D)} ${D.direction === "desc" ? "DESC" : "ASC"}`).join(", ")}` : "";
|
|
572
|
+
}, f = (l) => i(l.expression), $ = (l) => l ? ` ${l}` : "";
|
|
573
|
+
switch (e) {
|
|
574
|
+
case "mysql":
|
|
575
|
+
return `GROUP_CONCAT(${a}${t}${$(c(f))} SEPARATOR ${o})`;
|
|
576
|
+
case "postgres": {
|
|
577
|
+
const l = `CAST(${t} AS TEXT)`, A = c((D) => {
|
|
578
|
+
if (!s.distinct) return f(D);
|
|
579
|
+
if (F(D.expression, r[0])) return l;
|
|
580
|
+
throw new Error("PostgreSQL orders STRING_AGG of different values by the value only");
|
|
581
|
+
});
|
|
582
|
+
return `STRING_AGG(${a}${l}, ${o}${$(A)})`;
|
|
583
|
+
}
|
|
584
|
+
case "mssql": {
|
|
585
|
+
if (s.distinct)
|
|
586
|
+
throw new Error("SQL Server's STRING_AGG can't join only different values");
|
|
587
|
+
const l = c(f), A = l ? ` WITHIN GROUP (${l})` : "";
|
|
588
|
+
return `STRING_AGG(CAST(${t} AS NVARCHAR(MAX)), ${o})${A}`;
|
|
589
|
+
}
|
|
590
|
+
default:
|
|
591
|
+
return `STRING_AGG(${a}${t}, ${o}${$(c(f))})`;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
};
|
|
595
|
+
function N(t) {
|
|
596
|
+
return t.type === "binary" || t.type === "logical" || t.type === "if" || t.type === "case";
|
|
597
|
+
}
|
|
598
|
+
function E(t, r, e, n) {
|
|
599
|
+
return m(t, () => rr(t, r, e, n));
|
|
600
|
+
}
|
|
601
|
+
function nt(t, r, e, n) {
|
|
602
|
+
const s = (i) => {
|
|
603
|
+
if (!i) return "??";
|
|
604
|
+
const u = E(i, r, e, n);
|
|
605
|
+
return N(i) ? `(${u})` : u;
|
|
606
|
+
};
|
|
607
|
+
return m(t, () => {
|
|
608
|
+
const [i, u] = t.type === "list" ? t.items : [t];
|
|
609
|
+
return `${s(i)} AND ${s(u)}`;
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
function rr(t, r, e, n) {
|
|
613
|
+
var u;
|
|
614
|
+
const s = " ".repeat(e), i = " ".repeat(e + 1);
|
|
615
|
+
switch (t.type) {
|
|
616
|
+
case "literal":
|
|
617
|
+
return t.value === "" && !t.empty ? "??" : t.value === null && t.as ? Zt(t.as, r) : gt(t.value, r, n);
|
|
618
|
+
case "param": {
|
|
619
|
+
if (!n) return Jt.test(t.name) ? `:${t.name}` : "??";
|
|
620
|
+
if (!n.named.has(t.name))
|
|
621
|
+
throw new Error(`no value for parameter "${t.name}"`);
|
|
622
|
+
return n.values.push(n.named.get(t.name)), At(n.values.length - 1);
|
|
623
|
+
}
|
|
624
|
+
case "field": {
|
|
625
|
+
if (!t.column) return "??";
|
|
626
|
+
const o = S(t.column, r);
|
|
627
|
+
return t.table ? `${S(t.table, r)}.${o}` : o;
|
|
628
|
+
}
|
|
629
|
+
case "function": {
|
|
630
|
+
const o = t.args.map(($) => E($, r, e, n)), a = tr[t.name.toUpperCase()];
|
|
631
|
+
if (a)
|
|
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(", ")})`;
|
|
644
|
+
}
|
|
645
|
+
case "binary": {
|
|
646
|
+
const o = t.operator ? dt(t.operator, r) : "=", a = E(t.left, r, e, n), c = N(t.left) ? `(${a})` : a;
|
|
647
|
+
if (vt(t.operator)) return `${c} ${o}`;
|
|
648
|
+
if (!t.right) return `${c} ${o} ??`;
|
|
649
|
+
if (o === "BETWEEN")
|
|
650
|
+
return `${c} BETWEEN ${nt(t.right, r, e, n)}`;
|
|
651
|
+
if (o === "IN")
|
|
652
|
+
return t.right.type === "list" ? `${c} IN ${E(t.right, r, e, n)}` : `${c} IN (${E(t.right, r, e, n)})`;
|
|
653
|
+
const f = E(t.right, r, e, n), $ = N(t.right) ? `(${f})` : f;
|
|
654
|
+
if (t.operator === "IS NOT DISTINCT FROM" && q === "expanded") {
|
|
655
|
+
const l = (D) => {
|
|
656
|
+
const R = E(D, r, e, n);
|
|
657
|
+
return N(D) ? `(${R})` : R;
|
|
658
|
+
}, A = `${l(t.left)} IS NULL AND ${l(t.right)} IS NULL`;
|
|
659
|
+
return `(${c} = ${$} OR (${A}))`;
|
|
660
|
+
}
|
|
661
|
+
return `${c} ${o} ${$}`;
|
|
662
|
+
}
|
|
663
|
+
case "logical": {
|
|
664
|
+
if (t.operands.length === 0) return "/* empty */";
|
|
665
|
+
if (t.operands.length === 1)
|
|
666
|
+
return E(t.operands[0], r, e, n);
|
|
667
|
+
if (t.operator !== "AND" && t.operator !== "OR")
|
|
668
|
+
throw new Error(`unknown operator ${JSON.stringify(t.operator)}`);
|
|
669
|
+
const o = `
|
|
670
|
+
${s}${t.operator} `;
|
|
671
|
+
return t.operands.map((a) => {
|
|
672
|
+
if (a.type === "logical") {
|
|
673
|
+
const c = E(a, r, e + 1, n);
|
|
674
|
+
return `(
|
|
675
|
+
${i}${c}
|
|
676
|
+
${s})`;
|
|
677
|
+
}
|
|
678
|
+
return E(a, r, e, n);
|
|
679
|
+
}).join(o);
|
|
680
|
+
}
|
|
681
|
+
case "not": {
|
|
682
|
+
const { operand: o } = t;
|
|
683
|
+
if (o.type === "binary" && o.operator === "IN" && ((u = o.right) == null ? void 0 : u.type) === "subquery") {
|
|
684
|
+
const a = E(o.left, r, e, n);
|
|
685
|
+
return `${N(o.left) ? `(${a})` : a} NOT IN (${E(o.right, r, e, n)})`;
|
|
686
|
+
}
|
|
687
|
+
if (o.type === "binary" && o.operator === "BETWEEN" && o.right) {
|
|
688
|
+
const a = E(o.left, r, e, n);
|
|
689
|
+
return `${N(o.left) ? `(${a})` : a} NOT BETWEEN ${nt(o.right, r, e, n)}`;
|
|
690
|
+
}
|
|
691
|
+
if (o.type === "binary" && o.operator === "IS NOT DISTINCT FROM" && o.right && q === "native") {
|
|
692
|
+
if (r === "mysql") return `NOT (${E(o, r, e, n)})`;
|
|
693
|
+
const a = E(o.left, r, e, n), c = N(o.left) ? `(${a})` : a, f = E(o.right, r, e, n), $ = N(o.right) ? `(${f})` : f;
|
|
694
|
+
return `${c} IS DISTINCT FROM ${$}`;
|
|
695
|
+
}
|
|
696
|
+
if (o.type === "logical" || o.type === "not") {
|
|
697
|
+
const a = E(o, r, e + 1, n);
|
|
698
|
+
return `NOT (
|
|
699
|
+
${i}${a}
|
|
700
|
+
${s})`;
|
|
701
|
+
}
|
|
702
|
+
return `NOT ${E(o, r, e, n)}`;
|
|
703
|
+
}
|
|
704
|
+
case "if": {
|
|
705
|
+
const o = t.condition ? E(t.condition, r, e, n) : "??", a = t.then ? E(t.then, r, e, n) : "??", c = t.else ? E(t.else, r, e, n) : "NULL";
|
|
706
|
+
return r === "mysql" ? `IF(${o}, ${a}, ${c})` : r === "mssql" ? `IIF(${o}, ${a}, ${c})` : `CASE WHEN ${o} THEN ${a} ELSE ${c} END`;
|
|
707
|
+
}
|
|
708
|
+
case "case": {
|
|
709
|
+
const o = t.subject ? ` ${E(t.subject, r, e, n)}` : "", a = t.branches.map((f) => {
|
|
710
|
+
const $ = E(f.when, r, e + 1, n), l = E(f.then, r, e + 1, n);
|
|
711
|
+
return `${i}WHEN ${$} THEN ${l}`;
|
|
712
|
+
}).join(`
|
|
713
|
+
`), c = t.else ? `
|
|
714
|
+
${i}ELSE ${E(t.else, r, e + 1, n)}` : "";
|
|
715
|
+
return `CASE${o}
|
|
716
|
+
${a}${c}
|
|
717
|
+
${s}END`;
|
|
718
|
+
}
|
|
719
|
+
// EXISTS (SELECT 1 FROM … WHERE <link> AND <where>): the subquery written
|
|
720
|
+
// in place, one level in, so its params fall in reading order.
|
|
721
|
+
case "exists": {
|
|
722
|
+
const { query: o, link: a } = t, c = (l) => l.type === "logical" && l.operator === "AND" ? l.operands : [l], f = o.where ? { type: "logical", operator: "AND", operands: [...c(a), ...c(o.where)] } : a;
|
|
723
|
+
return `EXISTS (
|
|
724
|
+
${[
|
|
725
|
+
"SELECT 1",
|
|
726
|
+
// The clauses below write their expressions with formatNode.
|
|
727
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
728
|
+
Rt(o.from, r),
|
|
729
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
730
|
+
...o.joins.map((l) => pt(l, r, n)),
|
|
731
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
732
|
+
yt(f, r, n)
|
|
733
|
+
].join(`
|
|
734
|
+
`).replace(/^/gm, i)}
|
|
735
|
+
${s})`;
|
|
736
|
+
}
|
|
737
|
+
// IN's right side (issue 122): its query, on lines of its own, inside the
|
|
738
|
+
// parentheses IN writes around it.
|
|
739
|
+
// An empty list is unfinished, like a value not filled in.
|
|
740
|
+
case "list":
|
|
741
|
+
return `(${t.items.map((o) => E(o, r, e, n)).join(", ") || "??"})`;
|
|
742
|
+
case "subquery":
|
|
743
|
+
return `
|
|
744
|
+
${W(t.query, r, n).join(`
|
|
745
|
+
`).replace(/^/gm, i)}
|
|
746
|
+
${s}`;
|
|
747
|
+
default:
|
|
748
|
+
return "/* unknown */";
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
function yt(t, r, e) {
|
|
752
|
+
return `WHERE
|
|
753
|
+
${E(t, r, 1, e)}`;
|
|
754
|
+
}
|
|
755
|
+
const er = {
|
|
756
|
+
inner: "INNER JOIN",
|
|
757
|
+
left: "LEFT JOIN",
|
|
758
|
+
right: "RIGHT JOIN",
|
|
759
|
+
full: "FULL JOIN",
|
|
760
|
+
cross: "CROSS JOIN"
|
|
761
|
+
};
|
|
762
|
+
let Ot = /* @__PURE__ */ new Map();
|
|
763
|
+
function Nt(t, r, e) {
|
|
764
|
+
return t.shared !== void 0 ? S(Ot.get(t.shared) ?? "??", r) : t.query ? `(
|
|
765
|
+
${St(t.query, r, e).replace(/^/gm, " ")}
|
|
766
|
+
)` : S(t.table, r);
|
|
767
|
+
}
|
|
768
|
+
function St(t, r, e) {
|
|
769
|
+
return _t(t).map((n) => W(n, r, e).join(`
|
|
770
|
+
`)).join(Yt(t) && !t.all ? `
|
|
771
|
+
UNION
|
|
772
|
+
` : `
|
|
773
|
+
UNION ALL
|
|
774
|
+
`);
|
|
775
|
+
}
|
|
776
|
+
function nr(t, r, e) {
|
|
777
|
+
return t.length === 0 ? [] : [`WITH
|
|
778
|
+
${t.map((s) => m(s, () => {
|
|
779
|
+
const i = St(s.query, r, e).replace(/^/gm, " ");
|
|
780
|
+
return ` ${S(s.name, r)} AS (
|
|
781
|
+
${i}
|
|
782
|
+
)`;
|
|
783
|
+
})).join(`,
|
|
784
|
+
`)}`];
|
|
785
|
+
}
|
|
786
|
+
function Rt(t, r, e) {
|
|
787
|
+
const n = t.alias ? ` AS ${S(t.alias, r)}` : "";
|
|
788
|
+
return `FROM ${m(t, () => `${Nt(t, r, e)}${n}`)}`;
|
|
789
|
+
}
|
|
790
|
+
function pt(t, r, e) {
|
|
791
|
+
return m(t, () => {
|
|
792
|
+
const n = Nt(t, r, e), s = t.alias ? ` AS ${S(t.alias, r)}` : "";
|
|
793
|
+
if (t.joinType === "cross") return `CROSS JOIN ${n}${s}`;
|
|
794
|
+
const i = t.on ? E(t.on, r, 1, e) : "/* unconfigured */";
|
|
795
|
+
return `${er[t.joinType]} ${n}${s}
|
|
796
|
+
ON ${i}`;
|
|
797
|
+
});
|
|
798
|
+
}
|
|
799
|
+
function sr(t, r, e, n = "SELECT") {
|
|
800
|
+
if (t.length === 0) return `${n} ${m(t, () => "*")}`;
|
|
801
|
+
const s = t.map((i) => m(i, () => {
|
|
802
|
+
const u = E(i.expression, r, 1, e), o = i.alias ? ` AS ${S(i.alias, r)}` : "";
|
|
803
|
+
return `${u}${o}`;
|
|
804
|
+
}));
|
|
805
|
+
return `${n}
|
|
806
|
+
${s.join(`,
|
|
807
|
+
`)}`;
|
|
808
|
+
}
|
|
809
|
+
function or(t, r, e) {
|
|
810
|
+
return `GROUP BY
|
|
811
|
+
${t.map((s) => m(
|
|
812
|
+
s,
|
|
813
|
+
() => E(s.expression, r, 1, e)
|
|
814
|
+
)).join(`,
|
|
815
|
+
`)}`;
|
|
816
|
+
}
|
|
817
|
+
function ir(t, r, e) {
|
|
818
|
+
return `HAVING
|
|
819
|
+
${E(t, r, 1, e)}`;
|
|
820
|
+
}
|
|
821
|
+
function ur(t, r, e, n) {
|
|
822
|
+
return `ORDER BY
|
|
823
|
+
${t.map((i) => m(i, () => `${E(n(i.expression), r, 1, e)} ${i.direction === "desc" ? "DESC" : "ASC"}`)).join(`,
|
|
824
|
+
`)}`;
|
|
825
|
+
}
|
|
826
|
+
const k = (t) => t !== void 0 && Number.isInteger(t) && t >= 0 ? t : void 0;
|
|
827
|
+
function It(t, r) {
|
|
828
|
+
const e = r.resolveName ? Qt(t, r.resolveName) : t;
|
|
829
|
+
return r.defaultLimit !== void 0 && k(e.limit) === void 0 ? { ...e, limit: r.defaultLimit } : e;
|
|
830
|
+
}
|
|
831
|
+
const cr = "18446744073709551615";
|
|
832
|
+
function W(t, r, e, n = `SELECT${t.distinct ? " DISTINCT" : ""}`) {
|
|
833
|
+
const s = [
|
|
834
|
+
sr(t.select, r, e, n),
|
|
835
|
+
Rt(t.from, r, e),
|
|
836
|
+
...t.joins.map((u) => pt(u, r, e))
|
|
837
|
+
];
|
|
838
|
+
t.where && s.push(yt(t.where, r, e));
|
|
839
|
+
const i = t.groupBy ?? [];
|
|
840
|
+
if (i.length > 0 && s.push(or(i, r, e)), t.having) {
|
|
841
|
+
const u = r === "mysql" ? ot(t.having, t) : t.having;
|
|
842
|
+
s.push(ir(u, r, e));
|
|
843
|
+
}
|
|
844
|
+
return s;
|
|
845
|
+
}
|
|
846
|
+
function ht(t, r, e) {
|
|
847
|
+
C && (C.writing = [{ value: t, path: [] }]), Ot = new Map((t.shared ?? []).map((f) => [f.id, f.name]));
|
|
848
|
+
const n = nr(t.shared ?? [], r, e), s = k(t.limit), i = k(t.offset) || void 0, u = t.orderBy ?? [], o = r === "mssql" && s !== void 0 && i === void 0, a = `SELECT${t.distinct ? " DISTINCT" : ""}${o ? ` ${m("limit", () => `TOP (${s})`)}` : ""}`, c = W(t, r, e, a);
|
|
849
|
+
if (u.length > 0 && c.push(ur(u, r, e, (f) => r === "mysql" ? ot(f, t) : f)), r === "mssql") {
|
|
850
|
+
if (i !== void 0) {
|
|
851
|
+
u.length === 0 && c.push("ORDER BY (SELECT NULL)");
|
|
852
|
+
const f = s !== void 0 ? ` ${m("limit", () => `FETCH NEXT ${s} ROWS ONLY`)}` : "";
|
|
853
|
+
c.push(`${m("offset", () => `OFFSET ${i} ROWS`)}${f}`);
|
|
854
|
+
}
|
|
855
|
+
} else if (s !== void 0 || i !== void 0) {
|
|
856
|
+
const f = s !== void 0 || r === "mysql" ? m(s !== void 0 ? "limit" : {}, () => `LIMIT ${s ?? cr}`) : "", $ = i !== void 0 ? m("offset", () => `OFFSET ${i}`) : "";
|
|
857
|
+
c.push([f, $].filter(Boolean).join(" "));
|
|
858
|
+
}
|
|
859
|
+
return [...n, ...c].join(`
|
|
860
|
+
`);
|
|
861
|
+
}
|
|
862
|
+
function Or(t, r, e = {}) {
|
|
863
|
+
const n = e.values ? Pt(t, e.values) : t, s = e.schema ? Tt(n, e.schema).query : n, i = It(s, e);
|
|
864
|
+
return Dt(e.nullSafeEqual, () => ht(i, r));
|
|
865
|
+
}
|
|
866
|
+
function ar(t, r) {
|
|
867
|
+
switch (t) {
|
|
868
|
+
case "mysql":
|
|
869
|
+
return "?";
|
|
870
|
+
case "mssql":
|
|
871
|
+
return `@p${r}`;
|
|
872
|
+
case "postgres":
|
|
873
|
+
return `$${r}`;
|
|
874
|
+
default:
|
|
875
|
+
return "?";
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
function Nr(t, r, e = {}) {
|
|
879
|
+
const n = e.schema ? Tt(t, e.schema) : { query: t, params: /* @__PURE__ */ new Map() }, s = {
|
|
880
|
+
values: [],
|
|
881
|
+
named: new Map(Object.entries(e.values ?? {}).map(([c, f]) => {
|
|
882
|
+
const $ = n.params.get(c);
|
|
883
|
+
return [c, $ ? Et(f, $) : f];
|
|
884
|
+
}))
|
|
885
|
+
}, i = It(n.query, e), u = Dt(
|
|
886
|
+
e.nullSafeEqual,
|
|
887
|
+
() => ht(i, r, s)
|
|
888
|
+
), o = [];
|
|
889
|
+
return { sql: u.replace(zt, (c, f) => {
|
|
890
|
+
const $ = s.values[Number(f)];
|
|
891
|
+
return o.push($ instanceof Date ? it($) : $), ar(r, o.length);
|
|
892
|
+
}), params: o };
|
|
893
|
+
}
|
|
894
|
+
export {
|
|
895
|
+
gr as D,
|
|
896
|
+
Gt as S,
|
|
897
|
+
Nr as a,
|
|
898
|
+
Or as b,
|
|
899
|
+
Dr as c,
|
|
900
|
+
Ar as s,
|
|
901
|
+
mr as u,
|
|
902
|
+
yr as w
|
|
903
|
+
};
|