@nongt/visql-core 0.9.0 → 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core.js +83 -80
- package/dist/explain.js +325 -134
- package/dist/{issueLocation-BOSdd1JX.js → issueLocation-BlQ280IE.js} +176 -174
- package/dist/{params-MMOdKWnW.js → params-DkVFbMQn.js} +501 -495
- package/dist/{schema-dP_ISylQ.js → schema-CgL10bid.js} +333 -295
- package/dist/sql-import.js +1 -1
- package/dist/{sqlGen-B7-2-i9V.js → sqlGen-C1kZwvWI.js} +56 -56
- package/dist/types/core/pipelineFlow.d.ts +1 -1
- package/dist/types/core/schema.d.ts +56 -22
- package/dist/types/core-api.d.ts +1 -1
- package/dist/types/explain/explainQuery.d.ts +12 -6
- package/package.json +1 -1
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { C as Ye, E as Ze, F as er, G as Ge, H as Le, I as rr, J as _e, K as nr, L as tr, N as sr,
|
|
1
|
+
import { l as fe, z as Je, q as be, t as X, i as $e, y as ve, d as re, o as ye, b as Ue, j as he, f as Pe, A as He, D as Ke, B as Be, C as Oe, M as K, F as We, G as ce, u as oe, H as ze, I as Ve } from "./schema-CgL10bid.js";
|
|
2
|
+
import { C as Ye, E as Ze, F as er, G as Ge, H as Le, I as rr, J as _e, K as nr, L as tr, N as sr, t as ir, O as ar, P as or, A as lr, z as cr, b as ur, Q as je, M as fr, a as dr, R as Me, S as mr, w as yr, T as hr, U as gr } from "./params-DkVFbMQn.js";
|
|
3
3
|
const we = (n) => typeof n == "object" && n !== null && !Array.isArray(n) && !(n instanceof Date), Se = (n) => typeof n.shared == "string";
|
|
4
4
|
function ue(n, a) {
|
|
5
5
|
if (Array.isArray(n)) {
|
|
6
6
|
let r = !1;
|
|
7
|
-
const
|
|
7
|
+
const b = n.flatMap((v) => {
|
|
8
8
|
const l = ue(v, a);
|
|
9
9
|
return l !== v && (r = !0), l === void 0 ? [] : [l];
|
|
10
10
|
});
|
|
11
|
-
return r ?
|
|
11
|
+
return r ? b : n;
|
|
12
12
|
}
|
|
13
13
|
if (!we(n)) return n;
|
|
14
14
|
if (Se(n)) return a(n);
|
|
15
15
|
let c = !1;
|
|
16
|
-
const i = Object.entries(n).map(([r,
|
|
17
|
-
const v = ue(
|
|
18
|
-
return v !==
|
|
16
|
+
const i = Object.entries(n).map(([r, b]) => {
|
|
17
|
+
const v = ue(b, a);
|
|
18
|
+
return v !== b && (c = !0), [r, v];
|
|
19
19
|
});
|
|
20
20
|
return c ? Object.fromEntries(i) : n;
|
|
21
21
|
}
|
|
22
22
|
function Gr(n) {
|
|
23
23
|
const a = new Map((n.shared ?? []).map((i) => [i.id, []])), c = (i, r) => {
|
|
24
24
|
if (Array.isArray(i)) {
|
|
25
|
-
i.forEach((
|
|
25
|
+
i.forEach((b, v) => c(b, [...r, v]));
|
|
26
26
|
return;
|
|
27
27
|
}
|
|
28
28
|
if (we(i)) {
|
|
29
29
|
if (Se(i)) {
|
|
30
|
-
const
|
|
31
|
-
a.set(
|
|
30
|
+
const b = i.shared;
|
|
31
|
+
a.set(b, [...a.get(b) ?? [], r]);
|
|
32
32
|
return;
|
|
33
33
|
}
|
|
34
|
-
Object.entries(i).forEach(([
|
|
34
|
+
Object.entries(i).forEach(([b, v]) => c(v, [...r, b]));
|
|
35
35
|
}
|
|
36
36
|
};
|
|
37
37
|
return c(n, []), a;
|
|
@@ -52,10 +52,10 @@ function pr(n, a) {
|
|
|
52
52
|
return i;
|
|
53
53
|
}
|
|
54
54
|
function br(n, a, c, i = fe(), r) {
|
|
55
|
-
const
|
|
55
|
+
const b = n.shared ?? [], v = b.findIndex((d) => d.id === r), l = { id: i, name: a, query: c };
|
|
56
56
|
return {
|
|
57
57
|
...n,
|
|
58
|
-
shared: v < 0 ? [...
|
|
58
|
+
shared: v < 0 ? [...b, l] : [...b.slice(0, v), l, ...b.slice(v)]
|
|
59
59
|
};
|
|
60
60
|
}
|
|
61
61
|
const Qe = (n, a) => {
|
|
@@ -65,27 +65,27 @@ const Qe = (n, a) => {
|
|
|
65
65
|
function _r(n, a, c, i = {}) {
|
|
66
66
|
const r = Qe(n, c);
|
|
67
67
|
if (!r) return a;
|
|
68
|
-
const
|
|
68
|
+
const b = {
|
|
69
69
|
id: i.id ?? fe(),
|
|
70
70
|
shared: c,
|
|
71
71
|
alias: pr(r.name, a),
|
|
72
72
|
joinType: i.joinType ?? "inner",
|
|
73
73
|
...i.on ? { on: i.on } : {}
|
|
74
74
|
};
|
|
75
|
-
return { ...a, joins: [...a.joins,
|
|
75
|
+
return { ...a, joins: [...a.joins, b] };
|
|
76
76
|
}
|
|
77
77
|
function Qr(n, a, c) {
|
|
78
78
|
const i = Qe(n, c);
|
|
79
79
|
return i ? { ...a, from: { shared: c, alias: i.name } } : a;
|
|
80
80
|
}
|
|
81
81
|
function Rr(n, a, c = fe(), i = []) {
|
|
82
|
-
const r = Ye(n, i),
|
|
83
|
-
if (!
|
|
82
|
+
const r = Ye(n, i), b = r && Ze(r).find((f) => f.id === a);
|
|
83
|
+
if (!b) return n;
|
|
84
84
|
const v = new Set((n.shared ?? []).map((f) => f.name));
|
|
85
|
-
let l =
|
|
86
|
-
for (let f = 2; v.has(l); f += 1) l = `${
|
|
87
|
-
const d = br(n, l,
|
|
88
|
-
return er(d, i, (f) => f.from.query && f.from.id === a ? { ...f, from: { shared: c, alias:
|
|
85
|
+
let l = b.alias;
|
|
86
|
+
for (let f = 2; v.has(l); f += 1) l = `${b.alias}_${f}`;
|
|
87
|
+
const d = br(n, l, b.query, c, i[0]);
|
|
88
|
+
return er(d, i, (f) => f.from.query && f.from.id === a ? { ...f, from: { shared: c, alias: b.alias } } : {
|
|
89
89
|
...f,
|
|
90
90
|
joins: f.joins.map((I) => {
|
|
91
91
|
if (I.id !== a || !I.query) return I;
|
|
@@ -100,12 +100,12 @@ function Re(n, a) {
|
|
|
100
100
|
}
|
|
101
101
|
function Dr(n, a) {
|
|
102
102
|
const c = new Map((n.shared ?? []).map((r) => [r.id, r.query])), i = a.reduce(
|
|
103
|
-
(r,
|
|
103
|
+
(r, b) => typeof r == "object" && r !== null ? r[b] : void 0,
|
|
104
104
|
n
|
|
105
105
|
);
|
|
106
106
|
return !we(i) || !Se(i) ? n : ue(n, (r) => {
|
|
107
|
-
const
|
|
108
|
-
return r === i &&
|
|
107
|
+
const b = c.get(r.shared);
|
|
108
|
+
return r === i && b ? Re(r, b) : r;
|
|
109
109
|
});
|
|
110
110
|
}
|
|
111
111
|
function Fr(n, a, c) {
|
|
@@ -124,7 +124,7 @@ function qr(n, a, c = "subQueries") {
|
|
|
124
124
|
var d;
|
|
125
125
|
const i = (d = n.shared) == null ? void 0 : d.find((f) => f.id === a);
|
|
126
126
|
if (!i) return n;
|
|
127
|
-
const r = n.shared.filter((f) => f !== i), { shared:
|
|
127
|
+
const r = n.shared.filter((f) => f !== i), { shared: b, ...v } = n, l = r.length > 0 ? { ...v, shared: r } : v;
|
|
128
128
|
return ue(l, (f) => {
|
|
129
129
|
if (f.shared !== a) return f;
|
|
130
130
|
if (c === "subQueries") return Re(f, i.query);
|
|
@@ -150,10 +150,10 @@ function vr(n, a) {
|
|
|
150
150
|
return c.length > 0 && c.every((i) => n.select.some((r) => re(r.expression, i.expression) && a.includes(ye(r) ?? "")));
|
|
151
151
|
}
|
|
152
152
|
function wr(n, a) {
|
|
153
|
-
const c = X(n.from), i = /* @__PURE__ */ new Map([[c, n.from.table]]), r = [c],
|
|
153
|
+
const c = X(n.from), i = /* @__PURE__ */ new Map([[c, n.from.table]]), r = [c], b = n.shared ?? [];
|
|
154
154
|
return n.joins.map((v) => {
|
|
155
155
|
var w, E;
|
|
156
|
-
const { id: l } = v, d = Ge(v,
|
|
156
|
+
const { id: l } = v, d = Ge(v, b), f = X(v);
|
|
157
157
|
i.set(f, v.table);
|
|
158
158
|
const I = ge(v.on).flatMap(([T, O]) => [[T, O], [O, T]]).filter(([T, O]) => T.table === f && r.includes(O.table ?? ""));
|
|
159
159
|
if (r.push(f), v.joinType === "cross")
|
|
@@ -197,14 +197,14 @@ function wr(n, a) {
|
|
|
197
197
|
});
|
|
198
198
|
}
|
|
199
199
|
function pe(n, a, c = {}) {
|
|
200
|
-
const i = wr(n, a).map((l) => l.side === "unknown" && c.unknown === "repeat" ? { ...l, side: "many" } : l), r = [X(n.from), ...i.map((l) => l.key)],
|
|
200
|
+
const i = wr(n, a).map((l) => l.side === "unknown" && c.unknown === "repeat" ? { ...l, side: "many" } : l), r = [X(n.from), ...i.map((l) => l.key)], b = (l) => {
|
|
201
201
|
const d = /* @__PURE__ */ new Set([l]);
|
|
202
202
|
return i.forEach((f) => {
|
|
203
203
|
d.has(f.parent) && d.add(f.key);
|
|
204
204
|
}), d;
|
|
205
205
|
}, v = /* @__PURE__ */ new Set();
|
|
206
206
|
return i.forEach((l) => {
|
|
207
|
-
const d =
|
|
207
|
+
const d = b(l.key);
|
|
208
208
|
l.side === "many" && r.filter((f) => !d.has(f)).forEach((f) => v.add(f)), l.side === "one" && d.forEach((f) => v.add(f)), l.side === "cross" && r.forEach((f) => v.add(f));
|
|
209
209
|
}), v;
|
|
210
210
|
}
|
|
@@ -218,16 +218,16 @@ function Xr(n, a) {
|
|
|
218
218
|
}
|
|
219
219
|
function De(n, a, c = !1) {
|
|
220
220
|
const i = n.type === "function" && Sr.has(n.name.toUpperCase()) && !n.distinct, r = c || i;
|
|
221
|
-
return n.type === "field" ? r && n.table && a.has(n.table) ? [n.table] : [] : ve(n).flatMap((
|
|
221
|
+
return n.type === "field" ? r && n.table && a.has(n.table) ? [n.table] : [] : ve(n).flatMap((b) => De(b, a, r));
|
|
222
222
|
}
|
|
223
223
|
function kr(n, a) {
|
|
224
224
|
const c = pe(n, a), i = /* @__PURE__ */ new Set(), r = (d) => {
|
|
225
225
|
const f = d ? De(d, c) : [];
|
|
226
226
|
return f.forEach((I) => i.add(I)), f.length > 0;
|
|
227
|
-
},
|
|
227
|
+
}, b = n.select.filter((d) => r(d.expression)).map((d) => d.id), v = (n.orderBy ?? []).filter((d) => r(d.expression)).map((d) => d.id), l = r(n.having);
|
|
228
228
|
return {
|
|
229
229
|
tables: [...i],
|
|
230
|
-
select:
|
|
230
|
+
select: b,
|
|
231
231
|
orderBy: v,
|
|
232
232
|
having: l
|
|
233
233
|
};
|
|
@@ -237,13 +237,13 @@ function Ar(n) {
|
|
|
237
237
|
}
|
|
238
238
|
function Tr(n, a, c, i) {
|
|
239
239
|
const r = new Map(i);
|
|
240
|
-
return be(a).forEach((
|
|
240
|
+
return be(a).forEach((b) => {
|
|
241
241
|
var l;
|
|
242
|
-
const v =
|
|
243
|
-
const d = Ge(
|
|
242
|
+
const v = b.table !== void 0 ? (l = he(n, b.table)) == null ? void 0 : l.columns : (() => {
|
|
243
|
+
const d = Ge(b, c);
|
|
244
244
|
return d && _e(n, d, c);
|
|
245
245
|
})();
|
|
246
|
-
r.set(X(
|
|
246
|
+
r.set(X(b), new Set((v ?? []).filter((d) => d.joinOnly).map((d) => d.name)));
|
|
247
247
|
}), r;
|
|
248
248
|
}
|
|
249
249
|
function Nr(n, a) {
|
|
@@ -270,14 +270,14 @@ function Nr(n, a) {
|
|
|
270
270
|
return;
|
|
271
271
|
}
|
|
272
272
|
Object.entries(h).forEach(([E, T]) => r(T, [...d, E], f, I));
|
|
273
|
-
},
|
|
273
|
+
}, b = (l, d) => {
|
|
274
274
|
"union" in l ? Ue(l).forEach((f, I) => v(f, [...d, "union", I], !0)) : v(l, d, !0);
|
|
275
275
|
};
|
|
276
276
|
function v(l, d, f, I) {
|
|
277
277
|
var E, T, O;
|
|
278
278
|
const h = Tr(n, l, i, I);
|
|
279
|
-
l.from.query &&
|
|
280
|
-
C.query &&
|
|
279
|
+
l.from.query && b(l.from.query, [...d, "from", "query"]), l.joins.forEach((C, k) => {
|
|
280
|
+
C.query && b(C.query, [...d, "joins", k, "query"]), C.on && r(C.on, [...d, "joins", k, "on"], h, !0);
|
|
281
281
|
}), l.where && r(l.where, [...d, "where"], h, !1);
|
|
282
282
|
const w = l;
|
|
283
283
|
(E = w.select) == null || E.forEach((C, k) => r(
|
|
@@ -297,7 +297,7 @@ function Nr(n, a) {
|
|
|
297
297
|
!1
|
|
298
298
|
));
|
|
299
299
|
}
|
|
300
|
-
return i.forEach((l, d) =>
|
|
300
|
+
return i.forEach((l, d) => b(l.query, ["shared", d, "query"])), v(a, [], !1), c;
|
|
301
301
|
}
|
|
302
302
|
function Jr(n, a) {
|
|
303
303
|
return !(n === "mssql" && a === "REGEXP");
|
|
@@ -365,7 +365,7 @@ const le = (n) => n === "integer" ? "number" : n === "dateTime" ? "date" : n, z
|
|
|
365
365
|
dateTime: "a date and time",
|
|
366
366
|
time: "a time of day",
|
|
367
367
|
boolean: "yes / no"
|
|
368
|
-
}, x = (n) => typeof n == "object" && n !== null && !Array.isArray(n), U = (n) => typeof n == "string", Br = ["day", "week", "month", "year"], Or = (n) => x(n) && Br.includes(n.unit) && Number.isSafeInteger(n.offset) && (n.anchor === void 0 || n.unit !== "day" && (n.anchor === "start" || n.anchor === "end")), Y = (n) => n === void 0 || U(n),
|
|
368
|
+
}, x = (n) => typeof n == "object" && n !== null && !Array.isArray(n), U = (n) => typeof n == "string", Br = ["day", "week", "month", "year"], Or = (n) => x(n) && Br.includes(n.unit) && Number.isSafeInteger(n.offset) && (n.anchor === void 0 || n.unit !== "day" && (n.anchor === "start" || n.anchor === "end")), Y = (n) => n === void 0 || U(n), jr = (n) => n === null || U(n) || typeof n == "boolean" || typeof n == "bigint" || typeof n == "number" && Number.isFinite(n) || n instanceof Date && !Number.isNaN(n.getTime()), me = (n) => x(n) && n.type === "literal" && n.value === "" && n.empty !== !0;
|
|
369
369
|
function Kr(n, a, c = {}) {
|
|
370
370
|
var Te, Ne;
|
|
371
371
|
const i = [], r = (e, t, o, s = "error") => {
|
|
@@ -380,7 +380,7 @@ function Kr(n, a, c = {}) {
|
|
|
380
380
|
if (!x(a) || !U(a.id) || a.id === "")
|
|
381
381
|
return r("invalidStructure", "", "a query is an object with a non-empty string id"), i;
|
|
382
382
|
if (!("from" in a)) return i;
|
|
383
|
-
const
|
|
383
|
+
const b = Pe(n, c.tableFilter, c.columnFilter), v = He(c.functions ?? Ke);
|
|
384
384
|
let l = /* @__PURE__ */ new Map(), d = /* @__PURE__ */ new Map(), f;
|
|
385
385
|
const I = (e, t, o, s) => {
|
|
386
386
|
U(e) && e.length > t && r("tooLong", o, `${s} is longer than ${t} characters`);
|
|
@@ -396,12 +396,12 @@ function Kr(n, a, c = {}) {
|
|
|
396
396
|
return;
|
|
397
397
|
}
|
|
398
398
|
if (o && x(e) && e.shared !== void 0) {
|
|
399
|
-
const { shared:
|
|
400
|
-
if (!U(
|
|
399
|
+
const { shared: $, alias: u } = e, m = U(u) && u !== "";
|
|
400
|
+
if (!U($) || !m || e.table !== void 0) {
|
|
401
401
|
r("invalidStructure", t, "a shared query's place has a string `shared` and an `alias`, and no `table`"), m && (k(u, `${t}.alias`), l.set(u, void 0));
|
|
402
402
|
return;
|
|
403
403
|
}
|
|
404
|
-
I(u, K, `${t}.alias`, "an alias"), k(u, `${t}.alias`), O.add(
|
|
404
|
+
I(u, K, `${t}.alias`, "an alias"), k(u, `${t}.alias`), O.add($), E.has($) ? T.has($) || r("sharedOrder", `${t}.shared`, "a shared query can use only those before it") : r("unknownTable", `${t}.shared`, `no shared query "${$}"`), l.set(u, T.get($));
|
|
405
405
|
return;
|
|
406
406
|
}
|
|
407
407
|
if (!x(e) || !U(e.table) || !Y(e.alias)) {
|
|
@@ -409,13 +409,13 @@ function Kr(n, a, c = {}) {
|
|
|
409
409
|
return;
|
|
410
410
|
}
|
|
411
411
|
I(e.alias, K, `${t}.alias`, "an alias");
|
|
412
|
-
const s =
|
|
412
|
+
const s = b.tables.find(($) => $.name === e.table);
|
|
413
413
|
s || r("unknownTable", `${t}.table`, `unknown table "${e.table}"`), U(e.alias) && e.alias !== "" ? (k(e.alias, `${t}.alias`), l.set(e.alias, s), d.set(e.table, e.alias)) : (k(e.table, `${t}.table`), l.set(e.table, s));
|
|
414
414
|
}, q = (e, t, o) => {
|
|
415
415
|
var g;
|
|
416
416
|
if (!e) return;
|
|
417
|
-
const { args: s, name:
|
|
418
|
-
t < u ? r("incomplete", o, `${
|
|
417
|
+
const { args: s, name: $ } = e, u = s.filter((p) => !p.optional).length, m = (g = s[s.length - 1]) != null && g.variadic ? 1 / 0 : s.length;
|
|
418
|
+
t < u ? r("incomplete", o, `${$} needs ${u} argument(s)`) : t > m && r("argCount", o, `${$} takes ${m} argument(s)`);
|
|
419
419
|
};
|
|
420
420
|
let D, P, ne;
|
|
421
421
|
const de = (e) => {
|
|
@@ -425,71 +425,71 @@ function Kr(n, a, c = {}) {
|
|
|
425
425
|
return o && hr(o.type);
|
|
426
426
|
}, W = (e) => mr(e, de, v), V = c.typeMismatch ?? "warning", Z = (e, t, o) => {
|
|
427
427
|
if (e.type === "literal") {
|
|
428
|
-
if (e.value === null || e.value === "" && !e.empty || typeof e.value == "number" && !
|
|
428
|
+
if (e.value === null || e.value === "" && !e.empty || typeof e.value == "number" && !je(e.value) || yr(e.value, t) !== void 0) return;
|
|
429
429
|
const u = e.value instanceof Date ? "a date" : JSON.stringify(String(e.value));
|
|
430
430
|
r("typeMismatch", o, `${u} isn't ${z[t]}`, V);
|
|
431
431
|
return;
|
|
432
432
|
}
|
|
433
433
|
const s = W(e);
|
|
434
434
|
if (s === void 0 || le(s) === le(t)) return;
|
|
435
|
-
const
|
|
436
|
-
r("typeMismatch", o,
|
|
435
|
+
const $ = `${z[s]} compared with ${z[t]}`;
|
|
436
|
+
r("typeMismatch", o, $, V);
|
|
437
437
|
}, te = (e) => e && e.type !== "literal" ? W(e) : void 0, Fe = (e, t) => {
|
|
438
|
-
const { operator: o, left: s, right:
|
|
439
|
-
if (
|
|
440
|
-
const u = (g,
|
|
438
|
+
const { operator: o, left: s, right: $ } = e;
|
|
439
|
+
if (!$) return;
|
|
440
|
+
const u = (g, p, N) => {
|
|
441
441
|
if (g.type !== "param" || !g.relative) return;
|
|
442
|
-
const y = te(
|
|
442
|
+
const y = te(p);
|
|
443
443
|
if (y === void 0 || y === "date" || y === "dateTime") return;
|
|
444
444
|
const A = `a relative date compared with ${z[y]}`;
|
|
445
445
|
r("typeMismatch", N, A, V);
|
|
446
446
|
};
|
|
447
|
-
if (u(
|
|
447
|
+
if (u($, s, `${t}.right`), u(s, $, `${t}.left`), o === "LIKE" || o === "REGEXP") {
|
|
448
448
|
const g = te(s);
|
|
449
449
|
if (g !== void 0 && g !== "text") {
|
|
450
|
-
const
|
|
451
|
-
r("typeMismatch", `${t}.left`,
|
|
450
|
+
const p = `${o} matches text, not ${z[g]}`;
|
|
451
|
+
r("typeMismatch", `${t}.left`, p, V);
|
|
452
452
|
}
|
|
453
|
-
if (Z(
|
|
453
|
+
if (Z($, "text", `${t}.right`), o === "REGEXP" && $.type === "literal" && U($.value))
|
|
454
454
|
try {
|
|
455
|
-
new RegExp(
|
|
456
|
-
} catch (
|
|
457
|
-
const N = `not a regular expression: ${
|
|
455
|
+
new RegExp($.value);
|
|
456
|
+
} catch (p) {
|
|
457
|
+
const N = `not a regular expression: ${p.message}`;
|
|
458
458
|
r("invalidPattern", `${t}.right`, N);
|
|
459
459
|
}
|
|
460
460
|
return;
|
|
461
461
|
}
|
|
462
462
|
const m = te(s);
|
|
463
463
|
if (o === "IN" || o === "BETWEEN") {
|
|
464
|
-
if (m === void 0 ||
|
|
465
|
-
|
|
464
|
+
if (m === void 0 || $.type !== "list") return;
|
|
465
|
+
$.items.forEach((g, p) => Z(g, m, `${t}.right.items[${p}]`));
|
|
466
466
|
return;
|
|
467
467
|
}
|
|
468
|
-
if (m !== void 0) Z(
|
|
468
|
+
if (m !== void 0) Z($, m, `${t}.right`);
|
|
469
469
|
else {
|
|
470
|
-
const g = te(
|
|
470
|
+
const g = te($);
|
|
471
471
|
g !== void 0 && Z(s, g, `${t}.left`);
|
|
472
472
|
}
|
|
473
473
|
}, Ee = (e) => {
|
|
474
|
-
const t = e.map(([o]) => o.type === "literal" ?
|
|
474
|
+
const t = e.map(([o]) => o.type === "literal" ? Me(o) : W(o)).find((o) => o !== void 0);
|
|
475
475
|
t !== void 0 && e.forEach(([o, s]) => Z(o, t, s));
|
|
476
476
|
}, R = (e, t, o = !1) => {
|
|
477
477
|
if (!x(e) || !U(e.type)) {
|
|
478
478
|
r("invalidStructure", t, "an expression is an object with a string `type`");
|
|
479
479
|
return;
|
|
480
480
|
}
|
|
481
|
-
const s = (u) => r("invalidStructure", t, u),
|
|
481
|
+
const s = (u) => r("invalidStructure", t, u), $ = (u, m) => {
|
|
482
482
|
e[u] !== void 0 && R(e[u], `${t}.${u}`, m);
|
|
483
483
|
};
|
|
484
484
|
switch (o && e.type === "exists" && s("EXISTS is a condition, not a value"), Y(e.fromSelect) || s("`fromSelect` is a column's id"), e.type) {
|
|
485
485
|
case "literal":
|
|
486
|
-
if (!
|
|
486
|
+
if (!jr(e.value))
|
|
487
487
|
s("a literal value is a string, number, boolean, date or null");
|
|
488
488
|
else if (e.empty !== void 0 && typeof e.empty != "boolean")
|
|
489
489
|
s("a literal's `empty` is a boolean");
|
|
490
490
|
else if (me(e))
|
|
491
491
|
r("incomplete", t, "a value not filled in");
|
|
492
|
-
else if (typeof e.value == "number" && !
|
|
492
|
+
else if (typeof e.value == "number" && !je(e.value)) {
|
|
493
493
|
const u = `a number with more than ${fr} significant digits is written as another number`;
|
|
494
494
|
r("tooLong", `${t}.value`, u);
|
|
495
495
|
} else
|
|
@@ -526,9 +526,9 @@ function Kr(n, a, c = {}) {
|
|
|
526
526
|
const u = v[e.name.toUpperCase()];
|
|
527
527
|
if (c.dialect && (u != null && u.dialects) && !u.dialects.includes(c.dialect) && r("dialectUnsupported", `${t}.name`, `${u.name} isn't in this database`), I(e.name, K, `${t}.name`, "a function's name"), q(u, e.args.length, `${t}.args`), e.distinct !== void 0 && typeof e.distinct != "boolean" && s("a function's `distinct` is a boolean"), e.distinct === !0 && c.dialect === "mssql" && e.name.toUpperCase() === "STRING_AGG" && r("dialectUnsupported", t, "SQL Server's STRING_AGG can't join only different values"), e.args.forEach((m, g) => {
|
|
528
528
|
var y, A;
|
|
529
|
-
const
|
|
529
|
+
const p = i.length;
|
|
530
530
|
R(m, `${t}.args[${g}]`, !0), u && ((y = ce(u, g)) == null ? void 0 : y.kind) === "type-name" && x(m) && I(m.value, ur, `${t}.args[${g}].value`, "a type name"), u && ((A = ce(u, g)) == null ? void 0 : A.kind) === "fixed-text" && x(m) && !(m.type === "literal" && U(m.value)) && s(`${u.name}'s ${ce(u, g).label} is a text value`);
|
|
531
|
-
const N = !i.slice(
|
|
531
|
+
const N = !i.slice(p).some((Q) => Q.kind === "invalidStructure");
|
|
532
532
|
u != null && u.aggregate && N && oe(m, v) && r(
|
|
533
533
|
"misplacedAggregate",
|
|
534
534
|
`${t}.args[${g}]`,
|
|
@@ -540,15 +540,15 @@ function Kr(n, a, c = {}) {
|
|
|
540
540
|
break;
|
|
541
541
|
}
|
|
542
542
|
e.name.toUpperCase() !== "STRING_AGG" && s("only STRING_AGG takes an `orderBy`"), e.orderBy.forEach((m, g) => {
|
|
543
|
-
const
|
|
543
|
+
const p = `${t}.orderBy[${g}]`;
|
|
544
544
|
if (!x(m) || m.direction !== "asc" && m.direction !== "desc") {
|
|
545
|
-
r("invalidStructure",
|
|
545
|
+
r("invalidStructure", p, "a sort key has an `expression` and a `direction`");
|
|
546
546
|
return;
|
|
547
547
|
}
|
|
548
548
|
const N = i.length;
|
|
549
|
-
R(m.expression, `${
|
|
549
|
+
R(m.expression, `${p}.expression`, !0), !i.slice(N).some((y) => y.kind === "invalidStructure") && (oe(m.expression, v) && r("misplacedAggregate", `${p}.expression`, "an aggregate can't be inside another"), e.distinct === !0 && c.dialect === "postgres" && !re(m.expression, e.args[0]) && r(
|
|
550
550
|
"dialectUnsupported",
|
|
551
|
-
`${
|
|
551
|
+
`${p}.expression`,
|
|
552
552
|
"PostgreSQL orders STRING_AGG of different values by the value only"
|
|
553
553
|
));
|
|
554
554
|
});
|
|
@@ -569,8 +569,8 @@ function Kr(n, a, c = {}) {
|
|
|
569
569
|
else if (x(e.right) && e.right.type === "subquery")
|
|
570
570
|
e.operator === "IN" ? P(e.right, `${t}.right`) : r("invalidStructure", `${t}.right`, "a sub query is only IN's right side");
|
|
571
571
|
else if (e.operator === "BETWEEN") {
|
|
572
|
-
const { right: g } = e,
|
|
573
|
-
(
|
|
572
|
+
const { right: g } = e, p = x(g) && g.type === "list" && Array.isArray(g.items) ? g.items : void 0;
|
|
573
|
+
(p == null ? void 0 : p.length) !== 2 ? r("invalidStructure", `${t}.right`, "BETWEEN takes its two bounds as a list (expr.between)") : p.forEach((N, y) => {
|
|
574
574
|
const A = `${t}.right.items[${y}]`;
|
|
575
575
|
x(N) && N.type === "literal" && N.value === null ? r("incomplete", A, "a bound of NULL (never true)") : m && me(N) || R(N, A, !0);
|
|
576
576
|
});
|
|
@@ -596,24 +596,24 @@ function Kr(n, a, c = {}) {
|
|
|
596
596
|
e[m] === void 0 && r("incomplete", `${t}.${m}`, `an IF with no ${m}`);
|
|
597
597
|
});
|
|
598
598
|
const u = i.length;
|
|
599
|
-
|
|
599
|
+
$("condition", !1), $("then", !0), $("else", !0), i.slice(u).some((m) => m.kind === "invalidStructure") || Ee(
|
|
600
600
|
["then", "else"].filter((m) => e[m] !== void 0).map((m) => [e[m], `${t}.${m}`])
|
|
601
601
|
);
|
|
602
602
|
break;
|
|
603
603
|
}
|
|
604
604
|
case "case": {
|
|
605
605
|
const u = i.length;
|
|
606
|
-
|
|
606
|
+
$("subject", !0), Array.isArray(e.branches) ? (e.branches.length === 0 && r("incomplete", t, "a CASE with no WHEN"), e.branches.forEach((g, p) => {
|
|
607
607
|
if (!x(g)) {
|
|
608
|
-
const N = `${t}.branches[${
|
|
608
|
+
const N = `${t}.branches[${p}]`;
|
|
609
609
|
r("invalidStructure", N, "a branch has `when` and `then`");
|
|
610
610
|
return;
|
|
611
611
|
}
|
|
612
|
-
R(g.when, `${t}.branches[${
|
|
612
|
+
R(g.when, `${t}.branches[${p}].when`, e.subject !== void 0), R(g.then, `${t}.branches[${p}].then`, !0);
|
|
613
613
|
})) : s("a case node has a `branches` array");
|
|
614
614
|
const m = i.length;
|
|
615
|
-
|
|
616
|
-
...e.branches.map((g,
|
|
615
|
+
$("else", !0), Array.isArray(e.branches) && i.slice(u).concat(i.slice(m)).every((g) => g.kind !== "invalidStructure") && Ee([
|
|
616
|
+
...e.branches.map((g, p) => [g.then, `${t}.branches[${p}].then`]),
|
|
617
617
|
...e.else === void 0 ? [] : [[e.else, `${t}.else`]]
|
|
618
618
|
]);
|
|
619
619
|
break;
|
|
@@ -640,10 +640,10 @@ function Kr(n, a, c = {}) {
|
|
|
640
640
|
return;
|
|
641
641
|
}
|
|
642
642
|
h.has(o) && r("duplicateId", `${t}.id`, `the id "${o}" is used twice`), h.add(o);
|
|
643
|
-
const
|
|
643
|
+
const $ = { inQuery: l, aliasOf: d, outerTables: f };
|
|
644
644
|
l = /* @__PURE__ */ new Map(), d = /* @__PURE__ */ new Map(), f = void 0;
|
|
645
645
|
const u = i.length;
|
|
646
|
-
se(s, `${t}.query`, !1), { inQuery: l, aliasOf: d, outerTables: f } =
|
|
646
|
+
se(s, `${t}.query`, !1), { inQuery: l, aliasOf: d, outerTables: f } = $;
|
|
647
647
|
const m = i.length === u;
|
|
648
648
|
(Array.isArray(s.select) ? s.select : []).length !== 1 && r("subqueryColumns", `${t}.query.select`, "a sub query after IN picks exactly one column to compare with"), m && ie(s, `${t}.query`);
|
|
649
649
|
}, D = (e, t) => {
|
|
@@ -652,43 +652,45 @@ function Kr(n, a, c = {}) {
|
|
|
652
652
|
r("invalidStructure", `${t}.query`, "an EXISTS has a `query` with `from` and `joins`");
|
|
653
653
|
return;
|
|
654
654
|
}
|
|
655
|
-
const
|
|
656
|
-
l = /* @__PURE__ */ new Map(), d = /* @__PURE__ */ new Map(), f =
|
|
655
|
+
const $ = { inQuery: l, aliasOf: d, outerTables: f };
|
|
656
|
+
l = /* @__PURE__ */ new Map(), d = /* @__PURE__ */ new Map(), f = $.inQuery, [
|
|
657
657
|
[o.from, `${t}.query.from`],
|
|
658
|
-
...o.joins.map((
|
|
659
|
-
].forEach(([
|
|
658
|
+
...o.joins.map((p, N) => [p, `${t}.query.joins[${N}]`])
|
|
659
|
+
].forEach(([p, N], y) => {
|
|
660
660
|
const A = new Set(l.keys());
|
|
661
|
-
F(
|
|
661
|
+
F(p, N, y > 0), y > 0 && x(p) && L(p.joinType, `${N}.joinType`), [...l.keys()].filter((Q) => !A.has(Q) && $.inQuery.has(Q)).forEach((Q) => r(
|
|
662
662
|
"ambiguousTable",
|
|
663
663
|
`${N}.table`,
|
|
664
664
|
`"${Q}" is a table of the query around it too: give it an alias`
|
|
665
665
|
));
|
|
666
666
|
});
|
|
667
667
|
const m = [
|
|
668
|
-
...o.joins.map((
|
|
669
|
-
x(
|
|
668
|
+
...o.joins.map((p, N) => [
|
|
669
|
+
x(p) ? p.on : void 0,
|
|
670
670
|
`${t}.query.joins[${N}].on`
|
|
671
671
|
]),
|
|
672
672
|
[o.where, `${t}.query.where`]
|
|
673
673
|
];
|
|
674
|
-
m.forEach(([
|
|
675
|
-
|
|
674
|
+
m.forEach(([p, N]) => {
|
|
675
|
+
p !== void 0 && R(p, N);
|
|
676
|
+
}), o.joins.forEach((p, N) => {
|
|
677
|
+
x(p) && p.joinType !== "cross" && p.on === void 0 && r("incomplete", `${t}.query.joins[${N}].on`, "a join with no ON condition");
|
|
676
678
|
});
|
|
677
679
|
const g = { inQuery: l, aliasOf: d };
|
|
678
|
-
l = new Map([
|
|
679
|
-
x(
|
|
680
|
+
l = new Map([...$.inQuery, ...g.inQuery]), d = new Map([...$.aliasOf, ...g.aliasOf]), f = void 0, R(s, `${t}.link`), { inQuery: l, aliasOf: d, outerTables: f } = $, [...m, [s, `${t}.link`]].forEach(([p, N]) => {
|
|
681
|
+
x(p) && oe(p, v) && r("misplacedAggregate", N, "an aggregate (SUM, COUNT, …) can't be used here");
|
|
680
682
|
});
|
|
681
683
|
};
|
|
682
684
|
const ee = (e, t, o = []) => {
|
|
683
685
|
const s = new Set(o);
|
|
684
|
-
e.forEach((
|
|
685
|
-
!x(
|
|
686
|
+
e.forEach(($, u) => {
|
|
687
|
+
!x($) || !U($.id) || $.id === "" ? r("invalidStructure", `${t}[${u}]`, "every entry has a non-empty string id") : s.has($.id) ? r("duplicateId", `${t}[${u}].id`, `id "${$.id}" is used twice`) : s.add($.id);
|
|
686
688
|
});
|
|
687
689
|
}, ke = (e, t, o) => Array.isArray(e[t]) ? e[t] : (e[t] !== void 0 && r("invalidStructure", o, `\`${t}\` is an array`), []), se = (e, t, o) => {
|
|
688
690
|
const s = (y) => t ? `${t}.${y}` : y;
|
|
689
691
|
F(e.from, s("from"), !0);
|
|
690
|
-
const { from:
|
|
691
|
-
x(
|
|
692
|
+
const { from: $ } = e, u = x($) && $.query !== void 0 ? $.id : void 0;
|
|
693
|
+
x($) && $.query !== void 0 && (!U(u) || u === "") && r("invalidStructure", s("from"), "a derived table has a non-empty string id");
|
|
692
694
|
const m = Array.isArray(e.joins) ? e.joins : [];
|
|
693
695
|
Array.isArray(e.joins) || r("invalidStructure", s("joins"), "`joins` is an array"), m.forEach((y, A) => F(y, s(`joins[${A}]`), !0)), ee(m, s("joins"), U(u) ? [u] : []), m.forEach((y, A) => {
|
|
694
696
|
if (x(y)) {
|
|
@@ -705,8 +707,8 @@ function Kr(n, a, c = {}) {
|
|
|
705
707
|
Array.isArray(e.select) || r("invalidStructure", s("select"), "`select` is an array"), ee(g, s("select")), g.forEach((y, A) => {
|
|
706
708
|
x(y) && (I(y.alias, K, s(`select[${A}].alias`), "an alias"), Y(y.alias) || r("invalidStructure", s(`select[${A}].alias`), "an alias is a string"), R(y.expression, s(`select[${A}].expression`), !0));
|
|
707
709
|
});
|
|
708
|
-
const
|
|
709
|
-
if (ee(
|
|
710
|
+
const p = ke(e, "groupBy", s("groupBy"));
|
|
711
|
+
if (ee(p, s("groupBy")), p.forEach((y, A) => {
|
|
710
712
|
x(y) && (Y(y.fromSelect) || r("invalidStructure", s(`groupBy[${A}].fromSelect`), "`fromSelect` is a column's id"), R(y.expression, s(`groupBy[${A}].expression`), !0));
|
|
711
713
|
}), e.having !== void 0 && R(e.having, s("having")), e.distinct !== void 0 && typeof e.distinct != "boolean" && r("invalidStructure", s("distinct"), "`distinct` is a boolean"), !o) {
|
|
712
714
|
["orderBy", "limit", "offset", "shared"].forEach((y) => {
|
|
@@ -722,63 +724,63 @@ function Kr(n, a, c = {}) {
|
|
|
722
724
|
A !== void 0 && !(Number.isInteger(A) && A >= 0) ? r("invalidStructure", y, `\`${y}\` is a non-negative integer`) : A !== void 0 && !Number.isSafeInteger(A) && r("tooLong", y, `\`${y}\` is past ${Number.MAX_SAFE_INTEGER}, which is exact`);
|
|
723
725
|
});
|
|
724
726
|
}, qe = (e, t, o) => {
|
|
725
|
-
const s = `${t}.query`, { union:
|
|
726
|
-
if (!Array.isArray(
|
|
727
|
+
const s = `${t}.query`, { union: $ } = e;
|
|
728
|
+
if (!Array.isArray($) || $.length < 1 || typeof e.all != "boolean") {
|
|
727
729
|
r("invalidStructure", s, "a union has one or more branches in `union`, and a boolean `all`"), k(o, `${t}.alias`), l.set(o, void 0);
|
|
728
730
|
return;
|
|
729
731
|
}
|
|
730
732
|
let u = [];
|
|
731
733
|
const m = [];
|
|
732
734
|
let g = !0;
|
|
733
|
-
|
|
735
|
+
$.forEach((p, N) => {
|
|
734
736
|
const y = `${s}.union[${N}]`;
|
|
735
|
-
if (!x(
|
|
737
|
+
if (!x(p)) {
|
|
736
738
|
r("invalidStructure", y, "a union's branch is a query"), g = !1;
|
|
737
739
|
return;
|
|
738
740
|
}
|
|
739
741
|
const A = { inQuery: l, aliasOf: d, outerTables: f };
|
|
740
742
|
l = /* @__PURE__ */ new Map(), d = /* @__PURE__ */ new Map(), f = void 0;
|
|
741
743
|
const Q = i.length;
|
|
742
|
-
se(
|
|
743
|
-
const H = i.length === Q, S = (Array.isArray(
|
|
744
|
+
se(p, y, !1);
|
|
745
|
+
const H = i.length === Q, S = (Array.isArray(p.select) ? p.select : []).filter(x), B = H ? S.map((j) => j.expression.type === "literal" ? Me(j.expression) : W(j.expression)) : [];
|
|
744
746
|
if ({ inQuery: l, aliasOf: d, outerTables: f } = A, !H) {
|
|
745
747
|
g = !1;
|
|
746
748
|
return;
|
|
747
749
|
}
|
|
748
|
-
ie(
|
|
749
|
-
const G = S.map((
|
|
750
|
+
ie(p, y);
|
|
751
|
+
const G = S.map((j) => ye(j));
|
|
750
752
|
if (S.length === 0) {
|
|
751
753
|
r("unionColumns", `${y}.select`, "a union's branch names its columns"), g = !1;
|
|
752
754
|
return;
|
|
753
755
|
}
|
|
754
756
|
if (N === 0)
|
|
755
|
-
u = G, G.forEach((
|
|
756
|
-
if (
|
|
757
|
-
r("derivedColumnName", `${y}.select[${
|
|
758
|
-
}), G.forEach((
|
|
759
|
-
|
|
757
|
+
u = G, G.forEach((j, M) => {
|
|
758
|
+
if (j) return;
|
|
759
|
+
r("derivedColumnName", `${y}.select[${M}]`, "a union's column needs an alias: nothing outside could name it");
|
|
760
|
+
}), G.forEach((j, M) => {
|
|
761
|
+
j && G.indexOf(j) !== M && r("derivedColumnName", `${y}.select[${M}]`, `two columns named "${j}"`);
|
|
760
762
|
});
|
|
761
763
|
else if (G.length !== u.length) {
|
|
762
|
-
const
|
|
763
|
-
r("unionColumns", `${y}.select`,
|
|
764
|
+
const j = `${G.length} column(s), where the first branch has ${u.length}`;
|
|
765
|
+
r("unionColumns", `${y}.select`, j);
|
|
764
766
|
return;
|
|
765
767
|
} else
|
|
766
|
-
G.forEach((
|
|
767
|
-
if (
|
|
768
|
-
const _ = `column ${
|
|
769
|
-
r("unionColumns", `${y}.select[${
|
|
768
|
+
G.forEach((j, M) => {
|
|
769
|
+
if (j === u[M]) return;
|
|
770
|
+
const _ = `column ${M + 1} is "${String(u[M])}" in the first branch, not "${String(j)}"`;
|
|
771
|
+
r("unionColumns", `${y}.select[${M}]`, _);
|
|
770
772
|
});
|
|
771
|
-
B.forEach((
|
|
772
|
-
if (
|
|
773
|
-
const _ = m[
|
|
773
|
+
B.forEach((j, M) => {
|
|
774
|
+
if (j === void 0) return;
|
|
775
|
+
const _ = m[M];
|
|
774
776
|
if (_ === void 0)
|
|
775
|
-
m[
|
|
776
|
-
else if (le(_) !== le(
|
|
777
|
-
const J = `column "${String(u[
|
|
778
|
-
r("unionTypes", `${y}.select[${
|
|
777
|
+
m[M] = j;
|
|
778
|
+
else if (le(_) !== le(j)) {
|
|
779
|
+
const J = `column "${String(u[M])}" is ${z[j]} here, ${z[_]} in a branch before`;
|
|
780
|
+
r("unionTypes", `${y}.select[${M}].expression`, J);
|
|
779
781
|
}
|
|
780
782
|
});
|
|
781
|
-
}), k(o, `${t}.alias`), l.set(o, g ? u.filter((
|
|
783
|
+
}), k(o, `${t}.alias`), l.set(o, g ? u.filter((p) => p !== void 0) : void 0);
|
|
782
784
|
};
|
|
783
785
|
C = (e, t) => {
|
|
784
786
|
const { query: o, alias: s } = e;
|
|
@@ -791,20 +793,20 @@ function Kr(n, a, c = {}) {
|
|
|
791
793
|
return;
|
|
792
794
|
}
|
|
793
795
|
I(s, K, `${t}.alias`, "an alias");
|
|
794
|
-
const
|
|
796
|
+
const $ = { inQuery: l, aliasOf: d, outerTables: f };
|
|
795
797
|
l = /* @__PURE__ */ new Map(), d = /* @__PURE__ */ new Map(), f = void 0;
|
|
796
798
|
const u = i.length;
|
|
797
|
-
se(o, `${t}.query`, !1), { inQuery: l, aliasOf: d, outerTables: f } =
|
|
798
|
-
const m = Array.isArray(o.select) ? o.select : [], g = [],
|
|
799
|
+
se(o, `${t}.query`, !1), { inQuery: l, aliasOf: d, outerTables: f } = $;
|
|
800
|
+
const m = Array.isArray(o.select) ? o.select : [], g = [], p = (A, Q) => {
|
|
799
801
|
g.includes(A) ? r("derivedColumnName", Q, `two columns named "${A}"`) : g.push(A);
|
|
800
802
|
};
|
|
801
803
|
m.forEach((A, Q) => {
|
|
802
804
|
if (!x(A) || !x(A.expression)) return;
|
|
803
805
|
const H = ye(A);
|
|
804
|
-
H ?
|
|
806
|
+
H ? p(H, `${t}.query.select[${Q}]`) : r("derivedColumnName", `${t}.query.select[${Q}]`, "a derived table's column needs an alias: nothing outside could name it");
|
|
805
807
|
});
|
|
806
808
|
const N = i.length === u;
|
|
807
|
-
N && m.length === 0 && _e(
|
|
809
|
+
N && m.length === 0 && _e(b, o, w).forEach((A) => p(A.name, `${t}.query.select`)), k(s, `${t}.alias`), l.set(s, N || m.length > 0 ? g : void 0), N && ie(o, `${t}.query`);
|
|
808
810
|
const { drillAcross: y } = e;
|
|
809
811
|
y !== void 0 && !(x(y) && typeof y.synced == "boolean" && Number.isInteger(y.copied) && y.copied >= 0) && r("invalidStructure", `${t}.drillAcross`, "`drillAcross` has a boolean `synced` and a non-negative integer `copied`");
|
|
810
812
|
};
|
|
@@ -819,26 +821,26 @@ function Kr(n, a, c = {}) {
|
|
|
819
821
|
`the join condition of "${s.table}" uses "${s.needs}", which is joined after it`
|
|
820
822
|
);
|
|
821
823
|
}
|
|
822
|
-
const
|
|
824
|
+
const $ = (S, B) => {
|
|
823
825
|
oe(S, v) && r("misplacedAggregate", B, "an aggregate (SUM, COUNT, …) can't be used here");
|
|
824
826
|
};
|
|
825
|
-
|
|
826
|
-
S.joinType !== "cross" &&
|
|
827
|
-
}), (N = e.groupBy) == null || N.forEach((S, B) =>
|
|
827
|
+
$(e.where, o("where")), e.joins.forEach((S, B) => {
|
|
828
|
+
S.joinType !== "cross" && $(S.on, o(`joins[${B}].on`));
|
|
829
|
+
}), (N = e.groupBy) == null || N.forEach((S, B) => $(S.expression, o(`groupBy[${B}].expression`)));
|
|
828
830
|
const u = We(e, v), m = "isn't a group key or an aggregate (SUM, COUNT, …)";
|
|
829
831
|
if (u.selectAll && r("notGrouped", o("select"), "a grouped query has to pick its columns, not *"), e.select.forEach((S, B) => {
|
|
830
832
|
u.select.includes(S.id) && r("notGrouped", o(`select[${B}].expression`), `this column ${m}`);
|
|
831
833
|
}), (y = e.orderBy) == null || y.forEach((S, B) => {
|
|
832
834
|
u.orderBy.includes(S.id) && r("notGrouped", `orderBy[${B}].expression`, `this sort key ${m}`);
|
|
833
835
|
}), u.having && r("notGrouped", o("having"), `a column in this condition ${m}`), c.dialect === "mssql") {
|
|
834
|
-
const S = [], B = (
|
|
835
|
-
const [J, ae] =
|
|
836
|
+
const S = [], B = (M, _) => {
|
|
837
|
+
const [J, ae] = M.length <= _.length ? [M, _] : [_, M];
|
|
836
838
|
return J.every((xe, Ie) => xe.direction === ae[Ie].direction && re(xe.expression, ae[Ie].expression));
|
|
837
|
-
}, G = (
|
|
839
|
+
}, G = (M) => {
|
|
838
840
|
var J;
|
|
839
|
-
return [...
|
|
840
|
-
},
|
|
841
|
-
G(
|
|
841
|
+
return [...M.type === "function" && ((J = M.orderBy) != null && J.length) ? [M.orderBy] : [], ...ve(M).flatMap(G)];
|
|
842
|
+
}, j = (M, _) => {
|
|
843
|
+
G(M).forEach((J) => {
|
|
842
844
|
S.some((ae) => !B(ae, J)) && r(
|
|
843
845
|
"dialectUnsupported",
|
|
844
846
|
_,
|
|
@@ -846,7 +848,7 @@ function Kr(n, a, c = {}) {
|
|
|
846
848
|
), S.push(J);
|
|
847
849
|
});
|
|
848
850
|
};
|
|
849
|
-
e.select.forEach((
|
|
851
|
+
e.select.forEach((M, _) => j(M.expression, o(`select[${_}].expression`))), e.having && j(e.having, o("having")), (A = e.orderBy) == null || A.forEach((M, _) => j(M.expression, `orderBy[${_}].expression`));
|
|
850
852
|
}
|
|
851
853
|
if (c.allowUnknownFunctions && Be(e, v)) {
|
|
852
854
|
const S = (B, G) => {
|
|
@@ -861,23 +863,23 @@ function Kr(n, a, c = {}) {
|
|
|
861
863
|
}
|
|
862
864
|
e.joins.forEach((S, B) => {
|
|
863
865
|
if (S.joinType === "cross" || !S.on) return;
|
|
864
|
-
const G = ir(S.on),
|
|
865
|
-
G.some((_) => _ !==
|
|
866
|
+
const G = ir(S.on), j = X(S), M = be(e).slice(0, B + 1).map(X);
|
|
867
|
+
G.some((_) => _ !== j && !M.includes(_)) || G.includes(j) && G.some((_) => M.includes(_)) || r(
|
|
866
868
|
"unlinkedJoin",
|
|
867
869
|
o(`joins[${B}].on`),
|
|
868
|
-
`the join condition of "${
|
|
870
|
+
`the join condition of "${j}" doesn't compare it with a table joined before it: every row meets every row it lets through`,
|
|
869
871
|
"warning"
|
|
870
872
|
);
|
|
871
873
|
}), e.joins.forEach((S, B) => {
|
|
872
|
-
var
|
|
873
|
-
if (!S.query || !((
|
|
874
|
-
ar(
|
|
874
|
+
var j;
|
|
875
|
+
if (!S.query || !((j = S.drillAcross) != null && j.synced)) return;
|
|
876
|
+
ar(b, e, S) || r(
|
|
875
877
|
"drillAcrossOutOfStep",
|
|
876
878
|
o(`joins[${B}]`),
|
|
877
879
|
`"${S.alias}" is kept in step with the query but doesn't match its grouping`
|
|
878
880
|
);
|
|
879
881
|
const G = $e(S.query) ? void 0 : S.query.from.table;
|
|
880
|
-
G !== void 0 && or(
|
|
882
|
+
G !== void 0 && or(b, e, G) && r(
|
|
881
883
|
"drillAcrossFilterMixed",
|
|
882
884
|
o(`joins[${B}]`),
|
|
883
885
|
`a condition of the query names both what "${S.alias}" shares and what it doesn't: it isn't applied to it`,
|
|
@@ -891,16 +893,16 @@ function Kr(n, a, c = {}) {
|
|
|
891
893
|
"warning"
|
|
892
894
|
);
|
|
893
895
|
});
|
|
894
|
-
const g = kr(e,
|
|
896
|
+
const g = kr(e, b), p = (S) => r(
|
|
895
897
|
"fanOut",
|
|
896
898
|
S,
|
|
897
899
|
`${g.tables.map((B) => `"${B}"`).join(", ")} rows repeat in this join: a sum, average or count of their columns comes out too high`,
|
|
898
900
|
"warning"
|
|
899
901
|
);
|
|
900
902
|
e.select.forEach((S, B) => {
|
|
901
|
-
g.select.includes(S.id) &&
|
|
902
|
-
}), g.having &&
|
|
903
|
-
g.orderBy.includes(S.id) &&
|
|
903
|
+
g.select.includes(S.id) && p(o(`select[${B}].expression`));
|
|
904
|
+
}), g.having && p(o("having")), (H = e.orderBy) == null || H.forEach((S, B) => {
|
|
905
|
+
g.orderBy.includes(S.id) && p(`orderBy[${B}].expression`);
|
|
904
906
|
});
|
|
905
907
|
};
|
|
906
908
|
a.shared !== void 0 && !Array.isArray(a.shared) && r("invalidStructure", "shared", "`shared` is an array"), ee(w, "shared");
|
|
@@ -909,9 +911,9 @@ function Kr(n, a, c = {}) {
|
|
|
909
911
|
if (!x(e)) return;
|
|
910
912
|
const o = `shared[${t}]`, { name: s } = e;
|
|
911
913
|
!U(s) || s === "" ? r("invalidStructure", `${o}.name`, "a shared query has a non-empty string `name`") : (I(s, K, `${o}.name`, "a name"), Ae.has(s) && r("duplicateTableName", `${o}.name`, `two shared queries named "${s}"`), Ae.add(s));
|
|
912
|
-
const
|
|
913
|
-
l = /* @__PURE__ */ new Map(), d = /* @__PURE__ */ new Map(), f = void 0, C({ query: e.query, alias:
|
|
914
|
-
const m = l.get(
|
|
914
|
+
const $ = U(s) && s !== "" ? s : o, u = { inQuery: l, aliasOf: d, outerTables: f };
|
|
915
|
+
l = /* @__PURE__ */ new Map(), d = /* @__PURE__ */ new Map(), f = void 0, C({ query: e.query, alias: $ }, o);
|
|
916
|
+
const m = l.get($);
|
|
915
917
|
({ inQuery: l, aliasOf: d, outerTables: f } = u), U(e.id) && T.set(e.id, Array.isArray(m) ? m : void 0);
|
|
916
918
|
}), se(a, "", !0), i.length === 0) {
|
|
917
919
|
const e = a;
|
|
@@ -919,7 +921,7 @@ function Kr(n, a, c = {}) {
|
|
|
919
921
|
const { max: o } = c.requireLimit === !0 ? {} : c.requireLimit, s = Be(e, v) && !((Te = e.groupBy) != null && Te.length);
|
|
920
922
|
e.limit === void 0 && !s ? r("noLimit", "limit", "no LIMIT: the query reads every row", "warning") : o !== void 0 && e.limit !== void 0 && e.limit > o && r("limitTooHigh", "limit", `LIMIT ${e.limit} is above ${o}`, "warning");
|
|
921
923
|
}
|
|
922
|
-
nr(e,
|
|
924
|
+
nr(e, b).forEach((o) => {
|
|
923
925
|
r("paramConflict", "", `param "${o}" is compared against columns of different types`);
|
|
924
926
|
}), tr(e).forEach((o) => {
|
|
925
927
|
r("paramConflict", "", `param "${o}" stands for two different relative dates`);
|
|
@@ -953,7 +955,7 @@ function zr({ unit: n, offset: a, anchor: c }) {
|
|
|
953
955
|
const i = a < 0 ? `minus_${-a}` : `plus_${a}`;
|
|
954
956
|
return [n, a === 0 ? "0" : i, c].filter(Boolean).join("_");
|
|
955
957
|
}
|
|
956
|
-
const
|
|
958
|
+
const Mr = {
|
|
957
959
|
from: "from",
|
|
958
960
|
joins: "from",
|
|
959
961
|
where: "where",
|
|
@@ -966,7 +968,7 @@ const jr = {
|
|
|
966
968
|
offset: "order"
|
|
967
969
|
};
|
|
968
970
|
function Vr(n, a, c = {}) {
|
|
969
|
-
const { schema: i, functions: r } = c,
|
|
971
|
+
const { schema: i, functions: r } = c, b = (h, w) => typeof w == "number" ? h == null ? void 0 : h[w] : void 0, v = (h) => {
|
|
970
972
|
var w;
|
|
971
973
|
return h.alias || i && h.table !== void 0 && ((w = he(i, h.table)) == null ? void 0 : w.label) || h.table || "";
|
|
972
974
|
}, l = (h, w) => {
|
|
@@ -975,7 +977,7 @@ function Vr(n, a, c = {}) {
|
|
|
975
977
|
const [E, T, O] = w;
|
|
976
978
|
switch (h.type) {
|
|
977
979
|
case "logical": {
|
|
978
|
-
const k =
|
|
980
|
+
const k = b(h.operands, T);
|
|
979
981
|
return k ? [{ kind: "condition", number: T + 1 }, ...l(k, w.slice(2))] : [];
|
|
980
982
|
}
|
|
981
983
|
case "not":
|
|
@@ -983,11 +985,11 @@ function Vr(n, a, c = {}) {
|
|
|
983
985
|
case "binary": {
|
|
984
986
|
if (E !== "left" && E !== "right") return [];
|
|
985
987
|
const k = { kind: "side", side: E }, L = h[E];
|
|
986
|
-
return (L == null ? void 0 : L.type) === "subquery" && T === "query" ? [k, { kind: "inSubQuery" }, ...I(L.query, w.slice(2))] : (L == null ? void 0 : L.type) === "list" && T === "items" &&
|
|
988
|
+
return (L == null ? void 0 : L.type) === "subquery" && T === "query" ? [k, { kind: "inSubQuery" }, ...I(L.query, w.slice(2))] : (L == null ? void 0 : L.type) === "list" && T === "items" && b(L.items, O) ? [k, { kind: "listItem", number: O + 1 }] : [k, ...l(L, w.slice(1))];
|
|
987
989
|
}
|
|
988
990
|
case "function": {
|
|
989
991
|
const k = { kind: "function", name: h.name };
|
|
990
|
-
if (E === "args" &&
|
|
992
|
+
if (E === "args" && b(h.args, T)) {
|
|
991
993
|
const F = Ve(h.name, r), q = F && ((C = ce(F, T)) == null ? void 0 : C.label);
|
|
992
994
|
return [
|
|
993
995
|
k,
|
|
@@ -995,7 +997,7 @@ function Vr(n, a, c = {}) {
|
|
|
995
997
|
...l(h.args[T], w.slice(2))
|
|
996
998
|
];
|
|
997
999
|
}
|
|
998
|
-
const L = E === "orderBy" ?
|
|
1000
|
+
const L = E === "orderBy" ? b(h.orderBy, T) : void 0;
|
|
999
1001
|
return L ? [
|
|
1000
1002
|
k,
|
|
1001
1003
|
{ kind: "sortKey", number: T + 1 },
|
|
@@ -1007,7 +1009,7 @@ function Vr(n, a, c = {}) {
|
|
|
1007
1009
|
case "case": {
|
|
1008
1010
|
if (E === "subject" || E === "else")
|
|
1009
1011
|
return [{ kind: "case", part: E }, ...l(h[E], w.slice(1))];
|
|
1010
|
-
const k = E === "branches" ?
|
|
1012
|
+
const k = E === "branches" ? b(h.branches, T) : void 0;
|
|
1011
1013
|
return !k || O !== "when" && O !== "then" ? [] : [
|
|
1012
1014
|
{ kind: "case", part: O, branch: T + 1 },
|
|
1013
1015
|
...l(k[O], w.slice(3))
|
|
@@ -1028,14 +1030,14 @@ function Vr(n, a, c = {}) {
|
|
|
1028
1030
|
function f(h, w, E, T = "subQuery") {
|
|
1029
1031
|
const O = { kind: T, name: h };
|
|
1030
1032
|
if (!$e(w)) return [O, ...I(w, E)];
|
|
1031
|
-
const C = E[0] === "union" ?
|
|
1033
|
+
const C = E[0] === "union" ? b(Ue(w), E[1]) : void 0;
|
|
1032
1034
|
if (!C) return [O];
|
|
1033
1035
|
const k = E[1] + 1;
|
|
1034
1036
|
return [O, { kind: "unionQuery", number: k }, ...I(C, E.slice(2))];
|
|
1035
1037
|
}
|
|
1036
1038
|
function I(h, w) {
|
|
1037
1039
|
var F, q;
|
|
1038
|
-
const [E, T, O] = w, C =
|
|
1040
|
+
const [E, T, O] = w, C = Mr[String(E)];
|
|
1039
1041
|
if (!C) return [];
|
|
1040
1042
|
const k = { kind: "section", section: C }, L = w.slice(3);
|
|
1041
1043
|
switch (E) {
|
|
@@ -1043,14 +1045,14 @@ function Vr(n, a, c = {}) {
|
|
|
1043
1045
|
case "from":
|
|
1044
1046
|
return h.from.query ? T === "query" ? f(h.from.alias, h.from.query, w.slice(2)) : [{ kind: "subQuery", name: h.from.alias }] : [k, { kind: "table", name: v(h.from) }];
|
|
1045
1047
|
case "joins": {
|
|
1046
|
-
const D =
|
|
1048
|
+
const D = b(h.joins, T);
|
|
1047
1049
|
return D ? D.query ? d(D, w.slice(2)) : [k, ...d(D, w.slice(2))] : [k];
|
|
1048
1050
|
}
|
|
1049
1051
|
case "where":
|
|
1050
1052
|
case "having":
|
|
1051
1053
|
return [k, ...l(h[E], w.slice(1))];
|
|
1052
1054
|
case "select": {
|
|
1053
|
-
const D =
|
|
1055
|
+
const D = b(h.select, T);
|
|
1054
1056
|
if (!D) return [k];
|
|
1055
1057
|
const { expression: P, alias: ne } = D, de = P.type === "field" && i && P.table !== void 0 ? (q = (F = he(i, P.table)) == null ? void 0 : F.columns.find((V) => V.name === P.column)) == null ? void 0 : q.label : void 0, W = ne || de;
|
|
1056
1058
|
return [
|
|
@@ -1061,7 +1063,7 @@ function Vr(n, a, c = {}) {
|
|
|
1061
1063
|
}
|
|
1062
1064
|
case "groupBy":
|
|
1063
1065
|
case "orderBy": {
|
|
1064
|
-
const D =
|
|
1066
|
+
const D = b(h[E], T);
|
|
1065
1067
|
return D ? [
|
|
1066
1068
|
k,
|
|
1067
1069
|
{ kind: E === "groupBy" ? "groupKey" : "sortKey", number: T + 1 },
|
|
@@ -1073,7 +1075,7 @@ function Vr(n, a, c = {}) {
|
|
|
1073
1075
|
}
|
|
1074
1076
|
}
|
|
1075
1077
|
if (a[0] === "shared") {
|
|
1076
|
-
const h =
|
|
1078
|
+
const h = b(n.shared, a[1]);
|
|
1077
1079
|
return h ? a[2] === "query" ? f(h.name, h.query, a.slice(3), "sharedQuery") : [{ kind: "sharedQuery", name: h.name }] : [];
|
|
1078
1080
|
}
|
|
1079
1081
|
return I(n, a);
|