@nongt/visql-core 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -9
- package/dist/core.js +336 -78
- package/dist/explain.js +165 -0
- package/dist/issueLocation-BOSdd1JX.js +1106 -0
- package/dist/params-MMOdKWnW.js +2217 -0
- package/dist/{schema-DFC26AhZ.js → schema-dP_ISylQ.js} +270 -242
- package/dist/sql-import.js +1 -1
- package/dist/sqlGen-B7-2-i9V.js +903 -0
- package/dist/types/core/i18n/defaultMessages.d.ts +15 -0
- package/dist/types/core/planFindings.d.ts +27 -0
- package/dist/types/core/queryHelpers.d.ts +1 -1
- package/dist/types/core/schema.d.ts +35 -20
- package/dist/types/core/scopeQuery.d.ts +2 -2
- package/dist/types/core-api.d.ts +1 -0
- package/dist/types/explain/explainQuery.d.ts +53 -0
- package/dist/types/explain.d.ts +2 -0
- package/package.json +11 -3
- package/dist/core-api-jaQDPXVK.js +0 -1171
- package/dist/issueLocation-DEIwB1DW.js +0 -3290
|
@@ -0,0 +1,2217 @@
|
|
|
1
|
+
import { D as Q, h as U, q as S, t as T, i as C, I as _e, u as ne, C as jn, g as te, o as re, J as Te, k as A, a as oe, m as P, r as x, K as Je, n as E, L as qn, N as _, y as q, O as Rn, M as D, e as Ve, s as Gn, d as M, j as se, P as ve, b as Y, H as Fn, x as Qn, Q as We } from "./schema-dP_ISylQ.js";
|
|
2
|
+
import { c as Pn, s as _n, b as He } from "./calendarDate-CYlr9TvX.js";
|
|
3
|
+
const Mt = 1e3, Jn = 15, Ot = 40, Ut = 100;
|
|
4
|
+
function Vn(e) {
|
|
5
|
+
const [n] = e.replace(/^[+-]/, "").split(/e/i), [r, t = ""] = n.split(".");
|
|
6
|
+
return `${r}${t.replace(/0+$/, "")}`.replace(/^0+/, "").length;
|
|
7
|
+
}
|
|
8
|
+
function Wn(e) {
|
|
9
|
+
return Number.isFinite(e) && Vn(String(e)) <= Jn;
|
|
10
|
+
}
|
|
11
|
+
const Lt = [];
|
|
12
|
+
function O(e) {
|
|
13
|
+
return S(e).filter((n) => n.query !== void 0);
|
|
14
|
+
}
|
|
15
|
+
function me(e, n = []) {
|
|
16
|
+
var r;
|
|
17
|
+
return e.query ? e.query : e.shared === void 0 || (r = n.find((t) => t.id === e.shared)) == null ? void 0 : r.query;
|
|
18
|
+
}
|
|
19
|
+
function Xe(e) {
|
|
20
|
+
const n = [], r = (t) => {
|
|
21
|
+
t.type === "subquery" ? n.push(t) : oe(t).forEach(r);
|
|
22
|
+
};
|
|
23
|
+
return _e(e).forEach(r), n;
|
|
24
|
+
}
|
|
25
|
+
function Ke(e, n) {
|
|
26
|
+
let r = !1;
|
|
27
|
+
const t = (s) => {
|
|
28
|
+
if (s.type !== "subquery") return P(s, t);
|
|
29
|
+
const i = n(s);
|
|
30
|
+
return i !== s && (r = !0), i;
|
|
31
|
+
}, o = te(e, t, (s) => s);
|
|
32
|
+
return r ? o : e;
|
|
33
|
+
}
|
|
34
|
+
const Dt = (e, n) => `${e}#${n}`;
|
|
35
|
+
function Hn(e) {
|
|
36
|
+
return !("type" in e) && "of" in e;
|
|
37
|
+
}
|
|
38
|
+
const Xn = (e) => ({
|
|
39
|
+
id: e.id,
|
|
40
|
+
alias: e.name,
|
|
41
|
+
query: e.query
|
|
42
|
+
});
|
|
43
|
+
function Ie(e, n) {
|
|
44
|
+
var o;
|
|
45
|
+
const r = O(e).find((s) => s.id === n);
|
|
46
|
+
if (r) return r;
|
|
47
|
+
const t = (o = e.shared) == null ? void 0 : o.find((s) => s.id === n);
|
|
48
|
+
return t && Xn(t);
|
|
49
|
+
}
|
|
50
|
+
function Ye(e, n) {
|
|
51
|
+
const r = Ie(e, n);
|
|
52
|
+
if (r) return C(r.query) ? void 0 : r;
|
|
53
|
+
const t = Xe(e).find((a) => a.id === n);
|
|
54
|
+
if (t) return t;
|
|
55
|
+
const o = /^(.*)#(\d+)$/.exec(n), s = o ? Ie(e, o[1]) : void 0, i = Number(o == null ? void 0 : o[2]), u = s && C(s.query) ? s.query.union[i] : void 0;
|
|
56
|
+
return s && u ? {
|
|
57
|
+
id: n,
|
|
58
|
+
alias: s.alias,
|
|
59
|
+
query: u,
|
|
60
|
+
of: s,
|
|
61
|
+
index: i
|
|
62
|
+
} : void 0;
|
|
63
|
+
}
|
|
64
|
+
function ze(e) {
|
|
65
|
+
return { id: e.id, ...re(e.query) };
|
|
66
|
+
}
|
|
67
|
+
function le(e) {
|
|
68
|
+
const {
|
|
69
|
+
from: n,
|
|
70
|
+
joins: r,
|
|
71
|
+
where: t,
|
|
72
|
+
select: o,
|
|
73
|
+
groupBy: s,
|
|
74
|
+
having: i,
|
|
75
|
+
distinct: u
|
|
76
|
+
} = e;
|
|
77
|
+
return {
|
|
78
|
+
from: n,
|
|
79
|
+
joins: r,
|
|
80
|
+
select: o,
|
|
81
|
+
...t !== void 0 ? { where: t } : {},
|
|
82
|
+
...s !== void 0 ? { groupBy: s } : {},
|
|
83
|
+
...i !== void 0 ? { having: i } : {},
|
|
84
|
+
...u !== void 0 ? { distinct: u } : {}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function jt(e, n) {
|
|
88
|
+
return n.reduce((r, t) => {
|
|
89
|
+
const o = r && Ye(r, t);
|
|
90
|
+
return o ? ze(o) : void 0;
|
|
91
|
+
}, e);
|
|
92
|
+
}
|
|
93
|
+
function Ze(e, n, r) {
|
|
94
|
+
if (n.length === 0) return r(e);
|
|
95
|
+
const [t, ...o] = n, s = Ye(e, t);
|
|
96
|
+
if (!s) return e;
|
|
97
|
+
const i = ze(s), u = Ze(i, o, r);
|
|
98
|
+
if (u === i || !U(u)) return e;
|
|
99
|
+
if ("type" in s && s.type === "subquery") {
|
|
100
|
+
const l = { ...s, query: le(u) };
|
|
101
|
+
return Ke(e, (c) => c === s ? l : c);
|
|
102
|
+
}
|
|
103
|
+
const a = (l, c) => {
|
|
104
|
+
if (!O(e).some((d) => d.id === l.id))
|
|
105
|
+
return Te(e, (d, m) => m.id === l.id ? c : d);
|
|
106
|
+
const f = { ...l, query: c };
|
|
107
|
+
return l === e.from ? { ...e, from: f } : {
|
|
108
|
+
...e,
|
|
109
|
+
joins: e.joins.map((d) => d === l ? f : d)
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
if (Hn(s)) {
|
|
113
|
+
const { of: l, index: c } = s, { union: f } = l.query;
|
|
114
|
+
return a(l, {
|
|
115
|
+
...l.query,
|
|
116
|
+
union: f.map((d, m) => m === c ? le(u) : d)
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
return a(s, le(u));
|
|
120
|
+
}
|
|
121
|
+
const Kn = {
|
|
122
|
+
text: "varchar",
|
|
123
|
+
integer: "integer",
|
|
124
|
+
number: "decimal",
|
|
125
|
+
date: "date",
|
|
126
|
+
dateTime: "dateTime",
|
|
127
|
+
time: "time",
|
|
128
|
+
boolean: "boolean"
|
|
129
|
+
};
|
|
130
|
+
function pe(e, n, r = []) {
|
|
131
|
+
const t = re(n);
|
|
132
|
+
if (t.select.length === 0)
|
|
133
|
+
return S(t).flatMap((u) => {
|
|
134
|
+
var l;
|
|
135
|
+
if (u.table !== void 0) return ((l = A(e, u.table)) == null ? void 0 : l.columns) ?? [];
|
|
136
|
+
const a = me(u, r);
|
|
137
|
+
return a ? pe(e, a, r) : [];
|
|
138
|
+
});
|
|
139
|
+
const o = new Map(S(t).map((u) => [T(u), u.table])), s = (u) => {
|
|
140
|
+
var a;
|
|
141
|
+
if (u.type === "field") {
|
|
142
|
+
const l = u.table === void 0 ? void 0 : o.get(u.table), c = l === void 0 || (a = A(e, l)) == null ? void 0 : a.columns.find((f) => f.name === u.column);
|
|
143
|
+
return (c == null ? void 0 : c.type) ?? "varchar";
|
|
144
|
+
}
|
|
145
|
+
if (u.type === "function") {
|
|
146
|
+
const l = u.name.toUpperCase();
|
|
147
|
+
if (l === "COUNT") return "integer";
|
|
148
|
+
if (l === "SUM" || l === "AVG") return "decimal";
|
|
149
|
+
if ((l === "MIN" || l === "MAX") && u.args[0]) return s(u.args[0]);
|
|
150
|
+
}
|
|
151
|
+
return u.type === "literal" && typeof u.value == "number" ? "decimal" : u.type === "literal" && u.as ? Kn[u.as] : "varchar";
|
|
152
|
+
}, i = (u) => {
|
|
153
|
+
var f, d;
|
|
154
|
+
if (u.type !== "field" || u.table === void 0) return !1;
|
|
155
|
+
const a = S(t).find((m) => T(m) === u.table);
|
|
156
|
+
if (!a) return !1;
|
|
157
|
+
const l = a.table === void 0 ? me(a, r) : void 0, c = a.table !== void 0 ? (f = A(e, a.table)) == null ? void 0 : f.columns : l && pe(e, l, r);
|
|
158
|
+
return !!((d = c == null ? void 0 : c.find((m) => m.name === u.column)) != null && d.joinOnly);
|
|
159
|
+
};
|
|
160
|
+
return t.select.flatMap((u) => {
|
|
161
|
+
const a = x(u);
|
|
162
|
+
return a ? [{
|
|
163
|
+
name: a,
|
|
164
|
+
label: a,
|
|
165
|
+
type: s(u.expression),
|
|
166
|
+
...i(u.expression) ? { joinOnly: !0 } : {}
|
|
167
|
+
}] : [];
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
function en(e, n, r = []) {
|
|
171
|
+
const t = me(n, r), o = (t ? pe(e, t, r) : []).filter((s, i, u) => u.findIndex((a) => a.name === s.name) === i);
|
|
172
|
+
return { name: n.alias, label: n.alias, columns: o };
|
|
173
|
+
}
|
|
174
|
+
function qt(e, n, r = []) {
|
|
175
|
+
return S(n).filter((t) => t.table === void 0).map((t) => en(e, t, r));
|
|
176
|
+
}
|
|
177
|
+
function Rt(e, n = Q) {
|
|
178
|
+
if (C(e)) return !1;
|
|
179
|
+
const r = e;
|
|
180
|
+
return (r.groupBy ?? []).length === 0 && r.select.length > 0 && r.select.every((t) => ne(t.expression, n)) && jn(r, n).select.length === 0;
|
|
181
|
+
}
|
|
182
|
+
function ke(e, n, r) {
|
|
183
|
+
const t = (o) => o.type === "field" ? o.table === n ? { ...o, table: r } : o : o.type === "exists" && Je(o.query).has(n) ? { ...o, link: t(o.link) } : P(o, t);
|
|
184
|
+
return te(e, t, (o) => o);
|
|
185
|
+
}
|
|
186
|
+
function nn(e, n) {
|
|
187
|
+
const r = re(n), t = S(r).flatMap((o) => o.table === void 0 ? [] : [{ table: o.table, alias: o.alias }]);
|
|
188
|
+
return r.select.length === 0 ? t.flatMap(({ table: o }) => {
|
|
189
|
+
var s;
|
|
190
|
+
return (((s = A(e, o)) == null ? void 0 : s.columns) ?? []).map((i) => ({ name: i.name, table: o, column: i.name }));
|
|
191
|
+
}) : r.select.flatMap((o) => {
|
|
192
|
+
const { expression: s } = o;
|
|
193
|
+
if (s.type !== "field" || !s.column) return [];
|
|
194
|
+
const i = t.find((a) => T(a) === s.table), u = o.alias || s.column;
|
|
195
|
+
return i ? [{ name: u, table: i.table, column: s.column }] : [];
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
function tn(e, n) {
|
|
199
|
+
return O(n).flatMap((r) => nn(e, r.query).flatMap(({ name: t, table: o, column: s }) => e.relations.flatMap((i) => {
|
|
200
|
+
const u = i.fromTable === o && i.fromColumn === s;
|
|
201
|
+
if (!u && !(i.toTable === o && i.toColumn === s)) return [];
|
|
202
|
+
const a = u ? i.toTable : i.fromTable, l = {
|
|
203
|
+
type: "binary",
|
|
204
|
+
operator: "=",
|
|
205
|
+
left: { type: "field", table: a, column: u ? i.toColumn : i.fromColumn },
|
|
206
|
+
right: { type: "field", table: r.alias, column: t }
|
|
207
|
+
};
|
|
208
|
+
return [{ table: a, on: l }];
|
|
209
|
+
})));
|
|
210
|
+
}
|
|
211
|
+
function Yn(e, n, r) {
|
|
212
|
+
var t;
|
|
213
|
+
return (t = tn(e, n).find((o) => o.table === r)) == null ? void 0 : t.on;
|
|
214
|
+
}
|
|
215
|
+
function rn(e, n, r) {
|
|
216
|
+
var a;
|
|
217
|
+
const t = S(n).flatMap((l) => l !== r && l.table !== void 0 ? [{ table: l.table, alias: l.alias }] : []), o = (l, c) => c === void 0 ? { type: "field", table: l } : { type: "field", table: l, column: c }, i = nn(e, r.query).flatMap(({ name: l, table: c, column: f }) => t.flatMap((d) => e.relations.filter((m) => m.fromTable === c && m.fromColumn === f && m.toTable === d.table || m.toTable === c && m.toColumn === f && m.fromTable === d.table).map((m) => ({
|
|
218
|
+
name: l,
|
|
219
|
+
right: o(T(d), m.fromTable === c ? m.toColumn : m.fromColumn)
|
|
220
|
+
}))))[0], u = (a = en(e, r).columns[0]) == null ? void 0 : a.name;
|
|
221
|
+
return {
|
|
222
|
+
type: "binary",
|
|
223
|
+
operator: "=",
|
|
224
|
+
left: o(r.alias, (i == null ? void 0 : i.name) ?? u),
|
|
225
|
+
right: (i == null ? void 0 : i.right) ?? { type: "field" }
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
const H = (e, n) => `${e} ${n}${e === 1 ? "" : "s"}`;
|
|
229
|
+
function zn({ unit: e, offset: n, anchor: r }) {
|
|
230
|
+
const t = Math.abs(n);
|
|
231
|
+
if (r) {
|
|
232
|
+
const o = r === "start" ? "Start" : "End";
|
|
233
|
+
return n === 0 ? `${o} of this ${e}` : t === 1 ? `${o} of ${n < 0 ? "last" : "next"} ${e}` : n < 0 ? `${o} of the ${e} ${H(t, e)} ago` : `${o} of the ${e} in ${H(t, e)}`;
|
|
234
|
+
}
|
|
235
|
+
return n === 0 ? "Today" : e === "day" && t === 1 ? n < 0 ? "Yesterday" : "Tomorrow" : n < 0 ? `${H(t, e)} ago` : `In ${H(t, e)}`;
|
|
236
|
+
}
|
|
237
|
+
const Zn = {
|
|
238
|
+
// Section captions on the main canvas
|
|
239
|
+
sectionFrom: "From Tables",
|
|
240
|
+
sectionWhere: "Conditions",
|
|
241
|
+
sectionGroup: "Group Rows",
|
|
242
|
+
sectionSelect: "Columns Selection",
|
|
243
|
+
sectionOrder: "Sort & Limit",
|
|
244
|
+
/** Shared queries (WITH, issue 194): their section, before FROM. */
|
|
245
|
+
sectionShared: "Shared Queries",
|
|
246
|
+
// Breadcrumb / navigation
|
|
247
|
+
main: "Main",
|
|
248
|
+
close: "Close (ESC)",
|
|
249
|
+
jumpedTo: "Jumped to",
|
|
250
|
+
editInSubCanvas: (e) => `Edit in sub-canvas (${e})`,
|
|
251
|
+
// Reordering
|
|
252
|
+
moveUp: "Move Up",
|
|
253
|
+
moveDown: "Move Down",
|
|
254
|
+
moveGroupUp: "Move Group Up",
|
|
255
|
+
moveGroupDown: "Move Group Down",
|
|
256
|
+
moveBranchUp: "Move Branch Up",
|
|
257
|
+
moveBranchDown: "Move Branch Down",
|
|
258
|
+
// Conditions and groups
|
|
259
|
+
createCondition: "Create Condition",
|
|
260
|
+
/** Read-only mode, empty WHERE: stands in for the Create Condition button. */
|
|
261
|
+
noCondition: "No Condition",
|
|
262
|
+
addCondition: "Add Condition",
|
|
263
|
+
addGroup: "Add Group",
|
|
264
|
+
addIfExpression: "Add IF Expression",
|
|
265
|
+
addCaseExpression: "Add CASE Expression",
|
|
266
|
+
wrapInGroup: "Wrap in Group",
|
|
267
|
+
wrapInNewGroup: "Wrap in New Group",
|
|
268
|
+
collapseGroup: "Collapse Group",
|
|
269
|
+
// Wrapping in NOT (shown as NOT itself, like AND/OR — not "Exclude",
|
|
270
|
+
// which reads as "everything else" and NOT leaves out NULL rows too).
|
|
271
|
+
negateCondition: "Negate Condition",
|
|
272
|
+
negateGroup: "Negate Group",
|
|
273
|
+
removeNot: "Remove NOT",
|
|
274
|
+
delete: "Delete",
|
|
275
|
+
deleteGroup: "Delete Group",
|
|
276
|
+
deleteIf: "Delete IF",
|
|
277
|
+
deleteCase: "Delete CASE",
|
|
278
|
+
// EXISTS: whether a row has related rows ("Has Leave Request linked by
|
|
279
|
+
// Employee · Employee ID where …"). The button itself reads EXISTS.
|
|
280
|
+
addExists: "Add a related-rows condition",
|
|
281
|
+
/** An entry of its menu when the same table is linked more than one way
|
|
282
|
+
* (`via`: the foreign key's column). */
|
|
283
|
+
existsVia: (e, n) => `${e} — by ${n}`,
|
|
284
|
+
existsHas: "Has",
|
|
285
|
+
existsLinkedBy: "linked by",
|
|
286
|
+
existsWhere: "where",
|
|
287
|
+
/** No conditions on the related rows: any will do. */
|
|
288
|
+
existsAny: "any",
|
|
289
|
+
existsEdit: "Edit the related rows' conditions",
|
|
290
|
+
// IF / CASE
|
|
291
|
+
addBranch: "Add Branch",
|
|
292
|
+
removeBranch: "Remove Branch",
|
|
293
|
+
addElse: "Add ELSE",
|
|
294
|
+
removeElse: "Remove ELSE",
|
|
295
|
+
// Functions
|
|
296
|
+
addArg: "Add Arg",
|
|
297
|
+
removeArg: "Remove Arg",
|
|
298
|
+
// Value type menu
|
|
299
|
+
typeField: "Field",
|
|
300
|
+
typeValue: "Value",
|
|
301
|
+
/** NULL, picked as a value (no value, on purpose). */
|
|
302
|
+
typeNull: "Empty",
|
|
303
|
+
/** A value's types, picked when nothing typed is compared with it
|
|
304
|
+
* (issue 130). */
|
|
305
|
+
typeText: "Text",
|
|
306
|
+
typeNumber: "Number",
|
|
307
|
+
typeDate: "Date",
|
|
308
|
+
typeBoolean: "Yes / No",
|
|
309
|
+
typeFunction: "Function",
|
|
310
|
+
typeExpression: "Expression",
|
|
311
|
+
/** A named param (set by the application, shown read-only). */
|
|
312
|
+
typeParam: "Parameter",
|
|
313
|
+
/** A date relative to when the query runs (issue 140): the type menu's
|
|
314
|
+
* item, its words ("30 days ago", "Start of last month"), its presets'
|
|
315
|
+
* "Custom", and its custom parts. */
|
|
316
|
+
typeRelativeDate: "Relative date",
|
|
317
|
+
relativeDate: zn,
|
|
318
|
+
relativeCustom: "Custom…",
|
|
319
|
+
relativeUnits: {
|
|
320
|
+
day: "days",
|
|
321
|
+
week: "weeks",
|
|
322
|
+
month: "months",
|
|
323
|
+
year: "years"
|
|
324
|
+
},
|
|
325
|
+
relativeAgo: "ago",
|
|
326
|
+
relativeFromNow: "from now",
|
|
327
|
+
relativeSameDay: "That day",
|
|
328
|
+
relativeStart: "Start of it",
|
|
329
|
+
relativeEnd: "End of it",
|
|
330
|
+
/** A parameter's value isn't in the query: it's given when it runs. */
|
|
331
|
+
paramHint: "Given when the query runs",
|
|
332
|
+
paramNamePlaceholder: "name",
|
|
333
|
+
typeMenuBasic: "Basic",
|
|
334
|
+
/** A function picker's groups, where aggregates are offered: those
|
|
335
|
+
* (which group the rows) apart from the rest. Kept short: the list is
|
|
336
|
+
* as narrow as the name box. */
|
|
337
|
+
functionGroupAggregate: "Aggregates",
|
|
338
|
+
functionGroupOther: "Functions",
|
|
339
|
+
typeMenuCondition: "Condition",
|
|
340
|
+
/** IN's value as a query of its own (issue 122), when offered: its type,
|
|
341
|
+
* its menu section, and its canvas's name. */
|
|
342
|
+
typeSubquery: "Sub query",
|
|
343
|
+
typeMenuQuery: "Query",
|
|
344
|
+
/** IN's values, one each (issue 130): its type, adding one, taking one
|
|
345
|
+
* out. */
|
|
346
|
+
typeList: "Values",
|
|
347
|
+
/** A union of tables' rows (issue 121): the add-table picker's groups
|
|
348
|
+
* when rows can be added. */
|
|
349
|
+
/** A NULL value (no value at all): on a union's branch, that table's
|
|
350
|
+
* having none for the column — and filling one in instead. */
|
|
351
|
+
nullValue: "No value",
|
|
352
|
+
nullInUnion: "Empty for this table",
|
|
353
|
+
fillInValue: "Fill in a value",
|
|
354
|
+
openUnionBranch: (e) => `Open ${e}'s query`,
|
|
355
|
+
removeUnionBranch: (e) => `Remove ${e}'s query`,
|
|
356
|
+
joinTableGroup: "Join a table",
|
|
357
|
+
/** The heading over tables in no group, when others have one. */
|
|
358
|
+
otherTablesGroup: "Other tables",
|
|
359
|
+
/** Free-form unions (issue 153): a new one, the query as one's first
|
|
360
|
+
* query, another query for one, and a new query's canvas. */
|
|
361
|
+
createUnion: "Create Union",
|
|
362
|
+
unionWithQuery: "Union with another query",
|
|
363
|
+
addUnionQuery: "Add query",
|
|
364
|
+
/** A union of one query back as a plain query. */
|
|
365
|
+
unwrapUnion: "Make it a plain query",
|
|
366
|
+
newUnionQuery: "New query in the union",
|
|
367
|
+
/** Issue 153, step 4: a union joined. */
|
|
368
|
+
joinUnion: "Join a union",
|
|
369
|
+
addRowsGroup: "Add rows from another table",
|
|
370
|
+
addListValue: "Add value",
|
|
371
|
+
removeListValue: (e) => `Remove ${e}`,
|
|
372
|
+
subqueryLabel: "Sub query",
|
|
373
|
+
// A measure from another table (drill-across): in a SELECT column's type
|
|
374
|
+
// menu, then on its sub query.
|
|
375
|
+
/** The type menu's section, and its one item. */
|
|
376
|
+
typeMenuMeasure: "Measure",
|
|
377
|
+
measureFrom: "Measure from…",
|
|
378
|
+
/** Why it's off: nothing is summed up yet, so there are no groups. */
|
|
379
|
+
measureNeedsGroups: "Sum something up first (a measure is per group)",
|
|
380
|
+
/** Why a table can't be one: a group key's table it doesn't link to. */
|
|
381
|
+
measureUnlinked: (e) => `Doesn't link to ${e}`,
|
|
382
|
+
/** What to sum up of the table picked. */
|
|
383
|
+
measureCount: "Count of rows",
|
|
384
|
+
measureOf: (e, n) => `${{
|
|
385
|
+
SUM: "Sum",
|
|
386
|
+
AVG: "Average",
|
|
387
|
+
MIN: "Minimum",
|
|
388
|
+
MAX: "Maximum"
|
|
389
|
+
}[e]} of ${n}`,
|
|
390
|
+
/** The checkbox on a measure's FROM row. */
|
|
391
|
+
keepInStep: "Keep in step with the report",
|
|
392
|
+
/** On a measure's canvas: what it follows, while kept in step. */
|
|
393
|
+
measureFollows: (e) => `Kept in step with the report: per ${e}`,
|
|
394
|
+
measureAlso: (e) => `also ${e}`,
|
|
395
|
+
/** Where its rows come from (the report's base decides). */
|
|
396
|
+
measureRows: (e) => `Only groups with ${e} rows appear. To see every group, start from the table they belong to.`,
|
|
397
|
+
// FROM / JOIN
|
|
398
|
+
chooseBaseTableHint: "Choose a base table to get started.",
|
|
399
|
+
chooseBaseTable: "Choose a base table",
|
|
400
|
+
addTable: "+ Add Table / Sub Query",
|
|
401
|
+
removeJoin: "Remove Join",
|
|
402
|
+
removeBaseTable: "Remove Base Table",
|
|
403
|
+
setJoinCondition: "Set condition…",
|
|
404
|
+
conditionCount: (e) => `${e} condition${e === 1 ? "" : "s"}`,
|
|
405
|
+
/** Why a FROM row can't move there: `table`'s ON condition uses `needs`,
|
|
406
|
+
* which would then only be joined after it. */
|
|
407
|
+
joinOrderConflict: (e, n) => `${e}'s join condition uses ${n}, which must come first`,
|
|
408
|
+
// Sub queries (derived tables): a query of its own, joined like a table
|
|
409
|
+
// or as the base.
|
|
410
|
+
/** First in the Add Table and base table pickers: opens a new one's
|
|
411
|
+
* canvas, to pick its first table. */
|
|
412
|
+
createSubQuery: "Create Sub Query",
|
|
413
|
+
/** That canvas's label until its first table is picked. */
|
|
414
|
+
newSubQuery: "New sub query",
|
|
415
|
+
/** The icon after a sub query's name, which opens its own canvas: its
|
|
416
|
+
* tooltip and name (in read-only mode: viewSubQuery). */
|
|
417
|
+
editSubQuery: "Edit Sub Query",
|
|
418
|
+
viewSubQuery: "View Sub Query",
|
|
419
|
+
/** A derived table of one row (a total): CROSS JOIN it, no condition. */
|
|
420
|
+
joinToEveryRow: "Join to every row",
|
|
421
|
+
/** In place of its condition, once CROSS joined. */
|
|
422
|
+
joinedToEveryRow: "Joined to every row",
|
|
423
|
+
/** Shared queries (issue 194): the pickers' group of them; the action
|
|
424
|
+
* that makes one (its canvas, empty, labelled newSharedQuery until its
|
|
425
|
+
* first table); the icon opening one (in read-only mode:
|
|
426
|
+
* viewSharedQuery); removing one (its places become sub queries). */
|
|
427
|
+
sharedQueriesGroup: "Shared Queries",
|
|
428
|
+
createSharedQuery: "Create Shared Query",
|
|
429
|
+
newSharedQuery: "New shared query",
|
|
430
|
+
editSharedQuery: "Edit Shared Query",
|
|
431
|
+
viewSharedQuery: "View Shared Query",
|
|
432
|
+
removeSharedQuery: "Remove Shared Query",
|
|
433
|
+
/** The section's "+": a shared query made before it's used. */
|
|
434
|
+
addSharedQuery: "New Shared Query",
|
|
435
|
+
/** Under a shared query's name: where it's used. */
|
|
436
|
+
sharedUsedIn: (e) => e === 0 ? "Not used yet" : e === 1 ? "Used in 1 place" : `Used in ${e} places`,
|
|
437
|
+
/** On a sub query's row: it becomes a shared query; on a shared query's
|
|
438
|
+
* place: it gets a copy of its own, a sub query. */
|
|
439
|
+
makeShared: "Make Shared",
|
|
440
|
+
makeSubQuery: "Make Sub Query",
|
|
441
|
+
/** Removing a shared query that's used: what happens to its places. */
|
|
442
|
+
removeSharedKeepPlaces: "Remove: each place gets a copy as a sub query",
|
|
443
|
+
removeSharedWithPlaces: "Remove, with the tables using it",
|
|
444
|
+
/** Why the second can't be: a query reads from it as its base. */
|
|
445
|
+
removeSharedIsBase: "A query reads from it as its base",
|
|
446
|
+
/** Renaming a shared query: a name another one, or a table, has. */
|
|
447
|
+
sharedNameTaken: (e) => `Another shared query or table is called "${e}"`,
|
|
448
|
+
/** Renaming a derived table: no name given. */
|
|
449
|
+
derivedNameEmpty: "Give it a name",
|
|
450
|
+
/** Renaming a derived table: a name another table already has. */
|
|
451
|
+
derivedNameTaken: (e) => `Another table is called "${e}"`,
|
|
452
|
+
// GROUP BY / HAVING ("group" alone already names an AND/OR group)
|
|
453
|
+
/** The "+" that starts grouping. */
|
|
454
|
+
groupRows: "Group Rows",
|
|
455
|
+
/** A group key added for a SELECT column, following it. */
|
|
456
|
+
groupKeyFromColumn: (e) => `From column ${e}`,
|
|
457
|
+
/** A grouped query's HAVING or sort key with no group key to pick: why,
|
|
458
|
+
* and what to do instead. */
|
|
459
|
+
onlyGroupKeys: "Only group keys here — sum up another column with a function",
|
|
460
|
+
addGrouping: "Add Grouping",
|
|
461
|
+
removeGrouping: "Remove Grouping",
|
|
462
|
+
/** Read-only mode, no grouping. */
|
|
463
|
+
notGrouped: "Not grouped",
|
|
464
|
+
/** Above HAVING's tree (and the "+" that starts it) on the canvas — the
|
|
465
|
+
* counterpart of `sectionWhere`: conditions on groups, not rows. */
|
|
466
|
+
havingCaption: "Group Conditions",
|
|
467
|
+
addHaving: "Add Group Condition",
|
|
468
|
+
/** A column (in SELECT, ORDER BY or HAVING) that isn't one value per group. */
|
|
469
|
+
notPerGroup: "Each group has many of these values: group by it, or summarize it (SUM, COUNT…)",
|
|
470
|
+
/** Quick fix on a SELECT column that isn't one value per group. */
|
|
471
|
+
groupByColumn: "Group by this",
|
|
472
|
+
/** A sum, average or count over rows the joins repeat (the tables'
|
|
473
|
+
* labels, joined). */
|
|
474
|
+
fanOut: (e) => `${e} rows repeat in these joins (each matches several joined rows), so sums, averages and counts of their columns come out too high`,
|
|
475
|
+
/** A grouped query with no columns picked (SELECT *). */
|
|
476
|
+
groupedNeedsColumns: "Pick the columns to show when rows are grouped",
|
|
477
|
+
// COUNT's choice of what to count (the function sub-canvas)
|
|
478
|
+
countRows: "Every row",
|
|
479
|
+
countValues: "Rows with a value",
|
|
480
|
+
countDistinct: "Different values",
|
|
481
|
+
// STRING_AGG's choice of what to join (the function sub-canvas)
|
|
482
|
+
joinEveryValue: "Every value",
|
|
483
|
+
joinDistinctValues: "Different values",
|
|
484
|
+
// SELECT
|
|
485
|
+
addColumn: "Add Column",
|
|
486
|
+
/** Read-only mode, no columns picked: every column is selected. */
|
|
487
|
+
allColumns: "All",
|
|
488
|
+
removeColumn: "Remove Column",
|
|
489
|
+
aliasPlaceholder: "alias",
|
|
490
|
+
/** The SELECT node's DISTINCT switch. */
|
|
491
|
+
distinct: "Leave out duplicate rows",
|
|
492
|
+
/** Read-only mode, DISTINCT on. */
|
|
493
|
+
distinctOn: "Duplicate rows left out",
|
|
494
|
+
// ORDER BY / LIMIT / OFFSET (ASC/DESC themselves are SQL, not translated)
|
|
495
|
+
addSort: "Add Sort",
|
|
496
|
+
removeSort: "Remove Sort",
|
|
497
|
+
toggleSortDirection: "Ascending / descending",
|
|
498
|
+
/** Opens the row-count inputs. */
|
|
499
|
+
limitRows: "Limit rows",
|
|
500
|
+
removeLimit: "Remove row limit",
|
|
501
|
+
// The row-count inputs read as one sentence, each input between two
|
|
502
|
+
// fragments: "Show [50] rows, skipping the first [100]". A fragment may be
|
|
503
|
+
// empty where the language doesn't need it.
|
|
504
|
+
showRowsBefore: "Show",
|
|
505
|
+
showRowsAfter: "rows,",
|
|
506
|
+
skipRowsBefore: "skipping the first",
|
|
507
|
+
skipRowsAfter: "",
|
|
508
|
+
/** What the row counts come to (1-based; `to` undefined: no limit). */
|
|
509
|
+
rowRange: (e, n) => n === void 0 ? `Rows ${e} onward` : `Rows ${e}–${n}`,
|
|
510
|
+
allRows: "All rows",
|
|
511
|
+
/** LIMIT 0. */
|
|
512
|
+
noRows: "No rows",
|
|
513
|
+
/** Skipping rows without a sort: the database may return any rows. */
|
|
514
|
+
pagingNeedsSort: "Add a sort, or each page may get different rows",
|
|
515
|
+
/** DISTINCT on, and a sort key that isn't a selected column (no database
|
|
516
|
+
* accepts that). */
|
|
517
|
+
sortNeedsSelected: "Duplicate rows are left out, so sort only by selected columns",
|
|
518
|
+
/** Read-only mode, no sorting and no limit. */
|
|
519
|
+
noSort: "Unsorted",
|
|
520
|
+
// A text value marked as the empty text (the '' toggle next to it)
|
|
521
|
+
emptyText: "Empty",
|
|
522
|
+
markEmpty: "Empty text (no characters at all)",
|
|
523
|
+
unmarkEmpty: "Type a value instead",
|
|
524
|
+
// Running the query (the host's onRun) and its results under the canvas
|
|
525
|
+
run: "Run",
|
|
526
|
+
running: "Running…",
|
|
527
|
+
/** The Run button's tooltip. */
|
|
528
|
+
runShortcut: "Run the query (Ctrl/⌘+Enter)",
|
|
529
|
+
/** Why Run is off: no table picked yet. */
|
|
530
|
+
runNeedsTable: "Choose a table first",
|
|
531
|
+
/** Why Run is off: the query has errors. */
|
|
532
|
+
runNeedsFixes: (e) => `Fix ${e} problem${e === 1 ? "" : "s"} first`,
|
|
533
|
+
runFailed: "The query failed",
|
|
534
|
+
/** How many rows came back (`truncated`: more matched). */
|
|
535
|
+
runRows: (e, n) => `${n ? "First " : ""}${e} row${e === 1 ? "" : "s"}`,
|
|
536
|
+
runNoRows: "No rows",
|
|
537
|
+
/** The results are from before the latest edits. */
|
|
538
|
+
runOutdated: "The query has changed since this run",
|
|
539
|
+
closeResults: "Close results",
|
|
540
|
+
// The problems button (above the canvas) and its list (issue 132): errors,
|
|
541
|
+
// then warnings, one entry per validateQuery issue, its text by kind (see
|
|
542
|
+
// QueryIssueKind) and under it where it is (issueLocation).
|
|
543
|
+
problems: (e) => `${e} problem${e === 1 ? "" : "s"}`,
|
|
544
|
+
problemsErrors: "Errors — the query can't run",
|
|
545
|
+
problemsWarnings: "Warnings — it runs, check the result",
|
|
546
|
+
/** Issues about the query as a whole (no one section). */
|
|
547
|
+
problemsQuery: "Query",
|
|
548
|
+
// Performance hints from the database's query plan (the host's
|
|
549
|
+
// onExplain, issue 186): a button above the canvas, and a group of their
|
|
550
|
+
// own in the problems list, one entry per PlanFinding.
|
|
551
|
+
checkPerformance: "Check performance",
|
|
552
|
+
checkingPerformance: "Checking…",
|
|
553
|
+
/** The button's tooltip. */
|
|
554
|
+
checkPerformanceHint: "Ask the database how it would run this query",
|
|
555
|
+
/** What the host's onExplain threw. */
|
|
556
|
+
checkPerformanceFailed: (e) => `Couldn't check performance: ${e}`,
|
|
557
|
+
performanceHints: (e) => `${e} performance hint${e === 1 ? "" : "s"}`,
|
|
558
|
+
problemsPerformance: "Performance — it runs, but may be slow",
|
|
559
|
+
/** The hints are from before the latest edits. */
|
|
560
|
+
problemsPerformanceOutdated: "Performance — from before your latest edits",
|
|
561
|
+
/** `rows`: the plan's estimate, when it gives one. */
|
|
562
|
+
planFullScan: (e, n) => `Reads all of ${e}${n === void 0 ? "" : ` (about ${n.toLocaleString()} rows)`}`,
|
|
563
|
+
planMissingIndex: (e, n) => `No index on ${e} for ${n.join(", ")}`,
|
|
564
|
+
planSort: (e) => e === void 0 ? "The database sorts the rows itself" : `The database sorts about ${e.toLocaleString()} rows itself`,
|
|
565
|
+
planTemporaryTable: (e) => `Builds a temporary table${e === void 0 ? "" : ` of about ${e.toLocaleString()} rows`}`,
|
|
566
|
+
// Where a problem is: the steps of its location, joined by ' › '. A
|
|
567
|
+
// section is its caption, a sub query its name, a column its alias;
|
|
568
|
+
// SQL keywords (NOT, IF, CASE, WHEN) stay as they are.
|
|
569
|
+
locationJoinCondition: "join condition",
|
|
570
|
+
locationColumn: (e) => `column ${e}`,
|
|
571
|
+
locationGroupKey: (e) => `key ${e}`,
|
|
572
|
+
locationSortKey: (e) => `sort key ${e}`,
|
|
573
|
+
locationCondition: (e) => `condition ${e}`,
|
|
574
|
+
locationLeft: "left side",
|
|
575
|
+
locationRight: "right side",
|
|
576
|
+
/** One of IN's values, or a function's argument with no label. */
|
|
577
|
+
locationValue: (e) => `value ${e}`,
|
|
578
|
+
locationUnionQuery: (e) => `query ${e}`,
|
|
579
|
+
locationInSubquery: "sub query",
|
|
580
|
+
issueInvalidStructure: "Not in the form a query takes",
|
|
581
|
+
issueDuplicateId: "Two parts share an id",
|
|
582
|
+
issueUnknownTable: "A table that isn't available",
|
|
583
|
+
issueUnknownColumn: "A column that isn't available",
|
|
584
|
+
issueTableNotInQuery: "A column of a table that isn't in the query",
|
|
585
|
+
issueUnqualifiedField: "A column with no table",
|
|
586
|
+
issueIncomplete: "Not filled in yet",
|
|
587
|
+
issueArgCount: "A function given too many values",
|
|
588
|
+
issueAmbiguousTable: "A table named like one of the query around it",
|
|
589
|
+
issueDuplicateTableName: "Two tables with the same name: give one an alias",
|
|
590
|
+
issueDerivedColumnName: "A sub query's column with no name, or a name used twice",
|
|
591
|
+
issueUnknownFunction: "A function that isn't available",
|
|
592
|
+
issueJoinOrder: "A join condition uses a table joined after it",
|
|
593
|
+
issueDistinctSort: "Sorted by a column that isn't selected",
|
|
594
|
+
issueNotGrouped: "A column that isn't one value per group",
|
|
595
|
+
issueUnknownAggregate: "A function that may or may not sum rows up",
|
|
596
|
+
issueMisplacedAggregate: "SUM, COUNT and the like can't be used here",
|
|
597
|
+
issueParamConflict: "A parameter compared with columns of different types",
|
|
598
|
+
issueFanOut: "Sums, averages or counts come out too high: a join repeats rows",
|
|
599
|
+
issueCrossJoin: "Every row joined to every row of a table: rows multiply",
|
|
600
|
+
issueDrillAcrossOutOfStep: "A measure kept in step that no longer matches the grouping",
|
|
601
|
+
issueDrillAcrossFilterMixed: "A condition a measure from another table can't follow",
|
|
602
|
+
issueUnlinkedJoin: "A join condition not linking its table to one before it: rows multiply",
|
|
603
|
+
issueTooLong: "Too long: a name, a text, or a number with too many digits",
|
|
604
|
+
issueDialectUnsupported: "Something this database doesn't have",
|
|
605
|
+
issueSubqueryColumns: "A sub query after IN must pick exactly one column",
|
|
606
|
+
issueNotInSubquery: "NOT IN a sub query keeps no rows if it gives a NULL",
|
|
607
|
+
issueNoLimit: "No row limit: reads every row",
|
|
608
|
+
issueLimitTooHigh: "More rows than allowed",
|
|
609
|
+
issueTypeMismatch: "Values of types that don't go together",
|
|
610
|
+
issueInvalidPattern: "A pattern that isn't a regular expression",
|
|
611
|
+
issueUnionColumns: "Stacked queries whose columns don't line up",
|
|
612
|
+
issueUnionTypes: "Stacked queries giving a column of different types",
|
|
613
|
+
issueSharedOrder: "A shared query can use only the shared queries before it",
|
|
614
|
+
issueUnusedShared: "A shared query used nowhere",
|
|
615
|
+
issueJoinOnlyColumn: "A column for joining tables only, used as a value",
|
|
616
|
+
// What a problem is about, where the query says (issue 132); the kind's
|
|
617
|
+
// text above where it doesn't.
|
|
618
|
+
issueUnknownTableNamed: (e) => `There's no table "${e}"`,
|
|
619
|
+
issueUnknownColumnNamed: (e, n) => `${e} has no column "${n}"`,
|
|
620
|
+
issueJoinOnlyColumnNamed: (e) => `${e} is for joining tables only`,
|
|
621
|
+
issueTableNotInQueryNamed: (e) => `"${e}" isn't one of the query's tables`,
|
|
622
|
+
issueUnqualifiedFieldNamed: (e) => `"${e}" names no table`,
|
|
623
|
+
issueAmbiguousTableNamed: (e) => `"${e}" is a table of the query around it too: give it an alias`,
|
|
624
|
+
issueNoColumn: "No column picked",
|
|
625
|
+
issueNoValue: "No value filled in",
|
|
626
|
+
issueNothingToCompare: "Nothing to compare with",
|
|
627
|
+
issueComparedWithNull: "Compared with NULL, which is never true: use IS NULL",
|
|
628
|
+
issueNoJoinCondition: (e) => `${e}'s join condition isn't set`,
|
|
629
|
+
issueEmptyGroup: "An empty group of conditions",
|
|
630
|
+
/** `part`: 'condition', or the keyword THEN. */
|
|
631
|
+
issueIfPartMissing: (e) => `IF with no ${e}`,
|
|
632
|
+
issueCaseNoWhen: "CASE with no WHEN",
|
|
633
|
+
issueInNoValues: "IN with no values",
|
|
634
|
+
issueFunctionNeedsArgs: (e, n) => `${e} needs ${n} value${n === 1 ? "" : "s"}`,
|
|
635
|
+
issueFunctionTakesArgs: (e, n) => `${e} takes at most ${n} value${n === 1 ? "" : "s"}`,
|
|
636
|
+
issueDuplicateTableNamed: (e) => `Two tables named "${e}": give one an alias`,
|
|
637
|
+
issueDuplicateColumnNamed: (e) => `Two columns named "${e}"`,
|
|
638
|
+
issueUnnamedColumn: "A column with no name: give it an alias",
|
|
639
|
+
issueUnknownFunctionNamed: (e) => `${e} isn't an available function`,
|
|
640
|
+
issueDialectUnsupportedNamed: (e) => `${e} isn't in this database`,
|
|
641
|
+
issueJoinOrderNamed: (e) => `${e}'s join condition uses a table joined after it`,
|
|
642
|
+
issueNotGroupedNamed: (e) => `${e} isn't one value per group: group by it, or sum it up`,
|
|
643
|
+
issueSelectAllGrouped: "A grouped query has to pick its columns, not all of them (*)",
|
|
644
|
+
issueMisplacedAggregateNamed: (e) => `${e} can't be used here`,
|
|
645
|
+
// A collapsed expression's peek (issue 131): the whole of it, a part per
|
|
646
|
+
// line; picking a part goes to it.
|
|
647
|
+
peekHint: "Pick a part to go to it",
|
|
648
|
+
// Find (issue 131): what's in the query, to go to it.
|
|
649
|
+
/** `keys`: its shortcut, as the user's keyboard has it (⌘F, Ctrl+F). */
|
|
650
|
+
find: (e) => `Find in the query (${e})`,
|
|
651
|
+
findPlaceholder: "Find a column, value, function…",
|
|
652
|
+
findNothing: "Nothing in the query matches",
|
|
653
|
+
// The query's outline (issue 131), in place of the canvas's map: its
|
|
654
|
+
// sections in order, a part a line; picking one goes to it.
|
|
655
|
+
outline: "Outline",
|
|
656
|
+
outlineCollapse: "Fold the outline away",
|
|
657
|
+
outlineExpand: "Show the outline",
|
|
658
|
+
outlineAllColumns: "All columns",
|
|
659
|
+
outlineEmpty: "Nothing yet: pick a table to start.",
|
|
660
|
+
outlineLimit: (e) => `At most ${e} row${e === 1 ? "" : "s"}`,
|
|
661
|
+
outlineOffset: (e) => `Skip ${e} row${e === 1 ? "" : "s"}`
|
|
662
|
+
}, Me = (e) => "from" in e ? e.from : { table: e.table };
|
|
663
|
+
let Oe = 0;
|
|
664
|
+
function G() {
|
|
665
|
+
return Oe += 1, `join-${Date.now().toString(36)}-${Oe}`;
|
|
666
|
+
}
|
|
667
|
+
let Ue = 0;
|
|
668
|
+
function et() {
|
|
669
|
+
return Ue += 1, `select-${Date.now().toString(36)}-${Ue}`;
|
|
670
|
+
}
|
|
671
|
+
let Le = 0;
|
|
672
|
+
function De() {
|
|
673
|
+
return Le += 1, `group-${Date.now().toString(36)}-${Le}`;
|
|
674
|
+
}
|
|
675
|
+
let je = 0;
|
|
676
|
+
function nt() {
|
|
677
|
+
return je += 1, `order-${Date.now().toString(36)}-${je}`;
|
|
678
|
+
}
|
|
679
|
+
function ce(e, n, r) {
|
|
680
|
+
if (n < 0 || n >= e.length || r < 0 || r >= e.length || n === r) return e;
|
|
681
|
+
const t = [...e], [o] = t.splice(n, 1);
|
|
682
|
+
return t.splice(r, 0, o), t;
|
|
683
|
+
}
|
|
684
|
+
const qe = (e) => e !== void 0 && Number.isSafeInteger(e) && e >= 0 ? e : void 0, de = (e) => (e ?? "").length <= D;
|
|
685
|
+
function R(e, n, r) {
|
|
686
|
+
return e.map((t) => t.id === n ? r(t) : t);
|
|
687
|
+
}
|
|
688
|
+
function on(e, n, r) {
|
|
689
|
+
return e.map((t) => t.id === n ? r(t) : t);
|
|
690
|
+
}
|
|
691
|
+
function tt(e) {
|
|
692
|
+
return e.query ? { id: e.id, alias: e.alias, query: e.query } : e.shared !== void 0 ? { shared: e.shared, alias: e.alias } : { table: e.table, alias: e.alias };
|
|
693
|
+
}
|
|
694
|
+
function rt(e, n, r) {
|
|
695
|
+
return e.query ? {
|
|
696
|
+
id: e.id,
|
|
697
|
+
alias: e.alias,
|
|
698
|
+
query: e.query,
|
|
699
|
+
joinType: r,
|
|
700
|
+
on: n
|
|
701
|
+
} : e.shared !== void 0 ? {
|
|
702
|
+
id: G(),
|
|
703
|
+
shared: e.shared,
|
|
704
|
+
alias: e.alias,
|
|
705
|
+
joinType: r,
|
|
706
|
+
on: n
|
|
707
|
+
} : {
|
|
708
|
+
id: G(),
|
|
709
|
+
table: e.table,
|
|
710
|
+
alias: e.alias,
|
|
711
|
+
joinType: r,
|
|
712
|
+
on: n
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
function sn(e, n) {
|
|
716
|
+
const [r, ...t] = n;
|
|
717
|
+
return { ...e, from: tt(r), joins: t };
|
|
718
|
+
}
|
|
719
|
+
function I(e) {
|
|
720
|
+
const n = /* @__PURE__ */ new Set(), r = (t) => {
|
|
721
|
+
var o;
|
|
722
|
+
if (t)
|
|
723
|
+
switch (t.type) {
|
|
724
|
+
case "field":
|
|
725
|
+
t.table && n.add(t.table);
|
|
726
|
+
break;
|
|
727
|
+
case "binary":
|
|
728
|
+
r(t.left), r(t.right);
|
|
729
|
+
break;
|
|
730
|
+
case "logical":
|
|
731
|
+
t.operands.forEach(r);
|
|
732
|
+
break;
|
|
733
|
+
case "list":
|
|
734
|
+
t.items.forEach(r);
|
|
735
|
+
break;
|
|
736
|
+
case "not":
|
|
737
|
+
r(t.operand);
|
|
738
|
+
break;
|
|
739
|
+
case "if":
|
|
740
|
+
r(t.condition), r(t.then), r(t.else);
|
|
741
|
+
break;
|
|
742
|
+
case "case":
|
|
743
|
+
r(t.subject), t.branches.forEach((s) => {
|
|
744
|
+
r(s.when), r(s.then);
|
|
745
|
+
}), r(t.else);
|
|
746
|
+
break;
|
|
747
|
+
case "function":
|
|
748
|
+
t.args.forEach(r), (o = t.orderBy) == null || o.forEach((s) => r(s.expression));
|
|
749
|
+
break;
|
|
750
|
+
// Only the tables its link names on the outer query's side: its own
|
|
751
|
+
// tables are joined inside it (withExistsJoins), never to the query
|
|
752
|
+
// around it — that would repeat its rows, which EXISTS is there to avoid.
|
|
753
|
+
case "exists": {
|
|
754
|
+
const s = Je(t.query);
|
|
755
|
+
I(t.link).filter((i) => !s.has(i)).forEach((i) => n.add(i));
|
|
756
|
+
break;
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
};
|
|
760
|
+
return r(e), [...n];
|
|
761
|
+
}
|
|
762
|
+
function Ne(e, n, r, t, o) {
|
|
763
|
+
const s = _(n), i = t === void 0 && o === void 0, u = i ? Gn(e, s, r) : void 0, a = {
|
|
764
|
+
id: G(),
|
|
765
|
+
table: r,
|
|
766
|
+
joinType: t ?? (u == null ? void 0 : u.joinType) ?? "inner",
|
|
767
|
+
// Else matched to a derived table holding its key, if one does.
|
|
768
|
+
on: o ?? (u == null ? void 0 : u.on) ?? (i ? Yn(e, n, r) : void 0)
|
|
769
|
+
};
|
|
770
|
+
return { ...n, joins: [...n.joins, a] };
|
|
771
|
+
}
|
|
772
|
+
function ot(e, n, r) {
|
|
773
|
+
const t = _(n), o = Ve(e, t, r);
|
|
774
|
+
return (o && o.length > 0 ? o : [r]).reduce((i, u) => Ne(e, i, u), n);
|
|
775
|
+
}
|
|
776
|
+
function L(e, n, r) {
|
|
777
|
+
return I(r).reduce((t, o) => {
|
|
778
|
+
if (S(t).some((u) => u.alias === o)) return t;
|
|
779
|
+
const s = _(t);
|
|
780
|
+
return (Ve(e, s, o) ?? [o]).reduce((u, a) => Ne(e, u, a), t);
|
|
781
|
+
}, n);
|
|
782
|
+
}
|
|
783
|
+
function st(e, n, r, t) {
|
|
784
|
+
return {
|
|
785
|
+
...L(e, n, t),
|
|
786
|
+
select: on(n.select, r, (o) => ({ ...o, expression: t }))
|
|
787
|
+
};
|
|
788
|
+
}
|
|
789
|
+
function Ee(e, n, r) {
|
|
790
|
+
const t = S(e);
|
|
791
|
+
if (n < 0 || n >= t.length || r < 0 || r >= t.length || n === r) return e;
|
|
792
|
+
const o = [...t], [s] = o.splice(n, 1);
|
|
793
|
+
o.splice(r, 0, s);
|
|
794
|
+
const [i, u] = t;
|
|
795
|
+
let a, l;
|
|
796
|
+
if (n === 0 ? (l = i, a = u) : r === 0 && (a = t[n], l = i), a && l) {
|
|
797
|
+
const { on: c, joinType: f } = a;
|
|
798
|
+
o[o.indexOf(l)] = rt(l, c, f);
|
|
799
|
+
}
|
|
800
|
+
return sn(e, o);
|
|
801
|
+
}
|
|
802
|
+
function an(e) {
|
|
803
|
+
const n = /* @__PURE__ */ new Set([T(e.from)]);
|
|
804
|
+
let r;
|
|
805
|
+
return e.joins.some((t) => {
|
|
806
|
+
n.add(T(t));
|
|
807
|
+
const o = t.joinType === "cross" ? void 0 : t.on, s = I(o).find((i) => !n.has(i));
|
|
808
|
+
return s && (r = { table: t.table ?? T(t), needs: s }), s !== void 0;
|
|
809
|
+
}), r;
|
|
810
|
+
}
|
|
811
|
+
function Gt(e, n, r) {
|
|
812
|
+
const t = Ee(e, n, r);
|
|
813
|
+
return t === e ? void 0 : an(t);
|
|
814
|
+
}
|
|
815
|
+
function it(e, n, r) {
|
|
816
|
+
const t = Ee(e, n, r);
|
|
817
|
+
return t !== e && an(t) === void 0;
|
|
818
|
+
}
|
|
819
|
+
function at(e, n, r, t) {
|
|
820
|
+
return {
|
|
821
|
+
...L(e, n, t),
|
|
822
|
+
orderBy: (n.orderBy ?? []).map((o) => o.id === r ? { ...o, expression: t } : o)
|
|
823
|
+
};
|
|
824
|
+
}
|
|
825
|
+
function ut(e, n, r, t) {
|
|
826
|
+
return {
|
|
827
|
+
...L(e, n, t),
|
|
828
|
+
groupBy: (n.groupBy ?? []).map((o) => o.id === r ? { ...o, expression: t } : o)
|
|
829
|
+
};
|
|
830
|
+
}
|
|
831
|
+
function ie(e, n) {
|
|
832
|
+
if (!n || !qn(n)) return n;
|
|
833
|
+
const r = oe(n), t = r.map((a) => ie(e, a)), s = t.some((a, l) => a !== r[l]) ? P(n, (a) => t[r.indexOf(a)]) : n;
|
|
834
|
+
if (s.type !== "exists") return s;
|
|
835
|
+
const { query: i } = s, { joins: u } = L(
|
|
836
|
+
e,
|
|
837
|
+
{
|
|
838
|
+
id: "",
|
|
839
|
+
from: i.from,
|
|
840
|
+
joins: i.joins,
|
|
841
|
+
select: []
|
|
842
|
+
},
|
|
843
|
+
i.where
|
|
844
|
+
);
|
|
845
|
+
return u === i.joins ? s : { ...s, query: { ...i, joins: u } };
|
|
846
|
+
}
|
|
847
|
+
function Ft(e, n, r) {
|
|
848
|
+
const t = ie(e, r);
|
|
849
|
+
return { ...L(e, n, t), having: t };
|
|
850
|
+
}
|
|
851
|
+
function Re(e, n) {
|
|
852
|
+
const r = /* @__PURE__ */ new Set([
|
|
853
|
+
...n ? S(n).map(T) : [],
|
|
854
|
+
...q(e).map((o) => o.name)
|
|
855
|
+
]);
|
|
856
|
+
let t = 1;
|
|
857
|
+
for (; r.has(`sub_query_${t}`); ) t += 1;
|
|
858
|
+
return `sub_query_${t}`;
|
|
859
|
+
}
|
|
860
|
+
function Qt() {
|
|
861
|
+
return { id: E() };
|
|
862
|
+
}
|
|
863
|
+
function he(e, n, r) {
|
|
864
|
+
if (!U(n))
|
|
865
|
+
return r.type !== "setBaseTable" && r.type !== "setDerivedBaseTable" ? n : he(e, {
|
|
866
|
+
id: n.id,
|
|
867
|
+
...n.shared ? { shared: n.shared } : {},
|
|
868
|
+
from: { table: "" },
|
|
869
|
+
joins: [],
|
|
870
|
+
select: []
|
|
871
|
+
}, r);
|
|
872
|
+
switch (r.type) {
|
|
873
|
+
case "setBaseTable":
|
|
874
|
+
return { ...n, from: { table: r.table } };
|
|
875
|
+
case "setDerivedBaseTable":
|
|
876
|
+
return {
|
|
877
|
+
...n,
|
|
878
|
+
from: {
|
|
879
|
+
id: r.id ?? G(),
|
|
880
|
+
alias: Re(e, n),
|
|
881
|
+
query: { from: Me(r), joins: [], select: [] }
|
|
882
|
+
}
|
|
883
|
+
};
|
|
884
|
+
case "changeBaseAlias":
|
|
885
|
+
return de(r.alias) ? {
|
|
886
|
+
...n,
|
|
887
|
+
// A derived table (or a shared query's place) goes by its alias: it
|
|
888
|
+
// can't be left without one.
|
|
889
|
+
from: n.from.table === void 0 ? { ...n.from, alias: r.alias || n.from.alias } : { ...n.from, alias: r.alias }
|
|
890
|
+
} : n;
|
|
891
|
+
case "addJoin":
|
|
892
|
+
return r.joinType === void 0 && r.on === void 0 ? ot(e, n, r.table) : Ne(e, n, r.table, r.joinType, r.on);
|
|
893
|
+
case "addDerivedJoin": {
|
|
894
|
+
const t = {
|
|
895
|
+
id: r.id ?? G(),
|
|
896
|
+
alias: Re(e, n),
|
|
897
|
+
joinType: "left",
|
|
898
|
+
query: { from: Me(r), joins: [], select: [] }
|
|
899
|
+
}, o = rn(e, n, t);
|
|
900
|
+
return { ...n, joins: [...n.joins, { ...t, on: o }] };
|
|
901
|
+
}
|
|
902
|
+
case "renameDerivedTable": {
|
|
903
|
+
const t = O(n).find((a) => a.id === r.derivedId), o = r.alias.trim();
|
|
904
|
+
if (!t || o === t.alias) return n;
|
|
905
|
+
const s = S(n).filter((a) => a !== t).map(T);
|
|
906
|
+
if (!o || o.length > D || s.includes(o) || A(e, o)) return n;
|
|
907
|
+
const i = ke(n, t.alias, o), { from: u } = i;
|
|
908
|
+
return {
|
|
909
|
+
...i,
|
|
910
|
+
from: u.query && u.id === t.id ? { ...u, alias: o } : u,
|
|
911
|
+
joins: i.joins.map((a) => a.id === t.id ? { ...a, alias: o } : a)
|
|
912
|
+
};
|
|
913
|
+
}
|
|
914
|
+
case "renameSharedPlace": {
|
|
915
|
+
const t = r.joinId === void 0 ? n.from : n.joins.find((u) => u.id === r.joinId), o = r.alias.trim();
|
|
916
|
+
if ((t == null ? void 0 : t.shared) === void 0 || o === t.alias) return n;
|
|
917
|
+
const s = S(n).filter((u) => u !== t).map(T);
|
|
918
|
+
if (!o || o.length > D || s.includes(o) || A(e, o)) return n;
|
|
919
|
+
const i = ke(n, t.alias, o);
|
|
920
|
+
return r.joinId === void 0 ? { ...i, from: { ...i.from, alias: o } } : { ...i, joins: R(i.joins, r.joinId, (u) => ({ ...u, alias: o })) };
|
|
921
|
+
}
|
|
922
|
+
case "setMeasureSynced":
|
|
923
|
+
return {
|
|
924
|
+
...n,
|
|
925
|
+
joins: R(n.joins, r.joinId, (t) => t.query && t.drillAcross ? { ...t, drillAcross: { ...t.drillAcross, synced: r.synced } } : t)
|
|
926
|
+
};
|
|
927
|
+
case "removeJoin":
|
|
928
|
+
return { ...n, joins: n.joins.filter((t) => t.id !== r.joinId) };
|
|
929
|
+
case "changeJoinType":
|
|
930
|
+
return {
|
|
931
|
+
...n,
|
|
932
|
+
// A CROSS JOIN uses no ON, but keeps it: switched back, it's used again.
|
|
933
|
+
joins: R(n.joins, r.joinId, (t) => ({ ...t, joinType: r.joinType }))
|
|
934
|
+
};
|
|
935
|
+
case "changeJoinAlias":
|
|
936
|
+
return de(r.alias) ? {
|
|
937
|
+
...n,
|
|
938
|
+
// A derived table (or a shared query's place) goes by its alias: it
|
|
939
|
+
// can't be left without one.
|
|
940
|
+
joins: R(n.joins, r.joinId, (t) => t.table === void 0 ? { ...t, alias: r.alias || t.alias } : { ...t, alias: r.alias })
|
|
941
|
+
} : n;
|
|
942
|
+
case "changeJoinOn":
|
|
943
|
+
return {
|
|
944
|
+
...n,
|
|
945
|
+
joins: R(n.joins, r.joinId, (t) => ({
|
|
946
|
+
...t,
|
|
947
|
+
on: ie(e, r.on)
|
|
948
|
+
}))
|
|
949
|
+
};
|
|
950
|
+
case "reorderJoin": {
|
|
951
|
+
const t = n.joins.findIndex((i) => i.id === r.joinId);
|
|
952
|
+
if (t === -1) return n;
|
|
953
|
+
const o = r.direction === "up" ? t - 1 : t + 1;
|
|
954
|
+
if (o < 0 || o >= n.joins.length) return n;
|
|
955
|
+
const s = [...n.joins];
|
|
956
|
+
return [s[t], s[o]] = [s[o], s[t]], { ...n, joins: s };
|
|
957
|
+
}
|
|
958
|
+
case "reorderTables":
|
|
959
|
+
return it(n, r.fromIndex, r.toIndex) ? Ee(n, r.fromIndex, r.toIndex) : n;
|
|
960
|
+
case "removeBaseTable":
|
|
961
|
+
return n.joins.length > 0 ? sn(n, n.joins) : n.shared ? { id: n.id, shared: n.shared } : { id: n.id };
|
|
962
|
+
case "addSelectItem": {
|
|
963
|
+
const t = { id: et(), expression: { type: "field" } };
|
|
964
|
+
return { ...n, select: [...n.select, t] };
|
|
965
|
+
}
|
|
966
|
+
case "removeSelectItem":
|
|
967
|
+
return { ...n, select: n.select.filter((t) => t.id !== r.selectId) };
|
|
968
|
+
case "reorderSelectItems": {
|
|
969
|
+
const t = ce(n.select, r.fromIndex, r.toIndex);
|
|
970
|
+
return t === n.select ? n : { ...n, select: t };
|
|
971
|
+
}
|
|
972
|
+
case "setSelectAlias":
|
|
973
|
+
return de(r.alias) ? {
|
|
974
|
+
...n,
|
|
975
|
+
select: on(n.select, r.selectId, (t) => ({ ...t, alias: r.alias }))
|
|
976
|
+
} : n;
|
|
977
|
+
case "setSelectExpression":
|
|
978
|
+
return st(e, n, r.selectId, r.expression);
|
|
979
|
+
case "addGroupByItem": {
|
|
980
|
+
const t = { id: De(), expression: { type: "field" } };
|
|
981
|
+
return { ...n, groupBy: [...n.groupBy ?? [], t] };
|
|
982
|
+
}
|
|
983
|
+
case "removeGroupByItem": {
|
|
984
|
+
const t = (n.groupBy ?? []).filter((o) => o.id !== r.groupById);
|
|
985
|
+
return { ...n, groupBy: t.length > 0 ? t : void 0 };
|
|
986
|
+
}
|
|
987
|
+
case "reorderGroupByItems": {
|
|
988
|
+
const t = n.groupBy ?? [], o = ce(t, r.fromIndex, r.toIndex);
|
|
989
|
+
return o === t ? n : { ...n, groupBy: o };
|
|
990
|
+
}
|
|
991
|
+
case "setGroupByExpression":
|
|
992
|
+
return ut(e, n, r.groupById, r.expression);
|
|
993
|
+
case "groupBySelectItem": {
|
|
994
|
+
const t = n.select.find((s) => s.id === r.selectId);
|
|
995
|
+
if (!t) return n;
|
|
996
|
+
const o = { id: De(), expression: t.expression };
|
|
997
|
+
return { ...n, groupBy: [...n.groupBy ?? [], o] };
|
|
998
|
+
}
|
|
999
|
+
case "removeHaving":
|
|
1000
|
+
return { ...n, having: void 0 };
|
|
1001
|
+
case "setDistinct":
|
|
1002
|
+
return { ...n, distinct: r.distinct || void 0 };
|
|
1003
|
+
case "addOrderByItem": {
|
|
1004
|
+
const t = {
|
|
1005
|
+
id: nt(),
|
|
1006
|
+
expression: { type: "field" },
|
|
1007
|
+
direction: "asc"
|
|
1008
|
+
};
|
|
1009
|
+
return { ...n, orderBy: [...n.orderBy ?? [], t] };
|
|
1010
|
+
}
|
|
1011
|
+
case "removeOrderByItem": {
|
|
1012
|
+
const t = (n.orderBy ?? []).filter((o) => o.id !== r.orderById);
|
|
1013
|
+
return { ...n, orderBy: t.length > 0 ? t : void 0 };
|
|
1014
|
+
}
|
|
1015
|
+
case "reorderOrderByItems": {
|
|
1016
|
+
const t = n.orderBy ?? [], o = ce(t, r.fromIndex, r.toIndex);
|
|
1017
|
+
return o === t ? n : { ...n, orderBy: o };
|
|
1018
|
+
}
|
|
1019
|
+
case "setOrderByExpression":
|
|
1020
|
+
return at(e, n, r.orderById, r.expression);
|
|
1021
|
+
case "setOrderByDirection":
|
|
1022
|
+
return {
|
|
1023
|
+
...n,
|
|
1024
|
+
orderBy: (n.orderBy ?? []).map((t) => t.id === r.orderById ? { ...t, direction: r.direction } : t)
|
|
1025
|
+
};
|
|
1026
|
+
case "setLimit":
|
|
1027
|
+
return { ...n, limit: qe(r.limit) };
|
|
1028
|
+
case "setOffset":
|
|
1029
|
+
return { ...n, offset: qe(r.offset) };
|
|
1030
|
+
case "clearPaging":
|
|
1031
|
+
return { ...n, limit: void 0, offset: void 0 };
|
|
1032
|
+
/* v8 ignore start -- PipelineAction is a closed union; every real case is
|
|
1033
|
+
handled above. Exercised in tests via a bogus-cast, same convention as
|
|
1034
|
+
applyRootAction's own `default` in PipelineEditor.tsx. */
|
|
1035
|
+
default:
|
|
1036
|
+
return n;
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
const lt = {
|
|
1040
|
+
inner: "INNER",
|
|
1041
|
+
left: "LEFT",
|
|
1042
|
+
right: "RIGHT",
|
|
1043
|
+
full: "FULL",
|
|
1044
|
+
cross: "CROSS"
|
|
1045
|
+
};
|
|
1046
|
+
function Pt(e) {
|
|
1047
|
+
return `${lt[e.joinType]} JOIN ${T(e)}`;
|
|
1048
|
+
}
|
|
1049
|
+
function _t(e) {
|
|
1050
|
+
return e.alias || "Column";
|
|
1051
|
+
}
|
|
1052
|
+
function Jt() {
|
|
1053
|
+
return "Sort";
|
|
1054
|
+
}
|
|
1055
|
+
function Vt() {
|
|
1056
|
+
return "Group";
|
|
1057
|
+
}
|
|
1058
|
+
function Wt(e, n, r) {
|
|
1059
|
+
const t = S(n);
|
|
1060
|
+
if (r !== void 0) {
|
|
1061
|
+
const a = t.findIndex((l) => "id" in l && l.id === r);
|
|
1062
|
+
return new Set(t.slice(0, a + 1).map(T));
|
|
1063
|
+
}
|
|
1064
|
+
const o = new Set(_(n)), s = Rn(e, [...o]), i = new Set(tn(e, n).map((a) => a.table)), u = q(e).filter((a) => !o.has(a.name) && (s.has(a.name) || i.has(a.name)));
|
|
1065
|
+
return /* @__PURE__ */ new Set([...t.map(T), ...u.map((a) => a.name)]);
|
|
1066
|
+
}
|
|
1067
|
+
function Ht(e, n) {
|
|
1068
|
+
const r = S(n);
|
|
1069
|
+
return q(e).flatMap((t) => {
|
|
1070
|
+
const o = r.filter((s) => s.table === t.name).map(T);
|
|
1071
|
+
return (o.length > 0 ? o : [t.name]).map((s) => ({ ref: s, table: t }));
|
|
1072
|
+
});
|
|
1073
|
+
}
|
|
1074
|
+
function Xt(e, n) {
|
|
1075
|
+
const r = new Set(_(n));
|
|
1076
|
+
return q(e).filter((t) => !r.has(t.name));
|
|
1077
|
+
}
|
|
1078
|
+
function ye(e) {
|
|
1079
|
+
return e ? e.type === "logical" ? e.operands.reduce((n, r) => n + ye(r), 0) : e.type === "not" ? ye(e.operand) : 1 : 0;
|
|
1080
|
+
}
|
|
1081
|
+
function Ge(e) {
|
|
1082
|
+
return e.column ? e.table ? `${e.table}.${e.column}` : e.column : "?";
|
|
1083
|
+
}
|
|
1084
|
+
function Kt(e, n = Zn) {
|
|
1085
|
+
var r;
|
|
1086
|
+
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 : `${Ge(e.left)} ${e.operator ?? "="} ${Ge(e.right)}` : n.conditionCount(ye(e)) : n.setJoinCondition;
|
|
1087
|
+
}
|
|
1088
|
+
function Yt(e, n) {
|
|
1089
|
+
const r = new Set(I(e.where));
|
|
1090
|
+
return I(e.having).forEach((t) => r.add(t)), e.joins.filter((t) => t.id !== n).forEach((t) => I(t.on).forEach((o) => r.add(o))), [...e.select, ...e.groupBy ?? [], ...e.orderBy ?? []].forEach((t) => I(t.expression).forEach((o) => r.add(o))), r;
|
|
1091
|
+
}
|
|
1092
|
+
function zt(e, n, r) {
|
|
1093
|
+
const t = ie(e, r);
|
|
1094
|
+
return { ...L(e, n, t), where: t };
|
|
1095
|
+
}
|
|
1096
|
+
function Zt(e, n, r, t) {
|
|
1097
|
+
return r.length === 0 ? he(e, n, t) : U(n) ? Ze(n, r, (o) => he(e, o, t)) : n;
|
|
1098
|
+
}
|
|
1099
|
+
const un = (e) => e.query !== void 0 && !C(e.query);
|
|
1100
|
+
function ln(e) {
|
|
1101
|
+
const n = /* @__PURE__ */ new Map();
|
|
1102
|
+
return e.relations.forEach((r) => {
|
|
1103
|
+
n.set(r.fromTable, [...n.get(r.fromTable) ?? [], r.toTable]);
|
|
1104
|
+
}), n;
|
|
1105
|
+
}
|
|
1106
|
+
function J(e, n, r = ln(e)) {
|
|
1107
|
+
const t = /* @__PURE__ */ new Set([n]), o = [n];
|
|
1108
|
+
for (let s = o.shift(); s !== void 0; s = o.shift())
|
|
1109
|
+
(r.get(s) ?? []).forEach((i) => {
|
|
1110
|
+
t.has(i) || (t.add(i), o.push(i));
|
|
1111
|
+
});
|
|
1112
|
+
return t;
|
|
1113
|
+
}
|
|
1114
|
+
const j = (e) => e ? e.type === "logical" && e.operator === "AND" ? e.operands : [e] : [];
|
|
1115
|
+
function cn(e, n) {
|
|
1116
|
+
const r = new Map(S(e).map((t) => [T(t), t.table]));
|
|
1117
|
+
return I(n).map((t) => ({
|
|
1118
|
+
ref: t,
|
|
1119
|
+
table: r.has(t) ? r.get(t) : t
|
|
1120
|
+
}));
|
|
1121
|
+
}
|
|
1122
|
+
const Ce = (e, n) => cn(e, n).map((r) => r.table);
|
|
1123
|
+
function Fe(e, n) {
|
|
1124
|
+
const r = new Map(S(e).map((t) => [T(t), t.table]));
|
|
1125
|
+
return ve(n, (t) => {
|
|
1126
|
+
if (t.type !== "field" || t.table === void 0) return t;
|
|
1127
|
+
const o = r.get(t.table);
|
|
1128
|
+
return o === void 0 || o === t.table ? t : { ...t, table: o };
|
|
1129
|
+
});
|
|
1130
|
+
}
|
|
1131
|
+
function dn(e, n) {
|
|
1132
|
+
const r = (n.groupBy ?? []).flatMap((o) => cn(n, o.expression)), t = ln(e);
|
|
1133
|
+
return q(e).map((o) => {
|
|
1134
|
+
const s = J(e, o.name, t), i = r.find((u) => u.table === void 0 || !s.has(u.table));
|
|
1135
|
+
return i ? { table: o, unlinked: i.table ?? i.ref } : { table: o };
|
|
1136
|
+
});
|
|
1137
|
+
}
|
|
1138
|
+
function ct(e, n, r) {
|
|
1139
|
+
const t = J(e, r);
|
|
1140
|
+
return j(n.where).filter((o) => {
|
|
1141
|
+
const s = Ce(n, o);
|
|
1142
|
+
return s.length > 0 && s.every((i) => i !== void 0 && t.has(i));
|
|
1143
|
+
});
|
|
1144
|
+
}
|
|
1145
|
+
function er(e, n, r) {
|
|
1146
|
+
const t = J(e, r);
|
|
1147
|
+
return j(n.where).some((o) => {
|
|
1148
|
+
const s = Ce(n, o);
|
|
1149
|
+
return s.some((i) => i !== void 0 && t.has(i)) && s.some((i) => i === void 0 || !t.has(i));
|
|
1150
|
+
});
|
|
1151
|
+
}
|
|
1152
|
+
function X(e, n) {
|
|
1153
|
+
let r = e;
|
|
1154
|
+
for (let t = 2; n.has(r); t += 1) r = `${e}_${t}`;
|
|
1155
|
+
return r;
|
|
1156
|
+
}
|
|
1157
|
+
function be(e, n) {
|
|
1158
|
+
if (e.type === "field" && e.column) return e.column;
|
|
1159
|
+
if (e.type === "function") {
|
|
1160
|
+
const r = e.args[0];
|
|
1161
|
+
return (r == null ? void 0 : r.type) === "field" && r.column ? r.column : e.name.toLowerCase() || n;
|
|
1162
|
+
}
|
|
1163
|
+
return n;
|
|
1164
|
+
}
|
|
1165
|
+
const ge = (e, n) => ({ type: "field", table: e, column: n });
|
|
1166
|
+
function dt(e, n) {
|
|
1167
|
+
return {
|
|
1168
|
+
type: "logical",
|
|
1169
|
+
operator: "OR",
|
|
1170
|
+
operands: [
|
|
1171
|
+
{
|
|
1172
|
+
type: "binary",
|
|
1173
|
+
operator: "=",
|
|
1174
|
+
left: e,
|
|
1175
|
+
right: n
|
|
1176
|
+
},
|
|
1177
|
+
{
|
|
1178
|
+
type: "logical",
|
|
1179
|
+
operator: "AND",
|
|
1180
|
+
operands: [
|
|
1181
|
+
{ type: "binary", operator: "IS NULL", left: e },
|
|
1182
|
+
{ type: "binary", operator: "IS NULL", left: n }
|
|
1183
|
+
]
|
|
1184
|
+
}
|
|
1185
|
+
]
|
|
1186
|
+
};
|
|
1187
|
+
}
|
|
1188
|
+
function ft(e, n, r) {
|
|
1189
|
+
var s;
|
|
1190
|
+
if (r.type !== "field" || r.table === void 0 || r.column === void 0) return !1;
|
|
1191
|
+
const t = Ce(n, r)[0], o = (s = A(e, t)) == null ? void 0 : s.columns.find((i) => i.name === r.column);
|
|
1192
|
+
return o !== void 0 && !o.nullable;
|
|
1193
|
+
}
|
|
1194
|
+
function fn(e, n, r, t) {
|
|
1195
|
+
const o = n.groupBy.map((i) => i.expression), s = t.map((i, u) => ft(e, n, o[u]) ? {
|
|
1196
|
+
type: "binary",
|
|
1197
|
+
operator: "=",
|
|
1198
|
+
left: ge(r, i),
|
|
1199
|
+
right: o[u]
|
|
1200
|
+
} : dt(ge(r, i), o[u]));
|
|
1201
|
+
return s.length === 1 ? s[0] : { type: "logical", operator: "AND", operands: s };
|
|
1202
|
+
}
|
|
1203
|
+
function mn(e, n, r, t, o, s) {
|
|
1204
|
+
var b;
|
|
1205
|
+
const i = n.groupBy.map((h) => Fe(n, h.expression)), u = ct(e, n, r).map((h) => Fe(n, h)), a = new Set(o.map((h) => h.alias).filter((h) => !!h)), l = i.map((h, w) => {
|
|
1206
|
+
const v = X(be(h, `key_${w + 1}`), a);
|
|
1207
|
+
return a.add(v), v;
|
|
1208
|
+
}), c = [...u, ...t];
|
|
1209
|
+
let f = {
|
|
1210
|
+
id: "",
|
|
1211
|
+
from: { table: r },
|
|
1212
|
+
joins: [],
|
|
1213
|
+
select: []
|
|
1214
|
+
};
|
|
1215
|
+
[...i, ...c, ...o.map((h) => h.expression)].forEach((h) => {
|
|
1216
|
+
f = L(e, f, h);
|
|
1217
|
+
});
|
|
1218
|
+
const d = ((b = s == null ? void 0 : s.groupBy) == null ? void 0 : b.length) ?? 0, m = [...(s == null ? void 0 : s.joins) ?? []], g = f.joins.map((h) => {
|
|
1219
|
+
const w = m.findIndex(($) => $.table !== void 0 && $.table === h.table);
|
|
1220
|
+
if (w === -1) return h;
|
|
1221
|
+
const [v] = m.splice(w, 1);
|
|
1222
|
+
return { ...h, id: v.id };
|
|
1223
|
+
}), p = {
|
|
1224
|
+
...s,
|
|
1225
|
+
from: f.from,
|
|
1226
|
+
joins: g,
|
|
1227
|
+
where: c.length > 0 ? { type: "logical", operator: "AND", operands: c } : void 0,
|
|
1228
|
+
groupBy: i.map((h, w) => {
|
|
1229
|
+
var v, $;
|
|
1230
|
+
return {
|
|
1231
|
+
id: (($ = (v = s == null ? void 0 : s.groupBy) == null ? void 0 : v[w]) == null ? void 0 : $.id) ?? E(),
|
|
1232
|
+
expression: h
|
|
1233
|
+
};
|
|
1234
|
+
}),
|
|
1235
|
+
select: [
|
|
1236
|
+
...i.map((h, w) => {
|
|
1237
|
+
var v;
|
|
1238
|
+
return {
|
|
1239
|
+
id: w < d && ((v = s == null ? void 0 : s.select[w]) == null ? void 0 : v.id) || E(),
|
|
1240
|
+
alias: l[w],
|
|
1241
|
+
expression: h
|
|
1242
|
+
};
|
|
1243
|
+
}),
|
|
1244
|
+
...o
|
|
1245
|
+
]
|
|
1246
|
+
};
|
|
1247
|
+
return p.where === void 0 && delete p.where, { query: p, copied: u.length, keyNames: l };
|
|
1248
|
+
}
|
|
1249
|
+
function nr(e, n, r) {
|
|
1250
|
+
var g;
|
|
1251
|
+
if ((n.groupBy ?? []).length === 0)
|
|
1252
|
+
throw new Error("addMeasure: the query isn't grouped (a measure is per group)");
|
|
1253
|
+
const t = dn(e, n).find((p) => p.table.name === r.table);
|
|
1254
|
+
if (!t) throw new Error(`addMeasure: unknown table "${r.table}"`);
|
|
1255
|
+
if (t.unlinked !== void 0)
|
|
1256
|
+
throw new Error(`addMeasure: "${r.table}" doesn't share the query's grouping`);
|
|
1257
|
+
const o = j(r.where), s = n.joins.find((p) => {
|
|
1258
|
+
var b;
|
|
1259
|
+
return un(p) && ((b = p.drillAcross) == null ? void 0 : b.synced) === !0 && p.query.from.table === r.table && M(j(p.query.where).slice(p.drillAcross.copied), o);
|
|
1260
|
+
});
|
|
1261
|
+
let { joins: i } = n, u, a;
|
|
1262
|
+
if (s) {
|
|
1263
|
+
u = s.alias;
|
|
1264
|
+
const p = ((g = s.query.groupBy) == null ? void 0 : g.length) ?? 0, b = s.query.select.slice(p).find((h) => M(h.expression, r.value));
|
|
1265
|
+
if (b)
|
|
1266
|
+
a = b.alias;
|
|
1267
|
+
else {
|
|
1268
|
+
a = X(
|
|
1269
|
+
be(r.value, "value"),
|
|
1270
|
+
new Set(s.query.select.map((w) => w.alias ?? ""))
|
|
1271
|
+
);
|
|
1272
|
+
const h = { id: E(), alias: a, expression: r.value };
|
|
1273
|
+
i = i.map((w) => w === s ? { ...s, query: { ...s.query, select: [...s.query.select, h] } } : w);
|
|
1274
|
+
}
|
|
1275
|
+
} else {
|
|
1276
|
+
a = be(r.value, "value");
|
|
1277
|
+
const p = { id: E(), alias: a, expression: r.value }, b = mn(e, n, r.table, o, [p]), h = /* @__PURE__ */ new Set([
|
|
1278
|
+
...S(n).map(T),
|
|
1279
|
+
...q(e).map(($) => $.name)
|
|
1280
|
+
]), w = `${r.table}_per_${b.keyNames.join("_and_")}`.slice(0, D - 4);
|
|
1281
|
+
u = X(w, h);
|
|
1282
|
+
const v = {
|
|
1283
|
+
id: E(),
|
|
1284
|
+
alias: u,
|
|
1285
|
+
joinType: "left",
|
|
1286
|
+
query: b.query,
|
|
1287
|
+
on: fn(e, n, u, b.keyNames),
|
|
1288
|
+
drillAcross: { synced: !0, copied: b.copied }
|
|
1289
|
+
};
|
|
1290
|
+
i = [...i, v];
|
|
1291
|
+
}
|
|
1292
|
+
const l = {
|
|
1293
|
+
type: "function",
|
|
1294
|
+
name: "MAX",
|
|
1295
|
+
args: [ge(u, a)]
|
|
1296
|
+
}, c = new Set(n.select.map((p) => p.alias).filter((p) => !!p)), f = X(`${r.table}_${a}`.slice(0, D - 4), c);
|
|
1297
|
+
let d = r.selectId !== void 0 ? n.select.map((p) => p.id === r.selectId ? { ...p, expression: l, alias: p.alias || f } : p) : [...n.select, { id: E(), expression: l, alias: f }];
|
|
1298
|
+
return d = [...n.groupBy.filter((p) => !d.some((b) => M(b.expression, p.expression))).map((p) => ({ id: E(), expression: p.expression })), ...d], { ...n, joins: i, select: d };
|
|
1299
|
+
}
|
|
1300
|
+
function pn(e, n, r) {
|
|
1301
|
+
var c, f;
|
|
1302
|
+
if (!un(r)) return;
|
|
1303
|
+
const t = r, o = t.query.from.table, s = n.groupBy ?? [];
|
|
1304
|
+
if (o === void 0 || s.length === 0) return;
|
|
1305
|
+
const i = dn(e, n).find((d) => d.table.name === o);
|
|
1306
|
+
if (!i || i.unlinked !== void 0) return;
|
|
1307
|
+
const u = ((c = t.query.groupBy) == null ? void 0 : c.length) ?? 0, a = j(t.query.where).slice(((f = t.drillAcross) == null ? void 0 : f.copied) ?? 0), l = mn(e, n, o, a, t.query.select.slice(u), t.query);
|
|
1308
|
+
return {
|
|
1309
|
+
...t,
|
|
1310
|
+
query: l.query,
|
|
1311
|
+
on: fn(e, n, t.alias, l.keyNames),
|
|
1312
|
+
drillAcross: { synced: !0, copied: l.copied }
|
|
1313
|
+
};
|
|
1314
|
+
}
|
|
1315
|
+
function z(e) {
|
|
1316
|
+
var t, o;
|
|
1317
|
+
const n = re(e.query), r = ((t = n.groupBy) == null ? void 0 : t.length) ?? 0;
|
|
1318
|
+
return {
|
|
1319
|
+
keys: (n.groupBy ?? []).map((s) => s.expression),
|
|
1320
|
+
keyColumns: n.select.slice(0, r).map((s) => [s.alias, s.expression]),
|
|
1321
|
+
copied: j(n.where).slice(0, ((o = e.drillAcross) == null ? void 0 : o.copied) ?? 0),
|
|
1322
|
+
on: e.on
|
|
1323
|
+
};
|
|
1324
|
+
}
|
|
1325
|
+
function tr(e, n, r) {
|
|
1326
|
+
const t = pn(e, n, r);
|
|
1327
|
+
return t !== void 0 && M(z(t), z(r));
|
|
1328
|
+
}
|
|
1329
|
+
function hn(e, n, r) {
|
|
1330
|
+
let t = !1;
|
|
1331
|
+
const o = (u, a) => se(
|
|
1332
|
+
a,
|
|
1333
|
+
(l, c) => hn(
|
|
1334
|
+
e,
|
|
1335
|
+
u && C(u) === C(a) ? Y(u)[c] : void 0,
|
|
1336
|
+
l
|
|
1337
|
+
)
|
|
1338
|
+
), s = r.from.query ? (() => {
|
|
1339
|
+
const u = n != null && n.from.query && n.from.id === r.from.id ? n.from.query : void 0, a = o(u, r.from.query);
|
|
1340
|
+
return a === r.from.query ? r.from : { ...r.from, query: a };
|
|
1341
|
+
})() : r.from;
|
|
1342
|
+
s !== r.from && (t = !0);
|
|
1343
|
+
const i = r.joins.map((u) => {
|
|
1344
|
+
var f;
|
|
1345
|
+
if (!u.query) return u;
|
|
1346
|
+
const a = n == null ? void 0 : n.joins.find((d) => d.id === u.id && d.query !== void 0), l = o(a == null ? void 0 : a.query, u.query);
|
|
1347
|
+
let c = l === u.query ? u : { ...u, query: l };
|
|
1348
|
+
if ((f = c.drillAcross) != null && f.synced) {
|
|
1349
|
+
const d = pn(e, r, c);
|
|
1350
|
+
d && !M(d, c) && (c = d);
|
|
1351
|
+
} else if (c.drillAcross && (a != null && a.drillAcross) && !M(z(a), z(c))) {
|
|
1352
|
+
const d = { ...c };
|
|
1353
|
+
delete d.drillAcross, c = d;
|
|
1354
|
+
}
|
|
1355
|
+
return c !== u && (t = !0), c;
|
|
1356
|
+
});
|
|
1357
|
+
return t ? { ...r, from: s, joins: i } : r;
|
|
1358
|
+
}
|
|
1359
|
+
function mt(e, n, r) {
|
|
1360
|
+
return U(r) ? hn(e, n && U(n) ? n : void 0, r) : r;
|
|
1361
|
+
}
|
|
1362
|
+
const rr = ["text", "number", "date", "boolean"], pt = {
|
|
1363
|
+
varchar: "text",
|
|
1364
|
+
integer: "integer",
|
|
1365
|
+
decimal: "number",
|
|
1366
|
+
date: "date",
|
|
1367
|
+
dateTime: "dateTime",
|
|
1368
|
+
time: "time",
|
|
1369
|
+
boolean: "boolean"
|
|
1370
|
+
};
|
|
1371
|
+
function ht(e) {
|
|
1372
|
+
return pt[e];
|
|
1373
|
+
}
|
|
1374
|
+
function yt(e) {
|
|
1375
|
+
const n = e.trim().toUpperCase();
|
|
1376
|
+
if (/^((TINY|SMALL|MEDIUM|BIG)?INT|SIGNED|UNSIGNED)/.test(n)) return "integer";
|
|
1377
|
+
if (/^(DECIMAL|NUMERIC|FLOAT|DOUBLE|REAL|MONEY)/.test(n)) return "number";
|
|
1378
|
+
if (/^(DATETIME|TIMESTAMP|SMALLDATETIME)/.test(n)) return "dateTime";
|
|
1379
|
+
if (n.startsWith("DATE")) return "date";
|
|
1380
|
+
if (n.startsWith("TIME")) return "time";
|
|
1381
|
+
if (/^BOOL|^BIT$/.test(n)) return "boolean";
|
|
1382
|
+
if (/^(N?VAR)?CHAR|^N?TEXT|^STRING/.test(n)) return "text";
|
|
1383
|
+
}
|
|
1384
|
+
function yn(e) {
|
|
1385
|
+
if (e.as) return e.as;
|
|
1386
|
+
const { value: n } = e;
|
|
1387
|
+
if (typeof n == "string") return n === "" ? void 0 : "text";
|
|
1388
|
+
if (typeof n == "number") return Number.isInteger(n) ? "integer" : "number";
|
|
1389
|
+
if (typeof n == "bigint") return "integer";
|
|
1390
|
+
if (typeof n == "boolean") return "boolean";
|
|
1391
|
+
if (n instanceof Date) return He(n) ? "date" : "dateTime";
|
|
1392
|
+
}
|
|
1393
|
+
function bn(e, n, r) {
|
|
1394
|
+
if (!e) return;
|
|
1395
|
+
const t = (o) => bn(o, n, r);
|
|
1396
|
+
switch (e.type) {
|
|
1397
|
+
case "field":
|
|
1398
|
+
return n(e);
|
|
1399
|
+
case "literal":
|
|
1400
|
+
return yn(e);
|
|
1401
|
+
case "function": {
|
|
1402
|
+
const o = Fn(e.name, r), s = (o == null ? void 0 : o.args.findIndex((a) => a.kind === "type-name")) ?? -1, i = s >= 0 ? e.args[s] : void 0;
|
|
1403
|
+
if ((i == null ? void 0 : i.type) === "literal" && typeof i.value == "string")
|
|
1404
|
+
return yt(i.value);
|
|
1405
|
+
const u = o == null ? void 0 : o.returns;
|
|
1406
|
+
return u === "arg" ? t(e.args[0]) : u;
|
|
1407
|
+
}
|
|
1408
|
+
case "if":
|
|
1409
|
+
return t(e.then) ?? t(e.else);
|
|
1410
|
+
case "case":
|
|
1411
|
+
return [...e.branches.map((o) => o.then), e.else].map(t).find((o) => o !== void 0);
|
|
1412
|
+
default:
|
|
1413
|
+
return;
|
|
1414
|
+
}
|
|
1415
|
+
}
|
|
1416
|
+
const bt = /^[+-]?(\d+\.?\d*|\.\d+)(e[+-]?\d+)?$/i, gt = /^([01]\d|2[0-3]):([0-5]\d)(?::([0-5]\d))?$/, wt = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2}))?)?$/;
|
|
1417
|
+
function gn(e, n) {
|
|
1418
|
+
if (e === "" || e === null) return e;
|
|
1419
|
+
const r = typeof e == "string" ? e.trim() : void 0;
|
|
1420
|
+
switch (n) {
|
|
1421
|
+
case "text":
|
|
1422
|
+
return e instanceof Date ? _n(e) : String(e);
|
|
1423
|
+
case "integer":
|
|
1424
|
+
case "number": {
|
|
1425
|
+
let t;
|
|
1426
|
+
return typeof e == "number" ? t = e : typeof e == "bigint" ? t = Number(e) : r !== void 0 && bt.test(r) && (t = Number(r)), t === void 0 || !Wn(t) || n === "integer" && !Number.isInteger(t) ? void 0 : t;
|
|
1427
|
+
}
|
|
1428
|
+
case "date":
|
|
1429
|
+
case "dateTime": {
|
|
1430
|
+
if (e instanceof Date) return n === "date" && !He(e) ? void 0 : e;
|
|
1431
|
+
const t = r === void 0 ? null : wt.exec(r);
|
|
1432
|
+
if (!t) return;
|
|
1433
|
+
const [, o, s, i, u, a, l] = t.map(Number), c = Pn(o, s, i);
|
|
1434
|
+
return c.getUTCMonth() !== s - 1 || c.getUTCDate() !== i ? void 0 : t[4] === void 0 ? c : n === "date" ? void 0 : new Date(Date.UTC(o, s - 1, i, u, a, Number.isNaN(l) ? 0 : l));
|
|
1435
|
+
}
|
|
1436
|
+
case "time": {
|
|
1437
|
+
const t = r === void 0 ? null : gt.exec(r);
|
|
1438
|
+
return t ? `${t[1]}:${t[2]}:${t[3] ?? "00"}` : void 0;
|
|
1439
|
+
}
|
|
1440
|
+
case "boolean":
|
|
1441
|
+
return typeof e == "boolean" ? e : e === 1 || r === "1" || (r == null ? void 0 : r.toLowerCase()) === "true" ? !0 : e === 0 || r === "0" || (r == null ? void 0 : r.toLowerCase()) === "false" ? !1 : void 0;
|
|
1442
|
+
/* v8 ignore start -- ValueType is a closed union, every case above */
|
|
1443
|
+
default:
|
|
1444
|
+
return;
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
function St(e, n, r) {
|
|
1448
|
+
var c;
|
|
1449
|
+
if (n === void 0) return e;
|
|
1450
|
+
const { as: t, empty: o, ...s } = e;
|
|
1451
|
+
let i = gn(e.value, n);
|
|
1452
|
+
r != null && r.length && i !== "" && i !== null && (i = (c = r.find((f) => String(f.value) === String(e.value))) == null ? void 0 : c.value);
|
|
1453
|
+
const u = o && n === "text", a = i === void 0 ? { type: "literal", value: "" } : { ...s, value: i, ...u ? { empty: o } : {} };
|
|
1454
|
+
return t === void 0 && !!o == !!u && i === e.value ? e : a;
|
|
1455
|
+
}
|
|
1456
|
+
function or(e, n, r, t = !1) {
|
|
1457
|
+
const o = e.items.flatMap((i) => {
|
|
1458
|
+
if (i.type !== "literal") return [i];
|
|
1459
|
+
const u = St(i, n, r);
|
|
1460
|
+
return u.value === "" && !u.empty && !t ? [] : [u];
|
|
1461
|
+
});
|
|
1462
|
+
return o.length === e.items.length && o.every((i, u) => i === e.items[u]) ? e : { ...e, items: o };
|
|
1463
|
+
}
|
|
1464
|
+
function sr(e) {
|
|
1465
|
+
const n = yn(e);
|
|
1466
|
+
return n === "integer" || n === "number" ? "number" : n === "date" || n === "dateTime" ? "date" : n;
|
|
1467
|
+
}
|
|
1468
|
+
function ir(e, n) {
|
|
1469
|
+
const t = (e.type === "literal" ? gn(e.value, n) : void 0) ?? "", o = e.type === "literal" && e.empty && n === "text";
|
|
1470
|
+
return {
|
|
1471
|
+
type: "literal",
|
|
1472
|
+
value: t,
|
|
1473
|
+
as: n,
|
|
1474
|
+
...o ? { empty: o } : {}
|
|
1475
|
+
};
|
|
1476
|
+
}
|
|
1477
|
+
const fe = "IS NOT DISTINCT FROM";
|
|
1478
|
+
function ar(e, n = !1) {
|
|
1479
|
+
if (e !== void 0)
|
|
1480
|
+
return e === "text" ? [
|
|
1481
|
+
"=",
|
|
1482
|
+
"!=",
|
|
1483
|
+
fe,
|
|
1484
|
+
"LIKE",
|
|
1485
|
+
"REGEXP",
|
|
1486
|
+
"IN",
|
|
1487
|
+
...n ? ["BETWEEN"] : [],
|
|
1488
|
+
"IS NULL",
|
|
1489
|
+
"IS NOT NULL"
|
|
1490
|
+
] : e === "boolean" ? ["=", "!=", fe, "IS NULL", "IS NOT NULL"] : ["=", "!=", fe, "<", "<=", ">", ">=", "IN", "BETWEEN", "IS NULL", "IS NOT NULL"];
|
|
1491
|
+
}
|
|
1492
|
+
const wn = "source", Sn = "rows_", B = (e) => e ? { type: "literal", value: null, as: e } : { type: "literal", value: null }, F = (e) => e.type === "literal" && e.value === null, $e = (e) => new Map(
|
|
1493
|
+
S(e).map((n) => [T(n), n.table])
|
|
1494
|
+
);
|
|
1495
|
+
function we(e, n) {
|
|
1496
|
+
const r = $e(e);
|
|
1497
|
+
return I(n).map((t) => r.get(t) ?? t);
|
|
1498
|
+
}
|
|
1499
|
+
function Tn(e, n) {
|
|
1500
|
+
const r = $e(e);
|
|
1501
|
+
return ve(n, (t) => {
|
|
1502
|
+
if (t.type !== "field" || t.table === void 0) return t;
|
|
1503
|
+
const o = r.get(t.table);
|
|
1504
|
+
return o === void 0 || o === t.table ? t : { ...t, table: o };
|
|
1505
|
+
});
|
|
1506
|
+
}
|
|
1507
|
+
function Z(e, n, r) {
|
|
1508
|
+
const t = $e(n);
|
|
1509
|
+
return bn(r, (o) => {
|
|
1510
|
+
var u;
|
|
1511
|
+
const s = t.get(o.table ?? "") ?? o.table ?? "", i = (u = A(e, s)) == null ? void 0 : u.columns.find((a) => a.name === o.column);
|
|
1512
|
+
return i && ht(i.type);
|
|
1513
|
+
}, Q);
|
|
1514
|
+
}
|
|
1515
|
+
function xe(e, n) {
|
|
1516
|
+
let r = e;
|
|
1517
|
+
for (let t = 2; n.has(r); t += 1) r = `${e}_${t}`;
|
|
1518
|
+
return r;
|
|
1519
|
+
}
|
|
1520
|
+
const ee = (e) => e ? e.type === "logical" && e.operator === "AND" ? e.operands : [e] : [], vn = (e) => {
|
|
1521
|
+
if (e.length !== 0)
|
|
1522
|
+
return e.length === 1 ? e[0] : { type: "logical", operator: "AND", operands: e };
|
|
1523
|
+
}, k = (e) => e.from.table, Qe = (e, n) => A(e, n).label;
|
|
1524
|
+
function ae(e) {
|
|
1525
|
+
const { from: n } = e;
|
|
1526
|
+
return n.query && C(n.query) ? n : void 0;
|
|
1527
|
+
}
|
|
1528
|
+
function V(e, n) {
|
|
1529
|
+
if (n === void 0) return ae(e);
|
|
1530
|
+
const r = O(e).find((t) => t.id === n);
|
|
1531
|
+
return r && C(r.query) ? r : void 0;
|
|
1532
|
+
}
|
|
1533
|
+
function W(e, n, r) {
|
|
1534
|
+
const t = { ...n, query: r };
|
|
1535
|
+
return n === e.from ? { ...e, from: t } : { ...e, joins: e.joins.map((o) => o === n ? t : o) };
|
|
1536
|
+
}
|
|
1537
|
+
function ue(e, n) {
|
|
1538
|
+
const [r] = n;
|
|
1539
|
+
return r.select.map((t, o) => {
|
|
1540
|
+
var f;
|
|
1541
|
+
const s = x(t) ?? "", i = n.map((d) => {
|
|
1542
|
+
var m;
|
|
1543
|
+
return { branch: d, expression: (m = d.select[o]) == null ? void 0 : m.expression };
|
|
1544
|
+
}).filter((d) => d.expression !== void 0 && !F(d.expression)), a = ((f = n.map((d) => {
|
|
1545
|
+
var m;
|
|
1546
|
+
return (m = d.select[o]) == null ? void 0 : m.expression;
|
|
1547
|
+
}).filter(
|
|
1548
|
+
(d) => (d == null ? void 0 : d.type) === "literal" && d.value === null
|
|
1549
|
+
).find((d) => d.as)) == null ? void 0 : f.as) ?? (i[0] && Z(e, i[0].branch, i[0].expression));
|
|
1550
|
+
if (s === wn && i.length === n.length && i.every((d) => d.expression.type === "literal"))
|
|
1551
|
+
return {
|
|
1552
|
+
name: s,
|
|
1553
|
+
kind: "source",
|
|
1554
|
+
tables: [],
|
|
1555
|
+
type: "text"
|
|
1556
|
+
};
|
|
1557
|
+
if (s.startsWith(Sn) && i.length === 1 && i[0].expression.type === "literal")
|
|
1558
|
+
return {
|
|
1559
|
+
name: s,
|
|
1560
|
+
kind: "rows",
|
|
1561
|
+
tables: [],
|
|
1562
|
+
type: "integer"
|
|
1563
|
+
};
|
|
1564
|
+
const l = i[0];
|
|
1565
|
+
if (!l)
|
|
1566
|
+
return {
|
|
1567
|
+
name: s,
|
|
1568
|
+
kind: "shared",
|
|
1569
|
+
tables: [],
|
|
1570
|
+
type: a
|
|
1571
|
+
};
|
|
1572
|
+
const c = we(l.branch, l.expression);
|
|
1573
|
+
return i.some((d) => we(d.branch, d.expression).includes(k(d.branch))) ? {
|
|
1574
|
+
name: s,
|
|
1575
|
+
kind: "own",
|
|
1576
|
+
tables: c,
|
|
1577
|
+
type: a
|
|
1578
|
+
} : {
|
|
1579
|
+
name: s,
|
|
1580
|
+
kind: "shared",
|
|
1581
|
+
tables: c,
|
|
1582
|
+
type: a,
|
|
1583
|
+
expression: Tn(l.branch, l.expression)
|
|
1584
|
+
};
|
|
1585
|
+
});
|
|
1586
|
+
}
|
|
1587
|
+
function ur(e, n) {
|
|
1588
|
+
const r = ae(n);
|
|
1589
|
+
if (!r) return [];
|
|
1590
|
+
const { union: t } = r.query;
|
|
1591
|
+
return ue(e, t).map((o, s) => {
|
|
1592
|
+
const i = t.map((u) => !F(u.select[s].expression));
|
|
1593
|
+
return {
|
|
1594
|
+
name: o.name,
|
|
1595
|
+
kind: o.kind,
|
|
1596
|
+
filledBy: t.filter((u, a) => i[a]).map(k),
|
|
1597
|
+
nullIn: t.filter((u, a) => !i[a]).map(k)
|
|
1598
|
+
};
|
|
1599
|
+
});
|
|
1600
|
+
}
|
|
1601
|
+
function Nn(e, n, r, t, o) {
|
|
1602
|
+
const s = J(e, n), i = r.filter((c) => c.kind === "shared" && c.expression), u = (c) => c.tables.every((f) => s.has(f));
|
|
1603
|
+
if (!i.some(u)) return `"${n}" shares no column with the union`;
|
|
1604
|
+
const a = i.find((c) => !u(c));
|
|
1605
|
+
if (a && o === "refuse")
|
|
1606
|
+
return `"${n}" can't reach column "${a.name}" (${a.tables.join(", ")})`;
|
|
1607
|
+
const l = t.find((c) => !c.tables.every((f) => s.has(f)));
|
|
1608
|
+
if (l)
|
|
1609
|
+
return `"${n}" can't follow a condition on ${l.tables.join(", ")}`;
|
|
1610
|
+
}
|
|
1611
|
+
function En(e) {
|
|
1612
|
+
const [n] = e, r = new Set(e.map(k));
|
|
1613
|
+
return ee(n.where).flatMap((t) => {
|
|
1614
|
+
const o = we(n, t), s = o.filter((i) => r.has(i));
|
|
1615
|
+
if (s.length === 0) return [{ operand: Tn(n, t), tables: o }];
|
|
1616
|
+
if (s.length === o.length) return [];
|
|
1617
|
+
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");
|
|
1618
|
+
});
|
|
1619
|
+
}
|
|
1620
|
+
function Pe(e, n, r, t) {
|
|
1621
|
+
const o = t.unshared ?? "refuse", s = ue(e, n), i = En(n);
|
|
1622
|
+
if (!A(e, r.table))
|
|
1623
|
+
throw new Error(`addUnionTable: unknown table "${r.table}"`);
|
|
1624
|
+
if (n.some((y) => k(y) === r.table))
|
|
1625
|
+
throw new Error(`addUnionTable: "${r.table}" is in the union already`);
|
|
1626
|
+
const u = Nn(e, r.table, s, i, o);
|
|
1627
|
+
if (u) throw new Error(`addUnionTable: ${u}`);
|
|
1628
|
+
const a = J(e, r.table), l = r.label ?? Qe(e, r.table), c = s.map((y) => y.kind === "source" ? { type: "literal", value: l } : y.kind === "shared" && y.expression && y.tables.every((N) => a.has(N)) ? y.expression : B(y.type)), f = new Set(s.map((y) => y.name)), d = [], m = (y) => {
|
|
1629
|
+
const N = xe(y, f);
|
|
1630
|
+
return f.add(N), d.push(N), N;
|
|
1631
|
+
}, g = [], p = {
|
|
1632
|
+
from: { table: r.table },
|
|
1633
|
+
joins: []
|
|
1634
|
+
};
|
|
1635
|
+
r.columns.forEach((y) => {
|
|
1636
|
+
const N = y.alias ?? (y.expression.type === "field" ? y.expression.column : void 0) ?? "value", Dn = Z(e, p, y.expression);
|
|
1637
|
+
g.push({ name: m(N), own: y.expression, others: () => B(Dn) });
|
|
1638
|
+
}), t.source && !s.some((y) => y.kind === "source") && g.push({
|
|
1639
|
+
name: m(wn),
|
|
1640
|
+
own: { type: "literal", value: l },
|
|
1641
|
+
others: (y) => ({ type: "literal", value: Qe(e, k(y)) })
|
|
1642
|
+
});
|
|
1643
|
+
const b = (y) => `${Sn}${y}`;
|
|
1644
|
+
(t.rowMarkers || s.some((y) => y.kind === "rows")) && (n.filter((y) => !f.has(b(k(y)))).forEach((y) => {
|
|
1645
|
+
g.push({
|
|
1646
|
+
name: m(b(k(y))),
|
|
1647
|
+
own: B("integer"),
|
|
1648
|
+
others: (N) => N === y ? { type: "literal", value: 1 } : B("integer")
|
|
1649
|
+
});
|
|
1650
|
+
}), g.push({
|
|
1651
|
+
name: m(b(r.table)),
|
|
1652
|
+
own: { type: "literal", value: 1 },
|
|
1653
|
+
others: () => B("integer")
|
|
1654
|
+
}));
|
|
1655
|
+
const h = (y, N) => ({
|
|
1656
|
+
id: E(),
|
|
1657
|
+
alias: y,
|
|
1658
|
+
expression: N
|
|
1659
|
+
}), w = n.map((y) => ({
|
|
1660
|
+
...y,
|
|
1661
|
+
select: [...y.select, ...g.map((N) => h(N.name, N.others(y)))]
|
|
1662
|
+
})), v = vn([...i.map((y) => y.operand), ...ee(r.where)]);
|
|
1663
|
+
let $ = {
|
|
1664
|
+
id: "",
|
|
1665
|
+
from: { table: r.table },
|
|
1666
|
+
joins: [],
|
|
1667
|
+
...v ? { where: v } : {},
|
|
1668
|
+
select: [
|
|
1669
|
+
...s.map((y, N) => h(y.name, c[N])),
|
|
1670
|
+
...g.map((y) => h(y.name, y.own))
|
|
1671
|
+
]
|
|
1672
|
+
};
|
|
1673
|
+
[v, ...$.select.map((y) => y.expression)].forEach((y) => {
|
|
1674
|
+
$ = L(e, $, y);
|
|
1675
|
+
});
|
|
1676
|
+
const {
|
|
1677
|
+
from: Mn,
|
|
1678
|
+
joins: On,
|
|
1679
|
+
select: Un,
|
|
1680
|
+
where: Be
|
|
1681
|
+
} = $, Ln = {
|
|
1682
|
+
from: Mn,
|
|
1683
|
+
joins: On,
|
|
1684
|
+
select: Un,
|
|
1685
|
+
...Be ? { where: Be } : {}
|
|
1686
|
+
};
|
|
1687
|
+
return { union: [...w, Ln], added: d };
|
|
1688
|
+
}
|
|
1689
|
+
function Cn(e) {
|
|
1690
|
+
const n = (o) => new Error(`addUnionTable: ${o}`);
|
|
1691
|
+
if (e.from.query) throw n("the query reads from a derived table");
|
|
1692
|
+
if (e.select.length === 0) throw n("the query picks no columns (SELECT *)");
|
|
1693
|
+
if ((e.groupBy ?? []).length > 0 || e.having || e.distinct || (e.orderBy ?? []).length > 0 || e.select.some((o) => ne(o.expression, Q)))
|
|
1694
|
+
throw n("a union is of detail rows: no aggregates, GROUP BY, HAVING, DISTINCT or ORDER BY");
|
|
1695
|
+
const r = /* @__PURE__ */ new Set(), t = e.select.map((o, s) => {
|
|
1696
|
+
const i = xe(x(o) ?? `column_${s + 1}`, r);
|
|
1697
|
+
return r.add(i), { ...o, alias: i };
|
|
1698
|
+
});
|
|
1699
|
+
return {
|
|
1700
|
+
from: e.from,
|
|
1701
|
+
joins: e.joins,
|
|
1702
|
+
...e.where ? { where: e.where } : {},
|
|
1703
|
+
select: t
|
|
1704
|
+
};
|
|
1705
|
+
}
|
|
1706
|
+
function lr(e, n, r, t = {}) {
|
|
1707
|
+
const o = ae(n);
|
|
1708
|
+
if (o) {
|
|
1709
|
+
const { union: l, added: c } = Pe(e, o.query.union, r, t), f = { ...o, query: { ...o.query, union: l } }, d = n.select.length === 0 ? [] : [
|
|
1710
|
+
...n.select,
|
|
1711
|
+
...c.map((m) => ({
|
|
1712
|
+
id: E(),
|
|
1713
|
+
expression: { type: "field", table: o.alias, column: m }
|
|
1714
|
+
}))
|
|
1715
|
+
];
|
|
1716
|
+
return { ...n, from: f, select: d };
|
|
1717
|
+
}
|
|
1718
|
+
const s = Cn(n), { union: i } = Pe(e, [s], r, t), u = t.alias ?? "stacked", a = i[0].select.map((l) => l.alias);
|
|
1719
|
+
return {
|
|
1720
|
+
id: n.id,
|
|
1721
|
+
from: { id: E(), alias: u, query: { union: i, all: !0 } },
|
|
1722
|
+
joins: [],
|
|
1723
|
+
select: a.map((l) => ({
|
|
1724
|
+
id: E(),
|
|
1725
|
+
expression: { type: "field", table: u, column: l }
|
|
1726
|
+
})),
|
|
1727
|
+
...n.limit !== void 0 ? { limit: n.limit } : {},
|
|
1728
|
+
...n.offset !== void 0 ? { offset: n.offset } : {}
|
|
1729
|
+
};
|
|
1730
|
+
}
|
|
1731
|
+
function cr(e, n, r = {}) {
|
|
1732
|
+
const t = ae(n);
|
|
1733
|
+
let o;
|
|
1734
|
+
try {
|
|
1735
|
+
o = t ? t.query.union : [Cn(n)];
|
|
1736
|
+
} catch {
|
|
1737
|
+
return [];
|
|
1738
|
+
}
|
|
1739
|
+
const s = ue(e, o);
|
|
1740
|
+
let i;
|
|
1741
|
+
try {
|
|
1742
|
+
i = En(o);
|
|
1743
|
+
} catch {
|
|
1744
|
+
return [];
|
|
1745
|
+
}
|
|
1746
|
+
const u = /* @__PURE__ */ new Set([
|
|
1747
|
+
...o.map(k),
|
|
1748
|
+
...s.flatMap((a) => a.kind === "shared" ? a.tables : [])
|
|
1749
|
+
]);
|
|
1750
|
+
return e.tables.map((a) => a.name).filter((a) => !u.has(a) && Nn(e, a, s, i, r.unshared ?? "refuse") === void 0);
|
|
1751
|
+
}
|
|
1752
|
+
function Tt(e, n, r) {
|
|
1753
|
+
const t = r.union.findIndex((a, l) => a.select !== n.union[l].select);
|
|
1754
|
+
if (t === -1) return r;
|
|
1755
|
+
const o = r.union[t], s = n.union[t].select.map((a) => a.id), i = r.union.map((a, l) => {
|
|
1756
|
+
if (l === t) return a;
|
|
1757
|
+
const c = o.select.map((d) => {
|
|
1758
|
+
const m = x(d), g = Z(e, o, d.expression), p = s.indexOf(d.id), b = p === -1 ? void 0 : a.select[p];
|
|
1759
|
+
if (!b)
|
|
1760
|
+
return { id: E(), ...m ? { alias: m } : {}, expression: B(g) };
|
|
1761
|
+
let h = b;
|
|
1762
|
+
x(b) !== m && (h = { ...h, alias: m });
|
|
1763
|
+
const { expression: w } = b;
|
|
1764
|
+
return F(w) && w.type === "literal" && w.as !== g && (h = { ...h, expression: B(g) }), h;
|
|
1765
|
+
});
|
|
1766
|
+
return c.length === a.select.length && c.every((d, m) => d === a.select[m]) ? a : { ...a, select: c };
|
|
1767
|
+
}), u = i.map((a) => {
|
|
1768
|
+
const l = a.select.map((c, f) => {
|
|
1769
|
+
const { expression: d } = c;
|
|
1770
|
+
if (d.type !== "literal" || d.value !== null) return c;
|
|
1771
|
+
const m = i.find((p) => {
|
|
1772
|
+
var h;
|
|
1773
|
+
const b = (h = p.select[f]) == null ? void 0 : h.expression;
|
|
1774
|
+
return b !== void 0 && !F(b);
|
|
1775
|
+
}), g = m && Z(e, m, m.select[f].expression);
|
|
1776
|
+
return g && d.as !== g ? { ...c, expression: B(g) } : c;
|
|
1777
|
+
});
|
|
1778
|
+
return l.every((c, f) => c === a.select[f]) ? a : { ...a, select: l };
|
|
1779
|
+
});
|
|
1780
|
+
return u.every((a, l) => a === r.union[l]) ? r : { ...r, union: u };
|
|
1781
|
+
}
|
|
1782
|
+
function vt(e, n) {
|
|
1783
|
+
const r = (s) => s.union[0].select.map((i) => x(i)), t = r(e), o = r(n);
|
|
1784
|
+
return t.length !== o.length ? [] : t.flatMap((s, i) => {
|
|
1785
|
+
const u = o[i];
|
|
1786
|
+
return s && u && s !== u && !o.includes(s) && !t.includes(u) ? [{ from: s, to: u }] : [];
|
|
1787
|
+
});
|
|
1788
|
+
}
|
|
1789
|
+
function $n(e, n, r) {
|
|
1790
|
+
let t = r;
|
|
1791
|
+
n && C(n) && C(t) && n.union.length === t.union.length && (t = Tt(e, n, t));
|
|
1792
|
+
const o = t;
|
|
1793
|
+
return se(o, (s, i) => Ae(
|
|
1794
|
+
e,
|
|
1795
|
+
n && C(n) === C(o) ? Y(n)[i] : void 0,
|
|
1796
|
+
s
|
|
1797
|
+
));
|
|
1798
|
+
}
|
|
1799
|
+
function Ae(e, n, r) {
|
|
1800
|
+
const t = (d) => {
|
|
1801
|
+
var m;
|
|
1802
|
+
return $n(
|
|
1803
|
+
e,
|
|
1804
|
+
n && ((m = O(n).find((g) => g.id === d.id)) == null ? void 0 : m.query),
|
|
1805
|
+
d.query
|
|
1806
|
+
);
|
|
1807
|
+
}, { from: o } = r, s = o.query && t(o), i = s && s !== o.query ? { ...o, query: s } : o;
|
|
1808
|
+
let u = !1;
|
|
1809
|
+
const a = r.joins.map((d) => {
|
|
1810
|
+
if (!d.query) return d;
|
|
1811
|
+
const m = t(d);
|
|
1812
|
+
return m === d.query ? d : (u = !0, { ...d, query: m });
|
|
1813
|
+
});
|
|
1814
|
+
if (i === o && !u) return r;
|
|
1815
|
+
const l = { ...r, from: i, joins: a }, c = O(l).flatMap((d) => {
|
|
1816
|
+
var g;
|
|
1817
|
+
const m = n && ((g = O(n).find((p) => p.id === d.id)) == null ? void 0 : g.query);
|
|
1818
|
+
return m && C(m) && C(d.query) ? vt(m, d.query).map((p) => ({ ...p, alias: d.alias })) : [];
|
|
1819
|
+
});
|
|
1820
|
+
if (c.length === 0) return l;
|
|
1821
|
+
const f = (d) => {
|
|
1822
|
+
if (d.type !== "field") return P(d, f);
|
|
1823
|
+
const m = c.find((g) => g.alias === d.table && g.from === d.column);
|
|
1824
|
+
return m ? { ...d, column: m.to } : d;
|
|
1825
|
+
};
|
|
1826
|
+
return te(l, f, (d) => d);
|
|
1827
|
+
}
|
|
1828
|
+
function Nt(e, n, r) {
|
|
1829
|
+
if (!U(r)) return r;
|
|
1830
|
+
const t = n && U(n) ? n : void 0;
|
|
1831
|
+
return Te(Ae(e, t, r), (o, s) => {
|
|
1832
|
+
var i, u;
|
|
1833
|
+
return $n(
|
|
1834
|
+
e,
|
|
1835
|
+
(u = (i = t == null ? void 0 : t.shared) == null ? void 0 : i.find((a) => a.id === s.id)) == null ? void 0 : u.query,
|
|
1836
|
+
o
|
|
1837
|
+
);
|
|
1838
|
+
});
|
|
1839
|
+
}
|
|
1840
|
+
function xn(e) {
|
|
1841
|
+
return e.type === "field" ? [e] : oe(e).flatMap(xn);
|
|
1842
|
+
}
|
|
1843
|
+
const An = (e, n, r) => xn(e).some((t) => t.table === n && r.has(t.column ?? ""));
|
|
1844
|
+
function Et(e, n, r) {
|
|
1845
|
+
const t = n.query.union.filter((a, l) => l !== r), o = [
|
|
1846
|
+
e.where,
|
|
1847
|
+
e.having,
|
|
1848
|
+
...e.joins.map((a) => a.on),
|
|
1849
|
+
...(e.groupBy ?? []).map((a) => a.expression),
|
|
1850
|
+
...(e.orderBy ?? []).map((a) => a.expression)
|
|
1851
|
+
].filter((a) => a !== void 0), s = (a) => o.some(
|
|
1852
|
+
(l) => An(l, n.alias, /* @__PURE__ */ new Set([a]))
|
|
1853
|
+
), i = new Set(t[0].select.flatMap((a, l) => {
|
|
1854
|
+
const c = t.every((d) => d.select[l] && F(d.select[l].expression)), f = x(a);
|
|
1855
|
+
return c && !(f && s(f)) ? [l] : [];
|
|
1856
|
+
})), u = new Set([...i].flatMap((a) => {
|
|
1857
|
+
const l = x(t[0].select[a]);
|
|
1858
|
+
return l ? [l] : [];
|
|
1859
|
+
}));
|
|
1860
|
+
return { at: i, names: u };
|
|
1861
|
+
}
|
|
1862
|
+
function dr(e, n, r) {
|
|
1863
|
+
const t = V(e, r);
|
|
1864
|
+
if (!t || !t.query.union[n] || t.query.union.length < 2) return e;
|
|
1865
|
+
const o = Et(e, t, n), s = t.query.union.filter((l, c) => c !== n), i = o.at.size < s[0].select.length ? o : { at: /* @__PURE__ */ new Set(), names: /* @__PURE__ */ new Set() }, u = e.select.filter(
|
|
1866
|
+
(l) => !An(l.expression, t.alias, i.names)
|
|
1867
|
+
), a = s.map((l) => ({ ...l, select: l.select.filter((c, f) => !i.at.has(f)) }));
|
|
1868
|
+
return W({ ...e, select: u }, t, { ...t.query, union: a });
|
|
1869
|
+
}
|
|
1870
|
+
function Ct(e, n) {
|
|
1871
|
+
const r = /* @__PURE__ */ new Set([
|
|
1872
|
+
...n ? S(n).map(T) : [],
|
|
1873
|
+
...e.tables.map((o) => o.name)
|
|
1874
|
+
]);
|
|
1875
|
+
let t = 1;
|
|
1876
|
+
for (; r.has(`union_${t}`); ) t += 1;
|
|
1877
|
+
return `union_${t}`;
|
|
1878
|
+
}
|
|
1879
|
+
function fr(e, n, r, t) {
|
|
1880
|
+
return {
|
|
1881
|
+
id: e,
|
|
1882
|
+
from: { id: n, alias: r, query: { union: [{ from: t, joins: [], select: [] }], all: !0 } },
|
|
1883
|
+
joins: [],
|
|
1884
|
+
select: []
|
|
1885
|
+
};
|
|
1886
|
+
}
|
|
1887
|
+
function mr(e, n, r) {
|
|
1888
|
+
const t = /* @__PURE__ */ new Set(), o = e.select.map((p, b) => {
|
|
1889
|
+
const h = xe(x(p) ?? `column_${b + 1}`, t);
|
|
1890
|
+
return t.add(h), { ...p, alias: h };
|
|
1891
|
+
}), {
|
|
1892
|
+
from: s,
|
|
1893
|
+
joins: i,
|
|
1894
|
+
where: u,
|
|
1895
|
+
groupBy: a,
|
|
1896
|
+
having: l,
|
|
1897
|
+
distinct: c
|
|
1898
|
+
} = e, f = {
|
|
1899
|
+
from: s,
|
|
1900
|
+
joins: i,
|
|
1901
|
+
select: o,
|
|
1902
|
+
...u ? { where: u } : {},
|
|
1903
|
+
...a ? { groupBy: a } : {},
|
|
1904
|
+
...l ? { having: l } : {},
|
|
1905
|
+
...c !== void 0 ? { distinct: c } : {}
|
|
1906
|
+
}, d = (p) => ({ type: "field", table: r, column: p }), m = (p) => {
|
|
1907
|
+
var b;
|
|
1908
|
+
return (b = o.find((h) => M(h.expression, p))) == null ? void 0 : b.alias;
|
|
1909
|
+
}, g = {
|
|
1910
|
+
...e,
|
|
1911
|
+
from: { id: n, alias: r, query: { union: [f], all: !0 } },
|
|
1912
|
+
joins: [],
|
|
1913
|
+
select: o.map((p) => ({ id: E(), expression: d(p.alias) })),
|
|
1914
|
+
...e.orderBy ? {
|
|
1915
|
+
orderBy: e.orderBy.map((p) => {
|
|
1916
|
+
const b = m(p.expression);
|
|
1917
|
+
return b ? { ...p, expression: d(b) } : p;
|
|
1918
|
+
})
|
|
1919
|
+
} : {}
|
|
1920
|
+
};
|
|
1921
|
+
return ["where", "groupBy", "having", "distinct"].forEach((p) => delete g[p]), g;
|
|
1922
|
+
}
|
|
1923
|
+
function pr(e, n, r, t) {
|
|
1924
|
+
const o = V(n, r);
|
|
1925
|
+
if (!o) return n;
|
|
1926
|
+
const s = ue(e, o.query.union).map((i) => ({
|
|
1927
|
+
id: E(),
|
|
1928
|
+
...i.name ? { alias: i.name } : {},
|
|
1929
|
+
expression: B(i.type)
|
|
1930
|
+
}));
|
|
1931
|
+
return W(n, o, {
|
|
1932
|
+
...o.query,
|
|
1933
|
+
union: [...o.query.union, { from: t, joins: [], select: s }]
|
|
1934
|
+
});
|
|
1935
|
+
}
|
|
1936
|
+
function hr(e, n, r, t) {
|
|
1937
|
+
const o = {
|
|
1938
|
+
id: r,
|
|
1939
|
+
alias: Ct(e, n),
|
|
1940
|
+
joinType: "left",
|
|
1941
|
+
query: { union: [{ from: t, joins: [], select: [] }], all: !0 }
|
|
1942
|
+
};
|
|
1943
|
+
return { ...n, joins: [...n.joins, { ...o, on: rn(e, n, o) }] };
|
|
1944
|
+
}
|
|
1945
|
+
function yr(e, n, r) {
|
|
1946
|
+
const t = V(e, n);
|
|
1947
|
+
return !t || t.query.all === r ? e : W(e, t, { ...t.query, all: r });
|
|
1948
|
+
}
|
|
1949
|
+
function br(e, n, r, t, o) {
|
|
1950
|
+
const s = V(n, r), [i, ...u] = (s == null ? void 0 : s.query.union) ?? [], a = i == null ? void 0 : i.select[t], l = o.trim();
|
|
1951
|
+
if (!s || !a || !l || l.length > D || i.select.some((f) => x(f) === l)) return n;
|
|
1952
|
+
const c = W(n, s, {
|
|
1953
|
+
...s.query,
|
|
1954
|
+
union: [
|
|
1955
|
+
{ ...i, select: i.select.map((f, d) => d === t ? { ...f, alias: l } : f) },
|
|
1956
|
+
...u
|
|
1957
|
+
]
|
|
1958
|
+
});
|
|
1959
|
+
return Ae(e, n, c);
|
|
1960
|
+
}
|
|
1961
|
+
function gr(e, n) {
|
|
1962
|
+
var c;
|
|
1963
|
+
const r = V(e, n);
|
|
1964
|
+
if (!r || r.query.union.length !== 1) return e;
|
|
1965
|
+
const [t] = r.query.union, o = new Map(t.select.flatMap((f) => {
|
|
1966
|
+
const d = x(f);
|
|
1967
|
+
return d ? [[d, f.expression]] : [];
|
|
1968
|
+
})), s = (f) => f.type === "field" && f.table === r.alias && o.has(f.column ?? ""), i = (f) => ve(f, (d) => s(d) ? o.get(d.column) : d), u = e.select.map((f) => {
|
|
1969
|
+
const d = i(f.expression);
|
|
1970
|
+
if (f.alias || !s(f.expression)) return { ...f, expression: d };
|
|
1971
|
+
const m = f.expression.column;
|
|
1972
|
+
return x({ ...f, expression: d }) === m ? { ...f, expression: d } : { ...f, expression: d, alias: m };
|
|
1973
|
+
}), a = t.groupBy !== void 0 || t.having !== void 0 || !!t.distinct || t.select.some((f) => ne(f.expression, Q));
|
|
1974
|
+
if (r === e.from && !a) {
|
|
1975
|
+
const f = te({ ...e, select: u }, i, (d) => d);
|
|
1976
|
+
return {
|
|
1977
|
+
...f,
|
|
1978
|
+
from: t.from,
|
|
1979
|
+
joins: [...t.joins, ...f.joins],
|
|
1980
|
+
where: vn([...ee(t.where), ...ee(f.where)]),
|
|
1981
|
+
// Nothing picked: the query's own columns, as the union had them.
|
|
1982
|
+
select: f.select.length > 0 ? f.select : t.select
|
|
1983
|
+
};
|
|
1984
|
+
}
|
|
1985
|
+
const l = e.joins.length === 0 && !e.where && !((c = e.groupBy) != null && c.length) && !e.having && !e.distinct && [...e.select, ...e.orderBy ?? []].every((f) => s(f.expression));
|
|
1986
|
+
return r === e.from && l ? {
|
|
1987
|
+
...e,
|
|
1988
|
+
...t,
|
|
1989
|
+
select: u.length > 0 ? u : t.select,
|
|
1990
|
+
...e.orderBy ? {
|
|
1991
|
+
orderBy: e.orderBy.map((f) => ({ ...f, expression: i(f.expression) }))
|
|
1992
|
+
} : {}
|
|
1993
|
+
} : W(e, r, t);
|
|
1994
|
+
}
|
|
1995
|
+
function $t(e, n) {
|
|
1996
|
+
return e.select.filter((r) => ne(r.expression, n));
|
|
1997
|
+
}
|
|
1998
|
+
function xt(e) {
|
|
1999
|
+
return { ...e.expression, fromSelect: e.id };
|
|
2000
|
+
}
|
|
2001
|
+
function K(e) {
|
|
2002
|
+
const { fromSelect: n, ...r } = e;
|
|
2003
|
+
return n === void 0 ? e : r;
|
|
2004
|
+
}
|
|
2005
|
+
function Bn(e, n) {
|
|
2006
|
+
const r = (o) => typeof o.fromSelect == "string" ? n(o, o.fromSelect) : P(o, r), t = r(e);
|
|
2007
|
+
return M(t, e) ? e : t;
|
|
2008
|
+
}
|
|
2009
|
+
function At(e, n) {
|
|
2010
|
+
var u;
|
|
2011
|
+
const r = new Map($t(e, n).map((a) => [a.id, a])), t = (a) => Bn(a, (l, c) => {
|
|
2012
|
+
const f = r.get(c);
|
|
2013
|
+
return f ? xt(f) : K(l);
|
|
2014
|
+
}), o = e.having && t(e.having);
|
|
2015
|
+
let s = !1;
|
|
2016
|
+
const i = (u = e.orderBy) == null ? void 0 : u.map((a) => {
|
|
2017
|
+
const l = t(a.expression);
|
|
2018
|
+
return l === a.expression ? a : (s = !0, { ...a, expression: l });
|
|
2019
|
+
});
|
|
2020
|
+
return o === e.having && !s ? e : { ...e, having: o, ...i ? { orderBy: i } : {} };
|
|
2021
|
+
}
|
|
2022
|
+
function wr(e, n) {
|
|
2023
|
+
const r = new Set((n.groupBy ?? []).flatMap(({ expression: t }) => t.type === "field" && t.column ? [t.column.toLowerCase()] : []));
|
|
2024
|
+
return Bn(e, (t, o) => {
|
|
2025
|
+
const s = n.select.find((i) => i.id === o);
|
|
2026
|
+
return !(s != null && s.alias) || r.has(s.alias.toLowerCase()) || !M(K(t), s.expression) ? K(t) : { type: "field", column: s.alias };
|
|
2027
|
+
});
|
|
2028
|
+
}
|
|
2029
|
+
function Se(e, n) {
|
|
2030
|
+
const r = (c) => Se(c, n), t = (c) => se(c, r), o = e.from.query && t(e.from.query), s = o && o !== e.from.query ? { ...e.from, query: o } : e.from;
|
|
2031
|
+
let i = !1;
|
|
2032
|
+
const u = e.joins.map((c) => {
|
|
2033
|
+
if (!c.query) return c;
|
|
2034
|
+
const f = t(c.query);
|
|
2035
|
+
return f === c.query ? c : (i = !0, { ...c, query: f });
|
|
2036
|
+
}), a = s === e.from && !i ? e : { ...e, from: s, joins: u }, l = Ke(a, (c) => {
|
|
2037
|
+
const f = r(c.query);
|
|
2038
|
+
return f === c.query ? c : { ...c, query: f };
|
|
2039
|
+
});
|
|
2040
|
+
return Qn(At(l, n), n);
|
|
2041
|
+
}
|
|
2042
|
+
function Sr(e, n, r, t = Q) {
|
|
2043
|
+
if (!U(r)) return r;
|
|
2044
|
+
const o = Te(
|
|
2045
|
+
Se(r, t),
|
|
2046
|
+
(i) => se(i, (u) => Se(u, t))
|
|
2047
|
+
), s = Nt(e, n, o);
|
|
2048
|
+
return mt(e, n, s);
|
|
2049
|
+
}
|
|
2050
|
+
const Tr = [
|
|
2051
|
+
"=",
|
|
2052
|
+
"!=",
|
|
2053
|
+
"IS NOT DISTINCT FROM",
|
|
2054
|
+
"<",
|
|
2055
|
+
"<=",
|
|
2056
|
+
">",
|
|
2057
|
+
">=",
|
|
2058
|
+
"LIKE",
|
|
2059
|
+
"IN",
|
|
2060
|
+
"BETWEEN",
|
|
2061
|
+
"REGEXP",
|
|
2062
|
+
"IS NULL",
|
|
2063
|
+
"IS NOT NULL"
|
|
2064
|
+
], Bt = ["IS NULL", "IS NOT NULL"];
|
|
2065
|
+
function vr(e) {
|
|
2066
|
+
return Bt.includes(e);
|
|
2067
|
+
}
|
|
2068
|
+
const In = (e) => {
|
|
2069
|
+
const n = /* @__PURE__ */ new Map();
|
|
2070
|
+
return We(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;
|
|
2071
|
+
};
|
|
2072
|
+
function kn(e, n) {
|
|
2073
|
+
const r = /* @__PURE__ */ new Map(), t = (a) => (r.has(a) || r.set(a, { types: /* @__PURE__ */ new Set(), columns: /* @__PURE__ */ new Map() }), r.get(a)), o = (a, l) => {
|
|
2074
|
+
const c = new Map(a);
|
|
2075
|
+
return S(l).forEach((f) => c.set(T(f), f.table)), c;
|
|
2076
|
+
}, s = (a, l) => {
|
|
2077
|
+
if (!n || l.type !== "field" || !l.table || !l.column) return;
|
|
2078
|
+
const c = a.has(l.table) ? a.get(l.table) : l.table, f = c === void 0 ? void 0 : A(n, c), d = f == null ? void 0 : f.columns.find((m) => m.name === l.column);
|
|
2079
|
+
return f && d ? { table: f.name, column: d } : void 0;
|
|
2080
|
+
}, i = (a, l) => {
|
|
2081
|
+
if (a.type === "param" && t(a.name), a.type === "binary" && a.right) {
|
|
2082
|
+
const f = a.operator === "LIKE" || a.operator === "REGEXP", d = (g, p) => {
|
|
2083
|
+
const b = s(l, p);
|
|
2084
|
+
if (g.type !== "param" || !b) return;
|
|
2085
|
+
const h = t(g.name);
|
|
2086
|
+
if (h.types.add(b.column.type), !f) {
|
|
2087
|
+
const w = `${b.table}.${b.column.name}`;
|
|
2088
|
+
h.columns.set(w, { table: b.table, column: b.column.name });
|
|
2089
|
+
}
|
|
2090
|
+
};
|
|
2091
|
+
d(a.left, a.right), (a.right.type === "list" ? a.right.items : [a.right]).forEach((g) => d(g, a.left));
|
|
2092
|
+
}
|
|
2093
|
+
const c = a.type === "exists" ? o(l, a.query) : l;
|
|
2094
|
+
oe(a).forEach((f) => i(f, c));
|
|
2095
|
+
}, u = (a) => {
|
|
2096
|
+
const l = o(/* @__PURE__ */ new Map(), a);
|
|
2097
|
+
_e(a).forEach((c) => i(c, l)), S(a).forEach((c) => c.query && Y(c.query).forEach(u)), Xe(a).forEach((c) => u(c.query));
|
|
2098
|
+
};
|
|
2099
|
+
return (e.shared ?? []).forEach((a) => Y(a.query).forEach(u)), u(e), r;
|
|
2100
|
+
}
|
|
2101
|
+
function Nr(e, n) {
|
|
2102
|
+
const r = In(e);
|
|
2103
|
+
return [...kn(e, n)].map(([t, { types: o, columns: s }]) => {
|
|
2104
|
+
const i = r.get(t);
|
|
2105
|
+
return {
|
|
2106
|
+
name: t,
|
|
2107
|
+
...o.size === 1 ? { type: [...o][0] } : {},
|
|
2108
|
+
// (Two different ones: validateQuery reports it; the first here.)
|
|
2109
|
+
...i ? { relative: JSON.parse([...i][0]) } : {},
|
|
2110
|
+
...s.size === 1 ? { column: [...s.values()][0] } : {}
|
|
2111
|
+
};
|
|
2112
|
+
});
|
|
2113
|
+
}
|
|
2114
|
+
function Er(e) {
|
|
2115
|
+
return [...In(e)].filter(([, n]) => n.size > 1).map(([n]) => n);
|
|
2116
|
+
}
|
|
2117
|
+
function Cr(e, n) {
|
|
2118
|
+
return [...kn(e, n)].filter(([, r]) => r.types.size > 1).map(([r]) => r);
|
|
2119
|
+
}
|
|
2120
|
+
function $r(e, n) {
|
|
2121
|
+
const r = new Map(Object.entries(n));
|
|
2122
|
+
return We(e, (t) => {
|
|
2123
|
+
if (t.type !== "param") return t;
|
|
2124
|
+
if (!r.has(t.name)) throw new Error(`no value for parameter "${t.name}"`);
|
|
2125
|
+
const o = r.get(t.name);
|
|
2126
|
+
return o === "" ? { type: "literal", value: o, empty: !0 } : { type: "literal", value: o };
|
|
2127
|
+
});
|
|
2128
|
+
}
|
|
2129
|
+
export {
|
|
2130
|
+
Dt as $,
|
|
2131
|
+
vr as A,
|
|
2132
|
+
Tr as B,
|
|
2133
|
+
jt as C,
|
|
2134
|
+
Zn as D,
|
|
2135
|
+
O as E,
|
|
2136
|
+
Ze as F,
|
|
2137
|
+
me as G,
|
|
2138
|
+
Rt as H,
|
|
2139
|
+
K as I,
|
|
2140
|
+
pe as J,
|
|
2141
|
+
Cr as K,
|
|
2142
|
+
Er as L,
|
|
2143
|
+
Jn as M,
|
|
2144
|
+
an as N,
|
|
2145
|
+
tr as O,
|
|
2146
|
+
er as P,
|
|
2147
|
+
Wn as Q,
|
|
2148
|
+
yn as R,
|
|
2149
|
+
bn as S,
|
|
2150
|
+
ht as T,
|
|
2151
|
+
zn as U,
|
|
2152
|
+
Ut as V,
|
|
2153
|
+
Ye as W,
|
|
2154
|
+
Pt as X,
|
|
2155
|
+
_t as Y,
|
|
2156
|
+
Vt as Z,
|
|
2157
|
+
Jt as _,
|
|
2158
|
+
Mt as a,
|
|
2159
|
+
sr as a0,
|
|
2160
|
+
rr as a1,
|
|
2161
|
+
ir as a2,
|
|
2162
|
+
ar as a3,
|
|
2163
|
+
or as a4,
|
|
2164
|
+
St as a5,
|
|
2165
|
+
xt as a6,
|
|
2166
|
+
Vn as a7,
|
|
2167
|
+
tn as a8,
|
|
2168
|
+
Gt as a9,
|
|
2169
|
+
Xt as aa,
|
|
2170
|
+
Yt as ab,
|
|
2171
|
+
Kt as ac,
|
|
2172
|
+
en as ad,
|
|
2173
|
+
Lt as ae,
|
|
2174
|
+
Zt as af,
|
|
2175
|
+
ut as ag,
|
|
2176
|
+
Ft as ah,
|
|
2177
|
+
at as ai,
|
|
2178
|
+
st as aj,
|
|
2179
|
+
ie as ak,
|
|
2180
|
+
zt as al,
|
|
2181
|
+
Re as am,
|
|
2182
|
+
rn as an,
|
|
2183
|
+
$t as ao,
|
|
2184
|
+
qt as ap,
|
|
2185
|
+
Wt as aq,
|
|
2186
|
+
Ht as ar,
|
|
2187
|
+
Hn as as,
|
|
2188
|
+
Xe as at,
|
|
2189
|
+
ae as au,
|
|
2190
|
+
J as av,
|
|
2191
|
+
he as aw,
|
|
2192
|
+
Ot as b,
|
|
2193
|
+
Qt as c,
|
|
2194
|
+
nr as d,
|
|
2195
|
+
pr as e,
|
|
2196
|
+
hr as f,
|
|
2197
|
+
lr as g,
|
|
2198
|
+
fr as h,
|
|
2199
|
+
dr as i,
|
|
2200
|
+
br as j,
|
|
2201
|
+
mt as k,
|
|
2202
|
+
Ct as l,
|
|
2203
|
+
dn as m,
|
|
2204
|
+
mr as n,
|
|
2205
|
+
cr as o,
|
|
2206
|
+
ur as p,
|
|
2207
|
+
Nr as q,
|
|
2208
|
+
ke as r,
|
|
2209
|
+
yr as s,
|
|
2210
|
+
Sr as t,
|
|
2211
|
+
gr as u,
|
|
2212
|
+
I as v,
|
|
2213
|
+
G as w,
|
|
2214
|
+
gn as x,
|
|
2215
|
+
$r as y,
|
|
2216
|
+
wr as z
|
|
2217
|
+
};
|