@nongt/visql 0.6.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -2
- package/dist/{PipelineEditor-X83xfPeC.js → PipelineEditor-DtT1TPgd.js} +4046 -3906
- package/dist/{core-api-D4QbGLG-.js → core-api-jaQDPXVK.js} +459 -429
- package/dist/core.js +3 -3
- package/dist/fixtures.js +1 -1
- package/dist/index.js +5 -5
- package/dist/{issueLocation-BcILIZqd.js → issueLocation-DEIwB1DW.js} +944 -932
- package/dist/mui.js +225 -207
- package/dist/{schema-z95dxLxu.js → schema-DFC26AhZ.js} +142 -126
- package/dist/sql-import.js +613 -627
- package/dist/style.css +1 -1
- package/dist/types/adapters/mui/components.d.ts +8 -1
- package/dist/types/core/ast.d.ts +1 -1
- package/dist/types/core/i18n/defaultMessages.d.ts +2 -0
- package/dist/types/core/i18n/messages.d.ts +1 -0
- package/dist/types/core/params.d.ts +10 -0
- package/dist/types/core/schema.d.ts +19 -0
- package/dist/types/core/sqlGen.d.ts +10 -0
- package/dist/types/core/tablePickerOptions.d.ts +75 -0
- package/dist/types/core/ui/index.d.ts +6 -0
- package/dist/types/core/ui/types.d.ts +16 -0
- package/dist/types/core-api.d.ts +3 -3
- package/dist/types/features/queryEditor/PipelineEditor.d.ts +15 -0
- package/dist/types/index.d.ts +1 -0
- package/package.json +1 -1
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { D as $e, h as ue, q as Q, t as D, i as H, s as
|
|
2
|
-
import { c as Yt, s as zt, b as
|
|
3
|
-
const Zt = 1e3,
|
|
1
|
+
import { D as $e, h as ue, q as Q, t as D, i as H, s as Kn, l as le, u as Yn, w as Pe, e as Ve, x as fn, k as z, r as fe, o as xe, j as K, y as zn, d as W, z as Jt, A as Be, B as me, C as Pt, M as Y, F as Zn, G as Vt, g as Z, H as We, I as mn, b as be, J as et, n as Wt, K as nt, f as Ht, L as Xt, N as On, O as Un, P as Ge, Q as Kt } from "./schema-DFC26AhZ.js";
|
|
2
|
+
import { c as Yt, s as zt, b as tt } from "./calendarDate-CYlr9TvX.js";
|
|
3
|
+
const Zt = 1e3, rt = 15, er = 40, ls = 100;
|
|
4
4
|
function nr(e) {
|
|
5
5
|
const [n] = e.replace(/^[+-]/, "").split(/e/i), [r, t = ""] = n.split(".");
|
|
6
6
|
return `${r}${t.replace(/0+$/, "")}`.replace(/^0+/, "").length;
|
|
7
7
|
}
|
|
8
|
-
function
|
|
9
|
-
return Number.isFinite(e) && nr(String(e)) <=
|
|
8
|
+
function tn(e) {
|
|
9
|
+
return Number.isFinite(e) && nr(String(e)) <= rt;
|
|
10
10
|
}
|
|
11
11
|
const us = [];
|
|
12
12
|
function oe(e) {
|
|
@@ -16,18 +16,18 @@ function Te(e, n = []) {
|
|
|
16
16
|
var r;
|
|
17
17
|
return e.query ? e.query : e.shared === void 0 || (r = n.find((t) => t.id === e.shared)) == null ? void 0 : r.query;
|
|
18
18
|
}
|
|
19
|
-
function
|
|
19
|
+
function st(e) {
|
|
20
20
|
const n = [], r = (t) => {
|
|
21
21
|
t.type === "subquery" ? n.push(t) : fe(t).forEach(r);
|
|
22
22
|
};
|
|
23
|
-
return
|
|
23
|
+
return Kn(e).forEach(r), n;
|
|
24
24
|
}
|
|
25
|
-
function
|
|
25
|
+
function it(e, n) {
|
|
26
26
|
let r = !1;
|
|
27
27
|
const t = (i) => {
|
|
28
28
|
if (i.type !== "subquery") return xe(i, t);
|
|
29
|
-
const
|
|
30
|
-
return
|
|
29
|
+
const l = n(i);
|
|
30
|
+
return l !== i && (r = !0), l;
|
|
31
31
|
}, s = Pe(e, t, (i) => i);
|
|
32
32
|
return r ? s : e;
|
|
33
33
|
}
|
|
@@ -40,28 +40,28 @@ const rr = (e) => ({
|
|
|
40
40
|
alias: e.name,
|
|
41
41
|
query: e.query
|
|
42
42
|
});
|
|
43
|
-
function
|
|
43
|
+
function jn(e, n) {
|
|
44
44
|
var s;
|
|
45
45
|
const r = oe(e).find((i) => i.id === n);
|
|
46
46
|
if (r) return r;
|
|
47
47
|
const t = (s = e.shared) == null ? void 0 : s.find((i) => i.id === n);
|
|
48
48
|
return t && rr(t);
|
|
49
49
|
}
|
|
50
|
-
function
|
|
51
|
-
const r =
|
|
50
|
+
function ot(e, n) {
|
|
51
|
+
const r = jn(e, n);
|
|
52
52
|
if (r) return H(r.query) ? void 0 : r;
|
|
53
|
-
const t =
|
|
53
|
+
const t = st(e).find((o) => o.id === n);
|
|
54
54
|
if (t) return t;
|
|
55
|
-
const s = /^(.*)#(\d+)$/.exec(n), i = s ?
|
|
56
|
-
return i &&
|
|
55
|
+
const s = /^(.*)#(\d+)$/.exec(n), i = s ? jn(e, s[1]) : void 0, l = Number(s == null ? void 0 : s[2]), a = i && H(i.query) ? i.query.union[l] : void 0;
|
|
56
|
+
return i && a ? {
|
|
57
57
|
id: n,
|
|
58
58
|
alias: i.alias,
|
|
59
|
-
query:
|
|
59
|
+
query: a,
|
|
60
60
|
of: i,
|
|
61
|
-
index:
|
|
61
|
+
index: l
|
|
62
62
|
} : void 0;
|
|
63
63
|
}
|
|
64
|
-
function
|
|
64
|
+
function at(e) {
|
|
65
65
|
return { id: e.id, ...Ve(e.query) };
|
|
66
66
|
}
|
|
67
67
|
function Ye(e) {
|
|
@@ -71,8 +71,8 @@ function Ye(e) {
|
|
|
71
71
|
where: t,
|
|
72
72
|
select: s,
|
|
73
73
|
groupBy: i,
|
|
74
|
-
having:
|
|
75
|
-
distinct:
|
|
74
|
+
having: l,
|
|
75
|
+
distinct: a
|
|
76
76
|
} = e;
|
|
77
77
|
return {
|
|
78
78
|
from: n,
|
|
@@ -80,29 +80,29 @@ function Ye(e) {
|
|
|
80
80
|
select: s,
|
|
81
81
|
...t !== void 0 ? { where: t } : {},
|
|
82
82
|
...i !== void 0 ? { groupBy: i } : {},
|
|
83
|
-
...
|
|
84
|
-
...
|
|
83
|
+
...l !== void 0 ? { having: l } : {},
|
|
84
|
+
...a !== void 0 ? { distinct: a } : {}
|
|
85
85
|
};
|
|
86
86
|
}
|
|
87
87
|
function sr(e, n) {
|
|
88
88
|
return n.reduce((r, t) => {
|
|
89
|
-
const s = r &&
|
|
90
|
-
return s ?
|
|
89
|
+
const s = r && ot(r, t);
|
|
90
|
+
return s ? at(s) : void 0;
|
|
91
91
|
}, e);
|
|
92
92
|
}
|
|
93
|
-
function
|
|
93
|
+
function yn(e, n, r) {
|
|
94
94
|
if (n.length === 0) return r(e);
|
|
95
|
-
const [t, ...s] = n, i =
|
|
95
|
+
const [t, ...s] = n, i = ot(e, t);
|
|
96
96
|
if (!i) return e;
|
|
97
|
-
const
|
|
98
|
-
if (
|
|
97
|
+
const l = at(i), a = yn(l, s, r);
|
|
98
|
+
if (a === l || !ue(a)) return e;
|
|
99
99
|
if ("type" in i && i.type === "subquery") {
|
|
100
|
-
const c = { ...i, query: Ye(
|
|
101
|
-
return
|
|
100
|
+
const c = { ...i, query: Ye(a) };
|
|
101
|
+
return it(e, (m) => m === i ? c : m);
|
|
102
102
|
}
|
|
103
|
-
const
|
|
103
|
+
const o = (c, m) => {
|
|
104
104
|
if (!oe(e).some((f) => f.id === c.id))
|
|
105
|
-
return
|
|
105
|
+
return fn(e, (f, h) => h.id === c.id ? m : f);
|
|
106
106
|
const d = { ...c, query: m };
|
|
107
107
|
return c === e.from ? { ...e, from: d } : {
|
|
108
108
|
...e,
|
|
@@ -111,12 +111,12 @@ function mn(e, n, r) {
|
|
|
111
111
|
};
|
|
112
112
|
if (tr(i)) {
|
|
113
113
|
const { of: c, index: m } = i, { union: d } = c.query;
|
|
114
|
-
return
|
|
114
|
+
return o(c, {
|
|
115
115
|
...c.query,
|
|
116
|
-
union: d.map((f,
|
|
116
|
+
union: d.map((f, h) => h === m ? Ye(a) : f)
|
|
117
117
|
});
|
|
118
118
|
}
|
|
119
|
-
return
|
|
119
|
+
return o(i, Ye(a));
|
|
120
120
|
}
|
|
121
121
|
const ir = {
|
|
122
122
|
text: "varchar",
|
|
@@ -130,103 +130,103 @@ const ir = {
|
|
|
130
130
|
function Ne(e, n, r = []) {
|
|
131
131
|
const t = Ve(n);
|
|
132
132
|
if (t.select.length === 0)
|
|
133
|
-
return Q(t).flatMap((
|
|
133
|
+
return Q(t).flatMap((a) => {
|
|
134
134
|
var c;
|
|
135
|
-
if (
|
|
136
|
-
const
|
|
137
|
-
return
|
|
135
|
+
if (a.table !== void 0) return ((c = z(e, a.table)) == null ? void 0 : c.columns) ?? [];
|
|
136
|
+
const o = Te(a, r);
|
|
137
|
+
return o ? Ne(e, o, r) : [];
|
|
138
138
|
});
|
|
139
|
-
const s = new Map(Q(t).map((
|
|
140
|
-
var
|
|
141
|
-
if (
|
|
142
|
-
const c =
|
|
139
|
+
const s = new Map(Q(t).map((a) => [D(a), a.table])), i = (a) => {
|
|
140
|
+
var o;
|
|
141
|
+
if (a.type === "field") {
|
|
142
|
+
const c = a.table === void 0 ? void 0 : s.get(a.table), m = c === void 0 || (o = z(e, c)) == null ? void 0 : o.columns.find((d) => d.name === a.column);
|
|
143
143
|
return (m == null ? void 0 : m.type) ?? "varchar";
|
|
144
144
|
}
|
|
145
|
-
if (
|
|
146
|
-
const c =
|
|
145
|
+
if (a.type === "function") {
|
|
146
|
+
const c = a.name.toUpperCase();
|
|
147
147
|
if (c === "COUNT") return "integer";
|
|
148
148
|
if (c === "SUM" || c === "AVG") return "decimal";
|
|
149
|
-
if ((c === "MIN" || c === "MAX") &&
|
|
149
|
+
if ((c === "MIN" || c === "MAX") && a.args[0]) return i(a.args[0]);
|
|
150
150
|
}
|
|
151
|
-
return
|
|
152
|
-
},
|
|
151
|
+
return a.type === "literal" && typeof a.value == "number" ? "decimal" : a.type === "literal" && a.as ? ir[a.as] : "varchar";
|
|
152
|
+
}, l = (a) => {
|
|
153
153
|
var d, f;
|
|
154
|
-
if (
|
|
155
|
-
const
|
|
156
|
-
if (!
|
|
157
|
-
const c =
|
|
158
|
-
return !!((f = m == null ? void 0 : m.find((
|
|
154
|
+
if (a.type !== "field" || a.table === void 0) return !1;
|
|
155
|
+
const o = Q(t).find((h) => D(h) === a.table);
|
|
156
|
+
if (!o) return !1;
|
|
157
|
+
const c = o.table === void 0 ? Te(o, r) : void 0, m = o.table !== void 0 ? (d = z(e, o.table)) == null ? void 0 : d.columns : c && Ne(e, c, r);
|
|
158
|
+
return !!((f = m == null ? void 0 : m.find((h) => h.name === a.column)) != null && f.joinOnly);
|
|
159
159
|
};
|
|
160
|
-
return t.select.flatMap((
|
|
161
|
-
const
|
|
162
|
-
return
|
|
163
|
-
name:
|
|
164
|
-
label:
|
|
165
|
-
type: i(
|
|
166
|
-
...a
|
|
160
|
+
return t.select.flatMap((a) => {
|
|
161
|
+
const o = K(a);
|
|
162
|
+
return o ? [{
|
|
163
|
+
name: o,
|
|
164
|
+
label: o,
|
|
165
|
+
type: i(a.expression),
|
|
166
|
+
...l(a.expression) ? { joinOnly: !0 } : {}
|
|
167
167
|
}] : [];
|
|
168
168
|
});
|
|
169
169
|
}
|
|
170
|
-
function
|
|
171
|
-
const t = Te(n, r), s = (t ? Ne(e, t, r) : []).filter((i,
|
|
170
|
+
function lt(e, n, r = []) {
|
|
171
|
+
const t = Te(n, r), s = (t ? Ne(e, t, r) : []).filter((i, l, a) => a.findIndex((o) => o.name === i.name) === l);
|
|
172
172
|
return { name: n.alias, label: n.alias, columns: s };
|
|
173
173
|
}
|
|
174
174
|
function ds(e, n, r = []) {
|
|
175
|
-
return Q(n).filter((t) => t.table === void 0).map((t) =>
|
|
175
|
+
return Q(n).filter((t) => t.table === void 0).map((t) => lt(e, t, r));
|
|
176
176
|
}
|
|
177
|
-
function
|
|
177
|
+
function ut(e, n = $e) {
|
|
178
178
|
if (H(e)) return !1;
|
|
179
179
|
const r = e;
|
|
180
|
-
return (r.groupBy ?? []).length === 0 && r.select.length > 0 && r.select.every((t) => le(t.expression, n)) &&
|
|
180
|
+
return (r.groupBy ?? []).length === 0 && r.select.length > 0 && r.select.every((t) => le(t.expression, n)) && Yn(r, n).select.length === 0;
|
|
181
181
|
}
|
|
182
|
-
function
|
|
183
|
-
const t = (s) => s.type === "field" ? s.table === n ? { ...s, table: r } : s : s.type === "exists" &&
|
|
182
|
+
function Ln(e, n, r) {
|
|
183
|
+
const t = (s) => s.type === "field" ? s.table === n ? { ...s, table: r } : s : s.type === "exists" && zn(s.query).has(n) ? { ...s, link: t(s.link) } : xe(s, t);
|
|
184
184
|
return Pe(e, t, (s) => s);
|
|
185
185
|
}
|
|
186
|
-
function
|
|
186
|
+
function ct(e, n) {
|
|
187
187
|
const r = Ve(n), t = Q(r).flatMap((s) => s.table === void 0 ? [] : [{ table: s.table, alias: s.alias }]);
|
|
188
188
|
return r.select.length === 0 ? t.flatMap(({ table: s }) => {
|
|
189
189
|
var i;
|
|
190
|
-
return (((i = z(e, s)) == null ? void 0 : i.columns) ?? []).map((
|
|
190
|
+
return (((i = z(e, s)) == null ? void 0 : i.columns) ?? []).map((l) => ({ name: l.name, table: s, column: l.name }));
|
|
191
191
|
}) : r.select.flatMap((s) => {
|
|
192
192
|
const { expression: i } = s;
|
|
193
193
|
if (i.type !== "field" || !i.column) return [];
|
|
194
|
-
const
|
|
195
|
-
return
|
|
194
|
+
const l = t.find((o) => D(o) === i.table), a = s.alias || i.column;
|
|
195
|
+
return l ? [{ name: a, table: l.table, column: i.column }] : [];
|
|
196
196
|
});
|
|
197
197
|
}
|
|
198
|
-
function
|
|
199
|
-
return oe(n).flatMap((r) =>
|
|
200
|
-
const
|
|
201
|
-
if (!
|
|
202
|
-
const
|
|
198
|
+
function dt(e, n) {
|
|
199
|
+
return oe(n).flatMap((r) => ct(e, r.query).flatMap(({ name: t, table: s, column: i }) => e.relations.flatMap((l) => {
|
|
200
|
+
const a = l.fromTable === s && l.fromColumn === i;
|
|
201
|
+
if (!a && !(l.toTable === s && l.toColumn === i)) return [];
|
|
202
|
+
const o = a ? l.toTable : l.fromTable, c = {
|
|
203
203
|
type: "binary",
|
|
204
204
|
operator: "=",
|
|
205
|
-
left: { type: "field", table:
|
|
205
|
+
left: { type: "field", table: o, column: a ? l.toColumn : l.fromColumn },
|
|
206
206
|
right: { type: "field", table: r.alias, column: t }
|
|
207
207
|
};
|
|
208
|
-
return [{ table:
|
|
208
|
+
return [{ table: o, on: c }];
|
|
209
209
|
})));
|
|
210
210
|
}
|
|
211
211
|
function or(e, n, r) {
|
|
212
212
|
var t;
|
|
213
|
-
return (t =
|
|
213
|
+
return (t = dt(e, n).find((s) => s.table === r)) == null ? void 0 : t.on;
|
|
214
214
|
}
|
|
215
|
-
function
|
|
216
|
-
var
|
|
217
|
-
const t = Q(n).flatMap((c) => c !== r && c.table !== void 0 ? [{ table: c.table, alias: c.alias }] : []), s = (c, m) => m === void 0 ? { type: "field", table: c } : { type: "field", table: c, column: m },
|
|
215
|
+
function ft(e, n, r) {
|
|
216
|
+
var o;
|
|
217
|
+
const t = Q(n).flatMap((c) => c !== r && c.table !== void 0 ? [{ table: c.table, alias: c.alias }] : []), s = (c, m) => m === void 0 ? { type: "field", table: c } : { type: "field", table: c, column: m }, l = ct(e, r.query).flatMap(({ name: c, table: m, column: d }) => t.flatMap((f) => e.relations.filter((h) => h.fromTable === m && h.fromColumn === d && h.toTable === f.table || h.toTable === m && h.toColumn === d && h.fromTable === f.table).map((h) => ({
|
|
218
218
|
name: c,
|
|
219
|
-
right: s(D(f),
|
|
220
|
-
}))))[0],
|
|
219
|
+
right: s(D(f), h.fromTable === m ? h.toColumn : h.fromColumn)
|
|
220
|
+
}))))[0], a = (o = lt(e, r).columns[0]) == null ? void 0 : o.name;
|
|
221
221
|
return {
|
|
222
222
|
type: "binary",
|
|
223
223
|
operator: "=",
|
|
224
|
-
left: s(r.alias, (
|
|
225
|
-
right: (
|
|
224
|
+
left: s(r.alias, (l == null ? void 0 : l.name) ?? a),
|
|
225
|
+
right: (l == null ? void 0 : l.right) ?? { type: "field" }
|
|
226
226
|
};
|
|
227
227
|
}
|
|
228
228
|
const Le = (e, n) => `${e} ${n}${e === 1 ? "" : "s"}`;
|
|
229
|
-
function
|
|
229
|
+
function mt({ unit: e, offset: n, anchor: r }) {
|
|
230
230
|
const t = Math.abs(n);
|
|
231
231
|
if (r) {
|
|
232
232
|
const s = r === "start" ? "Start" : "End";
|
|
@@ -314,7 +314,7 @@ const ar = {
|
|
|
314
314
|
* item, its words ("30 days ago", "Start of last month"), its presets'
|
|
315
315
|
* "Custom", and its custom parts. */
|
|
316
316
|
typeRelativeDate: "Relative date",
|
|
317
|
-
relativeDate:
|
|
317
|
+
relativeDate: mt,
|
|
318
318
|
relativeCustom: "Custom…",
|
|
319
319
|
relativeUnits: {
|
|
320
320
|
day: "days",
|
|
@@ -354,6 +354,8 @@ const ar = {
|
|
|
354
354
|
openUnionBranch: (e) => `Open ${e}'s query`,
|
|
355
355
|
removeUnionBranch: (e) => `Remove ${e}'s query`,
|
|
356
356
|
joinTableGroup: "Join a table",
|
|
357
|
+
/** The heading over tables in no group, when others have one. */
|
|
358
|
+
otherTablesGroup: "Other tables",
|
|
357
359
|
/** Free-form unions (issue 153): a new one, the query as one's first
|
|
358
360
|
* query, another query for one, and a new query's canvas. */
|
|
359
361
|
createUnion: "Create Union",
|
|
@@ -639,33 +641,33 @@ const ar = {
|
|
|
639
641
|
outlineEmpty: "Nothing yet: pick a table to start.",
|
|
640
642
|
outlineLimit: (e) => `At most ${e} row${e === 1 ? "" : "s"}`,
|
|
641
643
|
outlineOffset: (e) => `Skip ${e} row${e === 1 ? "" : "s"}`
|
|
642
|
-
},
|
|
643
|
-
let
|
|
644
|
+
}, Dn = (e) => "from" in e ? e.from : { table: e.table };
|
|
645
|
+
let Gn = 0;
|
|
644
646
|
function Ae() {
|
|
645
|
-
return
|
|
647
|
+
return Gn += 1, `join-${Date.now().toString(36)}-${Gn}`;
|
|
646
648
|
}
|
|
647
|
-
let
|
|
649
|
+
let Rn = 0;
|
|
648
650
|
function lr() {
|
|
649
|
-
return
|
|
651
|
+
return Rn += 1, `select-${Date.now().toString(36)}-${Rn}`;
|
|
650
652
|
}
|
|
651
|
-
let
|
|
652
|
-
function
|
|
653
|
-
return
|
|
653
|
+
let qn = 0;
|
|
654
|
+
function Qn() {
|
|
655
|
+
return qn += 1, `group-${Date.now().toString(36)}-${qn}`;
|
|
654
656
|
}
|
|
655
|
-
let
|
|
657
|
+
let _n = 0;
|
|
656
658
|
function ur() {
|
|
657
|
-
return
|
|
659
|
+
return _n += 1, `order-${Date.now().toString(36)}-${_n}`;
|
|
658
660
|
}
|
|
659
661
|
function ze(e, n, r) {
|
|
660
662
|
if (n < 0 || n >= e.length || r < 0 || r >= e.length || n === r) return e;
|
|
661
663
|
const t = [...e], [s] = t.splice(n, 1);
|
|
662
664
|
return t.splice(r, 0, s), t;
|
|
663
665
|
}
|
|
664
|
-
const
|
|
666
|
+
const Fn = (e) => e !== void 0 && Number.isSafeInteger(e) && e >= 0 ? e : void 0, Ze = (e) => (e ?? "").length <= Y;
|
|
665
667
|
function Se(e, n, r) {
|
|
666
668
|
return e.map((t) => t.id === n ? r(t) : t);
|
|
667
669
|
}
|
|
668
|
-
function
|
|
670
|
+
function yt(e, n, r) {
|
|
669
671
|
return e.map((t) => t.id === n ? r(t) : t);
|
|
670
672
|
}
|
|
671
673
|
function cr(e) {
|
|
@@ -692,7 +694,7 @@ function dr(e, n, r) {
|
|
|
692
694
|
on: n
|
|
693
695
|
};
|
|
694
696
|
}
|
|
695
|
-
function
|
|
697
|
+
function pt(e, n) {
|
|
696
698
|
const [r, ...t] = n;
|
|
697
699
|
return { ...e, from: cr(r), joins: t };
|
|
698
700
|
}
|
|
@@ -731,39 +733,39 @@ function ne(e) {
|
|
|
731
733
|
// tables are joined inside it (withExistsJoins), never to the query
|
|
732
734
|
// around it — that would repeat its rows, which EXISTS is there to avoid.
|
|
733
735
|
case "exists": {
|
|
734
|
-
const i =
|
|
735
|
-
ne(t.link).filter((
|
|
736
|
+
const i = zn(t.query);
|
|
737
|
+
ne(t.link).filter((l) => !i.has(l)).forEach((l) => n.add(l));
|
|
736
738
|
break;
|
|
737
739
|
}
|
|
738
740
|
}
|
|
739
741
|
};
|
|
740
742
|
return r(e), [...n];
|
|
741
743
|
}
|
|
742
|
-
function
|
|
743
|
-
const i = Be(n),
|
|
744
|
+
function pn(e, n, r, t, s) {
|
|
745
|
+
const i = Be(n), l = t === void 0 && s === void 0, a = l ? Vt(e, i, r) : void 0, o = {
|
|
744
746
|
id: Ae(),
|
|
745
747
|
table: r,
|
|
746
|
-
joinType: t ?? (
|
|
748
|
+
joinType: t ?? (a == null ? void 0 : a.joinType) ?? "inner",
|
|
747
749
|
// Else matched to a derived table holding its key, if one does.
|
|
748
|
-
on: s ?? (
|
|
750
|
+
on: s ?? (a == null ? void 0 : a.on) ?? (l ? or(e, n, r) : void 0)
|
|
749
751
|
};
|
|
750
|
-
return { ...n, joins: [...n.joins,
|
|
752
|
+
return { ...n, joins: [...n.joins, o] };
|
|
751
753
|
}
|
|
752
754
|
function fr(e, n, r) {
|
|
753
|
-
const t = Be(n), s =
|
|
754
|
-
return (s && s.length > 0 ? s : [r]).reduce((
|
|
755
|
+
const t = Be(n), s = Zn(e, t, r);
|
|
756
|
+
return (s && s.length > 0 ? s : [r]).reduce((l, a) => pn(e, l, a), n);
|
|
755
757
|
}
|
|
756
758
|
function ce(e, n, r) {
|
|
757
759
|
return ne(r).reduce((t, s) => {
|
|
758
|
-
if (Q(t).some((
|
|
760
|
+
if (Q(t).some((a) => a.alias === s)) return t;
|
|
759
761
|
const i = Be(t);
|
|
760
|
-
return (
|
|
762
|
+
return (Zn(e, i, s) ?? [s]).reduce((a, o) => pn(e, a, o), t);
|
|
761
763
|
}, n);
|
|
762
764
|
}
|
|
763
765
|
function mr(e, n, r, t) {
|
|
764
766
|
return {
|
|
765
767
|
...ce(e, n, t),
|
|
766
|
-
select:
|
|
768
|
+
select: yt(n.select, r, (s) => ({ ...s, expression: t }))
|
|
767
769
|
};
|
|
768
770
|
}
|
|
769
771
|
function hn(e, n, r) {
|
|
@@ -771,38 +773,38 @@ function hn(e, n, r) {
|
|
|
771
773
|
if (n < 0 || n >= t.length || r < 0 || r >= t.length || n === r) return e;
|
|
772
774
|
const s = [...t], [i] = s.splice(n, 1);
|
|
773
775
|
s.splice(r, 0, i);
|
|
774
|
-
const [
|
|
775
|
-
let
|
|
776
|
-
if (n === 0 ? (c =
|
|
777
|
-
const { on: m, joinType: d } =
|
|
776
|
+
const [l, a] = t;
|
|
777
|
+
let o, c;
|
|
778
|
+
if (n === 0 ? (c = l, o = a) : r === 0 && (o = t[n], c = l), o && c) {
|
|
779
|
+
const { on: m, joinType: d } = o;
|
|
778
780
|
s[s.indexOf(c)] = dr(c, m, d);
|
|
779
781
|
}
|
|
780
|
-
return
|
|
782
|
+
return pt(e, s);
|
|
781
783
|
}
|
|
782
|
-
function
|
|
784
|
+
function gn(e) {
|
|
783
785
|
const n = /* @__PURE__ */ new Set([D(e.from)]);
|
|
784
786
|
let r;
|
|
785
787
|
return e.joins.some((t) => {
|
|
786
788
|
n.add(D(t));
|
|
787
|
-
const s = t.joinType === "cross" ? void 0 : t.on, i = ne(s).find((
|
|
789
|
+
const s = t.joinType === "cross" ? void 0 : t.on, i = ne(s).find((l) => !n.has(l));
|
|
788
790
|
return i && (r = { table: t.table ?? D(t), needs: i }), i !== void 0;
|
|
789
791
|
}), r;
|
|
790
792
|
}
|
|
791
793
|
function fs(e, n, r) {
|
|
792
794
|
const t = hn(e, n, r);
|
|
793
|
-
return t === e ? void 0 :
|
|
795
|
+
return t === e ? void 0 : gn(t);
|
|
794
796
|
}
|
|
795
797
|
function yr(e, n, r) {
|
|
796
798
|
const t = hn(e, n, r);
|
|
797
|
-
return t !== e &&
|
|
799
|
+
return t !== e && gn(t) === void 0;
|
|
798
800
|
}
|
|
799
|
-
function
|
|
801
|
+
function pr(e, n, r, t) {
|
|
800
802
|
return {
|
|
801
803
|
...ce(e, n, t),
|
|
802
804
|
orderBy: (n.orderBy ?? []).map((s) => s.id === r ? { ...s, expression: t } : s)
|
|
803
805
|
};
|
|
804
806
|
}
|
|
805
|
-
function
|
|
807
|
+
function hr(e, n, r, t) {
|
|
806
808
|
return {
|
|
807
809
|
...ce(e, n, t),
|
|
808
810
|
groupBy: (n.groupBy ?? []).map((s) => s.id === r ? { ...s, expression: t } : s)
|
|
@@ -810,25 +812,25 @@ function pr(e, n, r, t) {
|
|
|
810
812
|
}
|
|
811
813
|
function He(e, n) {
|
|
812
814
|
if (!n || !Jt(n)) return n;
|
|
813
|
-
const r = fe(n), t = r.map((
|
|
815
|
+
const r = fe(n), t = r.map((o) => He(e, o)), i = t.some((o, c) => o !== r[c]) ? xe(n, (o) => t[r.indexOf(o)]) : n;
|
|
814
816
|
if (i.type !== "exists") return i;
|
|
815
|
-
const { query:
|
|
817
|
+
const { query: l } = i, { joins: a } = ce(
|
|
816
818
|
e,
|
|
817
819
|
{
|
|
818
820
|
id: "",
|
|
819
|
-
from:
|
|
820
|
-
joins:
|
|
821
|
+
from: l.from,
|
|
822
|
+
joins: l.joins,
|
|
821
823
|
select: []
|
|
822
824
|
},
|
|
823
|
-
|
|
825
|
+
l.where
|
|
824
826
|
);
|
|
825
|
-
return
|
|
827
|
+
return a === l.joins ? i : { ...i, query: { ...l, joins: a } };
|
|
826
828
|
}
|
|
827
829
|
function ms(e, n, r) {
|
|
828
830
|
const t = He(e, r);
|
|
829
831
|
return { ...ce(e, n, t), having: t };
|
|
830
832
|
}
|
|
831
|
-
function
|
|
833
|
+
function Jn(e, n) {
|
|
832
834
|
const r = /* @__PURE__ */ new Set([
|
|
833
835
|
...n ? Q(n).map(D) : [],
|
|
834
836
|
...me(e).map((s) => s.name)
|
|
@@ -840,9 +842,9 @@ function Fn(e, n) {
|
|
|
840
842
|
function ys() {
|
|
841
843
|
return { id: W() };
|
|
842
844
|
}
|
|
843
|
-
function
|
|
845
|
+
function rn(e, n, r) {
|
|
844
846
|
if (!ue(n))
|
|
845
|
-
return r.type !== "setBaseTable" && r.type !== "setDerivedBaseTable" ? n :
|
|
847
|
+
return r.type !== "setBaseTable" && r.type !== "setDerivedBaseTable" ? n : rn(e, {
|
|
846
848
|
id: n.id,
|
|
847
849
|
...n.shared ? { shared: n.shared } : {},
|
|
848
850
|
from: { table: "" },
|
|
@@ -857,8 +859,8 @@ function tn(e, n, r) {
|
|
|
857
859
|
...n,
|
|
858
860
|
from: {
|
|
859
861
|
id: r.id ?? Ae(),
|
|
860
|
-
alias:
|
|
861
|
-
query: { from:
|
|
862
|
+
alias: Jn(e, n),
|
|
863
|
+
query: { from: Dn(r), joins: [], select: [] }
|
|
862
864
|
}
|
|
863
865
|
};
|
|
864
866
|
case "changeBaseAlias":
|
|
@@ -869,35 +871,35 @@ function tn(e, n, r) {
|
|
|
869
871
|
from: n.from.table === void 0 ? { ...n.from, alias: r.alias || n.from.alias } : { ...n.from, alias: r.alias }
|
|
870
872
|
} : n;
|
|
871
873
|
case "addJoin":
|
|
872
|
-
return r.joinType === void 0 && r.on === void 0 ? fr(e, n, r.table) :
|
|
874
|
+
return r.joinType === void 0 && r.on === void 0 ? fr(e, n, r.table) : pn(e, n, r.table, r.joinType, r.on);
|
|
873
875
|
case "addDerivedJoin": {
|
|
874
876
|
const t = {
|
|
875
877
|
id: r.id ?? Ae(),
|
|
876
|
-
alias:
|
|
878
|
+
alias: Jn(e, n),
|
|
877
879
|
joinType: "left",
|
|
878
|
-
query: { from:
|
|
879
|
-
}, s =
|
|
880
|
+
query: { from: Dn(r), joins: [], select: [] }
|
|
881
|
+
}, s = ft(e, n, t);
|
|
880
882
|
return { ...n, joins: [...n.joins, { ...t, on: s }] };
|
|
881
883
|
}
|
|
882
884
|
case "renameDerivedTable": {
|
|
883
|
-
const t = oe(n).find((
|
|
885
|
+
const t = oe(n).find((o) => o.id === r.derivedId), s = r.alias.trim();
|
|
884
886
|
if (!t || s === t.alias) return n;
|
|
885
|
-
const i = Q(n).filter((
|
|
887
|
+
const i = Q(n).filter((o) => o !== t).map(D);
|
|
886
888
|
if (!s || s.length > Y || i.includes(s) || z(e, s)) return n;
|
|
887
|
-
const
|
|
889
|
+
const l = Ln(n, t.alias, s), { from: a } = l;
|
|
888
890
|
return {
|
|
889
|
-
...
|
|
890
|
-
from:
|
|
891
|
-
joins:
|
|
891
|
+
...l,
|
|
892
|
+
from: a.query && a.id === t.id ? { ...a, alias: s } : a,
|
|
893
|
+
joins: l.joins.map((o) => o.id === t.id ? { ...o, alias: s } : o)
|
|
892
894
|
};
|
|
893
895
|
}
|
|
894
896
|
case "renameSharedPlace": {
|
|
895
|
-
const t = r.joinId === void 0 ? n.from : n.joins.find((
|
|
897
|
+
const t = r.joinId === void 0 ? n.from : n.joins.find((a) => a.id === r.joinId), s = r.alias.trim();
|
|
896
898
|
if ((t == null ? void 0 : t.shared) === void 0 || s === t.alias) return n;
|
|
897
|
-
const i = Q(n).filter((
|
|
899
|
+
const i = Q(n).filter((a) => a !== t).map(D);
|
|
898
900
|
if (!s || s.length > Y || i.includes(s) || z(e, s)) return n;
|
|
899
|
-
const
|
|
900
|
-
return r.joinId === void 0 ? { ...
|
|
901
|
+
const l = Ln(n, t.alias, s);
|
|
902
|
+
return r.joinId === void 0 ? { ...l, from: { ...l.from, alias: s } } : { ...l, joins: Se(l.joins, r.joinId, (a) => ({ ...a, alias: s })) };
|
|
901
903
|
}
|
|
902
904
|
case "setMeasureSynced":
|
|
903
905
|
return {
|
|
@@ -928,7 +930,7 @@ function tn(e, n, r) {
|
|
|
928
930
|
}))
|
|
929
931
|
};
|
|
930
932
|
case "reorderJoin": {
|
|
931
|
-
const t = n.joins.findIndex((
|
|
933
|
+
const t = n.joins.findIndex((l) => l.id === r.joinId);
|
|
932
934
|
if (t === -1) return n;
|
|
933
935
|
const s = r.direction === "up" ? t - 1 : t + 1;
|
|
934
936
|
if (s < 0 || s >= n.joins.length) return n;
|
|
@@ -938,7 +940,7 @@ function tn(e, n, r) {
|
|
|
938
940
|
case "reorderTables":
|
|
939
941
|
return yr(n, r.fromIndex, r.toIndex) ? hn(n, r.fromIndex, r.toIndex) : n;
|
|
940
942
|
case "removeBaseTable":
|
|
941
|
-
return n.joins.length > 0 ?
|
|
943
|
+
return n.joins.length > 0 ? pt(n, n.joins) : n.shared ? { id: n.id, shared: n.shared } : { id: n.id };
|
|
942
944
|
case "addSelectItem": {
|
|
943
945
|
const t = { id: lr(), expression: { type: "field" } };
|
|
944
946
|
return { ...n, select: [...n.select, t] };
|
|
@@ -952,12 +954,12 @@ function tn(e, n, r) {
|
|
|
952
954
|
case "setSelectAlias":
|
|
953
955
|
return Ze(r.alias) ? {
|
|
954
956
|
...n,
|
|
955
|
-
select:
|
|
957
|
+
select: yt(n.select, r.selectId, (t) => ({ ...t, alias: r.alias }))
|
|
956
958
|
} : n;
|
|
957
959
|
case "setSelectExpression":
|
|
958
960
|
return mr(e, n, r.selectId, r.expression);
|
|
959
961
|
case "addGroupByItem": {
|
|
960
|
-
const t = { id:
|
|
962
|
+
const t = { id: Qn(), expression: { type: "field" } };
|
|
961
963
|
return { ...n, groupBy: [...n.groupBy ?? [], t] };
|
|
962
964
|
}
|
|
963
965
|
case "removeGroupByItem": {
|
|
@@ -969,11 +971,11 @@ function tn(e, n, r) {
|
|
|
969
971
|
return s === t ? n : { ...n, groupBy: s };
|
|
970
972
|
}
|
|
971
973
|
case "setGroupByExpression":
|
|
972
|
-
return
|
|
974
|
+
return hr(e, n, r.groupById, r.expression);
|
|
973
975
|
case "groupBySelectItem": {
|
|
974
976
|
const t = n.select.find((i) => i.id === r.selectId);
|
|
975
977
|
if (!t) return n;
|
|
976
|
-
const s = { id:
|
|
978
|
+
const s = { id: Qn(), expression: t.expression };
|
|
977
979
|
return { ...n, groupBy: [...n.groupBy ?? [], s] };
|
|
978
980
|
}
|
|
979
981
|
case "removeHaving":
|
|
@@ -997,16 +999,16 @@ function tn(e, n, r) {
|
|
|
997
999
|
return s === t ? n : { ...n, orderBy: s };
|
|
998
1000
|
}
|
|
999
1001
|
case "setOrderByExpression":
|
|
1000
|
-
return
|
|
1002
|
+
return pr(e, n, r.orderById, r.expression);
|
|
1001
1003
|
case "setOrderByDirection":
|
|
1002
1004
|
return {
|
|
1003
1005
|
...n,
|
|
1004
1006
|
orderBy: (n.orderBy ?? []).map((t) => t.id === r.orderById ? { ...t, direction: r.direction } : t)
|
|
1005
1007
|
};
|
|
1006
1008
|
case "setLimit":
|
|
1007
|
-
return { ...n, limit:
|
|
1009
|
+
return { ...n, limit: Fn(r.limit) };
|
|
1008
1010
|
case "setOffset":
|
|
1009
|
-
return { ...n, offset:
|
|
1011
|
+
return { ...n, offset: Fn(r.offset) };
|
|
1010
1012
|
case "clearPaging":
|
|
1011
1013
|
return { ...n, limit: void 0, offset: void 0 };
|
|
1012
1014
|
/* v8 ignore start -- PipelineAction is a closed union; every real case is
|
|
@@ -1023,10 +1025,10 @@ const gr = {
|
|
|
1023
1025
|
full: "FULL",
|
|
1024
1026
|
cross: "CROSS"
|
|
1025
1027
|
};
|
|
1026
|
-
function
|
|
1028
|
+
function ps(e) {
|
|
1027
1029
|
return `${gr[e.joinType]} JOIN ${D(e)}`;
|
|
1028
1030
|
}
|
|
1029
|
-
function
|
|
1031
|
+
function hs(e) {
|
|
1030
1032
|
return e.alias || "Column";
|
|
1031
1033
|
}
|
|
1032
1034
|
function gs() {
|
|
@@ -1038,11 +1040,11 @@ function bs() {
|
|
|
1038
1040
|
function ws(e, n, r) {
|
|
1039
1041
|
const t = Q(n);
|
|
1040
1042
|
if (r !== void 0) {
|
|
1041
|
-
const
|
|
1042
|
-
return new Set(t.slice(0,
|
|
1043
|
+
const o = t.findIndex((c) => "id" in c && c.id === r);
|
|
1044
|
+
return new Set(t.slice(0, o + 1).map(D));
|
|
1043
1045
|
}
|
|
1044
|
-
const s = new Set(Be(n)), i = Pt(e, [...s]),
|
|
1045
|
-
return /* @__PURE__ */ new Set([...t.map(D), ...
|
|
1046
|
+
const s = new Set(Be(n)), i = Pt(e, [...s]), l = new Set(dt(e, n).map((o) => o.table)), a = me(e).filter((o) => !s.has(o.name) && (i.has(o.name) || l.has(o.name)));
|
|
1047
|
+
return /* @__PURE__ */ new Set([...t.map(D), ...a.map((o) => o.name)]);
|
|
1046
1048
|
}
|
|
1047
1049
|
function $s(e, n) {
|
|
1048
1050
|
const r = Q(n);
|
|
@@ -1055,15 +1057,15 @@ function vs(e, n) {
|
|
|
1055
1057
|
const r = new Set(Be(n));
|
|
1056
1058
|
return me(e).filter((t) => !r.has(t.name));
|
|
1057
1059
|
}
|
|
1058
|
-
function
|
|
1059
|
-
return e ? e.type === "logical" ? e.operands.reduce((n, r) => n +
|
|
1060
|
+
function sn(e) {
|
|
1061
|
+
return e ? e.type === "logical" ? e.operands.reduce((n, r) => n + sn(r), 0) : e.type === "not" ? sn(e.operand) : 1 : 0;
|
|
1060
1062
|
}
|
|
1061
|
-
function
|
|
1063
|
+
function Pn(e) {
|
|
1062
1064
|
return e.column ? e.table ? `${e.table}.${e.column}` : e.column : "?";
|
|
1063
1065
|
}
|
|
1064
1066
|
function Ss(e, n = ar) {
|
|
1065
1067
|
var r;
|
|
1066
|
-
return e ? e.type === "binary" && e.left.type === "field" && ((r = e.right) == null ? void 0 : r.type) === "field" ? !e.left.column && !e.right.column ? n.setJoinCondition : `${
|
|
1068
|
+
return e ? e.type === "binary" && e.left.type === "field" && ((r = e.right) == null ? void 0 : r.type) === "field" ? !e.left.column && !e.right.column ? n.setJoinCondition : `${Pn(e.left)} ${e.operator ?? "="} ${Pn(e.right)}` : n.conditionCount(sn(e)) : n.setJoinCondition;
|
|
1067
1069
|
}
|
|
1068
1070
|
function Es(e, n) {
|
|
1069
1071
|
const r = new Set(ne(e.where));
|
|
@@ -1074,40 +1076,40 @@ function Ts(e, n, r) {
|
|
|
1074
1076
|
return { ...ce(e, n, t), where: t };
|
|
1075
1077
|
}
|
|
1076
1078
|
function Ns(e, n, r, t) {
|
|
1077
|
-
return r.length === 0 ?
|
|
1079
|
+
return r.length === 0 ? rn(e, n, t) : ue(n) ? yn(n, r, (s) => rn(e, s, t)) : n;
|
|
1078
1080
|
}
|
|
1079
|
-
const
|
|
1081
|
+
const bn = (e) => typeof e == "object" && e !== null && !Array.isArray(e) && !(e instanceof Date), wn = (e) => typeof e.shared == "string";
|
|
1080
1082
|
function qe(e, n) {
|
|
1081
1083
|
if (Array.isArray(e)) {
|
|
1082
1084
|
let s = !1;
|
|
1083
|
-
const i = e.flatMap((
|
|
1084
|
-
const
|
|
1085
|
-
return
|
|
1085
|
+
const i = e.flatMap((l) => {
|
|
1086
|
+
const a = qe(l, n);
|
|
1087
|
+
return a !== l && (s = !0), a === void 0 ? [] : [a];
|
|
1086
1088
|
});
|
|
1087
1089
|
return s ? i : e;
|
|
1088
1090
|
}
|
|
1089
|
-
if (!
|
|
1090
|
-
if (
|
|
1091
|
+
if (!bn(e)) return e;
|
|
1092
|
+
if (wn(e)) return n(e);
|
|
1091
1093
|
let r = !1;
|
|
1092
1094
|
const t = Object.entries(e).map(([s, i]) => {
|
|
1093
|
-
const
|
|
1094
|
-
return
|
|
1095
|
+
const l = qe(i, n);
|
|
1096
|
+
return l !== i && (r = !0), [s, l];
|
|
1095
1097
|
});
|
|
1096
1098
|
return r ? Object.fromEntries(t) : e;
|
|
1097
1099
|
}
|
|
1098
1100
|
function As(e) {
|
|
1099
1101
|
const n = new Map((e.shared ?? []).map((t) => [t.id, []])), r = (t, s) => {
|
|
1100
1102
|
if (Array.isArray(t)) {
|
|
1101
|
-
t.forEach((i,
|
|
1103
|
+
t.forEach((i, l) => r(i, [...s, l]));
|
|
1102
1104
|
return;
|
|
1103
1105
|
}
|
|
1104
|
-
if (
|
|
1105
|
-
if (
|
|
1106
|
+
if (bn(t)) {
|
|
1107
|
+
if (wn(t)) {
|
|
1106
1108
|
const i = t.shared;
|
|
1107
1109
|
n.set(i, [...n.get(i) ?? [], s]);
|
|
1108
1110
|
return;
|
|
1109
1111
|
}
|
|
1110
|
-
Object.entries(t).forEach(([i,
|
|
1112
|
+
Object.entries(t).forEach(([i, l]) => r(l, [...s, i]));
|
|
1111
1113
|
}
|
|
1112
1114
|
};
|
|
1113
1115
|
return r(e, []), n;
|
|
@@ -1128,10 +1130,10 @@ function br(e, n) {
|
|
|
1128
1130
|
return t;
|
|
1129
1131
|
}
|
|
1130
1132
|
function wr(e, n, r, t = W(), s) {
|
|
1131
|
-
const i = e.shared ?? [],
|
|
1133
|
+
const i = e.shared ?? [], l = i.findIndex((o) => o.id === s), a = { id: t, name: n, query: r };
|
|
1132
1134
|
return {
|
|
1133
1135
|
...e,
|
|
1134
|
-
shared:
|
|
1136
|
+
shared: l < 0 ? [...i, a] : [...i.slice(0, l), a, ...i.slice(l)]
|
|
1135
1137
|
};
|
|
1136
1138
|
}
|
|
1137
1139
|
const ht = (e, n) => {
|
|
@@ -1154,14 +1156,14 @@ function Bs(e, n, r) {
|
|
|
1154
1156
|
const t = ht(e, r);
|
|
1155
1157
|
return t ? { ...n, from: { shared: r, alias: t.name } } : n;
|
|
1156
1158
|
}
|
|
1157
|
-
function
|
|
1159
|
+
function Is(e, n, r = W(), t = []) {
|
|
1158
1160
|
const s = sr(e, t), i = s && oe(s).find((c) => c.id === n);
|
|
1159
1161
|
if (!i) return e;
|
|
1160
|
-
const
|
|
1161
|
-
let
|
|
1162
|
-
for (let c = 2;
|
|
1163
|
-
const
|
|
1164
|
-
return
|
|
1162
|
+
const l = new Set((e.shared ?? []).map((c) => c.name));
|
|
1163
|
+
let a = i.alias;
|
|
1164
|
+
for (let c = 2; l.has(a); c += 1) a = `${i.alias}_${c}`;
|
|
1165
|
+
const o = wr(e, a, i.query, r, t[0]);
|
|
1166
|
+
return yn(o, t, (c) => c.from.query && c.from.id === n ? { ...c, from: { shared: r, alias: i.alias } } : {
|
|
1165
1167
|
...c,
|
|
1166
1168
|
joins: c.joins.map((m) => {
|
|
1167
1169
|
if (m.id !== n || !m.query) return m;
|
|
@@ -1170,18 +1172,18 @@ function Cs(e, n, r = W(), t = []) {
|
|
|
1170
1172
|
})
|
|
1171
1173
|
});
|
|
1172
1174
|
}
|
|
1173
|
-
function
|
|
1175
|
+
function gt(e, n) {
|
|
1174
1176
|
const { shared: r, ...t } = e;
|
|
1175
1177
|
return "joinType" in t ? { ...t, query: n } : { id: W(), alias: t.alias, query: n };
|
|
1176
1178
|
}
|
|
1177
|
-
function
|
|
1179
|
+
function Cs(e, n) {
|
|
1178
1180
|
const r = new Map((e.shared ?? []).map((s) => [s.id, s.query])), t = n.reduce(
|
|
1179
1181
|
(s, i) => typeof s == "object" && s !== null ? s[i] : void 0,
|
|
1180
1182
|
e
|
|
1181
1183
|
);
|
|
1182
|
-
return !
|
|
1184
|
+
return !bn(t) || !wn(t) ? e : qe(e, (s) => {
|
|
1183
1185
|
const i = r.get(s.shared);
|
|
1184
|
-
return s === t && i ?
|
|
1186
|
+
return s === t && i ? gt(s, i) : s;
|
|
1185
1187
|
});
|
|
1186
1188
|
}
|
|
1187
1189
|
function Ms(e, n, r) {
|
|
@@ -1197,67 +1199,67 @@ class $r extends Error {
|
|
|
1197
1199
|
}
|
|
1198
1200
|
}
|
|
1199
1201
|
function Os(e, n, r = "subQueries") {
|
|
1200
|
-
var
|
|
1201
|
-
const t = (
|
|
1202
|
+
var o;
|
|
1203
|
+
const t = (o = e.shared) == null ? void 0 : o.find((c) => c.id === n);
|
|
1202
1204
|
if (!t) return e;
|
|
1203
|
-
const s = e.shared.filter((c) => c !== t), { shared: i, ...
|
|
1204
|
-
return qe(
|
|
1205
|
+
const s = e.shared.filter((c) => c !== t), { shared: i, ...l } = e, a = s.length > 0 ? { ...l, shared: s } : l;
|
|
1206
|
+
return qe(a, (c) => {
|
|
1205
1207
|
if (c.shared !== n) return c;
|
|
1206
|
-
if (r === "subQueries") return
|
|
1208
|
+
if (r === "subQueries") return gt(c, t.query);
|
|
1207
1209
|
if (!("joinType" in c)) throw new $r(t.name);
|
|
1208
1210
|
});
|
|
1209
1211
|
}
|
|
1210
|
-
const
|
|
1211
|
-
function
|
|
1212
|
+
const bt = (e) => e.query !== void 0 && !H(e.query);
|
|
1213
|
+
function wt(e) {
|
|
1212
1214
|
const n = /* @__PURE__ */ new Map();
|
|
1213
1215
|
return e.relations.forEach((r) => {
|
|
1214
1216
|
n.set(r.fromTable, [...n.get(r.fromTable) ?? [], r.toTable]);
|
|
1215
1217
|
}), n;
|
|
1216
1218
|
}
|
|
1217
|
-
function
|
|
1219
|
+
function Ie(e, n, r = wt(e)) {
|
|
1218
1220
|
const t = /* @__PURE__ */ new Set([n]), s = [n];
|
|
1219
1221
|
for (let i = s.shift(); i !== void 0; i = s.shift())
|
|
1220
|
-
(r.get(i) ?? []).forEach((
|
|
1221
|
-
t.has(
|
|
1222
|
+
(r.get(i) ?? []).forEach((l) => {
|
|
1223
|
+
t.has(l) || (t.add(l), s.push(l));
|
|
1222
1224
|
});
|
|
1223
1225
|
return t;
|
|
1224
1226
|
}
|
|
1225
1227
|
const we = (e) => e ? e.type === "logical" && e.operator === "AND" ? e.operands : [e] : [];
|
|
1226
|
-
function
|
|
1228
|
+
function $t(e, n) {
|
|
1227
1229
|
const r = new Map(Q(e).map((t) => [D(t), t.table]));
|
|
1228
1230
|
return ne(n).map((t) => ({
|
|
1229
1231
|
ref: t,
|
|
1230
1232
|
table: r.has(t) ? r.get(t) : t
|
|
1231
1233
|
}));
|
|
1232
1234
|
}
|
|
1233
|
-
const
|
|
1234
|
-
function
|
|
1235
|
+
const $n = (e, n) => $t(e, n).map((r) => r.table);
|
|
1236
|
+
function Vn(e, n) {
|
|
1235
1237
|
const r = new Map(Q(e).map((t) => [D(t), t.table]));
|
|
1236
|
-
return
|
|
1238
|
+
return mn(n, (t) => {
|
|
1237
1239
|
if (t.type !== "field" || t.table === void 0) return t;
|
|
1238
1240
|
const s = r.get(t.table);
|
|
1239
1241
|
return s === void 0 || s === t.table ? t : { ...t, table: s };
|
|
1240
1242
|
});
|
|
1241
1243
|
}
|
|
1242
|
-
function
|
|
1243
|
-
const r = (n.groupBy ?? []).flatMap((s) =>
|
|
1244
|
+
function vt(e, n) {
|
|
1245
|
+
const r = (n.groupBy ?? []).flatMap((s) => $t(n, s.expression)), t = wt(e);
|
|
1244
1246
|
return me(e).map((s) => {
|
|
1245
|
-
const i =
|
|
1246
|
-
return
|
|
1247
|
+
const i = Ie(e, s.name, t), l = r.find((a) => a.table === void 0 || !i.has(a.table));
|
|
1248
|
+
return l ? { table: s, unlinked: l.table ?? l.ref } : { table: s };
|
|
1247
1249
|
});
|
|
1248
1250
|
}
|
|
1249
1251
|
function vr(e, n, r) {
|
|
1250
|
-
const t =
|
|
1252
|
+
const t = Ie(e, r);
|
|
1251
1253
|
return we(n.where).filter((s) => {
|
|
1252
|
-
const i =
|
|
1253
|
-
return i.length > 0 && i.every((
|
|
1254
|
+
const i = $n(n, s);
|
|
1255
|
+
return i.length > 0 && i.every((l) => l !== void 0 && t.has(l));
|
|
1254
1256
|
});
|
|
1255
1257
|
}
|
|
1256
1258
|
function Sr(e, n, r) {
|
|
1257
|
-
const t =
|
|
1259
|
+
const t = Ie(e, r);
|
|
1258
1260
|
return we(n.where).some((s) => {
|
|
1259
|
-
const i =
|
|
1260
|
-
return i.some((
|
|
1261
|
+
const i = $n(n, s);
|
|
1262
|
+
return i.some((l) => l !== void 0 && t.has(l)) && i.some((l) => l === void 0 || !t.has(l));
|
|
1261
1263
|
});
|
|
1262
1264
|
}
|
|
1263
1265
|
function Re(e, n) {
|
|
@@ -1265,7 +1267,7 @@ function Re(e, n) {
|
|
|
1265
1267
|
for (let t = 2; n.has(r); t += 1) r = `${e}_${t}`;
|
|
1266
1268
|
return r;
|
|
1267
1269
|
}
|
|
1268
|
-
function
|
|
1270
|
+
function on(e, n) {
|
|
1269
1271
|
if (e.type === "field" && e.column) return e.column;
|
|
1270
1272
|
if (e.type === "function") {
|
|
1271
1273
|
const r = e.args[0];
|
|
@@ -1273,7 +1275,7 @@ function sn(e, n) {
|
|
|
1273
1275
|
}
|
|
1274
1276
|
return n;
|
|
1275
1277
|
}
|
|
1276
|
-
const
|
|
1278
|
+
const an = (e, n) => ({ type: "field", table: e, column: n });
|
|
1277
1279
|
function Er(e, n) {
|
|
1278
1280
|
return {
|
|
1279
1281
|
type: "logical",
|
|
@@ -1299,127 +1301,127 @@ function Er(e, n) {
|
|
|
1299
1301
|
function Tr(e, n, r) {
|
|
1300
1302
|
var i;
|
|
1301
1303
|
if (r.type !== "field" || r.table === void 0 || r.column === void 0) return !1;
|
|
1302
|
-
const t =
|
|
1304
|
+
const t = $n(n, r)[0], s = (i = z(e, t)) == null ? void 0 : i.columns.find((l) => l.name === r.column);
|
|
1303
1305
|
return s !== void 0 && !s.nullable;
|
|
1304
1306
|
}
|
|
1305
|
-
function
|
|
1306
|
-
const s = n.groupBy.map((
|
|
1307
|
+
function St(e, n, r, t) {
|
|
1308
|
+
const s = n.groupBy.map((l) => l.expression), i = t.map((l, a) => Tr(e, n, s[a]) ? {
|
|
1307
1309
|
type: "binary",
|
|
1308
1310
|
operator: "=",
|
|
1309
|
-
left:
|
|
1310
|
-
right: s[
|
|
1311
|
-
} : Er(
|
|
1311
|
+
left: an(r, l),
|
|
1312
|
+
right: s[a]
|
|
1313
|
+
} : Er(an(r, l), s[a]));
|
|
1312
1314
|
return i.length === 1 ? i[0] : { type: "logical", operator: "AND", operands: i };
|
|
1313
1315
|
}
|
|
1314
|
-
function
|
|
1315
|
-
var
|
|
1316
|
-
const
|
|
1317
|
-
const G = Re(
|
|
1318
|
-
return
|
|
1319
|
-
}), m = [...
|
|
1316
|
+
function Et(e, n, r, t, s, i) {
|
|
1317
|
+
var T;
|
|
1318
|
+
const l = n.groupBy.map((b) => Vn(n, b.expression)), a = vr(e, n, r).map((b) => Vn(n, b)), o = new Set(s.map((b) => b.alias).filter((b) => !!b)), c = l.map((b, C) => {
|
|
1319
|
+
const G = Re(on(b, `key_${C + 1}`), o);
|
|
1320
|
+
return o.add(G), G;
|
|
1321
|
+
}), m = [...a, ...t];
|
|
1320
1322
|
let d = {
|
|
1321
1323
|
id: "",
|
|
1322
1324
|
from: { table: r },
|
|
1323
1325
|
joins: [],
|
|
1324
1326
|
select: []
|
|
1325
1327
|
};
|
|
1326
|
-
[...
|
|
1328
|
+
[...l, ...m, ...s.map((b) => b.expression)].forEach((b) => {
|
|
1327
1329
|
d = ce(e, d, b);
|
|
1328
1330
|
});
|
|
1329
|
-
const f = ((
|
|
1330
|
-
const
|
|
1331
|
-
if (
|
|
1332
|
-
const [G] =
|
|
1331
|
+
const f = ((T = i == null ? void 0 : i.groupBy) == null ? void 0 : T.length) ?? 0, h = [...(i == null ? void 0 : i.joins) ?? []], v = d.joins.map((b) => {
|
|
1332
|
+
const C = h.findIndex((R) => R.table !== void 0 && R.table === b.table);
|
|
1333
|
+
if (C === -1) return b;
|
|
1334
|
+
const [G] = h.splice(C, 1);
|
|
1333
1335
|
return { ...b, id: G.id };
|
|
1334
1336
|
}), $ = {
|
|
1335
1337
|
...i,
|
|
1336
1338
|
from: d.from,
|
|
1337
1339
|
joins: v,
|
|
1338
1340
|
where: m.length > 0 ? { type: "logical", operator: "AND", operands: m } : void 0,
|
|
1339
|
-
groupBy:
|
|
1341
|
+
groupBy: l.map((b, C) => {
|
|
1340
1342
|
var G, R;
|
|
1341
1343
|
return {
|
|
1342
|
-
id: ((R = (G = i == null ? void 0 : i.groupBy) == null ? void 0 : G[
|
|
1344
|
+
id: ((R = (G = i == null ? void 0 : i.groupBy) == null ? void 0 : G[C]) == null ? void 0 : R.id) ?? W(),
|
|
1343
1345
|
expression: b
|
|
1344
1346
|
};
|
|
1345
1347
|
}),
|
|
1346
1348
|
select: [
|
|
1347
|
-
...
|
|
1349
|
+
...l.map((b, C) => {
|
|
1348
1350
|
var G;
|
|
1349
1351
|
return {
|
|
1350
|
-
id:
|
|
1351
|
-
alias: c[
|
|
1352
|
+
id: C < f && ((G = i == null ? void 0 : i.select[C]) == null ? void 0 : G.id) || W(),
|
|
1353
|
+
alias: c[C],
|
|
1352
1354
|
expression: b
|
|
1353
1355
|
};
|
|
1354
1356
|
}),
|
|
1355
1357
|
...s
|
|
1356
1358
|
]
|
|
1357
1359
|
};
|
|
1358
|
-
return $.where === void 0 && delete $.where, { query: $, copied:
|
|
1360
|
+
return $.where === void 0 && delete $.where, { query: $, copied: a.length, keyNames: c };
|
|
1359
1361
|
}
|
|
1360
1362
|
function Us(e, n, r) {
|
|
1361
1363
|
var v;
|
|
1362
1364
|
if ((n.groupBy ?? []).length === 0)
|
|
1363
1365
|
throw new Error("addMeasure: the query isn't grouped (a measure is per group)");
|
|
1364
|
-
const t =
|
|
1366
|
+
const t = vt(e, n).find(($) => $.table.name === r.table);
|
|
1365
1367
|
if (!t) throw new Error(`addMeasure: unknown table "${r.table}"`);
|
|
1366
1368
|
if (t.unlinked !== void 0)
|
|
1367
1369
|
throw new Error(`addMeasure: "${r.table}" doesn't share the query's grouping`);
|
|
1368
1370
|
const s = we(r.where), i = n.joins.find(($) => {
|
|
1369
|
-
var
|
|
1370
|
-
return
|
|
1371
|
+
var T;
|
|
1372
|
+
return bt($) && ((T = $.drillAcross) == null ? void 0 : T.synced) === !0 && $.query.from.table === r.table && Z(we($.query.where).slice($.drillAcross.copied), s);
|
|
1371
1373
|
});
|
|
1372
|
-
let { joins:
|
|
1374
|
+
let { joins: l } = n, a, o;
|
|
1373
1375
|
if (i) {
|
|
1374
|
-
|
|
1375
|
-
const $ = ((v = i.query.groupBy) == null ? void 0 : v.length) ?? 0,
|
|
1376
|
-
if (
|
|
1377
|
-
|
|
1376
|
+
a = i.alias;
|
|
1377
|
+
const $ = ((v = i.query.groupBy) == null ? void 0 : v.length) ?? 0, T = i.query.select.slice($).find((b) => Z(b.expression, r.value));
|
|
1378
|
+
if (T)
|
|
1379
|
+
o = T.alias;
|
|
1378
1380
|
else {
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
new Set(i.query.select.map((
|
|
1381
|
+
o = Re(
|
|
1382
|
+
on(r.value, "value"),
|
|
1383
|
+
new Set(i.query.select.map((C) => C.alias ?? ""))
|
|
1382
1384
|
);
|
|
1383
|
-
const b = { id: W(), alias:
|
|
1384
|
-
|
|
1385
|
+
const b = { id: W(), alias: o, expression: r.value };
|
|
1386
|
+
l = l.map((C) => C === i ? { ...i, query: { ...i.query, select: [...i.query.select, b] } } : C);
|
|
1385
1387
|
}
|
|
1386
1388
|
} else {
|
|
1387
|
-
|
|
1388
|
-
const $ = { id: W(), alias:
|
|
1389
|
+
o = on(r.value, "value");
|
|
1390
|
+
const $ = { id: W(), alias: o, expression: r.value }, T = Et(e, n, r.table, s, [$]), b = /* @__PURE__ */ new Set([
|
|
1389
1391
|
...Q(n).map(D),
|
|
1390
1392
|
...me(e).map((R) => R.name)
|
|
1391
|
-
]),
|
|
1392
|
-
|
|
1393
|
+
]), C = `${r.table}_per_${T.keyNames.join("_and_")}`.slice(0, Y - 4);
|
|
1394
|
+
a = Re(C, b);
|
|
1393
1395
|
const G = {
|
|
1394
1396
|
id: W(),
|
|
1395
|
-
alias:
|
|
1397
|
+
alias: a,
|
|
1396
1398
|
joinType: "left",
|
|
1397
|
-
query:
|
|
1398
|
-
on:
|
|
1399
|
-
drillAcross: { synced: !0, copied:
|
|
1399
|
+
query: T.query,
|
|
1400
|
+
on: St(e, n, a, T.keyNames),
|
|
1401
|
+
drillAcross: { synced: !0, copied: T.copied }
|
|
1400
1402
|
};
|
|
1401
|
-
|
|
1403
|
+
l = [...l, G];
|
|
1402
1404
|
}
|
|
1403
1405
|
const c = {
|
|
1404
1406
|
type: "function",
|
|
1405
1407
|
name: "MAX",
|
|
1406
|
-
args: [
|
|
1407
|
-
}, m = new Set(n.select.map(($) => $.alias).filter(($) => !!$)), d = Re(`${r.table}_${
|
|
1408
|
+
args: [an(a, o)]
|
|
1409
|
+
}, m = new Set(n.select.map(($) => $.alias).filter(($) => !!$)), d = Re(`${r.table}_${o}`.slice(0, Y - 4), m);
|
|
1408
1410
|
let f = r.selectId !== void 0 ? n.select.map(($) => $.id === r.selectId ? { ...$, expression: c, alias: $.alias || d } : $) : [...n.select, { id: W(), expression: c, alias: d }];
|
|
1409
|
-
return f = [...n.groupBy.filter(($) => !f.some((
|
|
1411
|
+
return f = [...n.groupBy.filter(($) => !f.some((T) => Z(T.expression, $.expression))).map(($) => ({ id: W(), expression: $.expression })), ...f], { ...n, joins: l, select: f };
|
|
1410
1412
|
}
|
|
1411
|
-
function
|
|
1413
|
+
function Tt(e, n, r) {
|
|
1412
1414
|
var m, d;
|
|
1413
|
-
if (!
|
|
1415
|
+
if (!bt(r)) return;
|
|
1414
1416
|
const t = r, s = t.query.from.table, i = n.groupBy ?? [];
|
|
1415
1417
|
if (s === void 0 || i.length === 0) return;
|
|
1416
|
-
const
|
|
1417
|
-
if (!
|
|
1418
|
-
const
|
|
1418
|
+
const l = vt(e, n).find((f) => f.table.name === s);
|
|
1419
|
+
if (!l || l.unlinked !== void 0) return;
|
|
1420
|
+
const a = ((m = t.query.groupBy) == null ? void 0 : m.length) ?? 0, o = we(t.query.where).slice(((d = t.drillAcross) == null ? void 0 : d.copied) ?? 0), c = Et(e, n, s, o, t.query.select.slice(a), t.query);
|
|
1419
1421
|
return {
|
|
1420
1422
|
...t,
|
|
1421
1423
|
query: c.query,
|
|
1422
|
-
on:
|
|
1424
|
+
on: St(e, n, t.alias, c.keyNames),
|
|
1423
1425
|
drillAcross: { synced: !0, copied: c.copied }
|
|
1424
1426
|
};
|
|
1425
1427
|
}
|
|
@@ -1434,41 +1436,41 @@ function Qe(e) {
|
|
|
1434
1436
|
};
|
|
1435
1437
|
}
|
|
1436
1438
|
function Nr(e, n, r) {
|
|
1437
|
-
const t =
|
|
1439
|
+
const t = Tt(e, n, r);
|
|
1438
1440
|
return t !== void 0 && Z(Qe(t), Qe(r));
|
|
1439
1441
|
}
|
|
1440
|
-
function
|
|
1442
|
+
function Nt(e, n, r) {
|
|
1441
1443
|
let t = !1;
|
|
1442
|
-
const s = (
|
|
1443
|
-
|
|
1444
|
-
(c, m) =>
|
|
1444
|
+
const s = (a, o) => We(
|
|
1445
|
+
o,
|
|
1446
|
+
(c, m) => Nt(
|
|
1445
1447
|
e,
|
|
1446
|
-
|
|
1448
|
+
a && H(a) === H(o) ? be(a)[m] : void 0,
|
|
1447
1449
|
c
|
|
1448
1450
|
)
|
|
1449
1451
|
), i = r.from.query ? (() => {
|
|
1450
|
-
const
|
|
1451
|
-
return
|
|
1452
|
+
const a = n != null && n.from.query && n.from.id === r.from.id ? n.from.query : void 0, o = s(a, r.from.query);
|
|
1453
|
+
return o === r.from.query ? r.from : { ...r.from, query: o };
|
|
1452
1454
|
})() : r.from;
|
|
1453
1455
|
i !== r.from && (t = !0);
|
|
1454
|
-
const
|
|
1456
|
+
const l = r.joins.map((a) => {
|
|
1455
1457
|
var d;
|
|
1456
|
-
if (!
|
|
1457
|
-
const
|
|
1458
|
-
let m = c ===
|
|
1458
|
+
if (!a.query) return a;
|
|
1459
|
+
const o = n == null ? void 0 : n.joins.find((f) => f.id === a.id && f.query !== void 0), c = s(o == null ? void 0 : o.query, a.query);
|
|
1460
|
+
let m = c === a.query ? a : { ...a, query: c };
|
|
1459
1461
|
if ((d = m.drillAcross) != null && d.synced) {
|
|
1460
|
-
const f =
|
|
1462
|
+
const f = Tt(e, r, m);
|
|
1461
1463
|
f && !Z(f, m) && (m = f);
|
|
1462
|
-
} else if (m.drillAcross && (
|
|
1464
|
+
} else if (m.drillAcross && (o != null && o.drillAcross) && !Z(Qe(o), Qe(m))) {
|
|
1463
1465
|
const f = { ...m };
|
|
1464
1466
|
delete f.drillAcross, m = f;
|
|
1465
1467
|
}
|
|
1466
|
-
return m !==
|
|
1468
|
+
return m !== a && (t = !0), m;
|
|
1467
1469
|
});
|
|
1468
|
-
return t ? { ...r, from: i, joins:
|
|
1470
|
+
return t ? { ...r, from: i, joins: l } : r;
|
|
1469
1471
|
}
|
|
1470
1472
|
function Ar(e, n, r) {
|
|
1471
|
-
return ue(r) ?
|
|
1473
|
+
return ue(r) ? Nt(e, n && ue(n) ? n : void 0, r) : r;
|
|
1472
1474
|
}
|
|
1473
1475
|
const js = ["text", "number", "date", "boolean"], kr = {
|
|
1474
1476
|
varchar: "text",
|
|
@@ -1479,7 +1481,7 @@ const js = ["text", "number", "date", "boolean"], kr = {
|
|
|
1479
1481
|
time: "time",
|
|
1480
1482
|
boolean: "boolean"
|
|
1481
1483
|
};
|
|
1482
|
-
function
|
|
1484
|
+
function At(e) {
|
|
1483
1485
|
return kr[e];
|
|
1484
1486
|
}
|
|
1485
1487
|
function xr(e) {
|
|
@@ -1499,22 +1501,22 @@ function _e(e) {
|
|
|
1499
1501
|
if (typeof n == "number") return Number.isInteger(n) ? "integer" : "number";
|
|
1500
1502
|
if (typeof n == "bigint") return "integer";
|
|
1501
1503
|
if (typeof n == "boolean") return "boolean";
|
|
1502
|
-
if (n instanceof Date) return
|
|
1504
|
+
if (n instanceof Date) return tt(n) ? "date" : "dateTime";
|
|
1503
1505
|
}
|
|
1504
|
-
function
|
|
1506
|
+
function vn(e, n, r) {
|
|
1505
1507
|
if (!e) return;
|
|
1506
|
-
const t = (s) =>
|
|
1508
|
+
const t = (s) => vn(s, n, r);
|
|
1507
1509
|
switch (e.type) {
|
|
1508
1510
|
case "field":
|
|
1509
1511
|
return n(e);
|
|
1510
1512
|
case "literal":
|
|
1511
1513
|
return _e(e);
|
|
1512
1514
|
case "function": {
|
|
1513
|
-
const s =
|
|
1514
|
-
if ((
|
|
1515
|
-
return xr(
|
|
1516
|
-
const
|
|
1517
|
-
return
|
|
1515
|
+
const s = et(e.name, r), i = (s == null ? void 0 : s.args.findIndex((o) => o.kind === "type-name")) ?? -1, l = i >= 0 ? e.args[i] : void 0;
|
|
1516
|
+
if ((l == null ? void 0 : l.type) === "literal" && typeof l.value == "string")
|
|
1517
|
+
return xr(l.value);
|
|
1518
|
+
const a = s == null ? void 0 : s.returns;
|
|
1519
|
+
return a === "arg" ? t(e.args[0]) : a;
|
|
1518
1520
|
}
|
|
1519
1521
|
case "if":
|
|
1520
1522
|
return t(e.then) ?? t(e.else);
|
|
@@ -1524,8 +1526,8 @@ function $n(e, n, r) {
|
|
|
1524
1526
|
return;
|
|
1525
1527
|
}
|
|
1526
1528
|
}
|
|
1527
|
-
const Br = /^[+-]?(\d+\.?\d*|\.\d+)(e[+-]?\d+)?$/i,
|
|
1528
|
-
function
|
|
1529
|
+
const Br = /^[+-]?(\d+\.?\d*|\.\d+)(e[+-]?\d+)?$/i, Ir = /^([01]\d|2[0-3]):([0-5]\d)(?::([0-5]\d))?$/, Cr = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2}))?)?$/;
|
|
1530
|
+
function Sn(e, n) {
|
|
1529
1531
|
if (e === "" || e === null) return e;
|
|
1530
1532
|
const r = typeof e == "string" ? e.trim() : void 0;
|
|
1531
1533
|
switch (n) {
|
|
@@ -1534,18 +1536,18 @@ function vn(e, n) {
|
|
|
1534
1536
|
case "integer":
|
|
1535
1537
|
case "number": {
|
|
1536
1538
|
let t;
|
|
1537
|
-
return typeof e == "number" ? t = e : typeof e == "bigint" ? t = Number(e) : r !== void 0 && Br.test(r) && (t = Number(r)), t === void 0 || !
|
|
1539
|
+
return typeof e == "number" ? t = e : typeof e == "bigint" ? t = Number(e) : r !== void 0 && Br.test(r) && (t = Number(r)), t === void 0 || !tn(t) || n === "integer" && !Number.isInteger(t) ? void 0 : t;
|
|
1538
1540
|
}
|
|
1539
1541
|
case "date":
|
|
1540
1542
|
case "dateTime": {
|
|
1541
|
-
if (e instanceof Date) return n === "date" && !
|
|
1542
|
-
const t = r === void 0 ? null :
|
|
1543
|
+
if (e instanceof Date) return n === "date" && !tt(e) ? void 0 : e;
|
|
1544
|
+
const t = r === void 0 ? null : Cr.exec(r);
|
|
1543
1545
|
if (!t) return;
|
|
1544
|
-
const [, s, i, a, o,
|
|
1545
|
-
return m.getUTCMonth() !== i - 1 || m.getUTCDate() !==
|
|
1546
|
+
const [, s, i, l, a, o, c] = t.map(Number), m = Yt(s, i, l);
|
|
1547
|
+
return m.getUTCMonth() !== i - 1 || m.getUTCDate() !== l ? void 0 : t[4] === void 0 ? m : n === "date" ? void 0 : new Date(Date.UTC(s, i - 1, l, a, o, Number.isNaN(c) ? 0 : c));
|
|
1546
1548
|
}
|
|
1547
1549
|
case "time": {
|
|
1548
|
-
const t = r === void 0 ? null :
|
|
1550
|
+
const t = r === void 0 ? null : Ir.exec(r);
|
|
1549
1551
|
return t ? `${t[1]}:${t[2]}:${t[3] ?? "00"}` : void 0;
|
|
1550
1552
|
}
|
|
1551
1553
|
case "boolean":
|
|
@@ -1559,25 +1561,25 @@ function Mr(e, n, r) {
|
|
|
1559
1561
|
var m;
|
|
1560
1562
|
if (n === void 0) return e;
|
|
1561
1563
|
const { as: t, empty: s, ...i } = e;
|
|
1562
|
-
let
|
|
1563
|
-
r != null && r.length &&
|
|
1564
|
-
const
|
|
1565
|
-
return t === void 0 && !!s == !!
|
|
1564
|
+
let l = Sn(e.value, n);
|
|
1565
|
+
r != null && r.length && l !== "" && l !== null && (l = (m = r.find((d) => String(d.value) === String(e.value))) == null ? void 0 : m.value);
|
|
1566
|
+
const a = s && n === "text", o = l === void 0 ? { type: "literal", value: "" } : { ...i, value: l, ...a ? { empty: s } : {} };
|
|
1567
|
+
return t === void 0 && !!s == !!a && l === e.value ? e : o;
|
|
1566
1568
|
}
|
|
1567
1569
|
function Ls(e, n, r, t = !1) {
|
|
1568
|
-
const s = e.items.flatMap((
|
|
1569
|
-
if (
|
|
1570
|
-
const
|
|
1571
|
-
return
|
|
1570
|
+
const s = e.items.flatMap((l) => {
|
|
1571
|
+
if (l.type !== "literal") return [l];
|
|
1572
|
+
const a = Mr(l, n, r);
|
|
1573
|
+
return a.value === "" && !a.empty && !t ? [] : [a];
|
|
1572
1574
|
});
|
|
1573
|
-
return s.length === e.items.length && s.every((
|
|
1575
|
+
return s.length === e.items.length && s.every((l, a) => l === e.items[a]) ? e : { ...e, items: s };
|
|
1574
1576
|
}
|
|
1575
1577
|
function Ds(e) {
|
|
1576
1578
|
const n = _e(e);
|
|
1577
1579
|
return n === "integer" || n === "number" ? "number" : n === "date" || n === "dateTime" ? "date" : n;
|
|
1578
1580
|
}
|
|
1579
1581
|
function Gs(e, n) {
|
|
1580
|
-
const t = (e.type === "literal" ?
|
|
1582
|
+
const t = (e.type === "literal" ? Sn(e.value, n) : void 0) ?? "", s = e.type === "literal" && e.empty && n === "text";
|
|
1581
1583
|
return {
|
|
1582
1584
|
type: "literal",
|
|
1583
1585
|
value: t,
|
|
@@ -1585,56 +1587,58 @@ function Gs(e, n) {
|
|
|
1585
1587
|
...s ? { empty: s } : {}
|
|
1586
1588
|
};
|
|
1587
1589
|
}
|
|
1590
|
+
const en = "IS NOT DISTINCT FROM";
|
|
1588
1591
|
function Rs(e, n = !1) {
|
|
1589
1592
|
if (e !== void 0)
|
|
1590
1593
|
return e === "text" ? [
|
|
1591
1594
|
"=",
|
|
1592
1595
|
"!=",
|
|
1596
|
+
en,
|
|
1593
1597
|
"LIKE",
|
|
1594
1598
|
"REGEXP",
|
|
1595
1599
|
"IN",
|
|
1596
1600
|
...n ? ["BETWEEN"] : [],
|
|
1597
1601
|
"IS NULL",
|
|
1598
1602
|
"IS NOT NULL"
|
|
1599
|
-
] : e === "boolean" ? ["=", "!=", "IS NULL", "IS NOT NULL"] : ["=", "!=", "<", "<=", ">", ">=", "IN", "BETWEEN", "IS NULL", "IS NOT NULL"];
|
|
1603
|
+
] : e === "boolean" ? ["=", "!=", en, "IS NULL", "IS NOT NULL"] : ["=", "!=", en, "<", "<=", ">", ">=", "IN", "BETWEEN", "IS NULL", "IS NOT NULL"];
|
|
1600
1604
|
}
|
|
1601
|
-
const
|
|
1605
|
+
const kt = "source", xt = "rows_", se = (e) => e ? { type: "literal", value: null, as: e } : { type: "literal", value: null }, ke = (e) => e.type === "literal" && e.value === null, En = (e) => new Map(
|
|
1602
1606
|
Q(e).map((n) => [D(n), n.table])
|
|
1603
1607
|
);
|
|
1604
|
-
function
|
|
1605
|
-
const r =
|
|
1608
|
+
function ln(e, n) {
|
|
1609
|
+
const r = En(e);
|
|
1606
1610
|
return ne(n).map((t) => r.get(t) ?? t);
|
|
1607
1611
|
}
|
|
1608
|
-
function
|
|
1609
|
-
const r =
|
|
1610
|
-
return
|
|
1612
|
+
function Bt(e, n) {
|
|
1613
|
+
const r = En(e);
|
|
1614
|
+
return mn(n, (t) => {
|
|
1611
1615
|
if (t.type !== "field" || t.table === void 0) return t;
|
|
1612
1616
|
const s = r.get(t.table);
|
|
1613
1617
|
return s === void 0 || s === t.table ? t : { ...t, table: s };
|
|
1614
1618
|
});
|
|
1615
1619
|
}
|
|
1616
1620
|
function Fe(e, n, r) {
|
|
1617
|
-
const t =
|
|
1618
|
-
return
|
|
1619
|
-
var
|
|
1620
|
-
const i = t.get(s.table ?? "") ?? s.table ?? "",
|
|
1621
|
-
return
|
|
1621
|
+
const t = En(n);
|
|
1622
|
+
return vn(r, (s) => {
|
|
1623
|
+
var a;
|
|
1624
|
+
const i = t.get(s.table ?? "") ?? s.table ?? "", l = (a = z(e, i)) == null ? void 0 : a.columns.find((o) => o.name === s.column);
|
|
1625
|
+
return l && At(l.type);
|
|
1622
1626
|
}, $e);
|
|
1623
1627
|
}
|
|
1624
|
-
function
|
|
1628
|
+
function Tn(e, n) {
|
|
1625
1629
|
let r = e;
|
|
1626
1630
|
for (let t = 2; n.has(r); t += 1) r = `${e}_${t}`;
|
|
1627
1631
|
return r;
|
|
1628
1632
|
}
|
|
1629
|
-
const Je = (e) => e ? e.type === "logical" && e.operator === "AND" ? e.operands : [e] : [],
|
|
1633
|
+
const Je = (e) => e ? e.type === "logical" && e.operator === "AND" ? e.operands : [e] : [], It = (e) => {
|
|
1630
1634
|
if (e.length !== 0)
|
|
1631
1635
|
return e.length === 1 ? e[0] : { type: "logical", operator: "AND", operands: e };
|
|
1632
|
-
}, ie = (e) => e.from.table,
|
|
1636
|
+
}, ie = (e) => e.from.table, Wn = (e, n) => z(e, n).label;
|
|
1633
1637
|
function Xe(e) {
|
|
1634
1638
|
const { from: n } = e;
|
|
1635
1639
|
return n.query && H(n.query) ? n : void 0;
|
|
1636
1640
|
}
|
|
1637
|
-
function
|
|
1641
|
+
function Ce(e, n) {
|
|
1638
1642
|
if (n === void 0) return Xe(e);
|
|
1639
1643
|
const r = oe(e).find((t) => t.id === n);
|
|
1640
1644
|
return r && H(r.query) ? r : void 0;
|
|
@@ -1647,49 +1651,49 @@ function Ke(e, n) {
|
|
|
1647
1651
|
const [r] = n;
|
|
1648
1652
|
return r.select.map((t, s) => {
|
|
1649
1653
|
var d;
|
|
1650
|
-
const i = K(t) ?? "",
|
|
1651
|
-
var
|
|
1652
|
-
return { branch: f, expression: (
|
|
1653
|
-
}).filter((f) => f.expression !== void 0 && !ke(f.expression)),
|
|
1654
|
-
var
|
|
1655
|
-
return (
|
|
1654
|
+
const i = K(t) ?? "", l = n.map((f) => {
|
|
1655
|
+
var h;
|
|
1656
|
+
return { branch: f, expression: (h = f.select[s]) == null ? void 0 : h.expression };
|
|
1657
|
+
}).filter((f) => f.expression !== void 0 && !ke(f.expression)), o = ((d = n.map((f) => {
|
|
1658
|
+
var h;
|
|
1659
|
+
return (h = f.select[s]) == null ? void 0 : h.expression;
|
|
1656
1660
|
}).filter(
|
|
1657
1661
|
(f) => (f == null ? void 0 : f.type) === "literal" && f.value === null
|
|
1658
|
-
).find((f) => f.as)) == null ? void 0 : d.as) ?? (
|
|
1659
|
-
if (i ===
|
|
1662
|
+
).find((f) => f.as)) == null ? void 0 : d.as) ?? (l[0] && Fe(e, l[0].branch, l[0].expression));
|
|
1663
|
+
if (i === kt && l.length === n.length && l.every((f) => f.expression.type === "literal"))
|
|
1660
1664
|
return {
|
|
1661
1665
|
name: i,
|
|
1662
1666
|
kind: "source",
|
|
1663
1667
|
tables: [],
|
|
1664
1668
|
type: "text"
|
|
1665
1669
|
};
|
|
1666
|
-
if (i.startsWith(
|
|
1670
|
+
if (i.startsWith(xt) && l.length === 1 && l[0].expression.type === "literal")
|
|
1667
1671
|
return {
|
|
1668
1672
|
name: i,
|
|
1669
1673
|
kind: "rows",
|
|
1670
1674
|
tables: [],
|
|
1671
1675
|
type: "integer"
|
|
1672
1676
|
};
|
|
1673
|
-
const c =
|
|
1677
|
+
const c = l[0];
|
|
1674
1678
|
if (!c)
|
|
1675
1679
|
return {
|
|
1676
1680
|
name: i,
|
|
1677
1681
|
kind: "shared",
|
|
1678
1682
|
tables: [],
|
|
1679
|
-
type:
|
|
1683
|
+
type: o
|
|
1680
1684
|
};
|
|
1681
|
-
const m =
|
|
1682
|
-
return
|
|
1685
|
+
const m = ln(c.branch, c.expression);
|
|
1686
|
+
return l.some((f) => ln(f.branch, f.expression).includes(ie(f.branch))) ? {
|
|
1683
1687
|
name: i,
|
|
1684
1688
|
kind: "own",
|
|
1685
1689
|
tables: m,
|
|
1686
|
-
type:
|
|
1690
|
+
type: o
|
|
1687
1691
|
} : {
|
|
1688
1692
|
name: i,
|
|
1689
1693
|
kind: "shared",
|
|
1690
1694
|
tables: m,
|
|
1691
|
-
type:
|
|
1692
|
-
expression:
|
|
1695
|
+
type: o,
|
|
1696
|
+
expression: Bt(c.branch, c.expression)
|
|
1693
1697
|
};
|
|
1694
1698
|
});
|
|
1695
1699
|
}
|
|
@@ -1698,66 +1702,66 @@ function qs(e, n) {
|
|
|
1698
1702
|
if (!r) return [];
|
|
1699
1703
|
const { union: t } = r.query;
|
|
1700
1704
|
return Ke(e, t).map((s, i) => {
|
|
1701
|
-
const
|
|
1705
|
+
const l = t.map((a) => !ke(a.select[i].expression));
|
|
1702
1706
|
return {
|
|
1703
1707
|
name: s.name,
|
|
1704
1708
|
kind: s.kind,
|
|
1705
|
-
filledBy: t.filter((
|
|
1706
|
-
nullIn: t.filter((
|
|
1709
|
+
filledBy: t.filter((a, o) => l[o]).map(ie),
|
|
1710
|
+
nullIn: t.filter((a, o) => !l[o]).map(ie)
|
|
1707
1711
|
};
|
|
1708
1712
|
});
|
|
1709
1713
|
}
|
|
1710
1714
|
function Ct(e, n, r, t, s) {
|
|
1711
|
-
const i =
|
|
1712
|
-
if (!
|
|
1713
|
-
const
|
|
1714
|
-
if (
|
|
1715
|
-
return `"${n}" can't reach column "${
|
|
1715
|
+
const i = Ie(e, n), l = r.filter((m) => m.kind === "shared" && m.expression), a = (m) => m.tables.every((d) => i.has(d));
|
|
1716
|
+
if (!l.some(a)) return `"${n}" shares no column with the union`;
|
|
1717
|
+
const o = l.find((m) => !a(m));
|
|
1718
|
+
if (o && s === "refuse")
|
|
1719
|
+
return `"${n}" can't reach column "${o.name}" (${o.tables.join(", ")})`;
|
|
1716
1720
|
const c = t.find((m) => !m.tables.every((d) => i.has(d)));
|
|
1717
1721
|
if (c)
|
|
1718
1722
|
return `"${n}" can't follow a condition on ${c.tables.join(", ")}`;
|
|
1719
1723
|
}
|
|
1720
|
-
function
|
|
1724
|
+
function Mt(e) {
|
|
1721
1725
|
const [n] = e, r = new Set(e.map(ie));
|
|
1722
1726
|
return Je(n.where).flatMap((t) => {
|
|
1723
|
-
const s =
|
|
1724
|
-
if (i.length === 0) return [{ operand:
|
|
1727
|
+
const s = ln(n, t), i = s.filter((l) => r.has(l));
|
|
1728
|
+
if (i.length === 0) return [{ operand: Bt(n, t), tables: s }];
|
|
1725
1729
|
if (i.length === s.length) return [];
|
|
1726
1730
|
throw new Error("a condition naming both a branch's own table and shared ones can't be placed: split it, or put it on the union instead");
|
|
1727
1731
|
});
|
|
1728
1732
|
}
|
|
1729
|
-
function
|
|
1730
|
-
const s = t.unshared ?? "refuse", i = Ke(e, n),
|
|
1733
|
+
function Hn(e, n, r, t) {
|
|
1734
|
+
const s = t.unshared ?? "refuse", i = Ke(e, n), l = Mt(n);
|
|
1731
1735
|
if (!z(e, r.table))
|
|
1732
1736
|
throw new Error(`addUnionTable: unknown table "${r.table}"`);
|
|
1733
1737
|
if (n.some((x) => ie(x) === r.table))
|
|
1734
1738
|
throw new Error(`addUnionTable: "${r.table}" is in the union already`);
|
|
1735
|
-
const
|
|
1736
|
-
if (
|
|
1737
|
-
const
|
|
1738
|
-
const F =
|
|
1739
|
+
const a = Ct(e, r.table, i, l, s);
|
|
1740
|
+
if (a) throw new Error(`addUnionTable: ${a}`);
|
|
1741
|
+
const o = Ie(e, r.table), c = r.label ?? Wn(e, r.table), m = i.map((x) => x.kind === "source" ? { type: "literal", value: c } : x.kind === "shared" && x.expression && x.tables.every((F) => o.has(F)) ? x.expression : se(x.type)), d = new Set(i.map((x) => x.name)), f = [], h = (x) => {
|
|
1742
|
+
const F = Tn(x, d);
|
|
1739
1743
|
return d.add(F), f.push(F), F;
|
|
1740
1744
|
}, v = [], $ = {
|
|
1741
1745
|
from: { table: r.table },
|
|
1742
1746
|
joins: []
|
|
1743
1747
|
};
|
|
1744
1748
|
r.columns.forEach((x) => {
|
|
1745
|
-
const F = x.alias ?? (x.expression.type === "field" ? x.expression.column : void 0) ?? "value",
|
|
1746
|
-
v.push({ name:
|
|
1749
|
+
const F = x.alias ?? (x.expression.type === "field" ? x.expression.column : void 0) ?? "value", he = Fe(e, $, x.expression);
|
|
1750
|
+
v.push({ name: h(F), own: x.expression, others: () => se(he) });
|
|
1747
1751
|
}), t.source && !i.some((x) => x.kind === "source") && v.push({
|
|
1748
|
-
name:
|
|
1752
|
+
name: h(kt),
|
|
1749
1753
|
own: { type: "literal", value: c },
|
|
1750
|
-
others: (x) => ({ type: "literal", value:
|
|
1754
|
+
others: (x) => ({ type: "literal", value: Wn(e, ie(x)) })
|
|
1751
1755
|
});
|
|
1752
|
-
const
|
|
1753
|
-
(t.rowMarkers || i.some((x) => x.kind === "rows")) && (n.filter((x) => !d.has(
|
|
1756
|
+
const T = (x) => `${xt}${x}`;
|
|
1757
|
+
(t.rowMarkers || i.some((x) => x.kind === "rows")) && (n.filter((x) => !d.has(T(ie(x)))).forEach((x) => {
|
|
1754
1758
|
v.push({
|
|
1755
|
-
name:
|
|
1759
|
+
name: h(T(ie(x))),
|
|
1756
1760
|
own: se("integer"),
|
|
1757
1761
|
others: (F) => F === x ? { type: "literal", value: 1 } : se("integer")
|
|
1758
1762
|
});
|
|
1759
1763
|
}), v.push({
|
|
1760
|
-
name:
|
|
1764
|
+
name: h(T(r.table)),
|
|
1761
1765
|
own: { type: "literal", value: 1 },
|
|
1762
1766
|
others: () => se("integer")
|
|
1763
1767
|
}));
|
|
@@ -1765,10 +1769,10 @@ function Wn(e, n, r, t) {
|
|
|
1765
1769
|
id: W(),
|
|
1766
1770
|
alias: x,
|
|
1767
1771
|
expression: F
|
|
1768
|
-
}),
|
|
1772
|
+
}), C = n.map((x) => ({
|
|
1769
1773
|
...x,
|
|
1770
1774
|
select: [...x.select, ...v.map((F) => b(F.name, F.others(x)))]
|
|
1771
|
-
})), G =
|
|
1775
|
+
})), G = It([...l.map((x) => x.operand), ...Je(r.where)]);
|
|
1772
1776
|
let R = {
|
|
1773
1777
|
id: "",
|
|
1774
1778
|
from: { table: r.table },
|
|
@@ -1786,24 +1790,24 @@ function Wn(e, n, r, t) {
|
|
|
1786
1790
|
from: X,
|
|
1787
1791
|
joins: ee,
|
|
1788
1792
|
select: ye,
|
|
1789
|
-
where:
|
|
1793
|
+
where: pe
|
|
1790
1794
|
} = R, te = {
|
|
1791
1795
|
from: X,
|
|
1792
1796
|
joins: ee,
|
|
1793
1797
|
select: ye,
|
|
1794
|
-
...
|
|
1798
|
+
...pe ? { where: pe } : {}
|
|
1795
1799
|
};
|
|
1796
|
-
return { union: [...
|
|
1800
|
+
return { union: [...C, te], added: f };
|
|
1797
1801
|
}
|
|
1798
|
-
function
|
|
1802
|
+
function Ot(e) {
|
|
1799
1803
|
const n = (s) => new Error(`addUnionTable: ${s}`);
|
|
1800
1804
|
if (e.from.query) throw n("the query reads from a derived table");
|
|
1801
1805
|
if (e.select.length === 0) throw n("the query picks no columns (SELECT *)");
|
|
1802
1806
|
if ((e.groupBy ?? []).length > 0 || e.having || e.distinct || (e.orderBy ?? []).length > 0 || e.select.some((s) => le(s.expression, $e)))
|
|
1803
1807
|
throw n("a union is of detail rows: no aggregates, GROUP BY, HAVING, DISTINCT or ORDER BY");
|
|
1804
1808
|
const r = /* @__PURE__ */ new Set(), t = e.select.map((s, i) => {
|
|
1805
|
-
const
|
|
1806
|
-
return r.add(
|
|
1809
|
+
const l = Tn(K(s) ?? `column_${i + 1}`, r);
|
|
1810
|
+
return r.add(l), { ...s, alias: l };
|
|
1807
1811
|
});
|
|
1808
1812
|
return {
|
|
1809
1813
|
from: e.from,
|
|
@@ -1815,23 +1819,23 @@ function Mt(e) {
|
|
|
1815
1819
|
function Qs(e, n, r, t = {}) {
|
|
1816
1820
|
const s = Xe(n);
|
|
1817
1821
|
if (s) {
|
|
1818
|
-
const { union: c, added: m } =
|
|
1822
|
+
const { union: c, added: m } = Hn(e, s.query.union, r, t), d = { ...s, query: { ...s.query, union: c } }, f = n.select.length === 0 ? [] : [
|
|
1819
1823
|
...n.select,
|
|
1820
|
-
...m.map((
|
|
1824
|
+
...m.map((h) => ({
|
|
1821
1825
|
id: W(),
|
|
1822
|
-
expression: { type: "field", table: s.alias, column:
|
|
1826
|
+
expression: { type: "field", table: s.alias, column: h }
|
|
1823
1827
|
}))
|
|
1824
1828
|
];
|
|
1825
1829
|
return { ...n, from: d, select: f };
|
|
1826
1830
|
}
|
|
1827
|
-
const i =
|
|
1831
|
+
const i = Ot(n), { union: l } = Hn(e, [i], r, t), a = t.alias ?? "stacked", o = l[0].select.map((c) => c.alias);
|
|
1828
1832
|
return {
|
|
1829
1833
|
id: n.id,
|
|
1830
|
-
from: { id: W(), alias:
|
|
1834
|
+
from: { id: W(), alias: a, query: { union: l, all: !0 } },
|
|
1831
1835
|
joins: [],
|
|
1832
|
-
select:
|
|
1836
|
+
select: o.map((c) => ({
|
|
1833
1837
|
id: W(),
|
|
1834
|
-
expression: { type: "field", table:
|
|
1838
|
+
expression: { type: "field", table: a, column: c }
|
|
1835
1839
|
})),
|
|
1836
1840
|
...n.limit !== void 0 ? { limit: n.limit } : {},
|
|
1837
1841
|
...n.offset !== void 0 ? { offset: n.offset } : {}
|
|
@@ -1841,140 +1845,140 @@ function _s(e, n, r = {}) {
|
|
|
1841
1845
|
const t = Xe(n);
|
|
1842
1846
|
let s;
|
|
1843
1847
|
try {
|
|
1844
|
-
s = t ? t.query.union : [
|
|
1848
|
+
s = t ? t.query.union : [Ot(n)];
|
|
1845
1849
|
} catch {
|
|
1846
1850
|
return [];
|
|
1847
1851
|
}
|
|
1848
1852
|
const i = Ke(e, s);
|
|
1849
|
-
let
|
|
1853
|
+
let l;
|
|
1850
1854
|
try {
|
|
1851
|
-
|
|
1855
|
+
l = Mt(s);
|
|
1852
1856
|
} catch {
|
|
1853
1857
|
return [];
|
|
1854
1858
|
}
|
|
1855
|
-
const
|
|
1859
|
+
const a = /* @__PURE__ */ new Set([
|
|
1856
1860
|
...s.map(ie),
|
|
1857
|
-
...i.flatMap((
|
|
1861
|
+
...i.flatMap((o) => o.kind === "shared" ? o.tables : [])
|
|
1858
1862
|
]);
|
|
1859
|
-
return e.tables.map((
|
|
1863
|
+
return e.tables.map((o) => o.name).filter((o) => !a.has(o) && Ct(e, o, i, l, r.unshared ?? "refuse") === void 0);
|
|
1860
1864
|
}
|
|
1861
1865
|
function Or(e, n, r) {
|
|
1862
|
-
const t = r.union.findIndex((
|
|
1866
|
+
const t = r.union.findIndex((o, c) => o.select !== n.union[c].select);
|
|
1863
1867
|
if (t === -1) return r;
|
|
1864
|
-
const s = r.union[t], i = n.union[t].select.map((
|
|
1865
|
-
if (c === t) return
|
|
1868
|
+
const s = r.union[t], i = n.union[t].select.map((o) => o.id), l = r.union.map((o, c) => {
|
|
1869
|
+
if (c === t) return o;
|
|
1866
1870
|
const m = s.select.map((f) => {
|
|
1867
|
-
const
|
|
1868
|
-
if (!
|
|
1869
|
-
return { id: W(), ...
|
|
1870
|
-
let b =
|
|
1871
|
-
K(
|
|
1872
|
-
const { expression:
|
|
1873
|
-
return ke(
|
|
1871
|
+
const h = K(f), v = Fe(e, s, f.expression), $ = i.indexOf(f.id), T = $ === -1 ? void 0 : o.select[$];
|
|
1872
|
+
if (!T)
|
|
1873
|
+
return { id: W(), ...h ? { alias: h } : {}, expression: se(v) };
|
|
1874
|
+
let b = T;
|
|
1875
|
+
K(T) !== h && (b = { ...b, alias: h });
|
|
1876
|
+
const { expression: C } = T;
|
|
1877
|
+
return ke(C) && C.type === "literal" && C.as !== v && (b = { ...b, expression: se(v) }), b;
|
|
1874
1878
|
});
|
|
1875
|
-
return m.length ===
|
|
1876
|
-
}),
|
|
1877
|
-
const c =
|
|
1879
|
+
return m.length === o.select.length && m.every((f, h) => f === o.select[h]) ? o : { ...o, select: m };
|
|
1880
|
+
}), a = l.map((o) => {
|
|
1881
|
+
const c = o.select.map((m, d) => {
|
|
1878
1882
|
const { expression: f } = m;
|
|
1879
1883
|
if (f.type !== "literal" || f.value !== null) return m;
|
|
1880
|
-
const
|
|
1884
|
+
const h = l.find(($) => {
|
|
1881
1885
|
var b;
|
|
1882
|
-
const
|
|
1883
|
-
return
|
|
1884
|
-
}), v =
|
|
1886
|
+
const T = (b = $.select[d]) == null ? void 0 : b.expression;
|
|
1887
|
+
return T !== void 0 && !ke(T);
|
|
1888
|
+
}), v = h && Fe(e, h, h.select[d].expression);
|
|
1885
1889
|
return v && f.as !== v ? { ...m, expression: se(v) } : m;
|
|
1886
1890
|
});
|
|
1887
|
-
return c.every((m, d) => m ===
|
|
1891
|
+
return c.every((m, d) => m === o.select[d]) ? o : { ...o, select: c };
|
|
1888
1892
|
});
|
|
1889
|
-
return
|
|
1893
|
+
return a.every((o, c) => o === r.union[c]) ? r : { ...r, union: a };
|
|
1890
1894
|
}
|
|
1891
1895
|
function Ur(e, n) {
|
|
1892
|
-
const r = (i) => i.union[0].select.map((
|
|
1893
|
-
return t.length !== s.length ? [] : t.flatMap((i,
|
|
1894
|
-
const
|
|
1895
|
-
return i &&
|
|
1896
|
+
const r = (i) => i.union[0].select.map((l) => K(l)), t = r(e), s = r(n);
|
|
1897
|
+
return t.length !== s.length ? [] : t.flatMap((i, l) => {
|
|
1898
|
+
const a = s[l];
|
|
1899
|
+
return i && a && i !== a && !s.includes(i) && !t.includes(a) ? [{ from: i, to: a }] : [];
|
|
1896
1900
|
});
|
|
1897
1901
|
}
|
|
1898
|
-
function
|
|
1902
|
+
function Ut(e, n, r) {
|
|
1899
1903
|
let t = r;
|
|
1900
1904
|
n && H(n) && H(t) && n.union.length === t.union.length && (t = Or(e, n, t));
|
|
1901
1905
|
const s = t;
|
|
1902
|
-
return We(s, (i,
|
|
1906
|
+
return We(s, (i, l) => Nn(
|
|
1903
1907
|
e,
|
|
1904
|
-
n && H(n) === H(s) ? be(n)[
|
|
1908
|
+
n && H(n) === H(s) ? be(n)[l] : void 0,
|
|
1905
1909
|
i
|
|
1906
1910
|
));
|
|
1907
1911
|
}
|
|
1908
|
-
function
|
|
1912
|
+
function Nn(e, n, r) {
|
|
1909
1913
|
const t = (f) => {
|
|
1910
|
-
var
|
|
1911
|
-
return
|
|
1914
|
+
var h;
|
|
1915
|
+
return Ut(
|
|
1912
1916
|
e,
|
|
1913
|
-
n && ((
|
|
1917
|
+
n && ((h = oe(n).find((v) => v.id === f.id)) == null ? void 0 : h.query),
|
|
1914
1918
|
f.query
|
|
1915
1919
|
);
|
|
1916
|
-
}, { from: s } = r, i = s.query && t(s),
|
|
1917
|
-
let
|
|
1918
|
-
const
|
|
1920
|
+
}, { from: s } = r, i = s.query && t(s), l = i && i !== s.query ? { ...s, query: i } : s;
|
|
1921
|
+
let a = !1;
|
|
1922
|
+
const o = r.joins.map((f) => {
|
|
1919
1923
|
if (!f.query) return f;
|
|
1920
|
-
const
|
|
1921
|
-
return
|
|
1924
|
+
const h = t(f);
|
|
1925
|
+
return h === f.query ? f : (a = !0, { ...f, query: h });
|
|
1922
1926
|
});
|
|
1923
|
-
if (
|
|
1924
|
-
const c = { ...r, from:
|
|
1927
|
+
if (l === s && !a) return r;
|
|
1928
|
+
const c = { ...r, from: l, joins: o }, m = oe(c).flatMap((f) => {
|
|
1925
1929
|
var v;
|
|
1926
|
-
const
|
|
1927
|
-
return
|
|
1930
|
+
const h = n && ((v = oe(n).find(($) => $.id === f.id)) == null ? void 0 : v.query);
|
|
1931
|
+
return h && H(h) && H(f.query) ? Ur(h, f.query).map(($) => ({ ...$, alias: f.alias })) : [];
|
|
1928
1932
|
});
|
|
1929
1933
|
if (m.length === 0) return c;
|
|
1930
1934
|
const d = (f) => {
|
|
1931
1935
|
if (f.type !== "field") return xe(f, d);
|
|
1932
|
-
const
|
|
1933
|
-
return
|
|
1936
|
+
const h = m.find((v) => v.alias === f.table && v.from === f.column);
|
|
1937
|
+
return h ? { ...f, column: h.to } : f;
|
|
1934
1938
|
};
|
|
1935
1939
|
return Pe(c, d, (f) => f);
|
|
1936
1940
|
}
|
|
1937
1941
|
function jr(e, n, r) {
|
|
1938
1942
|
if (!ue(r)) return r;
|
|
1939
1943
|
const t = n && ue(n) ? n : void 0;
|
|
1940
|
-
return
|
|
1941
|
-
var
|
|
1942
|
-
return
|
|
1944
|
+
return fn(Nn(e, t, r), (s, i) => {
|
|
1945
|
+
var l, a;
|
|
1946
|
+
return Ut(
|
|
1943
1947
|
e,
|
|
1944
|
-
(
|
|
1948
|
+
(a = (l = t == null ? void 0 : t.shared) == null ? void 0 : l.find((o) => o.id === i.id)) == null ? void 0 : a.query,
|
|
1945
1949
|
s
|
|
1946
1950
|
);
|
|
1947
1951
|
});
|
|
1948
1952
|
}
|
|
1949
|
-
function
|
|
1950
|
-
return e.type === "field" ? [e] : fe(e).flatMap(
|
|
1953
|
+
function jt(e) {
|
|
1954
|
+
return e.type === "field" ? [e] : fe(e).flatMap(jt);
|
|
1951
1955
|
}
|
|
1952
|
-
const
|
|
1956
|
+
const Lt = (e, n, r) => jt(e).some((t) => t.table === n && r.has(t.column ?? ""));
|
|
1953
1957
|
function Lr(e, n, r) {
|
|
1954
|
-
const t = n.query.union.filter((
|
|
1958
|
+
const t = n.query.union.filter((o, c) => c !== r), s = [
|
|
1955
1959
|
e.where,
|
|
1956
1960
|
e.having,
|
|
1957
|
-
...e.joins.map((
|
|
1958
|
-
...(e.groupBy ?? []).map((
|
|
1959
|
-
...(e.orderBy ?? []).map((
|
|
1960
|
-
].filter((
|
|
1961
|
-
(c) =>
|
|
1962
|
-
),
|
|
1963
|
-
const m = t.every((f) => f.select[c] && ke(f.select[c].expression)), d = K(
|
|
1961
|
+
...e.joins.map((o) => o.on),
|
|
1962
|
+
...(e.groupBy ?? []).map((o) => o.expression),
|
|
1963
|
+
...(e.orderBy ?? []).map((o) => o.expression)
|
|
1964
|
+
].filter((o) => o !== void 0), i = (o) => s.some(
|
|
1965
|
+
(c) => Lt(c, n.alias, /* @__PURE__ */ new Set([o]))
|
|
1966
|
+
), l = new Set(t[0].select.flatMap((o, c) => {
|
|
1967
|
+
const m = t.every((f) => f.select[c] && ke(f.select[c].expression)), d = K(o);
|
|
1964
1968
|
return m && !(d && i(d)) ? [c] : [];
|
|
1965
|
-
})),
|
|
1966
|
-
const c = K(t[0].select[
|
|
1969
|
+
})), a = new Set([...l].flatMap((o) => {
|
|
1970
|
+
const c = K(t[0].select[o]);
|
|
1967
1971
|
return c ? [c] : [];
|
|
1968
1972
|
}));
|
|
1969
|
-
return { at:
|
|
1973
|
+
return { at: l, names: a };
|
|
1970
1974
|
}
|
|
1971
1975
|
function Fs(e, n, r) {
|
|
1972
|
-
const t =
|
|
1976
|
+
const t = Ce(e, r);
|
|
1973
1977
|
if (!t || !t.query.union[n] || t.query.union.length < 2) return e;
|
|
1974
|
-
const s = Lr(e, t, n), i = t.query.union.filter((c, m) => m !== n),
|
|
1975
|
-
(c) => !
|
|
1976
|
-
),
|
|
1977
|
-
return Me({ ...e, select:
|
|
1978
|
+
const s = Lr(e, t, n), i = t.query.union.filter((c, m) => m !== n), l = s.at.size < i[0].select.length ? s : { at: /* @__PURE__ */ new Set(), names: /* @__PURE__ */ new Set() }, a = e.select.filter(
|
|
1979
|
+
(c) => !Lt(c.expression, t.alias, l.names)
|
|
1980
|
+
), o = i.map((c) => ({ ...c, select: c.select.filter((m, d) => !l.at.has(d)) }));
|
|
1981
|
+
return Me({ ...e, select: a }, t, { ...t.query, union: o });
|
|
1978
1982
|
}
|
|
1979
1983
|
function Dr(e, n) {
|
|
1980
1984
|
const r = /* @__PURE__ */ new Set([
|
|
@@ -1994,27 +1998,27 @@ function Js(e, n, r, t) {
|
|
|
1994
1998
|
};
|
|
1995
1999
|
}
|
|
1996
2000
|
function Ps(e, n, r) {
|
|
1997
|
-
const t = /* @__PURE__ */ new Set(), s = e.select.map(($,
|
|
1998
|
-
const b =
|
|
2001
|
+
const t = /* @__PURE__ */ new Set(), s = e.select.map(($, T) => {
|
|
2002
|
+
const b = Tn(K($) ?? `column_${T + 1}`, t);
|
|
1999
2003
|
return t.add(b), { ...$, alias: b };
|
|
2000
2004
|
}), {
|
|
2001
2005
|
from: i,
|
|
2002
|
-
joins:
|
|
2003
|
-
where:
|
|
2004
|
-
groupBy:
|
|
2006
|
+
joins: l,
|
|
2007
|
+
where: a,
|
|
2008
|
+
groupBy: o,
|
|
2005
2009
|
having: c,
|
|
2006
2010
|
distinct: m
|
|
2007
2011
|
} = e, d = {
|
|
2008
2012
|
from: i,
|
|
2009
|
-
joins:
|
|
2013
|
+
joins: l,
|
|
2010
2014
|
select: s,
|
|
2011
|
-
...
|
|
2012
|
-
...
|
|
2015
|
+
...a ? { where: a } : {},
|
|
2016
|
+
...o ? { groupBy: o } : {},
|
|
2013
2017
|
...c ? { having: c } : {},
|
|
2014
2018
|
...m !== void 0 ? { distinct: m } : {}
|
|
2015
|
-
}, f = ($) => ({ type: "field", table: r, column: $ }),
|
|
2016
|
-
var
|
|
2017
|
-
return (
|
|
2019
|
+
}, f = ($) => ({ type: "field", table: r, column: $ }), h = ($) => {
|
|
2020
|
+
var T;
|
|
2021
|
+
return (T = s.find((b) => Z(b.expression, $))) == null ? void 0 : T.alias;
|
|
2018
2022
|
}, v = {
|
|
2019
2023
|
...e,
|
|
2020
2024
|
from: { id: n, alias: r, query: { union: [d], all: !0 } },
|
|
@@ -2022,20 +2026,20 @@ function Ps(e, n, r) {
|
|
|
2022
2026
|
select: s.map(($) => ({ id: W(), expression: f($.alias) })),
|
|
2023
2027
|
...e.orderBy ? {
|
|
2024
2028
|
orderBy: e.orderBy.map(($) => {
|
|
2025
|
-
const
|
|
2026
|
-
return
|
|
2029
|
+
const T = h($.expression);
|
|
2030
|
+
return T ? { ...$, expression: f(T) } : $;
|
|
2027
2031
|
})
|
|
2028
2032
|
} : {}
|
|
2029
2033
|
};
|
|
2030
2034
|
return ["where", "groupBy", "having", "distinct"].forEach(($) => delete v[$]), v;
|
|
2031
2035
|
}
|
|
2032
2036
|
function Vs(e, n, r, t) {
|
|
2033
|
-
const s =
|
|
2037
|
+
const s = Ce(n, r);
|
|
2034
2038
|
if (!s) return n;
|
|
2035
|
-
const i = Ke(e, s.query.union).map((
|
|
2039
|
+
const i = Ke(e, s.query.union).map((l) => ({
|
|
2036
2040
|
id: W(),
|
|
2037
|
-
...
|
|
2038
|
-
expression: se(
|
|
2041
|
+
...l.name ? { alias: l.name } : {},
|
|
2042
|
+
expression: se(l.type)
|
|
2039
2043
|
}));
|
|
2040
2044
|
return Me(n, s, {
|
|
2041
2045
|
...s.query,
|
|
@@ -2049,44 +2053,44 @@ function Ws(e, n, r, t) {
|
|
|
2049
2053
|
joinType: "left",
|
|
2050
2054
|
query: { union: [{ from: t, joins: [], select: [] }], all: !0 }
|
|
2051
2055
|
};
|
|
2052
|
-
return { ...n, joins: [...n.joins, { ...s, on:
|
|
2056
|
+
return { ...n, joins: [...n.joins, { ...s, on: ft(e, n, s) }] };
|
|
2053
2057
|
}
|
|
2054
2058
|
function Hs(e, n, r) {
|
|
2055
|
-
const t =
|
|
2059
|
+
const t = Ce(e, n);
|
|
2056
2060
|
return !t || t.query.all === r ? e : Me(e, t, { ...t.query, all: r });
|
|
2057
2061
|
}
|
|
2058
2062
|
function Xs(e, n, r, t, s) {
|
|
2059
|
-
const i =
|
|
2060
|
-
if (!i || !
|
|
2063
|
+
const i = Ce(n, r), [l, ...a] = (i == null ? void 0 : i.query.union) ?? [], o = l == null ? void 0 : l.select[t], c = s.trim();
|
|
2064
|
+
if (!i || !o || !c || c.length > Y || l.select.some((d) => K(d) === c)) return n;
|
|
2061
2065
|
const m = Me(n, i, {
|
|
2062
2066
|
...i.query,
|
|
2063
2067
|
union: [
|
|
2064
|
-
{ ...
|
|
2065
|
-
...
|
|
2068
|
+
{ ...l, select: l.select.map((d, f) => f === t ? { ...d, alias: c } : d) },
|
|
2069
|
+
...a
|
|
2066
2070
|
]
|
|
2067
2071
|
});
|
|
2068
|
-
return
|
|
2072
|
+
return Nn(e, n, m);
|
|
2069
2073
|
}
|
|
2070
2074
|
function Ks(e, n) {
|
|
2071
2075
|
var m;
|
|
2072
|
-
const r =
|
|
2076
|
+
const r = Ce(e, n);
|
|
2073
2077
|
if (!r || r.query.union.length !== 1) return e;
|
|
2074
2078
|
const [t] = r.query.union, s = new Map(t.select.flatMap((d) => {
|
|
2075
2079
|
const f = K(d);
|
|
2076
2080
|
return f ? [[f, d.expression]] : [];
|
|
2077
|
-
})), i = (d) => d.type === "field" && d.table === r.alias && s.has(d.column ?? ""),
|
|
2078
|
-
const f =
|
|
2081
|
+
})), i = (d) => d.type === "field" && d.table === r.alias && s.has(d.column ?? ""), l = (d) => mn(d, (f) => i(f) ? s.get(f.column) : f), a = e.select.map((d) => {
|
|
2082
|
+
const f = l(d.expression);
|
|
2079
2083
|
if (d.alias || !i(d.expression)) return { ...d, expression: f };
|
|
2080
|
-
const
|
|
2081
|
-
return K({ ...d, expression: f }) ===
|
|
2082
|
-
}),
|
|
2083
|
-
if (r === e.from && !
|
|
2084
|
-
const d = Pe({ ...e, select:
|
|
2084
|
+
const h = d.expression.column;
|
|
2085
|
+
return K({ ...d, expression: f }) === h ? { ...d, expression: f } : { ...d, expression: f, alias: h };
|
|
2086
|
+
}), o = t.groupBy !== void 0 || t.having !== void 0 || !!t.distinct || t.select.some((d) => le(d.expression, $e));
|
|
2087
|
+
if (r === e.from && !o) {
|
|
2088
|
+
const d = Pe({ ...e, select: a }, l, (f) => f);
|
|
2085
2089
|
return {
|
|
2086
2090
|
...d,
|
|
2087
2091
|
from: t.from,
|
|
2088
2092
|
joins: [...t.joins, ...d.joins],
|
|
2089
|
-
where:
|
|
2093
|
+
where: It([...Je(t.where), ...Je(d.where)]),
|
|
2090
2094
|
// Nothing picked: the query's own columns, as the union had them.
|
|
2091
2095
|
select: d.select.length > 0 ? d.select : t.select
|
|
2092
2096
|
};
|
|
@@ -2095,22 +2099,22 @@ function Ks(e, n) {
|
|
|
2095
2099
|
return r === e.from && c ? {
|
|
2096
2100
|
...e,
|
|
2097
2101
|
...t,
|
|
2098
|
-
select:
|
|
2102
|
+
select: a.length > 0 ? a : t.select,
|
|
2099
2103
|
...e.orderBy ? {
|
|
2100
|
-
orderBy: e.orderBy.map((d) => ({ ...d, expression:
|
|
2104
|
+
orderBy: e.orderBy.map((d) => ({ ...d, expression: l(d.expression) }))
|
|
2101
2105
|
} : {}
|
|
2102
2106
|
} : Me(e, r, t);
|
|
2103
2107
|
}
|
|
2104
|
-
const
|
|
2105
|
-
function
|
|
2108
|
+
const Xn = (e, n) => e.type === "binary" && e.operator === "IS NULL" && Z(e.left, n);
|
|
2109
|
+
function un(e) {
|
|
2106
2110
|
var n;
|
|
2107
2111
|
if (!e) return [];
|
|
2108
|
-
if (e.type === "logical" && e.operator === "AND") return e.operands.flatMap(
|
|
2112
|
+
if (e.type === "logical" && e.operator === "AND") return e.operands.flatMap(un);
|
|
2109
2113
|
if (e.type === "binary" && (e.operator ?? "=") === "=" && e.left.type === "field" && ((n = e.right) == null ? void 0 : n.type) === "field")
|
|
2110
2114
|
return [[e.left, e.right]];
|
|
2111
2115
|
if (e.type === "logical" && e.operator === "OR" && e.operands.length === 2) {
|
|
2112
|
-
const [[r] = [], t] = [
|
|
2113
|
-
if (r && t.type === "logical" && t.operator === "AND" && t.operands.length === 2 &&
|
|
2116
|
+
const [[r] = [], t] = [un(e.operands[0]), e.operands[1]];
|
|
2117
|
+
if (r && t.type === "logical" && t.operator === "AND" && t.operands.length === 2 && Xn(t.operands[0], r[0]) && Xn(t.operands[1], r[1]))
|
|
2114
2118
|
return [r];
|
|
2115
2119
|
}
|
|
2116
2120
|
return [];
|
|
@@ -2121,62 +2125,62 @@ function Gr(e, n) {
|
|
|
2121
2125
|
}
|
|
2122
2126
|
function Rr(e, n) {
|
|
2123
2127
|
const r = D(e.from), t = /* @__PURE__ */ new Map([[r, e.from.table]]), s = [r], i = e.shared ?? [];
|
|
2124
|
-
return e.joins.map((
|
|
2125
|
-
var f,
|
|
2126
|
-
const { id:
|
|
2127
|
-
t.set(c,
|
|
2128
|
-
const m =
|
|
2129
|
-
if (s.push(c),
|
|
2128
|
+
return e.joins.map((l) => {
|
|
2129
|
+
var f, h;
|
|
2130
|
+
const { id: a } = l, o = Te(l, i), c = D(l);
|
|
2131
|
+
t.set(c, l.table);
|
|
2132
|
+
const m = un(l.on).flatMap(([v, $]) => [[v, $], [$, v]]).filter(([v, $]) => v.table === c && s.includes($.table ?? ""));
|
|
2133
|
+
if (s.push(c), l.joinType === "cross")
|
|
2130
2134
|
return {
|
|
2131
|
-
joinId:
|
|
2135
|
+
joinId: a,
|
|
2132
2136
|
key: c,
|
|
2133
2137
|
parent: r,
|
|
2134
|
-
side:
|
|
2138
|
+
side: o && ut(o) ? "one" : "cross"
|
|
2135
2139
|
};
|
|
2136
|
-
if (
|
|
2137
|
-
const v = m.length > 0 &&
|
|
2140
|
+
if (l.table === void 0) {
|
|
2141
|
+
const v = m.length > 0 && o !== void 0 && !H(o) && Gr(o, m.map(([$]) => $.column));
|
|
2138
2142
|
return {
|
|
2139
|
-
joinId:
|
|
2143
|
+
joinId: a,
|
|
2140
2144
|
key: c,
|
|
2141
2145
|
parent: ((f = m[0]) == null ? void 0 : f[1].table) ?? r,
|
|
2142
2146
|
side: v ? "one" : "unknown"
|
|
2143
2147
|
};
|
|
2144
2148
|
}
|
|
2145
2149
|
return m.map(([v, $]) => {
|
|
2146
|
-
const
|
|
2147
|
-
if (b([
|
|
2150
|
+
const T = t.get($.table), b = (C, G) => n.relations.some((R) => R.fromTable === C[0] && R.fromColumn === C[1].column && R.toTable === G[0] && R.toColumn === G[1].column);
|
|
2151
|
+
if (b([l.table, v], [T, $]))
|
|
2148
2152
|
return {
|
|
2149
|
-
joinId:
|
|
2153
|
+
joinId: a,
|
|
2150
2154
|
key: c,
|
|
2151
2155
|
parent: $.table,
|
|
2152
2156
|
side: "many"
|
|
2153
2157
|
};
|
|
2154
|
-
if (b([
|
|
2158
|
+
if (b([T, $], [l.table, v]))
|
|
2155
2159
|
return {
|
|
2156
|
-
joinId:
|
|
2160
|
+
joinId: a,
|
|
2157
2161
|
key: c,
|
|
2158
2162
|
parent: $.table,
|
|
2159
2163
|
side: "one"
|
|
2160
2164
|
};
|
|
2161
2165
|
}).find((v) => v !== void 0) ?? {
|
|
2162
|
-
joinId:
|
|
2166
|
+
joinId: a,
|
|
2163
2167
|
key: c,
|
|
2164
|
-
parent: ((
|
|
2168
|
+
parent: ((h = m[0]) == null ? void 0 : h[1].table) ?? r,
|
|
2165
2169
|
side: "unknown"
|
|
2166
2170
|
};
|
|
2167
2171
|
});
|
|
2168
2172
|
}
|
|
2169
|
-
function
|
|
2170
|
-
const t = Rr(e, n).map((
|
|
2171
|
-
const
|
|
2173
|
+
function cn(e, n, r = {}) {
|
|
2174
|
+
const t = Rr(e, n).map((a) => a.side === "unknown" && r.unknown === "repeat" ? { ...a, side: "many" } : a), s = [D(e.from), ...t.map((a) => a.key)], i = (a) => {
|
|
2175
|
+
const o = /* @__PURE__ */ new Set([a]);
|
|
2172
2176
|
return t.forEach((c) => {
|
|
2173
|
-
|
|
2174
|
-
}),
|
|
2175
|
-
},
|
|
2176
|
-
return t.forEach((
|
|
2177
|
-
const
|
|
2178
|
-
|
|
2179
|
-
}),
|
|
2177
|
+
o.has(c.parent) && o.add(c.key);
|
|
2178
|
+
}), o;
|
|
2179
|
+
}, l = /* @__PURE__ */ new Set();
|
|
2180
|
+
return t.forEach((a) => {
|
|
2181
|
+
const o = i(a.key);
|
|
2182
|
+
a.side === "many" && s.filter((c) => !o.has(c)).forEach((c) => l.add(c)), a.side === "one" && o.forEach((c) => l.add(c)), a.side === "cross" && s.forEach((c) => l.add(c));
|
|
2183
|
+
}), l;
|
|
2180
2184
|
}
|
|
2181
2185
|
const qr = /* @__PURE__ */ new Set(["SUM", "AVG", "COUNT"]);
|
|
2182
2186
|
function Qr(e, n) {
|
|
@@ -2184,22 +2188,22 @@ function Qr(e, n) {
|
|
|
2184
2188
|
}
|
|
2185
2189
|
function Ys(e, n) {
|
|
2186
2190
|
const r = D(e.from);
|
|
2187
|
-
return e.joins.some((s) => s.joinType === "right" || s.joinType === "full") ||
|
|
2191
|
+
return e.joins.some((s) => s.joinType === "right" || s.joinType === "full") || cn(e, n).has(r) ? "many" : cn(e, n, { unknown: "repeat" }).has(r) ? "unknown" : "one";
|
|
2188
2192
|
}
|
|
2189
|
-
function
|
|
2193
|
+
function Dt(e, n, r = !1) {
|
|
2190
2194
|
const t = e.type === "function" && qr.has(e.name.toUpperCase()) && !e.distinct, s = r || t;
|
|
2191
|
-
return e.type === "field" ? s && e.table && n.has(e.table) ? [e.table] : [] : fe(e).flatMap((i) =>
|
|
2195
|
+
return e.type === "field" ? s && e.table && n.has(e.table) ? [e.table] : [] : fe(e).flatMap((i) => Dt(i, n, s));
|
|
2192
2196
|
}
|
|
2193
2197
|
function _r(e, n) {
|
|
2194
|
-
const r =
|
|
2195
|
-
const c =
|
|
2198
|
+
const r = cn(e, n), t = /* @__PURE__ */ new Set(), s = (o) => {
|
|
2199
|
+
const c = o ? Dt(o, r) : [];
|
|
2196
2200
|
return c.forEach((m) => t.add(m)), c.length > 0;
|
|
2197
|
-
}, i = e.select.filter((
|
|
2201
|
+
}, i = e.select.filter((o) => s(o.expression)).map((o) => o.id), l = (e.orderBy ?? []).filter((o) => s(o.expression)).map((o) => o.id), a = s(e.having);
|
|
2198
2202
|
return {
|
|
2199
2203
|
tables: [...t],
|
|
2200
2204
|
select: i,
|
|
2201
|
-
orderBy:
|
|
2202
|
-
having:
|
|
2205
|
+
orderBy: l,
|
|
2206
|
+
having: a
|
|
2203
2207
|
};
|
|
2204
2208
|
}
|
|
2205
2209
|
function Fr(e, n) {
|
|
@@ -2212,38 +2216,38 @@ function Ee(e) {
|
|
|
2212
2216
|
const { fromSelect: n, ...r } = e;
|
|
2213
2217
|
return n === void 0 ? e : r;
|
|
2214
2218
|
}
|
|
2215
|
-
function
|
|
2219
|
+
function Gt(e, n) {
|
|
2216
2220
|
const r = (s) => typeof s.fromSelect == "string" ? n(s, s.fromSelect) : xe(s, r), t = r(e);
|
|
2217
2221
|
return Z(t, e) ? e : t;
|
|
2218
2222
|
}
|
|
2219
2223
|
function Pr(e, n) {
|
|
2220
|
-
var
|
|
2221
|
-
const r = new Map(Fr(e, n).map((
|
|
2224
|
+
var a;
|
|
2225
|
+
const r = new Map(Fr(e, n).map((o) => [o.id, o])), t = (o) => Gt(o, (c, m) => {
|
|
2222
2226
|
const d = r.get(m);
|
|
2223
2227
|
return d ? Jr(d) : Ee(c);
|
|
2224
2228
|
}), s = e.having && t(e.having);
|
|
2225
2229
|
let i = !1;
|
|
2226
|
-
const
|
|
2227
|
-
const c = t(
|
|
2228
|
-
return c ===
|
|
2230
|
+
const l = (a = e.orderBy) == null ? void 0 : a.map((o) => {
|
|
2231
|
+
const c = t(o.expression);
|
|
2232
|
+
return c === o.expression ? o : (i = !0, { ...o, expression: c });
|
|
2229
2233
|
});
|
|
2230
|
-
return s === e.having && !i ? e : { ...e, having: s, ...
|
|
2234
|
+
return s === e.having && !i ? e : { ...e, having: s, ...l ? { orderBy: l } : {} };
|
|
2231
2235
|
}
|
|
2232
2236
|
function zs(e, n) {
|
|
2233
2237
|
const r = new Set((n.groupBy ?? []).flatMap(({ expression: t }) => t.type === "field" && t.column ? [t.column.toLowerCase()] : []));
|
|
2234
|
-
return
|
|
2235
|
-
const i = n.select.find((
|
|
2238
|
+
return Gt(e, (t, s) => {
|
|
2239
|
+
const i = n.select.find((l) => l.id === s);
|
|
2236
2240
|
return !(i != null && i.alias) || r.has(i.alias.toLowerCase()) || !Z(Ee(t), i.expression) ? Ee(t) : { type: "field", column: i.alias };
|
|
2237
2241
|
});
|
|
2238
2242
|
}
|
|
2239
|
-
function
|
|
2240
|
-
const r = (m) =>
|
|
2241
|
-
let
|
|
2242
|
-
const
|
|
2243
|
+
function dn(e, n) {
|
|
2244
|
+
const r = (m) => dn(m, n), t = (m) => We(m, r), s = e.from.query && t(e.from.query), i = s && s !== e.from.query ? { ...e.from, query: s } : e.from;
|
|
2245
|
+
let l = !1;
|
|
2246
|
+
const a = e.joins.map((m) => {
|
|
2243
2247
|
if (!m.query) return m;
|
|
2244
2248
|
const d = t(m.query);
|
|
2245
|
-
return d === m.query ? m : (
|
|
2246
|
-
}),
|
|
2249
|
+
return d === m.query ? m : (l = !0, { ...m, query: d });
|
|
2250
|
+
}), o = i === e.from && !l ? e : { ...e, from: i, joins: a }, c = it(o, (m) => {
|
|
2247
2251
|
const d = r(m.query);
|
|
2248
2252
|
return d === m.query ? m : { ...m, query: d };
|
|
2249
2253
|
});
|
|
@@ -2251,15 +2255,16 @@ function cn(e, n) {
|
|
|
2251
2255
|
}
|
|
2252
2256
|
function Zs(e, n, r, t = $e) {
|
|
2253
2257
|
if (!ue(r)) return r;
|
|
2254
|
-
const s =
|
|
2255
|
-
|
|
2256
|
-
(
|
|
2258
|
+
const s = fn(
|
|
2259
|
+
dn(r, t),
|
|
2260
|
+
(l) => We(l, (a) => dn(a, t))
|
|
2257
2261
|
), i = jr(e, n, s);
|
|
2258
2262
|
return Ar(e, n, i);
|
|
2259
2263
|
}
|
|
2260
2264
|
const Vr = [
|
|
2261
2265
|
"=",
|
|
2262
2266
|
"!=",
|
|
2267
|
+
"IS NOT DISTINCT FROM",
|
|
2263
2268
|
"<",
|
|
2264
2269
|
"<=",
|
|
2265
2270
|
">",
|
|
@@ -2277,43 +2282,49 @@ function Hr(e) {
|
|
|
2277
2282
|
function Xr(e) {
|
|
2278
2283
|
return !e.distinct || !e.orderBy ? [] : e.orderBy.filter(({ expression: n }) => !(n.type === "field" && !n.column)).filter(({ expression: n }) => e.select.length === 0 ? n.type !== "field" : !e.select.some((r) => Z(r.expression, Ee(n)))).map(({ id: n }) => n);
|
|
2279
2284
|
}
|
|
2280
|
-
function Gt(e, n) {
|
|
2281
|
-
n(e), fe(e).forEach((r) => Gt(r, n));
|
|
2282
|
-
}
|
|
2283
2285
|
const Rt = (e) => {
|
|
2284
2286
|
const n = /* @__PURE__ */ new Map();
|
|
2285
|
-
return
|
|
2287
|
+
return nt(e, (r) => (r.type === "param" && r.relative && (n.has(r.name) || n.set(r.name, /* @__PURE__ */ new Set()), n.get(r.name).add(JSON.stringify(r.relative))), r)), n;
|
|
2286
2288
|
};
|
|
2287
2289
|
function qt(e, n) {
|
|
2288
|
-
const r = /* @__PURE__ */ new Map(), t = (
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
}
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2290
|
+
const r = /* @__PURE__ */ new Map(), t = (o) => (r.has(o) || r.set(o, { types: /* @__PURE__ */ new Set(), columns: /* @__PURE__ */ new Map() }), r.get(o)), s = (o, c) => {
|
|
2291
|
+
const m = new Map(o);
|
|
2292
|
+
return Q(c).forEach((d) => m.set(D(d), d.table)), m;
|
|
2293
|
+
}, i = (o, c) => {
|
|
2294
|
+
if (!n || c.type !== "field" || !c.table || !c.column) return;
|
|
2295
|
+
const m = o.has(c.table) ? o.get(c.table) : c.table, d = m === void 0 ? void 0 : z(n, m), f = d == null ? void 0 : d.columns.find((h) => h.name === c.column);
|
|
2296
|
+
return d && f ? { table: d.name, column: f } : void 0;
|
|
2297
|
+
}, l = (o, c) => {
|
|
2298
|
+
if (o.type === "param" && t(o.name), o.type === "binary" && o.right) {
|
|
2299
|
+
const d = o.operator === "LIKE" || o.operator === "REGEXP", f = (v, $) => {
|
|
2300
|
+
const T = i(c, $);
|
|
2301
|
+
if (v.type !== "param" || !T) return;
|
|
2302
|
+
const b = t(v.name);
|
|
2303
|
+
if (b.types.add(T.column.type), !d) {
|
|
2304
|
+
const C = `${T.table}.${T.column.name}`;
|
|
2305
|
+
b.columns.set(C, { table: T.table, column: T.column.name });
|
|
2306
|
+
}
|
|
2307
|
+
};
|
|
2308
|
+
f(o.left, o.right), (o.right.type === "list" ? o.right.items : [o.right]).forEach((v) => f(v, o.left));
|
|
2309
|
+
}
|
|
2310
|
+
const m = o.type === "exists" ? s(c, o.query) : c;
|
|
2311
|
+
fe(o).forEach((d) => l(d, m));
|
|
2312
|
+
}, a = (o) => {
|
|
2313
|
+
const c = s(/* @__PURE__ */ new Map(), o);
|
|
2314
|
+
Kn(o).forEach((m) => l(m, c)), Q(o).forEach((m) => m.query && be(m.query).forEach(a)), st(o).forEach((m) => a(m.query));
|
|
2305
2315
|
};
|
|
2306
|
-
return (e.shared ?? []).forEach((
|
|
2316
|
+
return (e.shared ?? []).forEach((o) => be(o.query).forEach(a)), a(e), r;
|
|
2307
2317
|
}
|
|
2308
2318
|
function ei(e, n) {
|
|
2309
2319
|
const r = Rt(e);
|
|
2310
|
-
return [...qt(e, n)].map(([t, s]) => {
|
|
2311
|
-
const
|
|
2320
|
+
return [...qt(e, n)].map(([t, { types: s, columns: i }]) => {
|
|
2321
|
+
const l = r.get(t);
|
|
2312
2322
|
return {
|
|
2313
2323
|
name: t,
|
|
2314
2324
|
...s.size === 1 ? { type: [...s][0] } : {},
|
|
2315
2325
|
// (Two different ones: validateQuery reports it; the first here.)
|
|
2316
|
-
...
|
|
2326
|
+
...l ? { relative: JSON.parse([...l][0]) } : {},
|
|
2327
|
+
...i.size === 1 ? { column: [...i.values()][0] } : {}
|
|
2317
2328
|
};
|
|
2318
2329
|
});
|
|
2319
2330
|
}
|
|
@@ -2321,11 +2332,11 @@ function Kr(e) {
|
|
|
2321
2332
|
return [...Rt(e)].filter(([, n]) => n.size > 1).map(([n]) => n);
|
|
2322
2333
|
}
|
|
2323
2334
|
function Yr(e, n) {
|
|
2324
|
-
return [...qt(e, n)].filter(([, r]) => r.size > 1).map(([r]) => r);
|
|
2335
|
+
return [...qt(e, n)].filter(([, r]) => r.types.size > 1).map(([r]) => r);
|
|
2325
2336
|
}
|
|
2326
2337
|
function ni(e, n) {
|
|
2327
2338
|
const r = new Map(Object.entries(n));
|
|
2328
|
-
return
|
|
2339
|
+
return nt(e, (t) => {
|
|
2329
2340
|
if (t.type !== "param") return t;
|
|
2330
2341
|
if (!r.has(t.name)) throw new Error(`no value for parameter "${t.name}"`);
|
|
2331
2342
|
const s = r.get(t.name);
|
|
@@ -2335,66 +2346,66 @@ function ni(e, n) {
|
|
|
2335
2346
|
function zr(e, n, r, t) {
|
|
2336
2347
|
const s = new Map(t);
|
|
2337
2348
|
return Q(n).forEach((i) => {
|
|
2338
|
-
var
|
|
2339
|
-
const
|
|
2340
|
-
const
|
|
2341
|
-
return
|
|
2349
|
+
var a;
|
|
2350
|
+
const l = i.table !== void 0 ? (a = z(e, i.table)) == null ? void 0 : a.columns : (() => {
|
|
2351
|
+
const o = Te(i, r);
|
|
2352
|
+
return o && Ne(e, o, r);
|
|
2342
2353
|
})();
|
|
2343
|
-
s.set(D(i), new Set((
|
|
2354
|
+
s.set(D(i), new Set((l ?? []).filter((o) => o.joinOnly).map((o) => o.name)));
|
|
2344
2355
|
}), s;
|
|
2345
2356
|
}
|
|
2346
2357
|
function Zr(e, n) {
|
|
2347
|
-
const r = [], t = n.shared ?? [], s = (
|
|
2358
|
+
const r = [], t = n.shared ?? [], s = (a, o, c, m) => {
|
|
2348
2359
|
var f;
|
|
2349
|
-
if (Array.isArray(
|
|
2350
|
-
|
|
2360
|
+
if (Array.isArray(a)) {
|
|
2361
|
+
a.forEach((h, v) => s(h, [...o, v], c, m));
|
|
2351
2362
|
return;
|
|
2352
2363
|
}
|
|
2353
|
-
if (typeof
|
|
2354
|
-
const d =
|
|
2364
|
+
if (typeof a != "object" || a === null || a instanceof Date) return;
|
|
2365
|
+
const d = a;
|
|
2355
2366
|
if (d.type === "field") {
|
|
2356
|
-
const { table:
|
|
2357
|
-
!m &&
|
|
2367
|
+
const { table: h, column: v } = d;
|
|
2368
|
+
!m && h && v && ((f = c.get(h)) != null && f.has(v)) && r.push(o);
|
|
2358
2369
|
return;
|
|
2359
2370
|
}
|
|
2360
2371
|
if (d.type === "subquery") {
|
|
2361
|
-
|
|
2372
|
+
l(d.query, [...o, "query"], !0);
|
|
2362
2373
|
return;
|
|
2363
2374
|
}
|
|
2364
2375
|
if (d.type === "exists") {
|
|
2365
|
-
const
|
|
2366
|
-
|
|
2376
|
+
const h = d.query;
|
|
2377
|
+
l(h, [...o, "query"], !1, c);
|
|
2367
2378
|
return;
|
|
2368
2379
|
}
|
|
2369
|
-
Object.entries(d).forEach(([
|
|
2370
|
-
}, i = (
|
|
2371
|
-
"union" in
|
|
2380
|
+
Object.entries(d).forEach(([h, v]) => s(v, [...o, h], c, m));
|
|
2381
|
+
}, i = (a, o) => {
|
|
2382
|
+
"union" in a ? be(a).forEach((c, m) => l(c, [...o, "union", m], !0)) : l(a, o, !0);
|
|
2372
2383
|
};
|
|
2373
|
-
function a
|
|
2374
|
-
var
|
|
2375
|
-
const d = zr(e,
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
}),
|
|
2379
|
-
const f =
|
|
2380
|
-
(
|
|
2381
|
-
|
|
2382
|
-
[...
|
|
2384
|
+
function l(a, o, c, m) {
|
|
2385
|
+
var h, v, $;
|
|
2386
|
+
const d = zr(e, a, t, m);
|
|
2387
|
+
a.from.query && i(a.from.query, [...o, "from", "query"]), a.joins.forEach((T, b) => {
|
|
2388
|
+
T.query && i(T.query, [...o, "joins", b, "query"]), T.on && s(T.on, [...o, "joins", b, "on"], d, !0);
|
|
2389
|
+
}), a.where && s(a.where, [...o, "where"], d, !1);
|
|
2390
|
+
const f = a;
|
|
2391
|
+
(h = f.select) == null || h.forEach((T, b) => s(
|
|
2392
|
+
T.expression,
|
|
2393
|
+
[...o, "select", b, "expression"],
|
|
2383
2394
|
d,
|
|
2384
2395
|
c
|
|
2385
|
-
)), (v = f.groupBy) == null || v.forEach((
|
|
2386
|
-
|
|
2387
|
-
[...
|
|
2396
|
+
)), (v = f.groupBy) == null || v.forEach((T, b) => s(
|
|
2397
|
+
T.expression,
|
|
2398
|
+
[...o, "groupBy", b, "expression"],
|
|
2388
2399
|
d,
|
|
2389
2400
|
c
|
|
2390
|
-
)), f.having && s(f.having, [...
|
|
2391
|
-
|
|
2392
|
-
[...
|
|
2401
|
+
)), f.having && s(f.having, [...o, "having"], d, !1), ($ = f.orderBy) == null || $.forEach((T, b) => s(
|
|
2402
|
+
T.expression,
|
|
2403
|
+
[...o, "orderBy", b, "expression"],
|
|
2393
2404
|
d,
|
|
2394
2405
|
!1
|
|
2395
2406
|
));
|
|
2396
2407
|
}
|
|
2397
|
-
return t.forEach((
|
|
2408
|
+
return t.forEach((a, o) => i(a.query, ["shared", o, "query"])), l(n, [], !1), r;
|
|
2398
2409
|
}
|
|
2399
2410
|
function ti(e, n) {
|
|
2400
2411
|
return !(e === "mssql" && n === "REGEXP");
|
|
@@ -2462,13 +2473,13 @@ const De = (e) => e === "integer" ? "number" : e === "dateTime" ? "date" : e, de
|
|
|
2462
2473
|
dateTime: "a date and time",
|
|
2463
2474
|
time: "a time of day",
|
|
2464
2475
|
boolean: "yes / no"
|
|
2465
|
-
}, O = (e) => typeof e == "object" && e !== null && !Array.isArray(e), q = (e) => typeof e == "string", ts = ["day", "week", "month", "year"], rs = (e) => O(e) && ts.includes(e.unit) && Number.isSafeInteger(e.offset) && (e.anchor === void 0 || e.unit !== "day" && (e.anchor === "start" || e.anchor === "end")), ge = (e) => e === void 0 || q(e), ss = (e) => e === null || q(e) || typeof e == "boolean" || typeof e == "bigint" || typeof e == "number" && Number.isFinite(e) || e instanceof Date && !Number.isNaN(e.getTime()),
|
|
2476
|
+
}, O = (e) => typeof e == "object" && e !== null && !Array.isArray(e), q = (e) => typeof e == "string", ts = ["day", "week", "month", "year"], rs = (e) => O(e) && ts.includes(e.unit) && Number.isSafeInteger(e.offset) && (e.anchor === void 0 || e.unit !== "day" && (e.anchor === "start" || e.anchor === "end")), ge = (e) => e === void 0 || q(e), ss = (e) => e === null || q(e) || typeof e == "boolean" || typeof e == "bigint" || typeof e == "number" && Number.isFinite(e) || e instanceof Date && !Number.isNaN(e.getTime()), nn = (e) => O(e) && e.type === "literal" && e.value === "" && e.empty !== !0;
|
|
2466
2477
|
function ii(e, n, r = {}) {
|
|
2467
|
-
var
|
|
2468
|
-
const t = [], s = (u, y, g,
|
|
2478
|
+
var Bn, In;
|
|
2479
|
+
const t = [], s = (u, y, g, p = "error") => {
|
|
2469
2480
|
t.push({
|
|
2470
2481
|
kind: u,
|
|
2471
|
-
severity:
|
|
2482
|
+
severity: p,
|
|
2472
2483
|
path: y,
|
|
2473
2484
|
pathSegments: ns(y),
|
|
2474
2485
|
message: g
|
|
@@ -2477,28 +2488,28 @@ function ii(e, n, r = {}) {
|
|
|
2477
2488
|
if (!O(n) || !q(n.id) || n.id === "")
|
|
2478
2489
|
return s("invalidStructure", "", "a query is an object with a non-empty string id"), t;
|
|
2479
2490
|
if (!("from" in n)) return t;
|
|
2480
|
-
const i = Ht(e, r.tableFilter, r.columnFilter),
|
|
2481
|
-
let
|
|
2482
|
-
const m = (u, y, g,
|
|
2483
|
-
q(u) && u.length > y && s("tooLong", g, `${
|
|
2484
|
-
}, d = /* @__PURE__ */ new Set(), f = O(n) && Array.isArray(n.shared) ? n.shared : [],
|
|
2485
|
-
let
|
|
2491
|
+
const i = Ht(e, r.tableFilter, r.columnFilter), l = Xt(r.functions ?? $e);
|
|
2492
|
+
let a = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), c;
|
|
2493
|
+
const m = (u, y, g, p) => {
|
|
2494
|
+
q(u) && u.length > y && s("tooLong", g, `${p} is longer than ${y} characters`);
|
|
2495
|
+
}, d = /* @__PURE__ */ new Set(), f = O(n) && Array.isArray(n.shared) ? n.shared : [], h = new Set(f.flatMap((u) => O(u) && q(u.id) ? [u.id] : [])), v = /* @__PURE__ */ new Map(), $ = /* @__PURE__ */ new Set();
|
|
2496
|
+
let T;
|
|
2486
2497
|
const b = (u, y) => {
|
|
2487
|
-
|
|
2488
|
-
},
|
|
2489
|
-
|
|
2498
|
+
a.has(u) && s("duplicateTableName", y, `two tables named "${u}"`);
|
|
2499
|
+
}, C = (u, y) => {
|
|
2500
|
+
Un.includes(u) && !es(r.dialect, u) && s("dialectUnsupported", y, `${u.toUpperCase()} JOIN isn't in this database`);
|
|
2490
2501
|
}, G = (u, y, g = !1) => {
|
|
2491
2502
|
if (g && O(u) && u.query !== void 0) {
|
|
2492
|
-
|
|
2503
|
+
T(u, y);
|
|
2493
2504
|
return;
|
|
2494
2505
|
}
|
|
2495
2506
|
if (g && O(u) && u.shared !== void 0) {
|
|
2496
|
-
const { shared:
|
|
2497
|
-
if (!q(
|
|
2498
|
-
s("invalidStructure", y, "a shared query's place has a string `shared` and an `alias`, and no `table`"), S && (b(w, `${y}.alias`),
|
|
2507
|
+
const { shared: A, alias: w } = u, S = q(w) && w !== "";
|
|
2508
|
+
if (!q(A) || !S || u.table !== void 0) {
|
|
2509
|
+
s("invalidStructure", y, "a shared query's place has a string `shared` and an `alias`, and no `table`"), S && (b(w, `${y}.alias`), a.set(w, void 0));
|
|
2499
2510
|
return;
|
|
2500
2511
|
}
|
|
2501
|
-
m(w, Y, `${y}.alias`, "an alias"), b(w, `${y}.alias`), $.add(
|
|
2512
|
+
m(w, Y, `${y}.alias`, "an alias"), b(w, `${y}.alias`), $.add(A), h.has(A) ? v.has(A) || s("sharedOrder", `${y}.shared`, "a shared query can use only those before it") : s("unknownTable", `${y}.shared`, `no shared query "${A}"`), a.set(w, v.get(A));
|
|
2502
2513
|
return;
|
|
2503
2514
|
}
|
|
2504
2515
|
if (!O(u) || !q(u.table) || !ge(u.alias)) {
|
|
@@ -2506,146 +2517,146 @@ function ii(e, n, r = {}) {
|
|
|
2506
2517
|
return;
|
|
2507
2518
|
}
|
|
2508
2519
|
m(u.alias, Y, `${y}.alias`, "an alias");
|
|
2509
|
-
const
|
|
2510
|
-
|
|
2520
|
+
const p = i.tables.find((A) => A.name === u.table);
|
|
2521
|
+
p || s("unknownTable", `${y}.table`, `unknown table "${u.table}"`), q(u.alias) && u.alias !== "" ? (b(u.alias, `${y}.alias`), a.set(u.alias, p), o.set(u.table, u.alias)) : (b(u.table, `${y}.table`), a.set(u.table, p));
|
|
2511
2522
|
}, R = (u, y, g) => {
|
|
2512
|
-
var
|
|
2523
|
+
var N;
|
|
2513
2524
|
if (!u) return;
|
|
2514
|
-
const { args:
|
|
2515
|
-
y < w ? s("incomplete", g, `${
|
|
2525
|
+
const { args: p, name: A } = u, w = p.filter((k) => !k.optional).length, S = (N = p[p.length - 1]) != null && N.variadic ? 1 / 0 : p.length;
|
|
2526
|
+
y < w ? s("incomplete", g, `${A} needs ${w} argument(s)`) : y > S && s("argCount", g, `${A} takes ${S} argument(s)`);
|
|
2516
2527
|
};
|
|
2517
2528
|
let X, ee, ye;
|
|
2518
|
-
const
|
|
2519
|
-
const y = u.table && (
|
|
2529
|
+
const pe = (u) => {
|
|
2530
|
+
const y = u.table && (a.get(u.table) ?? (c == null ? void 0 : c.get(u.table)));
|
|
2520
2531
|
if (!y || Array.isArray(y)) return;
|
|
2521
|
-
const g = y.columns.find((
|
|
2522
|
-
return g &&
|
|
2523
|
-
}, te = (u) =>
|
|
2532
|
+
const g = y.columns.find((p) => p.name === u.column);
|
|
2533
|
+
return g && At(g.type);
|
|
2534
|
+
}, te = (u) => vn(u, pe, l), x = r.typeMismatch ?? "warning", F = (u, y, g) => {
|
|
2524
2535
|
if (u.type === "literal") {
|
|
2525
|
-
if (u.value === null || u.value === "" && !u.empty || typeof u.value == "number" && !
|
|
2536
|
+
if (u.value === null || u.value === "" && !u.empty || typeof u.value == "number" && !tn(u.value) || Sn(u.value, y) !== void 0) return;
|
|
2526
2537
|
const w = u.value instanceof Date ? "a date" : JSON.stringify(String(u.value));
|
|
2527
2538
|
s("typeMismatch", g, `${w} isn't ${de[y]}`, x);
|
|
2528
2539
|
return;
|
|
2529
2540
|
}
|
|
2530
|
-
const
|
|
2531
|
-
if (
|
|
2532
|
-
const
|
|
2533
|
-
s("typeMismatch", g,
|
|
2534
|
-
},
|
|
2535
|
-
const { operator: g, left:
|
|
2536
|
-
if (!
|
|
2537
|
-
const w = (
|
|
2538
|
-
if (
|
|
2539
|
-
const E =
|
|
2541
|
+
const p = te(u);
|
|
2542
|
+
if (p === void 0 || De(p) === De(y)) return;
|
|
2543
|
+
const A = `${de[p]} compared with ${de[y]}`;
|
|
2544
|
+
s("typeMismatch", g, A, x);
|
|
2545
|
+
}, he = (u) => u && u.type !== "literal" ? te(u) : void 0, Qt = (u, y) => {
|
|
2546
|
+
const { operator: g, left: p, right: A } = u;
|
|
2547
|
+
if (!A) return;
|
|
2548
|
+
const w = (N, k, M) => {
|
|
2549
|
+
if (N.type !== "param" || !N.relative) return;
|
|
2550
|
+
const E = he(k);
|
|
2540
2551
|
if (E === void 0 || E === "date" || E === "dateTime") return;
|
|
2541
|
-
const
|
|
2542
|
-
s("typeMismatch", M,
|
|
2552
|
+
const I = `a relative date compared with ${de[E]}`;
|
|
2553
|
+
s("typeMismatch", M, I, x);
|
|
2543
2554
|
};
|
|
2544
|
-
if (w(
|
|
2545
|
-
const
|
|
2546
|
-
if (
|
|
2547
|
-
const
|
|
2548
|
-
s("typeMismatch", `${y}.left`,
|
|
2555
|
+
if (w(A, p, `${y}.right`), w(p, A, `${y}.left`), g === "LIKE" || g === "REGEXP") {
|
|
2556
|
+
const N = he(p);
|
|
2557
|
+
if (N !== void 0 && N !== "text") {
|
|
2558
|
+
const k = `${g} matches text, not ${de[N]}`;
|
|
2559
|
+
s("typeMismatch", `${y}.left`, k, x);
|
|
2549
2560
|
}
|
|
2550
|
-
if (F(
|
|
2561
|
+
if (F(A, "text", `${y}.right`), g === "REGEXP" && A.type === "literal" && q(A.value))
|
|
2551
2562
|
try {
|
|
2552
|
-
new RegExp(
|
|
2553
|
-
} catch (
|
|
2554
|
-
const M = `not a regular expression: ${
|
|
2563
|
+
new RegExp(A.value);
|
|
2564
|
+
} catch (k) {
|
|
2565
|
+
const M = `not a regular expression: ${k.message}`;
|
|
2555
2566
|
s("invalidPattern", `${y}.right`, M);
|
|
2556
2567
|
}
|
|
2557
2568
|
return;
|
|
2558
2569
|
}
|
|
2559
|
-
const S =
|
|
2570
|
+
const S = he(p);
|
|
2560
2571
|
if (g === "IN" || g === "BETWEEN") {
|
|
2561
|
-
if (S === void 0 ||
|
|
2562
|
-
|
|
2572
|
+
if (S === void 0 || A.type !== "list") return;
|
|
2573
|
+
A.items.forEach((N, k) => F(N, S, `${y}.right.items[${k}]`));
|
|
2563
2574
|
return;
|
|
2564
2575
|
}
|
|
2565
|
-
if (S !== void 0) F(
|
|
2576
|
+
if (S !== void 0) F(A, S, `${y}.right`);
|
|
2566
2577
|
else {
|
|
2567
|
-
const
|
|
2568
|
-
|
|
2578
|
+
const N = he(A);
|
|
2579
|
+
N !== void 0 && F(p, N, `${y}.left`);
|
|
2569
2580
|
}
|
|
2570
|
-
},
|
|
2581
|
+
}, An = (u) => {
|
|
2571
2582
|
const y = u.map(([g]) => g.type === "literal" ? _e(g) : te(g)).find((g) => g !== void 0);
|
|
2572
|
-
y !== void 0 && u.forEach(([g,
|
|
2583
|
+
y !== void 0 && u.forEach(([g, p]) => F(g, y, p));
|
|
2573
2584
|
}, V = (u, y, g = !1) => {
|
|
2574
2585
|
if (!O(u) || !q(u.type)) {
|
|
2575
2586
|
s("invalidStructure", y, "an expression is an object with a string `type`");
|
|
2576
2587
|
return;
|
|
2577
2588
|
}
|
|
2578
|
-
const
|
|
2589
|
+
const p = (w) => s("invalidStructure", y, w), A = (w, S) => {
|
|
2579
2590
|
u[w] !== void 0 && V(u[w], `${y}.${w}`, S);
|
|
2580
2591
|
};
|
|
2581
|
-
switch (g && u.type === "exists" &&
|
|
2592
|
+
switch (g && u.type === "exists" && p("EXISTS is a condition, not a value"), ge(u.fromSelect) || p("`fromSelect` is a column's id"), u.type) {
|
|
2582
2593
|
case "literal":
|
|
2583
2594
|
if (!ss(u.value))
|
|
2584
|
-
|
|
2595
|
+
p("a literal value is a string, number, boolean, date or null");
|
|
2585
2596
|
else if (u.empty !== void 0 && typeof u.empty != "boolean")
|
|
2586
|
-
|
|
2587
|
-
else if (
|
|
2597
|
+
p("a literal's `empty` is a boolean");
|
|
2598
|
+
else if (nn(u))
|
|
2588
2599
|
s("incomplete", y, "a value not filled in");
|
|
2589
|
-
else if (typeof u.value == "number" && !
|
|
2590
|
-
const w = `a number with more than ${
|
|
2600
|
+
else if (typeof u.value == "number" && !tn(u.value)) {
|
|
2601
|
+
const w = `a number with more than ${rt} significant digits is written as another number`;
|
|
2591
2602
|
s("tooLong", `${y}.value`, w);
|
|
2592
2603
|
} else
|
|
2593
2604
|
m(u.value, Zt, `${y}.value`, "a text value");
|
|
2594
2605
|
break;
|
|
2595
2606
|
case "param":
|
|
2596
|
-
(!q(u.name) || !/^[A-Za-z_][A-Za-z0-9_]*$/.test(u.name)) &&
|
|
2607
|
+
(!q(u.name) || !/^[A-Za-z_][A-Za-z0-9_]*$/.test(u.name)) && p("a param has a plain identifier as its `name`"), u.relative !== void 0 && !rs(u.relative) && p("a relative date has a `unit` (day, week, month or year), a whole `offset`, and an `anchor` (start or end) only for a week, month or year"), m(u.name, Y, `${y}.name`, "a parameter's name");
|
|
2597
2608
|
break;
|
|
2598
2609
|
case "field":
|
|
2599
2610
|
if (!ge(u.table) || !ge(u.column))
|
|
2600
|
-
|
|
2611
|
+
p("a field has string `table` and `column`");
|
|
2601
2612
|
else if (u.column === void 0 || u.column === "")
|
|
2602
2613
|
s("incomplete", y, "a field with no column picked");
|
|
2603
2614
|
else if (u.table === void 0 || u.table === "")
|
|
2604
2615
|
s("unqualifiedField", y, `column "${u.column}" names no table`);
|
|
2605
|
-
else if (
|
|
2606
|
-
const w =
|
|
2607
|
-
if (!(Array.isArray(w) ? w.includes(u.column) : !w || w.columns.some((
|
|
2608
|
-
const
|
|
2609
|
-
s("unknownColumn", `${y}.column`, `unknown column "${
|
|
2616
|
+
else if (a.has(u.table)) {
|
|
2617
|
+
const w = a.get(u.table);
|
|
2618
|
+
if (!(Array.isArray(w) ? w.includes(u.column) : !w || w.columns.some((N) => N.name === u.column))) {
|
|
2619
|
+
const N = `${u.table}.${u.column}`;
|
|
2620
|
+
s("unknownColumn", `${y}.column`, `unknown column "${N}"`);
|
|
2610
2621
|
}
|
|
2611
2622
|
} else {
|
|
2612
|
-
const w =
|
|
2623
|
+
const w = o.get(u.table);
|
|
2613
2624
|
let S = `table "${u.table}" isn't in the query`;
|
|
2614
2625
|
w ? S = `table "${u.table}" is named "${w}" in the query: use that` : c != null && c.has(u.table) && (S = `table "${u.table}" is the outer query's: only the EXISTS link names it`), s("tableNotInQuery", `${y}.table`, S);
|
|
2615
2626
|
}
|
|
2616
2627
|
break;
|
|
2617
2628
|
case "function": {
|
|
2618
2629
|
if (!q(u.name) || !Array.isArray(u.args)) {
|
|
2619
|
-
|
|
2630
|
+
p("a function has a string `name` and an `args` array");
|
|
2620
2631
|
break;
|
|
2621
2632
|
}
|
|
2622
|
-
!r.allowUnknownFunctions && !
|
|
2623
|
-
const w =
|
|
2624
|
-
if (r.dialect && (w != null && w.dialects) && !w.dialects.includes(r.dialect) && s("dialectUnsupported", `${y}.name`, `${w.name} isn't in this database`), m(u.name, Y, `${y}.name`, "a function's name"), R(w, u.args.length, `${y}.args`), u.distinct !== void 0 && typeof u.distinct != "boolean" &&
|
|
2625
|
-
var E,
|
|
2626
|
-
const
|
|
2627
|
-
V(S, `${y}.args[${
|
|
2628
|
-
const M = !t.slice(
|
|
2629
|
-
w != null && w.aggregate && M && le(S,
|
|
2633
|
+
!r.allowUnknownFunctions && !l[u.name.toUpperCase()] && s("unknownFunction", `${y}.name`, `function "${u.name}" isn't allowed`);
|
|
2634
|
+
const w = l[u.name.toUpperCase()];
|
|
2635
|
+
if (r.dialect && (w != null && w.dialects) && !w.dialects.includes(r.dialect) && s("dialectUnsupported", `${y}.name`, `${w.name} isn't in this database`), m(u.name, Y, `${y}.name`, "a function's name"), R(w, u.args.length, `${y}.args`), u.distinct !== void 0 && typeof u.distinct != "boolean" && p("a function's `distinct` is a boolean"), u.distinct === !0 && r.dialect === "mssql" && u.name.toUpperCase() === "STRING_AGG" && s("dialectUnsupported", y, "SQL Server's STRING_AGG can't join only different values"), u.args.forEach((S, N) => {
|
|
2636
|
+
var E, I;
|
|
2637
|
+
const k = t.length;
|
|
2638
|
+
V(S, `${y}.args[${N}]`, !0), w && ((E = Ge(w, N)) == null ? void 0 : E.kind) === "type-name" && O(S) && m(S.value, er, `${y}.args[${N}].value`, "a type name"), w && ((I = Ge(w, N)) == null ? void 0 : I.kind) === "fixed-text" && O(S) && !(S.type === "literal" && q(S.value)) && p(`${w.name}'s ${Ge(w, N).label} is a text value`);
|
|
2639
|
+
const M = !t.slice(k).some((P) => P.kind === "invalidStructure");
|
|
2640
|
+
w != null && w.aggregate && M && le(S, l) && s(
|
|
2630
2641
|
"misplacedAggregate",
|
|
2631
|
-
`${y}.args[${
|
|
2642
|
+
`${y}.args[${N}]`,
|
|
2632
2643
|
"an aggregate can't be inside another: sum up per group in a sub query first"
|
|
2633
2644
|
);
|
|
2634
2645
|
}), u.orderBy !== void 0) {
|
|
2635
2646
|
if (!Array.isArray(u.orderBy)) {
|
|
2636
|
-
|
|
2647
|
+
p("a function's `orderBy` is an array");
|
|
2637
2648
|
break;
|
|
2638
2649
|
}
|
|
2639
|
-
u.name.toUpperCase() !== "STRING_AGG" &&
|
|
2640
|
-
const
|
|
2650
|
+
u.name.toUpperCase() !== "STRING_AGG" && p("only STRING_AGG takes an `orderBy`"), u.orderBy.forEach((S, N) => {
|
|
2651
|
+
const k = `${y}.orderBy[${N}]`;
|
|
2641
2652
|
if (!O(S) || S.direction !== "asc" && S.direction !== "desc") {
|
|
2642
|
-
s("invalidStructure",
|
|
2653
|
+
s("invalidStructure", k, "a sort key has an `expression` and a `direction`");
|
|
2643
2654
|
return;
|
|
2644
2655
|
}
|
|
2645
2656
|
const M = t.length;
|
|
2646
|
-
V(S.expression, `${
|
|
2657
|
+
V(S.expression, `${k}.expression`, !0), !t.slice(M).some((E) => E.kind === "invalidStructure") && (le(S.expression, l) && s("misplacedAggregate", `${k}.expression`, "an aggregate can't be inside another"), u.distinct === !0 && r.dialect === "postgres" && !Z(S.expression, u.args[0]) && s(
|
|
2647
2658
|
"dialectUnsupported",
|
|
2648
|
-
`${
|
|
2659
|
+
`${k}.expression`,
|
|
2649
2660
|
"PostgreSQL orders STRING_AGG of different values by the value only"
|
|
2650
2661
|
));
|
|
2651
2662
|
});
|
|
@@ -2653,31 +2664,31 @@ function ii(e, n, r = {}) {
|
|
|
2653
2664
|
break;
|
|
2654
2665
|
}
|
|
2655
2666
|
case "binary": {
|
|
2656
|
-
u.operator !== void 0 && !Vr.includes(u.operator) &&
|
|
2667
|
+
u.operator !== void 0 && !Vr.includes(u.operator) && p(`unknown operator ${JSON.stringify(u.operator)}`), u.operator === "REGEXP" && r.dialect === "mssql" && s("dialectUnsupported", y, "SQL Server has no REGEXP");
|
|
2657
2668
|
const w = t.length;
|
|
2658
2669
|
V(u.left, `${y}.left`, !0);
|
|
2659
|
-
const S = t.slice(w).some((
|
|
2670
|
+
const S = t.slice(w).some((N) => N.kind === "incomplete");
|
|
2660
2671
|
if (Hr(u.operator))
|
|
2661
|
-
u.right !== void 0 &&
|
|
2672
|
+
u.right !== void 0 && p(`${String(u.operator)} takes no right side`);
|
|
2662
2673
|
else if (u.right === void 0)
|
|
2663
2674
|
s("incomplete", `${y}.right`, "a comparison with nothing to compare to");
|
|
2664
|
-
else if (O(u.right) && u.right.type === "literal" && u.right.value === null)
|
|
2675
|
+
else if (O(u.right) && u.right.type === "literal" && u.right.value === null && u.operator !== "IS NOT DISTINCT FROM")
|
|
2665
2676
|
s("incomplete", `${y}.right`, "compared with NULL (never true): use IS NULL");
|
|
2666
2677
|
else if (O(u.right) && u.right.type === "subquery")
|
|
2667
2678
|
u.operator === "IN" ? ee(u.right, `${y}.right`) : s("invalidStructure", `${y}.right`, "a sub query is only IN's right side");
|
|
2668
2679
|
else if (u.operator === "BETWEEN") {
|
|
2669
|
-
const { right:
|
|
2670
|
-
(
|
|
2671
|
-
const
|
|
2672
|
-
O(M) && M.type === "literal" && M.value === null ? s("incomplete",
|
|
2680
|
+
const { right: N } = u, k = O(N) && N.type === "list" && Array.isArray(N.items) ? N.items : void 0;
|
|
2681
|
+
(k == null ? void 0 : k.length) !== 2 ? s("invalidStructure", `${y}.right`, "BETWEEN takes its two bounds as a list (expr.between)") : k.forEach((M, E) => {
|
|
2682
|
+
const I = `${y}.right.items[${E}]`;
|
|
2683
|
+
O(M) && M.type === "literal" && M.value === null ? s("incomplete", I, "a bound of NULL (never true)") : S && nn(M) || V(M, I, !0);
|
|
2673
2684
|
});
|
|
2674
|
-
} else O(u.right) && u.right.type === "list" ? u.operator === "IN" ? ye(u.right, `${y}.right`) : s("invalidStructure", `${y}.right`, "a list is only IN's or BETWEEN's right side") : u.operator === "IN" ? s("invalidStructure", `${y}.right`, "IN takes a list (expr.list) or a sub query") : S &&
|
|
2675
|
-
t.slice(w).some((
|
|
2685
|
+
} else O(u.right) && u.right.type === "list" ? u.operator === "IN" ? ye(u.right, `${y}.right`) : s("invalidStructure", `${y}.right`, "a list is only IN's or BETWEEN's right side") : u.operator === "IN" ? s("invalidStructure", `${y}.right`, "IN takes a list (expr.list) or a sub query") : S && nn(u.right) || V(u.right, `${y}.right`, !0);
|
|
2686
|
+
t.slice(w).some((N) => N.kind === "invalidStructure") || Qt(u, y);
|
|
2676
2687
|
break;
|
|
2677
2688
|
}
|
|
2678
2689
|
case "logical":
|
|
2679
|
-
if (u.operator !== "AND" && u.operator !== "OR" &&
|
|
2680
|
-
|
|
2690
|
+
if (u.operator !== "AND" && u.operator !== "OR" && p("a logical operator is AND or OR"), !Array.isArray(u.operands)) {
|
|
2691
|
+
p("a logical node has an `operands` array");
|
|
2681
2692
|
break;
|
|
2682
2693
|
}
|
|
2683
2694
|
u.operands.length === 0 && s("incomplete", y, "an empty AND/OR group"), u.operands.forEach((w, S) => V(w, `${y}.operands[${S}]`));
|
|
@@ -2693,24 +2704,24 @@ function ii(e, n, r = {}) {
|
|
|
2693
2704
|
u[S] === void 0 && s("incomplete", `${y}.${S}`, `an IF with no ${S}`);
|
|
2694
2705
|
});
|
|
2695
2706
|
const w = t.length;
|
|
2696
|
-
|
|
2707
|
+
A("condition", !1), A("then", !0), A("else", !0), t.slice(w).some((S) => S.kind === "invalidStructure") || An(
|
|
2697
2708
|
["then", "else"].filter((S) => u[S] !== void 0).map((S) => [u[S], `${y}.${S}`])
|
|
2698
2709
|
);
|
|
2699
2710
|
break;
|
|
2700
2711
|
}
|
|
2701
2712
|
case "case": {
|
|
2702
2713
|
const w = t.length;
|
|
2703
|
-
|
|
2704
|
-
if (!O(
|
|
2705
|
-
const M = `${y}.branches[${
|
|
2714
|
+
A("subject", !0), Array.isArray(u.branches) ? (u.branches.length === 0 && s("incomplete", y, "a CASE with no WHEN"), u.branches.forEach((N, k) => {
|
|
2715
|
+
if (!O(N)) {
|
|
2716
|
+
const M = `${y}.branches[${k}]`;
|
|
2706
2717
|
s("invalidStructure", M, "a branch has `when` and `then`");
|
|
2707
2718
|
return;
|
|
2708
2719
|
}
|
|
2709
|
-
V(
|
|
2710
|
-
})) :
|
|
2720
|
+
V(N.when, `${y}.branches[${k}].when`, u.subject !== void 0), V(N.then, `${y}.branches[${k}].then`, !0);
|
|
2721
|
+
})) : p("a case node has a `branches` array");
|
|
2711
2722
|
const S = t.length;
|
|
2712
|
-
|
|
2713
|
-
...u.branches.map((
|
|
2723
|
+
A("else", !0), Array.isArray(u.branches) && t.slice(w).concat(t.slice(S)).every((N) => N.kind !== "invalidStructure") && An([
|
|
2724
|
+
...u.branches.map((N, k) => [N.then, `${y}.branches[${k}].then`]),
|
|
2714
2725
|
...u.else === void 0 ? [] : [[u.else, `${y}.else`]]
|
|
2715
2726
|
]);
|
|
2716
2727
|
break;
|
|
@@ -2719,133 +2730,133 @@ function ii(e, n, r = {}) {
|
|
|
2719
2730
|
X(u, y);
|
|
2720
2731
|
break;
|
|
2721
2732
|
case "subquery":
|
|
2722
|
-
|
|
2733
|
+
p("a sub query is only IN's right side");
|
|
2723
2734
|
break;
|
|
2724
2735
|
case "list":
|
|
2725
|
-
|
|
2736
|
+
p("a list is only IN's right side");
|
|
2726
2737
|
break;
|
|
2727
2738
|
default:
|
|
2728
|
-
|
|
2739
|
+
p(`unknown expression type ${JSON.stringify(u.type)}`);
|
|
2729
2740
|
}
|
|
2730
2741
|
};
|
|
2731
2742
|
ye = (u, y) => {
|
|
2732
|
-
Array.isArray(u.items) ? u.items.length === 0 ? s("incomplete", y, "IN with no values") : u.items.forEach((g,
|
|
2743
|
+
Array.isArray(u.items) ? u.items.length === 0 ? s("incomplete", y, "IN with no values") : u.items.forEach((g, p) => V(g, `${y}.items[${p}]`, !0)) : s("invalidStructure", y, "a list has an `items` array");
|
|
2733
2744
|
}, ee = (u, y) => {
|
|
2734
|
-
const { id: g, query:
|
|
2735
|
-
if (!q(g) || g === "" || !O(
|
|
2745
|
+
const { id: g, query: p } = u;
|
|
2746
|
+
if (!q(g) || g === "" || !O(p)) {
|
|
2736
2747
|
s("invalidStructure", y, "a sub query has a non-empty string `id` and a `query`");
|
|
2737
2748
|
return;
|
|
2738
2749
|
}
|
|
2739
2750
|
d.has(g) && s("duplicateId", `${y}.id`, `the id "${g}" is used twice`), d.add(g);
|
|
2740
|
-
const
|
|
2741
|
-
|
|
2751
|
+
const A = { inQuery: a, aliasOf: o, outerTables: c };
|
|
2752
|
+
a = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), c = void 0;
|
|
2742
2753
|
const w = t.length;
|
|
2743
|
-
Oe(
|
|
2754
|
+
Oe(p, `${y}.query`, !1), { inQuery: a, aliasOf: o, outerTables: c } = A;
|
|
2744
2755
|
const S = t.length === w;
|
|
2745
|
-
(Array.isArray(
|
|
2756
|
+
(Array.isArray(p.select) ? p.select : []).length !== 1 && s("subqueryColumns", `${y}.query.select`, "a sub query after IN picks exactly one column to compare with"), S && Ue(p, `${y}.query`);
|
|
2746
2757
|
}, X = (u, y) => {
|
|
2747
|
-
const { query: g, link:
|
|
2758
|
+
const { query: g, link: p } = u;
|
|
2748
2759
|
if (!O(g) || !Array.isArray(g.joins)) {
|
|
2749
2760
|
s("invalidStructure", `${y}.query`, "an EXISTS has a `query` with `from` and `joins`");
|
|
2750
2761
|
return;
|
|
2751
2762
|
}
|
|
2752
|
-
const
|
|
2753
|
-
|
|
2763
|
+
const A = { inQuery: a, aliasOf: o, outerTables: c };
|
|
2764
|
+
a = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), c = A.inQuery, [
|
|
2754
2765
|
[g.from, `${y}.query.from`],
|
|
2755
|
-
...g.joins.map((
|
|
2756
|
-
].forEach(([
|
|
2757
|
-
const
|
|
2758
|
-
G(
|
|
2766
|
+
...g.joins.map((k, M) => [k, `${y}.query.joins[${M}]`])
|
|
2767
|
+
].forEach(([k, M], E) => {
|
|
2768
|
+
const I = new Set(a.keys());
|
|
2769
|
+
G(k, M, E > 0), E > 0 && O(k) && C(k.joinType, `${M}.joinType`), [...a.keys()].filter((P) => !I.has(P) && A.inQuery.has(P)).forEach((P) => s(
|
|
2759
2770
|
"ambiguousTable",
|
|
2760
2771
|
`${M}.table`,
|
|
2761
2772
|
`"${P}" is a table of the query around it too: give it an alias`
|
|
2762
2773
|
));
|
|
2763
2774
|
});
|
|
2764
2775
|
const S = [
|
|
2765
|
-
...g.joins.map((
|
|
2766
|
-
O(
|
|
2776
|
+
...g.joins.map((k, M) => [
|
|
2777
|
+
O(k) ? k.on : void 0,
|
|
2767
2778
|
`${y}.query.joins[${M}].on`
|
|
2768
2779
|
]),
|
|
2769
2780
|
[g.where, `${y}.query.where`]
|
|
2770
2781
|
];
|
|
2771
|
-
S.forEach(([
|
|
2772
|
-
|
|
2782
|
+
S.forEach(([k, M]) => {
|
|
2783
|
+
k !== void 0 && V(k, M);
|
|
2773
2784
|
});
|
|
2774
|
-
const
|
|
2775
|
-
|
|
2776
|
-
O(
|
|
2785
|
+
const N = { inQuery: a, aliasOf: o };
|
|
2786
|
+
a = new Map([...A.inQuery, ...N.inQuery]), o = new Map([...A.aliasOf, ...N.aliasOf]), c = void 0, V(p, `${y}.link`), { inQuery: a, aliasOf: o, outerTables: c } = A, [...S, [p, `${y}.link`]].forEach(([k, M]) => {
|
|
2787
|
+
O(k) && le(k, l) && s("misplacedAggregate", M, "an aggregate (SUM, COUNT, …) can't be used here");
|
|
2777
2788
|
});
|
|
2778
2789
|
};
|
|
2779
2790
|
const ve = (u, y, g = []) => {
|
|
2780
|
-
const
|
|
2781
|
-
u.forEach((
|
|
2782
|
-
!O(
|
|
2791
|
+
const p = new Set(g);
|
|
2792
|
+
u.forEach((A, w) => {
|
|
2793
|
+
!O(A) || !q(A.id) || A.id === "" ? s("invalidStructure", `${y}[${w}]`, "every entry has a non-empty string id") : p.has(A.id) ? s("duplicateId", `${y}[${w}].id`, `id "${A.id}" is used twice`) : p.add(A.id);
|
|
2783
2794
|
});
|
|
2784
|
-
},
|
|
2785
|
-
const
|
|
2786
|
-
G(u.from,
|
|
2787
|
-
const { from:
|
|
2788
|
-
O(
|
|
2795
|
+
}, kn = (u, y, g) => Array.isArray(u[y]) ? u[y] : (u[y] !== void 0 && s("invalidStructure", g, `\`${y}\` is an array`), []), Oe = (u, y, g) => {
|
|
2796
|
+
const p = (E) => y ? `${y}.${E}` : E;
|
|
2797
|
+
G(u.from, p("from"), !0);
|
|
2798
|
+
const { from: A } = u, w = O(A) && A.query !== void 0 ? A.id : void 0;
|
|
2799
|
+
O(A) && A.query !== void 0 && (!q(w) || w === "") && s("invalidStructure", p("from"), "a derived table has a non-empty string id");
|
|
2789
2800
|
const S = Array.isArray(u.joins) ? u.joins : [];
|
|
2790
|
-
Array.isArray(u.joins) || s("invalidStructure",
|
|
2801
|
+
Array.isArray(u.joins) || s("invalidStructure", p("joins"), "`joins` is an array"), S.forEach((E, I) => G(E, p(`joins[${I}]`), !0)), ve(S, p("joins"), q(w) ? [w] : []), S.forEach((E, I) => {
|
|
2791
2802
|
if (O(E)) {
|
|
2792
|
-
if (
|
|
2793
|
-
|
|
2803
|
+
if (Un.includes(E.joinType))
|
|
2804
|
+
C(E.joinType, p(`joins[${I}].joinType`));
|
|
2794
2805
|
else {
|
|
2795
2806
|
const P = JSON.stringify(E.joinType);
|
|
2796
|
-
s("invalidStructure",
|
|
2807
|
+
s("invalidStructure", p(`joins[${I}].joinType`), `unknown join type ${P}`);
|
|
2797
2808
|
}
|
|
2798
|
-
E.joinType !== "cross" && (E.on !== void 0 ? V(E.on,
|
|
2809
|
+
E.joinType !== "cross" && (E.on !== void 0 ? V(E.on, p(`joins[${I}].on`)) : s("incomplete", p(`joins[${I}].on`), "a join with no ON condition"));
|
|
2799
2810
|
}
|
|
2800
|
-
}), u.where !== void 0 && V(u.where,
|
|
2801
|
-
const
|
|
2802
|
-
Array.isArray(u.select) || s("invalidStructure",
|
|
2803
|
-
O(E) && (m(E.alias, Y,
|
|
2811
|
+
}), u.where !== void 0 && V(u.where, p("where"));
|
|
2812
|
+
const N = Array.isArray(u.select) ? u.select : [];
|
|
2813
|
+
Array.isArray(u.select) || s("invalidStructure", p("select"), "`select` is an array"), ve(N, p("select")), N.forEach((E, I) => {
|
|
2814
|
+
O(E) && (m(E.alias, Y, p(`select[${I}].alias`), "an alias"), ge(E.alias) || s("invalidStructure", p(`select[${I}].alias`), "an alias is a string"), V(E.expression, p(`select[${I}].expression`), !0));
|
|
2804
2815
|
});
|
|
2805
|
-
const
|
|
2806
|
-
if (ve(
|
|
2807
|
-
O(E) && (ge(E.fromSelect) || s("invalidStructure",
|
|
2808
|
-
}), u.having !== void 0 && V(u.having,
|
|
2816
|
+
const k = kn(u, "groupBy", p("groupBy"));
|
|
2817
|
+
if (ve(k, p("groupBy")), k.forEach((E, I) => {
|
|
2818
|
+
O(E) && (ge(E.fromSelect) || s("invalidStructure", p(`groupBy[${I}].fromSelect`), "`fromSelect` is a column's id"), V(E.expression, p(`groupBy[${I}].expression`), !0));
|
|
2819
|
+
}), u.having !== void 0 && V(u.having, p("having")), u.distinct !== void 0 && typeof u.distinct != "boolean" && s("invalidStructure", p("distinct"), "`distinct` is a boolean"), !g) {
|
|
2809
2820
|
["orderBy", "limit", "offset", "shared"].forEach((E) => {
|
|
2810
|
-
u[E] !== void 0 && s("invalidStructure",
|
|
2821
|
+
u[E] !== void 0 && s("invalidStructure", p(E), `a derived table has no \`${E}\``);
|
|
2811
2822
|
});
|
|
2812
2823
|
return;
|
|
2813
2824
|
}
|
|
2814
|
-
const M =
|
|
2815
|
-
ve(M, "orderBy"), M.forEach((E,
|
|
2816
|
-
O(E) && (E.direction !== "asc" && E.direction !== "desc" && s("invalidStructure", `orderBy[${
|
|
2825
|
+
const M = kn(u, "orderBy", "orderBy");
|
|
2826
|
+
ve(M, "orderBy"), M.forEach((E, I) => {
|
|
2827
|
+
O(E) && (E.direction !== "asc" && E.direction !== "desc" && s("invalidStructure", `orderBy[${I}].direction`, 'a sort direction is "asc" or "desc"'), V(E.expression, `orderBy[${I}].expression`, !0));
|
|
2817
2828
|
}), ["limit", "offset"].forEach((E) => {
|
|
2818
|
-
const
|
|
2819
|
-
|
|
2829
|
+
const I = u[E];
|
|
2830
|
+
I !== void 0 && !(Number.isInteger(I) && I >= 0) ? s("invalidStructure", E, `\`${E}\` is a non-negative integer`) : I !== void 0 && !Number.isSafeInteger(I) && s("tooLong", E, `\`${E}\` is past ${Number.MAX_SAFE_INTEGER}, which is exact`);
|
|
2820
2831
|
});
|
|
2821
2832
|
}, _t = (u, y, g) => {
|
|
2822
|
-
const
|
|
2823
|
-
if (!Array.isArray(
|
|
2824
|
-
s("invalidStructure",
|
|
2833
|
+
const p = `${y}.query`, { union: A } = u;
|
|
2834
|
+
if (!Array.isArray(A) || A.length < 1 || typeof u.all != "boolean") {
|
|
2835
|
+
s("invalidStructure", p, "a union has one or more branches in `union`, and a boolean `all`"), b(g, `${y}.alias`), a.set(g, void 0);
|
|
2825
2836
|
return;
|
|
2826
2837
|
}
|
|
2827
2838
|
let w = [];
|
|
2828
2839
|
const S = [];
|
|
2829
|
-
let
|
|
2830
|
-
|
|
2831
|
-
const E = `${
|
|
2832
|
-
if (!O(
|
|
2833
|
-
s("invalidStructure", E, "a union's branch is a query"),
|
|
2840
|
+
let N = !0;
|
|
2841
|
+
A.forEach((k, M) => {
|
|
2842
|
+
const E = `${p}.union[${M}]`;
|
|
2843
|
+
if (!O(k)) {
|
|
2844
|
+
s("invalidStructure", E, "a union's branch is a query"), N = !1;
|
|
2834
2845
|
return;
|
|
2835
2846
|
}
|
|
2836
|
-
const
|
|
2837
|
-
|
|
2847
|
+
const I = { inQuery: a, aliasOf: o, outerTables: c };
|
|
2848
|
+
a = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), c = void 0;
|
|
2838
2849
|
const P = t.length;
|
|
2839
|
-
Oe(
|
|
2840
|
-
const ae = t.length === P, B = (Array.isArray(
|
|
2841
|
-
if ({ inQuery:
|
|
2842
|
-
|
|
2850
|
+
Oe(k, E, !1);
|
|
2851
|
+
const ae = t.length === P, B = (Array.isArray(k.select) ? k.select : []).filter(O), U = ae ? B.map((j) => j.expression.type === "literal" ? _e(j.expression) : te(j.expression)) : [];
|
|
2852
|
+
if ({ inQuery: a, aliasOf: o, outerTables: c } = I, !ae) {
|
|
2853
|
+
N = !1;
|
|
2843
2854
|
return;
|
|
2844
2855
|
}
|
|
2845
|
-
Ue(
|
|
2856
|
+
Ue(k, E);
|
|
2846
2857
|
const _ = B.map((j) => K(j));
|
|
2847
2858
|
if (B.length === 0) {
|
|
2848
|
-
s("unionColumns", `${E}.select`, "a union's branch names its columns"),
|
|
2859
|
+
s("unionColumns", `${E}.select`, "a union's branch names its columns"), N = !1;
|
|
2849
2860
|
return;
|
|
2850
2861
|
}
|
|
2851
2862
|
if (M === 0)
|
|
@@ -2875,54 +2886,54 @@ function ii(e, n, r = {}) {
|
|
|
2875
2886
|
s("unionTypes", `${E}.select[${L}].expression`, re);
|
|
2876
2887
|
}
|
|
2877
2888
|
});
|
|
2878
|
-
}), b(g, `${y}.alias`),
|
|
2889
|
+
}), b(g, `${y}.alias`), a.set(g, N ? w.filter((k) => k !== void 0) : void 0);
|
|
2879
2890
|
};
|
|
2880
|
-
|
|
2881
|
-
const { query: g, alias:
|
|
2882
|
-
if (u.table === void 0 && O(g) && g.union !== void 0 && q(
|
|
2883
|
-
m(
|
|
2891
|
+
T = (u, y) => {
|
|
2892
|
+
const { query: g, alias: p } = u;
|
|
2893
|
+
if (u.table === void 0 && O(g) && g.union !== void 0 && q(p) && p !== "") {
|
|
2894
|
+
m(p, Y, `${y}.alias`, "an alias"), _t(g, y, p);
|
|
2884
2895
|
return;
|
|
2885
2896
|
}
|
|
2886
|
-
if (u.table !== void 0 || !O(g) || !q(
|
|
2887
|
-
s("invalidStructure", y, "a derived table has a `query` and an `alias`, and no `table`"), q(
|
|
2897
|
+
if (u.table !== void 0 || !O(g) || !q(p) || p === "") {
|
|
2898
|
+
s("invalidStructure", y, "a derived table has a `query` and an `alias`, and no `table`"), q(p) && p !== "" && (b(p, `${y}.alias`), a.set(p, void 0));
|
|
2888
2899
|
return;
|
|
2889
2900
|
}
|
|
2890
|
-
m(
|
|
2891
|
-
const
|
|
2892
|
-
|
|
2901
|
+
m(p, Y, `${y}.alias`, "an alias");
|
|
2902
|
+
const A = { inQuery: a, aliasOf: o, outerTables: c };
|
|
2903
|
+
a = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), c = void 0;
|
|
2893
2904
|
const w = t.length;
|
|
2894
|
-
Oe(g, `${y}.query`, !1), { inQuery:
|
|
2895
|
-
const S = Array.isArray(g.select) ? g.select : [],
|
|
2896
|
-
|
|
2905
|
+
Oe(g, `${y}.query`, !1), { inQuery: a, aliasOf: o, outerTables: c } = A;
|
|
2906
|
+
const S = Array.isArray(g.select) ? g.select : [], N = [], k = (I, P) => {
|
|
2907
|
+
N.includes(I) ? s("derivedColumnName", P, `two columns named "${I}"`) : N.push(I);
|
|
2897
2908
|
};
|
|
2898
|
-
S.forEach((
|
|
2899
|
-
if (!O(
|
|
2900
|
-
const ae = K(
|
|
2901
|
-
ae ?
|
|
2909
|
+
S.forEach((I, P) => {
|
|
2910
|
+
if (!O(I) || !O(I.expression)) return;
|
|
2911
|
+
const ae = K(I);
|
|
2912
|
+
ae ? k(ae, `${y}.query.select[${P}]`) : s("derivedColumnName", `${y}.query.select[${P}]`, "a derived table's column needs an alias: nothing outside could name it");
|
|
2902
2913
|
});
|
|
2903
2914
|
const M = t.length === w;
|
|
2904
|
-
M && S.length === 0 && Ne(i, g, f).forEach((
|
|
2915
|
+
M && S.length === 0 && Ne(i, g, f).forEach((I) => k(I.name, `${y}.query.select`)), b(p, `${y}.alias`), a.set(p, M || S.length > 0 ? N : void 0), M && Ue(g, `${y}.query`);
|
|
2905
2916
|
const { drillAcross: E } = u;
|
|
2906
2917
|
E !== void 0 && !(O(E) && typeof E.synced == "boolean" && Number.isInteger(E.copied) && E.copied >= 0) && s("invalidStructure", `${y}.drillAcross`, "`drillAcross` has a boolean `synced` and a non-negative integer `copied`");
|
|
2907
2918
|
};
|
|
2908
2919
|
const Ue = (u, y) => {
|
|
2909
|
-
var M, E,
|
|
2910
|
-
const g = (B) => y ? `${y}.${B}` : B,
|
|
2911
|
-
if (
|
|
2912
|
-
const B = u.joins.findIndex((U) => (U.table ?? D(U)) ===
|
|
2920
|
+
var M, E, I, P, ae;
|
|
2921
|
+
const g = (B) => y ? `${y}.${B}` : B, p = gn(u);
|
|
2922
|
+
if (p) {
|
|
2923
|
+
const B = u.joins.findIndex((U) => (U.table ?? D(U)) === p.table);
|
|
2913
2924
|
s(
|
|
2914
2925
|
"joinOrder",
|
|
2915
2926
|
g(`joins[${B}].on`),
|
|
2916
|
-
`the join condition of "${
|
|
2927
|
+
`the join condition of "${p.table}" uses "${p.needs}", which is joined after it`
|
|
2917
2928
|
);
|
|
2918
2929
|
}
|
|
2919
|
-
const
|
|
2920
|
-
le(B,
|
|
2930
|
+
const A = (B, U) => {
|
|
2931
|
+
le(B, l) && s("misplacedAggregate", U, "an aggregate (SUM, COUNT, …) can't be used here");
|
|
2921
2932
|
};
|
|
2922
|
-
|
|
2923
|
-
B.joinType !== "cross" &&
|
|
2924
|
-
}), (M = u.groupBy) == null || M.forEach((B, U) =>
|
|
2925
|
-
const w =
|
|
2933
|
+
A(u.where, g("where")), u.joins.forEach((B, U) => {
|
|
2934
|
+
B.joinType !== "cross" && A(B.on, g(`joins[${U}].on`));
|
|
2935
|
+
}), (M = u.groupBy) == null || M.forEach((B, U) => A(B.expression, g(`groupBy[${U}].expression`)));
|
|
2936
|
+
const w = Yn(u, l), S = "isn't a group key or an aggregate (SUM, COUNT, …)";
|
|
2926
2937
|
if (w.selectAll && s("notGrouped", g("select"), "a grouped query has to pick its columns, not *"), u.select.forEach((B, U) => {
|
|
2927
2938
|
w.select.includes(B.id) && s("notGrouped", g(`select[${U}].expression`), `this column ${S}`);
|
|
2928
2939
|
}), (E = u.orderBy) == null || E.forEach((B, U) => {
|
|
@@ -2930,7 +2941,7 @@ function ii(e, n, r = {}) {
|
|
|
2930
2941
|
}), w.having && s("notGrouped", g("having"), `a column in this condition ${S}`), r.dialect === "mssql") {
|
|
2931
2942
|
const B = [], U = (L, J) => {
|
|
2932
2943
|
const [re, je] = L.length <= J.length ? [L, J] : [J, L];
|
|
2933
|
-
return re.every((Cn,
|
|
2944
|
+
return re.every((Cn, Mn) => Cn.direction === je[Mn].direction && Z(Cn.expression, je[Mn].expression));
|
|
2934
2945
|
}, _ = (L) => {
|
|
2935
2946
|
var re;
|
|
2936
2947
|
return [...L.type === "function" && ((re = L.orderBy) != null && re.length) ? [L.orderBy] : [], ...fe(L).flatMap(_)];
|
|
@@ -2943,11 +2954,11 @@ function ii(e, n, r = {}) {
|
|
|
2943
2954
|
), B.push(re);
|
|
2944
2955
|
});
|
|
2945
2956
|
};
|
|
2946
|
-
u.select.forEach((L, J) => j(L.expression, g(`select[${J}].expression`))), u.having && j(u.having, g("having")), (
|
|
2957
|
+
u.select.forEach((L, J) => j(L.expression, g(`select[${J}].expression`))), u.having && j(u.having, g("having")), (I = u.orderBy) == null || I.forEach((L, J) => j(L.expression, `orderBy[${J}].expression`));
|
|
2947
2958
|
}
|
|
2948
|
-
if (r.allowUnknownFunctions &&
|
|
2959
|
+
if (r.allowUnknownFunctions && On(u, l)) {
|
|
2949
2960
|
const B = (U, _) => {
|
|
2950
|
-
Kt(U,
|
|
2961
|
+
Kt(U, l) && s(
|
|
2951
2962
|
"unknownAggregate",
|
|
2952
2963
|
_,
|
|
2953
2964
|
"a function the editor doesn't know: it isn't made a group key, in case it sums rows up — declare it in `functions` (`aggregate: true` if it does)",
|
|
@@ -2981,40 +2992,40 @@ function ii(e, n, r = {}) {
|
|
|
2981
2992
|
"warning"
|
|
2982
2993
|
);
|
|
2983
2994
|
}), u.joins.forEach((B, U) => {
|
|
2984
|
-
B.joinType !== "cross" || B.query &&
|
|
2995
|
+
B.joinType !== "cross" || B.query && ut(B.query, l) || s(
|
|
2985
2996
|
"crossJoin",
|
|
2986
2997
|
g(`joins[${U}]`),
|
|
2987
2998
|
`every row of "${D(B)}" meets every row of the query: they multiply`,
|
|
2988
2999
|
"warning"
|
|
2989
3000
|
);
|
|
2990
3001
|
});
|
|
2991
|
-
const
|
|
3002
|
+
const N = _r(u, i), k = (B) => s(
|
|
2992
3003
|
"fanOut",
|
|
2993
3004
|
B,
|
|
2994
|
-
`${
|
|
3005
|
+
`${N.tables.map((U) => `"${U}"`).join(", ")} rows repeat in this join: a sum, average or count of their columns comes out too high`,
|
|
2995
3006
|
"warning"
|
|
2996
3007
|
);
|
|
2997
3008
|
u.select.forEach((B, U) => {
|
|
2998
|
-
|
|
2999
|
-
}),
|
|
3000
|
-
|
|
3009
|
+
N.select.includes(B.id) && k(g(`select[${U}].expression`));
|
|
3010
|
+
}), N.having && k(g("having")), (ae = u.orderBy) == null || ae.forEach((B, U) => {
|
|
3011
|
+
N.orderBy.includes(B.id) && k(`orderBy[${U}].expression`);
|
|
3001
3012
|
});
|
|
3002
3013
|
};
|
|
3003
3014
|
n.shared !== void 0 && !Array.isArray(n.shared) && s("invalidStructure", "shared", "`shared` is an array"), ve(f, "shared");
|
|
3004
|
-
const
|
|
3015
|
+
const xn = /* @__PURE__ */ new Set();
|
|
3005
3016
|
if (f.forEach((u, y) => {
|
|
3006
3017
|
if (!O(u)) return;
|
|
3007
|
-
const g = `shared[${y}]`, { name:
|
|
3008
|
-
!q(
|
|
3009
|
-
const
|
|
3010
|
-
|
|
3011
|
-
const S =
|
|
3012
|
-
({ inQuery:
|
|
3018
|
+
const g = `shared[${y}]`, { name: p } = u;
|
|
3019
|
+
!q(p) || p === "" ? s("invalidStructure", `${g}.name`, "a shared query has a non-empty string `name`") : (m(p, Y, `${g}.name`, "a name"), xn.has(p) && s("duplicateTableName", `${g}.name`, `two shared queries named "${p}"`), xn.add(p));
|
|
3020
|
+
const A = q(p) && p !== "" ? p : g, w = { inQuery: a, aliasOf: o, outerTables: c };
|
|
3021
|
+
a = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), c = void 0, T({ query: u.query, alias: A }, g);
|
|
3022
|
+
const S = a.get(A);
|
|
3023
|
+
({ inQuery: a, aliasOf: o, outerTables: c } = w), q(u.id) && v.set(u.id, Array.isArray(S) ? S : void 0);
|
|
3013
3024
|
}), Oe(n, "", !0), t.length === 0) {
|
|
3014
3025
|
const u = n;
|
|
3015
3026
|
if (Ue(u, ""), r.requireLimit) {
|
|
3016
|
-
const { max: g } = r.requireLimit === !0 ? {} : r.requireLimit,
|
|
3017
|
-
u.limit === void 0 && !
|
|
3027
|
+
const { max: g } = r.requireLimit === !0 ? {} : r.requireLimit, p = On(u, l) && !((Bn = u.groupBy) != null && Bn.length);
|
|
3028
|
+
u.limit === void 0 && !p ? s("noLimit", "limit", "no LIMIT: the query reads every row", "warning") : g !== void 0 && u.limit !== void 0 && u.limit > g && s("limitTooHigh", "limit", `LIMIT ${u.limit} is above ${g}`, "warning");
|
|
3018
3029
|
}
|
|
3019
3030
|
Yr(u, i).forEach((g) => {
|
|
3020
3031
|
s("paramConflict", "", `param "${g}" is compared against columns of different types`);
|
|
@@ -3022,10 +3033,10 @@ function ii(e, n, r = {}) {
|
|
|
3022
3033
|
s("paramConflict", "", `param "${g}" stands for two different relative dates`);
|
|
3023
3034
|
});
|
|
3024
3035
|
const y = Xr(u);
|
|
3025
|
-
(
|
|
3036
|
+
(In = u.orderBy) == null || In.forEach((g, p) => {
|
|
3026
3037
|
y.includes(g.id) && s(
|
|
3027
3038
|
"distinctSort",
|
|
3028
|
-
`orderBy[${
|
|
3039
|
+
`orderBy[${p}].expression`,
|
|
3029
3040
|
"with DISTINCT, a sort key has to be one of the selected columns"
|
|
3030
3041
|
);
|
|
3031
3042
|
});
|
|
@@ -3041,7 +3052,7 @@ function ii(e, n, r = {}) {
|
|
|
3041
3052
|
return t.filter((u) => u.severity === "error" || !Ft.includes(u.kind));
|
|
3042
3053
|
}
|
|
3043
3054
|
function oi(e, n = {
|
|
3044
|
-
relativeDate:
|
|
3055
|
+
relativeDate: mt
|
|
3045
3056
|
}) {
|
|
3046
3057
|
return n.relativeDate(e);
|
|
3047
3058
|
}
|
|
@@ -3063,106 +3074,106 @@ const is = {
|
|
|
3063
3074
|
offset: "order"
|
|
3064
3075
|
};
|
|
3065
3076
|
function li(e, n, r = {}) {
|
|
3066
|
-
const { schema: t, functions: s } = r, i = (d, f) => typeof f == "number" ? d == null ? void 0 : d[f] : void 0,
|
|
3077
|
+
const { schema: t, functions: s } = r, i = (d, f) => typeof f == "number" ? d == null ? void 0 : d[f] : void 0, l = (d) => {
|
|
3067
3078
|
var f;
|
|
3068
3079
|
return d.alias || t && d.table !== void 0 && ((f = z(t, d.table)) == null ? void 0 : f.label) || d.table || "";
|
|
3069
|
-
},
|
|
3070
|
-
var
|
|
3080
|
+
}, a = (d, f) => {
|
|
3081
|
+
var T;
|
|
3071
3082
|
if (!d || f.length === 0) return [];
|
|
3072
|
-
const [
|
|
3083
|
+
const [h, v, $] = f;
|
|
3073
3084
|
switch (d.type) {
|
|
3074
3085
|
case "logical": {
|
|
3075
3086
|
const b = i(d.operands, v);
|
|
3076
|
-
return b ? [{ kind: "condition", number: v + 1 }, ...
|
|
3087
|
+
return b ? [{ kind: "condition", number: v + 1 }, ...a(b, f.slice(2))] : [];
|
|
3077
3088
|
}
|
|
3078
3089
|
case "not":
|
|
3079
|
-
return [{ kind: "not" }, ...
|
|
3090
|
+
return [{ kind: "not" }, ...a(d.operand, f.slice(1))];
|
|
3080
3091
|
case "binary": {
|
|
3081
|
-
if (
|
|
3082
|
-
const b = { kind: "side", side:
|
|
3083
|
-
return (
|
|
3092
|
+
if (h !== "left" && h !== "right") return [];
|
|
3093
|
+
const b = { kind: "side", side: h }, C = d[h];
|
|
3094
|
+
return (C == null ? void 0 : C.type) === "subquery" && v === "query" ? [b, { kind: "inSubQuery" }, ...m(C.query, f.slice(2))] : (C == null ? void 0 : C.type) === "list" && v === "items" && i(C.items, $) ? [b, { kind: "listItem", number: $ + 1 }] : [b, ...a(C, f.slice(1))];
|
|
3084
3095
|
}
|
|
3085
3096
|
case "function": {
|
|
3086
3097
|
const b = { kind: "function", name: d.name };
|
|
3087
|
-
if (
|
|
3088
|
-
const G =
|
|
3098
|
+
if (h === "args" && i(d.args, v)) {
|
|
3099
|
+
const G = et(d.name, s), R = G && ((T = Ge(G, v)) == null ? void 0 : T.label);
|
|
3089
3100
|
return [
|
|
3090
3101
|
b,
|
|
3091
3102
|
{ kind: "argument", number: v + 1, ...R && { label: R } },
|
|
3092
|
-
...
|
|
3103
|
+
...a(d.args[v], f.slice(2))
|
|
3093
3104
|
];
|
|
3094
3105
|
}
|
|
3095
|
-
const
|
|
3096
|
-
return
|
|
3106
|
+
const C = h === "orderBy" ? i(d.orderBy, v) : void 0;
|
|
3107
|
+
return C ? [
|
|
3097
3108
|
b,
|
|
3098
3109
|
{ kind: "sortKey", number: v + 1 },
|
|
3099
|
-
...
|
|
3110
|
+
...a(C.expression, f.slice(3))
|
|
3100
3111
|
] : [b];
|
|
3101
3112
|
}
|
|
3102
3113
|
case "if":
|
|
3103
|
-
return
|
|
3114
|
+
return h === "condition" || h === "then" || h === "else" ? [{ kind: "if", part: h }, ...a(d[h], f.slice(1))] : [];
|
|
3104
3115
|
case "case": {
|
|
3105
|
-
if (
|
|
3106
|
-
return [{ kind: "case", part:
|
|
3107
|
-
const b =
|
|
3116
|
+
if (h === "subject" || h === "else")
|
|
3117
|
+
return [{ kind: "case", part: h }, ...a(d[h], f.slice(1))];
|
|
3118
|
+
const b = h === "branches" ? i(d.branches, v) : void 0;
|
|
3108
3119
|
return !b || $ !== "when" && $ !== "then" ? [] : [
|
|
3109
3120
|
{ kind: "case", part: $, branch: v + 1 },
|
|
3110
|
-
...
|
|
3121
|
+
...a(b[$], f.slice(3))
|
|
3111
3122
|
];
|
|
3112
3123
|
}
|
|
3113
3124
|
case "exists": {
|
|
3114
|
-
const b = { kind: "exists", table:
|
|
3115
|
-
return
|
|
3125
|
+
const b = { kind: "exists", table: l(d.query.from) };
|
|
3126
|
+
return h === "link" ? [b, { kind: "existsLink" }] : h === "query" ? [b, ...m(d.query, f.slice(1))] : [b];
|
|
3116
3127
|
}
|
|
3117
3128
|
default:
|
|
3118
3129
|
return [];
|
|
3119
3130
|
}
|
|
3120
|
-
},
|
|
3131
|
+
}, o = (d, f) => {
|
|
3121
3132
|
if (d.query && f[0] === "query") return c(d.alias, d.query, f.slice(1));
|
|
3122
|
-
const
|
|
3123
|
-
return f[0] === "on" ? [
|
|
3133
|
+
const h = d.query ? { kind: "subQuery", name: d.alias } : { kind: "table", name: l(d) };
|
|
3134
|
+
return f[0] === "on" ? [h, { kind: "joinCondition" }, ...a(d.on, f.slice(1))] : [h];
|
|
3124
3135
|
};
|
|
3125
|
-
function c(d, f,
|
|
3136
|
+
function c(d, f, h, v = "subQuery") {
|
|
3126
3137
|
const $ = { kind: v, name: d };
|
|
3127
|
-
if (!H(f)) return [$, ...m(f,
|
|
3128
|
-
const
|
|
3129
|
-
if (!
|
|
3130
|
-
const b =
|
|
3131
|
-
return [$, { kind: "unionQuery", number: b }, ...m(
|
|
3138
|
+
if (!H(f)) return [$, ...m(f, h)];
|
|
3139
|
+
const T = h[0] === "union" ? i(be(f), h[1]) : void 0;
|
|
3140
|
+
if (!T) return [$];
|
|
3141
|
+
const b = h[1] + 1;
|
|
3142
|
+
return [$, { kind: "unionQuery", number: b }, ...m(T, h.slice(2))];
|
|
3132
3143
|
}
|
|
3133
3144
|
function m(d, f) {
|
|
3134
3145
|
var G, R;
|
|
3135
|
-
const [
|
|
3136
|
-
if (!
|
|
3137
|
-
const b = { kind: "section", section:
|
|
3138
|
-
switch (
|
|
3146
|
+
const [h, v, $] = f, T = is[String(h)];
|
|
3147
|
+
if (!T) return [];
|
|
3148
|
+
const b = { kind: "section", section: T }, C = f.slice(3);
|
|
3149
|
+
switch (h) {
|
|
3139
3150
|
// A sub query is a place of its own: no "From Tables" before it.
|
|
3140
3151
|
case "from":
|
|
3141
|
-
return d.from.query ? v === "query" ? c(d.from.alias, d.from.query, f.slice(2)) : [{ kind: "subQuery", name: d.from.alias }] : [b, { kind: "table", name:
|
|
3152
|
+
return d.from.query ? v === "query" ? c(d.from.alias, d.from.query, f.slice(2)) : [{ kind: "subQuery", name: d.from.alias }] : [b, { kind: "table", name: l(d.from) }];
|
|
3142
3153
|
case "joins": {
|
|
3143
3154
|
const X = i(d.joins, v);
|
|
3144
|
-
return X ? X.query ?
|
|
3155
|
+
return X ? X.query ? o(X, f.slice(2)) : [b, ...o(X, f.slice(2))] : [b];
|
|
3145
3156
|
}
|
|
3146
3157
|
case "where":
|
|
3147
3158
|
case "having":
|
|
3148
|
-
return [b, ...
|
|
3159
|
+
return [b, ...a(d[h], f.slice(1))];
|
|
3149
3160
|
case "select": {
|
|
3150
3161
|
const X = i(d.select, v);
|
|
3151
3162
|
if (!X) return [b];
|
|
3152
|
-
const { expression: ee, alias: ye } = X,
|
|
3163
|
+
const { expression: ee, alias: ye } = X, pe = ee.type === "field" && t && ee.table !== void 0 ? (R = (G = z(t, ee.table)) == null ? void 0 : G.columns.find((x) => x.name === ee.column)) == null ? void 0 : R.label : void 0, te = ye || pe;
|
|
3153
3164
|
return [
|
|
3154
3165
|
b,
|
|
3155
3166
|
{ kind: "column", number: v + 1, ...te && { name: te } },
|
|
3156
|
-
...$ === "expression" ?
|
|
3167
|
+
...$ === "expression" ? a(ee, C) : []
|
|
3157
3168
|
];
|
|
3158
3169
|
}
|
|
3159
3170
|
case "groupBy":
|
|
3160
3171
|
case "orderBy": {
|
|
3161
|
-
const X = i(d[
|
|
3172
|
+
const X = i(d[h], v);
|
|
3162
3173
|
return X ? [
|
|
3163
3174
|
b,
|
|
3164
|
-
{ kind:
|
|
3165
|
-
...$ === "expression" ?
|
|
3175
|
+
{ kind: h === "groupBy" ? "groupKey" : "sortKey", number: v + 1 },
|
|
3176
|
+
...$ === "expression" ? a(X.expression, C) : []
|
|
3166
3177
|
] : [b];
|
|
3167
3178
|
}
|
|
3168
3179
|
default:
|
|
@@ -3181,7 +3192,7 @@ export {
|
|
|
3181
3192
|
Rr as B,
|
|
3182
3193
|
Qr as C,
|
|
3183
3194
|
ar as D,
|
|
3184
|
-
|
|
3195
|
+
cn as E,
|
|
3185
3196
|
Ys as F,
|
|
3186
3197
|
ei as G,
|
|
3187
3198
|
oi as H,
|
|
@@ -3189,18 +3200,18 @@ export {
|
|
|
3189
3200
|
ii as J,
|
|
3190
3201
|
li as K,
|
|
3191
3202
|
ls as L,
|
|
3192
|
-
|
|
3203
|
+
rt as M,
|
|
3193
3204
|
sr as N,
|
|
3194
|
-
|
|
3195
|
-
|
|
3205
|
+
ot as O,
|
|
3206
|
+
ps as P,
|
|
3196
3207
|
ri as Q,
|
|
3197
|
-
|
|
3208
|
+
hs as R,
|
|
3198
3209
|
$r as S,
|
|
3199
3210
|
bs as T,
|
|
3200
3211
|
gs as U,
|
|
3201
3212
|
cs as V,
|
|
3202
|
-
|
|
3203
|
-
|
|
3213
|
+
vn as W,
|
|
3214
|
+
At as X,
|
|
3204
3215
|
Ds as Y,
|
|
3205
3216
|
js as Z,
|
|
3206
3217
|
Gs as _,
|
|
@@ -3213,43 +3224,44 @@ export {
|
|
|
3213
3224
|
Jr as a5,
|
|
3214
3225
|
_e as a6,
|
|
3215
3226
|
nr as a7,
|
|
3216
|
-
|
|
3227
|
+
Sn as a8,
|
|
3217
3228
|
ai as a9,
|
|
3218
|
-
|
|
3219
|
-
|
|
3220
|
-
|
|
3221
|
-
|
|
3222
|
-
|
|
3223
|
-
|
|
3224
|
-
|
|
3225
|
-
|
|
3226
|
-
|
|
3227
|
-
|
|
3228
|
-
|
|
3229
|
-
|
|
3230
|
-
|
|
3231
|
-
|
|
3232
|
-
|
|
3233
|
-
|
|
3234
|
-
|
|
3235
|
-
|
|
3236
|
-
|
|
3237
|
-
|
|
3238
|
-
|
|
3239
|
-
|
|
3240
|
-
|
|
3241
|
-
hr as
|
|
3242
|
-
|
|
3243
|
-
|
|
3244
|
-
|
|
3245
|
-
|
|
3246
|
-
|
|
3247
|
-
|
|
3248
|
-
|
|
3249
|
-
|
|
3250
|
-
|
|
3251
|
-
|
|
3252
|
-
|
|
3229
|
+
$s as aA,
|
|
3230
|
+
tr as aB,
|
|
3231
|
+
st as aC,
|
|
3232
|
+
ne as aD,
|
|
3233
|
+
Ae as aE,
|
|
3234
|
+
Xe as aF,
|
|
3235
|
+
Ie as aG,
|
|
3236
|
+
rn as aH,
|
|
3237
|
+
ni as aI,
|
|
3238
|
+
zs as aJ,
|
|
3239
|
+
dt as aa,
|
|
3240
|
+
fs as ab,
|
|
3241
|
+
vs as ac,
|
|
3242
|
+
Es as ad,
|
|
3243
|
+
ut as ae,
|
|
3244
|
+
Ss as af,
|
|
3245
|
+
es as ag,
|
|
3246
|
+
Xr as ah,
|
|
3247
|
+
lt as ai,
|
|
3248
|
+
Zs as aj,
|
|
3249
|
+
us as ak,
|
|
3250
|
+
Ns as al,
|
|
3251
|
+
yn as am,
|
|
3252
|
+
hr as an,
|
|
3253
|
+
ms as ao,
|
|
3254
|
+
pr as ap,
|
|
3255
|
+
mr as aq,
|
|
3256
|
+
He as ar,
|
|
3257
|
+
Ts as as,
|
|
3258
|
+
Jn as at,
|
|
3259
|
+
ft as au,
|
|
3260
|
+
Fr as av,
|
|
3261
|
+
_r as aw,
|
|
3262
|
+
oe as ax,
|
|
3263
|
+
ds as ay,
|
|
3264
|
+
ws as az,
|
|
3253
3265
|
er as b,
|
|
3254
3266
|
ys as c,
|
|
3255
3267
|
Us as d,
|
|
@@ -3261,16 +3273,16 @@ export {
|
|
|
3261
3273
|
Dr as j,
|
|
3262
3274
|
Ar as k,
|
|
3263
3275
|
Ps as l,
|
|
3264
|
-
|
|
3276
|
+
vt as m,
|
|
3265
3277
|
_s as n,
|
|
3266
3278
|
qs as o,
|
|
3267
3279
|
wr as p,
|
|
3268
3280
|
xs as q,
|
|
3269
3281
|
Fs as r,
|
|
3270
3282
|
Hs as s,
|
|
3271
|
-
|
|
3283
|
+
Is as t,
|
|
3272
3284
|
Ks as u,
|
|
3273
|
-
|
|
3285
|
+
Cs as v,
|
|
3274
3286
|
Os as w,
|
|
3275
3287
|
Ms as x,
|
|
3276
3288
|
Bs as y,
|