@nongt/visql 0.5.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -2
- package/dist/{PipelineEditor-Cx_c0xZL.js → PipelineEditor-X83xfPeC.js} +3195 -3167
- package/dist/{core-api-DzslKd1R.js → core-api-D4QbGLG-.js} +1 -1
- package/dist/core.js +2 -2
- package/dist/index.js +4 -4
- package/dist/issueLocation-BcILIZqd.js +3278 -0
- package/dist/mui.js +1 -1
- package/dist/style.css +1 -1
- package/dist/types/core/DialectContext.d.ts +1 -2
- package/dist/types/core/JoinKeysContext.d.ts +6 -0
- package/dist/types/core/components/flow/GroupByFlowNode.d.ts +3 -0
- package/dist/types/core/components/flow/SelectFlowNode.d.ts +3 -0
- package/dist/types/core/dialectSupport.d.ts +6 -0
- package/dist/types/core/i18n/defaultMessages.d.ts +2 -0
- package/dist/types/core/i18n/messages.d.ts +2 -0
- package/dist/types/core/joinOnly.d.ts +9 -0
- package/dist/types/core/queryValidation.d.ts +5 -2
- package/dist/types/core/schema.d.ts +7 -0
- package/dist/types/features/queryEditor/SubFlowPanel.d.ts +5 -1
- package/package.json +4 -2
- package/dist/issueLocation-CWUdTtCh.js +0 -3183
|
@@ -1,3183 +0,0 @@
|
|
|
1
|
-
import { D as ge, h as ce, q as F, t as D, i as H, s as Hn, l as ue, u as Xn, w as _e, e as Fe, x as un, k as Z, r as me, o as Ne, j as K, y as Kn, d as W, z as _t, A as he, B as Ae, C as Ft, M as Y, F as Yn, G as Jt, g as z, H as Je, I as cn, b as Se, J as zn, n as Pt, K as Zn, f as Vt, L as Wt, N as Mn, O as Ht, P as Le, Q as Xt } from "./schema-z95dxLxu.js";
|
|
2
|
-
import { c as Kt, s as Yt, b as et } from "./calendarDate-CYlr9TvX.js";
|
|
3
|
-
const zt = 1e3, nt = 15, Zt = 40, ss = 100;
|
|
4
|
-
function er(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 Ze(e) {
|
|
9
|
-
return Number.isFinite(e) && er(String(e)) <= nt;
|
|
10
|
-
}
|
|
11
|
-
const is = [];
|
|
12
|
-
function oe(e) {
|
|
13
|
-
return F(e).filter((n) => n.query !== void 0);
|
|
14
|
-
}
|
|
15
|
-
function dn(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 tt(e) {
|
|
20
|
-
const n = [], r = (t) => {
|
|
21
|
-
t.type === "subquery" ? n.push(t) : me(t).forEach(r);
|
|
22
|
-
};
|
|
23
|
-
return Hn(e).forEach(r), n;
|
|
24
|
-
}
|
|
25
|
-
function rt(e, n) {
|
|
26
|
-
let r = !1;
|
|
27
|
-
const t = (i) => {
|
|
28
|
-
if (i.type !== "subquery") return Ne(i, t);
|
|
29
|
-
const a = n(i);
|
|
30
|
-
return a !== i && (r = !0), a;
|
|
31
|
-
}, s = _e(e, t, (i) => i);
|
|
32
|
-
return r ? s : e;
|
|
33
|
-
}
|
|
34
|
-
const as = (e, n) => `${e}#${n}`;
|
|
35
|
-
function nr(e) {
|
|
36
|
-
return !("type" in e) && "of" in e;
|
|
37
|
-
}
|
|
38
|
-
const tr = (e) => ({
|
|
39
|
-
id: e.id,
|
|
40
|
-
alias: e.name,
|
|
41
|
-
query: e.query
|
|
42
|
-
});
|
|
43
|
-
function On(e, n) {
|
|
44
|
-
var s;
|
|
45
|
-
const r = oe(e).find((i) => i.id === n);
|
|
46
|
-
if (r) return r;
|
|
47
|
-
const t = (s = e.shared) == null ? void 0 : s.find((i) => i.id === n);
|
|
48
|
-
return t && tr(t);
|
|
49
|
-
}
|
|
50
|
-
function st(e, n) {
|
|
51
|
-
const r = On(e, n);
|
|
52
|
-
if (r) return H(r.query) ? void 0 : r;
|
|
53
|
-
const t = tt(e).find((u) => u.id === n);
|
|
54
|
-
if (t) return t;
|
|
55
|
-
const s = /^(.*)#(\d+)$/.exec(n), i = s ? On(e, s[1]) : void 0, a = Number(s == null ? void 0 : s[2]), l = i && H(i.query) ? i.query.union[a] : void 0;
|
|
56
|
-
return i && l ? {
|
|
57
|
-
id: n,
|
|
58
|
-
alias: i.alias,
|
|
59
|
-
query: l,
|
|
60
|
-
of: i,
|
|
61
|
-
index: a
|
|
62
|
-
} : void 0;
|
|
63
|
-
}
|
|
64
|
-
function it(e) {
|
|
65
|
-
return { id: e.id, ...Fe(e.query) };
|
|
66
|
-
}
|
|
67
|
-
function Xe(e) {
|
|
68
|
-
const {
|
|
69
|
-
from: n,
|
|
70
|
-
joins: r,
|
|
71
|
-
where: t,
|
|
72
|
-
select: s,
|
|
73
|
-
groupBy: i,
|
|
74
|
-
having: a,
|
|
75
|
-
distinct: l
|
|
76
|
-
} = e;
|
|
77
|
-
return {
|
|
78
|
-
from: n,
|
|
79
|
-
joins: r,
|
|
80
|
-
select: s,
|
|
81
|
-
...t !== void 0 ? { where: t } : {},
|
|
82
|
-
...i !== void 0 ? { groupBy: i } : {},
|
|
83
|
-
...a !== void 0 ? { having: a } : {},
|
|
84
|
-
...l !== void 0 ? { distinct: l } : {}
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
function rr(e, n) {
|
|
88
|
-
return n.reduce((r, t) => {
|
|
89
|
-
const s = r && st(r, t);
|
|
90
|
-
return s ? it(s) : void 0;
|
|
91
|
-
}, e);
|
|
92
|
-
}
|
|
93
|
-
function fn(e, n, r) {
|
|
94
|
-
if (n.length === 0) return r(e);
|
|
95
|
-
const [t, ...s] = n, i = st(e, t);
|
|
96
|
-
if (!i) return e;
|
|
97
|
-
const a = it(i), l = fn(a, s, r);
|
|
98
|
-
if (l === a || !ce(l)) return e;
|
|
99
|
-
if ("type" in i && i.type === "subquery") {
|
|
100
|
-
const c = { ...i, query: Xe(l) };
|
|
101
|
-
return rt(e, (m) => m === i ? c : m);
|
|
102
|
-
}
|
|
103
|
-
const u = (c, m) => {
|
|
104
|
-
if (!oe(e).some((d) => d.id === c.id))
|
|
105
|
-
return un(e, (d, g) => g.id === c.id ? m : d);
|
|
106
|
-
const f = { ...c, query: m };
|
|
107
|
-
return c === e.from ? { ...e, from: f } : {
|
|
108
|
-
...e,
|
|
109
|
-
joins: e.joins.map((d) => d === c ? f : d)
|
|
110
|
-
};
|
|
111
|
-
};
|
|
112
|
-
if (nr(i)) {
|
|
113
|
-
const { of: c, index: m } = i, { union: f } = c.query;
|
|
114
|
-
return u(c, {
|
|
115
|
-
...c.query,
|
|
116
|
-
union: f.map((d, g) => g === m ? Xe(l) : d)
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
return u(i, Xe(l));
|
|
120
|
-
}
|
|
121
|
-
const sr = {
|
|
122
|
-
text: "varchar",
|
|
123
|
-
integer: "integer",
|
|
124
|
-
number: "decimal",
|
|
125
|
-
date: "date",
|
|
126
|
-
dateTime: "dateTime",
|
|
127
|
-
time: "time",
|
|
128
|
-
boolean: "boolean"
|
|
129
|
-
};
|
|
130
|
-
function mn(e, n, r = []) {
|
|
131
|
-
const t = Fe(n);
|
|
132
|
-
if (t.select.length === 0)
|
|
133
|
-
return F(t).flatMap((a) => {
|
|
134
|
-
var u;
|
|
135
|
-
if (a.table !== void 0) return ((u = Z(e, a.table)) == null ? void 0 : u.columns) ?? [];
|
|
136
|
-
const l = dn(a, r);
|
|
137
|
-
return l ? mn(e, l, r) : [];
|
|
138
|
-
});
|
|
139
|
-
const s = new Map(F(t).map((a) => [D(a), a.table])), i = (a) => {
|
|
140
|
-
var l;
|
|
141
|
-
if (a.type === "field") {
|
|
142
|
-
const u = a.table === void 0 ? void 0 : s.get(a.table), c = u === void 0 || (l = Z(e, u)) == null ? void 0 : l.columns.find((m) => m.name === a.column);
|
|
143
|
-
return (c == null ? void 0 : c.type) ?? "varchar";
|
|
144
|
-
}
|
|
145
|
-
if (a.type === "function") {
|
|
146
|
-
const u = a.name.toUpperCase();
|
|
147
|
-
if (u === "COUNT") return "integer";
|
|
148
|
-
if (u === "SUM" || u === "AVG") return "decimal";
|
|
149
|
-
if ((u === "MIN" || u === "MAX") && a.args[0]) return i(a.args[0]);
|
|
150
|
-
}
|
|
151
|
-
return a.type === "literal" && typeof a.value == "number" ? "decimal" : a.type === "literal" && a.as ? sr[a.as] : "varchar";
|
|
152
|
-
};
|
|
153
|
-
return t.select.flatMap((a) => {
|
|
154
|
-
const l = K(a);
|
|
155
|
-
return l ? [{ name: l, label: l, type: i(a.expression) }] : [];
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
function at(e, n, r = []) {
|
|
159
|
-
const t = dn(n, r), s = (t ? mn(e, t, r) : []).filter((i, a, l) => l.findIndex((u) => u.name === i.name) === a);
|
|
160
|
-
return { name: n.alias, label: n.alias, columns: s };
|
|
161
|
-
}
|
|
162
|
-
function os(e, n, r = []) {
|
|
163
|
-
return F(n).filter((t) => t.table === void 0).map((t) => at(e, t, r));
|
|
164
|
-
}
|
|
165
|
-
function ot(e, n = ge) {
|
|
166
|
-
if (H(e)) return !1;
|
|
167
|
-
const r = e;
|
|
168
|
-
return (r.groupBy ?? []).length === 0 && r.select.length > 0 && r.select.every((t) => ue(t.expression, n)) && Xn(r, n).select.length === 0;
|
|
169
|
-
}
|
|
170
|
-
function Un(e, n, r) {
|
|
171
|
-
const t = (s) => s.type === "field" ? s.table === n ? { ...s, table: r } : s : s.type === "exists" && Kn(s.query).has(n) ? { ...s, link: t(s.link) } : Ne(s, t);
|
|
172
|
-
return _e(e, t, (s) => s);
|
|
173
|
-
}
|
|
174
|
-
function lt(e, n) {
|
|
175
|
-
const r = Fe(n), t = F(r).flatMap((s) => s.table === void 0 ? [] : [{ table: s.table, alias: s.alias }]);
|
|
176
|
-
return r.select.length === 0 ? t.flatMap(({ table: s }) => {
|
|
177
|
-
var i;
|
|
178
|
-
return (((i = Z(e, s)) == null ? void 0 : i.columns) ?? []).map((a) => ({ name: a.name, table: s, column: a.name }));
|
|
179
|
-
}) : r.select.flatMap((s) => {
|
|
180
|
-
const { expression: i } = s;
|
|
181
|
-
if (i.type !== "field" || !i.column) return [];
|
|
182
|
-
const a = t.find((u) => D(u) === i.table), l = s.alias || i.column;
|
|
183
|
-
return a ? [{ name: l, table: a.table, column: i.column }] : [];
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
function ut(e, n) {
|
|
187
|
-
return oe(n).flatMap((r) => lt(e, r.query).flatMap(({ name: t, table: s, column: i }) => e.relations.flatMap((a) => {
|
|
188
|
-
const l = a.fromTable === s && a.fromColumn === i;
|
|
189
|
-
if (!l && !(a.toTable === s && a.toColumn === i)) return [];
|
|
190
|
-
const u = l ? a.toTable : a.fromTable, c = {
|
|
191
|
-
type: "binary",
|
|
192
|
-
operator: "=",
|
|
193
|
-
left: { type: "field", table: u, column: l ? a.toColumn : a.fromColumn },
|
|
194
|
-
right: { type: "field", table: r.alias, column: t }
|
|
195
|
-
};
|
|
196
|
-
return [{ table: u, on: c }];
|
|
197
|
-
})));
|
|
198
|
-
}
|
|
199
|
-
function ir(e, n, r) {
|
|
200
|
-
var t;
|
|
201
|
-
return (t = ut(e, n).find((s) => s.table === r)) == null ? void 0 : t.on;
|
|
202
|
-
}
|
|
203
|
-
function ct(e, n, r) {
|
|
204
|
-
var u;
|
|
205
|
-
const t = F(n).flatMap((c) => c !== r && c.table !== void 0 ? [{ table: c.table, alias: c.alias }] : []), s = (c, m) => m === void 0 ? { type: "field", table: c } : { type: "field", table: c, column: m }, a = lt(e, r.query).flatMap(({ name: c, table: m, column: f }) => t.flatMap((d) => e.relations.filter((g) => g.fromTable === m && g.fromColumn === f && g.toTable === d.table || g.toTable === m && g.toColumn === f && g.fromTable === d.table).map((g) => ({
|
|
206
|
-
name: c,
|
|
207
|
-
right: s(D(d), g.fromTable === m ? g.toColumn : g.fromColumn)
|
|
208
|
-
}))))[0], l = (u = at(e, r).columns[0]) == null ? void 0 : u.name;
|
|
209
|
-
return {
|
|
210
|
-
type: "binary",
|
|
211
|
-
operator: "=",
|
|
212
|
-
left: s(r.alias, (a == null ? void 0 : a.name) ?? l),
|
|
213
|
-
right: (a == null ? void 0 : a.right) ?? { type: "field" }
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
const Oe = (e, n) => `${e} ${n}${e === 1 ? "" : "s"}`;
|
|
217
|
-
function dt({ unit: e, offset: n, anchor: r }) {
|
|
218
|
-
const t = Math.abs(n);
|
|
219
|
-
if (r) {
|
|
220
|
-
const s = r === "start" ? "Start" : "End";
|
|
221
|
-
return n === 0 ? `${s} of this ${e}` : t === 1 ? `${s} of ${n < 0 ? "last" : "next"} ${e}` : n < 0 ? `${s} of the ${e} ${Oe(t, e)} ago` : `${s} of the ${e} in ${Oe(t, e)}`;
|
|
222
|
-
}
|
|
223
|
-
return n === 0 ? "Today" : e === "day" && t === 1 ? n < 0 ? "Yesterday" : "Tomorrow" : n < 0 ? `${Oe(t, e)} ago` : `In ${Oe(t, e)}`;
|
|
224
|
-
}
|
|
225
|
-
const ar = {
|
|
226
|
-
// Section captions on the main canvas
|
|
227
|
-
sectionFrom: "From Tables",
|
|
228
|
-
sectionWhere: "Conditions",
|
|
229
|
-
sectionGroup: "Group Rows",
|
|
230
|
-
sectionSelect: "Columns Selection",
|
|
231
|
-
sectionOrder: "Sort & Limit",
|
|
232
|
-
/** Shared queries (WITH, issue 194): their section, before FROM. */
|
|
233
|
-
sectionShared: "Shared Queries",
|
|
234
|
-
// Breadcrumb / navigation
|
|
235
|
-
main: "Main",
|
|
236
|
-
close: "Close (ESC)",
|
|
237
|
-
jumpedTo: "Jumped to",
|
|
238
|
-
editInSubCanvas: (e) => `Edit in sub-canvas (${e})`,
|
|
239
|
-
// Reordering
|
|
240
|
-
moveUp: "Move Up",
|
|
241
|
-
moveDown: "Move Down",
|
|
242
|
-
moveGroupUp: "Move Group Up",
|
|
243
|
-
moveGroupDown: "Move Group Down",
|
|
244
|
-
moveBranchUp: "Move Branch Up",
|
|
245
|
-
moveBranchDown: "Move Branch Down",
|
|
246
|
-
// Conditions and groups
|
|
247
|
-
createCondition: "Create Condition",
|
|
248
|
-
/** Read-only mode, empty WHERE: stands in for the Create Condition button. */
|
|
249
|
-
noCondition: "No Condition",
|
|
250
|
-
addCondition: "Add Condition",
|
|
251
|
-
addGroup: "Add Group",
|
|
252
|
-
addIfExpression: "Add IF Expression",
|
|
253
|
-
addCaseExpression: "Add CASE Expression",
|
|
254
|
-
wrapInGroup: "Wrap in Group",
|
|
255
|
-
wrapInNewGroup: "Wrap in New Group",
|
|
256
|
-
collapseGroup: "Collapse Group",
|
|
257
|
-
// Wrapping in NOT (shown as NOT itself, like AND/OR — not "Exclude",
|
|
258
|
-
// which reads as "everything else" and NOT leaves out NULL rows too).
|
|
259
|
-
negateCondition: "Negate Condition",
|
|
260
|
-
negateGroup: "Negate Group",
|
|
261
|
-
removeNot: "Remove NOT",
|
|
262
|
-
delete: "Delete",
|
|
263
|
-
deleteGroup: "Delete Group",
|
|
264
|
-
deleteIf: "Delete IF",
|
|
265
|
-
deleteCase: "Delete CASE",
|
|
266
|
-
// EXISTS: whether a row has related rows ("Has Leave Request linked by
|
|
267
|
-
// Employee · Employee ID where …"). The button itself reads EXISTS.
|
|
268
|
-
addExists: "Add a related-rows condition",
|
|
269
|
-
/** An entry of its menu when the same table is linked more than one way
|
|
270
|
-
* (`via`: the foreign key's column). */
|
|
271
|
-
existsVia: (e, n) => `${e} — by ${n}`,
|
|
272
|
-
existsHas: "Has",
|
|
273
|
-
existsLinkedBy: "linked by",
|
|
274
|
-
existsWhere: "where",
|
|
275
|
-
/** No conditions on the related rows: any will do. */
|
|
276
|
-
existsAny: "any",
|
|
277
|
-
existsEdit: "Edit the related rows' conditions",
|
|
278
|
-
// IF / CASE
|
|
279
|
-
addBranch: "Add Branch",
|
|
280
|
-
removeBranch: "Remove Branch",
|
|
281
|
-
addElse: "Add ELSE",
|
|
282
|
-
removeElse: "Remove ELSE",
|
|
283
|
-
// Functions
|
|
284
|
-
addArg: "Add Arg",
|
|
285
|
-
removeArg: "Remove Arg",
|
|
286
|
-
// Value type menu
|
|
287
|
-
typeField: "Field",
|
|
288
|
-
typeValue: "Value",
|
|
289
|
-
/** NULL, picked as a value (no value, on purpose). */
|
|
290
|
-
typeNull: "Empty",
|
|
291
|
-
/** A value's types, picked when nothing typed is compared with it
|
|
292
|
-
* (issue 130). */
|
|
293
|
-
typeText: "Text",
|
|
294
|
-
typeNumber: "Number",
|
|
295
|
-
typeDate: "Date",
|
|
296
|
-
typeBoolean: "Yes / No",
|
|
297
|
-
typeFunction: "Function",
|
|
298
|
-
typeExpression: "Expression",
|
|
299
|
-
/** A named param (set by the application, shown read-only). */
|
|
300
|
-
typeParam: "Parameter",
|
|
301
|
-
/** A date relative to when the query runs (issue 140): the type menu's
|
|
302
|
-
* item, its words ("30 days ago", "Start of last month"), its presets'
|
|
303
|
-
* "Custom", and its custom parts. */
|
|
304
|
-
typeRelativeDate: "Relative date",
|
|
305
|
-
relativeDate: dt,
|
|
306
|
-
relativeCustom: "Custom…",
|
|
307
|
-
relativeUnits: {
|
|
308
|
-
day: "days",
|
|
309
|
-
week: "weeks",
|
|
310
|
-
month: "months",
|
|
311
|
-
year: "years"
|
|
312
|
-
},
|
|
313
|
-
relativeAgo: "ago",
|
|
314
|
-
relativeFromNow: "from now",
|
|
315
|
-
relativeSameDay: "That day",
|
|
316
|
-
relativeStart: "Start of it",
|
|
317
|
-
relativeEnd: "End of it",
|
|
318
|
-
/** A parameter's value isn't in the query: it's given when it runs. */
|
|
319
|
-
paramHint: "Given when the query runs",
|
|
320
|
-
paramNamePlaceholder: "name",
|
|
321
|
-
typeMenuBasic: "Basic",
|
|
322
|
-
/** A function picker's groups, where aggregates are offered: those
|
|
323
|
-
* (which group the rows) apart from the rest. Kept short: the list is
|
|
324
|
-
* as narrow as the name box. */
|
|
325
|
-
functionGroupAggregate: "Aggregates",
|
|
326
|
-
functionGroupOther: "Functions",
|
|
327
|
-
typeMenuCondition: "Condition",
|
|
328
|
-
/** IN's value as a query of its own (issue 122), when offered: its type,
|
|
329
|
-
* its menu section, and its canvas's name. */
|
|
330
|
-
typeSubquery: "Sub query",
|
|
331
|
-
typeMenuQuery: "Query",
|
|
332
|
-
/** IN's values, one each (issue 130): its type, adding one, taking one
|
|
333
|
-
* out. */
|
|
334
|
-
typeList: "Values",
|
|
335
|
-
/** A union of tables' rows (issue 121): the add-table picker's groups
|
|
336
|
-
* when rows can be added. */
|
|
337
|
-
/** A NULL value (no value at all): on a union's branch, that table's
|
|
338
|
-
* having none for the column — and filling one in instead. */
|
|
339
|
-
nullValue: "No value",
|
|
340
|
-
nullInUnion: "Empty for this table",
|
|
341
|
-
fillInValue: "Fill in a value",
|
|
342
|
-
openUnionBranch: (e) => `Open ${e}'s query`,
|
|
343
|
-
removeUnionBranch: (e) => `Remove ${e}'s query`,
|
|
344
|
-
joinTableGroup: "Join a table",
|
|
345
|
-
/** Free-form unions (issue 153): a new one, the query as one's first
|
|
346
|
-
* query, another query for one, and a new query's canvas. */
|
|
347
|
-
createUnion: "Create Union",
|
|
348
|
-
unionWithQuery: "Union with another query",
|
|
349
|
-
addUnionQuery: "Add query",
|
|
350
|
-
/** A union of one query back as a plain query. */
|
|
351
|
-
unwrapUnion: "Make it a plain query",
|
|
352
|
-
newUnionQuery: "New query in the union",
|
|
353
|
-
/** Issue 153, step 4: a union joined. */
|
|
354
|
-
joinUnion: "Join a union",
|
|
355
|
-
addRowsGroup: "Add rows from another table",
|
|
356
|
-
addListValue: "Add value",
|
|
357
|
-
removeListValue: (e) => `Remove ${e}`,
|
|
358
|
-
subqueryLabel: "Sub query",
|
|
359
|
-
// A measure from another table (drill-across): in a SELECT column's type
|
|
360
|
-
// menu, then on its sub query.
|
|
361
|
-
/** The type menu's section, and its one item. */
|
|
362
|
-
typeMenuMeasure: "Measure",
|
|
363
|
-
measureFrom: "Measure from…",
|
|
364
|
-
/** Why it's off: nothing is summed up yet, so there are no groups. */
|
|
365
|
-
measureNeedsGroups: "Sum something up first (a measure is per group)",
|
|
366
|
-
/** Why a table can't be one: a group key's table it doesn't link to. */
|
|
367
|
-
measureUnlinked: (e) => `Doesn't link to ${e}`,
|
|
368
|
-
/** What to sum up of the table picked. */
|
|
369
|
-
measureCount: "Count of rows",
|
|
370
|
-
measureOf: (e, n) => `${{
|
|
371
|
-
SUM: "Sum",
|
|
372
|
-
AVG: "Average",
|
|
373
|
-
MIN: "Minimum",
|
|
374
|
-
MAX: "Maximum"
|
|
375
|
-
}[e]} of ${n}`,
|
|
376
|
-
/** The checkbox on a measure's FROM row. */
|
|
377
|
-
keepInStep: "Keep in step with the report",
|
|
378
|
-
/** On a measure's canvas: what it follows, while kept in step. */
|
|
379
|
-
measureFollows: (e) => `Kept in step with the report: per ${e}`,
|
|
380
|
-
measureAlso: (e) => `also ${e}`,
|
|
381
|
-
/** Where its rows come from (the report's base decides). */
|
|
382
|
-
measureRows: (e) => `Only groups with ${e} rows appear. To see every group, start from the table they belong to.`,
|
|
383
|
-
// FROM / JOIN
|
|
384
|
-
chooseBaseTableHint: "Choose a base table to get started.",
|
|
385
|
-
chooseBaseTable: "Choose a base table",
|
|
386
|
-
addTable: "+ Add Table / Sub Query",
|
|
387
|
-
removeJoin: "Remove Join",
|
|
388
|
-
removeBaseTable: "Remove Base Table",
|
|
389
|
-
setJoinCondition: "Set condition…",
|
|
390
|
-
conditionCount: (e) => `${e} condition${e === 1 ? "" : "s"}`,
|
|
391
|
-
/** Why a FROM row can't move there: `table`'s ON condition uses `needs`,
|
|
392
|
-
* which would then only be joined after it. */
|
|
393
|
-
joinOrderConflict: (e, n) => `${e}'s join condition uses ${n}, which must come first`,
|
|
394
|
-
// Sub queries (derived tables): a query of its own, joined like a table
|
|
395
|
-
// or as the base.
|
|
396
|
-
/** First in the Add Table and base table pickers: opens a new one's
|
|
397
|
-
* canvas, to pick its first table. */
|
|
398
|
-
createSubQuery: "Create Sub Query",
|
|
399
|
-
/** That canvas's label until its first table is picked. */
|
|
400
|
-
newSubQuery: "New sub query",
|
|
401
|
-
/** The icon after a sub query's name, which opens its own canvas: its
|
|
402
|
-
* tooltip and name (in read-only mode: viewSubQuery). */
|
|
403
|
-
editSubQuery: "Edit Sub Query",
|
|
404
|
-
viewSubQuery: "View Sub Query",
|
|
405
|
-
/** A derived table of one row (a total): CROSS JOIN it, no condition. */
|
|
406
|
-
joinToEveryRow: "Join to every row",
|
|
407
|
-
/** In place of its condition, once CROSS joined. */
|
|
408
|
-
joinedToEveryRow: "Joined to every row",
|
|
409
|
-
/** Shared queries (issue 194): the pickers' group of them; the action
|
|
410
|
-
* that makes one (its canvas, empty, labelled newSharedQuery until its
|
|
411
|
-
* first table); the icon opening one (in read-only mode:
|
|
412
|
-
* viewSharedQuery); removing one (its places become sub queries). */
|
|
413
|
-
sharedQueriesGroup: "Shared Queries",
|
|
414
|
-
createSharedQuery: "Create Shared Query",
|
|
415
|
-
newSharedQuery: "New shared query",
|
|
416
|
-
editSharedQuery: "Edit Shared Query",
|
|
417
|
-
viewSharedQuery: "View Shared Query",
|
|
418
|
-
removeSharedQuery: "Remove Shared Query",
|
|
419
|
-
/** The section's "+": a shared query made before it's used. */
|
|
420
|
-
addSharedQuery: "New Shared Query",
|
|
421
|
-
/** Under a shared query's name: where it's used. */
|
|
422
|
-
sharedUsedIn: (e) => e === 0 ? "Not used yet" : e === 1 ? "Used in 1 place" : `Used in ${e} places`,
|
|
423
|
-
/** On a sub query's row: it becomes a shared query; on a shared query's
|
|
424
|
-
* place: it gets a copy of its own, a sub query. */
|
|
425
|
-
makeShared: "Make Shared",
|
|
426
|
-
makeSubQuery: "Make Sub Query",
|
|
427
|
-
/** Removing a shared query that's used: what happens to its places. */
|
|
428
|
-
removeSharedKeepPlaces: "Remove: each place gets a copy as a sub query",
|
|
429
|
-
removeSharedWithPlaces: "Remove, with the tables using it",
|
|
430
|
-
/** Why the second can't be: a query reads from it as its base. */
|
|
431
|
-
removeSharedIsBase: "A query reads from it as its base",
|
|
432
|
-
/** Renaming a shared query: a name another one, or a table, has. */
|
|
433
|
-
sharedNameTaken: (e) => `Another shared query or table is called "${e}"`,
|
|
434
|
-
/** Renaming a derived table: no name given. */
|
|
435
|
-
derivedNameEmpty: "Give it a name",
|
|
436
|
-
/** Renaming a derived table: a name another table already has. */
|
|
437
|
-
derivedNameTaken: (e) => `Another table is called "${e}"`,
|
|
438
|
-
// GROUP BY / HAVING ("group" alone already names an AND/OR group)
|
|
439
|
-
/** The "+" that starts grouping. */
|
|
440
|
-
groupRows: "Group Rows",
|
|
441
|
-
/** A group key added for a SELECT column, following it. */
|
|
442
|
-
groupKeyFromColumn: (e) => `From column ${e}`,
|
|
443
|
-
/** A grouped query's HAVING or sort key with no group key to pick: why,
|
|
444
|
-
* and what to do instead. */
|
|
445
|
-
onlyGroupKeys: "Only group keys here — sum up another column with a function",
|
|
446
|
-
addGrouping: "Add Grouping",
|
|
447
|
-
removeGrouping: "Remove Grouping",
|
|
448
|
-
/** Read-only mode, no grouping. */
|
|
449
|
-
notGrouped: "Not grouped",
|
|
450
|
-
/** Above HAVING's tree (and the "+" that starts it) on the canvas — the
|
|
451
|
-
* counterpart of `sectionWhere`: conditions on groups, not rows. */
|
|
452
|
-
havingCaption: "Group Conditions",
|
|
453
|
-
addHaving: "Add Group Condition",
|
|
454
|
-
/** A column (in SELECT, ORDER BY or HAVING) that isn't one value per group. */
|
|
455
|
-
notPerGroup: "Each group has many of these values: group by it, or summarize it (SUM, COUNT…)",
|
|
456
|
-
/** Quick fix on a SELECT column that isn't one value per group. */
|
|
457
|
-
groupByColumn: "Group by this",
|
|
458
|
-
/** A sum, average or count over rows the joins repeat (the tables'
|
|
459
|
-
* labels, joined). */
|
|
460
|
-
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`,
|
|
461
|
-
/** A grouped query with no columns picked (SELECT *). */
|
|
462
|
-
groupedNeedsColumns: "Pick the columns to show when rows are grouped",
|
|
463
|
-
// COUNT's choice of what to count (the function sub-canvas)
|
|
464
|
-
countRows: "Every row",
|
|
465
|
-
countValues: "Rows with a value",
|
|
466
|
-
countDistinct: "Different values",
|
|
467
|
-
// STRING_AGG's choice of what to join (the function sub-canvas)
|
|
468
|
-
joinEveryValue: "Every value",
|
|
469
|
-
joinDistinctValues: "Different values",
|
|
470
|
-
// SELECT
|
|
471
|
-
addColumn: "Add Column",
|
|
472
|
-
/** Read-only mode, no columns picked: every column is selected. */
|
|
473
|
-
allColumns: "All",
|
|
474
|
-
removeColumn: "Remove Column",
|
|
475
|
-
aliasPlaceholder: "alias",
|
|
476
|
-
/** The SELECT node's DISTINCT switch. */
|
|
477
|
-
distinct: "Leave out duplicate rows",
|
|
478
|
-
/** Read-only mode, DISTINCT on. */
|
|
479
|
-
distinctOn: "Duplicate rows left out",
|
|
480
|
-
// ORDER BY / LIMIT / OFFSET (ASC/DESC themselves are SQL, not translated)
|
|
481
|
-
addSort: "Add Sort",
|
|
482
|
-
removeSort: "Remove Sort",
|
|
483
|
-
toggleSortDirection: "Ascending / descending",
|
|
484
|
-
/** Opens the row-count inputs. */
|
|
485
|
-
limitRows: "Limit rows",
|
|
486
|
-
removeLimit: "Remove row limit",
|
|
487
|
-
// The row-count inputs read as one sentence, each input between two
|
|
488
|
-
// fragments: "Show [50] rows, skipping the first [100]". A fragment may be
|
|
489
|
-
// empty where the language doesn't need it.
|
|
490
|
-
showRowsBefore: "Show",
|
|
491
|
-
showRowsAfter: "rows,",
|
|
492
|
-
skipRowsBefore: "skipping the first",
|
|
493
|
-
skipRowsAfter: "",
|
|
494
|
-
/** What the row counts come to (1-based; `to` undefined: no limit). */
|
|
495
|
-
rowRange: (e, n) => n === void 0 ? `Rows ${e} onward` : `Rows ${e}–${n}`,
|
|
496
|
-
allRows: "All rows",
|
|
497
|
-
/** LIMIT 0. */
|
|
498
|
-
noRows: "No rows",
|
|
499
|
-
/** Skipping rows without a sort: the database may return any rows. */
|
|
500
|
-
pagingNeedsSort: "Add a sort, or each page may get different rows",
|
|
501
|
-
/** DISTINCT on, and a sort key that isn't a selected column (no database
|
|
502
|
-
* accepts that). */
|
|
503
|
-
sortNeedsSelected: "Duplicate rows are left out, so sort only by selected columns",
|
|
504
|
-
/** Read-only mode, no sorting and no limit. */
|
|
505
|
-
noSort: "Unsorted",
|
|
506
|
-
// A text value marked as the empty text (the '' toggle next to it)
|
|
507
|
-
emptyText: "Empty",
|
|
508
|
-
markEmpty: "Empty text (no characters at all)",
|
|
509
|
-
unmarkEmpty: "Type a value instead",
|
|
510
|
-
// Running the query (the host's onRun) and its results under the canvas
|
|
511
|
-
run: "Run",
|
|
512
|
-
running: "Running…",
|
|
513
|
-
/** The Run button's tooltip. */
|
|
514
|
-
runShortcut: "Run the query (Ctrl/⌘+Enter)",
|
|
515
|
-
/** Why Run is off: no table picked yet. */
|
|
516
|
-
runNeedsTable: "Choose a table first",
|
|
517
|
-
/** Why Run is off: the query has errors. */
|
|
518
|
-
runNeedsFixes: (e) => `Fix ${e} problem${e === 1 ? "" : "s"} first`,
|
|
519
|
-
runFailed: "The query failed",
|
|
520
|
-
/** How many rows came back (`truncated`: more matched). */
|
|
521
|
-
runRows: (e, n) => `${n ? "First " : ""}${e} row${e === 1 ? "" : "s"}`,
|
|
522
|
-
runNoRows: "No rows",
|
|
523
|
-
/** The results are from before the latest edits. */
|
|
524
|
-
runOutdated: "The query has changed since this run",
|
|
525
|
-
closeResults: "Close results",
|
|
526
|
-
// The problems button (above the canvas) and its list (issue 132): errors,
|
|
527
|
-
// then warnings, one entry per validateQuery issue, its text by kind (see
|
|
528
|
-
// QueryIssueKind) and under it where it is (issueLocation).
|
|
529
|
-
problems: (e) => `${e} problem${e === 1 ? "" : "s"}`,
|
|
530
|
-
problemsErrors: "Errors — the query can't run",
|
|
531
|
-
problemsWarnings: "Warnings — it runs, check the result",
|
|
532
|
-
/** Issues about the query as a whole (no one section). */
|
|
533
|
-
problemsQuery: "Query",
|
|
534
|
-
// Where a problem is: the steps of its location, joined by ' › '. A
|
|
535
|
-
// section is its caption, a sub query its name, a column its alias;
|
|
536
|
-
// SQL keywords (NOT, IF, CASE, WHEN) stay as they are.
|
|
537
|
-
locationJoinCondition: "join condition",
|
|
538
|
-
locationColumn: (e) => `column ${e}`,
|
|
539
|
-
locationGroupKey: (e) => `key ${e}`,
|
|
540
|
-
locationSortKey: (e) => `sort key ${e}`,
|
|
541
|
-
locationCondition: (e) => `condition ${e}`,
|
|
542
|
-
locationLeft: "left side",
|
|
543
|
-
locationRight: "right side",
|
|
544
|
-
/** One of IN's values, or a function's argument with no label. */
|
|
545
|
-
locationValue: (e) => `value ${e}`,
|
|
546
|
-
locationUnionQuery: (e) => `query ${e}`,
|
|
547
|
-
locationInSubquery: "sub query",
|
|
548
|
-
issueInvalidStructure: "Not in the form a query takes",
|
|
549
|
-
issueDuplicateId: "Two parts share an id",
|
|
550
|
-
issueUnknownTable: "A table that isn't available",
|
|
551
|
-
issueUnknownColumn: "A column that isn't available",
|
|
552
|
-
issueTableNotInQuery: "A column of a table that isn't in the query",
|
|
553
|
-
issueUnqualifiedField: "A column with no table",
|
|
554
|
-
issueIncomplete: "Not filled in yet",
|
|
555
|
-
issueArgCount: "A function given too many values",
|
|
556
|
-
issueAmbiguousTable: "A table named like one of the query around it",
|
|
557
|
-
issueDuplicateTableName: "Two tables with the same name: give one an alias",
|
|
558
|
-
issueDerivedColumnName: "A sub query's column with no name, or a name used twice",
|
|
559
|
-
issueUnknownFunction: "A function that isn't available",
|
|
560
|
-
issueJoinOrder: "A join condition uses a table joined after it",
|
|
561
|
-
issueDistinctSort: "Sorted by a column that isn't selected",
|
|
562
|
-
issueNotGrouped: "A column that isn't one value per group",
|
|
563
|
-
issueUnknownAggregate: "A function that may or may not sum rows up",
|
|
564
|
-
issueMisplacedAggregate: "SUM, COUNT and the like can't be used here",
|
|
565
|
-
issueParamConflict: "A parameter compared with columns of different types",
|
|
566
|
-
issueFanOut: "Sums, averages or counts come out too high: a join repeats rows",
|
|
567
|
-
issueCrossJoin: "Every row joined to every row of a table: rows multiply",
|
|
568
|
-
issueDrillAcrossOutOfStep: "A measure kept in step that no longer matches the grouping",
|
|
569
|
-
issueDrillAcrossFilterMixed: "A condition a measure from another table can't follow",
|
|
570
|
-
issueUnlinkedJoin: "A join condition not linking its table to one before it: rows multiply",
|
|
571
|
-
issueTooLong: "Too long: a name, a text, or a number with too many digits",
|
|
572
|
-
issueDialectUnsupported: "Something this database doesn't have",
|
|
573
|
-
issueSubqueryColumns: "A sub query after IN must pick exactly one column",
|
|
574
|
-
issueNotInSubquery: "NOT IN a sub query keeps no rows if it gives a NULL",
|
|
575
|
-
issueNoLimit: "No row limit: reads every row",
|
|
576
|
-
issueLimitTooHigh: "More rows than allowed",
|
|
577
|
-
issueTypeMismatch: "Values of types that don't go together",
|
|
578
|
-
issueInvalidPattern: "A pattern that isn't a regular expression",
|
|
579
|
-
issueUnionColumns: "Stacked queries whose columns don't line up",
|
|
580
|
-
issueUnionTypes: "Stacked queries giving a column of different types",
|
|
581
|
-
issueSharedOrder: "A shared query can use only the shared queries before it",
|
|
582
|
-
issueUnusedShared: "A shared query used nowhere",
|
|
583
|
-
// What a problem is about, where the query says (issue 132); the kind's
|
|
584
|
-
// text above where it doesn't.
|
|
585
|
-
issueUnknownTableNamed: (e) => `There's no table "${e}"`,
|
|
586
|
-
issueUnknownColumnNamed: (e, n) => `${e} has no column "${n}"`,
|
|
587
|
-
issueTableNotInQueryNamed: (e) => `"${e}" isn't one of the query's tables`,
|
|
588
|
-
issueUnqualifiedFieldNamed: (e) => `"${e}" names no table`,
|
|
589
|
-
issueAmbiguousTableNamed: (e) => `"${e}" is a table of the query around it too: give it an alias`,
|
|
590
|
-
issueNoColumn: "No column picked",
|
|
591
|
-
issueNoValue: "No value filled in",
|
|
592
|
-
issueNothingToCompare: "Nothing to compare with",
|
|
593
|
-
issueComparedWithNull: "Compared with NULL, which is never true: use IS NULL",
|
|
594
|
-
issueNoJoinCondition: (e) => `${e}'s join condition isn't set`,
|
|
595
|
-
issueEmptyGroup: "An empty group of conditions",
|
|
596
|
-
/** `part`: 'condition', or the keyword THEN. */
|
|
597
|
-
issueIfPartMissing: (e) => `IF with no ${e}`,
|
|
598
|
-
issueCaseNoWhen: "CASE with no WHEN",
|
|
599
|
-
issueInNoValues: "IN with no values",
|
|
600
|
-
issueFunctionNeedsArgs: (e, n) => `${e} needs ${n} value${n === 1 ? "" : "s"}`,
|
|
601
|
-
issueFunctionTakesArgs: (e, n) => `${e} takes at most ${n} value${n === 1 ? "" : "s"}`,
|
|
602
|
-
issueDuplicateTableNamed: (e) => `Two tables named "${e}": give one an alias`,
|
|
603
|
-
issueDuplicateColumnNamed: (e) => `Two columns named "${e}"`,
|
|
604
|
-
issueUnnamedColumn: "A column with no name: give it an alias",
|
|
605
|
-
issueUnknownFunctionNamed: (e) => `${e} isn't an available function`,
|
|
606
|
-
issueDialectUnsupportedNamed: (e) => `${e} isn't in this database`,
|
|
607
|
-
issueJoinOrderNamed: (e) => `${e}'s join condition uses a table joined after it`,
|
|
608
|
-
issueNotGroupedNamed: (e) => `${e} isn't one value per group: group by it, or sum it up`,
|
|
609
|
-
issueSelectAllGrouped: "A grouped query has to pick its columns, not all of them (*)",
|
|
610
|
-
issueMisplacedAggregateNamed: (e) => `${e} can't be used here`,
|
|
611
|
-
// A collapsed expression's peek (issue 131): the whole of it, a part per
|
|
612
|
-
// line; picking a part goes to it.
|
|
613
|
-
peekHint: "Pick a part to go to it",
|
|
614
|
-
// Find (issue 131): what's in the query, to go to it.
|
|
615
|
-
/** `keys`: its shortcut, as the user's keyboard has it (⌘F, Ctrl+F). */
|
|
616
|
-
find: (e) => `Find in the query (${e})`,
|
|
617
|
-
findPlaceholder: "Find a column, value, function…",
|
|
618
|
-
findNothing: "Nothing in the query matches",
|
|
619
|
-
// The query's outline (issue 131), in place of the canvas's map: its
|
|
620
|
-
// sections in order, a part a line; picking one goes to it.
|
|
621
|
-
outline: "Outline",
|
|
622
|
-
outlineCollapse: "Fold the outline away",
|
|
623
|
-
outlineExpand: "Show the outline",
|
|
624
|
-
outlineAllColumns: "All columns",
|
|
625
|
-
outlineEmpty: "Nothing yet: pick a table to start.",
|
|
626
|
-
outlineLimit: (e) => `At most ${e} row${e === 1 ? "" : "s"}`,
|
|
627
|
-
outlineOffset: (e) => `Skip ${e} row${e === 1 ? "" : "s"}`
|
|
628
|
-
}, Ln = (e) => "from" in e ? e.from : { table: e.table };
|
|
629
|
-
let jn = 0;
|
|
630
|
-
function Ee() {
|
|
631
|
-
return jn += 1, `join-${Date.now().toString(36)}-${jn}`;
|
|
632
|
-
}
|
|
633
|
-
let Dn = 0;
|
|
634
|
-
function or() {
|
|
635
|
-
return Dn += 1, `select-${Date.now().toString(36)}-${Dn}`;
|
|
636
|
-
}
|
|
637
|
-
let Gn = 0;
|
|
638
|
-
function Rn() {
|
|
639
|
-
return Gn += 1, `group-${Date.now().toString(36)}-${Gn}`;
|
|
640
|
-
}
|
|
641
|
-
let Qn = 0;
|
|
642
|
-
function lr() {
|
|
643
|
-
return Qn += 1, `order-${Date.now().toString(36)}-${Qn}`;
|
|
644
|
-
}
|
|
645
|
-
function Ke(e, n, r) {
|
|
646
|
-
if (n < 0 || n >= e.length || r < 0 || r >= e.length || n === r) return e;
|
|
647
|
-
const t = [...e], [s] = t.splice(n, 1);
|
|
648
|
-
return t.splice(r, 0, s), t;
|
|
649
|
-
}
|
|
650
|
-
const qn = (e) => e !== void 0 && Number.isSafeInteger(e) && e >= 0 ? e : void 0, Ye = (e) => (e ?? "").length <= Y;
|
|
651
|
-
function $e(e, n, r) {
|
|
652
|
-
return e.map((t) => t.id === n ? r(t) : t);
|
|
653
|
-
}
|
|
654
|
-
function ft(e, n, r) {
|
|
655
|
-
return e.map((t) => t.id === n ? r(t) : t);
|
|
656
|
-
}
|
|
657
|
-
function ur(e) {
|
|
658
|
-
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 };
|
|
659
|
-
}
|
|
660
|
-
function cr(e, n, r) {
|
|
661
|
-
return e.query ? {
|
|
662
|
-
id: e.id,
|
|
663
|
-
alias: e.alias,
|
|
664
|
-
query: e.query,
|
|
665
|
-
joinType: r,
|
|
666
|
-
on: n
|
|
667
|
-
} : e.shared !== void 0 ? {
|
|
668
|
-
id: Ee(),
|
|
669
|
-
shared: e.shared,
|
|
670
|
-
alias: e.alias,
|
|
671
|
-
joinType: r,
|
|
672
|
-
on: n
|
|
673
|
-
} : {
|
|
674
|
-
id: Ee(),
|
|
675
|
-
table: e.table,
|
|
676
|
-
alias: e.alias,
|
|
677
|
-
joinType: r,
|
|
678
|
-
on: n
|
|
679
|
-
};
|
|
680
|
-
}
|
|
681
|
-
function mt(e, n) {
|
|
682
|
-
const [r, ...t] = n;
|
|
683
|
-
return { ...e, from: ur(r), joins: t };
|
|
684
|
-
}
|
|
685
|
-
function ne(e) {
|
|
686
|
-
const n = /* @__PURE__ */ new Set(), r = (t) => {
|
|
687
|
-
var s;
|
|
688
|
-
if (t)
|
|
689
|
-
switch (t.type) {
|
|
690
|
-
case "field":
|
|
691
|
-
t.table && n.add(t.table);
|
|
692
|
-
break;
|
|
693
|
-
case "binary":
|
|
694
|
-
r(t.left), r(t.right);
|
|
695
|
-
break;
|
|
696
|
-
case "logical":
|
|
697
|
-
t.operands.forEach(r);
|
|
698
|
-
break;
|
|
699
|
-
case "list":
|
|
700
|
-
t.items.forEach(r);
|
|
701
|
-
break;
|
|
702
|
-
case "not":
|
|
703
|
-
r(t.operand);
|
|
704
|
-
break;
|
|
705
|
-
case "if":
|
|
706
|
-
r(t.condition), r(t.then), r(t.else);
|
|
707
|
-
break;
|
|
708
|
-
case "case":
|
|
709
|
-
r(t.subject), t.branches.forEach((i) => {
|
|
710
|
-
r(i.when), r(i.then);
|
|
711
|
-
}), r(t.else);
|
|
712
|
-
break;
|
|
713
|
-
case "function":
|
|
714
|
-
t.args.forEach(r), (s = t.orderBy) == null || s.forEach((i) => r(i.expression));
|
|
715
|
-
break;
|
|
716
|
-
// Only the tables its link names on the outer query's side: its own
|
|
717
|
-
// tables are joined inside it (withExistsJoins), never to the query
|
|
718
|
-
// around it — that would repeat its rows, which EXISTS is there to avoid.
|
|
719
|
-
case "exists": {
|
|
720
|
-
const i = Kn(t.query);
|
|
721
|
-
ne(t.link).filter((a) => !i.has(a)).forEach((a) => n.add(a));
|
|
722
|
-
break;
|
|
723
|
-
}
|
|
724
|
-
}
|
|
725
|
-
};
|
|
726
|
-
return r(e), [...n];
|
|
727
|
-
}
|
|
728
|
-
function hn(e, n, r, t, s) {
|
|
729
|
-
const i = Ae(n), a = t === void 0 && s === void 0, l = a ? Jt(e, i, r) : void 0, u = {
|
|
730
|
-
id: Ee(),
|
|
731
|
-
table: r,
|
|
732
|
-
joinType: t ?? (l == null ? void 0 : l.joinType) ?? "inner",
|
|
733
|
-
// Else matched to a derived table holding its key, if one does.
|
|
734
|
-
on: s ?? (l == null ? void 0 : l.on) ?? (a ? ir(e, n, r) : void 0)
|
|
735
|
-
};
|
|
736
|
-
return { ...n, joins: [...n.joins, u] };
|
|
737
|
-
}
|
|
738
|
-
function dr(e, n, r) {
|
|
739
|
-
const t = Ae(n), s = Yn(e, t, r);
|
|
740
|
-
return (s && s.length > 0 ? s : [r]).reduce((a, l) => hn(e, a, l), n);
|
|
741
|
-
}
|
|
742
|
-
function de(e, n, r) {
|
|
743
|
-
return ne(r).reduce((t, s) => {
|
|
744
|
-
if (F(t).some((l) => l.alias === s)) return t;
|
|
745
|
-
const i = Ae(t);
|
|
746
|
-
return (Yn(e, i, s) ?? [s]).reduce((l, u) => hn(e, l, u), t);
|
|
747
|
-
}, n);
|
|
748
|
-
}
|
|
749
|
-
function fr(e, n, r, t) {
|
|
750
|
-
return {
|
|
751
|
-
...de(e, n, t),
|
|
752
|
-
select: ft(n.select, r, (s) => ({ ...s, expression: t }))
|
|
753
|
-
};
|
|
754
|
-
}
|
|
755
|
-
function pn(e, n, r) {
|
|
756
|
-
const t = F(e);
|
|
757
|
-
if (n < 0 || n >= t.length || r < 0 || r >= t.length || n === r) return e;
|
|
758
|
-
const s = [...t], [i] = s.splice(n, 1);
|
|
759
|
-
s.splice(r, 0, i);
|
|
760
|
-
const [a, l] = t;
|
|
761
|
-
let u, c;
|
|
762
|
-
if (n === 0 ? (c = a, u = l) : r === 0 && (u = t[n], c = a), u && c) {
|
|
763
|
-
const { on: m, joinType: f } = u;
|
|
764
|
-
s[s.indexOf(c)] = cr(c, m, f);
|
|
765
|
-
}
|
|
766
|
-
return mt(e, s);
|
|
767
|
-
}
|
|
768
|
-
function yn(e) {
|
|
769
|
-
const n = /* @__PURE__ */ new Set([D(e.from)]);
|
|
770
|
-
let r;
|
|
771
|
-
return e.joins.some((t) => {
|
|
772
|
-
n.add(D(t));
|
|
773
|
-
const s = t.joinType === "cross" ? void 0 : t.on, i = ne(s).find((a) => !n.has(a));
|
|
774
|
-
return i && (r = { table: t.table ?? D(t), needs: i }), i !== void 0;
|
|
775
|
-
}), r;
|
|
776
|
-
}
|
|
777
|
-
function ls(e, n, r) {
|
|
778
|
-
const t = pn(e, n, r);
|
|
779
|
-
return t === e ? void 0 : yn(t);
|
|
780
|
-
}
|
|
781
|
-
function mr(e, n, r) {
|
|
782
|
-
const t = pn(e, n, r);
|
|
783
|
-
return t !== e && yn(t) === void 0;
|
|
784
|
-
}
|
|
785
|
-
function hr(e, n, r, t) {
|
|
786
|
-
return {
|
|
787
|
-
...de(e, n, t),
|
|
788
|
-
orderBy: (n.orderBy ?? []).map((s) => s.id === r ? { ...s, expression: t } : s)
|
|
789
|
-
};
|
|
790
|
-
}
|
|
791
|
-
function pr(e, n, r, t) {
|
|
792
|
-
return {
|
|
793
|
-
...de(e, n, t),
|
|
794
|
-
groupBy: (n.groupBy ?? []).map((s) => s.id === r ? { ...s, expression: t } : s)
|
|
795
|
-
};
|
|
796
|
-
}
|
|
797
|
-
function Pe(e, n) {
|
|
798
|
-
if (!n || !_t(n)) return n;
|
|
799
|
-
const r = me(n), t = r.map((u) => Pe(e, u)), i = t.some((u, c) => u !== r[c]) ? Ne(n, (u) => t[r.indexOf(u)]) : n;
|
|
800
|
-
if (i.type !== "exists") return i;
|
|
801
|
-
const { query: a } = i, { joins: l } = de(
|
|
802
|
-
e,
|
|
803
|
-
{
|
|
804
|
-
id: "",
|
|
805
|
-
from: a.from,
|
|
806
|
-
joins: a.joins,
|
|
807
|
-
select: []
|
|
808
|
-
},
|
|
809
|
-
a.where
|
|
810
|
-
);
|
|
811
|
-
return l === a.joins ? i : { ...i, query: { ...a, joins: l } };
|
|
812
|
-
}
|
|
813
|
-
function us(e, n, r) {
|
|
814
|
-
const t = Pe(e, r);
|
|
815
|
-
return { ...de(e, n, t), having: t };
|
|
816
|
-
}
|
|
817
|
-
function _n(e, n) {
|
|
818
|
-
const r = /* @__PURE__ */ new Set([
|
|
819
|
-
...n ? F(n).map(D) : [],
|
|
820
|
-
...he(e).map((s) => s.name)
|
|
821
|
-
]);
|
|
822
|
-
let t = 1;
|
|
823
|
-
for (; r.has(`sub_query_${t}`); ) t += 1;
|
|
824
|
-
return `sub_query_${t}`;
|
|
825
|
-
}
|
|
826
|
-
function cs() {
|
|
827
|
-
return { id: W() };
|
|
828
|
-
}
|
|
829
|
-
function en(e, n, r) {
|
|
830
|
-
if (!ce(n))
|
|
831
|
-
return r.type !== "setBaseTable" && r.type !== "setDerivedBaseTable" ? n : en(e, {
|
|
832
|
-
id: n.id,
|
|
833
|
-
...n.shared ? { shared: n.shared } : {},
|
|
834
|
-
from: { table: "" },
|
|
835
|
-
joins: [],
|
|
836
|
-
select: []
|
|
837
|
-
}, r);
|
|
838
|
-
switch (r.type) {
|
|
839
|
-
case "setBaseTable":
|
|
840
|
-
return { ...n, from: { table: r.table } };
|
|
841
|
-
case "setDerivedBaseTable":
|
|
842
|
-
return {
|
|
843
|
-
...n,
|
|
844
|
-
from: {
|
|
845
|
-
id: r.id ?? Ee(),
|
|
846
|
-
alias: _n(e, n),
|
|
847
|
-
query: { from: Ln(r), joins: [], select: [] }
|
|
848
|
-
}
|
|
849
|
-
};
|
|
850
|
-
case "changeBaseAlias":
|
|
851
|
-
return Ye(r.alias) ? {
|
|
852
|
-
...n,
|
|
853
|
-
// A derived table (or a shared query's place) goes by its alias: it
|
|
854
|
-
// can't be left without one.
|
|
855
|
-
from: n.from.table === void 0 ? { ...n.from, alias: r.alias || n.from.alias } : { ...n.from, alias: r.alias }
|
|
856
|
-
} : n;
|
|
857
|
-
case "addJoin":
|
|
858
|
-
return r.joinType === void 0 && r.on === void 0 ? dr(e, n, r.table) : hn(e, n, r.table, r.joinType, r.on);
|
|
859
|
-
case "addDerivedJoin": {
|
|
860
|
-
const t = {
|
|
861
|
-
id: r.id ?? Ee(),
|
|
862
|
-
alias: _n(e, n),
|
|
863
|
-
joinType: "left",
|
|
864
|
-
query: { from: Ln(r), joins: [], select: [] }
|
|
865
|
-
}, s = ct(e, n, t);
|
|
866
|
-
return { ...n, joins: [...n.joins, { ...t, on: s }] };
|
|
867
|
-
}
|
|
868
|
-
case "renameDerivedTable": {
|
|
869
|
-
const t = oe(n).find((u) => u.id === r.derivedId), s = r.alias.trim();
|
|
870
|
-
if (!t || s === t.alias) return n;
|
|
871
|
-
const i = F(n).filter((u) => u !== t).map(D);
|
|
872
|
-
if (!s || s.length > Y || i.includes(s) || Z(e, s)) return n;
|
|
873
|
-
const a = Un(n, t.alias, s), { from: l } = a;
|
|
874
|
-
return {
|
|
875
|
-
...a,
|
|
876
|
-
from: l.query && l.id === t.id ? { ...l, alias: s } : l,
|
|
877
|
-
joins: a.joins.map((u) => u.id === t.id ? { ...u, alias: s } : u)
|
|
878
|
-
};
|
|
879
|
-
}
|
|
880
|
-
case "renameSharedPlace": {
|
|
881
|
-
const t = r.joinId === void 0 ? n.from : n.joins.find((l) => l.id === r.joinId), s = r.alias.trim();
|
|
882
|
-
if ((t == null ? void 0 : t.shared) === void 0 || s === t.alias) return n;
|
|
883
|
-
const i = F(n).filter((l) => l !== t).map(D);
|
|
884
|
-
if (!s || s.length > Y || i.includes(s) || Z(e, s)) return n;
|
|
885
|
-
const a = Un(n, t.alias, s);
|
|
886
|
-
return r.joinId === void 0 ? { ...a, from: { ...a.from, alias: s } } : { ...a, joins: $e(a.joins, r.joinId, (l) => ({ ...l, alias: s })) };
|
|
887
|
-
}
|
|
888
|
-
case "setMeasureSynced":
|
|
889
|
-
return {
|
|
890
|
-
...n,
|
|
891
|
-
joins: $e(n.joins, r.joinId, (t) => t.query && t.drillAcross ? { ...t, drillAcross: { ...t.drillAcross, synced: r.synced } } : t)
|
|
892
|
-
};
|
|
893
|
-
case "removeJoin":
|
|
894
|
-
return { ...n, joins: n.joins.filter((t) => t.id !== r.joinId) };
|
|
895
|
-
case "changeJoinType":
|
|
896
|
-
return {
|
|
897
|
-
...n,
|
|
898
|
-
// A CROSS JOIN uses no ON, but keeps it: switched back, it's used again.
|
|
899
|
-
joins: $e(n.joins, r.joinId, (t) => ({ ...t, joinType: r.joinType }))
|
|
900
|
-
};
|
|
901
|
-
case "changeJoinAlias":
|
|
902
|
-
return Ye(r.alias) ? {
|
|
903
|
-
...n,
|
|
904
|
-
// A derived table (or a shared query's place) goes by its alias: it
|
|
905
|
-
// can't be left without one.
|
|
906
|
-
joins: $e(n.joins, r.joinId, (t) => t.table === void 0 ? { ...t, alias: r.alias || t.alias } : { ...t, alias: r.alias })
|
|
907
|
-
} : n;
|
|
908
|
-
case "changeJoinOn":
|
|
909
|
-
return {
|
|
910
|
-
...n,
|
|
911
|
-
joins: $e(n.joins, r.joinId, (t) => ({
|
|
912
|
-
...t,
|
|
913
|
-
on: Pe(e, r.on)
|
|
914
|
-
}))
|
|
915
|
-
};
|
|
916
|
-
case "reorderJoin": {
|
|
917
|
-
const t = n.joins.findIndex((a) => a.id === r.joinId);
|
|
918
|
-
if (t === -1) return n;
|
|
919
|
-
const s = r.direction === "up" ? t - 1 : t + 1;
|
|
920
|
-
if (s < 0 || s >= n.joins.length) return n;
|
|
921
|
-
const i = [...n.joins];
|
|
922
|
-
return [i[t], i[s]] = [i[s], i[t]], { ...n, joins: i };
|
|
923
|
-
}
|
|
924
|
-
case "reorderTables":
|
|
925
|
-
return mr(n, r.fromIndex, r.toIndex) ? pn(n, r.fromIndex, r.toIndex) : n;
|
|
926
|
-
case "removeBaseTable":
|
|
927
|
-
return n.joins.length > 0 ? mt(n, n.joins) : n.shared ? { id: n.id, shared: n.shared } : { id: n.id };
|
|
928
|
-
case "addSelectItem": {
|
|
929
|
-
const t = { id: or(), expression: { type: "field" } };
|
|
930
|
-
return { ...n, select: [...n.select, t] };
|
|
931
|
-
}
|
|
932
|
-
case "removeSelectItem":
|
|
933
|
-
return { ...n, select: n.select.filter((t) => t.id !== r.selectId) };
|
|
934
|
-
case "reorderSelectItems": {
|
|
935
|
-
const t = Ke(n.select, r.fromIndex, r.toIndex);
|
|
936
|
-
return t === n.select ? n : { ...n, select: t };
|
|
937
|
-
}
|
|
938
|
-
case "setSelectAlias":
|
|
939
|
-
return Ye(r.alias) ? {
|
|
940
|
-
...n,
|
|
941
|
-
select: ft(n.select, r.selectId, (t) => ({ ...t, alias: r.alias }))
|
|
942
|
-
} : n;
|
|
943
|
-
case "setSelectExpression":
|
|
944
|
-
return fr(e, n, r.selectId, r.expression);
|
|
945
|
-
case "addGroupByItem": {
|
|
946
|
-
const t = { id: Rn(), expression: { type: "field" } };
|
|
947
|
-
return { ...n, groupBy: [...n.groupBy ?? [], t] };
|
|
948
|
-
}
|
|
949
|
-
case "removeGroupByItem": {
|
|
950
|
-
const t = (n.groupBy ?? []).filter((s) => s.id !== r.groupById);
|
|
951
|
-
return { ...n, groupBy: t.length > 0 ? t : void 0 };
|
|
952
|
-
}
|
|
953
|
-
case "reorderGroupByItems": {
|
|
954
|
-
const t = n.groupBy ?? [], s = Ke(t, r.fromIndex, r.toIndex);
|
|
955
|
-
return s === t ? n : { ...n, groupBy: s };
|
|
956
|
-
}
|
|
957
|
-
case "setGroupByExpression":
|
|
958
|
-
return pr(e, n, r.groupById, r.expression);
|
|
959
|
-
case "groupBySelectItem": {
|
|
960
|
-
const t = n.select.find((i) => i.id === r.selectId);
|
|
961
|
-
if (!t) return n;
|
|
962
|
-
const s = { id: Rn(), expression: t.expression };
|
|
963
|
-
return { ...n, groupBy: [...n.groupBy ?? [], s] };
|
|
964
|
-
}
|
|
965
|
-
case "removeHaving":
|
|
966
|
-
return { ...n, having: void 0 };
|
|
967
|
-
case "setDistinct":
|
|
968
|
-
return { ...n, distinct: r.distinct || void 0 };
|
|
969
|
-
case "addOrderByItem": {
|
|
970
|
-
const t = {
|
|
971
|
-
id: lr(),
|
|
972
|
-
expression: { type: "field" },
|
|
973
|
-
direction: "asc"
|
|
974
|
-
};
|
|
975
|
-
return { ...n, orderBy: [...n.orderBy ?? [], t] };
|
|
976
|
-
}
|
|
977
|
-
case "removeOrderByItem": {
|
|
978
|
-
const t = (n.orderBy ?? []).filter((s) => s.id !== r.orderById);
|
|
979
|
-
return { ...n, orderBy: t.length > 0 ? t : void 0 };
|
|
980
|
-
}
|
|
981
|
-
case "reorderOrderByItems": {
|
|
982
|
-
const t = n.orderBy ?? [], s = Ke(t, r.fromIndex, r.toIndex);
|
|
983
|
-
return s === t ? n : { ...n, orderBy: s };
|
|
984
|
-
}
|
|
985
|
-
case "setOrderByExpression":
|
|
986
|
-
return hr(e, n, r.orderById, r.expression);
|
|
987
|
-
case "setOrderByDirection":
|
|
988
|
-
return {
|
|
989
|
-
...n,
|
|
990
|
-
orderBy: (n.orderBy ?? []).map((t) => t.id === r.orderById ? { ...t, direction: r.direction } : t)
|
|
991
|
-
};
|
|
992
|
-
case "setLimit":
|
|
993
|
-
return { ...n, limit: qn(r.limit) };
|
|
994
|
-
case "setOffset":
|
|
995
|
-
return { ...n, offset: qn(r.offset) };
|
|
996
|
-
case "clearPaging":
|
|
997
|
-
return { ...n, limit: void 0, offset: void 0 };
|
|
998
|
-
/* v8 ignore start -- PipelineAction is a closed union; every real case is
|
|
999
|
-
handled above. Exercised in tests via a bogus-cast, same convention as
|
|
1000
|
-
applyRootAction's own `default` in PipelineEditor.tsx. */
|
|
1001
|
-
default:
|
|
1002
|
-
return n;
|
|
1003
|
-
}
|
|
1004
|
-
}
|
|
1005
|
-
const yr = {
|
|
1006
|
-
inner: "INNER",
|
|
1007
|
-
left: "LEFT",
|
|
1008
|
-
right: "RIGHT",
|
|
1009
|
-
full: "FULL",
|
|
1010
|
-
cross: "CROSS"
|
|
1011
|
-
};
|
|
1012
|
-
function ds(e) {
|
|
1013
|
-
return `${yr[e.joinType]} JOIN ${D(e)}`;
|
|
1014
|
-
}
|
|
1015
|
-
function fs(e) {
|
|
1016
|
-
return e.alias || "Column";
|
|
1017
|
-
}
|
|
1018
|
-
function ms() {
|
|
1019
|
-
return "Sort";
|
|
1020
|
-
}
|
|
1021
|
-
function hs() {
|
|
1022
|
-
return "Group";
|
|
1023
|
-
}
|
|
1024
|
-
function ps(e, n, r) {
|
|
1025
|
-
const t = F(n);
|
|
1026
|
-
if (r !== void 0) {
|
|
1027
|
-
const u = t.findIndex((c) => "id" in c && c.id === r);
|
|
1028
|
-
return new Set(t.slice(0, u + 1).map(D));
|
|
1029
|
-
}
|
|
1030
|
-
const s = new Set(Ae(n)), i = Ft(e, [...s]), a = new Set(ut(e, n).map((u) => u.table)), l = he(e).filter((u) => !s.has(u.name) && (i.has(u.name) || a.has(u.name)));
|
|
1031
|
-
return /* @__PURE__ */ new Set([...t.map(D), ...l.map((u) => u.name)]);
|
|
1032
|
-
}
|
|
1033
|
-
function ys(e, n) {
|
|
1034
|
-
const r = F(n);
|
|
1035
|
-
return he(e).flatMap((t) => {
|
|
1036
|
-
const s = r.filter((i) => i.table === t.name).map(D);
|
|
1037
|
-
return (s.length > 0 ? s : [t.name]).map((i) => ({ ref: i, table: t }));
|
|
1038
|
-
});
|
|
1039
|
-
}
|
|
1040
|
-
function gs(e, n) {
|
|
1041
|
-
const r = new Set(Ae(n));
|
|
1042
|
-
return he(e).filter((t) => !r.has(t.name));
|
|
1043
|
-
}
|
|
1044
|
-
function nn(e) {
|
|
1045
|
-
return e ? e.type === "logical" ? e.operands.reduce((n, r) => n + nn(r), 0) : e.type === "not" ? nn(e.operand) : 1 : 0;
|
|
1046
|
-
}
|
|
1047
|
-
function Fn(e) {
|
|
1048
|
-
return e.column ? e.table ? `${e.table}.${e.column}` : e.column : "?";
|
|
1049
|
-
}
|
|
1050
|
-
function bs(e, n = ar) {
|
|
1051
|
-
var r;
|
|
1052
|
-
return e ? e.type === "binary" && e.left.type === "field" && ((r = e.right) == null ? void 0 : r.type) === "field" ? !e.left.column && !e.right.column ? n.setJoinCondition : `${Fn(e.left)} ${e.operator ?? "="} ${Fn(e.right)}` : n.conditionCount(nn(e)) : n.setJoinCondition;
|
|
1053
|
-
}
|
|
1054
|
-
function ws(e, n) {
|
|
1055
|
-
const r = new Set(ne(e.where));
|
|
1056
|
-
return ne(e.having).forEach((t) => r.add(t)), e.joins.filter((t) => t.id !== n).forEach((t) => ne(t.on).forEach((s) => r.add(s))), [...e.select, ...e.groupBy ?? [], ...e.orderBy ?? []].forEach((t) => ne(t.expression).forEach((s) => r.add(s))), r;
|
|
1057
|
-
}
|
|
1058
|
-
function $s(e, n, r) {
|
|
1059
|
-
const t = Pe(e, r);
|
|
1060
|
-
return { ...de(e, n, t), where: t };
|
|
1061
|
-
}
|
|
1062
|
-
function vs(e, n, r, t) {
|
|
1063
|
-
return r.length === 0 ? en(e, n, t) : ce(n) ? fn(n, r, (s) => en(e, s, t)) : n;
|
|
1064
|
-
}
|
|
1065
|
-
const gn = (e) => typeof e == "object" && e !== null && !Array.isArray(e) && !(e instanceof Date), bn = (e) => typeof e.shared == "string";
|
|
1066
|
-
function De(e, n) {
|
|
1067
|
-
if (Array.isArray(e)) {
|
|
1068
|
-
let s = !1;
|
|
1069
|
-
const i = e.flatMap((a) => {
|
|
1070
|
-
const l = De(a, n);
|
|
1071
|
-
return l !== a && (s = !0), l === void 0 ? [] : [l];
|
|
1072
|
-
});
|
|
1073
|
-
return s ? i : e;
|
|
1074
|
-
}
|
|
1075
|
-
if (!gn(e)) return e;
|
|
1076
|
-
if (bn(e)) return n(e);
|
|
1077
|
-
let r = !1;
|
|
1078
|
-
const t = Object.entries(e).map(([s, i]) => {
|
|
1079
|
-
const a = De(i, n);
|
|
1080
|
-
return a !== i && (r = !0), [s, a];
|
|
1081
|
-
});
|
|
1082
|
-
return r ? Object.fromEntries(t) : e;
|
|
1083
|
-
}
|
|
1084
|
-
function Ss(e) {
|
|
1085
|
-
const n = new Map((e.shared ?? []).map((t) => [t.id, []])), r = (t, s) => {
|
|
1086
|
-
if (Array.isArray(t)) {
|
|
1087
|
-
t.forEach((i, a) => r(i, [...s, a]));
|
|
1088
|
-
return;
|
|
1089
|
-
}
|
|
1090
|
-
if (gn(t)) {
|
|
1091
|
-
if (bn(t)) {
|
|
1092
|
-
const i = t.shared;
|
|
1093
|
-
n.set(i, [...n.get(i) ?? [], s]);
|
|
1094
|
-
return;
|
|
1095
|
-
}
|
|
1096
|
-
Object.entries(t).forEach(([i, a]) => r(a, [...s, i]));
|
|
1097
|
-
}
|
|
1098
|
-
};
|
|
1099
|
-
return r(e, []), n;
|
|
1100
|
-
}
|
|
1101
|
-
function Es(e, n) {
|
|
1102
|
-
const r = /* @__PURE__ */ new Set([
|
|
1103
|
-
...(n.shared ?? []).map((s) => s.name),
|
|
1104
|
-
...he(e).map((s) => s.name)
|
|
1105
|
-
]);
|
|
1106
|
-
let t = 1;
|
|
1107
|
-
for (; r.has(`shared_query_${t}`); ) t += 1;
|
|
1108
|
-
return `shared_query_${t}`;
|
|
1109
|
-
}
|
|
1110
|
-
function gr(e, n) {
|
|
1111
|
-
const r = new Set(F(n).map(D));
|
|
1112
|
-
let t = e;
|
|
1113
|
-
for (let s = 2; r.has(t); s += 1) t = `${e}_${s}`;
|
|
1114
|
-
return t;
|
|
1115
|
-
}
|
|
1116
|
-
function br(e, n, r, t = W(), s) {
|
|
1117
|
-
const i = e.shared ?? [], a = i.findIndex((u) => u.id === s), l = { id: t, name: n, query: r };
|
|
1118
|
-
return {
|
|
1119
|
-
...e,
|
|
1120
|
-
shared: a < 0 ? [...i, l] : [...i.slice(0, a), l, ...i.slice(a)]
|
|
1121
|
-
};
|
|
1122
|
-
}
|
|
1123
|
-
const ht = (e, n) => {
|
|
1124
|
-
var r;
|
|
1125
|
-
return (r = e.shared) == null ? void 0 : r.find((t) => t.id === n);
|
|
1126
|
-
};
|
|
1127
|
-
function Ts(e, n, r, t = {}) {
|
|
1128
|
-
const s = ht(e, r);
|
|
1129
|
-
if (!s) return n;
|
|
1130
|
-
const i = {
|
|
1131
|
-
id: t.id ?? W(),
|
|
1132
|
-
shared: r,
|
|
1133
|
-
alias: gr(s.name, n),
|
|
1134
|
-
joinType: t.joinType ?? "inner",
|
|
1135
|
-
...t.on ? { on: t.on } : {}
|
|
1136
|
-
};
|
|
1137
|
-
return { ...n, joins: [...n.joins, i] };
|
|
1138
|
-
}
|
|
1139
|
-
function Ns(e, n, r) {
|
|
1140
|
-
const t = ht(e, r);
|
|
1141
|
-
return t ? { ...n, from: { shared: r, alias: t.name } } : n;
|
|
1142
|
-
}
|
|
1143
|
-
function As(e, n, r = W(), t = []) {
|
|
1144
|
-
const s = rr(e, t), i = s && oe(s).find((c) => c.id === n);
|
|
1145
|
-
if (!i) return e;
|
|
1146
|
-
const a = new Set((e.shared ?? []).map((c) => c.name));
|
|
1147
|
-
let l = i.alias;
|
|
1148
|
-
for (let c = 2; a.has(l); c += 1) l = `${i.alias}_${c}`;
|
|
1149
|
-
const u = br(e, l, i.query, r, t[0]);
|
|
1150
|
-
return fn(u, t, (c) => c.from.query && c.from.id === n ? { ...c, from: { shared: r, alias: i.alias } } : {
|
|
1151
|
-
...c,
|
|
1152
|
-
joins: c.joins.map((m) => {
|
|
1153
|
-
if (m.id !== n || !m.query) return m;
|
|
1154
|
-
const { query: f, ...d } = m;
|
|
1155
|
-
return { ...d, shared: r };
|
|
1156
|
-
})
|
|
1157
|
-
});
|
|
1158
|
-
}
|
|
1159
|
-
function pt(e, n) {
|
|
1160
|
-
const { shared: r, ...t } = e;
|
|
1161
|
-
return "joinType" in t ? { ...t, query: n } : { id: W(), alias: t.alias, query: n };
|
|
1162
|
-
}
|
|
1163
|
-
function ks(e, n) {
|
|
1164
|
-
const r = new Map((e.shared ?? []).map((s) => [s.id, s.query])), t = n.reduce(
|
|
1165
|
-
(s, i) => typeof s == "object" && s !== null ? s[i] : void 0,
|
|
1166
|
-
e
|
|
1167
|
-
);
|
|
1168
|
-
return !gn(t) || !bn(t) ? e : De(e, (s) => {
|
|
1169
|
-
const i = r.get(s.shared);
|
|
1170
|
-
return s === t && i ? pt(s, i) : s;
|
|
1171
|
-
});
|
|
1172
|
-
}
|
|
1173
|
-
function xs(e, n, r) {
|
|
1174
|
-
var t;
|
|
1175
|
-
return !((t = e.shared) != null && t.some((s) => s.id === n)) || r === "" ? e : {
|
|
1176
|
-
...e,
|
|
1177
|
-
shared: e.shared.map((s) => s.id === n ? { ...s, name: r } : s)
|
|
1178
|
-
};
|
|
1179
|
-
}
|
|
1180
|
-
class wr extends Error {
|
|
1181
|
-
constructor(n) {
|
|
1182
|
-
super(`removeSharedQuery: "${n}" is a query's base: make it a sub query there instead`), this.name = "SharedBaseError";
|
|
1183
|
-
}
|
|
1184
|
-
}
|
|
1185
|
-
function Bs(e, n, r = "subQueries") {
|
|
1186
|
-
var u;
|
|
1187
|
-
const t = (u = e.shared) == null ? void 0 : u.find((c) => c.id === n);
|
|
1188
|
-
if (!t) return e;
|
|
1189
|
-
const s = e.shared.filter((c) => c !== t), { shared: i, ...a } = e, l = s.length > 0 ? { ...a, shared: s } : a;
|
|
1190
|
-
return De(l, (c) => {
|
|
1191
|
-
if (c.shared !== n) return c;
|
|
1192
|
-
if (r === "subQueries") return pt(c, t.query);
|
|
1193
|
-
if (!("joinType" in c)) throw new wr(t.name);
|
|
1194
|
-
});
|
|
1195
|
-
}
|
|
1196
|
-
const yt = (e) => e.query !== void 0 && !H(e.query);
|
|
1197
|
-
function gt(e) {
|
|
1198
|
-
const n = /* @__PURE__ */ new Map();
|
|
1199
|
-
return e.relations.forEach((r) => {
|
|
1200
|
-
n.set(r.fromTable, [...n.get(r.fromTable) ?? [], r.toTable]);
|
|
1201
|
-
}), n;
|
|
1202
|
-
}
|
|
1203
|
-
function ke(e, n, r = gt(e)) {
|
|
1204
|
-
const t = /* @__PURE__ */ new Set([n]), s = [n];
|
|
1205
|
-
for (let i = s.shift(); i !== void 0; i = s.shift())
|
|
1206
|
-
(r.get(i) ?? []).forEach((a) => {
|
|
1207
|
-
t.has(a) || (t.add(a), s.push(a));
|
|
1208
|
-
});
|
|
1209
|
-
return t;
|
|
1210
|
-
}
|
|
1211
|
-
const ye = (e) => e ? e.type === "logical" && e.operator === "AND" ? e.operands : [e] : [];
|
|
1212
|
-
function bt(e, n) {
|
|
1213
|
-
const r = new Map(F(e).map((t) => [D(t), t.table]));
|
|
1214
|
-
return ne(n).map((t) => ({
|
|
1215
|
-
ref: t,
|
|
1216
|
-
table: r.has(t) ? r.get(t) : t
|
|
1217
|
-
}));
|
|
1218
|
-
}
|
|
1219
|
-
const wn = (e, n) => bt(e, n).map((r) => r.table);
|
|
1220
|
-
function Jn(e, n) {
|
|
1221
|
-
const r = new Map(F(e).map((t) => [D(t), t.table]));
|
|
1222
|
-
return cn(n, (t) => {
|
|
1223
|
-
if (t.type !== "field" || t.table === void 0) return t;
|
|
1224
|
-
const s = r.get(t.table);
|
|
1225
|
-
return s === void 0 || s === t.table ? t : { ...t, table: s };
|
|
1226
|
-
});
|
|
1227
|
-
}
|
|
1228
|
-
function wt(e, n) {
|
|
1229
|
-
const r = (n.groupBy ?? []).flatMap((s) => bt(n, s.expression)), t = gt(e);
|
|
1230
|
-
return he(e).map((s) => {
|
|
1231
|
-
const i = ke(e, s.name, t), a = r.find((l) => l.table === void 0 || !i.has(l.table));
|
|
1232
|
-
return a ? { table: s, unlinked: a.table ?? a.ref } : { table: s };
|
|
1233
|
-
});
|
|
1234
|
-
}
|
|
1235
|
-
function $r(e, n, r) {
|
|
1236
|
-
const t = ke(e, r);
|
|
1237
|
-
return ye(n.where).filter((s) => {
|
|
1238
|
-
const i = wn(n, s);
|
|
1239
|
-
return i.length > 0 && i.every((a) => a !== void 0 && t.has(a));
|
|
1240
|
-
});
|
|
1241
|
-
}
|
|
1242
|
-
function vr(e, n, r) {
|
|
1243
|
-
const t = ke(e, r);
|
|
1244
|
-
return ye(n.where).some((s) => {
|
|
1245
|
-
const i = wn(n, s);
|
|
1246
|
-
return i.some((a) => a !== void 0 && t.has(a)) && i.some((a) => a === void 0 || !t.has(a));
|
|
1247
|
-
});
|
|
1248
|
-
}
|
|
1249
|
-
function je(e, n) {
|
|
1250
|
-
let r = e;
|
|
1251
|
-
for (let t = 2; n.has(r); t += 1) r = `${e}_${t}`;
|
|
1252
|
-
return r;
|
|
1253
|
-
}
|
|
1254
|
-
function tn(e, n) {
|
|
1255
|
-
if (e.type === "field" && e.column) return e.column;
|
|
1256
|
-
if (e.type === "function") {
|
|
1257
|
-
const r = e.args[0];
|
|
1258
|
-
return (r == null ? void 0 : r.type) === "field" && r.column ? r.column : e.name.toLowerCase() || n;
|
|
1259
|
-
}
|
|
1260
|
-
return n;
|
|
1261
|
-
}
|
|
1262
|
-
const rn = (e, n) => ({ type: "field", table: e, column: n });
|
|
1263
|
-
function Sr(e, n) {
|
|
1264
|
-
return {
|
|
1265
|
-
type: "logical",
|
|
1266
|
-
operator: "OR",
|
|
1267
|
-
operands: [
|
|
1268
|
-
{
|
|
1269
|
-
type: "binary",
|
|
1270
|
-
operator: "=",
|
|
1271
|
-
left: e,
|
|
1272
|
-
right: n
|
|
1273
|
-
},
|
|
1274
|
-
{
|
|
1275
|
-
type: "logical",
|
|
1276
|
-
operator: "AND",
|
|
1277
|
-
operands: [
|
|
1278
|
-
{ type: "binary", operator: "IS NULL", left: e },
|
|
1279
|
-
{ type: "binary", operator: "IS NULL", left: n }
|
|
1280
|
-
]
|
|
1281
|
-
}
|
|
1282
|
-
]
|
|
1283
|
-
};
|
|
1284
|
-
}
|
|
1285
|
-
function Er(e, n, r) {
|
|
1286
|
-
var i;
|
|
1287
|
-
if (r.type !== "field" || r.table === void 0 || r.column === void 0) return !1;
|
|
1288
|
-
const t = wn(n, r)[0], s = (i = Z(e, t)) == null ? void 0 : i.columns.find((a) => a.name === r.column);
|
|
1289
|
-
return s !== void 0 && !s.nullable;
|
|
1290
|
-
}
|
|
1291
|
-
function $t(e, n, r, t) {
|
|
1292
|
-
const s = n.groupBy.map((a) => a.expression), i = t.map((a, l) => Er(e, n, s[l]) ? {
|
|
1293
|
-
type: "binary",
|
|
1294
|
-
operator: "=",
|
|
1295
|
-
left: rn(r, a),
|
|
1296
|
-
right: s[l]
|
|
1297
|
-
} : Sr(rn(r, a), s[l]));
|
|
1298
|
-
return i.length === 1 ? i[0] : { type: "logical", operator: "AND", operands: i };
|
|
1299
|
-
}
|
|
1300
|
-
function vt(e, n, r, t, s, i) {
|
|
1301
|
-
var x;
|
|
1302
|
-
const a = n.groupBy.map((w) => Jn(n, w.expression)), l = $r(e, n, r).map((w) => Jn(n, w)), u = new Set(s.map((w) => w.alias).filter((w) => !!w)), c = a.map((w, C) => {
|
|
1303
|
-
const G = je(tn(w, `key_${C + 1}`), u);
|
|
1304
|
-
return u.add(G), G;
|
|
1305
|
-
}), m = [...l, ...t];
|
|
1306
|
-
let f = {
|
|
1307
|
-
id: "",
|
|
1308
|
-
from: { table: r },
|
|
1309
|
-
joins: [],
|
|
1310
|
-
select: []
|
|
1311
|
-
};
|
|
1312
|
-
[...a, ...m, ...s.map((w) => w.expression)].forEach((w) => {
|
|
1313
|
-
f = de(e, f, w);
|
|
1314
|
-
});
|
|
1315
|
-
const d = ((x = i == null ? void 0 : i.groupBy) == null ? void 0 : x.length) ?? 0, g = [...(i == null ? void 0 : i.joins) ?? []], E = f.joins.map((w) => {
|
|
1316
|
-
const C = g.findIndex((R) => R.table !== void 0 && R.table === w.table);
|
|
1317
|
-
if (C === -1) return w;
|
|
1318
|
-
const [G] = g.splice(C, 1);
|
|
1319
|
-
return { ...w, id: G.id };
|
|
1320
|
-
}), $ = {
|
|
1321
|
-
...i,
|
|
1322
|
-
from: f.from,
|
|
1323
|
-
joins: E,
|
|
1324
|
-
where: m.length > 0 ? { type: "logical", operator: "AND", operands: m } : void 0,
|
|
1325
|
-
groupBy: a.map((w, C) => {
|
|
1326
|
-
var G, R;
|
|
1327
|
-
return {
|
|
1328
|
-
id: ((R = (G = i == null ? void 0 : i.groupBy) == null ? void 0 : G[C]) == null ? void 0 : R.id) ?? W(),
|
|
1329
|
-
expression: w
|
|
1330
|
-
};
|
|
1331
|
-
}),
|
|
1332
|
-
select: [
|
|
1333
|
-
...a.map((w, C) => {
|
|
1334
|
-
var G;
|
|
1335
|
-
return {
|
|
1336
|
-
id: C < d && ((G = i == null ? void 0 : i.select[C]) == null ? void 0 : G.id) || W(),
|
|
1337
|
-
alias: c[C],
|
|
1338
|
-
expression: w
|
|
1339
|
-
};
|
|
1340
|
-
}),
|
|
1341
|
-
...s
|
|
1342
|
-
]
|
|
1343
|
-
};
|
|
1344
|
-
return $.where === void 0 && delete $.where, { query: $, copied: l.length, keyNames: c };
|
|
1345
|
-
}
|
|
1346
|
-
function Is(e, n, r) {
|
|
1347
|
-
var E;
|
|
1348
|
-
if ((n.groupBy ?? []).length === 0)
|
|
1349
|
-
throw new Error("addMeasure: the query isn't grouped (a measure is per group)");
|
|
1350
|
-
const t = wt(e, n).find(($) => $.table.name === r.table);
|
|
1351
|
-
if (!t) throw new Error(`addMeasure: unknown table "${r.table}"`);
|
|
1352
|
-
if (t.unlinked !== void 0)
|
|
1353
|
-
throw new Error(`addMeasure: "${r.table}" doesn't share the query's grouping`);
|
|
1354
|
-
const s = ye(r.where), i = n.joins.find(($) => {
|
|
1355
|
-
var x;
|
|
1356
|
-
return yt($) && ((x = $.drillAcross) == null ? void 0 : x.synced) === !0 && $.query.from.table === r.table && z(ye($.query.where).slice($.drillAcross.copied), s);
|
|
1357
|
-
});
|
|
1358
|
-
let { joins: a } = n, l, u;
|
|
1359
|
-
if (i) {
|
|
1360
|
-
l = i.alias;
|
|
1361
|
-
const $ = ((E = i.query.groupBy) == null ? void 0 : E.length) ?? 0, x = i.query.select.slice($).find((w) => z(w.expression, r.value));
|
|
1362
|
-
if (x)
|
|
1363
|
-
u = x.alias;
|
|
1364
|
-
else {
|
|
1365
|
-
u = je(
|
|
1366
|
-
tn(r.value, "value"),
|
|
1367
|
-
new Set(i.query.select.map((C) => C.alias ?? ""))
|
|
1368
|
-
);
|
|
1369
|
-
const w = { id: W(), alias: u, expression: r.value };
|
|
1370
|
-
a = a.map((C) => C === i ? { ...i, query: { ...i.query, select: [...i.query.select, w] } } : C);
|
|
1371
|
-
}
|
|
1372
|
-
} else {
|
|
1373
|
-
u = tn(r.value, "value");
|
|
1374
|
-
const $ = { id: W(), alias: u, expression: r.value }, x = vt(e, n, r.table, s, [$]), w = /* @__PURE__ */ new Set([
|
|
1375
|
-
...F(n).map(D),
|
|
1376
|
-
...he(e).map((R) => R.name)
|
|
1377
|
-
]), C = `${r.table}_per_${x.keyNames.join("_and_")}`.slice(0, Y - 4);
|
|
1378
|
-
l = je(C, w);
|
|
1379
|
-
const G = {
|
|
1380
|
-
id: W(),
|
|
1381
|
-
alias: l,
|
|
1382
|
-
joinType: "left",
|
|
1383
|
-
query: x.query,
|
|
1384
|
-
on: $t(e, n, l, x.keyNames),
|
|
1385
|
-
drillAcross: { synced: !0, copied: x.copied }
|
|
1386
|
-
};
|
|
1387
|
-
a = [...a, G];
|
|
1388
|
-
}
|
|
1389
|
-
const c = {
|
|
1390
|
-
type: "function",
|
|
1391
|
-
name: "MAX",
|
|
1392
|
-
args: [rn(l, u)]
|
|
1393
|
-
}, m = new Set(n.select.map(($) => $.alias).filter(($) => !!$)), f = je(`${r.table}_${u}`.slice(0, Y - 4), m);
|
|
1394
|
-
let d = r.selectId !== void 0 ? n.select.map(($) => $.id === r.selectId ? { ...$, expression: c, alias: $.alias || f } : $) : [...n.select, { id: W(), expression: c, alias: f }];
|
|
1395
|
-
return d = [...n.groupBy.filter(($) => !d.some((x) => z(x.expression, $.expression))).map(($) => ({ id: W(), expression: $.expression })), ...d], { ...n, joins: a, select: d };
|
|
1396
|
-
}
|
|
1397
|
-
function St(e, n, r) {
|
|
1398
|
-
var m, f;
|
|
1399
|
-
if (!yt(r)) return;
|
|
1400
|
-
const t = r, s = t.query.from.table, i = n.groupBy ?? [];
|
|
1401
|
-
if (s === void 0 || i.length === 0) return;
|
|
1402
|
-
const a = wt(e, n).find((d) => d.table.name === s);
|
|
1403
|
-
if (!a || a.unlinked !== void 0) return;
|
|
1404
|
-
const l = ((m = t.query.groupBy) == null ? void 0 : m.length) ?? 0, u = ye(t.query.where).slice(((f = t.drillAcross) == null ? void 0 : f.copied) ?? 0), c = vt(e, n, s, u, t.query.select.slice(l), t.query);
|
|
1405
|
-
return {
|
|
1406
|
-
...t,
|
|
1407
|
-
query: c.query,
|
|
1408
|
-
on: $t(e, n, t.alias, c.keyNames),
|
|
1409
|
-
drillAcross: { synced: !0, copied: c.copied }
|
|
1410
|
-
};
|
|
1411
|
-
}
|
|
1412
|
-
function Ge(e) {
|
|
1413
|
-
var t, s;
|
|
1414
|
-
const n = Fe(e.query), r = ((t = n.groupBy) == null ? void 0 : t.length) ?? 0;
|
|
1415
|
-
return {
|
|
1416
|
-
keys: (n.groupBy ?? []).map((i) => i.expression),
|
|
1417
|
-
keyColumns: n.select.slice(0, r).map((i) => [i.alias, i.expression]),
|
|
1418
|
-
copied: ye(n.where).slice(0, ((s = e.drillAcross) == null ? void 0 : s.copied) ?? 0),
|
|
1419
|
-
on: e.on
|
|
1420
|
-
};
|
|
1421
|
-
}
|
|
1422
|
-
function Tr(e, n, r) {
|
|
1423
|
-
const t = St(e, n, r);
|
|
1424
|
-
return t !== void 0 && z(Ge(t), Ge(r));
|
|
1425
|
-
}
|
|
1426
|
-
function Et(e, n, r) {
|
|
1427
|
-
let t = !1;
|
|
1428
|
-
const s = (l, u) => Je(
|
|
1429
|
-
u,
|
|
1430
|
-
(c, m) => Et(
|
|
1431
|
-
e,
|
|
1432
|
-
l && H(l) === H(u) ? Se(l)[m] : void 0,
|
|
1433
|
-
c
|
|
1434
|
-
)
|
|
1435
|
-
), i = r.from.query ? (() => {
|
|
1436
|
-
const l = n != null && n.from.query && n.from.id === r.from.id ? n.from.query : void 0, u = s(l, r.from.query);
|
|
1437
|
-
return u === r.from.query ? r.from : { ...r.from, query: u };
|
|
1438
|
-
})() : r.from;
|
|
1439
|
-
i !== r.from && (t = !0);
|
|
1440
|
-
const a = r.joins.map((l) => {
|
|
1441
|
-
var f;
|
|
1442
|
-
if (!l.query) return l;
|
|
1443
|
-
const u = n == null ? void 0 : n.joins.find((d) => d.id === l.id && d.query !== void 0), c = s(u == null ? void 0 : u.query, l.query);
|
|
1444
|
-
let m = c === l.query ? l : { ...l, query: c };
|
|
1445
|
-
if ((f = m.drillAcross) != null && f.synced) {
|
|
1446
|
-
const d = St(e, r, m);
|
|
1447
|
-
d && !z(d, m) && (m = d);
|
|
1448
|
-
} else if (m.drillAcross && (u != null && u.drillAcross) && !z(Ge(u), Ge(m))) {
|
|
1449
|
-
const d = { ...m };
|
|
1450
|
-
delete d.drillAcross, m = d;
|
|
1451
|
-
}
|
|
1452
|
-
return m !== l && (t = !0), m;
|
|
1453
|
-
});
|
|
1454
|
-
return t ? { ...r, from: i, joins: a } : r;
|
|
1455
|
-
}
|
|
1456
|
-
function Nr(e, n, r) {
|
|
1457
|
-
return ce(r) ? Et(e, n && ce(n) ? n : void 0, r) : r;
|
|
1458
|
-
}
|
|
1459
|
-
const Cs = ["text", "number", "date", "boolean"], Ar = {
|
|
1460
|
-
varchar: "text",
|
|
1461
|
-
integer: "integer",
|
|
1462
|
-
decimal: "number",
|
|
1463
|
-
date: "date",
|
|
1464
|
-
dateTime: "dateTime",
|
|
1465
|
-
time: "time",
|
|
1466
|
-
boolean: "boolean"
|
|
1467
|
-
};
|
|
1468
|
-
function Tt(e) {
|
|
1469
|
-
return Ar[e];
|
|
1470
|
-
}
|
|
1471
|
-
function kr(e) {
|
|
1472
|
-
const n = e.trim().toUpperCase();
|
|
1473
|
-
if (/^((TINY|SMALL|MEDIUM|BIG)?INT|SIGNED|UNSIGNED)/.test(n)) return "integer";
|
|
1474
|
-
if (/^(DECIMAL|NUMERIC|FLOAT|DOUBLE|REAL|MONEY)/.test(n)) return "number";
|
|
1475
|
-
if (/^(DATETIME|TIMESTAMP|SMALLDATETIME)/.test(n)) return "dateTime";
|
|
1476
|
-
if (n.startsWith("DATE")) return "date";
|
|
1477
|
-
if (n.startsWith("TIME")) return "time";
|
|
1478
|
-
if (/^BOOL|^BIT$/.test(n)) return "boolean";
|
|
1479
|
-
if (/^(N?VAR)?CHAR|^N?TEXT|^STRING/.test(n)) return "text";
|
|
1480
|
-
}
|
|
1481
|
-
function Re(e) {
|
|
1482
|
-
if (e.as) return e.as;
|
|
1483
|
-
const { value: n } = e;
|
|
1484
|
-
if (typeof n == "string") return n === "" ? void 0 : "text";
|
|
1485
|
-
if (typeof n == "number") return Number.isInteger(n) ? "integer" : "number";
|
|
1486
|
-
if (typeof n == "bigint") return "integer";
|
|
1487
|
-
if (typeof n == "boolean") return "boolean";
|
|
1488
|
-
if (n instanceof Date) return et(n) ? "date" : "dateTime";
|
|
1489
|
-
}
|
|
1490
|
-
function $n(e, n, r) {
|
|
1491
|
-
if (!e) return;
|
|
1492
|
-
const t = (s) => $n(s, n, r);
|
|
1493
|
-
switch (e.type) {
|
|
1494
|
-
case "field":
|
|
1495
|
-
return n(e);
|
|
1496
|
-
case "literal":
|
|
1497
|
-
return Re(e);
|
|
1498
|
-
case "function": {
|
|
1499
|
-
const s = zn(e.name, r), i = (s == null ? void 0 : s.args.findIndex((u) => u.kind === "type-name")) ?? -1, a = i >= 0 ? e.args[i] : void 0;
|
|
1500
|
-
if ((a == null ? void 0 : a.type) === "literal" && typeof a.value == "string")
|
|
1501
|
-
return kr(a.value);
|
|
1502
|
-
const l = s == null ? void 0 : s.returns;
|
|
1503
|
-
return l === "arg" ? t(e.args[0]) : l;
|
|
1504
|
-
}
|
|
1505
|
-
case "if":
|
|
1506
|
-
return t(e.then) ?? t(e.else);
|
|
1507
|
-
case "case":
|
|
1508
|
-
return [...e.branches.map((s) => s.then), e.else].map(t).find((s) => s !== void 0);
|
|
1509
|
-
default:
|
|
1510
|
-
return;
|
|
1511
|
-
}
|
|
1512
|
-
}
|
|
1513
|
-
const xr = /^[+-]?(\d+\.?\d*|\.\d+)(e[+-]?\d+)?$/i, Br = /^([01]\d|2[0-3]):([0-5]\d)(?::([0-5]\d))?$/, Ir = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2}))?)?$/;
|
|
1514
|
-
function vn(e, n) {
|
|
1515
|
-
if (e === "" || e === null) return e;
|
|
1516
|
-
const r = typeof e == "string" ? e.trim() : void 0;
|
|
1517
|
-
switch (n) {
|
|
1518
|
-
case "text":
|
|
1519
|
-
return e instanceof Date ? Yt(e) : String(e);
|
|
1520
|
-
case "integer":
|
|
1521
|
-
case "number": {
|
|
1522
|
-
let t;
|
|
1523
|
-
return typeof e == "number" ? t = e : typeof e == "bigint" ? t = Number(e) : r !== void 0 && xr.test(r) && (t = Number(r)), t === void 0 || !Ze(t) || n === "integer" && !Number.isInteger(t) ? void 0 : t;
|
|
1524
|
-
}
|
|
1525
|
-
case "date":
|
|
1526
|
-
case "dateTime": {
|
|
1527
|
-
if (e instanceof Date) return n === "date" && !et(e) ? void 0 : e;
|
|
1528
|
-
const t = r === void 0 ? null : Ir.exec(r);
|
|
1529
|
-
if (!t) return;
|
|
1530
|
-
const [, s, i, a, l, u, c] = t.map(Number), m = Kt(s, i, a);
|
|
1531
|
-
return m.getUTCMonth() !== i - 1 || m.getUTCDate() !== a ? void 0 : t[4] === void 0 ? m : n === "date" ? void 0 : new Date(Date.UTC(s, i - 1, a, l, u, Number.isNaN(c) ? 0 : c));
|
|
1532
|
-
}
|
|
1533
|
-
case "time": {
|
|
1534
|
-
const t = r === void 0 ? null : Br.exec(r);
|
|
1535
|
-
return t ? `${t[1]}:${t[2]}:${t[3] ?? "00"}` : void 0;
|
|
1536
|
-
}
|
|
1537
|
-
case "boolean":
|
|
1538
|
-
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;
|
|
1539
|
-
/* v8 ignore start -- ValueType is a closed union, every case above */
|
|
1540
|
-
default:
|
|
1541
|
-
return;
|
|
1542
|
-
}
|
|
1543
|
-
}
|
|
1544
|
-
function Cr(e, n, r) {
|
|
1545
|
-
var m;
|
|
1546
|
-
if (n === void 0) return e;
|
|
1547
|
-
const { as: t, empty: s, ...i } = e;
|
|
1548
|
-
let a = vn(e.value, n);
|
|
1549
|
-
r != null && r.length && a !== "" && a !== null && (a = (m = r.find((f) => String(f.value) === String(e.value))) == null ? void 0 : m.value);
|
|
1550
|
-
const l = s && n === "text", u = a === void 0 ? { type: "literal", value: "" } : { ...i, value: a, ...l ? { empty: s } : {} };
|
|
1551
|
-
return t === void 0 && !!s == !!l && a === e.value ? e : u;
|
|
1552
|
-
}
|
|
1553
|
-
function Ms(e, n, r, t = !1) {
|
|
1554
|
-
const s = e.items.flatMap((a) => {
|
|
1555
|
-
if (a.type !== "literal") return [a];
|
|
1556
|
-
const l = Cr(a, n, r);
|
|
1557
|
-
return l.value === "" && !l.empty && !t ? [] : [l];
|
|
1558
|
-
});
|
|
1559
|
-
return s.length === e.items.length && s.every((a, l) => a === e.items[l]) ? e : { ...e, items: s };
|
|
1560
|
-
}
|
|
1561
|
-
function Os(e) {
|
|
1562
|
-
const n = Re(e);
|
|
1563
|
-
return n === "integer" || n === "number" ? "number" : n === "date" || n === "dateTime" ? "date" : n;
|
|
1564
|
-
}
|
|
1565
|
-
function Us(e, n) {
|
|
1566
|
-
const t = (e.type === "literal" ? vn(e.value, n) : void 0) ?? "", s = e.type === "literal" && e.empty && n === "text";
|
|
1567
|
-
return {
|
|
1568
|
-
type: "literal",
|
|
1569
|
-
value: t,
|
|
1570
|
-
as: n,
|
|
1571
|
-
...s ? { empty: s } : {}
|
|
1572
|
-
};
|
|
1573
|
-
}
|
|
1574
|
-
function Ls(e, n = !1) {
|
|
1575
|
-
if (e !== void 0)
|
|
1576
|
-
return e === "text" ? [
|
|
1577
|
-
"=",
|
|
1578
|
-
"!=",
|
|
1579
|
-
"LIKE",
|
|
1580
|
-
"REGEXP",
|
|
1581
|
-
"IN",
|
|
1582
|
-
...n ? ["BETWEEN"] : [],
|
|
1583
|
-
"IS NULL",
|
|
1584
|
-
"IS NOT NULL"
|
|
1585
|
-
] : e === "boolean" ? ["=", "!=", "IS NULL", "IS NOT NULL"] : ["=", "!=", "<", "<=", ">", ">=", "IN", "BETWEEN", "IS NULL", "IS NOT NULL"];
|
|
1586
|
-
}
|
|
1587
|
-
const Nt = "source", At = "rows_", ie = (e) => e ? { type: "literal", value: null, as: e } : { type: "literal", value: null }, Te = (e) => e.type === "literal" && e.value === null, Sn = (e) => new Map(
|
|
1588
|
-
F(e).map((n) => [D(n), n.table])
|
|
1589
|
-
);
|
|
1590
|
-
function sn(e, n) {
|
|
1591
|
-
const r = Sn(e);
|
|
1592
|
-
return ne(n).map((t) => r.get(t) ?? t);
|
|
1593
|
-
}
|
|
1594
|
-
function kt(e, n) {
|
|
1595
|
-
const r = Sn(e);
|
|
1596
|
-
return cn(n, (t) => {
|
|
1597
|
-
if (t.type !== "field" || t.table === void 0) return t;
|
|
1598
|
-
const s = r.get(t.table);
|
|
1599
|
-
return s === void 0 || s === t.table ? t : { ...t, table: s };
|
|
1600
|
-
});
|
|
1601
|
-
}
|
|
1602
|
-
function Qe(e, n, r) {
|
|
1603
|
-
const t = Sn(n);
|
|
1604
|
-
return $n(r, (s) => {
|
|
1605
|
-
var l;
|
|
1606
|
-
const i = t.get(s.table ?? "") ?? s.table ?? "", a = (l = Z(e, i)) == null ? void 0 : l.columns.find((u) => u.name === s.column);
|
|
1607
|
-
return a && Tt(a.type);
|
|
1608
|
-
}, ge);
|
|
1609
|
-
}
|
|
1610
|
-
function En(e, n) {
|
|
1611
|
-
let r = e;
|
|
1612
|
-
for (let t = 2; n.has(r); t += 1) r = `${e}_${t}`;
|
|
1613
|
-
return r;
|
|
1614
|
-
}
|
|
1615
|
-
const qe = (e) => e ? e.type === "logical" && e.operator === "AND" ? e.operands : [e] : [], xt = (e) => {
|
|
1616
|
-
if (e.length !== 0)
|
|
1617
|
-
return e.length === 1 ? e[0] : { type: "logical", operator: "AND", operands: e };
|
|
1618
|
-
}, ae = (e) => e.from.table, Pn = (e, n) => Z(e, n).label;
|
|
1619
|
-
function Ve(e) {
|
|
1620
|
-
const { from: n } = e;
|
|
1621
|
-
return n.query && H(n.query) ? n : void 0;
|
|
1622
|
-
}
|
|
1623
|
-
function xe(e, n) {
|
|
1624
|
-
if (n === void 0) return Ve(e);
|
|
1625
|
-
const r = oe(e).find((t) => t.id === n);
|
|
1626
|
-
return r && H(r.query) ? r : void 0;
|
|
1627
|
-
}
|
|
1628
|
-
function Be(e, n, r) {
|
|
1629
|
-
const t = { ...n, query: r };
|
|
1630
|
-
return n === e.from ? { ...e, from: t } : { ...e, joins: e.joins.map((s) => s === n ? t : s) };
|
|
1631
|
-
}
|
|
1632
|
-
function We(e, n) {
|
|
1633
|
-
const [r] = n;
|
|
1634
|
-
return r.select.map((t, s) => {
|
|
1635
|
-
var f;
|
|
1636
|
-
const i = K(t) ?? "", a = n.map((d) => {
|
|
1637
|
-
var g;
|
|
1638
|
-
return { branch: d, expression: (g = d.select[s]) == null ? void 0 : g.expression };
|
|
1639
|
-
}).filter((d) => d.expression !== void 0 && !Te(d.expression)), u = ((f = n.map((d) => {
|
|
1640
|
-
var g;
|
|
1641
|
-
return (g = d.select[s]) == null ? void 0 : g.expression;
|
|
1642
|
-
}).filter(
|
|
1643
|
-
(d) => (d == null ? void 0 : d.type) === "literal" && d.value === null
|
|
1644
|
-
).find((d) => d.as)) == null ? void 0 : f.as) ?? (a[0] && Qe(e, a[0].branch, a[0].expression));
|
|
1645
|
-
if (i === Nt && a.length === n.length && a.every((d) => d.expression.type === "literal"))
|
|
1646
|
-
return {
|
|
1647
|
-
name: i,
|
|
1648
|
-
kind: "source",
|
|
1649
|
-
tables: [],
|
|
1650
|
-
type: "text"
|
|
1651
|
-
};
|
|
1652
|
-
if (i.startsWith(At) && a.length === 1 && a[0].expression.type === "literal")
|
|
1653
|
-
return {
|
|
1654
|
-
name: i,
|
|
1655
|
-
kind: "rows",
|
|
1656
|
-
tables: [],
|
|
1657
|
-
type: "integer"
|
|
1658
|
-
};
|
|
1659
|
-
const c = a[0];
|
|
1660
|
-
if (!c)
|
|
1661
|
-
return {
|
|
1662
|
-
name: i,
|
|
1663
|
-
kind: "shared",
|
|
1664
|
-
tables: [],
|
|
1665
|
-
type: u
|
|
1666
|
-
};
|
|
1667
|
-
const m = sn(c.branch, c.expression);
|
|
1668
|
-
return a.some((d) => sn(d.branch, d.expression).includes(ae(d.branch))) ? {
|
|
1669
|
-
name: i,
|
|
1670
|
-
kind: "own",
|
|
1671
|
-
tables: m,
|
|
1672
|
-
type: u
|
|
1673
|
-
} : {
|
|
1674
|
-
name: i,
|
|
1675
|
-
kind: "shared",
|
|
1676
|
-
tables: m,
|
|
1677
|
-
type: u,
|
|
1678
|
-
expression: kt(c.branch, c.expression)
|
|
1679
|
-
};
|
|
1680
|
-
});
|
|
1681
|
-
}
|
|
1682
|
-
function js(e, n) {
|
|
1683
|
-
const r = Ve(n);
|
|
1684
|
-
if (!r) return [];
|
|
1685
|
-
const { union: t } = r.query;
|
|
1686
|
-
return We(e, t).map((s, i) => {
|
|
1687
|
-
const a = t.map((l) => !Te(l.select[i].expression));
|
|
1688
|
-
return {
|
|
1689
|
-
name: s.name,
|
|
1690
|
-
kind: s.kind,
|
|
1691
|
-
filledBy: t.filter((l, u) => a[u]).map(ae),
|
|
1692
|
-
nullIn: t.filter((l, u) => !a[u]).map(ae)
|
|
1693
|
-
};
|
|
1694
|
-
});
|
|
1695
|
-
}
|
|
1696
|
-
function Bt(e, n, r, t, s) {
|
|
1697
|
-
const i = ke(e, n), a = r.filter((m) => m.kind === "shared" && m.expression), l = (m) => m.tables.every((f) => i.has(f));
|
|
1698
|
-
if (!a.some(l)) return `"${n}" shares no column with the union`;
|
|
1699
|
-
const u = a.find((m) => !l(m));
|
|
1700
|
-
if (u && s === "refuse")
|
|
1701
|
-
return `"${n}" can't reach column "${u.name}" (${u.tables.join(", ")})`;
|
|
1702
|
-
const c = t.find((m) => !m.tables.every((f) => i.has(f)));
|
|
1703
|
-
if (c)
|
|
1704
|
-
return `"${n}" can't follow a condition on ${c.tables.join(", ")}`;
|
|
1705
|
-
}
|
|
1706
|
-
function It(e) {
|
|
1707
|
-
const [n] = e, r = new Set(e.map(ae));
|
|
1708
|
-
return qe(n.where).flatMap((t) => {
|
|
1709
|
-
const s = sn(n, t), i = s.filter((a) => r.has(a));
|
|
1710
|
-
if (i.length === 0) return [{ operand: kt(n, t), tables: s }];
|
|
1711
|
-
if (i.length === s.length) return [];
|
|
1712
|
-
throw new Error("a condition naming both a branch's own table and shared ones can't be placed: split it, or put it on the union instead");
|
|
1713
|
-
});
|
|
1714
|
-
}
|
|
1715
|
-
function Vn(e, n, r, t) {
|
|
1716
|
-
const s = t.unshared ?? "refuse", i = We(e, n), a = It(n);
|
|
1717
|
-
if (!Z(e, r.table))
|
|
1718
|
-
throw new Error(`addUnionTable: unknown table "${r.table}"`);
|
|
1719
|
-
if (n.some((k) => ae(k) === r.table))
|
|
1720
|
-
throw new Error(`addUnionTable: "${r.table}" is in the union already`);
|
|
1721
|
-
const l = Bt(e, r.table, i, a, s);
|
|
1722
|
-
if (l) throw new Error(`addUnionTable: ${l}`);
|
|
1723
|
-
const u = ke(e, r.table), c = r.label ?? Pn(e, r.table), m = i.map((k) => k.kind === "source" ? { type: "literal", value: c } : k.kind === "shared" && k.expression && k.tables.every((J) => u.has(J)) ? k.expression : ie(k.type)), f = new Set(i.map((k) => k.name)), d = [], g = (k) => {
|
|
1724
|
-
const J = En(k, f);
|
|
1725
|
-
return f.add(J), d.push(J), J;
|
|
1726
|
-
}, E = [], $ = {
|
|
1727
|
-
from: { table: r.table },
|
|
1728
|
-
joins: []
|
|
1729
|
-
};
|
|
1730
|
-
r.columns.forEach((k) => {
|
|
1731
|
-
const J = k.alias ?? (k.expression.type === "field" ? k.expression.column : void 0) ?? "value", He = Qe(e, $, k.expression);
|
|
1732
|
-
E.push({ name: g(J), own: k.expression, others: () => ie(He) });
|
|
1733
|
-
}), t.source && !i.some((k) => k.kind === "source") && E.push({
|
|
1734
|
-
name: g(Nt),
|
|
1735
|
-
own: { type: "literal", value: c },
|
|
1736
|
-
others: (k) => ({ type: "literal", value: Pn(e, ae(k)) })
|
|
1737
|
-
});
|
|
1738
|
-
const x = (k) => `${At}${k}`;
|
|
1739
|
-
(t.rowMarkers || i.some((k) => k.kind === "rows")) && (n.filter((k) => !f.has(x(ae(k)))).forEach((k) => {
|
|
1740
|
-
E.push({
|
|
1741
|
-
name: g(x(ae(k))),
|
|
1742
|
-
own: ie("integer"),
|
|
1743
|
-
others: (J) => J === k ? { type: "literal", value: 1 } : ie("integer")
|
|
1744
|
-
});
|
|
1745
|
-
}), E.push({
|
|
1746
|
-
name: g(x(r.table)),
|
|
1747
|
-
own: { type: "literal", value: 1 },
|
|
1748
|
-
others: () => ie("integer")
|
|
1749
|
-
}));
|
|
1750
|
-
const w = (k, J) => ({
|
|
1751
|
-
id: W(),
|
|
1752
|
-
alias: k,
|
|
1753
|
-
expression: J
|
|
1754
|
-
}), C = n.map((k) => ({
|
|
1755
|
-
...k,
|
|
1756
|
-
select: [...k.select, ...E.map((J) => w(J.name, J.others(k)))]
|
|
1757
|
-
})), G = xt([...a.map((k) => k.operand), ...qe(r.where)]);
|
|
1758
|
-
let R = {
|
|
1759
|
-
id: "",
|
|
1760
|
-
from: { table: r.table },
|
|
1761
|
-
joins: [],
|
|
1762
|
-
...G ? { where: G } : {},
|
|
1763
|
-
select: [
|
|
1764
|
-
...i.map((k, J) => w(k.name, m[J])),
|
|
1765
|
-
...E.map((k) => w(k.name, k.own))
|
|
1766
|
-
]
|
|
1767
|
-
};
|
|
1768
|
-
[G, ...R.select.map((k) => k.expression)].forEach((k) => {
|
|
1769
|
-
R = de(e, R, k);
|
|
1770
|
-
});
|
|
1771
|
-
const {
|
|
1772
|
-
from: X,
|
|
1773
|
-
joins: ee,
|
|
1774
|
-
select: be,
|
|
1775
|
-
where: te
|
|
1776
|
-
} = R, re = {
|
|
1777
|
-
from: X,
|
|
1778
|
-
joins: ee,
|
|
1779
|
-
select: be,
|
|
1780
|
-
...te ? { where: te } : {}
|
|
1781
|
-
};
|
|
1782
|
-
return { union: [...C, re], added: d };
|
|
1783
|
-
}
|
|
1784
|
-
function Ct(e) {
|
|
1785
|
-
const n = (s) => new Error(`addUnionTable: ${s}`);
|
|
1786
|
-
if (e.from.query) throw n("the query reads from a derived table");
|
|
1787
|
-
if (e.select.length === 0) throw n("the query picks no columns (SELECT *)");
|
|
1788
|
-
if ((e.groupBy ?? []).length > 0 || e.having || e.distinct || (e.orderBy ?? []).length > 0 || e.select.some((s) => ue(s.expression, ge)))
|
|
1789
|
-
throw n("a union is of detail rows: no aggregates, GROUP BY, HAVING, DISTINCT or ORDER BY");
|
|
1790
|
-
const r = /* @__PURE__ */ new Set(), t = e.select.map((s, i) => {
|
|
1791
|
-
const a = En(K(s) ?? `column_${i + 1}`, r);
|
|
1792
|
-
return r.add(a), { ...s, alias: a };
|
|
1793
|
-
});
|
|
1794
|
-
return {
|
|
1795
|
-
from: e.from,
|
|
1796
|
-
joins: e.joins,
|
|
1797
|
-
...e.where ? { where: e.where } : {},
|
|
1798
|
-
select: t
|
|
1799
|
-
};
|
|
1800
|
-
}
|
|
1801
|
-
function Ds(e, n, r, t = {}) {
|
|
1802
|
-
const s = Ve(n);
|
|
1803
|
-
if (s) {
|
|
1804
|
-
const { union: c, added: m } = Vn(e, s.query.union, r, t), f = { ...s, query: { ...s.query, union: c } }, d = n.select.length === 0 ? [] : [
|
|
1805
|
-
...n.select,
|
|
1806
|
-
...m.map((g) => ({
|
|
1807
|
-
id: W(),
|
|
1808
|
-
expression: { type: "field", table: s.alias, column: g }
|
|
1809
|
-
}))
|
|
1810
|
-
];
|
|
1811
|
-
return { ...n, from: f, select: d };
|
|
1812
|
-
}
|
|
1813
|
-
const i = Ct(n), { union: a } = Vn(e, [i], r, t), l = t.alias ?? "stacked", u = a[0].select.map((c) => c.alias);
|
|
1814
|
-
return {
|
|
1815
|
-
id: n.id,
|
|
1816
|
-
from: { id: W(), alias: l, query: { union: a, all: !0 } },
|
|
1817
|
-
joins: [],
|
|
1818
|
-
select: u.map((c) => ({
|
|
1819
|
-
id: W(),
|
|
1820
|
-
expression: { type: "field", table: l, column: c }
|
|
1821
|
-
})),
|
|
1822
|
-
...n.limit !== void 0 ? { limit: n.limit } : {},
|
|
1823
|
-
...n.offset !== void 0 ? { offset: n.offset } : {}
|
|
1824
|
-
};
|
|
1825
|
-
}
|
|
1826
|
-
function Gs(e, n, r = {}) {
|
|
1827
|
-
const t = Ve(n);
|
|
1828
|
-
let s;
|
|
1829
|
-
try {
|
|
1830
|
-
s = t ? t.query.union : [Ct(n)];
|
|
1831
|
-
} catch {
|
|
1832
|
-
return [];
|
|
1833
|
-
}
|
|
1834
|
-
const i = We(e, s);
|
|
1835
|
-
let a;
|
|
1836
|
-
try {
|
|
1837
|
-
a = It(s);
|
|
1838
|
-
} catch {
|
|
1839
|
-
return [];
|
|
1840
|
-
}
|
|
1841
|
-
const l = /* @__PURE__ */ new Set([
|
|
1842
|
-
...s.map(ae),
|
|
1843
|
-
...i.flatMap((u) => u.kind === "shared" ? u.tables : [])
|
|
1844
|
-
]);
|
|
1845
|
-
return e.tables.map((u) => u.name).filter((u) => !l.has(u) && Bt(e, u, i, a, r.unshared ?? "refuse") === void 0);
|
|
1846
|
-
}
|
|
1847
|
-
function Mr(e, n, r) {
|
|
1848
|
-
const t = r.union.findIndex((u, c) => u.select !== n.union[c].select);
|
|
1849
|
-
if (t === -1) return r;
|
|
1850
|
-
const s = r.union[t], i = n.union[t].select.map((u) => u.id), a = r.union.map((u, c) => {
|
|
1851
|
-
if (c === t) return u;
|
|
1852
|
-
const m = s.select.map((d) => {
|
|
1853
|
-
const g = K(d), E = Qe(e, s, d.expression), $ = i.indexOf(d.id), x = $ === -1 ? void 0 : u.select[$];
|
|
1854
|
-
if (!x)
|
|
1855
|
-
return { id: W(), ...g ? { alias: g } : {}, expression: ie(E) };
|
|
1856
|
-
let w = x;
|
|
1857
|
-
K(x) !== g && (w = { ...w, alias: g });
|
|
1858
|
-
const { expression: C } = x;
|
|
1859
|
-
return Te(C) && C.type === "literal" && C.as !== E && (w = { ...w, expression: ie(E) }), w;
|
|
1860
|
-
});
|
|
1861
|
-
return m.length === u.select.length && m.every((d, g) => d === u.select[g]) ? u : { ...u, select: m };
|
|
1862
|
-
}), l = a.map((u) => {
|
|
1863
|
-
const c = u.select.map((m, f) => {
|
|
1864
|
-
const { expression: d } = m;
|
|
1865
|
-
if (d.type !== "literal" || d.value !== null) return m;
|
|
1866
|
-
const g = a.find(($) => {
|
|
1867
|
-
var w;
|
|
1868
|
-
const x = (w = $.select[f]) == null ? void 0 : w.expression;
|
|
1869
|
-
return x !== void 0 && !Te(x);
|
|
1870
|
-
}), E = g && Qe(e, g, g.select[f].expression);
|
|
1871
|
-
return E && d.as !== E ? { ...m, expression: ie(E) } : m;
|
|
1872
|
-
});
|
|
1873
|
-
return c.every((m, f) => m === u.select[f]) ? u : { ...u, select: c };
|
|
1874
|
-
});
|
|
1875
|
-
return l.every((u, c) => u === r.union[c]) ? r : { ...r, union: l };
|
|
1876
|
-
}
|
|
1877
|
-
function Or(e, n) {
|
|
1878
|
-
const r = (i) => i.union[0].select.map((a) => K(a)), t = r(e), s = r(n);
|
|
1879
|
-
return t.length !== s.length ? [] : t.flatMap((i, a) => {
|
|
1880
|
-
const l = s[a];
|
|
1881
|
-
return i && l && i !== l && !s.includes(i) && !t.includes(l) ? [{ from: i, to: l }] : [];
|
|
1882
|
-
});
|
|
1883
|
-
}
|
|
1884
|
-
function Mt(e, n, r) {
|
|
1885
|
-
let t = r;
|
|
1886
|
-
n && H(n) && H(t) && n.union.length === t.union.length && (t = Mr(e, n, t));
|
|
1887
|
-
const s = t;
|
|
1888
|
-
return Je(s, (i, a) => Tn(
|
|
1889
|
-
e,
|
|
1890
|
-
n && H(n) === H(s) ? Se(n)[a] : void 0,
|
|
1891
|
-
i
|
|
1892
|
-
));
|
|
1893
|
-
}
|
|
1894
|
-
function Tn(e, n, r) {
|
|
1895
|
-
const t = (d) => {
|
|
1896
|
-
var g;
|
|
1897
|
-
return Mt(
|
|
1898
|
-
e,
|
|
1899
|
-
n && ((g = oe(n).find((E) => E.id === d.id)) == null ? void 0 : g.query),
|
|
1900
|
-
d.query
|
|
1901
|
-
);
|
|
1902
|
-
}, { from: s } = r, i = s.query && t(s), a = i && i !== s.query ? { ...s, query: i } : s;
|
|
1903
|
-
let l = !1;
|
|
1904
|
-
const u = r.joins.map((d) => {
|
|
1905
|
-
if (!d.query) return d;
|
|
1906
|
-
const g = t(d);
|
|
1907
|
-
return g === d.query ? d : (l = !0, { ...d, query: g });
|
|
1908
|
-
});
|
|
1909
|
-
if (a === s && !l) return r;
|
|
1910
|
-
const c = { ...r, from: a, joins: u }, m = oe(c).flatMap((d) => {
|
|
1911
|
-
var E;
|
|
1912
|
-
const g = n && ((E = oe(n).find(($) => $.id === d.id)) == null ? void 0 : E.query);
|
|
1913
|
-
return g && H(g) && H(d.query) ? Or(g, d.query).map(($) => ({ ...$, alias: d.alias })) : [];
|
|
1914
|
-
});
|
|
1915
|
-
if (m.length === 0) return c;
|
|
1916
|
-
const f = (d) => {
|
|
1917
|
-
if (d.type !== "field") return Ne(d, f);
|
|
1918
|
-
const g = m.find((E) => E.alias === d.table && E.from === d.column);
|
|
1919
|
-
return g ? { ...d, column: g.to } : d;
|
|
1920
|
-
};
|
|
1921
|
-
return _e(c, f, (d) => d);
|
|
1922
|
-
}
|
|
1923
|
-
function Ur(e, n, r) {
|
|
1924
|
-
if (!ce(r)) return r;
|
|
1925
|
-
const t = n && ce(n) ? n : void 0;
|
|
1926
|
-
return un(Tn(e, t, r), (s, i) => {
|
|
1927
|
-
var a, l;
|
|
1928
|
-
return Mt(
|
|
1929
|
-
e,
|
|
1930
|
-
(l = (a = t == null ? void 0 : t.shared) == null ? void 0 : a.find((u) => u.id === i.id)) == null ? void 0 : l.query,
|
|
1931
|
-
s
|
|
1932
|
-
);
|
|
1933
|
-
});
|
|
1934
|
-
}
|
|
1935
|
-
function Ot(e) {
|
|
1936
|
-
return e.type === "field" ? [e] : me(e).flatMap(Ot);
|
|
1937
|
-
}
|
|
1938
|
-
const Ut = (e, n, r) => Ot(e).some((t) => t.table === n && r.has(t.column ?? ""));
|
|
1939
|
-
function Lr(e, n, r) {
|
|
1940
|
-
const t = n.query.union.filter((u, c) => c !== r), s = [
|
|
1941
|
-
e.where,
|
|
1942
|
-
e.having,
|
|
1943
|
-
...e.joins.map((u) => u.on),
|
|
1944
|
-
...(e.groupBy ?? []).map((u) => u.expression),
|
|
1945
|
-
...(e.orderBy ?? []).map((u) => u.expression)
|
|
1946
|
-
].filter((u) => u !== void 0), i = (u) => s.some(
|
|
1947
|
-
(c) => Ut(c, n.alias, /* @__PURE__ */ new Set([u]))
|
|
1948
|
-
), a = new Set(t[0].select.flatMap((u, c) => {
|
|
1949
|
-
const m = t.every((d) => d.select[c] && Te(d.select[c].expression)), f = K(u);
|
|
1950
|
-
return m && !(f && i(f)) ? [c] : [];
|
|
1951
|
-
})), l = new Set([...a].flatMap((u) => {
|
|
1952
|
-
const c = K(t[0].select[u]);
|
|
1953
|
-
return c ? [c] : [];
|
|
1954
|
-
}));
|
|
1955
|
-
return { at: a, names: l };
|
|
1956
|
-
}
|
|
1957
|
-
function Rs(e, n, r) {
|
|
1958
|
-
const t = xe(e, r);
|
|
1959
|
-
if (!t || !t.query.union[n] || t.query.union.length < 2) return e;
|
|
1960
|
-
const s = Lr(e, t, n), i = t.query.union.filter((c, m) => m !== n), a = s.at.size < i[0].select.length ? s : { at: /* @__PURE__ */ new Set(), names: /* @__PURE__ */ new Set() }, l = e.select.filter(
|
|
1961
|
-
(c) => !Ut(c.expression, t.alias, a.names)
|
|
1962
|
-
), u = i.map((c) => ({ ...c, select: c.select.filter((m, f) => !a.at.has(f)) }));
|
|
1963
|
-
return Be({ ...e, select: l }, t, { ...t.query, union: u });
|
|
1964
|
-
}
|
|
1965
|
-
function jr(e, n) {
|
|
1966
|
-
const r = /* @__PURE__ */ new Set([
|
|
1967
|
-
...n ? F(n).map(D) : [],
|
|
1968
|
-
...e.tables.map((s) => s.name)
|
|
1969
|
-
]);
|
|
1970
|
-
let t = 1;
|
|
1971
|
-
for (; r.has(`union_${t}`); ) t += 1;
|
|
1972
|
-
return `union_${t}`;
|
|
1973
|
-
}
|
|
1974
|
-
function Qs(e, n, r, t) {
|
|
1975
|
-
return {
|
|
1976
|
-
id: e,
|
|
1977
|
-
from: { id: n, alias: r, query: { union: [{ from: t, joins: [], select: [] }], all: !0 } },
|
|
1978
|
-
joins: [],
|
|
1979
|
-
select: []
|
|
1980
|
-
};
|
|
1981
|
-
}
|
|
1982
|
-
function qs(e, n, r) {
|
|
1983
|
-
const t = /* @__PURE__ */ new Set(), s = e.select.map(($, x) => {
|
|
1984
|
-
const w = En(K($) ?? `column_${x + 1}`, t);
|
|
1985
|
-
return t.add(w), { ...$, alias: w };
|
|
1986
|
-
}), {
|
|
1987
|
-
from: i,
|
|
1988
|
-
joins: a,
|
|
1989
|
-
where: l,
|
|
1990
|
-
groupBy: u,
|
|
1991
|
-
having: c,
|
|
1992
|
-
distinct: m
|
|
1993
|
-
} = e, f = {
|
|
1994
|
-
from: i,
|
|
1995
|
-
joins: a,
|
|
1996
|
-
select: s,
|
|
1997
|
-
...l ? { where: l } : {},
|
|
1998
|
-
...u ? { groupBy: u } : {},
|
|
1999
|
-
...c ? { having: c } : {},
|
|
2000
|
-
...m !== void 0 ? { distinct: m } : {}
|
|
2001
|
-
}, d = ($) => ({ type: "field", table: r, column: $ }), g = ($) => {
|
|
2002
|
-
var x;
|
|
2003
|
-
return (x = s.find((w) => z(w.expression, $))) == null ? void 0 : x.alias;
|
|
2004
|
-
}, E = {
|
|
2005
|
-
...e,
|
|
2006
|
-
from: { id: n, alias: r, query: { union: [f], all: !0 } },
|
|
2007
|
-
joins: [],
|
|
2008
|
-
select: s.map(($) => ({ id: W(), expression: d($.alias) })),
|
|
2009
|
-
...e.orderBy ? {
|
|
2010
|
-
orderBy: e.orderBy.map(($) => {
|
|
2011
|
-
const x = g($.expression);
|
|
2012
|
-
return x ? { ...$, expression: d(x) } : $;
|
|
2013
|
-
})
|
|
2014
|
-
} : {}
|
|
2015
|
-
};
|
|
2016
|
-
return ["where", "groupBy", "having", "distinct"].forEach(($) => delete E[$]), E;
|
|
2017
|
-
}
|
|
2018
|
-
function _s(e, n, r, t) {
|
|
2019
|
-
const s = xe(n, r);
|
|
2020
|
-
if (!s) return n;
|
|
2021
|
-
const i = We(e, s.query.union).map((a) => ({
|
|
2022
|
-
id: W(),
|
|
2023
|
-
...a.name ? { alias: a.name } : {},
|
|
2024
|
-
expression: ie(a.type)
|
|
2025
|
-
}));
|
|
2026
|
-
return Be(n, s, {
|
|
2027
|
-
...s.query,
|
|
2028
|
-
union: [...s.query.union, { from: t, joins: [], select: i }]
|
|
2029
|
-
});
|
|
2030
|
-
}
|
|
2031
|
-
function Fs(e, n, r, t) {
|
|
2032
|
-
const s = {
|
|
2033
|
-
id: r,
|
|
2034
|
-
alias: jr(e, n),
|
|
2035
|
-
joinType: "left",
|
|
2036
|
-
query: { union: [{ from: t, joins: [], select: [] }], all: !0 }
|
|
2037
|
-
};
|
|
2038
|
-
return { ...n, joins: [...n.joins, { ...s, on: ct(e, n, s) }] };
|
|
2039
|
-
}
|
|
2040
|
-
function Js(e, n, r) {
|
|
2041
|
-
const t = xe(e, n);
|
|
2042
|
-
return !t || t.query.all === r ? e : Be(e, t, { ...t.query, all: r });
|
|
2043
|
-
}
|
|
2044
|
-
function Ps(e, n, r, t, s) {
|
|
2045
|
-
const i = xe(n, r), [a, ...l] = (i == null ? void 0 : i.query.union) ?? [], u = a == null ? void 0 : a.select[t], c = s.trim();
|
|
2046
|
-
if (!i || !u || !c || c.length > Y || a.select.some((f) => K(f) === c)) return n;
|
|
2047
|
-
const m = Be(n, i, {
|
|
2048
|
-
...i.query,
|
|
2049
|
-
union: [
|
|
2050
|
-
{ ...a, select: a.select.map((f, d) => d === t ? { ...f, alias: c } : f) },
|
|
2051
|
-
...l
|
|
2052
|
-
]
|
|
2053
|
-
});
|
|
2054
|
-
return Tn(e, n, m);
|
|
2055
|
-
}
|
|
2056
|
-
function Vs(e, n) {
|
|
2057
|
-
var m;
|
|
2058
|
-
const r = xe(e, n);
|
|
2059
|
-
if (!r || r.query.union.length !== 1) return e;
|
|
2060
|
-
const [t] = r.query.union, s = new Map(t.select.flatMap((f) => {
|
|
2061
|
-
const d = K(f);
|
|
2062
|
-
return d ? [[d, f.expression]] : [];
|
|
2063
|
-
})), i = (f) => f.type === "field" && f.table === r.alias && s.has(f.column ?? ""), a = (f) => cn(f, (d) => i(d) ? s.get(d.column) : d), l = e.select.map((f) => {
|
|
2064
|
-
const d = a(f.expression);
|
|
2065
|
-
if (f.alias || !i(f.expression)) return { ...f, expression: d };
|
|
2066
|
-
const g = f.expression.column;
|
|
2067
|
-
return K({ ...f, expression: d }) === g ? { ...f, expression: d } : { ...f, expression: d, alias: g };
|
|
2068
|
-
}), u = t.groupBy !== void 0 || t.having !== void 0 || !!t.distinct || t.select.some((f) => ue(f.expression, ge));
|
|
2069
|
-
if (r === e.from && !u) {
|
|
2070
|
-
const f = _e({ ...e, select: l }, a, (d) => d);
|
|
2071
|
-
return {
|
|
2072
|
-
...f,
|
|
2073
|
-
from: t.from,
|
|
2074
|
-
joins: [...t.joins, ...f.joins],
|
|
2075
|
-
where: xt([...qe(t.where), ...qe(f.where)]),
|
|
2076
|
-
// Nothing picked: the query's own columns, as the union had them.
|
|
2077
|
-
select: f.select.length > 0 ? f.select : t.select
|
|
2078
|
-
};
|
|
2079
|
-
}
|
|
2080
|
-
const c = e.joins.length === 0 && !e.where && !((m = e.groupBy) != null && m.length) && !e.having && !e.distinct && [...e.select, ...e.orderBy ?? []].every((f) => i(f.expression));
|
|
2081
|
-
return r === e.from && c ? {
|
|
2082
|
-
...e,
|
|
2083
|
-
...t,
|
|
2084
|
-
select: l.length > 0 ? l : t.select,
|
|
2085
|
-
...e.orderBy ? {
|
|
2086
|
-
orderBy: e.orderBy.map((f) => ({ ...f, expression: a(f.expression) }))
|
|
2087
|
-
} : {}
|
|
2088
|
-
} : Be(e, r, t);
|
|
2089
|
-
}
|
|
2090
|
-
const Wn = (e, n) => e.type === "binary" && e.operator === "IS NULL" && z(e.left, n);
|
|
2091
|
-
function an(e) {
|
|
2092
|
-
var n;
|
|
2093
|
-
if (!e) return [];
|
|
2094
|
-
if (e.type === "logical" && e.operator === "AND") return e.operands.flatMap(an);
|
|
2095
|
-
if (e.type === "binary" && (e.operator ?? "=") === "=" && e.left.type === "field" && ((n = e.right) == null ? void 0 : n.type) === "field")
|
|
2096
|
-
return [[e.left, e.right]];
|
|
2097
|
-
if (e.type === "logical" && e.operator === "OR" && e.operands.length === 2) {
|
|
2098
|
-
const [[r] = [], t] = [an(e.operands[0]), e.operands[1]];
|
|
2099
|
-
if (r && t.type === "logical" && t.operator === "AND" && t.operands.length === 2 && Wn(t.operands[0], r[0]) && Wn(t.operands[1], r[1]))
|
|
2100
|
-
return [r];
|
|
2101
|
-
}
|
|
2102
|
-
return [];
|
|
2103
|
-
}
|
|
2104
|
-
function Dr(e, n) {
|
|
2105
|
-
const r = e.groupBy ?? [];
|
|
2106
|
-
return r.length > 0 && r.every((t) => e.select.some((s) => z(s.expression, t.expression) && n.includes(K(s) ?? "")));
|
|
2107
|
-
}
|
|
2108
|
-
function Gr(e, n) {
|
|
2109
|
-
const r = D(e.from), t = /* @__PURE__ */ new Map([[r, e.from.table]]), s = [r], i = e.shared ?? [];
|
|
2110
|
-
return e.joins.map((a) => {
|
|
2111
|
-
var d, g;
|
|
2112
|
-
const { id: l } = a, u = dn(a, i), c = D(a);
|
|
2113
|
-
t.set(c, a.table);
|
|
2114
|
-
const m = an(a.on).flatMap(([E, $]) => [[E, $], [$, E]]).filter(([E, $]) => E.table === c && s.includes($.table ?? ""));
|
|
2115
|
-
if (s.push(c), a.joinType === "cross")
|
|
2116
|
-
return {
|
|
2117
|
-
joinId: l,
|
|
2118
|
-
key: c,
|
|
2119
|
-
parent: r,
|
|
2120
|
-
side: u && ot(u) ? "one" : "cross"
|
|
2121
|
-
};
|
|
2122
|
-
if (a.table === void 0) {
|
|
2123
|
-
const E = m.length > 0 && u !== void 0 && !H(u) && Dr(u, m.map(([$]) => $.column));
|
|
2124
|
-
return {
|
|
2125
|
-
joinId: l,
|
|
2126
|
-
key: c,
|
|
2127
|
-
parent: ((d = m[0]) == null ? void 0 : d[1].table) ?? r,
|
|
2128
|
-
side: E ? "one" : "unknown"
|
|
2129
|
-
};
|
|
2130
|
-
}
|
|
2131
|
-
return m.map(([E, $]) => {
|
|
2132
|
-
const x = t.get($.table), w = (C, G) => n.relations.some((R) => R.fromTable === C[0] && R.fromColumn === C[1].column && R.toTable === G[0] && R.toColumn === G[1].column);
|
|
2133
|
-
if (w([a.table, E], [x, $]))
|
|
2134
|
-
return {
|
|
2135
|
-
joinId: l,
|
|
2136
|
-
key: c,
|
|
2137
|
-
parent: $.table,
|
|
2138
|
-
side: "many"
|
|
2139
|
-
};
|
|
2140
|
-
if (w([x, $], [a.table, E]))
|
|
2141
|
-
return {
|
|
2142
|
-
joinId: l,
|
|
2143
|
-
key: c,
|
|
2144
|
-
parent: $.table,
|
|
2145
|
-
side: "one"
|
|
2146
|
-
};
|
|
2147
|
-
}).find((E) => E !== void 0) ?? {
|
|
2148
|
-
joinId: l,
|
|
2149
|
-
key: c,
|
|
2150
|
-
parent: ((g = m[0]) == null ? void 0 : g[1].table) ?? r,
|
|
2151
|
-
side: "unknown"
|
|
2152
|
-
};
|
|
2153
|
-
});
|
|
2154
|
-
}
|
|
2155
|
-
function on(e, n, r = {}) {
|
|
2156
|
-
const t = Gr(e, n).map((l) => l.side === "unknown" && r.unknown === "repeat" ? { ...l, side: "many" } : l), s = [D(e.from), ...t.map((l) => l.key)], i = (l) => {
|
|
2157
|
-
const u = /* @__PURE__ */ new Set([l]);
|
|
2158
|
-
return t.forEach((c) => {
|
|
2159
|
-
u.has(c.parent) && u.add(c.key);
|
|
2160
|
-
}), u;
|
|
2161
|
-
}, a = /* @__PURE__ */ new Set();
|
|
2162
|
-
return t.forEach((l) => {
|
|
2163
|
-
const u = i(l.key);
|
|
2164
|
-
l.side === "many" && s.filter((c) => !u.has(c)).forEach((c) => a.add(c)), l.side === "one" && u.forEach((c) => a.add(c)), l.side === "cross" && s.forEach((c) => a.add(c));
|
|
2165
|
-
}), a;
|
|
2166
|
-
}
|
|
2167
|
-
const Rr = /* @__PURE__ */ new Set(["SUM", "AVG", "COUNT"]);
|
|
2168
|
-
function Qr(e, n) {
|
|
2169
|
-
return e.type === "field" ? e.table && n.has(e.table) ? [e.table] : [] : [...new Set(me(e).flatMap((r) => Qr(r, n)))];
|
|
2170
|
-
}
|
|
2171
|
-
function Ws(e, n) {
|
|
2172
|
-
const r = D(e.from);
|
|
2173
|
-
return e.joins.some((s) => s.joinType === "right" || s.joinType === "full") || on(e, n).has(r) ? "many" : on(e, n, { unknown: "repeat" }).has(r) ? "unknown" : "one";
|
|
2174
|
-
}
|
|
2175
|
-
function Lt(e, n, r = !1) {
|
|
2176
|
-
const t = e.type === "function" && Rr.has(e.name.toUpperCase()) && !e.distinct, s = r || t;
|
|
2177
|
-
return e.type === "field" ? s && e.table && n.has(e.table) ? [e.table] : [] : me(e).flatMap((i) => Lt(i, n, s));
|
|
2178
|
-
}
|
|
2179
|
-
function qr(e, n) {
|
|
2180
|
-
const r = on(e, n), t = /* @__PURE__ */ new Set(), s = (u) => {
|
|
2181
|
-
const c = u ? Lt(u, r) : [];
|
|
2182
|
-
return c.forEach((m) => t.add(m)), c.length > 0;
|
|
2183
|
-
}, i = e.select.filter((u) => s(u.expression)).map((u) => u.id), a = (e.orderBy ?? []).filter((u) => s(u.expression)).map((u) => u.id), l = s(e.having);
|
|
2184
|
-
return {
|
|
2185
|
-
tables: [...t],
|
|
2186
|
-
select: i,
|
|
2187
|
-
orderBy: a,
|
|
2188
|
-
having: l
|
|
2189
|
-
};
|
|
2190
|
-
}
|
|
2191
|
-
function _r(e, n) {
|
|
2192
|
-
return e.select.filter((r) => ue(r.expression, n));
|
|
2193
|
-
}
|
|
2194
|
-
function Fr(e) {
|
|
2195
|
-
return { ...e.expression, fromSelect: e.id };
|
|
2196
|
-
}
|
|
2197
|
-
function ve(e) {
|
|
2198
|
-
const { fromSelect: n, ...r } = e;
|
|
2199
|
-
return n === void 0 ? e : r;
|
|
2200
|
-
}
|
|
2201
|
-
function jt(e, n) {
|
|
2202
|
-
const r = (s) => typeof s.fromSelect == "string" ? n(s, s.fromSelect) : Ne(s, r), t = r(e);
|
|
2203
|
-
return z(t, e) ? e : t;
|
|
2204
|
-
}
|
|
2205
|
-
function Jr(e, n) {
|
|
2206
|
-
var l;
|
|
2207
|
-
const r = new Map(_r(e, n).map((u) => [u.id, u])), t = (u) => jt(u, (c, m) => {
|
|
2208
|
-
const f = r.get(m);
|
|
2209
|
-
return f ? Fr(f) : ve(c);
|
|
2210
|
-
}), s = e.having && t(e.having);
|
|
2211
|
-
let i = !1;
|
|
2212
|
-
const a = (l = e.orderBy) == null ? void 0 : l.map((u) => {
|
|
2213
|
-
const c = t(u.expression);
|
|
2214
|
-
return c === u.expression ? u : (i = !0, { ...u, expression: c });
|
|
2215
|
-
});
|
|
2216
|
-
return s === e.having && !i ? e : { ...e, having: s, ...a ? { orderBy: a } : {} };
|
|
2217
|
-
}
|
|
2218
|
-
function Hs(e, n) {
|
|
2219
|
-
const r = new Set((n.groupBy ?? []).flatMap(({ expression: t }) => t.type === "field" && t.column ? [t.column.toLowerCase()] : []));
|
|
2220
|
-
return jt(e, (t, s) => {
|
|
2221
|
-
const i = n.select.find((a) => a.id === s);
|
|
2222
|
-
return !(i != null && i.alias) || r.has(i.alias.toLowerCase()) || !z(ve(t), i.expression) ? ve(t) : { type: "field", column: i.alias };
|
|
2223
|
-
});
|
|
2224
|
-
}
|
|
2225
|
-
function ln(e, n) {
|
|
2226
|
-
const r = (m) => ln(m, n), t = (m) => Je(m, r), s = e.from.query && t(e.from.query), i = s && s !== e.from.query ? { ...e.from, query: s } : e.from;
|
|
2227
|
-
let a = !1;
|
|
2228
|
-
const l = e.joins.map((m) => {
|
|
2229
|
-
if (!m.query) return m;
|
|
2230
|
-
const f = t(m.query);
|
|
2231
|
-
return f === m.query ? m : (a = !0, { ...m, query: f });
|
|
2232
|
-
}), u = i === e.from && !a ? e : { ...e, from: i, joins: l }, c = rt(u, (m) => {
|
|
2233
|
-
const f = r(m.query);
|
|
2234
|
-
return f === m.query ? m : { ...m, query: f };
|
|
2235
|
-
});
|
|
2236
|
-
return Pt(Jr(c, n), n);
|
|
2237
|
-
}
|
|
2238
|
-
function Xs(e, n, r, t = ge) {
|
|
2239
|
-
if (!ce(r)) return r;
|
|
2240
|
-
const s = un(
|
|
2241
|
-
ln(r, t),
|
|
2242
|
-
(a) => Je(a, (l) => ln(l, t))
|
|
2243
|
-
), i = Ur(e, n, s);
|
|
2244
|
-
return Nr(e, n, i);
|
|
2245
|
-
}
|
|
2246
|
-
const Pr = [
|
|
2247
|
-
"=",
|
|
2248
|
-
"!=",
|
|
2249
|
-
"<",
|
|
2250
|
-
"<=",
|
|
2251
|
-
">",
|
|
2252
|
-
">=",
|
|
2253
|
-
"LIKE",
|
|
2254
|
-
"IN",
|
|
2255
|
-
"BETWEEN",
|
|
2256
|
-
"REGEXP",
|
|
2257
|
-
"IS NULL",
|
|
2258
|
-
"IS NOT NULL"
|
|
2259
|
-
], Vr = ["IS NULL", "IS NOT NULL"];
|
|
2260
|
-
function Wr(e) {
|
|
2261
|
-
return Vr.includes(e);
|
|
2262
|
-
}
|
|
2263
|
-
function Hr(e) {
|
|
2264
|
-
return !e.distinct || !e.orderBy ? [] : e.orderBy.filter(({ expression: n }) => !(n.type === "field" && !n.column)).filter(({ expression: n }) => e.select.length === 0 ? n.type !== "field" : !e.select.some((r) => z(r.expression, ve(n)))).map(({ id: n }) => n);
|
|
2265
|
-
}
|
|
2266
|
-
function Dt(e, n) {
|
|
2267
|
-
n(e), me(e).forEach((r) => Dt(r, n));
|
|
2268
|
-
}
|
|
2269
|
-
const Gt = (e) => {
|
|
2270
|
-
const n = /* @__PURE__ */ new Map();
|
|
2271
|
-
return Zn(e, (r) => (r.type === "param" && r.relative && (n.has(r.name) || n.set(r.name, /* @__PURE__ */ new Set()), n.get(r.name).add(JSON.stringify(r.relative))), r)), n;
|
|
2272
|
-
};
|
|
2273
|
-
function Rt(e, n) {
|
|
2274
|
-
const r = /* @__PURE__ */ new Map(), t = (i, a) => Dt(i, (l) => {
|
|
2275
|
-
if (l.type === "param" && !r.has(l.name) && r.set(l.name, /* @__PURE__ */ new Set()), l.type !== "binary" || !l.right) return;
|
|
2276
|
-
const u = (m, f) => {
|
|
2277
|
-
const d = a(f);
|
|
2278
|
-
m.type !== "param" || !d || (r.has(m.name) || r.set(m.name, /* @__PURE__ */ new Set()), r.get(m.name).add(d));
|
|
2279
|
-
};
|
|
2280
|
-
u(l.left, l.right), (l.right.type === "list" ? l.right.items : [l.right]).forEach((m) => u(m, l.left));
|
|
2281
|
-
}), s = (i) => {
|
|
2282
|
-
const a = /* @__PURE__ */ new Map();
|
|
2283
|
-
F(i).forEach((u) => a.set(D(u), u.table));
|
|
2284
|
-
const l = (u) => {
|
|
2285
|
-
var f;
|
|
2286
|
-
if (!n || u.type !== "field" || !u.table || !u.column) return;
|
|
2287
|
-
const c = a.has(u.table) ? a.get(u.table) : u.table, m = c === void 0 ? void 0 : Z(n, c);
|
|
2288
|
-
return (f = m == null ? void 0 : m.columns.find((d) => d.name === u.column)) == null ? void 0 : f.type;
|
|
2289
|
-
};
|
|
2290
|
-
Hn(i).forEach((u) => t(u, l)), F(i).forEach((u) => u.query && Se(u.query).forEach(s)), tt(i).forEach((u) => s(u.query));
|
|
2291
|
-
};
|
|
2292
|
-
return (e.shared ?? []).forEach((i) => Se(i.query).forEach(s)), s(e), r;
|
|
2293
|
-
}
|
|
2294
|
-
function Ks(e, n) {
|
|
2295
|
-
const r = Gt(e);
|
|
2296
|
-
return [...Rt(e, n)].map(([t, s]) => {
|
|
2297
|
-
const i = r.get(t);
|
|
2298
|
-
return {
|
|
2299
|
-
name: t,
|
|
2300
|
-
...s.size === 1 ? { type: [...s][0] } : {},
|
|
2301
|
-
// (Two different ones: validateQuery reports it; the first here.)
|
|
2302
|
-
...i ? { relative: JSON.parse([...i][0]) } : {}
|
|
2303
|
-
};
|
|
2304
|
-
});
|
|
2305
|
-
}
|
|
2306
|
-
function Xr(e) {
|
|
2307
|
-
return [...Gt(e)].filter(([, n]) => n.size > 1).map(([n]) => n);
|
|
2308
|
-
}
|
|
2309
|
-
function Kr(e, n) {
|
|
2310
|
-
return [...Rt(e, n)].filter(([, r]) => r.size > 1).map(([r]) => r);
|
|
2311
|
-
}
|
|
2312
|
-
function Ys(e, n) {
|
|
2313
|
-
const r = new Map(Object.entries(n));
|
|
2314
|
-
return Zn(e, (t) => {
|
|
2315
|
-
if (t.type !== "param") return t;
|
|
2316
|
-
if (!r.has(t.name)) throw new Error(`no value for parameter "${t.name}"`);
|
|
2317
|
-
const s = r.get(t.name);
|
|
2318
|
-
return s === "" ? { type: "literal", value: s, empty: !0 } : { type: "literal", value: s };
|
|
2319
|
-
});
|
|
2320
|
-
}
|
|
2321
|
-
const zs = [
|
|
2322
|
-
"invalidStructure",
|
|
2323
|
-
"duplicateId",
|
|
2324
|
-
"unknownTable",
|
|
2325
|
-
"unknownColumn",
|
|
2326
|
-
"tableNotInQuery",
|
|
2327
|
-
"unqualifiedField",
|
|
2328
|
-
"incomplete",
|
|
2329
|
-
"argCount",
|
|
2330
|
-
"ambiguousTable",
|
|
2331
|
-
"duplicateTableName",
|
|
2332
|
-
"derivedColumnName",
|
|
2333
|
-
"unknownFunction",
|
|
2334
|
-
"joinOrder",
|
|
2335
|
-
"distinctSort",
|
|
2336
|
-
"notGrouped",
|
|
2337
|
-
"unknownAggregate",
|
|
2338
|
-
"misplacedAggregate",
|
|
2339
|
-
"paramConflict",
|
|
2340
|
-
"fanOut",
|
|
2341
|
-
"crossJoin",
|
|
2342
|
-
"unlinkedJoin",
|
|
2343
|
-
"drillAcrossOutOfStep",
|
|
2344
|
-
"drillAcrossFilterMixed",
|
|
2345
|
-
"tooLong",
|
|
2346
|
-
"dialectUnsupported",
|
|
2347
|
-
"subqueryColumns",
|
|
2348
|
-
"notInSubquery",
|
|
2349
|
-
"noLimit",
|
|
2350
|
-
"limitTooHigh",
|
|
2351
|
-
"typeMismatch",
|
|
2352
|
-
"invalidPattern",
|
|
2353
|
-
"unionColumns",
|
|
2354
|
-
"unionTypes",
|
|
2355
|
-
"sharedOrder",
|
|
2356
|
-
"unusedShared"
|
|
2357
|
-
], Zs = [
|
|
2358
|
-
"fanOut",
|
|
2359
|
-
"crossJoin",
|
|
2360
|
-
"unlinkedJoin",
|
|
2361
|
-
"drillAcrossFilterMixed",
|
|
2362
|
-
"notInSubquery",
|
|
2363
|
-
"noLimit",
|
|
2364
|
-
"limitTooHigh",
|
|
2365
|
-
"typeMismatch",
|
|
2366
|
-
"unknownAggregate",
|
|
2367
|
-
"unusedShared"
|
|
2368
|
-
];
|
|
2369
|
-
function Yr(e) {
|
|
2370
|
-
return [...e.matchAll(/([A-Za-z_][A-Za-z0-9_]*)|\[(\d+)\]/g)].map(([, n, r]) => n ?? Number(r));
|
|
2371
|
-
}
|
|
2372
|
-
const Ue = (e) => e === "integer" ? "number" : e === "dateTime" ? "date" : e, fe = {
|
|
2373
|
-
text: "text",
|
|
2374
|
-
integer: "a whole number",
|
|
2375
|
-
number: "a number",
|
|
2376
|
-
date: "a date",
|
|
2377
|
-
dateTime: "a date and time",
|
|
2378
|
-
time: "a time of day",
|
|
2379
|
-
boolean: "yes / no"
|
|
2380
|
-
}, O = (e) => typeof e == "object" && e !== null && !Array.isArray(e), Q = (e) => typeof e == "string", zr = ["day", "week", "month", "year"], Zr = (e) => O(e) && zr.includes(e.unit) && Number.isSafeInteger(e.offset) && (e.anchor === void 0 || e.unit !== "day" && (e.anchor === "start" || e.anchor === "end")), pe = (e) => e === void 0 || Q(e), es = (e) => e === null || Q(e) || typeof e == "boolean" || typeof e == "bigint" || typeof e == "number" && Number.isFinite(e) || e instanceof Date && !Number.isNaN(e.getTime()), ze = (e) => O(e) && e.type === "literal" && e.value === "" && e.empty !== !0;
|
|
2381
|
-
function ei(e, n, r = {}) {
|
|
2382
|
-
var xn, Bn;
|
|
2383
|
-
const t = [], s = (o, h, y, p = "error") => {
|
|
2384
|
-
t.push({
|
|
2385
|
-
kind: o,
|
|
2386
|
-
severity: p,
|
|
2387
|
-
path: h,
|
|
2388
|
-
pathSegments: Yr(h),
|
|
2389
|
-
message: y
|
|
2390
|
-
});
|
|
2391
|
-
};
|
|
2392
|
-
if (!O(n) || !Q(n.id) || n.id === "")
|
|
2393
|
-
return s("invalidStructure", "", "a query is an object with a non-empty string id"), t;
|
|
2394
|
-
if (!("from" in n)) return t;
|
|
2395
|
-
const i = Vt(e, r.tableFilter, r.columnFilter), a = Wt(r.functions ?? ge);
|
|
2396
|
-
let l = /* @__PURE__ */ new Map(), u = /* @__PURE__ */ new Map(), c;
|
|
2397
|
-
const m = (o, h, y, p) => {
|
|
2398
|
-
Q(o) && o.length > h && s("tooLong", y, `${p} is longer than ${h} characters`);
|
|
2399
|
-
}, f = /* @__PURE__ */ new Set(), d = O(n) && Array.isArray(n.shared) ? n.shared : [], g = new Set(d.flatMap((o) => O(o) && Q(o.id) ? [o.id] : [])), E = /* @__PURE__ */ new Map(), $ = /* @__PURE__ */ new Set();
|
|
2400
|
-
let x;
|
|
2401
|
-
const w = (o, h) => {
|
|
2402
|
-
l.has(o) && s("duplicateTableName", h, `two tables named "${o}"`);
|
|
2403
|
-
}, C = (o, h, y = !1) => {
|
|
2404
|
-
if (y && O(o) && o.query !== void 0) {
|
|
2405
|
-
x(o, h);
|
|
2406
|
-
return;
|
|
2407
|
-
}
|
|
2408
|
-
if (y && O(o) && o.shared !== void 0) {
|
|
2409
|
-
const { shared: N, alias: b } = o, v = Q(b) && b !== "";
|
|
2410
|
-
if (!Q(N) || !v || o.table !== void 0) {
|
|
2411
|
-
s("invalidStructure", h, "a shared query's place has a string `shared` and an `alias`, and no `table`"), v && (w(b, `${h}.alias`), l.set(b, void 0));
|
|
2412
|
-
return;
|
|
2413
|
-
}
|
|
2414
|
-
m(b, Y, `${h}.alias`, "an alias"), w(b, `${h}.alias`), $.add(N), g.has(N) ? E.has(N) || s("sharedOrder", `${h}.shared`, "a shared query can use only those before it") : s("unknownTable", `${h}.shared`, `no shared query "${N}"`), l.set(b, E.get(N));
|
|
2415
|
-
return;
|
|
2416
|
-
}
|
|
2417
|
-
if (!O(o) || !Q(o.table) || !pe(o.alias)) {
|
|
2418
|
-
s("invalidStructure", h, "a table entry has a string `table` and optional `alias`");
|
|
2419
|
-
return;
|
|
2420
|
-
}
|
|
2421
|
-
m(o.alias, Y, `${h}.alias`, "an alias");
|
|
2422
|
-
const p = i.tables.find((N) => N.name === o.table);
|
|
2423
|
-
p || s("unknownTable", `${h}.table`, `unknown table "${o.table}"`), Q(o.alias) && o.alias !== "" ? (w(o.alias, `${h}.alias`), l.set(o.alias, p), u.set(o.table, o.alias)) : (w(o.table, `${h}.table`), l.set(o.table, p));
|
|
2424
|
-
}, G = (o, h, y) => {
|
|
2425
|
-
var T;
|
|
2426
|
-
if (!o) return;
|
|
2427
|
-
const { args: p, name: N } = o, b = p.filter((A) => !A.optional).length, v = (T = p[p.length - 1]) != null && T.variadic ? 1 / 0 : p.length;
|
|
2428
|
-
h < b ? s("incomplete", y, `${N} needs ${b} argument(s)`) : h > v && s("argCount", y, `${N} takes ${v} argument(s)`);
|
|
2429
|
-
};
|
|
2430
|
-
let R, X, ee;
|
|
2431
|
-
const be = (o) => {
|
|
2432
|
-
const h = o.table && (l.get(o.table) ?? (c == null ? void 0 : c.get(o.table)));
|
|
2433
|
-
if (!h || Array.isArray(h)) return;
|
|
2434
|
-
const y = h.columns.find((p) => p.name === o.column);
|
|
2435
|
-
return y && Tt(y.type);
|
|
2436
|
-
}, te = (o) => $n(o, be, a), re = r.typeMismatch ?? "warning", k = (o, h, y) => {
|
|
2437
|
-
if (o.type === "literal") {
|
|
2438
|
-
if (o.value === null || o.value === "" && !o.empty || typeof o.value == "number" && !Ze(o.value) || vn(o.value, h) !== void 0) return;
|
|
2439
|
-
const b = o.value instanceof Date ? "a date" : JSON.stringify(String(o.value));
|
|
2440
|
-
s("typeMismatch", y, `${b} isn't ${fe[h]}`, re);
|
|
2441
|
-
return;
|
|
2442
|
-
}
|
|
2443
|
-
const p = te(o);
|
|
2444
|
-
if (p === void 0 || Ue(p) === Ue(h)) return;
|
|
2445
|
-
const N = `${fe[p]} compared with ${fe[h]}`;
|
|
2446
|
-
s("typeMismatch", y, N, re);
|
|
2447
|
-
}, J = (o) => o && o.type !== "literal" ? te(o) : void 0, He = (o, h) => {
|
|
2448
|
-
const { operator: y, left: p, right: N } = o;
|
|
2449
|
-
if (!N) return;
|
|
2450
|
-
const b = (T, A, M) => {
|
|
2451
|
-
if (T.type !== "param" || !T.relative) return;
|
|
2452
|
-
const S = J(A);
|
|
2453
|
-
if (S === void 0 || S === "date" || S === "dateTime") return;
|
|
2454
|
-
const I = `a relative date compared with ${fe[S]}`;
|
|
2455
|
-
s("typeMismatch", M, I, re);
|
|
2456
|
-
};
|
|
2457
|
-
if (b(N, p, `${h}.right`), b(p, N, `${h}.left`), y === "LIKE" || y === "REGEXP") {
|
|
2458
|
-
const T = J(p);
|
|
2459
|
-
if (T !== void 0 && T !== "text") {
|
|
2460
|
-
const A = `${y} matches text, not ${fe[T]}`;
|
|
2461
|
-
s("typeMismatch", `${h}.left`, A, re);
|
|
2462
|
-
}
|
|
2463
|
-
if (k(N, "text", `${h}.right`), y === "REGEXP" && N.type === "literal" && Q(N.value))
|
|
2464
|
-
try {
|
|
2465
|
-
new RegExp(N.value);
|
|
2466
|
-
} catch (A) {
|
|
2467
|
-
const M = `not a regular expression: ${A.message}`;
|
|
2468
|
-
s("invalidPattern", `${h}.right`, M);
|
|
2469
|
-
}
|
|
2470
|
-
return;
|
|
2471
|
-
}
|
|
2472
|
-
const v = J(p);
|
|
2473
|
-
if (y === "IN" || y === "BETWEEN") {
|
|
2474
|
-
if (v === void 0 || N.type !== "list") return;
|
|
2475
|
-
N.items.forEach((T, A) => k(T, v, `${h}.right.items[${A}]`));
|
|
2476
|
-
return;
|
|
2477
|
-
}
|
|
2478
|
-
if (v !== void 0) k(N, v, `${h}.right`);
|
|
2479
|
-
else {
|
|
2480
|
-
const T = J(N);
|
|
2481
|
-
T !== void 0 && k(p, T, `${h}.left`);
|
|
2482
|
-
}
|
|
2483
|
-
}, Nn = (o) => {
|
|
2484
|
-
const h = o.map(([y]) => y.type === "literal" ? Re(y) : te(y)).find((y) => y !== void 0);
|
|
2485
|
-
h !== void 0 && o.forEach(([y, p]) => k(y, h, p));
|
|
2486
|
-
}, V = (o, h, y = !1) => {
|
|
2487
|
-
if (!O(o) || !Q(o.type)) {
|
|
2488
|
-
s("invalidStructure", h, "an expression is an object with a string `type`");
|
|
2489
|
-
return;
|
|
2490
|
-
}
|
|
2491
|
-
const p = (b) => s("invalidStructure", h, b), N = (b, v) => {
|
|
2492
|
-
o[b] !== void 0 && V(o[b], `${h}.${b}`, v);
|
|
2493
|
-
};
|
|
2494
|
-
switch (y && o.type === "exists" && p("EXISTS is a condition, not a value"), pe(o.fromSelect) || p("`fromSelect` is a column's id"), o.type) {
|
|
2495
|
-
case "literal":
|
|
2496
|
-
if (!es(o.value))
|
|
2497
|
-
p("a literal value is a string, number, boolean, date or null");
|
|
2498
|
-
else if (o.empty !== void 0 && typeof o.empty != "boolean")
|
|
2499
|
-
p("a literal's `empty` is a boolean");
|
|
2500
|
-
else if (ze(o))
|
|
2501
|
-
s("incomplete", h, "a value not filled in");
|
|
2502
|
-
else if (typeof o.value == "number" && !Ze(o.value)) {
|
|
2503
|
-
const b = `a number with more than ${nt} significant digits is written as another number`;
|
|
2504
|
-
s("tooLong", `${h}.value`, b);
|
|
2505
|
-
} else
|
|
2506
|
-
m(o.value, zt, `${h}.value`, "a text value");
|
|
2507
|
-
break;
|
|
2508
|
-
case "param":
|
|
2509
|
-
(!Q(o.name) || !/^[A-Za-z_][A-Za-z0-9_]*$/.test(o.name)) && p("a param has a plain identifier as its `name`"), o.relative !== void 0 && !Zr(o.relative) && p("a relative date has a `unit` (day, week, month or year), a whole `offset`, and an `anchor` (start or end) only for a week, month or year"), m(o.name, Y, `${h}.name`, "a parameter's name");
|
|
2510
|
-
break;
|
|
2511
|
-
case "field":
|
|
2512
|
-
if (!pe(o.table) || !pe(o.column))
|
|
2513
|
-
p("a field has string `table` and `column`");
|
|
2514
|
-
else if (o.column === void 0 || o.column === "")
|
|
2515
|
-
s("incomplete", h, "a field with no column picked");
|
|
2516
|
-
else if (o.table === void 0 || o.table === "")
|
|
2517
|
-
s("unqualifiedField", h, `column "${o.column}" names no table`);
|
|
2518
|
-
else if (l.has(o.table)) {
|
|
2519
|
-
const b = l.get(o.table);
|
|
2520
|
-
if (!(Array.isArray(b) ? b.includes(o.column) : !b || b.columns.some((T) => T.name === o.column))) {
|
|
2521
|
-
const T = `${o.table}.${o.column}`;
|
|
2522
|
-
s("unknownColumn", `${h}.column`, `unknown column "${T}"`);
|
|
2523
|
-
}
|
|
2524
|
-
} else {
|
|
2525
|
-
const b = u.get(o.table);
|
|
2526
|
-
let v = `table "${o.table}" isn't in the query`;
|
|
2527
|
-
b ? v = `table "${o.table}" is named "${b}" in the query: use that` : c != null && c.has(o.table) && (v = `table "${o.table}" is the outer query's: only the EXISTS link names it`), s("tableNotInQuery", `${h}.table`, v);
|
|
2528
|
-
}
|
|
2529
|
-
break;
|
|
2530
|
-
case "function": {
|
|
2531
|
-
if (!Q(o.name) || !Array.isArray(o.args)) {
|
|
2532
|
-
p("a function has a string `name` and an `args` array");
|
|
2533
|
-
break;
|
|
2534
|
-
}
|
|
2535
|
-
!r.allowUnknownFunctions && !a[o.name.toUpperCase()] && s("unknownFunction", `${h}.name`, `function "${o.name}" isn't allowed`);
|
|
2536
|
-
const b = a[o.name.toUpperCase()];
|
|
2537
|
-
if (r.dialect && (b != null && b.dialects) && !b.dialects.includes(r.dialect) && s("dialectUnsupported", `${h}.name`, `${b.name} isn't in this database`), m(o.name, Y, `${h}.name`, "a function's name"), G(b, o.args.length, `${h}.args`), o.distinct !== void 0 && typeof o.distinct != "boolean" && p("a function's `distinct` is a boolean"), o.distinct === !0 && r.dialect === "mssql" && o.name.toUpperCase() === "STRING_AGG" && s("dialectUnsupported", h, "SQL Server's STRING_AGG can't join only different values"), o.args.forEach((v, T) => {
|
|
2538
|
-
var S, I;
|
|
2539
|
-
const A = t.length;
|
|
2540
|
-
V(v, `${h}.args[${T}]`, !0), b && ((S = Le(b, T)) == null ? void 0 : S.kind) === "type-name" && O(v) && m(v.value, Zt, `${h}.args[${T}].value`, "a type name"), b && ((I = Le(b, T)) == null ? void 0 : I.kind) === "fixed-text" && O(v) && !(v.type === "literal" && Q(v.value)) && p(`${b.name}'s ${Le(b, T).label} is a text value`);
|
|
2541
|
-
const M = !t.slice(A).some((P) => P.kind === "invalidStructure");
|
|
2542
|
-
b != null && b.aggregate && M && ue(v, a) && s(
|
|
2543
|
-
"misplacedAggregate",
|
|
2544
|
-
`${h}.args[${T}]`,
|
|
2545
|
-
"an aggregate can't be inside another: sum up per group in a sub query first"
|
|
2546
|
-
);
|
|
2547
|
-
}), o.orderBy !== void 0) {
|
|
2548
|
-
if (!Array.isArray(o.orderBy)) {
|
|
2549
|
-
p("a function's `orderBy` is an array");
|
|
2550
|
-
break;
|
|
2551
|
-
}
|
|
2552
|
-
o.name.toUpperCase() !== "STRING_AGG" && p("only STRING_AGG takes an `orderBy`"), o.orderBy.forEach((v, T) => {
|
|
2553
|
-
const A = `${h}.orderBy[${T}]`;
|
|
2554
|
-
if (!O(v) || v.direction !== "asc" && v.direction !== "desc") {
|
|
2555
|
-
s("invalidStructure", A, "a sort key has an `expression` and a `direction`");
|
|
2556
|
-
return;
|
|
2557
|
-
}
|
|
2558
|
-
const M = t.length;
|
|
2559
|
-
V(v.expression, `${A}.expression`, !0), !t.slice(M).some((S) => S.kind === "invalidStructure") && (ue(v.expression, a) && s("misplacedAggregate", `${A}.expression`, "an aggregate can't be inside another"), o.distinct === !0 && r.dialect === "postgres" && !z(v.expression, o.args[0]) && s(
|
|
2560
|
-
"dialectUnsupported",
|
|
2561
|
-
`${A}.expression`,
|
|
2562
|
-
"PostgreSQL orders STRING_AGG of different values by the value only"
|
|
2563
|
-
));
|
|
2564
|
-
});
|
|
2565
|
-
}
|
|
2566
|
-
break;
|
|
2567
|
-
}
|
|
2568
|
-
case "binary": {
|
|
2569
|
-
o.operator !== void 0 && !Pr.includes(o.operator) && p(`unknown operator ${JSON.stringify(o.operator)}`), o.operator === "REGEXP" && r.dialect === "mssql" && s("dialectUnsupported", h, "SQL Server has no REGEXP");
|
|
2570
|
-
const b = t.length;
|
|
2571
|
-
V(o.left, `${h}.left`, !0);
|
|
2572
|
-
const v = t.slice(b).some((T) => T.kind === "incomplete");
|
|
2573
|
-
if (Wr(o.operator))
|
|
2574
|
-
o.right !== void 0 && p(`${String(o.operator)} takes no right side`);
|
|
2575
|
-
else if (o.right === void 0)
|
|
2576
|
-
s("incomplete", `${h}.right`, "a comparison with nothing to compare to");
|
|
2577
|
-
else if (O(o.right) && o.right.type === "literal" && o.right.value === null)
|
|
2578
|
-
s("incomplete", `${h}.right`, "compared with NULL (never true): use IS NULL");
|
|
2579
|
-
else if (O(o.right) && o.right.type === "subquery")
|
|
2580
|
-
o.operator === "IN" ? X(o.right, `${h}.right`) : s("invalidStructure", `${h}.right`, "a sub query is only IN's right side");
|
|
2581
|
-
else if (o.operator === "BETWEEN") {
|
|
2582
|
-
const { right: T } = o, A = O(T) && T.type === "list" && Array.isArray(T.items) ? T.items : void 0;
|
|
2583
|
-
(A == null ? void 0 : A.length) !== 2 ? s("invalidStructure", `${h}.right`, "BETWEEN takes its two bounds as a list (expr.between)") : A.forEach((M, S) => {
|
|
2584
|
-
const I = `${h}.right.items[${S}]`;
|
|
2585
|
-
O(M) && M.type === "literal" && M.value === null ? s("incomplete", I, "a bound of NULL (never true)") : v && ze(M) || V(M, I, !0);
|
|
2586
|
-
});
|
|
2587
|
-
} else O(o.right) && o.right.type === "list" ? o.operator === "IN" ? ee(o.right, `${h}.right`) : s("invalidStructure", `${h}.right`, "a list is only IN's or BETWEEN's right side") : o.operator === "IN" ? s("invalidStructure", `${h}.right`, "IN takes a list (expr.list) or a sub query") : v && ze(o.right) || V(o.right, `${h}.right`, !0);
|
|
2588
|
-
t.slice(b).some((T) => T.kind === "invalidStructure") || He(o, h);
|
|
2589
|
-
break;
|
|
2590
|
-
}
|
|
2591
|
-
case "logical":
|
|
2592
|
-
if (o.operator !== "AND" && o.operator !== "OR" && p("a logical operator is AND or OR"), !Array.isArray(o.operands)) {
|
|
2593
|
-
p("a logical node has an `operands` array");
|
|
2594
|
-
break;
|
|
2595
|
-
}
|
|
2596
|
-
o.operands.length === 0 && s("incomplete", h, "an empty AND/OR group"), o.operands.forEach((b, v) => V(b, `${h}.operands[${v}]`));
|
|
2597
|
-
break;
|
|
2598
|
-
case "not": {
|
|
2599
|
-
V(o.operand, `${h}.operand`);
|
|
2600
|
-
const { operand: b } = o;
|
|
2601
|
-
O(b) && b.type === "binary" && b.operator === "IN" && O(b.right) && b.right.type === "subquery" && s("notInSubquery", h, "NOT IN keeps no rows at all if the sub query gives a NULL: NOT EXISTS doesn't", "warning");
|
|
2602
|
-
break;
|
|
2603
|
-
}
|
|
2604
|
-
case "if": {
|
|
2605
|
-
["condition", "then"].forEach((v) => {
|
|
2606
|
-
o[v] === void 0 && s("incomplete", `${h}.${v}`, `an IF with no ${v}`);
|
|
2607
|
-
});
|
|
2608
|
-
const b = t.length;
|
|
2609
|
-
N("condition", !1), N("then", !0), N("else", !0), t.slice(b).some((v) => v.kind === "invalidStructure") || Nn(
|
|
2610
|
-
["then", "else"].filter((v) => o[v] !== void 0).map((v) => [o[v], `${h}.${v}`])
|
|
2611
|
-
);
|
|
2612
|
-
break;
|
|
2613
|
-
}
|
|
2614
|
-
case "case": {
|
|
2615
|
-
const b = t.length;
|
|
2616
|
-
N("subject", !0), Array.isArray(o.branches) ? (o.branches.length === 0 && s("incomplete", h, "a CASE with no WHEN"), o.branches.forEach((T, A) => {
|
|
2617
|
-
if (!O(T)) {
|
|
2618
|
-
const M = `${h}.branches[${A}]`;
|
|
2619
|
-
s("invalidStructure", M, "a branch has `when` and `then`");
|
|
2620
|
-
return;
|
|
2621
|
-
}
|
|
2622
|
-
V(T.when, `${h}.branches[${A}].when`, o.subject !== void 0), V(T.then, `${h}.branches[${A}].then`, !0);
|
|
2623
|
-
})) : p("a case node has a `branches` array");
|
|
2624
|
-
const v = t.length;
|
|
2625
|
-
N("else", !0), Array.isArray(o.branches) && t.slice(b).concat(t.slice(v)).every((T) => T.kind !== "invalidStructure") && Nn([
|
|
2626
|
-
...o.branches.map((T, A) => [T.then, `${h}.branches[${A}].then`]),
|
|
2627
|
-
...o.else === void 0 ? [] : [[o.else, `${h}.else`]]
|
|
2628
|
-
]);
|
|
2629
|
-
break;
|
|
2630
|
-
}
|
|
2631
|
-
case "exists":
|
|
2632
|
-
R(o, h);
|
|
2633
|
-
break;
|
|
2634
|
-
case "subquery":
|
|
2635
|
-
p("a sub query is only IN's right side");
|
|
2636
|
-
break;
|
|
2637
|
-
case "list":
|
|
2638
|
-
p("a list is only IN's right side");
|
|
2639
|
-
break;
|
|
2640
|
-
default:
|
|
2641
|
-
p(`unknown expression type ${JSON.stringify(o.type)}`);
|
|
2642
|
-
}
|
|
2643
|
-
};
|
|
2644
|
-
ee = (o, h) => {
|
|
2645
|
-
Array.isArray(o.items) ? o.items.length === 0 ? s("incomplete", h, "IN with no values") : o.items.forEach((y, p) => V(y, `${h}.items[${p}]`, !0)) : s("invalidStructure", h, "a list has an `items` array");
|
|
2646
|
-
}, X = (o, h) => {
|
|
2647
|
-
const { id: y, query: p } = o;
|
|
2648
|
-
if (!Q(y) || y === "" || !O(p)) {
|
|
2649
|
-
s("invalidStructure", h, "a sub query has a non-empty string `id` and a `query`");
|
|
2650
|
-
return;
|
|
2651
|
-
}
|
|
2652
|
-
f.has(y) && s("duplicateId", `${h}.id`, `the id "${y}" is used twice`), f.add(y);
|
|
2653
|
-
const N = { inQuery: l, aliasOf: u, outerTables: c };
|
|
2654
|
-
l = /* @__PURE__ */ new Map(), u = /* @__PURE__ */ new Map(), c = void 0;
|
|
2655
|
-
const b = t.length;
|
|
2656
|
-
Ie(p, `${h}.query`, !1), { inQuery: l, aliasOf: u, outerTables: c } = N;
|
|
2657
|
-
const v = t.length === b;
|
|
2658
|
-
(Array.isArray(p.select) ? p.select : []).length !== 1 && s("subqueryColumns", `${h}.query.select`, "a sub query after IN picks exactly one column to compare with"), v && Ce(p, `${h}.query`);
|
|
2659
|
-
}, R = (o, h) => {
|
|
2660
|
-
const { query: y, link: p } = o;
|
|
2661
|
-
if (!O(y) || !Array.isArray(y.joins)) {
|
|
2662
|
-
s("invalidStructure", `${h}.query`, "an EXISTS has a `query` with `from` and `joins`");
|
|
2663
|
-
return;
|
|
2664
|
-
}
|
|
2665
|
-
const N = { inQuery: l, aliasOf: u, outerTables: c };
|
|
2666
|
-
l = /* @__PURE__ */ new Map(), u = /* @__PURE__ */ new Map(), c = N.inQuery, [
|
|
2667
|
-
[y.from, `${h}.query.from`],
|
|
2668
|
-
...y.joins.map((A, M) => [A, `${h}.query.joins[${M}]`])
|
|
2669
|
-
].forEach(([A, M], S) => {
|
|
2670
|
-
const I = new Set(l.keys());
|
|
2671
|
-
C(A, M, S > 0), [...l.keys()].filter((P) => !I.has(P) && N.inQuery.has(P)).forEach((P) => s(
|
|
2672
|
-
"ambiguousTable",
|
|
2673
|
-
`${M}.table`,
|
|
2674
|
-
`"${P}" is a table of the query around it too: give it an alias`
|
|
2675
|
-
));
|
|
2676
|
-
});
|
|
2677
|
-
const v = [
|
|
2678
|
-
...y.joins.map((A, M) => [
|
|
2679
|
-
O(A) ? A.on : void 0,
|
|
2680
|
-
`${h}.query.joins[${M}].on`
|
|
2681
|
-
]),
|
|
2682
|
-
[y.where, `${h}.query.where`]
|
|
2683
|
-
];
|
|
2684
|
-
v.forEach(([A, M]) => {
|
|
2685
|
-
A !== void 0 && V(A, M);
|
|
2686
|
-
});
|
|
2687
|
-
const T = { inQuery: l, aliasOf: u };
|
|
2688
|
-
l = new Map([...N.inQuery, ...T.inQuery]), u = new Map([...N.aliasOf, ...T.aliasOf]), c = void 0, V(p, `${h}.link`), { inQuery: l, aliasOf: u, outerTables: c } = N, [...v, [p, `${h}.link`]].forEach(([A, M]) => {
|
|
2689
|
-
O(A) && ue(A, a) && s("misplacedAggregate", M, "an aggregate (SUM, COUNT, …) can't be used here");
|
|
2690
|
-
});
|
|
2691
|
-
};
|
|
2692
|
-
const we = (o, h, y = []) => {
|
|
2693
|
-
const p = new Set(y);
|
|
2694
|
-
o.forEach((N, b) => {
|
|
2695
|
-
!O(N) || !Q(N.id) || N.id === "" ? s("invalidStructure", `${h}[${b}]`, "every entry has a non-empty string id") : p.has(N.id) ? s("duplicateId", `${h}[${b}].id`, `id "${N.id}" is used twice`) : p.add(N.id);
|
|
2696
|
-
});
|
|
2697
|
-
}, An = (o, h, y) => Array.isArray(o[h]) ? o[h] : (o[h] !== void 0 && s("invalidStructure", y, `\`${h}\` is an array`), []), Ie = (o, h, y) => {
|
|
2698
|
-
const p = (S) => h ? `${h}.${S}` : S;
|
|
2699
|
-
C(o.from, p("from"), !0);
|
|
2700
|
-
const { from: N } = o, b = O(N) && N.query !== void 0 ? N.id : void 0;
|
|
2701
|
-
O(N) && N.query !== void 0 && (!Q(b) || b === "") && s("invalidStructure", p("from"), "a derived table has a non-empty string id");
|
|
2702
|
-
const v = Array.isArray(o.joins) ? o.joins : [];
|
|
2703
|
-
Array.isArray(o.joins) || s("invalidStructure", p("joins"), "`joins` is an array"), v.forEach((S, I) => C(S, p(`joins[${I}]`), !0)), we(v, p("joins"), Q(b) ? [b] : []), v.forEach((S, I) => {
|
|
2704
|
-
if (O(S)) {
|
|
2705
|
-
if (!Ht.includes(S.joinType)) {
|
|
2706
|
-
const P = JSON.stringify(S.joinType);
|
|
2707
|
-
s("invalidStructure", p(`joins[${I}].joinType`), `unknown join type ${P}`);
|
|
2708
|
-
}
|
|
2709
|
-
S.joinType !== "cross" && (S.on !== void 0 ? V(S.on, p(`joins[${I}].on`)) : s("incomplete", p(`joins[${I}].on`), "a join with no ON condition"));
|
|
2710
|
-
}
|
|
2711
|
-
}), o.where !== void 0 && V(o.where, p("where"));
|
|
2712
|
-
const T = Array.isArray(o.select) ? o.select : [];
|
|
2713
|
-
Array.isArray(o.select) || s("invalidStructure", p("select"), "`select` is an array"), we(T, p("select")), T.forEach((S, I) => {
|
|
2714
|
-
O(S) && (m(S.alias, Y, p(`select[${I}].alias`), "an alias"), pe(S.alias) || s("invalidStructure", p(`select[${I}].alias`), "an alias is a string"), V(S.expression, p(`select[${I}].expression`), !0));
|
|
2715
|
-
});
|
|
2716
|
-
const A = An(o, "groupBy", p("groupBy"));
|
|
2717
|
-
if (we(A, p("groupBy")), A.forEach((S, I) => {
|
|
2718
|
-
O(S) && (pe(S.fromSelect) || s("invalidStructure", p(`groupBy[${I}].fromSelect`), "`fromSelect` is a column's id"), V(S.expression, p(`groupBy[${I}].expression`), !0));
|
|
2719
|
-
}), o.having !== void 0 && V(o.having, p("having")), o.distinct !== void 0 && typeof o.distinct != "boolean" && s("invalidStructure", p("distinct"), "`distinct` is a boolean"), !y) {
|
|
2720
|
-
["orderBy", "limit", "offset", "shared"].forEach((S) => {
|
|
2721
|
-
o[S] !== void 0 && s("invalidStructure", p(S), `a derived table has no \`${S}\``);
|
|
2722
|
-
});
|
|
2723
|
-
return;
|
|
2724
|
-
}
|
|
2725
|
-
const M = An(o, "orderBy", "orderBy");
|
|
2726
|
-
we(M, "orderBy"), M.forEach((S, I) => {
|
|
2727
|
-
O(S) && (S.direction !== "asc" && S.direction !== "desc" && s("invalidStructure", `orderBy[${I}].direction`, 'a sort direction is "asc" or "desc"'), V(S.expression, `orderBy[${I}].expression`, !0));
|
|
2728
|
-
}), ["limit", "offset"].forEach((S) => {
|
|
2729
|
-
const I = o[S];
|
|
2730
|
-
I !== void 0 && !(Number.isInteger(I) && I >= 0) ? s("invalidStructure", S, `\`${S}\` is a non-negative integer`) : I !== void 0 && !Number.isSafeInteger(I) && s("tooLong", S, `\`${S}\` is past ${Number.MAX_SAFE_INTEGER}, which is exact`);
|
|
2731
|
-
});
|
|
2732
|
-
}, Qt = (o, h, y) => {
|
|
2733
|
-
const p = `${h}.query`, { union: N } = o;
|
|
2734
|
-
if (!Array.isArray(N) || N.length < 1 || typeof o.all != "boolean") {
|
|
2735
|
-
s("invalidStructure", p, "a union has one or more branches in `union`, and a boolean `all`"), w(y, `${h}.alias`), l.set(y, void 0);
|
|
2736
|
-
return;
|
|
2737
|
-
}
|
|
2738
|
-
let b = [];
|
|
2739
|
-
const v = [];
|
|
2740
|
-
let T = !0;
|
|
2741
|
-
N.forEach((A, M) => {
|
|
2742
|
-
const S = `${p}.union[${M}]`;
|
|
2743
|
-
if (!O(A)) {
|
|
2744
|
-
s("invalidStructure", S, "a union's branch is a query"), T = !1;
|
|
2745
|
-
return;
|
|
2746
|
-
}
|
|
2747
|
-
const I = { inQuery: l, aliasOf: u, outerTables: c };
|
|
2748
|
-
l = /* @__PURE__ */ new Map(), u = /* @__PURE__ */ new Map(), c = void 0;
|
|
2749
|
-
const P = t.length;
|
|
2750
|
-
Ie(A, S, !1);
|
|
2751
|
-
const le = t.length === P, B = (Array.isArray(A.select) ? A.select : []).filter(O), U = le ? B.map((L) => L.expression.type === "literal" ? Re(L.expression) : te(L.expression)) : [];
|
|
2752
|
-
if ({ inQuery: l, aliasOf: u, outerTables: c } = I, !le) {
|
|
2753
|
-
T = !1;
|
|
2754
|
-
return;
|
|
2755
|
-
}
|
|
2756
|
-
Ce(A, S);
|
|
2757
|
-
const q = B.map((L) => K(L));
|
|
2758
|
-
if (B.length === 0) {
|
|
2759
|
-
s("unionColumns", `${S}.select`, "a union's branch names its columns"), T = !1;
|
|
2760
|
-
return;
|
|
2761
|
-
}
|
|
2762
|
-
if (M === 0)
|
|
2763
|
-
b = q, q.forEach((L, j) => {
|
|
2764
|
-
if (L) return;
|
|
2765
|
-
s("derivedColumnName", `${S}.select[${j}]`, "a union's column needs an alias: nothing outside could name it");
|
|
2766
|
-
}), q.forEach((L, j) => {
|
|
2767
|
-
L && q.indexOf(L) !== j && s("derivedColumnName", `${S}.select[${j}]`, `two columns named "${L}"`);
|
|
2768
|
-
});
|
|
2769
|
-
else if (q.length !== b.length) {
|
|
2770
|
-
const L = `${q.length} column(s), where the first branch has ${b.length}`;
|
|
2771
|
-
s("unionColumns", `${S}.select`, L);
|
|
2772
|
-
return;
|
|
2773
|
-
} else
|
|
2774
|
-
q.forEach((L, j) => {
|
|
2775
|
-
if (L === b[j]) return;
|
|
2776
|
-
const _ = `column ${j + 1} is "${String(b[j])}" in the first branch, not "${String(L)}"`;
|
|
2777
|
-
s("unionColumns", `${S}.select[${j}]`, _);
|
|
2778
|
-
});
|
|
2779
|
-
U.forEach((L, j) => {
|
|
2780
|
-
if (L === void 0) return;
|
|
2781
|
-
const _ = v[j];
|
|
2782
|
-
if (_ === void 0)
|
|
2783
|
-
v[j] = L;
|
|
2784
|
-
else if (Ue(_) !== Ue(L)) {
|
|
2785
|
-
const se = `column "${String(b[j])}" is ${fe[L]} here, ${fe[_]} in a branch before`;
|
|
2786
|
-
s("unionTypes", `${S}.select[${j}].expression`, se);
|
|
2787
|
-
}
|
|
2788
|
-
});
|
|
2789
|
-
}), w(y, `${h}.alias`), l.set(y, T ? b.filter((A) => A !== void 0) : void 0);
|
|
2790
|
-
};
|
|
2791
|
-
x = (o, h) => {
|
|
2792
|
-
const { query: y, alias: p } = o;
|
|
2793
|
-
if (o.table === void 0 && O(y) && y.union !== void 0 && Q(p) && p !== "") {
|
|
2794
|
-
m(p, Y, `${h}.alias`, "an alias"), Qt(y, h, p);
|
|
2795
|
-
return;
|
|
2796
|
-
}
|
|
2797
|
-
if (o.table !== void 0 || !O(y) || !Q(p) || p === "") {
|
|
2798
|
-
s("invalidStructure", h, "a derived table has a `query` and an `alias`, and no `table`"), Q(p) && p !== "" && (w(p, `${h}.alias`), l.set(p, void 0));
|
|
2799
|
-
return;
|
|
2800
|
-
}
|
|
2801
|
-
m(p, Y, `${h}.alias`, "an alias");
|
|
2802
|
-
const N = { inQuery: l, aliasOf: u, outerTables: c };
|
|
2803
|
-
l = /* @__PURE__ */ new Map(), u = /* @__PURE__ */ new Map(), c = void 0;
|
|
2804
|
-
const b = t.length;
|
|
2805
|
-
Ie(y, `${h}.query`, !1), { inQuery: l, aliasOf: u, outerTables: c } = N;
|
|
2806
|
-
const v = Array.isArray(y.select) ? y.select : [], T = [], A = (I, P) => {
|
|
2807
|
-
T.includes(I) ? s("derivedColumnName", P, `two columns named "${I}"`) : T.push(I);
|
|
2808
|
-
};
|
|
2809
|
-
v.forEach((I, P) => {
|
|
2810
|
-
if (!O(I) || !O(I.expression)) return;
|
|
2811
|
-
const le = K(I);
|
|
2812
|
-
le ? A(le, `${h}.query.select[${P}]`) : s("derivedColumnName", `${h}.query.select[${P}]`, "a derived table's column needs an alias: nothing outside could name it");
|
|
2813
|
-
});
|
|
2814
|
-
const M = t.length === b;
|
|
2815
|
-
M && v.length === 0 && mn(i, y, d).forEach((I) => A(I.name, `${h}.query.select`)), w(p, `${h}.alias`), l.set(p, M || v.length > 0 ? T : void 0), M && Ce(y, `${h}.query`);
|
|
2816
|
-
const { drillAcross: S } = o;
|
|
2817
|
-
S !== void 0 && !(O(S) && typeof S.synced == "boolean" && Number.isInteger(S.copied) && S.copied >= 0) && s("invalidStructure", `${h}.drillAcross`, "`drillAcross` has a boolean `synced` and a non-negative integer `copied`");
|
|
2818
|
-
};
|
|
2819
|
-
const Ce = (o, h) => {
|
|
2820
|
-
var M, S, I, P, le;
|
|
2821
|
-
const y = (B) => h ? `${h}.${B}` : B, p = yn(o);
|
|
2822
|
-
if (p) {
|
|
2823
|
-
const B = o.joins.findIndex((U) => (U.table ?? D(U)) === p.table);
|
|
2824
|
-
s(
|
|
2825
|
-
"joinOrder",
|
|
2826
|
-
y(`joins[${B}].on`),
|
|
2827
|
-
`the join condition of "${p.table}" uses "${p.needs}", which is joined after it`
|
|
2828
|
-
);
|
|
2829
|
-
}
|
|
2830
|
-
const N = (B, U) => {
|
|
2831
|
-
ue(B, a) && s("misplacedAggregate", U, "an aggregate (SUM, COUNT, …) can't be used here");
|
|
2832
|
-
};
|
|
2833
|
-
N(o.where, y("where")), o.joins.forEach((B, U) => {
|
|
2834
|
-
B.joinType !== "cross" && N(B.on, y(`joins[${U}].on`));
|
|
2835
|
-
}), (M = o.groupBy) == null || M.forEach((B, U) => N(B.expression, y(`groupBy[${U}].expression`)));
|
|
2836
|
-
const b = Xn(o, a), v = "isn't a group key or an aggregate (SUM, COUNT, …)";
|
|
2837
|
-
if (b.selectAll && s("notGrouped", y("select"), "a grouped query has to pick its columns, not *"), o.select.forEach((B, U) => {
|
|
2838
|
-
b.select.includes(B.id) && s("notGrouped", y(`select[${U}].expression`), `this column ${v}`);
|
|
2839
|
-
}), (S = o.orderBy) == null || S.forEach((B, U) => {
|
|
2840
|
-
b.orderBy.includes(B.id) && s("notGrouped", `orderBy[${U}].expression`, `this sort key ${v}`);
|
|
2841
|
-
}), b.having && s("notGrouped", y("having"), `a column in this condition ${v}`), r.dialect === "mssql") {
|
|
2842
|
-
const B = [], U = (j, _) => {
|
|
2843
|
-
const [se, Me] = j.length <= _.length ? [j, _] : [_, j];
|
|
2844
|
-
return se.every((In, Cn) => In.direction === Me[Cn].direction && z(In.expression, Me[Cn].expression));
|
|
2845
|
-
}, q = (j) => {
|
|
2846
|
-
var se;
|
|
2847
|
-
return [...j.type === "function" && ((se = j.orderBy) != null && se.length) ? [j.orderBy] : [], ...me(j).flatMap(q)];
|
|
2848
|
-
}, L = (j, _) => {
|
|
2849
|
-
q(j).forEach((se) => {
|
|
2850
|
-
B.some((Me) => !U(Me, se)) && s(
|
|
2851
|
-
"dialectUnsupported",
|
|
2852
|
-
_,
|
|
2853
|
-
"SQL Server sorts every STRING_AGG of a query alike: this one's order differs"
|
|
2854
|
-
), B.push(se);
|
|
2855
|
-
});
|
|
2856
|
-
};
|
|
2857
|
-
o.select.forEach((j, _) => L(j.expression, y(`select[${_}].expression`))), o.having && L(o.having, y("having")), (I = o.orderBy) == null || I.forEach((j, _) => L(j.expression, `orderBy[${_}].expression`));
|
|
2858
|
-
}
|
|
2859
|
-
if (r.allowUnknownFunctions && Mn(o, a)) {
|
|
2860
|
-
const B = (U, q) => {
|
|
2861
|
-
Xt(U, a) && s(
|
|
2862
|
-
"unknownAggregate",
|
|
2863
|
-
q,
|
|
2864
|
-
"a function the editor doesn't know: it isn't made a group key, in case it sums rows up — declare it in `functions` (`aggregate: true` if it does)",
|
|
2865
|
-
"warning"
|
|
2866
|
-
);
|
|
2867
|
-
};
|
|
2868
|
-
o.select.forEach((U, q) => B(U.expression, y(`select[${q}].expression`))), (P = o.orderBy) == null || P.forEach((U, q) => B(U.expression, `orderBy[${q}].expression`)), B(o.having, y("having"));
|
|
2869
|
-
}
|
|
2870
|
-
o.joins.forEach((B, U) => {
|
|
2871
|
-
if (B.joinType === "cross" || !B.on) return;
|
|
2872
|
-
const q = ne(B.on), L = D(B), j = F(o).slice(0, U + 1).map(D);
|
|
2873
|
-
q.some((_) => _ !== L && !j.includes(_)) || q.includes(L) && q.some((_) => j.includes(_)) || s(
|
|
2874
|
-
"unlinkedJoin",
|
|
2875
|
-
y(`joins[${U}].on`),
|
|
2876
|
-
`the join condition of "${L}" doesn't compare it with a table joined before it: every row meets every row it lets through`,
|
|
2877
|
-
"warning"
|
|
2878
|
-
);
|
|
2879
|
-
}), o.joins.forEach((B, U) => {
|
|
2880
|
-
var L;
|
|
2881
|
-
if (!B.query || !((L = B.drillAcross) != null && L.synced)) return;
|
|
2882
|
-
Tr(i, o, B) || s(
|
|
2883
|
-
"drillAcrossOutOfStep",
|
|
2884
|
-
y(`joins[${U}]`),
|
|
2885
|
-
`"${B.alias}" is kept in step with the query but doesn't match its grouping`
|
|
2886
|
-
);
|
|
2887
|
-
const q = H(B.query) ? void 0 : B.query.from.table;
|
|
2888
|
-
q !== void 0 && vr(i, o, q) && s(
|
|
2889
|
-
"drillAcrossFilterMixed",
|
|
2890
|
-
y(`joins[${U}]`),
|
|
2891
|
-
`a condition of the query names both what "${B.alias}" shares and what it doesn't: it isn't applied to it`,
|
|
2892
|
-
"warning"
|
|
2893
|
-
);
|
|
2894
|
-
}), o.joins.forEach((B, U) => {
|
|
2895
|
-
B.joinType !== "cross" || B.query && ot(B.query, a) || s(
|
|
2896
|
-
"crossJoin",
|
|
2897
|
-
y(`joins[${U}]`),
|
|
2898
|
-
`every row of "${D(B)}" meets every row of the query: they multiply`,
|
|
2899
|
-
"warning"
|
|
2900
|
-
);
|
|
2901
|
-
});
|
|
2902
|
-
const T = qr(o, i), A = (B) => s(
|
|
2903
|
-
"fanOut",
|
|
2904
|
-
B,
|
|
2905
|
-
`${T.tables.map((U) => `"${U}"`).join(", ")} rows repeat in this join: a sum, average or count of their columns comes out too high`,
|
|
2906
|
-
"warning"
|
|
2907
|
-
);
|
|
2908
|
-
o.select.forEach((B, U) => {
|
|
2909
|
-
T.select.includes(B.id) && A(y(`select[${U}].expression`));
|
|
2910
|
-
}), T.having && A(y("having")), (le = o.orderBy) == null || le.forEach((B, U) => {
|
|
2911
|
-
T.orderBy.includes(B.id) && A(`orderBy[${U}].expression`);
|
|
2912
|
-
});
|
|
2913
|
-
};
|
|
2914
|
-
n.shared !== void 0 && !Array.isArray(n.shared) && s("invalidStructure", "shared", "`shared` is an array"), we(d, "shared");
|
|
2915
|
-
const kn = /* @__PURE__ */ new Set();
|
|
2916
|
-
if (d.forEach((o, h) => {
|
|
2917
|
-
if (!O(o)) return;
|
|
2918
|
-
const y = `shared[${h}]`, { name: p } = o;
|
|
2919
|
-
!Q(p) || p === "" ? s("invalidStructure", `${y}.name`, "a shared query has a non-empty string `name`") : (m(p, Y, `${y}.name`, "a name"), kn.has(p) && s("duplicateTableName", `${y}.name`, `two shared queries named "${p}"`), kn.add(p));
|
|
2920
|
-
const N = Q(p) && p !== "" ? p : y, b = { inQuery: l, aliasOf: u, outerTables: c };
|
|
2921
|
-
l = /* @__PURE__ */ new Map(), u = /* @__PURE__ */ new Map(), c = void 0, x({ query: o.query, alias: N }, y);
|
|
2922
|
-
const v = l.get(N);
|
|
2923
|
-
({ inQuery: l, aliasOf: u, outerTables: c } = b), Q(o.id) && E.set(o.id, Array.isArray(v) ? v : void 0);
|
|
2924
|
-
}), Ie(n, "", !0), t.length === 0) {
|
|
2925
|
-
const o = n;
|
|
2926
|
-
if (Ce(o, ""), r.requireLimit) {
|
|
2927
|
-
const { max: y } = r.requireLimit === !0 ? {} : r.requireLimit, p = Mn(o, a) && !((xn = o.groupBy) != null && xn.length);
|
|
2928
|
-
o.limit === void 0 && !p ? s("noLimit", "limit", "no LIMIT: the query reads every row", "warning") : y !== void 0 && o.limit !== void 0 && o.limit > y && s("limitTooHigh", "limit", `LIMIT ${o.limit} is above ${y}`, "warning");
|
|
2929
|
-
}
|
|
2930
|
-
Kr(o, i).forEach((y) => {
|
|
2931
|
-
s("paramConflict", "", `param "${y}" is compared against columns of different types`);
|
|
2932
|
-
}), Xr(o).forEach((y) => {
|
|
2933
|
-
s("paramConflict", "", `param "${y}" stands for two different relative dates`);
|
|
2934
|
-
});
|
|
2935
|
-
const h = Hr(o);
|
|
2936
|
-
(Bn = o.orderBy) == null || Bn.forEach((y, p) => {
|
|
2937
|
-
h.includes(y.id) && s(
|
|
2938
|
-
"distinctSort",
|
|
2939
|
-
`orderBy[${p}].expression`,
|
|
2940
|
-
"with DISTINCT, a sort key has to be one of the selected columns"
|
|
2941
|
-
);
|
|
2942
|
-
});
|
|
2943
|
-
}
|
|
2944
|
-
d.forEach((o, h) => {
|
|
2945
|
-
!O(o) || !Q(o.id) || $.has(o.id) || s("unusedShared", `shared[${h}]`, "a shared query used nowhere", "warning");
|
|
2946
|
-
});
|
|
2947
|
-
const qt = r.hideWarnings ?? [];
|
|
2948
|
-
return t.filter((o) => o.severity === "error" || !qt.includes(o.kind));
|
|
2949
|
-
}
|
|
2950
|
-
function ni(e, n = {
|
|
2951
|
-
relativeDate: dt
|
|
2952
|
-
}) {
|
|
2953
|
-
return n.relativeDate(e);
|
|
2954
|
-
}
|
|
2955
|
-
function ti({ unit: e, offset: n, anchor: r }) {
|
|
2956
|
-
if (e === "day" && n === 0) return "today";
|
|
2957
|
-
const t = n < 0 ? `minus_${-n}` : `plus_${n}`;
|
|
2958
|
-
return [e, n === 0 ? "0" : t, r].filter(Boolean).join("_");
|
|
2959
|
-
}
|
|
2960
|
-
const ns = {
|
|
2961
|
-
from: "from",
|
|
2962
|
-
joins: "from",
|
|
2963
|
-
where: "where",
|
|
2964
|
-
groupBy: "group",
|
|
2965
|
-
having: "having",
|
|
2966
|
-
select: "select",
|
|
2967
|
-
distinct: "select",
|
|
2968
|
-
orderBy: "order",
|
|
2969
|
-
limit: "order",
|
|
2970
|
-
offset: "order"
|
|
2971
|
-
};
|
|
2972
|
-
function ri(e, n, r = {}) {
|
|
2973
|
-
const { schema: t, functions: s } = r, i = (f, d) => typeof d == "number" ? f == null ? void 0 : f[d] : void 0, a = (f) => {
|
|
2974
|
-
var d;
|
|
2975
|
-
return f.alias || t && f.table !== void 0 && ((d = Z(t, f.table)) == null ? void 0 : d.label) || f.table || "";
|
|
2976
|
-
}, l = (f, d) => {
|
|
2977
|
-
var x;
|
|
2978
|
-
if (!f || d.length === 0) return [];
|
|
2979
|
-
const [g, E, $] = d;
|
|
2980
|
-
switch (f.type) {
|
|
2981
|
-
case "logical": {
|
|
2982
|
-
const w = i(f.operands, E);
|
|
2983
|
-
return w ? [{ kind: "condition", number: E + 1 }, ...l(w, d.slice(2))] : [];
|
|
2984
|
-
}
|
|
2985
|
-
case "not":
|
|
2986
|
-
return [{ kind: "not" }, ...l(f.operand, d.slice(1))];
|
|
2987
|
-
case "binary": {
|
|
2988
|
-
if (g !== "left" && g !== "right") return [];
|
|
2989
|
-
const w = { kind: "side", side: g }, C = f[g];
|
|
2990
|
-
return (C == null ? void 0 : C.type) === "subquery" && E === "query" ? [w, { kind: "inSubQuery" }, ...m(C.query, d.slice(2))] : (C == null ? void 0 : C.type) === "list" && E === "items" && i(C.items, $) ? [w, { kind: "listItem", number: $ + 1 }] : [w, ...l(C, d.slice(1))];
|
|
2991
|
-
}
|
|
2992
|
-
case "function": {
|
|
2993
|
-
const w = { kind: "function", name: f.name };
|
|
2994
|
-
if (g === "args" && i(f.args, E)) {
|
|
2995
|
-
const G = zn(f.name, s), R = G && ((x = Le(G, E)) == null ? void 0 : x.label);
|
|
2996
|
-
return [
|
|
2997
|
-
w,
|
|
2998
|
-
{ kind: "argument", number: E + 1, ...R && { label: R } },
|
|
2999
|
-
...l(f.args[E], d.slice(2))
|
|
3000
|
-
];
|
|
3001
|
-
}
|
|
3002
|
-
const C = g === "orderBy" ? i(f.orderBy, E) : void 0;
|
|
3003
|
-
return C ? [
|
|
3004
|
-
w,
|
|
3005
|
-
{ kind: "sortKey", number: E + 1 },
|
|
3006
|
-
...l(C.expression, d.slice(3))
|
|
3007
|
-
] : [w];
|
|
3008
|
-
}
|
|
3009
|
-
case "if":
|
|
3010
|
-
return g === "condition" || g === "then" || g === "else" ? [{ kind: "if", part: g }, ...l(f[g], d.slice(1))] : [];
|
|
3011
|
-
case "case": {
|
|
3012
|
-
if (g === "subject" || g === "else")
|
|
3013
|
-
return [{ kind: "case", part: g }, ...l(f[g], d.slice(1))];
|
|
3014
|
-
const w = g === "branches" ? i(f.branches, E) : void 0;
|
|
3015
|
-
return !w || $ !== "when" && $ !== "then" ? [] : [
|
|
3016
|
-
{ kind: "case", part: $, branch: E + 1 },
|
|
3017
|
-
...l(w[$], d.slice(3))
|
|
3018
|
-
];
|
|
3019
|
-
}
|
|
3020
|
-
case "exists": {
|
|
3021
|
-
const w = { kind: "exists", table: a(f.query.from) };
|
|
3022
|
-
return g === "link" ? [w, { kind: "existsLink" }] : g === "query" ? [w, ...m(f.query, d.slice(1))] : [w];
|
|
3023
|
-
}
|
|
3024
|
-
default:
|
|
3025
|
-
return [];
|
|
3026
|
-
}
|
|
3027
|
-
}, u = (f, d) => {
|
|
3028
|
-
if (f.query && d[0] === "query") return c(f.alias, f.query, d.slice(1));
|
|
3029
|
-
const g = f.query ? { kind: "subQuery", name: f.alias } : { kind: "table", name: a(f) };
|
|
3030
|
-
return d[0] === "on" ? [g, { kind: "joinCondition" }, ...l(f.on, d.slice(1))] : [g];
|
|
3031
|
-
};
|
|
3032
|
-
function c(f, d, g, E = "subQuery") {
|
|
3033
|
-
const $ = { kind: E, name: f };
|
|
3034
|
-
if (!H(d)) return [$, ...m(d, g)];
|
|
3035
|
-
const x = g[0] === "union" ? i(Se(d), g[1]) : void 0;
|
|
3036
|
-
if (!x) return [$];
|
|
3037
|
-
const w = g[1] + 1;
|
|
3038
|
-
return [$, { kind: "unionQuery", number: w }, ...m(x, g.slice(2))];
|
|
3039
|
-
}
|
|
3040
|
-
function m(f, d) {
|
|
3041
|
-
var G, R;
|
|
3042
|
-
const [g, E, $] = d, x = ns[String(g)];
|
|
3043
|
-
if (!x) return [];
|
|
3044
|
-
const w = { kind: "section", section: x }, C = d.slice(3);
|
|
3045
|
-
switch (g) {
|
|
3046
|
-
// A sub query is a place of its own: no "From Tables" before it.
|
|
3047
|
-
case "from":
|
|
3048
|
-
return f.from.query ? E === "query" ? c(f.from.alias, f.from.query, d.slice(2)) : [{ kind: "subQuery", name: f.from.alias }] : [w, { kind: "table", name: a(f.from) }];
|
|
3049
|
-
case "joins": {
|
|
3050
|
-
const X = i(f.joins, E);
|
|
3051
|
-
return X ? X.query ? u(X, d.slice(2)) : [w, ...u(X, d.slice(2))] : [w];
|
|
3052
|
-
}
|
|
3053
|
-
case "where":
|
|
3054
|
-
case "having":
|
|
3055
|
-
return [w, ...l(f[g], d.slice(1))];
|
|
3056
|
-
case "select": {
|
|
3057
|
-
const X = i(f.select, E);
|
|
3058
|
-
if (!X) return [w];
|
|
3059
|
-
const { expression: ee, alias: be } = X, te = ee.type === "field" && t && ee.table !== void 0 ? (R = (G = Z(t, ee.table)) == null ? void 0 : G.columns.find((k) => k.name === ee.column)) == null ? void 0 : R.label : void 0, re = be || te;
|
|
3060
|
-
return [
|
|
3061
|
-
w,
|
|
3062
|
-
{ kind: "column", number: E + 1, ...re && { name: re } },
|
|
3063
|
-
...$ === "expression" ? l(ee, C) : []
|
|
3064
|
-
];
|
|
3065
|
-
}
|
|
3066
|
-
case "groupBy":
|
|
3067
|
-
case "orderBy": {
|
|
3068
|
-
const X = i(f[g], E);
|
|
3069
|
-
return X ? [
|
|
3070
|
-
w,
|
|
3071
|
-
{ kind: g === "groupBy" ? "groupKey" : "sortKey", number: E + 1 },
|
|
3072
|
-
...$ === "expression" ? l(X.expression, C) : []
|
|
3073
|
-
] : [w];
|
|
3074
|
-
}
|
|
3075
|
-
default:
|
|
3076
|
-
return [w];
|
|
3077
|
-
}
|
|
3078
|
-
}
|
|
3079
|
-
if (n[0] === "shared") {
|
|
3080
|
-
const f = i(e.shared, n[1]);
|
|
3081
|
-
return f ? n[2] === "query" ? c(f.name, f.query, n.slice(3), "sharedQuery") : [{ kind: "sharedQuery", name: f.name }] : [];
|
|
3082
|
-
}
|
|
3083
|
-
return m(e, n);
|
|
3084
|
-
}
|
|
3085
|
-
export {
|
|
3086
|
-
Wr as $,
|
|
3087
|
-
Ss as A,
|
|
3088
|
-
Gr as B,
|
|
3089
|
-
Qr as C,
|
|
3090
|
-
ar as D,
|
|
3091
|
-
on as E,
|
|
3092
|
-
Ws as F,
|
|
3093
|
-
Ks as G,
|
|
3094
|
-
ni as H,
|
|
3095
|
-
Zs as I,
|
|
3096
|
-
ei as J,
|
|
3097
|
-
ri as K,
|
|
3098
|
-
ss as L,
|
|
3099
|
-
nt as M,
|
|
3100
|
-
rr as N,
|
|
3101
|
-
st as O,
|
|
3102
|
-
ds as P,
|
|
3103
|
-
zs as Q,
|
|
3104
|
-
fs as R,
|
|
3105
|
-
wr as S,
|
|
3106
|
-
hs as T,
|
|
3107
|
-
ms as U,
|
|
3108
|
-
as as V,
|
|
3109
|
-
$n as W,
|
|
3110
|
-
Tt as X,
|
|
3111
|
-
Os as Y,
|
|
3112
|
-
Cs as Z,
|
|
3113
|
-
Us as _,
|
|
3114
|
-
zt as a,
|
|
3115
|
-
Pr as a0,
|
|
3116
|
-
Ls as a1,
|
|
3117
|
-
Ms as a2,
|
|
3118
|
-
Cr as a3,
|
|
3119
|
-
Fr as a4,
|
|
3120
|
-
Re as a5,
|
|
3121
|
-
er as a6,
|
|
3122
|
-
vn as a7,
|
|
3123
|
-
ti as a8,
|
|
3124
|
-
ls as a9,
|
|
3125
|
-
ne as aA,
|
|
3126
|
-
Ee as aB,
|
|
3127
|
-
Ve as aC,
|
|
3128
|
-
ke as aD,
|
|
3129
|
-
en as aE,
|
|
3130
|
-
Ys as aF,
|
|
3131
|
-
Hs as aG,
|
|
3132
|
-
ws as aa,
|
|
3133
|
-
ot as ab,
|
|
3134
|
-
bs as ac,
|
|
3135
|
-
gs as ad,
|
|
3136
|
-
Hr as ae,
|
|
3137
|
-
at as af,
|
|
3138
|
-
Xs as ag,
|
|
3139
|
-
is as ah,
|
|
3140
|
-
vs as ai,
|
|
3141
|
-
fn as aj,
|
|
3142
|
-
pr as ak,
|
|
3143
|
-
us as al,
|
|
3144
|
-
hr as am,
|
|
3145
|
-
fr as an,
|
|
3146
|
-
Pe as ao,
|
|
3147
|
-
$s as ap,
|
|
3148
|
-
_n as aq,
|
|
3149
|
-
ct as ar,
|
|
3150
|
-
_r as as,
|
|
3151
|
-
qr as at,
|
|
3152
|
-
oe as au,
|
|
3153
|
-
os as av,
|
|
3154
|
-
ps as aw,
|
|
3155
|
-
ys as ax,
|
|
3156
|
-
nr as ay,
|
|
3157
|
-
tt as az,
|
|
3158
|
-
Zt as b,
|
|
3159
|
-
cs as c,
|
|
3160
|
-
Is as d,
|
|
3161
|
-
_s as e,
|
|
3162
|
-
Fs as f,
|
|
3163
|
-
Ds as g,
|
|
3164
|
-
Qs as h,
|
|
3165
|
-
Ps as i,
|
|
3166
|
-
jr as j,
|
|
3167
|
-
Nr as k,
|
|
3168
|
-
qs as l,
|
|
3169
|
-
wt as m,
|
|
3170
|
-
Gs as n,
|
|
3171
|
-
js as o,
|
|
3172
|
-
br as p,
|
|
3173
|
-
Ts as q,
|
|
3174
|
-
Rs as r,
|
|
3175
|
-
Js as s,
|
|
3176
|
-
As as t,
|
|
3177
|
-
Vs as u,
|
|
3178
|
-
ks as v,
|
|
3179
|
-
Bs as w,
|
|
3180
|
-
xs as x,
|
|
3181
|
-
Ns as y,
|
|
3182
|
-
Es as z
|
|
3183
|
-
};
|