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