@nongt/visql-core 0.6.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{core-api-D4QbGLG-.js → core-api-jaQDPXVK.js} +459 -429
- package/dist/core.js +3 -3
- package/dist/fixtures.js +1 -1
- package/dist/{issueLocation-BcILIZqd.js → issueLocation-DEIwB1DW.js} +944 -932
- package/dist/{schema-z95dxLxu.js → schema-DFC26AhZ.js} +142 -126
- package/dist/sql-import.js +613 -627
- package/dist/types/core/ast.d.ts +1 -1
- package/dist/types/core/i18n/defaultMessages.d.ts +2 -0
- package/dist/types/core/params.d.ts +10 -0
- package/dist/types/core/schema.d.ts +19 -0
- package/dist/types/core/sqlGen.d.ts +10 -0
- package/dist/types/core-api.d.ts +3 -3
- package/package.json +1 -1
package/dist/sql-import.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import { c as Dt, d as nt, p as
|
|
1
|
+
var Ct = Object.defineProperty;
|
|
2
|
+
var Lt = (o, T, D) => T in o ? Ct(o, T, { enumerable: !0, configurable: !0, writable: !0, value: D }) : o[T] = D;
|
|
3
|
+
var It = (o, T, D) => Lt(o, typeof T != "symbol" ? T + "" : T, D);
|
|
4
|
+
import { c as Dt, d as nt, p as gt, D as Ut, e as bt, g as Rt, j as Mt, q as qt, k as Nt, l as wt, m as xt, n as Ft, t as $t, o as Bt, r as Wt } from "./schema-DFC26AhZ.js";
|
|
5
5
|
import { t as Pt, f as _t, a as Gt } from "./calendarDate-CYlr9TvX.js";
|
|
6
|
-
function
|
|
7
|
-
const
|
|
6
|
+
function kt(o, T, D = T) {
|
|
7
|
+
const k = o.slice(0, T);
|
|
8
8
|
return {
|
|
9
9
|
offset: T,
|
|
10
|
-
line:
|
|
10
|
+
line: k.split(`
|
|
11
11
|
`).length,
|
|
12
|
-
column: T -
|
|
12
|
+
column: T - k.lastIndexOf(`
|
|
13
13
|
`),
|
|
14
|
-
length:
|
|
14
|
+
length: D - T
|
|
15
15
|
};
|
|
16
16
|
}
|
|
17
17
|
class ft extends Error {
|
|
18
|
-
constructor(
|
|
19
|
-
super(
|
|
20
|
-
|
|
21
|
-
this.name = "SqlSyntaxError", this.at =
|
|
18
|
+
constructor(D, k, v, a = v) {
|
|
19
|
+
super(D);
|
|
20
|
+
It(this, "at");
|
|
21
|
+
this.name = "SqlSyntaxError", this.at = kt(k, v, a);
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
const Ht = {
|
|
@@ -64,28 +64,28 @@ const Ht = {
|
|
|
64
64
|
mssql: /^(?::([A-Za-z_]\w*)|(@)([A-Za-z_]\w*))/
|
|
65
65
|
};
|
|
66
66
|
function Zt(o, T) {
|
|
67
|
-
const
|
|
68
|
-
const M =
|
|
69
|
-
let
|
|
70
|
-
for (;
|
|
71
|
-
const
|
|
72
|
-
if (M &&
|
|
73
|
-
|
|
74
|
-
else if (
|
|
75
|
-
|
|
67
|
+
const D = [...jt[T], ...Xt], k = T === "mysql", v = (i, b, I) => {
|
|
68
|
+
const M = k && i === "string";
|
|
69
|
+
let J = "", q = b + 1;
|
|
70
|
+
for (; q < o.length; ) {
|
|
71
|
+
const d = o[q];
|
|
72
|
+
if (M && d === "\\" && q + 1 < o.length)
|
|
73
|
+
J += Kt[o[q + 1]] ?? o[q + 1], q += 2;
|
|
74
|
+
else if (d === I && o[q + 1] === I)
|
|
75
|
+
J += I, q += 2;
|
|
76
76
|
else {
|
|
77
|
-
if (
|
|
78
|
-
return { kind: i, value:
|
|
79
|
-
|
|
77
|
+
if (d === I)
|
|
78
|
+
return { kind: i, value: J, end: q + 1 };
|
|
79
|
+
J += d, q += 1;
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
|
-
const
|
|
83
|
-
throw new ft(`${
|
|
82
|
+
const C = i === "string" ? "A text" : "A quoted name";
|
|
83
|
+
throw new ft(`${C} is never closed`, o, b, o.length);
|
|
84
84
|
}, a = (i) => {
|
|
85
|
-
var
|
|
86
|
-
const
|
|
87
|
-
if (/\s/.test(
|
|
88
|
-
if (I.startsWith("--") ||
|
|
85
|
+
var F, H;
|
|
86
|
+
const b = o[i], I = o.slice(i), M = Ht[T][b], J = (F = Yt.exec(I)) == null ? void 0 : F[0], q = (H = zt.exec(I)) == null ? void 0 : H[0], C = Vt[T].exec(I), d = D.find((w) => I.startsWith(w));
|
|
87
|
+
if (/\s/.test(b)) return { end: i + 1 };
|
|
88
|
+
if (I.startsWith("--") || k && b === "#") {
|
|
89
89
|
const w = o.indexOf(`
|
|
90
90
|
`, i);
|
|
91
91
|
return { end: w < 0 ? o.length : w };
|
|
@@ -96,45 +96,45 @@ function Zt(o, T) {
|
|
|
96
96
|
throw new ft("A comment is never closed", o, i, i + 2);
|
|
97
97
|
return { end: w + 2 };
|
|
98
98
|
}
|
|
99
|
-
if (
|
|
99
|
+
if (b === "'" || k && b === '"') return v("string", i, b);
|
|
100
100
|
if (/^N'/i.test(I)) return v("string", i + 1, "'");
|
|
101
101
|
if (M) return v("quoted", i, M);
|
|
102
|
-
if (
|
|
103
|
-
if (
|
|
104
|
-
if (
|
|
102
|
+
if (J) return { kind: "number", value: J, end: i + J.length };
|
|
103
|
+
if (q) return { kind: "word", value: q, end: i + q.length };
|
|
104
|
+
if (C)
|
|
105
105
|
return {
|
|
106
106
|
kind: "param",
|
|
107
|
-
value:
|
|
108
|
-
style:
|
|
109
|
-
end: i +
|
|
107
|
+
value: C[1] ?? C[3],
|
|
108
|
+
style: C[2] ?? ":",
|
|
109
|
+
end: i + C[0].length
|
|
110
110
|
};
|
|
111
|
-
if (
|
|
112
|
-
throw new ft(`Unexpected character "${
|
|
113
|
-
},
|
|
111
|
+
if (d) return { kind: "op", value: d, end: i + d.length };
|
|
112
|
+
throw new ft(`Unexpected character "${b}"`, o, i, i + 1);
|
|
113
|
+
}, p = [];
|
|
114
114
|
for (let i = 0; i < o.length; ) {
|
|
115
|
-
const
|
|
116
|
-
|
|
115
|
+
const b = a(i);
|
|
116
|
+
b.kind && p.push({ ...b, start: i }), i = b.end;
|
|
117
117
|
}
|
|
118
|
-
return
|
|
118
|
+
return p.push({
|
|
119
119
|
kind: "end",
|
|
120
120
|
value: "",
|
|
121
121
|
start: o.length,
|
|
122
122
|
end: o.length
|
|
123
|
-
}),
|
|
123
|
+
}), p;
|
|
124
124
|
}
|
|
125
|
-
function
|
|
125
|
+
function it(o, T, D, k, v = "error") {
|
|
126
126
|
const a = v === "leftOut" && o.lenient;
|
|
127
127
|
o.problems.push({
|
|
128
128
|
severity: v === "warning" || a ? "warning" : "error",
|
|
129
129
|
kind: T,
|
|
130
|
-
message: a ? `${
|
|
131
|
-
at:
|
|
130
|
+
message: a ? `${D} (left out)` : D,
|
|
131
|
+
at: kt(o.sql, k.start, k.end)
|
|
132
132
|
});
|
|
133
133
|
}
|
|
134
134
|
function st(o, T) {
|
|
135
135
|
if (o.includes(T)) return T;
|
|
136
|
-
const
|
|
137
|
-
return
|
|
136
|
+
const D = T.toLowerCase(), k = o.filter((v) => v.toLowerCase() === D);
|
|
137
|
+
return k.length === 1 ? k[0] : void 0;
|
|
138
138
|
}
|
|
139
139
|
const Qt = {
|
|
140
140
|
mysql: {
|
|
@@ -178,200 +178,186 @@ function At(o) {
|
|
|
178
178
|
const se = (o) => o.map((T) => T.value).join("."), re = (o) => ({
|
|
179
179
|
start: o[0].span.start,
|
|
180
180
|
end: o[o.length - 1].span.end
|
|
181
|
-
}), ht = (o) => (o == null ? void 0 : o.kind) === "name" && o.parts.length === 1 && !o.parts[0].quoted ? o.parts[0].value.toUpperCase() : void 0,
|
|
182
|
-
function oe(o, T,
|
|
183
|
-
const
|
|
181
|
+
}), ht = (o) => (o == null ? void 0 : o.kind) === "name" && o.parts.length === 1 && !o.parts[0].quoted ? o.parts[0].value.toUpperCase() : void 0, pt = (o) => o !== void 0 && Dt.includes(o);
|
|
182
|
+
function oe(o, T, D) {
|
|
183
|
+
const k = (t, r, u) => (it(o, t, r, u), Tt()), v = (t, r) => k("unsupported", t, r), a = (t) => o.sql.slice(t.start, t.end), p = (t) => ct(t), i = (t) => {
|
|
184
184
|
const r = te[t.toUpperCase()];
|
|
185
|
-
return r && (!r.dialects || r.dialects.includes(o.dialect)) ?
|
|
186
|
-
},
|
|
187
|
-
const
|
|
188
|
-
return !
|
|
185
|
+
return r && (!r.dialects || r.dialects.includes(o.dialect)) ? gt(r.name, o.functions) : gt(t, o.functions);
|
|
186
|
+
}, b = (t, r) => k("unknownFunction", `${t} isn't a function the query can use`, r), I = (t, r, u, h = {}) => {
|
|
187
|
+
const U = i(t);
|
|
188
|
+
return !U && !o.allowUnknownFunctions ? b(t, u) : h.distinct && U && !U.aggregate ? v(`DISTINCT goes only in an aggregate, not in ${U.name}`, u) : {
|
|
189
189
|
type: "function",
|
|
190
|
-
name: (
|
|
190
|
+
name: (U == null ? void 0 : U.name) ?? t,
|
|
191
191
|
args: r,
|
|
192
192
|
...h.distinct && { distinct: !0 },
|
|
193
193
|
...h.orderBy && { orderBy: h.orderBy }
|
|
194
194
|
};
|
|
195
|
-
}, M = (t) => rt(t),
|
|
196
|
-
var
|
|
195
|
+
}, M = (t) => rt(t), J = (t) => {
|
|
196
|
+
var $, Z;
|
|
197
197
|
const r = t.parts[t.parts.length - 1], u = t.parts[t.parts.length - 2];
|
|
198
198
|
if (!u) {
|
|
199
|
-
const
|
|
200
|
-
if (
|
|
199
|
+
const x = ($ = D.alias) == null ? void 0 : $.call(D, r);
|
|
200
|
+
if (x) return x;
|
|
201
201
|
}
|
|
202
|
-
for (let
|
|
203
|
-
const
|
|
204
|
-
const y = { type: "field", table:
|
|
205
|
-
return
|
|
202
|
+
for (let x = D.scope, z = !1; x; x = x.outer, z = !0) {
|
|
203
|
+
const s = (c, f) => {
|
|
204
|
+
const y = { type: "field", table: c, column: f };
|
|
205
|
+
return z && o.outerFields.add(y), y;
|
|
206
206
|
};
|
|
207
207
|
if (u) {
|
|
208
|
-
const
|
|
209
|
-
if (
|
|
210
|
-
const y = st(
|
|
211
|
-
return y ?
|
|
208
|
+
const c = st(x.tables.map((y) => y.ref), u.value), f = x.tables.find((y) => y.ref === c);
|
|
209
|
+
if (f) {
|
|
210
|
+
const y = st(f.columns, r.value);
|
|
211
|
+
return y ? s(f.ref, y) : k("unknownColumn", `${f.ref} has no column "${r.value}"`, r.span);
|
|
212
212
|
}
|
|
213
213
|
} else {
|
|
214
|
-
const
|
|
215
|
-
const y = st(
|
|
216
|
-
return y ? [{ ref:
|
|
214
|
+
const c = x.tables.flatMap((f) => {
|
|
215
|
+
const y = st(f.columns, r.value);
|
|
216
|
+
return y ? [{ ref: f.ref, col: y }] : [];
|
|
217
217
|
});
|
|
218
|
-
if (
|
|
219
|
-
const
|
|
220
|
-
return
|
|
218
|
+
if (c.length > 1) {
|
|
219
|
+
const f = c.map((y) => y.ref).join(" and ");
|
|
220
|
+
return k(
|
|
221
221
|
"ambiguousColumn",
|
|
222
|
-
`"${r.value}" is a column of ${
|
|
222
|
+
`"${r.value}" is a column of ${f}: name its table`,
|
|
223
223
|
t.span
|
|
224
224
|
);
|
|
225
225
|
}
|
|
226
|
-
if (
|
|
226
|
+
if (c.length === 1) return s(c[0].ref, c[0].col);
|
|
227
227
|
}
|
|
228
228
|
}
|
|
229
229
|
const h = !u && !r.quoted ? ee[r.value.toUpperCase()] : void 0;
|
|
230
230
|
if (h) return I(h, [], t.span);
|
|
231
|
-
if ((
|
|
231
|
+
if ((Z = D.scope.around) == null ? void 0 : Z.tables.some((x) => u ? st([x.ref], u.value) : st(x.columns, r.value)))
|
|
232
232
|
return v(
|
|
233
233
|
`${a(t.span)} is in the query around this sub query, which it can't name`,
|
|
234
234
|
t.span
|
|
235
235
|
);
|
|
236
|
-
const
|
|
237
|
-
if (
|
|
238
|
-
const
|
|
239
|
-
return
|
|
236
|
+
const P = u && D.scope.tables.find((x) => x.table !== void 0 && x.ref !== x.table && st([x.table], u.value));
|
|
237
|
+
if (P) {
|
|
238
|
+
const x = `${P.table} goes by its alias here: ${P.ref}`;
|
|
239
|
+
return k("unknownTable", x, u.span);
|
|
240
240
|
}
|
|
241
|
-
return u ?
|
|
242
|
-
},
|
|
241
|
+
return u ? k("unknownTable", `No table here is named "${u.value}"`, u.span) : k("unknownColumn", `No table here has a column "${r.value}"`, r.span);
|
|
242
|
+
}, q = (t) => {
|
|
243
243
|
if (t.kind === "binary" && t.op === "*" && t.right.kind === "interval") {
|
|
244
|
-
const h =
|
|
245
|
-
return h && ae(h.amount) ? { amount:
|
|
244
|
+
const h = q(t.right);
|
|
245
|
+
return h && ae(h.amount) ? { amount: p(t.left), unit: h.unit } : void 0;
|
|
246
246
|
}
|
|
247
247
|
if (t.kind !== "interval") return;
|
|
248
248
|
if (t.unit === "MICROSECOND" && t.value.kind === "binary" && t.value.op === "*" && t.value.right.kind === "number" && t.value.right.text === "1000")
|
|
249
|
-
return { amount:
|
|
250
|
-
if (t.unit) return
|
|
249
|
+
return { amount: p(t.value.left), unit: "MILLISECOND" };
|
|
250
|
+
if (t.unit) return pt(t.unit) ? { amount: p(t.value), unit: t.unit } : void 0;
|
|
251
251
|
const r = t.value.kind === "string" ? /^\s*(-?\d+(?:\.\d+)?)\s+([A-Za-z]+?)s?\s*$/.exec(t.value.value) : null, u = r == null ? void 0 : r[2].toUpperCase();
|
|
252
|
-
return r &&
|
|
253
|
-
},
|
|
254
|
-
const r =
|
|
252
|
+
return r && pt(u) ? { amount: rt(Number(r[1])), unit: u } : void 0;
|
|
253
|
+
}, C = (t) => {
|
|
254
|
+
const r = q(t.right);
|
|
255
255
|
if (r)
|
|
256
256
|
return I(t.op === "+" ? "DATE_ADD" : "DATE_SUB", [
|
|
257
|
-
|
|
257
|
+
p(t.left),
|
|
258
258
|
r.amount,
|
|
259
259
|
M(r.unit)
|
|
260
260
|
], t.span);
|
|
261
|
-
const u = t.op === "+" ?
|
|
262
|
-
return u && I("DATE_ADD", [
|
|
263
|
-
},
|
|
264
|
-
const [u, h,
|
|
261
|
+
const u = t.op === "+" ? q(t.left) : void 0;
|
|
262
|
+
return u && I("DATE_ADD", [p(t.right), u.amount, M(u.unit)], t.span);
|
|
263
|
+
}, d = (t, r) => {
|
|
264
|
+
const [u, h, U] = t.args, P = t.args.length;
|
|
265
265
|
switch (r) {
|
|
266
266
|
case "DATE_ADD":
|
|
267
267
|
case "DATE_SUB":
|
|
268
268
|
case "ADDDATE":
|
|
269
269
|
case "SUBDATE": {
|
|
270
|
-
const
|
|
271
|
-
return
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
M(
|
|
270
|
+
const $ = P === 2 ? q(h) : void 0;
|
|
271
|
+
return $ && I(r === "DATE_ADD" || r === "ADDDATE" ? "DATE_ADD" : "DATE_SUB", [
|
|
272
|
+
p(u),
|
|
273
|
+
$.amount,
|
|
274
|
+
M($.unit)
|
|
275
275
|
], t.span);
|
|
276
276
|
}
|
|
277
277
|
case "DATEADD": {
|
|
278
|
-
const
|
|
279
|
-
if (
|
|
280
|
-
const
|
|
281
|
-
return I(
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
M(
|
|
278
|
+
const $ = ht(u);
|
|
279
|
+
if (P !== 3 || !pt($)) return;
|
|
280
|
+
const Z = h.kind === "unary" && h.op === "-";
|
|
281
|
+
return I(Z ? "DATE_SUB" : "DATE_ADD", [
|
|
282
|
+
p(U),
|
|
283
|
+
p(Z ? h.operand : h),
|
|
284
|
+
M($)
|
|
285
285
|
], t.span);
|
|
286
286
|
}
|
|
287
287
|
case "TIMESTAMPDIFF":
|
|
288
288
|
case "DATEDIFF": {
|
|
289
|
-
const
|
|
290
|
-
return
|
|
289
|
+
const $ = ht(u);
|
|
290
|
+
return P === 3 && pt($) ? I("DATEDIFF", [p(h), p(U), M($)], t.span) : r === "DATEDIFF" && P === 2 ? I("DATEDIFF", [p(h), p(u), M("DAY")], t.span) : void 0;
|
|
291
291
|
}
|
|
292
292
|
case "DATEPART": {
|
|
293
|
-
const
|
|
294
|
-
return
|
|
293
|
+
const $ = ht(u);
|
|
294
|
+
return P === 2 && pt($) ? I("EXTRACT", [p(h), M($)], t.span) : void 0;
|
|
295
295
|
}
|
|
296
296
|
case "DATE_TRUNC": {
|
|
297
|
-
const
|
|
298
|
-
return
|
|
297
|
+
const $ = (u == null ? void 0 : u.kind) === "string" ? u.value.toUpperCase() : void 0;
|
|
298
|
+
return P === 2 && pt($) ? I("DATE_TRUNC", [p(h), M($)], t.span) : void 0;
|
|
299
299
|
}
|
|
300
300
|
default:
|
|
301
301
|
return;
|
|
302
302
|
}
|
|
303
|
-
},
|
|
304
|
-
const h = Jt[o.dialect],
|
|
305
|
-
if (
|
|
306
|
-
return v(`${r} takes one value${
|
|
303
|
+
}, F = (t, r) => t.map((u) => (u.nulls && it(o, "unsupported", "NULLS FIRST / LAST can't be imported", u.span, "leftOut"), { expression: p(r(u.expr)), direction: u.direction ?? "asc" })), H = (t, r, u) => {
|
|
304
|
+
const h = Jt[o.dialect], U = (c) => c.kind === "cast" && c.type.toUpperCase() === h ? c.operand : c, P = r === "GROUP_CONCAT", [$, Z, ...x] = t.args;
|
|
305
|
+
if (!$ || x.length > 0 || P && Z)
|
|
306
|
+
return v(`${r} takes one value${P ? "" : " and a separator"}`, u);
|
|
307
307
|
if (t.orderBy && t.withinGroup)
|
|
308
308
|
return v("Two orders for one STRING_AGG", u);
|
|
309
|
-
const
|
|
310
|
-
if (
|
|
311
|
-
return v("A separator can only be a text",
|
|
312
|
-
const
|
|
309
|
+
const z = P ? t.separator ?? { kind: "string", value: ",", span: u } : Z;
|
|
310
|
+
if (z && z.kind !== "string")
|
|
311
|
+
return v("A separator can only be a text", z.span);
|
|
312
|
+
const s = t.orderBy ?? t.withinGroup;
|
|
313
313
|
return I("STRING_AGG", [
|
|
314
|
-
|
|
314
|
+
p(U($)),
|
|
315
315
|
// Without one, STRING_AGG's separator is ', '.
|
|
316
|
-
...
|
|
316
|
+
...z && z.value !== ", " ? [rt(z.value)] : []
|
|
317
317
|
], u, {
|
|
318
318
|
distinct: t.distinct,
|
|
319
319
|
// With DISTINCT, PostgreSQL sorts by the value as cast.
|
|
320
|
-
...
|
|
320
|
+
...s && { orderBy: F(s, U) }
|
|
321
321
|
});
|
|
322
322
|
}, w = (t) => {
|
|
323
|
-
var
|
|
323
|
+
var Z, x;
|
|
324
324
|
const r = se(t.name), u = r.toUpperCase(), h = re(t.name);
|
|
325
325
|
if (t.over) return v("A window function (OVER …) can't be imported", t.over);
|
|
326
326
|
if (t.filter) return v("FILTER (…) can't be imported", t.filter);
|
|
327
327
|
if ((u === "IF" || u === "IIF") && t.args.length === 3) {
|
|
328
|
-
const [
|
|
328
|
+
const [z, s, c] = t.args.map(p);
|
|
329
329
|
return {
|
|
330
330
|
type: "if",
|
|
331
|
-
condition:
|
|
332
|
-
then:
|
|
333
|
-
else:
|
|
331
|
+
condition: z,
|
|
332
|
+
then: s,
|
|
333
|
+
else: c
|
|
334
334
|
};
|
|
335
335
|
}
|
|
336
|
-
const
|
|
337
|
-
if (
|
|
336
|
+
const U = d(t, u);
|
|
337
|
+
if (U) return U;
|
|
338
338
|
if (u === "STRING_AGG" || u === "GROUP_CONCAT" || u === "LISTAGG")
|
|
339
|
-
return
|
|
340
|
-
const
|
|
341
|
-
if (
|
|
339
|
+
return H(t, u, t.span);
|
|
340
|
+
const P = ((Z = t.orderBy) == null ? void 0 : Z[0]) ?? ((x = t.withinGroup) == null ? void 0 : x[0]);
|
|
341
|
+
if (P) return v(`${r} takes no order of its own`, P.span);
|
|
342
342
|
if (t.separator)
|
|
343
343
|
return v("SEPARATOR goes only in GROUP_CONCAT", t.separator.span);
|
|
344
344
|
if (u === "COUNT" && t.args.length === 1 && t.args[0].kind === "star" && !t.args[0].table)
|
|
345
345
|
return I(r, [], h);
|
|
346
|
-
if (!i(r) && !o.allowUnknownFunctions) return
|
|
347
|
-
const
|
|
348
|
-
return I(r,
|
|
349
|
-
},
|
|
346
|
+
if (!i(r) && !o.allowUnknownFunctions) return b(r, h);
|
|
347
|
+
const $ = t.args.map((z) => z.kind === "star" ? v("`*` only goes in COUNT(*)", z.span) : p(z));
|
|
348
|
+
return I(r, $, h, { distinct: t.distinct });
|
|
349
|
+
}, V = (t, r, u) => ({
|
|
350
350
|
type: "binary",
|
|
351
351
|
operator: t,
|
|
352
352
|
left: r,
|
|
353
353
|
right: u
|
|
354
354
|
}), ot = (t) => {
|
|
355
|
-
const r = [], u = (h,
|
|
356
|
-
!
|
|
355
|
+
const r = [], u = (h, U, P) => {
|
|
356
|
+
!P && h.kind === "binary" && h.op === t.op && h.span.start === U.span.start ? (u(h.left, h, !1), u(h.right, h, !0)) : r.push(p(h));
|
|
357
357
|
};
|
|
358
358
|
return u(t.left, t, !1), u(t.right, t, !0), { type: "logical", operator: t.op, operands: r };
|
|
359
|
-
},
|
|
360
|
-
const r =
|
|
361
|
-
return {
|
|
362
|
-
type: "logical",
|
|
363
|
-
operator: "OR",
|
|
364
|
-
operands: [z("=", r, u), {
|
|
365
|
-
type: "logical",
|
|
366
|
-
operator: "AND",
|
|
367
|
-
operands: [
|
|
368
|
-
{ type: "binary", operator: "IS NULL", left: r },
|
|
369
|
-
{ type: "binary", operator: "IS NULL", left: u }
|
|
370
|
-
]
|
|
371
|
-
}]
|
|
372
|
-
};
|
|
373
|
-
}, pt = (t) => {
|
|
374
|
-
const r = () => [c(t.left), c(t.right)];
|
|
359
|
+
}, ut = (t) => {
|
|
360
|
+
const r = () => [p(t.left), p(t.right)];
|
|
375
361
|
if (t.escape) return v("LIKE … ESCAPE can't be imported", t.escape.span);
|
|
376
362
|
switch (t.op) {
|
|
377
363
|
case "AND":
|
|
@@ -382,32 +368,32 @@ function oe(o, T, U) {
|
|
|
382
368
|
case "<=":
|
|
383
369
|
case ">":
|
|
384
370
|
case ">=":
|
|
385
|
-
return
|
|
371
|
+
return V(t.op, ...r());
|
|
386
372
|
case "<>":
|
|
387
373
|
case "!=":
|
|
388
|
-
return
|
|
374
|
+
return V("!=", ...r());
|
|
389
375
|
case "LIKE":
|
|
390
|
-
return
|
|
376
|
+
return V("LIKE", ...r());
|
|
391
377
|
case "NOT LIKE":
|
|
392
|
-
return { type: "not", operand:
|
|
378
|
+
return { type: "not", operand: V("LIKE", ...r()) };
|
|
393
379
|
case "REGEXP":
|
|
394
380
|
case "RLIKE":
|
|
395
381
|
case "~":
|
|
396
|
-
return
|
|
382
|
+
return V("REGEXP", ...r());
|
|
397
383
|
case "NOT REGEXP":
|
|
398
384
|
case "NOT RLIKE":
|
|
399
385
|
case "!~":
|
|
400
|
-
return { type: "not", operand:
|
|
386
|
+
return { type: "not", operand: V("REGEXP", ...r()) };
|
|
401
387
|
case "<=>":
|
|
402
388
|
case "IS NOT DISTINCT FROM":
|
|
403
|
-
return
|
|
389
|
+
return V("IS NOT DISTINCT FROM", ...r());
|
|
404
390
|
case "IS DISTINCT FROM":
|
|
405
|
-
return { type: "not", operand:
|
|
391
|
+
return { type: "not", operand: V("IS NOT DISTINCT FROM", ...r()) };
|
|
406
392
|
case "+":
|
|
407
393
|
case "-": {
|
|
408
|
-
const u =
|
|
394
|
+
const u = C(t);
|
|
409
395
|
if (u) return u;
|
|
410
|
-
const h = [t.left, t.right].find((
|
|
396
|
+
const h = [t.left, t.right].find((U) => U.kind === "interval");
|
|
411
397
|
return h ? v(`${a(h.span)} can't be imported`, h.span) : v(`Arithmetic (${t.op}) can't be imported`, t.span);
|
|
412
398
|
}
|
|
413
399
|
case "*":
|
|
@@ -420,10 +406,10 @@ function oe(o, T, U) {
|
|
|
420
406
|
return v(`${t.op} can't be imported`, t.span);
|
|
421
407
|
}
|
|
422
408
|
};
|
|
423
|
-
function
|
|
409
|
+
function ct(t) {
|
|
424
410
|
switch (t.kind) {
|
|
425
411
|
case "name":
|
|
426
|
-
return
|
|
412
|
+
return J(t);
|
|
427
413
|
case "star":
|
|
428
414
|
return v("`*` only goes in SELECT or COUNT(*)", t.span);
|
|
429
415
|
case "string":
|
|
@@ -442,31 +428,31 @@ function oe(o, T, U) {
|
|
|
442
428
|
case "interval":
|
|
443
429
|
return v("An INTERVAL goes only in date arithmetic", t.span);
|
|
444
430
|
case "param":
|
|
445
|
-
return t.style === ":" || t.style === "@" ? { type: "param", name: t.name } :
|
|
431
|
+
return t.style === ":" || t.style === "@" ? { type: "param", name: t.name } : k("positionalParam", `${a(t.span)} has no name: write :name`, t.span);
|
|
446
432
|
case "unary":
|
|
447
|
-
if (t.op === "NOT") return { type: "not", operand:
|
|
448
|
-
if (t.op === "+") return
|
|
433
|
+
if (t.op === "NOT") return { type: "not", operand: p(t.operand) };
|
|
434
|
+
if (t.op === "+") return p(t.operand);
|
|
449
435
|
if (t.operand.kind === "number") {
|
|
450
436
|
const r = At(t.operand.text);
|
|
451
437
|
return rt(-r);
|
|
452
438
|
}
|
|
453
439
|
return v("Arithmetic (-) can't be imported", t.span);
|
|
454
440
|
case "binary":
|
|
455
|
-
return
|
|
441
|
+
return ut(t);
|
|
456
442
|
case "isNull":
|
|
457
443
|
return {
|
|
458
444
|
type: "binary",
|
|
459
445
|
operator: t.not ? "IS NOT NULL" : "IS NULL",
|
|
460
|
-
left:
|
|
446
|
+
left: p(t.operand)
|
|
461
447
|
};
|
|
462
448
|
case "in": {
|
|
463
|
-
const r =
|
|
449
|
+
const r = p(t.operand);
|
|
464
450
|
let u;
|
|
465
451
|
if (t.query) {
|
|
466
|
-
const
|
|
467
|
-
u =
|
|
452
|
+
const U = o.inQuery(t.query, D.scope);
|
|
453
|
+
u = U ? { type: "subquery", id: nt(), query: U } : Tt();
|
|
468
454
|
} else
|
|
469
|
-
u = { type: "list", items: t.list.map(
|
|
455
|
+
u = { type: "list", items: t.list.map(p) };
|
|
470
456
|
const h = {
|
|
471
457
|
type: "binary",
|
|
472
458
|
operator: "IN",
|
|
@@ -479,8 +465,8 @@ function oe(o, T, U) {
|
|
|
479
465
|
const r = {
|
|
480
466
|
type: "binary",
|
|
481
467
|
operator: "BETWEEN",
|
|
482
|
-
left:
|
|
483
|
-
right: { type: "list", items: [
|
|
468
|
+
left: p(t.operand),
|
|
469
|
+
right: { type: "list", items: [p(t.low), p(t.high)] }
|
|
484
470
|
};
|
|
485
471
|
return t.not ? { type: "not", operand: r } : r;
|
|
486
472
|
}
|
|
@@ -488,12 +474,12 @@ function oe(o, T, U) {
|
|
|
488
474
|
return w(t);
|
|
489
475
|
case "cast": {
|
|
490
476
|
const r = t.type.toUpperCase(), u = Qt[o.dialect][r];
|
|
491
|
-
return t.operand.kind === "null" && u ? { type: "literal", value: null, as: u } : ne.test(t.type) ? I("CAST", [
|
|
477
|
+
return t.operand.kind === "null" && u ? { type: "literal", value: null, as: u } : ne.test(t.type) ? I("CAST", [p(t.operand), rt(t.type)], t.span) : v(`The type ${t.type} can't be imported`, t.span);
|
|
492
478
|
}
|
|
493
479
|
case "extract":
|
|
494
|
-
return
|
|
480
|
+
return pt(t.unit) ? I("EXTRACT", [p(t.operand), M(t.unit)], t.span) : v(`EXTRACT of ${t.unit} can't be imported`, t.span);
|
|
495
481
|
case "case": {
|
|
496
|
-
const r = t.branches.map((
|
|
482
|
+
const r = t.branches.map((U) => ({ when: p(U.when), then: p(U.then) })), u = t.subject && p(t.subject), h = t.else && p(t.else);
|
|
497
483
|
return o.dialect === "postgres" && !u && r.length === 1 && h ? {
|
|
498
484
|
type: "if",
|
|
499
485
|
condition: r[0].when,
|
|
@@ -507,7 +493,7 @@ function oe(o, T, U) {
|
|
|
507
493
|
};
|
|
508
494
|
}
|
|
509
495
|
case "exists":
|
|
510
|
-
return o.existsQuery(t.query,
|
|
496
|
+
return o.existsQuery(t.query, D.scope, t.span) ?? Tt();
|
|
511
497
|
default:
|
|
512
498
|
return v(
|
|
513
499
|
"A sub query as a value can't be imported: only after IN or EXISTS",
|
|
@@ -515,7 +501,7 @@ function oe(o, T, U) {
|
|
|
515
501
|
);
|
|
516
502
|
}
|
|
517
503
|
}
|
|
518
|
-
return
|
|
504
|
+
return ct(T);
|
|
519
505
|
}
|
|
520
506
|
function ae(o) {
|
|
521
507
|
return o.type === "literal" && o.value === 1;
|
|
@@ -601,763 +587,763 @@ const St = /* @__PURE__ */ new Set([
|
|
|
601
587
|
"MICROSECOND"
|
|
602
588
|
]), le = /* @__PURE__ */ new Set(["PRECISION", "VARYING", "WITH", "WITHOUT", "TIME", "ZONE"]);
|
|
603
589
|
function pe(o, T) {
|
|
604
|
-
const
|
|
605
|
-
let
|
|
606
|
-
const a = (e = 0) =>
|
|
590
|
+
const D = Zt(o, T);
|
|
591
|
+
let k = 0, v = 0;
|
|
592
|
+
const a = (e = 0) => D[Math.min(k + e, D.length - 1)], p = () => {
|
|
607
593
|
const e = a();
|
|
608
|
-
return e.kind !== "end" && (
|
|
609
|
-
}, i = (e) => ({ start: e, end: v }),
|
|
610
|
-
throw new ft(e, o,
|
|
611
|
-
},
|
|
612
|
-
const
|
|
613
|
-
for (;
|
|
614
|
-
return
|
|
615
|
-
}, ot = (e) => I(e, "SELECT", "WITH"),
|
|
594
|
+
return e.kind !== "end" && (k += 1), v = e.end, e;
|
|
595
|
+
}, i = (e) => ({ start: e, end: v }), b = (e) => e.kind === "word" ? e.value.toUpperCase() : "", I = (e, ...n) => n.includes(b(e)), M = (e, n) => e.kind === "op" && e.value === n, J = (e) => e.kind === "end" ? "the end" : `"${o.slice(e.start, e.end)}"`, q = (e, n = a()) => {
|
|
596
|
+
throw new ft(e, o, n.start, n.end);
|
|
597
|
+
}, C = (e) => q(`Expected ${e} but found ${J(a())}`), d = (...e) => I(a(), ...e) && p(), F = (e) => M(a(), e) && p(), H = (e) => d(e) || C(e), w = (e) => F(e) || C(`"${e}"`), V = (e) => {
|
|
598
|
+
const n = [e()];
|
|
599
|
+
for (; F(","); ) n.push(e());
|
|
600
|
+
return n;
|
|
601
|
+
}, ot = (e) => I(e, "SELECT", "WITH"), ut = (e) => ({
|
|
616
602
|
value: e.value,
|
|
617
603
|
quoted: e.kind !== "word",
|
|
618
604
|
span: { start: e.start, end: e.end }
|
|
619
|
-
}),
|
|
620
|
-
const
|
|
621
|
-
return
|
|
622
|
-
},
|
|
605
|
+
}), ct = (e) => e.kind === "quoted" || e.kind === "word" && !St.has(b(e)), t = () => ct(a()) ? ut(p()) : C("a name"), r = (e = !1) => {
|
|
606
|
+
const n = d("AS"), m = a();
|
|
607
|
+
return ct(m) || e && m.kind === "string" ? ut(p()) : n ? C("a name") : void 0;
|
|
608
|
+
}, u = () => {
|
|
623
609
|
const { start: e } = w("(");
|
|
624
|
-
for (let
|
|
625
|
-
const m =
|
|
626
|
-
m.kind === "end" &&
|
|
610
|
+
for (let n = 1; n > 0; ) {
|
|
611
|
+
const m = p();
|
|
612
|
+
m.kind === "end" && C('")"'), M(m, "(") && (n += 1), M(m, ")") && (n -= 1);
|
|
627
613
|
}
|
|
628
614
|
return i(e);
|
|
629
|
-
},
|
|
630
|
-
const { start: e } = a(),
|
|
615
|
+
}, h = () => {
|
|
616
|
+
const { start: e } = a(), n = E(), m = d("ASC", "DESC"), A = d("NULLS") && (d("FIRST", "LAST") || C("FIRST or LAST"));
|
|
631
617
|
return {
|
|
632
|
-
expr:
|
|
618
|
+
expr: n,
|
|
633
619
|
...m && { direction: m.value.toLowerCase() },
|
|
634
|
-
...
|
|
620
|
+
...A && { nulls: A.value.toLowerCase() },
|
|
635
621
|
span: i(e)
|
|
636
622
|
};
|
|
637
|
-
},
|
|
638
|
-
a().kind !== "word" &&
|
|
639
|
-
const
|
|
640
|
-
for (; a().kind === "word" && (e || le.has(
|
|
641
|
-
|
|
623
|
+
}, U = () => (H("BY"), V(h)), P = (e) => {
|
|
624
|
+
a().kind !== "word" && C("a type");
|
|
625
|
+
const n = [p().value];
|
|
626
|
+
for (; a().kind === "word" && (e || le.has(b(a()))); )
|
|
627
|
+
n.push(p().value);
|
|
642
628
|
let m = "";
|
|
643
|
-
if (
|
|
644
|
-
const
|
|
645
|
-
const
|
|
646
|
-
return
|
|
629
|
+
if (F("(")) {
|
|
630
|
+
const g = V(() => {
|
|
631
|
+
const A = a();
|
|
632
|
+
return A.kind === "number" || I(A, "MAX") ? p().value : C("a size");
|
|
647
633
|
});
|
|
648
|
-
w(")"), m = `(${
|
|
634
|
+
w(")"), m = `(${g.join(", ")})`;
|
|
649
635
|
}
|
|
650
|
-
return
|
|
651
|
-
},
|
|
636
|
+
return n.join(" ") + m;
|
|
637
|
+
}, $ = (e, n) => {
|
|
652
638
|
w("(");
|
|
653
|
-
const m = !!
|
|
654
|
-
m ||
|
|
655
|
-
let
|
|
656
|
-
M(a(), ")") || (
|
|
657
|
-
let
|
|
658
|
-
|
|
639
|
+
const m = !!d("DISTINCT");
|
|
640
|
+
m || d("ALL");
|
|
641
|
+
let g = [], A, G;
|
|
642
|
+
M(a(), ")") || (g = V(E), d("ORDER") && (A = U()), T === "mysql" && d("SEPARATOR") && (G = E())), w(")");
|
|
643
|
+
let Q;
|
|
644
|
+
d("WITHIN") && (H("GROUP"), w("("), H("ORDER"), Q = U(), w(")"));
|
|
659
645
|
let dt;
|
|
660
|
-
I(a(), "FILTER") && M(a(1), "(") && (
|
|
646
|
+
I(a(), "FILTER") && M(a(1), "(") && (p(), dt = u());
|
|
661
647
|
let Et;
|
|
662
|
-
const vt =
|
|
663
|
-
return vt && (M(a(), "(") ?
|
|
648
|
+
const vt = d("OVER");
|
|
649
|
+
return vt && (M(a(), "(") ? u() : t(), Et = i(vt.start)), {
|
|
664
650
|
kind: "call",
|
|
665
651
|
name: e,
|
|
666
|
-
args:
|
|
652
|
+
args: g,
|
|
667
653
|
distinct: m,
|
|
668
|
-
...
|
|
669
|
-
...
|
|
670
|
-
...
|
|
654
|
+
...A && { orderBy: A },
|
|
655
|
+
...G && { separator: G },
|
|
656
|
+
...Q && { withinGroup: Q },
|
|
671
657
|
...dt && { filter: dt },
|
|
672
658
|
...Et && { over: Et },
|
|
673
|
-
span: i(
|
|
659
|
+
span: i(n)
|
|
674
660
|
};
|
|
675
|
-
},
|
|
676
|
-
const { start: e } = a(),
|
|
677
|
-
for (;
|
|
678
|
-
if (
|
|
679
|
-
|
|
661
|
+
}, Z = () => {
|
|
662
|
+
const { start: e } = a(), n = [ut(p())];
|
|
663
|
+
for (; F("."); ) {
|
|
664
|
+
if (F("*")) return { kind: "star", table: n, span: i(e) };
|
|
665
|
+
n.push(t());
|
|
680
666
|
}
|
|
681
|
-
return M(a(), "(") ?
|
|
682
|
-
},
|
|
683
|
-
const
|
|
684
|
-
for (;
|
|
685
|
-
const
|
|
686
|
-
|
|
667
|
+
return M(a(), "(") ? $(n, e) : { kind: "name", parts: n, span: i(e) };
|
|
668
|
+
}, x = (e) => {
|
|
669
|
+
const n = I(a(), "WHEN") ? void 0 : E(), m = [];
|
|
670
|
+
for (; d("WHEN"); ) {
|
|
671
|
+
const A = E();
|
|
672
|
+
H("THEN"), m.push({ when: A, then: E() });
|
|
687
673
|
}
|
|
688
|
-
m.length === 0 &&
|
|
689
|
-
const
|
|
690
|
-
return
|
|
674
|
+
m.length === 0 && C("WHEN");
|
|
675
|
+
const g = d("ELSE") ? E() : void 0;
|
|
676
|
+
return H("END"), {
|
|
691
677
|
kind: "case",
|
|
692
|
-
...
|
|
678
|
+
...n && { subject: n },
|
|
693
679
|
branches: m,
|
|
694
|
-
...
|
|
680
|
+
...g && { else: g },
|
|
695
681
|
span: i(e)
|
|
696
682
|
};
|
|
697
|
-
},
|
|
698
|
-
const e = a(), { start:
|
|
699
|
-
if (e.kind === "number") return { kind: "number", text:
|
|
700
|
-
if (e.kind === "string") return { kind: "string", value:
|
|
683
|
+
}, z = () => {
|
|
684
|
+
const e = a(), { start: n } = e, m = b(e);
|
|
685
|
+
if (e.kind === "number") return { kind: "number", text: p().value, span: i(n) };
|
|
686
|
+
if (e.kind === "string") return { kind: "string", value: p().value, span: i(n) };
|
|
701
687
|
if (e.kind === "param")
|
|
702
|
-
return
|
|
688
|
+
return p(), {
|
|
703
689
|
kind: "param",
|
|
704
690
|
style: e.style,
|
|
705
691
|
name: e.value,
|
|
706
|
-
span: i(
|
|
692
|
+
span: i(n)
|
|
707
693
|
};
|
|
708
|
-
if (e.kind === "quoted") return
|
|
709
|
-
if (
|
|
710
|
-
if (
|
|
694
|
+
if (e.kind === "quoted") return Z();
|
|
695
|
+
if (F("*")) return { kind: "star", span: i(n) };
|
|
696
|
+
if (F("(")) {
|
|
711
697
|
if (ot(a())) {
|
|
712
|
-
const
|
|
713
|
-
return w(")"), { kind: "subquery", query:
|
|
698
|
+
const A = et();
|
|
699
|
+
return w(")"), { kind: "subquery", query: A, span: i(n) };
|
|
714
700
|
}
|
|
715
|
-
const
|
|
716
|
-
return M(a(), ",") &&
|
|
701
|
+
const g = E();
|
|
702
|
+
return M(a(), ",") && q("A list of values in parentheses (a row) can't be imported"), w(")"), g;
|
|
717
703
|
}
|
|
718
|
-
if (e.kind !== "word") return
|
|
704
|
+
if (e.kind !== "word") return C("a value");
|
|
719
705
|
if (m === "NULL" || m === "TRUE" || m === "FALSE")
|
|
720
|
-
return
|
|
721
|
-
if (
|
|
722
|
-
if (
|
|
706
|
+
return p(), { kind: m.toLowerCase(), span: i(n) };
|
|
707
|
+
if (d("CASE")) return x(n);
|
|
708
|
+
if (d("EXISTS")) {
|
|
723
709
|
w("(");
|
|
724
|
-
const
|
|
725
|
-
return w(")"), { kind: "exists", query:
|
|
710
|
+
const g = et();
|
|
711
|
+
return w(")"), { kind: "exists", query: g, span: i(n) };
|
|
726
712
|
}
|
|
727
713
|
if ((m === "CAST" || m === "EXTRACT") && M(a(1), "(")) {
|
|
728
|
-
if (
|
|
729
|
-
const
|
|
730
|
-
|
|
731
|
-
const
|
|
714
|
+
if (p(), p(), m === "CAST") {
|
|
715
|
+
const G = E();
|
|
716
|
+
H("AS");
|
|
717
|
+
const Q = P(!0);
|
|
732
718
|
return w(")"), {
|
|
733
719
|
kind: "cast",
|
|
734
|
-
operand:
|
|
735
|
-
type:
|
|
736
|
-
span: i(
|
|
720
|
+
operand: G,
|
|
721
|
+
type: Q,
|
|
722
|
+
span: i(n)
|
|
737
723
|
};
|
|
738
724
|
}
|
|
739
|
-
const
|
|
740
|
-
|
|
741
|
-
const
|
|
725
|
+
const g = a().kind === "word" ? b(p()) : C("a unit");
|
|
726
|
+
H("FROM");
|
|
727
|
+
const A = E();
|
|
742
728
|
return w(")"), {
|
|
743
729
|
kind: "extract",
|
|
744
|
-
unit:
|
|
745
|
-
operand:
|
|
746
|
-
span: i(
|
|
730
|
+
unit: g,
|
|
731
|
+
operand: A,
|
|
732
|
+
span: i(n)
|
|
747
733
|
};
|
|
748
734
|
}
|
|
749
735
|
if ((m === "DATE" || m === "TIME" || m === "TIMESTAMP") && a(1).kind === "string")
|
|
750
|
-
return
|
|
736
|
+
return p(), {
|
|
751
737
|
kind: "typed",
|
|
752
738
|
type: m,
|
|
753
|
-
value:
|
|
754
|
-
span: i(
|
|
739
|
+
value: p().value,
|
|
740
|
+
span: i(n)
|
|
755
741
|
};
|
|
756
|
-
if (
|
|
757
|
-
const
|
|
742
|
+
if (d("INTERVAL")) {
|
|
743
|
+
const g = L(), A = ce.has(b(a())) ? b(p()) : void 0;
|
|
758
744
|
return {
|
|
759
745
|
kind: "interval",
|
|
760
|
-
value:
|
|
761
|
-
...
|
|
762
|
-
span: i(
|
|
746
|
+
value: g,
|
|
747
|
+
...A && { unit: A },
|
|
748
|
+
span: i(n)
|
|
763
749
|
};
|
|
764
750
|
}
|
|
765
|
-
return St.has(m) && !(ie.has(m) && M(a(1), "(")) ?
|
|
766
|
-
},
|
|
751
|
+
return St.has(m) && !(ie.has(m) && M(a(1), "(")) ? C("a value") : Z();
|
|
752
|
+
}, s = () => {
|
|
767
753
|
const { start: e } = a();
|
|
768
|
-
let
|
|
769
|
-
for (;
|
|
770
|
-
|
|
754
|
+
let n = z();
|
|
755
|
+
for (; F("::"); )
|
|
756
|
+
n = {
|
|
771
757
|
kind: "cast",
|
|
772
|
-
operand:
|
|
773
|
-
type:
|
|
758
|
+
operand: n,
|
|
759
|
+
type: P(!1),
|
|
774
760
|
span: i(e)
|
|
775
761
|
};
|
|
776
|
-
return
|
|
777
|
-
},
|
|
778
|
-
const { start: e } = a(),
|
|
779
|
-
if (!
|
|
780
|
-
const m =
|
|
762
|
+
return n;
|
|
763
|
+
}, c = () => {
|
|
764
|
+
const { start: e } = a(), n = F("-") || F("+");
|
|
765
|
+
if (!n) return s();
|
|
766
|
+
const m = c();
|
|
781
767
|
return {
|
|
782
768
|
kind: "unary",
|
|
783
|
-
op:
|
|
769
|
+
op: n.value,
|
|
784
770
|
operand: m,
|
|
785
771
|
span: i(e)
|
|
786
772
|
};
|
|
787
|
-
},
|
|
773
|
+
}, f = (e, n) => () => {
|
|
788
774
|
const { start: m } = a();
|
|
789
|
-
let
|
|
790
|
-
for (let
|
|
791
|
-
|
|
792
|
-
const
|
|
793
|
-
|
|
775
|
+
let g = e();
|
|
776
|
+
for (let A = n(a()); A; A = n(a())) {
|
|
777
|
+
p();
|
|
778
|
+
const G = e();
|
|
779
|
+
g = {
|
|
794
780
|
kind: "binary",
|
|
795
|
-
op:
|
|
796
|
-
left:
|
|
797
|
-
right:
|
|
781
|
+
op: A,
|
|
782
|
+
left: g,
|
|
783
|
+
right: G,
|
|
798
784
|
span: i(m)
|
|
799
785
|
};
|
|
800
786
|
}
|
|
801
|
-
return
|
|
802
|
-
},
|
|
787
|
+
return g;
|
|
788
|
+
}, y = (...e) => (n) => n.kind === "op" && e.includes(n.value) ? n.value : void 0, S = f(c, y("*", "/", "%")), L = f(S, y("+", "-")), B = f(L, y(...T === "mysql" ? [] : ["||"])), _ = () => {
|
|
803
789
|
const { start: e } = a();
|
|
804
|
-
let
|
|
790
|
+
let n = B();
|
|
805
791
|
for (; ; ) {
|
|
806
792
|
const m = a();
|
|
807
793
|
if (m.kind === "op" && ue.has(m.value))
|
|
808
|
-
|
|
794
|
+
p(), n = {
|
|
809
795
|
kind: "binary",
|
|
810
796
|
op: m.value,
|
|
811
|
-
left:
|
|
797
|
+
left: n,
|
|
812
798
|
right: B(),
|
|
813
799
|
span: i(e)
|
|
814
800
|
};
|
|
815
|
-
else if (
|
|
816
|
-
const
|
|
817
|
-
|
|
801
|
+
else if (d("IS")) {
|
|
802
|
+
const g = !!d("NOT");
|
|
803
|
+
d("NULL") ? n = {
|
|
818
804
|
kind: "isNull",
|
|
819
|
-
operand:
|
|
820
|
-
not:
|
|
805
|
+
operand: n,
|
|
806
|
+
not: g,
|
|
821
807
|
span: i(e)
|
|
822
|
-
} :
|
|
808
|
+
} : d("DISTINCT") ? (H("FROM"), n = {
|
|
823
809
|
kind: "binary",
|
|
824
|
-
op: `IS ${
|
|
825
|
-
left:
|
|
810
|
+
op: `IS ${g ? "NOT " : ""}DISTINCT FROM`,
|
|
811
|
+
left: n,
|
|
826
812
|
right: B(),
|
|
827
813
|
span: i(e)
|
|
828
|
-
}) : I(a(), "TRUE", "FALSE") ?
|
|
814
|
+
}) : I(a(), "TRUE", "FALSE") ? n = {
|
|
829
815
|
kind: "binary",
|
|
830
|
-
op:
|
|
831
|
-
left:
|
|
832
|
-
right:
|
|
816
|
+
op: g ? "IS NOT" : "IS",
|
|
817
|
+
left: n,
|
|
818
|
+
right: z(),
|
|
833
819
|
span: i(e)
|
|
834
|
-
} :
|
|
820
|
+
} : C("NULL");
|
|
835
821
|
} else {
|
|
836
|
-
const
|
|
837
|
-
|
|
838
|
-
const
|
|
839
|
-
if (
|
|
822
|
+
const g = I(m, "NOT");
|
|
823
|
+
g && p();
|
|
824
|
+
const A = a();
|
|
825
|
+
if (d("IN")) {
|
|
840
826
|
w("(");
|
|
841
|
-
const
|
|
842
|
-
w(")"),
|
|
827
|
+
const G = ot(a()) ? { query: et() } : { list: V(E) };
|
|
828
|
+
w(")"), n = {
|
|
843
829
|
kind: "in",
|
|
844
|
-
operand:
|
|
845
|
-
not:
|
|
846
|
-
...
|
|
830
|
+
operand: n,
|
|
831
|
+
not: g,
|
|
832
|
+
...G,
|
|
847
833
|
span: i(e)
|
|
848
834
|
};
|
|
849
|
-
} else if (
|
|
850
|
-
const
|
|
851
|
-
|
|
835
|
+
} else if (d("BETWEEN")) {
|
|
836
|
+
const G = B();
|
|
837
|
+
H("AND"), n = {
|
|
852
838
|
kind: "between",
|
|
853
|
-
operand:
|
|
854
|
-
not:
|
|
855
|
-
low:
|
|
839
|
+
operand: n,
|
|
840
|
+
not: g,
|
|
841
|
+
low: G,
|
|
856
842
|
high: B(),
|
|
857
843
|
span: i(e)
|
|
858
844
|
};
|
|
859
|
-
} else if (
|
|
860
|
-
const
|
|
861
|
-
|
|
845
|
+
} else if (d("LIKE", "ILIKE", "REGEXP", "RLIKE")) {
|
|
846
|
+
const G = B(), Q = d("ESCAPE") && z();
|
|
847
|
+
n = {
|
|
862
848
|
kind: "binary",
|
|
863
|
-
op: `${
|
|
864
|
-
left:
|
|
865
|
-
right:
|
|
866
|
-
...
|
|
849
|
+
op: `${g ? "NOT " : ""}${b(A)}`,
|
|
850
|
+
left: n,
|
|
851
|
+
right: G,
|
|
852
|
+
...Q && { escape: Q },
|
|
867
853
|
span: i(e)
|
|
868
854
|
};
|
|
869
855
|
} else
|
|
870
|
-
return
|
|
856
|
+
return g ? C("IN, BETWEEN or LIKE") : n;
|
|
871
857
|
}
|
|
872
858
|
}
|
|
873
|
-
},
|
|
859
|
+
}, X = () => {
|
|
874
860
|
const { start: e } = a();
|
|
875
|
-
return
|
|
861
|
+
return d("NOT") ? {
|
|
876
862
|
kind: "unary",
|
|
877
863
|
op: "NOT",
|
|
878
|
-
operand:
|
|
864
|
+
operand: X(),
|
|
879
865
|
span: i(e)
|
|
880
|
-
} :
|
|
881
|
-
}, j =
|
|
866
|
+
} : _();
|
|
867
|
+
}, j = f(X, (e) => I(e, "AND") || T === "mysql" && M(e, "&&") ? "AND" : void 0), E = f(j, (e) => I(e, "OR") || T === "mysql" && M(e, "||") ? "OR" : void 0), l = () => {
|
|
882
868
|
const { start: e } = a();
|
|
883
|
-
if (
|
|
884
|
-
!ot(a()) && !M(a(), "(") &&
|
|
885
|
-
const
|
|
869
|
+
if (F("(")) {
|
|
870
|
+
!ot(a()) && !M(a(), "(") && C("a sub query (SELECT …)");
|
|
871
|
+
const g = et();
|
|
886
872
|
w(")");
|
|
887
|
-
const
|
|
873
|
+
const A = r();
|
|
888
874
|
return {
|
|
889
875
|
kind: "derived",
|
|
890
|
-
query:
|
|
891
|
-
...
|
|
876
|
+
query: g,
|
|
877
|
+
...A && { alias: A },
|
|
892
878
|
span: i(e)
|
|
893
879
|
};
|
|
894
880
|
}
|
|
895
|
-
const
|
|
896
|
-
for (;
|
|
897
|
-
const m =
|
|
881
|
+
const n = [t()];
|
|
882
|
+
for (; F("."); ) n.push(t());
|
|
883
|
+
const m = r();
|
|
898
884
|
return {
|
|
899
885
|
kind: "table",
|
|
900
|
-
name:
|
|
886
|
+
name: n,
|
|
901
887
|
...m && { alias: m },
|
|
902
888
|
span: i(e)
|
|
903
889
|
};
|
|
904
|
-
},
|
|
905
|
-
if (
|
|
906
|
-
const e =
|
|
890
|
+
}, N = () => {
|
|
891
|
+
if (d("JOIN")) return "inner";
|
|
892
|
+
const e = d("INNER", "LEFT", "RIGHT", "FULL", "CROSS");
|
|
907
893
|
if (!e) {
|
|
908
|
-
I(a(), "NATURAL") &&
|
|
894
|
+
I(a(), "NATURAL") && q("NATURAL JOIN can't be imported: name its ON");
|
|
909
895
|
return;
|
|
910
896
|
}
|
|
911
|
-
I(e, "INNER", "CROSS") ||
|
|
912
|
-
const
|
|
913
|
-
return
|
|
897
|
+
I(e, "INNER", "CROSS") || d("OUTER"), H("JOIN");
|
|
898
|
+
const n = e.value.toLowerCase();
|
|
899
|
+
return n === "inner" || n === "cross" || n === "left" || n === "right" ? n : "full";
|
|
914
900
|
}, K = () => {
|
|
915
|
-
const e = l(),
|
|
901
|
+
const e = l(), n = [];
|
|
916
902
|
for (; ; ) {
|
|
917
|
-
const { start: m } = a(),
|
|
918
|
-
if (!
|
|
919
|
-
const
|
|
920
|
-
let
|
|
921
|
-
|
|
922
|
-
type:
|
|
923
|
-
table:
|
|
924
|
-
...
|
|
925
|
-
...
|
|
903
|
+
const { start: m } = a(), g = N();
|
|
904
|
+
if (!g) return { table: e, joins: n };
|
|
905
|
+
const A = l();
|
|
906
|
+
let G, Q;
|
|
907
|
+
g !== "cross" && (d("USING") ? (w("("), Q = V(t), w(")")) : (H("ON"), G = E())), n.push({
|
|
908
|
+
type: g,
|
|
909
|
+
table: A,
|
|
910
|
+
...G && { on: G },
|
|
911
|
+
...Q && { using: Q },
|
|
926
912
|
span: i(m)
|
|
927
913
|
});
|
|
928
914
|
}
|
|
929
915
|
}, W = () => {
|
|
930
|
-
const { start: e } = a(),
|
|
931
|
-
return { expr:
|
|
916
|
+
const { start: e } = a(), n = E(), m = r(T !== "postgres");
|
|
917
|
+
return { expr: n, ...m && { alias: m }, span: i(e) };
|
|
932
918
|
}, O = () => {
|
|
933
919
|
const { start: e } = a();
|
|
934
|
-
|
|
935
|
-
const
|
|
936
|
-
|
|
920
|
+
H("SELECT");
|
|
921
|
+
const n = !!d("DISTINCT");
|
|
922
|
+
n || d("ALL");
|
|
937
923
|
let m;
|
|
938
|
-
T === "mssql" &&
|
|
939
|
-
const
|
|
940
|
-
let
|
|
941
|
-
|
|
942
|
-
const dt =
|
|
924
|
+
T === "mssql" && d("TOP") && (m = M(a(), "(") ? z() : c(), I(a(), "PERCENT") && q("TOP … PERCENT can't be imported"), I(a(), "WITH") && q("TOP … WITH TIES can't be imported"));
|
|
925
|
+
const g = V(W), A = d("FROM") ? V(K) : [], G = d("WHERE") ? E() : void 0;
|
|
926
|
+
let Q = [];
|
|
927
|
+
d("GROUP") && (H("BY"), Q = V(E));
|
|
928
|
+
const dt = d("HAVING") ? E() : void 0;
|
|
943
929
|
return {
|
|
944
930
|
kind: "select",
|
|
945
|
-
distinct:
|
|
931
|
+
distinct: n,
|
|
946
932
|
...m && { top: m },
|
|
947
|
-
columns:
|
|
948
|
-
from:
|
|
949
|
-
...
|
|
950
|
-
groupBy:
|
|
933
|
+
columns: g,
|
|
934
|
+
from: A,
|
|
935
|
+
...G && { where: G },
|
|
936
|
+
groupBy: Q,
|
|
951
937
|
...dt && { having: dt },
|
|
952
938
|
span: i(e)
|
|
953
939
|
};
|
|
954
940
|
}, R = () => {
|
|
955
941
|
const { start: e } = a();
|
|
956
|
-
if (
|
|
957
|
-
const
|
|
958
|
-
return w(")"), { kind: "nested", query:
|
|
959
|
-
}, Y = (e, ...
|
|
942
|
+
if (!F("(")) return O();
|
|
943
|
+
const n = et();
|
|
944
|
+
return w(")"), { kind: "nested", query: n, span: i(e) };
|
|
945
|
+
}, Y = (e, ...n) => () => {
|
|
960
946
|
const { start: m } = a();
|
|
961
|
-
let
|
|
962
|
-
for (let
|
|
963
|
-
const
|
|
964
|
-
|
|
947
|
+
let g = e();
|
|
948
|
+
for (let A = d(...n); A; A = d(...n)) {
|
|
949
|
+
const G = !!d("ALL");
|
|
950
|
+
G || d("DISTINCT"), g = {
|
|
965
951
|
kind: "setOp",
|
|
966
|
-
op:
|
|
967
|
-
all:
|
|
968
|
-
left:
|
|
952
|
+
op: b(A),
|
|
953
|
+
all: G,
|
|
954
|
+
left: g,
|
|
969
955
|
right: e(),
|
|
970
956
|
span: i(m)
|
|
971
957
|
};
|
|
972
958
|
}
|
|
973
|
-
return
|
|
974
|
-
},
|
|
975
|
-
const { start: e } = a(),
|
|
959
|
+
return g;
|
|
960
|
+
}, tt = Y(Y(R, "INTERSECT"), "UNION", "EXCEPT"), at = () => {
|
|
961
|
+
const { start: e } = a(), n = t();
|
|
976
962
|
let m;
|
|
977
|
-
|
|
978
|
-
const
|
|
979
|
-
!
|
|
980
|
-
const
|
|
963
|
+
F("(") && (m = V(t), w(")")), H("AS");
|
|
964
|
+
const g = a().start, A = d("NOT");
|
|
965
|
+
!d("MATERIALIZED") && A && C("MATERIALIZED");
|
|
966
|
+
const G = g === a().start ? void 0 : i(g);
|
|
981
967
|
w("(");
|
|
982
|
-
const
|
|
968
|
+
const Q = et();
|
|
983
969
|
return w(")"), {
|
|
984
|
-
name:
|
|
970
|
+
name: n,
|
|
985
971
|
...m && { columns: m },
|
|
986
|
-
...
|
|
987
|
-
query:
|
|
972
|
+
...G && { hint: G },
|
|
973
|
+
query: Q,
|
|
988
974
|
span: i(e)
|
|
989
975
|
};
|
|
990
|
-
},
|
|
991
|
-
let e,
|
|
992
|
-
return T !== "mssql" &&
|
|
976
|
+
}, lt = () => {
|
|
977
|
+
let e, n;
|
|
978
|
+
return T !== "mssql" && d("LIMIT") && (T === "postgres" && d("ALL") || (e = E(), T === "mysql" && F(",") && (n = e, e = E()))), d("OFFSET") && (n = E(), !d("ROWS", "ROW") && T === "mssql" && C("ROWS")), T !== "mysql" && d("FETCH") && (d("FIRST", "NEXT") || C("NEXT"), e = E(), d("ROWS", "ROW") || C("ROWS"), H("ONLY")), { ...e && { limit: e }, ...n && { offset: n } };
|
|
993
979
|
};
|
|
994
980
|
function et() {
|
|
995
981
|
const { start: e } = a();
|
|
996
|
-
let
|
|
997
|
-
|
|
998
|
-
const
|
|
982
|
+
let n, m = !1;
|
|
983
|
+
d("WITH") && (m = !!d("RECURSIVE"), n = V(at));
|
|
984
|
+
const g = tt(), A = d("ORDER") ? U() : [];
|
|
999
985
|
return {
|
|
1000
|
-
...
|
|
986
|
+
...n && { with: n },
|
|
1001
987
|
...m && { recursive: m },
|
|
1002
|
-
body:
|
|
1003
|
-
orderBy:
|
|
1004
|
-
...
|
|
988
|
+
body: g,
|
|
989
|
+
orderBy: A,
|
|
990
|
+
...lt(),
|
|
1005
991
|
span: i(e)
|
|
1006
992
|
};
|
|
1007
993
|
}
|
|
1008
994
|
const mt = et();
|
|
1009
|
-
return
|
|
995
|
+
return F(";"), a().kind !== "end" && q(ot(a()) ? "Only one query can be imported" : `Unexpected ${J(a())} after the query`), mt;
|
|
1010
996
|
}
|
|
1011
997
|
const de = "18446744073709551615", yt = (o, T) => wt(o.expression, T) ? { ...o.expression, fromSelect: o.id } : o.expression;
|
|
1012
|
-
function Ot(o, T,
|
|
1013
|
-
const
|
|
998
|
+
function Ot(o, T, D) {
|
|
999
|
+
const k = T.filter((a) => wt(a.expression, D)), v = (a) => {
|
|
1014
1000
|
if (a.fromSelect !== void 0) return a;
|
|
1015
|
-
const
|
|
1016
|
-
return
|
|
1001
|
+
const p = k.find((i) => Rt(i.expression, a));
|
|
1002
|
+
return p ? { ...a, fromSelect: p.id } : Bt(a, v);
|
|
1017
1003
|
};
|
|
1018
1004
|
return v(o);
|
|
1019
1005
|
}
|
|
1020
1006
|
function Te(o, T) {
|
|
1021
|
-
const { dialect:
|
|
1007
|
+
const { dialect: D, schema: k } = T, v = T.functions ?? Ut;
|
|
1022
1008
|
let a;
|
|
1023
1009
|
try {
|
|
1024
|
-
a = pe(o,
|
|
1025
|
-
} catch (
|
|
1026
|
-
if (!(
|
|
1010
|
+
a = pe(o, D);
|
|
1011
|
+
} catch (s) {
|
|
1012
|
+
if (!(s instanceof ft)) throw s;
|
|
1027
1013
|
return {
|
|
1028
1014
|
problems: [{
|
|
1029
1015
|
severity: "error",
|
|
1030
1016
|
kind: "syntax",
|
|
1031
|
-
message:
|
|
1032
|
-
at:
|
|
1017
|
+
message: s.message,
|
|
1018
|
+
at: s.at
|
|
1033
1019
|
}]
|
|
1034
1020
|
};
|
|
1035
1021
|
}
|
|
1036
|
-
const
|
|
1022
|
+
const p = T.mode === "lenient", i = {
|
|
1037
1023
|
sql: o,
|
|
1038
|
-
lenient:
|
|
1039
|
-
dialect:
|
|
1040
|
-
schema:
|
|
1024
|
+
lenient: p,
|
|
1025
|
+
dialect: D,
|
|
1026
|
+
schema: k,
|
|
1041
1027
|
functions: v,
|
|
1042
1028
|
allowUnknownFunctions: T.allowUnknownFunctions ?? !1,
|
|
1043
1029
|
problems: [],
|
|
1044
1030
|
outerFields: /* @__PURE__ */ new WeakSet(),
|
|
1045
|
-
existsQuery: (
|
|
1046
|
-
inQuery: (
|
|
1047
|
-
const
|
|
1048
|
-
if (!
|
|
1049
|
-
|
|
1031
|
+
existsQuery: (s, c, f) => U(s, c, f),
|
|
1032
|
+
inQuery: (s, c) => {
|
|
1033
|
+
const f = u(s, c, "IN");
|
|
1034
|
+
if (!f || !("union" in f)) return f;
|
|
1035
|
+
b("A UNION after IN can't be imported", s.span);
|
|
1050
1036
|
}
|
|
1051
|
-
},
|
|
1052
|
-
const
|
|
1053
|
-
return { value:
|
|
1054
|
-
},
|
|
1055
|
-
Object.assign(
|
|
1056
|
-
}),
|
|
1057
|
-
(
|
|
1037
|
+
}, b = (s, c, f) => it(i, "unsupported", s, c, f), I = (s, c) => oe(i, s, c), M = (s) => {
|
|
1038
|
+
const c = i.problems.length, f = s(), y = i.problems.slice(c).filter((S) => S.severity === "error");
|
|
1039
|
+
return { value: f, failed: y };
|
|
1040
|
+
}, J = (s, c) => s.forEach((f) => {
|
|
1041
|
+
Object.assign(f, { severity: "warning", message: `${f.message} (${c} is left out)` });
|
|
1042
|
+
}), q = /* @__PURE__ */ new Map(), C = [], d = /* @__PURE__ */ new Map(), F = [], H = /* @__PURE__ */ new Set(), w = (s) => F.reduceRight(
|
|
1043
|
+
(c, f) => c ?? f.get(s.toLowerCase()),
|
|
1058
1044
|
void 0
|
|
1059
|
-
),
|
|
1060
|
-
const
|
|
1061
|
-
|
|
1062
|
-
const
|
|
1063
|
-
if (
|
|
1064
|
-
const
|
|
1065
|
-
let
|
|
1066
|
-
if (
|
|
1067
|
-
if (
|
|
1068
|
-
const E = `${
|
|
1069
|
-
return
|
|
1045
|
+
), V = (s) => {
|
|
1046
|
+
const c = s.name.value;
|
|
1047
|
+
s.hint && it(i, "unsupported", "MATERIALIZED only changes how the database plans it, not its rows (left out)", s.hint, "warning"), H.add(c.toLowerCase());
|
|
1048
|
+
const f = u(s.query, { tables: [] }, "WITH");
|
|
1049
|
+
if (H.delete(c.toLowerCase()), !f) return !1;
|
|
1050
|
+
const y = bt(f);
|
|
1051
|
+
let S = f;
|
|
1052
|
+
if (s.columns) {
|
|
1053
|
+
if (s.columns.length !== y.select.length) {
|
|
1054
|
+
const E = `${c} names ${s.columns.length} column(s), its query gives ${y.select.length}`;
|
|
1055
|
+
return it(i, "unsupported", E, s.span), !1;
|
|
1070
1056
|
}
|
|
1071
|
-
const j =
|
|
1072
|
-
|
|
1057
|
+
const j = y.select.map((E, l) => ({ ...E, alias: s.columns[l].value }));
|
|
1058
|
+
S = "union" in f ? { ...f, union: [{ ...y, select: j }, ...f.union.slice(1)] } : { ...y, select: j };
|
|
1073
1059
|
}
|
|
1074
|
-
const
|
|
1075
|
-
let B =
|
|
1076
|
-
for (let j = 2;
|
|
1077
|
-
const
|
|
1078
|
-
|
|
1079
|
-
const
|
|
1080
|
-
return
|
|
1081
|
-
}, ot = (
|
|
1082
|
-
if (!
|
|
1083
|
-
if (
|
|
1084
|
-
|
|
1060
|
+
const L = new Set(C.map((j) => j.name));
|
|
1061
|
+
let B = c;
|
|
1062
|
+
for (let j = 2; L.has(B); j += 1) B = `${c}_${j}`;
|
|
1063
|
+
const _ = nt();
|
|
1064
|
+
C.push({ id: _, name: B, query: S });
|
|
1065
|
+
const X = t(S);
|
|
1066
|
+
return d.set(_, X), F[F.length - 1].set(c.toLowerCase(), { id: _, columns: X }), !0;
|
|
1067
|
+
}, ot = (s, c) => {
|
|
1068
|
+
if (!s.with) return c();
|
|
1069
|
+
if (s.recursive) {
|
|
1070
|
+
b("WITH RECURSIVE can't be imported", s.with[0].span);
|
|
1085
1071
|
return;
|
|
1086
1072
|
}
|
|
1087
|
-
|
|
1073
|
+
F.push(/* @__PURE__ */ new Map());
|
|
1088
1074
|
try {
|
|
1089
|
-
return
|
|
1075
|
+
return s.with.every(V) ? c() : void 0;
|
|
1090
1076
|
} finally {
|
|
1091
|
-
|
|
1077
|
+
F.pop();
|
|
1092
1078
|
}
|
|
1093
|
-
},
|
|
1094
|
-
const
|
|
1095
|
-
...
|
|
1096
|
-
...
|
|
1097
|
-
groupBy:
|
|
1079
|
+
}, ut = (s) => {
|
|
1080
|
+
const c = xt(s, v) ? s : {
|
|
1081
|
+
...s,
|
|
1082
|
+
...s.groupBy && {
|
|
1083
|
+
groupBy: s.groupBy.map(({ fromSelect: S, ...L }) => L)
|
|
1098
1084
|
}
|
|
1099
|
-
},
|
|
1100
|
-
return (
|
|
1101
|
-
const
|
|
1102
|
-
|
|
1103
|
-
}),
|
|
1104
|
-
},
|
|
1105
|
-
const
|
|
1085
|
+
}, f = Ft(c, v), y = new Set((c.groupBy ?? []).map((S) => S.id));
|
|
1086
|
+
return (f.groupBy ?? []).filter((S) => !y.has(S.id)).forEach((S) => {
|
|
1087
|
+
const L = q.get(S.fromSelect), B = `${o.slice(L.start, L.end)} isn't in GROUP BY: it's added, as a column next to an aggregate is one value per group`;
|
|
1088
|
+
it(i, "groupedBy", B, L, "warning");
|
|
1089
|
+
}), f;
|
|
1090
|
+
}, ct = (s, c) => {
|
|
1091
|
+
const f = c.tables, y = (E) => {
|
|
1106
1092
|
var O;
|
|
1107
1093
|
const l = (O = E.alias) == null ? void 0 : O.value;
|
|
1108
|
-
let
|
|
1094
|
+
let N, K;
|
|
1109
1095
|
if (E.kind === "derived") {
|
|
1110
1096
|
if (!l) {
|
|
1111
|
-
|
|
1097
|
+
b("A sub query in FROM needs a name: (SELECT …) AS name", E.span);
|
|
1112
1098
|
return;
|
|
1113
1099
|
}
|
|
1114
|
-
const R =
|
|
1100
|
+
const R = u(E.query, c, "FROM");
|
|
1115
1101
|
if (!R) return;
|
|
1116
|
-
|
|
1102
|
+
N = { id: nt(), alias: l, query: R }, K = t(R);
|
|
1117
1103
|
} else if (E.name.length === 1 && w(E.name[0].value)) {
|
|
1118
1104
|
const R = w(E.name[0].value);
|
|
1119
|
-
|
|
1120
|
-
} else if (E.name.length === 1 &&
|
|
1121
|
-
|
|
1105
|
+
N = { shared: R.id, alias: l ?? E.name[0].value }, { columns: K } = R;
|
|
1106
|
+
} else if (E.name.length === 1 && H.has(E.name[0].value.toLowerCase())) {
|
|
1107
|
+
b("A WITH query naming itself (recursive) can't be imported", E.span);
|
|
1122
1108
|
return;
|
|
1123
1109
|
} else {
|
|
1124
|
-
const R = E.name[E.name.length - 1], Y = st(
|
|
1110
|
+
const R = E.name[E.name.length - 1], Y = st(k.tables.map((tt) => tt.name), R.value);
|
|
1125
1111
|
if (Y === void 0) {
|
|
1126
|
-
|
|
1112
|
+
it(i, "unknownTable", `The schema has no table "${R.value}"`, R.span);
|
|
1127
1113
|
return;
|
|
1128
1114
|
}
|
|
1129
|
-
|
|
1115
|
+
N = { table: Y, ...l && { alias: l } }, K = Nt(k, Y).columns.map((tt) => tt.name);
|
|
1130
1116
|
}
|
|
1131
|
-
const W = $t(
|
|
1132
|
-
if (
|
|
1117
|
+
const W = $t(N);
|
|
1118
|
+
if (f.some((R) => R.ref.toLowerCase() === W.toLowerCase())) {
|
|
1133
1119
|
const R = `Two tables here are named "${W}": give one an alias`;
|
|
1134
|
-
|
|
1120
|
+
it(i, "duplicateTable", R, E.span);
|
|
1135
1121
|
return;
|
|
1136
1122
|
}
|
|
1137
|
-
return
|
|
1123
|
+
return f.push({
|
|
1138
1124
|
ref: W,
|
|
1139
1125
|
columns: K,
|
|
1140
|
-
...
|
|
1141
|
-
}),
|
|
1142
|
-
}, [
|
|
1143
|
-
let
|
|
1126
|
+
...N.table !== void 0 && { table: N.table }
|
|
1127
|
+
}), N;
|
|
1128
|
+
}, [S, ...L] = s, B = y(S.table), _ = [];
|
|
1129
|
+
let X = B !== void 0;
|
|
1144
1130
|
return [
|
|
1145
|
-
...
|
|
1146
|
-
...
|
|
1131
|
+
...S.joins,
|
|
1132
|
+
...L.flatMap((E) => [
|
|
1147
1133
|
{ type: "cross", table: E.table, span: E.table.span },
|
|
1148
1134
|
...E.joins
|
|
1149
1135
|
])
|
|
1150
1136
|
].forEach((E) => {
|
|
1151
1137
|
if (E.using) {
|
|
1152
|
-
|
|
1138
|
+
b("JOIN … USING can't be imported: write ON", E.span), X = !1;
|
|
1153
1139
|
return;
|
|
1154
1140
|
}
|
|
1155
|
-
const l =
|
|
1141
|
+
const l = y(E.table);
|
|
1156
1142
|
if (!l) {
|
|
1157
|
-
|
|
1143
|
+
X = !1;
|
|
1158
1144
|
return;
|
|
1159
1145
|
}
|
|
1160
|
-
const
|
|
1161
|
-
l.query ?
|
|
1146
|
+
const N = E.on && I(E.on, { scope: c }), K = { joinType: E.type, ...N && { on: N } };
|
|
1147
|
+
l.query ? _.push({
|
|
1162
1148
|
id: l.id,
|
|
1163
1149
|
alias: l.alias,
|
|
1164
1150
|
query: l.query,
|
|
1165
1151
|
...K
|
|
1166
|
-
}) : l.shared !== void 0 ?
|
|
1152
|
+
}) : l.shared !== void 0 ? _.push({
|
|
1167
1153
|
id: nt(),
|
|
1168
1154
|
shared: l.shared,
|
|
1169
1155
|
alias: l.alias,
|
|
1170
1156
|
...K
|
|
1171
|
-
}) :
|
|
1157
|
+
}) : _.push({
|
|
1172
1158
|
id: nt(),
|
|
1173
1159
|
table: l.table,
|
|
1174
1160
|
...l.alias && { alias: l.alias },
|
|
1175
1161
|
...K
|
|
1176
1162
|
});
|
|
1177
|
-
}),
|
|
1163
|
+
}), X ? { from: B, joins: _ } : void 0;
|
|
1178
1164
|
};
|
|
1179
|
-
function
|
|
1180
|
-
const
|
|
1181
|
-
return
|
|
1165
|
+
function t(s) {
|
|
1166
|
+
const c = bt(s);
|
|
1167
|
+
return c.select.length > 0 ? c.select.flatMap((f) => Mt(f) ?? []) : qt(c).flatMap((f) => f.query ? t(f.query) : f.shared !== void 0 ? d.get(f.shared) : Nt(k, f.table).columns.map((y) => y.name));
|
|
1182
1168
|
}
|
|
1183
|
-
function
|
|
1184
|
-
if (
|
|
1185
|
-
|
|
1169
|
+
function r(s, c, f) {
|
|
1170
|
+
if (s.top && b(`TOP goes only in the query itself, not in ${f}`, s.top.span, "leftOut"), s.from.length === 0) {
|
|
1171
|
+
b("A query without FROM can't be imported", s.span);
|
|
1186
1172
|
return;
|
|
1187
1173
|
}
|
|
1188
|
-
const
|
|
1189
|
-
if (!
|
|
1190
|
-
const
|
|
1174
|
+
const y = ct(s.from, c);
|
|
1175
|
+
if (!y) return;
|
|
1176
|
+
const S = { scope: c }, [L] = s.columns, B = s.columns.length === 1 && L.expr.kind === "star" && !L.expr.table, _ = (O) => {
|
|
1191
1177
|
var R;
|
|
1192
1178
|
if (O.expr.kind === "star") {
|
|
1193
|
-
const Y = O.expr,
|
|
1194
|
-
return
|
|
1179
|
+
const Y = O.expr, tt = (R = Y.table) == null ? void 0 : R[Y.table.length - 1], at = tt ? c.tables.filter((lt) => st([lt.ref], tt.value)) : c.tables;
|
|
1180
|
+
return tt && at.length === 0 && it(i, "unknownTable", `No table here is named "${tt.value}"`, tt.span), at.flatMap((lt) => lt.columns.map((et) => ({
|
|
1195
1181
|
id: nt(),
|
|
1196
|
-
expression: { type: "field", table:
|
|
1182
|
+
expression: { type: "field", table: lt.ref, column: et }
|
|
1197
1183
|
})));
|
|
1198
1184
|
}
|
|
1199
1185
|
return [{
|
|
1200
1186
|
id: nt(),
|
|
1201
|
-
expression: I(O.expr,
|
|
1187
|
+
expression: I(O.expr, S),
|
|
1202
1188
|
...O.alias && { alias: O.alias.value }
|
|
1203
1189
|
}];
|
|
1204
|
-
},
|
|
1205
|
-
const { value: R, failed: Y } = M(() =>
|
|
1206
|
-
return
|
|
1190
|
+
}, X = B ? [] : s.columns.flatMap((O) => {
|
|
1191
|
+
const { value: R, failed: Y } = M(() => _(O));
|
|
1192
|
+
return p && Y.length > 0 ? (J(Y, "its SELECT column"), []) : (R.forEach((tt) => q.set(tt.id, O.span)), R);
|
|
1207
1193
|
}), j = (O) => {
|
|
1208
|
-
const R = st(
|
|
1209
|
-
return R === void 0 ? void 0 :
|
|
1210
|
-
}, E = (O) => O.kind === "number" && /^\d+$/.test(O.text) ?
|
|
1194
|
+
const R = st(X.flatMap((Y) => Y.alias ?? []), O.value);
|
|
1195
|
+
return R === void 0 ? void 0 : X.find((Y) => Y.alias === R);
|
|
1196
|
+
}, E = (O) => O.kind === "number" && /^\d+$/.test(O.text) ? X[Number(O.text) - 1] : void 0, l = s.where && I(s.where, S), N = s.groupBy.map((O) => {
|
|
1211
1197
|
const R = E(O), Y = (R == null ? void 0 : R.expression) ?? I(O, {
|
|
1212
|
-
scope:
|
|
1213
|
-
alias: (
|
|
1198
|
+
scope: c,
|
|
1199
|
+
alias: (at) => {
|
|
1214
1200
|
var et;
|
|
1215
|
-
return
|
|
1201
|
+
return c.tables.some((mt) => st(mt.columns, at.value)) || (et = j(at)) == null ? void 0 : et.expression;
|
|
1216
1202
|
}
|
|
1217
|
-
}),
|
|
1218
|
-
return { id: nt(), expression: Y, ...
|
|
1219
|
-
}), K =
|
|
1220
|
-
scope:
|
|
1221
|
-
...
|
|
1203
|
+
}), tt = X.find((at) => Rt(at.expression, Y));
|
|
1204
|
+
return { id: nt(), expression: Y, ...tt && { fromSelect: tt.id } };
|
|
1205
|
+
}), K = N.flatMap(({ expression: O }) => O.type === "field" && O.column ? [O.column.toLowerCase()] : []), W = s.having && Ot(I(s.having, {
|
|
1206
|
+
scope: c,
|
|
1207
|
+
...D === "mysql" && {
|
|
1222
1208
|
alias: (O) => {
|
|
1223
1209
|
if (K.includes(O.value.toLowerCase())) return;
|
|
1224
1210
|
const R = j(O);
|
|
1225
1211
|
return R && yt(R, v);
|
|
1226
1212
|
}
|
|
1227
1213
|
}
|
|
1228
|
-
}),
|
|
1214
|
+
}), X, v);
|
|
1229
1215
|
return {
|
|
1230
|
-
from:
|
|
1231
|
-
joins:
|
|
1216
|
+
from: y.from,
|
|
1217
|
+
joins: y.joins,
|
|
1232
1218
|
...l && { where: l },
|
|
1233
|
-
select:
|
|
1234
|
-
...
|
|
1219
|
+
select: X,
|
|
1220
|
+
...N.length > 0 && { groupBy: N },
|
|
1235
1221
|
...W && { having: W },
|
|
1236
|
-
...
|
|
1222
|
+
...s.distinct && { distinct: !0 }
|
|
1237
1223
|
};
|
|
1238
1224
|
}
|
|
1239
|
-
function
|
|
1240
|
-
return ot(
|
|
1225
|
+
function u(s, c, f) {
|
|
1226
|
+
return ot(s, () => h(s, c, f));
|
|
1241
1227
|
}
|
|
1242
|
-
function
|
|
1243
|
-
var
|
|
1244
|
-
const
|
|
1245
|
-
if (
|
|
1246
|
-
const l = `ORDER BY, LIMIT and OFFSET go only in the query itself, not in ${
|
|
1247
|
-
|
|
1228
|
+
function h(s, c, f) {
|
|
1229
|
+
var X, j, E;
|
|
1230
|
+
const y = ((X = s.orderBy[0]) == null ? void 0 : X.span) ?? ((j = s.limit) == null ? void 0 : j.span) ?? ((E = s.offset) == null ? void 0 : E.span);
|
|
1231
|
+
if (y) {
|
|
1232
|
+
const l = `ORDER BY, LIMIT and OFFSET go only in the query itself, not in ${f}`;
|
|
1233
|
+
b(l, y, "leftOut");
|
|
1248
1234
|
}
|
|
1249
|
-
const
|
|
1235
|
+
const S = [], L = /* @__PURE__ */ new Set(), B = (l) => {
|
|
1250
1236
|
if (l.kind === "select")
|
|
1251
|
-
return
|
|
1237
|
+
return S.push(l), !0;
|
|
1252
1238
|
if (l.kind === "nested") {
|
|
1253
|
-
const
|
|
1254
|
-
return
|
|
1239
|
+
const N = l.query;
|
|
1240
|
+
return N.with || N.orderBy.length > 0 || N.limit || N.offset ? (b(
|
|
1255
1241
|
"A UNION's query in parentheses can't be imported with ORDER BY, LIMIT or WITH",
|
|
1256
1242
|
l.span
|
|
1257
|
-
), !1) : B(
|
|
1243
|
+
), !1) : B(N.body);
|
|
1258
1244
|
}
|
|
1259
|
-
return l.op !== "UNION" ? (
|
|
1245
|
+
return l.op !== "UNION" ? (b(`${l.op} can't be imported`, l.span), !1) : (L.add(l.all), B(l.left) && B(l.right));
|
|
1260
1246
|
};
|
|
1261
|
-
if (!B(
|
|
1262
|
-
if (
|
|
1263
|
-
|
|
1247
|
+
if (!B(s.body)) return;
|
|
1248
|
+
if (L.size > 1) {
|
|
1249
|
+
b("UNION and UNION ALL mixed can't be imported", s.body.span);
|
|
1264
1250
|
return;
|
|
1265
1251
|
}
|
|
1266
|
-
const
|
|
1267
|
-
const
|
|
1268
|
-
return
|
|
1252
|
+
const _ = S.map((l) => {
|
|
1253
|
+
const N = r(l, { tables: [], around: c }, f);
|
|
1254
|
+
return N && ut(N);
|
|
1269
1255
|
});
|
|
1270
|
-
if (!
|
|
1271
|
-
return
|
|
1256
|
+
if (!_.some((l) => l === void 0))
|
|
1257
|
+
return _.length === 1 ? _[0] : { union: _, all: L.has(!0) };
|
|
1272
1258
|
}
|
|
1273
|
-
function
|
|
1274
|
-
var
|
|
1275
|
-
const
|
|
1276
|
-
if (!
|
|
1277
|
-
|
|
1259
|
+
function U(s, c, f) {
|
|
1260
|
+
var N, K;
|
|
1261
|
+
const y = s.body.kind === "select" ? s.body : void 0;
|
|
1262
|
+
if (!y || s.with || s.orderBy.length > 0 || s.limit || s.offset || y.groupBy.length > 0 || y.having || y.distinct || y.top) {
|
|
1263
|
+
b("An EXISTS can only be SELECT … FROM … WHERE …", f);
|
|
1278
1264
|
return;
|
|
1279
1265
|
}
|
|
1280
|
-
const
|
|
1281
|
-
if ((
|
|
1282
|
-
|
|
1266
|
+
const S = (N = y.from[0]) == null ? void 0 : N.table;
|
|
1267
|
+
if ((S == null ? void 0 : S.kind) === "derived") {
|
|
1268
|
+
b("An EXISTS's first table can't be a sub query", S.span);
|
|
1283
1269
|
return;
|
|
1284
1270
|
}
|
|
1285
|
-
if ((
|
|
1286
|
-
|
|
1271
|
+
if ((S == null ? void 0 : S.kind) === "table" && S.name.length === 1 && w(S.name[0].value)) {
|
|
1272
|
+
b("An EXISTS's first table can't be a WITH query", S.span);
|
|
1287
1273
|
return;
|
|
1288
1274
|
}
|
|
1289
|
-
const
|
|
1290
|
-
if (!
|
|
1291
|
-
const B = (W) => W.type === "field" ? [W] : Wt(W).flatMap(B),
|
|
1275
|
+
const L = r({ ...y, columns: [] }, { tables: [], outer: c }, "EXISTS");
|
|
1276
|
+
if (!L) return;
|
|
1277
|
+
const B = (W) => W.type === "field" ? [W] : Wt(W).flatMap(B), _ = ((K = L.where) == null ? void 0 : K.type) === "logical" && L.where.operator === "AND" ? L.where.operands : L.where && [L.where], X = (W) => B(W).some((O) => i.outerFields.has(O)), j = (_ == null ? void 0 : _.filter(X)) ?? [], E = (_ == null ? void 0 : _.filter((W) => !X(W))) ?? [];
|
|
1292
1278
|
if (j.length === 0) {
|
|
1293
|
-
|
|
1279
|
+
b("An EXISTS must name the query around it in its WHERE", f);
|
|
1294
1280
|
return;
|
|
1295
1281
|
}
|
|
1296
1282
|
const l = (W) => W.length === 1 ? W[0] : { type: "logical", operator: "AND", operands: W };
|
|
1297
1283
|
return {
|
|
1298
1284
|
type: "exists",
|
|
1299
1285
|
query: {
|
|
1300
|
-
from:
|
|
1301
|
-
joins:
|
|
1286
|
+
from: L.from,
|
|
1287
|
+
joins: L.joins,
|
|
1302
1288
|
...E.length > 0 && { where: l(E) }
|
|
1303
1289
|
},
|
|
1304
1290
|
link: l(j)
|
|
1305
1291
|
};
|
|
1306
1292
|
}
|
|
1307
|
-
const
|
|
1308
|
-
if (
|
|
1309
|
-
if (
|
|
1310
|
-
|
|
1293
|
+
const P = (s, c) => {
|
|
1294
|
+
if (s) {
|
|
1295
|
+
if (s.kind === "number" && /^\d+$/.test(s.text)) return Number(s.text);
|
|
1296
|
+
b(`${c} can only be a whole number`, s.span, "leftOut");
|
|
1311
1297
|
}
|
|
1312
|
-
},
|
|
1298
|
+
}, $ = () => ot(a, () => Z()), Z = () => {
|
|
1313
1299
|
var E;
|
|
1314
|
-
const
|
|
1315
|
-
let
|
|
1300
|
+
const s = { tables: [] };
|
|
1301
|
+
let c, f;
|
|
1316
1302
|
if (a.body.kind === "select") {
|
|
1317
1303
|
const l = a.body;
|
|
1318
|
-
|
|
1304
|
+
f = l.top && P(l.top, "TOP"), c = r({ ...l, top: void 0 }, s, "the query");
|
|
1319
1305
|
} else {
|
|
1320
|
-
const l = { body: a.body, orderBy: [], span: a.body.span },
|
|
1321
|
-
if (
|
|
1322
|
-
const K =
|
|
1306
|
+
const l = { body: a.body, orderBy: [], span: a.body.span }, N = u(l, s, "a UNION");
|
|
1307
|
+
if (N) {
|
|
1308
|
+
const K = k.tables.map((R) => R.name);
|
|
1323
1309
|
let W = 1;
|
|
1324
1310
|
for (; K.includes(`sub_query_${W}`); ) W += 1;
|
|
1325
1311
|
const O = `sub_query_${W}`;
|
|
1326
|
-
|
|
1327
|
-
from: { id: nt(), alias: O, query:
|
|
1312
|
+
s.tables.push({ ref: O, columns: t(N) }), c = {
|
|
1313
|
+
from: { id: nt(), alias: O, query: N },
|
|
1328
1314
|
joins: [],
|
|
1329
1315
|
select: []
|
|
1330
1316
|
};
|
|
1331
1317
|
}
|
|
1332
1318
|
}
|
|
1333
|
-
if (!
|
|
1334
|
-
const
|
|
1319
|
+
if (!c) return;
|
|
1320
|
+
const y = ((E = a.limit) == null ? void 0 : E.kind) === "number" && a.limit.text === de ? void 0 : f ?? P(a.limit, "LIMIT"), S = P(a.offset, "OFFSET") || void 0, L = c.select, B = L.length > 0 ? L.map((l) => yt(l, v)) : s.tables.flatMap((l) => l.columns.map((N) => ({
|
|
1335
1321
|
type: "field",
|
|
1336
1322
|
table: l.ref,
|
|
1337
|
-
column:
|
|
1338
|
-
}))),
|
|
1339
|
-
l.nulls &&
|
|
1340
|
-
const { expr:
|
|
1323
|
+
column: N
|
|
1324
|
+
}))), _ = (l) => l.kind === "subquery" && l.query.body.kind === "select" && l.query.body.from.length === 0 && l.query.body.columns.length === 1 && l.query.body.columns[0].expr.kind === "null", X = (l) => {
|
|
1325
|
+
l.nulls && b("NULLS FIRST / LAST can't be imported", l.span, "leftOut");
|
|
1326
|
+
const { expr: N } = l, K = N.kind === "name" && N.parts.length === 1 ? st(L.flatMap((Y) => Y.alias ?? []), N.parts[0].value) : void 0, W = K !== void 0 && L.find((Y) => Y.alias === K), O = N.kind === "number" && /^\d+$/.test(N.text) ? B[Number(N.text) - 1] : void 0, R = W && yt(W, v) || O || I(N, { scope: s });
|
|
1341
1327
|
return {
|
|
1342
1328
|
id: nt(),
|
|
1343
|
-
expression: Ot(R,
|
|
1329
|
+
expression: Ot(R, L, v),
|
|
1344
1330
|
direction: l.direction ?? "asc"
|
|
1345
1331
|
};
|
|
1346
|
-
}, j = a.orderBy.filter((l) => !
|
|
1347
|
-
const { value:
|
|
1348
|
-
return
|
|
1332
|
+
}, j = a.orderBy.filter((l) => !_(l.expr)).flatMap((l) => {
|
|
1333
|
+
const { value: N, failed: K } = M(() => X(l));
|
|
1334
|
+
return p && K.length > 0 && y === void 0 && S === void 0 ? (J(K, "its sort key"), []) : [N];
|
|
1349
1335
|
});
|
|
1350
|
-
return
|
|
1336
|
+
return ut({
|
|
1351
1337
|
id: nt(),
|
|
1352
|
-
...
|
|
1353
|
-
...
|
|
1338
|
+
...C.length > 0 && { shared: C },
|
|
1339
|
+
...c,
|
|
1354
1340
|
...j.length > 0 && { orderBy: j },
|
|
1355
|
-
...
|
|
1356
|
-
...
|
|
1341
|
+
...y !== void 0 && { limit: y },
|
|
1342
|
+
...S && { offset: S }
|
|
1357
1343
|
});
|
|
1358
|
-
},
|
|
1344
|
+
}, x = $(), z = i.problems.some((s) => s.severity === "error");
|
|
1359
1345
|
return {
|
|
1360
|
-
...
|
|
1346
|
+
...x && (p || !z) && { query: x },
|
|
1361
1347
|
problems: i.problems
|
|
1362
1348
|
};
|
|
1363
1349
|
}
|