@nongt/visql 0.12.0 → 0.12.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{PipelineEditor-DHfbOSzT.js → PipelineEditor-DDN3mL0I.js} +1058 -1058
- package/dist/core.js +1 -1
- package/dist/explain.js +1 -1
- package/dist/fixtures.js +1 -1
- package/dist/index.js +4 -4
- package/dist/mui.js +1 -1
- package/dist/{sqlGen-DGXVba3D.js → sqlGen-BbCX8Ywc.js} +179 -178
- package/dist/types/core/sqlGen.d.ts +8 -0
- package/package.json +1 -1
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var w = (t, r, e) =>
|
|
4
|
-
import { q as Y, d as F, a as
|
|
5
|
-
import { p as
|
|
6
|
-
import { s as
|
|
7
|
-
class
|
|
1
|
+
var Ut = Object.defineProperty;
|
|
2
|
+
var Yt = (t, r, e) => r in t ? Ut(t, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[r] = e;
|
|
3
|
+
var w = (t, r, e) => Yt(t, typeof r != "symbol" ? r + "" : r, e);
|
|
4
|
+
import { q as Y, d as F, a as _t, s as jt, m as Q, e as ft, g as Ht, t as vt, j as Bt, b as Pt, i as Gt } from "./schema-DsMO9oFE.js";
|
|
5
|
+
import { p as tt, t as rt, v as xt, w as kt, x as Xt, y as $t, z as Vt, A as Wt } from "./params-Btjlj45_.js";
|
|
6
|
+
import { s as lt } from "./calendarDate-CYlr9TvX.js";
|
|
7
|
+
class Qt extends Error {
|
|
8
8
|
constructor(e, n, s, o) {
|
|
9
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`);
|
|
@@ -19,7 +19,7 @@ class Wt extends Error {
|
|
|
19
19
|
this.name = "ScopeError", this.path = e, this.table = n, o && (this.alias = o);
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
|
-
const _ = (t) => Object.prototype.toString.call(t) === "[object Object]",
|
|
22
|
+
const _ = (t) => Object.prototype.toString.call(t) === "[object Object]", Et = (t) => Array.isArray(t.joins) && _(t.from);
|
|
23
23
|
function q(t, r, e) {
|
|
24
24
|
if (Array.isArray(t)) {
|
|
25
25
|
const u = t.map((i, a) => q(i, [...r, a], e));
|
|
@@ -31,9 +31,9 @@ function q(t, r, e) {
|
|
|
31
31
|
const a = q(i, [...r, u], e);
|
|
32
32
|
return a !== i && (n = !0), [u, a];
|
|
33
33
|
}), o = n ? Object.fromEntries(s) : t;
|
|
34
|
-
return
|
|
34
|
+
return Et(o) ? e(o, r) : o;
|
|
35
35
|
}
|
|
36
|
-
const
|
|
36
|
+
const Tt = (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 }] : []), At = (t) => Y(t).flatMap((r) => {
|
|
37
37
|
const e = r.alias ?? r.table;
|
|
38
38
|
return e === void 0 ? [] : [e];
|
|
39
39
|
});
|
|
@@ -42,76 +42,76 @@ function P(t, r) {
|
|
|
42
42
|
const e = t.table && r.get(t.table);
|
|
43
43
|
return e ? { ...t, table: e } : t;
|
|
44
44
|
}
|
|
45
|
-
return
|
|
45
|
+
return Q(t, (e) => P(e, r));
|
|
46
46
|
}
|
|
47
|
-
const
|
|
48
|
-
function
|
|
47
|
+
const mt = (t) => t ? t.type === "logical" && t.operator === "AND" ? t.operands : [t] : [], Jt = (t) => t.joinType === "inner" || t.joinType === "left";
|
|
48
|
+
function zt(t, r, e, n, s) {
|
|
49
49
|
const o = r.type === "binary" && r.left && r.right ? { ...r, left: r.right, right: r.left } : r, u = { forward: !1, back: !1 };
|
|
50
50
|
return t.joins.forEach((i) => {
|
|
51
51
|
const a = i.alias ?? i.table;
|
|
52
|
-
if (a !== e && a !== n || !
|
|
52
|
+
if (a !== e && a !== n || !Jt(i) || !mt(i.on).some((f) => F(f, r) || F(f, o))) return;
|
|
53
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
|
-
const
|
|
58
|
-
function
|
|
59
|
-
const o =
|
|
57
|
+
const et = (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 gt(t, r, e, n, s = new Set(At(r))) {
|
|
59
|
+
const o = rt(n.condition), u = Y(r), i = (a) => {
|
|
60
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
63
|
}, y = (g, O, H) => {
|
|
64
|
-
if (O === g.length) return
|
|
65
|
-
const D = g[O], L =
|
|
64
|
+
if (O === g.length) return et(A(c.get(H)));
|
|
65
|
+
const D = g[O], L = jt(t, [...c.keys()], D, { anyShape: !0 });
|
|
66
66
|
if (!L) return !1;
|
|
67
|
-
const
|
|
68
|
-
c.set(D, T), f.set(T, { ref:
|
|
69
|
-
},
|
|
67
|
+
const z = c.get(rt(L.on).find((T) => T !== D)), K = (T) => P(L.on, new Map([...c, [D, T]])), Z = o.includes(D), d = (T, h) => {
|
|
68
|
+
c.set(D, T), f.set(T, { ref: z, tie: h });
|
|
69
|
+
}, qt = (T, h) => (d(T, h), et(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 h = T.alias ?? D, v =
|
|
73
|
-
return (v.forward || v.back) &&
|
|
72
|
+
const h = T.alias ?? D, v = zt(r, K(h), z, h, Z);
|
|
73
|
+
return (v.forward || v.back) && qt(h, v);
|
|
74
74
|
})) return !0;
|
|
75
75
|
if (!a) return !1;
|
|
76
|
-
let
|
|
77
|
-
for (let T = 2; $.has(
|
|
78
|
-
return $.add(
|
|
79
|
-
id:
|
|
76
|
+
let R = D;
|
|
77
|
+
for (let T = 2; $.has(R); T += 1) R = `${D}_${T}`;
|
|
78
|
+
return $.add(R), d(R, { back: !0, forward: L.joinType === "inner" || Z }), E.push({
|
|
79
|
+
id: xt(),
|
|
80
80
|
table: D,
|
|
81
|
-
...
|
|
81
|
+
...R !== D ? { alias: R } : {},
|
|
82
82
|
joinType: L.joinType,
|
|
83
|
-
on:
|
|
83
|
+
on: K(R)
|
|
84
84
|
}), y(g, O + 1, H);
|
|
85
85
|
};
|
|
86
|
-
let
|
|
86
|
+
let p;
|
|
87
87
|
if (o.every((g) => {
|
|
88
|
-
if (
|
|
89
|
-
const O =
|
|
90
|
-
return O ? y(O, 0, g) : (
|
|
88
|
+
if (p || c.has(g)) return !0;
|
|
89
|
+
const O = _t(t, [...c.keys()], g, { anyShape: !0 });
|
|
90
|
+
return O ? y(O, 0, g) : (p = g, !0);
|
|
91
91
|
}))
|
|
92
|
-
return $.forEach((g) => s.add(g)), { joins: E, condition: P(n.condition, c), ...
|
|
92
|
+
return $.forEach((g) => s.add(g)), { joins: E, condition: P(n.condition, c), ...p ? { missing: p } : {} };
|
|
93
93
|
};
|
|
94
94
|
return i(!1) ?? i(!0);
|
|
95
95
|
}
|
|
96
|
-
function
|
|
97
|
-
return
|
|
96
|
+
function yt(t, r) {
|
|
97
|
+
return mt(t).some((e) => F(e, r));
|
|
98
98
|
}
|
|
99
|
-
function
|
|
99
|
+
function Kt(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
|
-
function
|
|
103
|
-
return
|
|
104
|
-
const i =
|
|
105
|
-
if (i.missing) throw new
|
|
106
|
-
const a =
|
|
102
|
+
function nt(t, r, e, n, s) {
|
|
103
|
+
return Tt(r, e).reduce((o, u) => {
|
|
104
|
+
const i = gt(t, o, u, e, s);
|
|
105
|
+
if (i.missing) throw new Qt(n, u.table, i.missing, u.alias);
|
|
106
|
+
const a = yt(o.where, i.condition);
|
|
107
107
|
return i.joins.length === 0 && a ? o : {
|
|
108
108
|
...o,
|
|
109
109
|
joins: [...o.joins, ...i.joins],
|
|
110
|
-
where: a ? o.where :
|
|
110
|
+
where: a ? o.where : Kt(o.where, i.condition)
|
|
111
111
|
};
|
|
112
112
|
}, r);
|
|
113
113
|
}
|
|
114
|
-
function
|
|
114
|
+
function st(t) {
|
|
115
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));
|
|
@@ -119,7 +119,7 @@ function nt(t) {
|
|
|
119
119
|
}
|
|
120
120
|
if (!_(n)) return;
|
|
121
121
|
let i = u;
|
|
122
|
-
|
|
122
|
+
Et(n) && (i = o ?? /* @__PURE__ */ new Set(), r.set(JSON.stringify(s), i), At(n).forEach((a) => i.add(a))), Object.entries(n).forEach(([a, c]) => e(
|
|
123
123
|
c,
|
|
124
124
|
[...s, a],
|
|
125
125
|
n.type === "exists" && a === "query" ? i : void 0,
|
|
@@ -129,22 +129,22 @@ function nt(t) {
|
|
|
129
129
|
return e(t, [], void 0, /* @__PURE__ */ new Set()), r;
|
|
130
130
|
}
|
|
131
131
|
function Rr(t, r, e) {
|
|
132
|
-
const n =
|
|
132
|
+
const n = nt(
|
|
133
133
|
t,
|
|
134
134
|
r,
|
|
135
135
|
e,
|
|
136
136
|
[],
|
|
137
|
-
|
|
138
|
-
), s =
|
|
139
|
-
return
|
|
137
|
+
st(r).get("[]")
|
|
138
|
+
), s = tt(t, r, n), o = st(s), u = q(s, [], (i, a) => a.length === 0 ? i : nt(t, i, e, a, o.get(JSON.stringify(a))));
|
|
139
|
+
return tt(t, s, u);
|
|
140
140
|
}
|
|
141
|
-
function
|
|
141
|
+
function hr(t, r, e) {
|
|
142
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 q(r, [], (s, o) => (
|
|
146
|
-
const i =
|
|
147
|
-
(i.missing || i.joins.length > 0 || !
|
|
145
|
+
return q(r, [], (s, o) => (Tt(s, e).forEach((u) => {
|
|
146
|
+
const i = gt(t, s, u, e);
|
|
147
|
+
(i.missing || i.joins.length > 0 || !yt(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) {
|
|
@@ -161,9 +161,9 @@ function G(t, r) {
|
|
|
161
161
|
n.delete(o), s.delete(o);
|
|
162
162
|
}), { tableOf: e, aliases: n, derived: s };
|
|
163
163
|
}
|
|
164
|
-
function
|
|
164
|
+
function Zt(t, r, e) {
|
|
165
165
|
const n = t.on && M(t.on, r, e);
|
|
166
|
-
return t.query ? { ...t, on: n, query:
|
|
166
|
+
return t.query ? { ...t, on: n, query: Ht(t.query, (s) => j(s, e)) } : t.shared !== void 0 ? { ...t, on: n } : { ...t, on: n, table: e(t.table) };
|
|
167
167
|
}
|
|
168
168
|
function M(t, r, e) {
|
|
169
169
|
if (t.type === "field") {
|
|
@@ -184,15 +184,15 @@ function M(t, r, e) {
|
|
|
184
184
|
query: {
|
|
185
185
|
...n,
|
|
186
186
|
from: { ...n.from, table: e(n.from.table) },
|
|
187
|
-
joins: n.joins.map((i) =>
|
|
187
|
+
joins: n.joins.map((i) => Zt(i, o, e)),
|
|
188
188
|
...n.where ? { where: M(n.where, o, e) } : {}
|
|
189
189
|
}
|
|
190
190
|
};
|
|
191
191
|
}
|
|
192
|
-
return
|
|
192
|
+
return Q(t, (n) => M(n, r, e));
|
|
193
193
|
}
|
|
194
194
|
function j(t, r) {
|
|
195
|
-
const e = G([t.from, ...t.joins]), n =
|
|
195
|
+
const e = G([t.from, ...t.joins]), n = ft(
|
|
196
196
|
t,
|
|
197
197
|
(s) => M(s, e, r),
|
|
198
198
|
(s) => j(s, r)
|
|
@@ -217,17 +217,17 @@ function x(t, r) {
|
|
|
217
217
|
joins: e.joins.map(n)
|
|
218
218
|
};
|
|
219
219
|
}
|
|
220
|
-
function
|
|
220
|
+
function dt(t, r) {
|
|
221
221
|
return j(x(t, r), r);
|
|
222
222
|
}
|
|
223
|
-
const
|
|
224
|
-
function
|
|
223
|
+
const tr = /^(\d{2}):(\d{2})(?::(\d{2}))?$/, ot = /YYYY|MM|DD|HH|mm|ss/g, b = (t, r = 2) => String(t).padStart(r, "0");
|
|
224
|
+
function Dt(t, r) {
|
|
225
225
|
let e;
|
|
226
|
-
const n = typeof t == "string" ?
|
|
226
|
+
const n = typeof t == "string" ? tr.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" ? kt(t, "dateTime") : t;
|
|
231
231
|
u instanceof Date && (e = {
|
|
232
232
|
YYYY: b(u.getUTCFullYear(), 4),
|
|
233
233
|
MM: b(u.getUTCMonth() + 1),
|
|
@@ -239,20 +239,20 @@ function yt(t, r) {
|
|
|
239
239
|
}
|
|
240
240
|
if (!e) return t;
|
|
241
241
|
const s = e;
|
|
242
|
-
return (r.match(
|
|
242
|
+
return (r.match(ot) ?? []).some((u) => s[u] === void 0) ? t : r.replace(ot, (u) => s[u]);
|
|
243
243
|
}
|
|
244
|
-
function
|
|
244
|
+
function Ot(t, r) {
|
|
245
245
|
const e = /* @__PURE__ */ new Map(), n = (s) => {
|
|
246
246
|
const o = /* @__PURE__ */ new Map();
|
|
247
|
-
Y(s).forEach((c) => o.set(
|
|
247
|
+
Y(s).forEach((c) => o.set(vt(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 = o.has(c.table) ? o.get(c.table) : c.table, E = f === void 0 ? void 0 :
|
|
250
|
+
const f = o.has(c.table) ? o.get(c.table) : c.table, E = f === void 0 ? void 0 : Bt(r, f), $ = E == null ? void 0 : E.columns.find((A) => A.name === c.column);
|
|
251
251
|
return $ == null ? void 0 : $.format;
|
|
252
252
|
}, i = (c, f) => {
|
|
253
253
|
switch (c.type) {
|
|
254
254
|
case "literal":
|
|
255
|
-
return { ...c, value:
|
|
255
|
+
return { ...c, value: Dt(c.value, f) };
|
|
256
256
|
case "list":
|
|
257
257
|
return { ...c, items: c.items.map((E) => i(E, f)) };
|
|
258
258
|
case "param":
|
|
@@ -262,7 +262,7 @@ function Dt(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 = Q(c, a);
|
|
266
266
|
if (f.type !== "binary" || !f.right) return f;
|
|
267
267
|
const E = u(f.left), $ = u(f.right);
|
|
268
268
|
return {
|
|
@@ -271,11 +271,11 @@ function Dt(t, r) {
|
|
|
271
271
|
right: E ? i(f.right, E) : f.right
|
|
272
272
|
};
|
|
273
273
|
};
|
|
274
|
-
return
|
|
274
|
+
return ft(s, a, (c) => n(c));
|
|
275
275
|
};
|
|
276
276
|
return { query: n(t), params: e };
|
|
277
277
|
}
|
|
278
|
-
const
|
|
278
|
+
const Ir = ["mysql", "mssql", "postgres"], Cr = {
|
|
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 rr = /^[A-Za-z_][A-Za-z0-9_]*$/, St = (t) => `${t}`, er = /\uE000(\d+)\uE000/g;
|
|
297
297
|
let C;
|
|
298
|
-
function
|
|
298
|
+
function br(t, r) {
|
|
299
299
|
C = { ...t, writing: [] };
|
|
300
300
|
try {
|
|
301
301
|
return r();
|
|
@@ -303,17 +303,17 @@ function Cr(t, r) {
|
|
|
303
303
|
C = void 0;
|
|
304
304
|
}
|
|
305
305
|
}
|
|
306
|
-
function
|
|
306
|
+
function Nt(t, r, e) {
|
|
307
307
|
if (typeof t != "object" || t === null || t instanceof Date) return;
|
|
308
308
|
let n;
|
|
309
309
|
return Object.entries(t).some(([s, o]) => {
|
|
310
310
|
const u = [...e, Array.isArray(t) ? Number(s) : s];
|
|
311
|
-
return n = o === r ? u :
|
|
311
|
+
return n = o === r ? u : Nt(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], o = typeof t == "string" ? [t] :
|
|
316
|
+
const { writing: e, paths: n } = C, s = e[e.length - 1], o = typeof t == "string" ? [t] : Nt(s.value, t, s.path);
|
|
317
317
|
if (!o) return r();
|
|
318
318
|
e.push({ value: t, path: o });
|
|
319
319
|
try {
|
|
@@ -322,7 +322,7 @@ function m(t, r) {
|
|
|
322
322
|
e.pop();
|
|
323
323
|
}
|
|
324
324
|
}
|
|
325
|
-
const
|
|
325
|
+
const nr = {
|
|
326
326
|
mysql: {
|
|
327
327
|
text: "CHAR",
|
|
328
328
|
integer: "SIGNED",
|
|
@@ -351,33 +351,34 @@ const er = {
|
|
|
351
351
|
boolean: "boolean"
|
|
352
352
|
}
|
|
353
353
|
};
|
|
354
|
-
function
|
|
354
|
+
function sr(t, r) {
|
|
355
355
|
var n;
|
|
356
|
-
const e = (n =
|
|
356
|
+
const e = (n = nr[r]) == null ? void 0 : n[t];
|
|
357
357
|
return e ? r === "postgres" ? `NULL::${e}` : `CAST(NULL AS ${e})` : "NULL";
|
|
358
358
|
}
|
|
359
|
-
|
|
359
|
+
let k = !0;
|
|
360
|
+
function pt(t, r, e) {
|
|
360
361
|
if (t == null) return "NULL";
|
|
361
362
|
if (e)
|
|
362
|
-
return e.values.push(t),
|
|
363
|
+
return e.values.push(t), St(e.values.length - 1);
|
|
363
364
|
if (typeof t == "boolean")
|
|
364
365
|
return r === "mssql" ? t ? "1" : "0" : t ? "TRUE" : "FALSE";
|
|
365
366
|
if (typeof t == "string") {
|
|
366
|
-
const n = C ? t.replace(/[\uE001-\uE003]/g, "") : t;
|
|
367
|
-
return
|
|
367
|
+
const n = C ? t.replace(/[\uE001-\uE003]/g, "") : t, s = r === "mysql" && k ? n.replace(/\\/g, "\\\\") : n;
|
|
368
|
+
return s === "" ? "''" : `'${s.replace(/'/g, "''")}'`;
|
|
368
369
|
}
|
|
369
|
-
return typeof t == "number" || typeof t == "bigint" ? String(t) : t instanceof Date ? `'${
|
|
370
|
+
return typeof t == "number" || typeof t == "bigint" ? String(t) : t instanceof Date ? `'${lt(t)}'` : String(t);
|
|
370
371
|
}
|
|
371
372
|
let U = "native", X = "decimal";
|
|
372
373
|
function Rt(t, r) {
|
|
373
|
-
U = t.nullSafeEqual ?? "native", X = t.division ?? "decimal";
|
|
374
|
+
U = t.nullSafeEqual ?? "native", X = t.division ?? "decimal", k = t.backslashEscapes ?? !0;
|
|
374
375
|
try {
|
|
375
376
|
return r();
|
|
376
377
|
} finally {
|
|
377
|
-
U = "native", X = "decimal";
|
|
378
|
+
U = "native", X = "decimal", k = !0;
|
|
378
379
|
}
|
|
379
380
|
}
|
|
380
|
-
function
|
|
381
|
+
function or(t, r) {
|
|
381
382
|
switch (t) {
|
|
382
383
|
case "!=":
|
|
383
384
|
return r === "mssql" ? "<>" : "!=";
|
|
@@ -388,7 +389,7 @@ function sr(t, r) {
|
|
|
388
389
|
if (r === "mssql") throw new Error("REGEXP isn't available in SQL Server");
|
|
389
390
|
return "REGEXP";
|
|
390
391
|
default:
|
|
391
|
-
if (!
|
|
392
|
+
if (!Wt.includes(t))
|
|
392
393
|
throw new Error(`unknown operator ${JSON.stringify(t)}`);
|
|
393
394
|
return t;
|
|
394
395
|
}
|
|
@@ -410,15 +411,15 @@ const B = {
|
|
|
410
411
|
function S(t) {
|
|
411
412
|
return t.type === "binary" || t.type === "logical" || t.type === "if" || t.type === "case";
|
|
412
413
|
}
|
|
413
|
-
const
|
|
414
|
-
function
|
|
414
|
+
const it = (t, r) => (r == null ? void 0 : r.type) === "arithmetic" || r && S(r) ? `(${t})` : t;
|
|
415
|
+
function ut(t, [r = "??", , e], n) {
|
|
415
416
|
if (n.length < 2) return `${t}(${r})`;
|
|
416
417
|
const s = n[1], o = s.type === "literal" && Number.isInteger(s.value) && s.value >= 0 ? String(s.value) : "??";
|
|
417
418
|
return `${t}(${r}, ${o}${e === void 0 ? "" : `, ${e}`})`;
|
|
418
419
|
}
|
|
419
|
-
const
|
|
420
|
+
const ir = {
|
|
420
421
|
// SQL Server has no MOD(), only `%`.
|
|
421
|
-
MOD: ([t = "??", r = "??"], e, n) => n === "mssql" ? `(${
|
|
422
|
+
MOD: ([t = "??", r = "??"], e, n) => n === "mssql" ? `(${it(t, e[0])} % ${it(r, e[1])})` : `MOD(${t}, ${r})`,
|
|
422
423
|
// ---- Date arithmetic ----
|
|
423
424
|
DATE_ADD: ([t, r], e, n, s) => {
|
|
424
425
|
var u;
|
|
@@ -577,13 +578,13 @@ const or = {
|
|
|
577
578
|
// ---- Window functions (issue 20) ----
|
|
578
579
|
// The offset goes into the SQL as a whole number even in parameterized
|
|
579
580
|
// SQL: MySQL takes nothing else there. OVER is added by formatNode.
|
|
580
|
-
LAG: (t, r) =>
|
|
581
|
-
LEAD: (t, r) =>
|
|
581
|
+
LAG: (t, r) => ut("LAG", t, r),
|
|
582
|
+
LEAD: (t, r) => ut("LEAD", t, r),
|
|
582
583
|
// ---- String aggregation (issue 137) ----
|
|
583
584
|
// The separator goes into the SQL as a literal even in parameterized SQL:
|
|
584
585
|
// MySQL's SEPARATOR takes nothing else.
|
|
585
586
|
STRING_AGG: ([t = "??"], r, e, n, s, o) => {
|
|
586
|
-
const u = r[1] ?? { type: "literal", value: ", " }, i = u.type === "literal" && typeof u.value == "string" ?
|
|
587
|
+
const u = r[1] ?? { type: "literal", value: ", " }, i = u.type === "literal" && typeof u.value == "string" ? pt(u.value, e) : "??", a = s.distinct ? "DISTINCT " : "", c = ($) => {
|
|
587
588
|
var A;
|
|
588
589
|
return (A = s.orderBy) != null && A.length ? `ORDER BY ${s.orderBy.map((y) => `${$(y)} ${y.direction === "desc" ? "DESC" : "ASC"}`).join(", ")}` : "";
|
|
589
590
|
}, f = ($) => o($.expression), E = ($) => $ ? ` ${$}` : "";
|
|
@@ -609,7 +610,7 @@ const or = {
|
|
|
609
610
|
}
|
|
610
611
|
}
|
|
611
612
|
};
|
|
612
|
-
function
|
|
613
|
+
function ur(t, r) {
|
|
613
614
|
var n, s;
|
|
614
615
|
return `OVER (${[
|
|
615
616
|
...(n = t.partitionBy) != null && n.length ? [`PARTITION BY ${t.partitionBy.map(r).join(", ")}`] : [],
|
|
@@ -617,10 +618,10 @@ function ir(t, r) {
|
|
|
617
618
|
].join(" ")})`;
|
|
618
619
|
}
|
|
619
620
|
function l(t, r, e, n) {
|
|
620
|
-
return m(t, () =>
|
|
621
|
+
return m(t, () => fr(t, r, e, n));
|
|
621
622
|
}
|
|
622
|
-
function
|
|
623
|
-
const s = t.args.map((a) => l(a, r, e, n)), o =
|
|
623
|
+
function cr(t, r, e, n) {
|
|
624
|
+
const s = t.args.map((a) => l(a, r, e, n)), o = ir[t.name.toUpperCase()];
|
|
624
625
|
if (o)
|
|
625
626
|
return o(
|
|
626
627
|
s,
|
|
@@ -635,7 +636,7 @@ function ur(t, r, e, n) {
|
|
|
635
636
|
const i = t.distinct && s.length > 0 ? "DISTINCT " : "";
|
|
636
637
|
return `${u}(${i}${s.join(", ")})`;
|
|
637
638
|
}
|
|
638
|
-
function
|
|
639
|
+
function ct(t, r, e, n) {
|
|
639
640
|
const s = (o) => {
|
|
640
641
|
if (!o) return "??";
|
|
641
642
|
const u = l(o, r, e, n);
|
|
@@ -646,31 +647,31 @@ function ut(t, r, e, n) {
|
|
|
646
647
|
return `${s(o)} AND ${s(u)}`;
|
|
647
648
|
});
|
|
648
649
|
}
|
|
649
|
-
const
|
|
650
|
+
const V = {
|
|
650
651
|
"+": 1,
|
|
651
652
|
"-": 1,
|
|
652
653
|
"*": 2,
|
|
653
654
|
"/": 2
|
|
654
655
|
};
|
|
655
|
-
function
|
|
656
|
+
function at(t, r, e, n, s, o) {
|
|
656
657
|
const u = l(t, n, s, o);
|
|
657
|
-
return t.type === "arithmetic" && (
|
|
658
|
+
return t.type === "arithmetic" && (V[t.operator] < r || e && V[t.operator] === r) || S(t) ? `(${u})` : u;
|
|
658
659
|
}
|
|
659
|
-
function
|
|
660
|
-
const s =
|
|
660
|
+
function ar(t, r, e, n) {
|
|
661
|
+
const s = V[t.operator], o = at(t.left, s, !1, r, e, n), u = at(t.right, s, !0, r, e, n);
|
|
661
662
|
return t.operator === "/" && X === "decimal" && r !== "mysql" ? `${o} * 1.0 / ${u}` : `${o} ${t.operator} ${u}`;
|
|
662
663
|
}
|
|
663
|
-
function
|
|
664
|
+
function fr(t, r, e, n) {
|
|
664
665
|
var u;
|
|
665
666
|
const s = " ".repeat(e), o = " ".repeat(e + 1);
|
|
666
667
|
switch (t.type) {
|
|
667
668
|
case "literal":
|
|
668
|
-
return t.value === "" && !t.empty ? "??" : t.value === null && t.as ?
|
|
669
|
+
return t.value === "" && !t.empty ? "??" : t.value === null && t.as ? sr(t.as, r) : pt(t.value, r, n);
|
|
669
670
|
case "param": {
|
|
670
|
-
if (!n) return
|
|
671
|
+
if (!n) return rr.test(t.name) ? `:${t.name}` : "??";
|
|
671
672
|
if (!n.named.has(t.name))
|
|
672
673
|
throw new Error(`no value for parameter "${t.name}"`);
|
|
673
|
-
return n.values.push(n.named.get(t.name)),
|
|
674
|
+
return n.values.push(n.named.get(t.name)), St(n.values.length - 1);
|
|
674
675
|
}
|
|
675
676
|
case "field": {
|
|
676
677
|
if (!t.column) return "??";
|
|
@@ -678,29 +679,29 @@ function ar(t, r, e, n) {
|
|
|
678
679
|
return t.table ? `${N(t.table, r)}.${i}` : i;
|
|
679
680
|
}
|
|
680
681
|
case "function": {
|
|
681
|
-
const i =
|
|
682
|
-
return t.over ? `${i} ${
|
|
682
|
+
const i = cr(t, r, e, n);
|
|
683
|
+
return t.over ? `${i} ${ur(t, (a) => l(a, r, e, n))}` : i;
|
|
683
684
|
}
|
|
684
685
|
case "binary": {
|
|
685
|
-
const i = t.operator ?
|
|
686
|
-
if (
|
|
686
|
+
const i = t.operator ? or(t.operator, r) : "=", a = l(t.left, r, e, n), c = S(t.left) ? `(${a})` : a;
|
|
687
|
+
if (Vt(t.operator)) return `${c} ${i}`;
|
|
687
688
|
if (!t.right) return `${c} ${i} ??`;
|
|
688
689
|
if (i === "BETWEEN")
|
|
689
|
-
return `${c} BETWEEN ${
|
|
690
|
+
return `${c} BETWEEN ${ct(t.right, r, e, n)}`;
|
|
690
691
|
if (i === "IN")
|
|
691
692
|
return t.right.type === "list" ? `${c} IN ${l(t.right, r, e, n)}` : `${c} IN (${l(t.right, r, e, n)})`;
|
|
692
693
|
const f = l(t.right, r, e, n), E = S(t.right) ? `(${f})` : f;
|
|
693
694
|
if (t.operator === "IS NOT DISTINCT FROM" && U === "expanded") {
|
|
694
695
|
const $ = (y) => {
|
|
695
|
-
const
|
|
696
|
-
return S(y) ? `(${
|
|
696
|
+
const p = l(y, r, e, n);
|
|
697
|
+
return S(y) ? `(${p})` : p;
|
|
697
698
|
}, A = `${$(t.left)} IS NULL AND ${$(t.right)} IS NULL`;
|
|
698
699
|
return `(${c} = ${E} OR (${A}))`;
|
|
699
700
|
}
|
|
700
701
|
return `${c} ${i} ${E}`;
|
|
701
702
|
}
|
|
702
703
|
case "arithmetic":
|
|
703
|
-
return
|
|
704
|
+
return ar(t, r, e, n);
|
|
704
705
|
case "logical": {
|
|
705
706
|
if (t.operands.length === 0) return "/* empty */";
|
|
706
707
|
if (t.operands.length === 1)
|
|
@@ -727,7 +728,7 @@ ${s})`;
|
|
|
727
728
|
}
|
|
728
729
|
if (i.type === "binary" && i.operator === "BETWEEN" && i.right) {
|
|
729
730
|
const a = l(i.left, r, e, n);
|
|
730
|
-
return `${S(i.left) ? `(${a})` : a} NOT BETWEEN ${
|
|
731
|
+
return `${S(i.left) ? `(${a})` : a} NOT BETWEEN ${ct(i.right, r, e, n)}`;
|
|
731
732
|
}
|
|
732
733
|
if (i.type === "binary" && i.operator === "IS NOT DISTINCT FROM" && i.right && U === "native") {
|
|
733
734
|
if (r === "mysql") return `NOT (${l(i, r, e, n)})`;
|
|
@@ -766,11 +767,11 @@ ${[
|
|
|
766
767
|
"SELECT 1",
|
|
767
768
|
// The clauses below write their expressions with formatNode.
|
|
768
769
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
769
|
-
|
|
770
|
+
Lt(i.from, r),
|
|
770
771
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
771
|
-
...i.joins.map(($) =>
|
|
772
|
+
...i.joins.map(($) => Mt($, r, n)),
|
|
772
773
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
773
|
-
|
|
774
|
+
ht(f, r, n)
|
|
774
775
|
].join(`
|
|
775
776
|
`).replace(/^/gm, o)}
|
|
776
777
|
${s})`;
|
|
@@ -782,62 +783,62 @@ ${s})`;
|
|
|
782
783
|
return `(${t.items.map((i) => l(i, r, e, n)).join(", ") || "??"})`;
|
|
783
784
|
case "subquery":
|
|
784
785
|
return `
|
|
785
|
-
${
|
|
786
|
+
${J(t.query, r, n).join(`
|
|
786
787
|
`).replace(/^/gm, o)}
|
|
787
788
|
${s}`;
|
|
788
789
|
default:
|
|
789
790
|
return "/* unknown */";
|
|
790
791
|
}
|
|
791
792
|
}
|
|
792
|
-
function
|
|
793
|
+
function ht(t, r, e) {
|
|
793
794
|
return `WHERE
|
|
794
795
|
${l(t, r, 1, e)}`;
|
|
795
796
|
}
|
|
796
|
-
const
|
|
797
|
+
const $r = {
|
|
797
798
|
inner: "INNER JOIN",
|
|
798
799
|
left: "LEFT JOIN",
|
|
799
800
|
right: "RIGHT JOIN",
|
|
800
801
|
full: "FULL JOIN",
|
|
801
802
|
cross: "CROSS JOIN"
|
|
802
803
|
};
|
|
803
|
-
let
|
|
804
|
-
function
|
|
805
|
-
return t.shared !== void 0 ? N(
|
|
806
|
-
${
|
|
804
|
+
let It = /* @__PURE__ */ new Map();
|
|
805
|
+
function Ct(t, r, e) {
|
|
806
|
+
return t.shared !== void 0 ? N(It.get(t.shared) ?? "??", r) : t.query ? `(
|
|
807
|
+
${bt(t.query, r, e).replace(/^/gm, " ")}
|
|
807
808
|
)` : N(t.table, r);
|
|
808
809
|
}
|
|
809
|
-
function
|
|
810
|
-
return
|
|
811
|
-
`)).join(
|
|
810
|
+
function bt(t, r, e) {
|
|
811
|
+
return Pt(t).map((n) => J(n, r, e).join(`
|
|
812
|
+
`)).join(Gt(t) && !t.all ? `
|
|
812
813
|
UNION
|
|
813
814
|
` : `
|
|
814
815
|
UNION ALL
|
|
815
816
|
`);
|
|
816
817
|
}
|
|
817
|
-
function
|
|
818
|
+
function lr(t, r, e) {
|
|
818
819
|
return t.length === 0 ? [] : [`WITH
|
|
819
820
|
${t.map((s) => m(s, () => {
|
|
820
|
-
const o =
|
|
821
|
+
const o = bt(s.query, r, e).replace(/^/gm, " ");
|
|
821
822
|
return ` ${N(s.name, r)} AS (
|
|
822
823
|
${o}
|
|
823
824
|
)`;
|
|
824
825
|
})).join(`,
|
|
825
826
|
`)}`];
|
|
826
827
|
}
|
|
827
|
-
function
|
|
828
|
+
function Lt(t, r, e) {
|
|
828
829
|
const n = t.alias ? ` AS ${N(t.alias, r)}` : "";
|
|
829
|
-
return `FROM ${m(t, () => `${
|
|
830
|
+
return `FROM ${m(t, () => `${Ct(t, r, e)}${n}`)}`;
|
|
830
831
|
}
|
|
831
|
-
function
|
|
832
|
+
function Mt(t, r, e) {
|
|
832
833
|
return m(t, () => {
|
|
833
|
-
const n =
|
|
834
|
+
const n = Ct(t, r, e), s = t.alias ? ` AS ${N(t.alias, r)}` : "";
|
|
834
835
|
if (t.joinType === "cross") return `CROSS JOIN ${n}${s}`;
|
|
835
836
|
const o = t.on ? l(t.on, r, 1, e) : "/* unconfigured */";
|
|
836
|
-
return `${
|
|
837
|
+
return `${$r[t.joinType]} ${n}${s}
|
|
837
838
|
ON ${o}`;
|
|
838
839
|
});
|
|
839
840
|
}
|
|
840
|
-
function
|
|
841
|
+
function Er(t, r, e, n = "SELECT") {
|
|
841
842
|
if (t.length === 0) return `${n} ${m(t, () => "*")}`;
|
|
842
843
|
const s = t.map((o) => m(o, () => {
|
|
843
844
|
const u = l(o.expression, r, 1, e), i = o.alias ? ` AS ${N(o.alias, r)}` : "";
|
|
@@ -847,7 +848,7 @@ function lr(t, r, e, n = "SELECT") {
|
|
|
847
848
|
${s.join(`,
|
|
848
849
|
`)}`;
|
|
849
850
|
}
|
|
850
|
-
function
|
|
851
|
+
function Tr(t, r, e) {
|
|
851
852
|
return `GROUP BY
|
|
852
853
|
${t.map((s) => m(
|
|
853
854
|
s,
|
|
@@ -855,56 +856,56 @@ function Er(t, r, e) {
|
|
|
855
856
|
)).join(`,
|
|
856
857
|
`)}`;
|
|
857
858
|
}
|
|
858
|
-
function
|
|
859
|
+
function Ar(t, r, e) {
|
|
859
860
|
return `HAVING
|
|
860
861
|
${l(t, r, 1, e)}`;
|
|
861
862
|
}
|
|
862
|
-
function
|
|
863
|
+
function mr(t, r, e, n) {
|
|
863
864
|
return `ORDER BY
|
|
864
865
|
${t.map((o) => m(o, () => `${l(n(o.expression), r, 1, e)} ${o.direction === "desc" ? "DESC" : "ASC"}`)).join(`,
|
|
865
866
|
`)}`;
|
|
866
867
|
}
|
|
867
|
-
const
|
|
868
|
-
function
|
|
869
|
-
const e = r.resolveName ?
|
|
870
|
-
return r.defaultLimit !== void 0 &&
|
|
868
|
+
const W = (t) => t !== void 0 && Number.isInteger(t) && t >= 0 ? t : void 0;
|
|
869
|
+
function wt(t, r) {
|
|
870
|
+
const e = r.resolveName ? dt(t, r.resolveName) : t;
|
|
871
|
+
return r.defaultLimit !== void 0 && W(e.limit) === void 0 ? { ...e, limit: r.defaultLimit } : e;
|
|
871
872
|
}
|
|
872
|
-
const
|
|
873
|
-
function
|
|
873
|
+
const gr = "18446744073709551615";
|
|
874
|
+
function J(t, r, e, n = `SELECT${t.distinct ? " DISTINCT" : ""}`) {
|
|
874
875
|
const s = [
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
...t.joins.map((u) =>
|
|
876
|
+
Er(t.select, r, e, n),
|
|
877
|
+
Lt(t.from, r, e),
|
|
878
|
+
...t.joins.map((u) => Mt(u, r, e))
|
|
878
879
|
];
|
|
879
|
-
t.where && s.push(
|
|
880
|
+
t.where && s.push(ht(t.where, r, e));
|
|
880
881
|
const o = t.groupBy ?? [];
|
|
881
|
-
if (o.length > 0 && s.push(
|
|
882
|
-
const u = r === "mysql" ?
|
|
883
|
-
s.push(
|
|
882
|
+
if (o.length > 0 && s.push(Tr(o, r, e)), t.having) {
|
|
883
|
+
const u = r === "mysql" ? $t(t.having, t) : t.having;
|
|
884
|
+
s.push(Ar(u, r, e));
|
|
884
885
|
}
|
|
885
886
|
return s;
|
|
886
887
|
}
|
|
887
|
-
function
|
|
888
|
-
C && (C.writing = [{ value: t, path: [] }]),
|
|
889
|
-
const n =
|
|
890
|
-
if (u.length > 0 && c.push(
|
|
888
|
+
function Ft(t, r, e) {
|
|
889
|
+
C && (C.writing = [{ value: t, path: [] }]), It = new Map((t.shared ?? []).map((f) => [f.id, f.name]));
|
|
890
|
+
const n = lr(t.shared ?? [], r, e), s = W(t.limit), o = W(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 = J(t, r, e, a);
|
|
891
|
+
if (u.length > 0 && c.push(mr(u, r, e, (f) => r === "mysql" ? $t(f, t) : f)), r === "mssql") {
|
|
891
892
|
if (o !== void 0) {
|
|
892
893
|
u.length === 0 && c.push("ORDER BY (SELECT NULL)");
|
|
893
894
|
const f = s !== void 0 ? ` ${m("limit", () => `FETCH NEXT ${s} ROWS ONLY`)}` : "";
|
|
894
895
|
c.push(`${m("offset", () => `OFFSET ${o} ROWS`)}${f}`);
|
|
895
896
|
}
|
|
896
897
|
} else if (s !== void 0 || o !== void 0) {
|
|
897
|
-
const f = s !== void 0 || r === "mysql" ? m(s !== void 0 ? "limit" : {}, () => `LIMIT ${s ??
|
|
898
|
+
const f = s !== void 0 || r === "mysql" ? m(s !== void 0 ? "limit" : {}, () => `LIMIT ${s ?? gr}`) : "", E = o !== void 0 ? m("offset", () => `OFFSET ${o}`) : "";
|
|
898
899
|
c.push([f, E].filter(Boolean).join(" "));
|
|
899
900
|
}
|
|
900
901
|
return [...n, ...c].join(`
|
|
901
902
|
`);
|
|
902
903
|
}
|
|
903
|
-
function
|
|
904
|
-
const n = e.values ? Xt(t, e.values) : t, s = e.schema ?
|
|
905
|
-
return Rt(e, () =>
|
|
904
|
+
function Lr(t, r, e = {}) {
|
|
905
|
+
const n = e.values ? Xt(t, e.values) : t, s = e.schema ? Ot(n, e.schema).query : n, o = wt(s, e);
|
|
906
|
+
return Rt(e, () => Ft(o, r));
|
|
906
907
|
}
|
|
907
|
-
function
|
|
908
|
+
function yr(t, r) {
|
|
908
909
|
switch (t) {
|
|
909
910
|
case "mysql":
|
|
910
911
|
return "?";
|
|
@@ -916,26 +917,26 @@ function gr(t, r) {
|
|
|
916
917
|
return "?";
|
|
917
918
|
}
|
|
918
919
|
}
|
|
919
|
-
function
|
|
920
|
-
const n = e.schema ?
|
|
920
|
+
function Mr(t, r, e = {}) {
|
|
921
|
+
const n = e.schema ? Ot(t, e.schema) : { query: t, params: /* @__PURE__ */ new Map() }, s = {
|
|
921
922
|
values: [],
|
|
922
923
|
named: new Map(Object.entries(e.values ?? {}).map(([c, f]) => {
|
|
923
924
|
const E = n.params.get(c);
|
|
924
|
-
return [c, E ?
|
|
925
|
+
return [c, E ? Dt(f, E) : f];
|
|
925
926
|
}))
|
|
926
|
-
}, o =
|
|
927
|
-
return { sql: u.replace(
|
|
927
|
+
}, o = wt(n.query, e), u = Rt(e, () => Ft(o, r, s)), i = [];
|
|
928
|
+
return { sql: u.replace(er, (c, f) => {
|
|
928
929
|
const E = s.values[Number(f)];
|
|
929
|
-
return i.push(E instanceof Date ?
|
|
930
|
+
return i.push(E instanceof Date ? lt(E) : E), yr(r, i.length);
|
|
930
931
|
}), params: i };
|
|
931
932
|
}
|
|
932
933
|
export {
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
934
|
+
Ir as D,
|
|
935
|
+
Qt as S,
|
|
936
|
+
Mr as a,
|
|
937
|
+
Lr as b,
|
|
938
|
+
Cr as c,
|
|
938
939
|
Rr as s,
|
|
939
|
-
|
|
940
|
-
|
|
940
|
+
hr as u,
|
|
941
|
+
br as w
|
|
941
942
|
};
|