@nongt/visql 0.7.0 → 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.
@@ -1,24 +1,24 @@
1
- var kt = Object.defineProperty;
2
- var Ct = (o, T, U) => T in o ? kt(o, T, { enumerable: !0, configurable: !0, writable: !0, value: U }) : o[T] = U;
3
- var gt = (o, T, U) => Ct(o, typeof T != "symbol" ? T + "" : T, U);
4
- import { c as Dt, d as nt, p as It, 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";
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 Lt(o, T, U = T) {
7
- const L = o.slice(0, T);
6
+ function kt(o, T, D = T) {
7
+ const k = o.slice(0, T);
8
8
  return {
9
9
  offset: T,
10
- line: L.split(`
10
+ line: k.split(`
11
11
  `).length,
12
- column: T - L.lastIndexOf(`
12
+ column: T - k.lastIndexOf(`
13
13
  `),
14
- length: U - T
14
+ length: D - T
15
15
  };
16
16
  }
17
17
  class ft extends Error {
18
- constructor(U, L, v, a = v) {
19
- super(U);
20
- gt(this, "at");
21
- this.name = "SqlSyntaxError", this.at = Lt(L, v, a);
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 U = [...jt[T], ...Xt], L = T === "mysql", v = (i, N, I) => {
68
- const M = L && i === "string";
69
- let Q = "", x = N + 1;
70
- for (; x < o.length; ) {
71
- const f = o[x];
72
- if (M && f === "\\" && x + 1 < o.length)
73
- Q += Kt[o[x + 1]] ?? o[x + 1], x += 2;
74
- else if (f === I && o[x + 1] === I)
75
- Q += I, x += 2;
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 (f === I)
78
- return { kind: i, value: Q, end: x + 1 };
79
- Q += f, x += 1;
77
+ if (d === I)
78
+ return { kind: i, value: J, end: q + 1 };
79
+ J += d, q += 1;
80
80
  }
81
81
  }
82
- const k = i === "string" ? "A text" : "A quoted name";
83
- throw new ft(`${k} is never closed`, o, N, o.length);
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 $, G;
86
- const N = o[i], I = o.slice(i), M = Ht[T][N], Q = ($ = Yt.exec(I)) == null ? void 0 : $[0], x = (G = zt.exec(I)) == null ? void 0 : G[0], k = Vt[T].exec(I), f = U.find((w) => I.startsWith(w));
87
- if (/\s/.test(N)) return { end: i + 1 };
88
- if (I.startsWith("--") || L && N === "#") {
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 (N === "'" || L && N === '"') return v("string", i, N);
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 (Q) return { kind: "number", value: Q, end: i + Q.length };
103
- if (x) return { kind: "word", value: x, end: i + x.length };
104
- if (k)
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: k[1] ?? k[3],
108
- style: k[2] ?? ":",
109
- end: i + k[0].length
107
+ value: C[1] ?? C[3],
108
+ style: C[2] ?? ":",
109
+ end: i + C[0].length
110
110
  };
111
- if (f) return { kind: "op", value: f, end: i + f.length };
112
- throw new ft(`Unexpected character "${N}"`, o, i, i + 1);
113
- }, c = [];
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 N = a(i);
116
- N.kind && c.push({ ...N, start: i }), i = N.end;
115
+ const b = a(i);
116
+ b.kind && p.push({ ...b, start: i }), i = b.end;
117
117
  }
118
- return c.push({
118
+ return p.push({
119
119
  kind: "end",
120
120
  value: "",
121
121
  start: o.length,
122
122
  end: o.length
123
- }), c;
123
+ }), p;
124
124
  }
125
- function ut(o, T, U, L, v = "error") {
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 ? `${U} (left out)` : U,
131
- at: Lt(o.sql, L.start, L.end)
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 U = T.toLowerCase(), L = o.filter((v) => v.toLowerCase() === U);
137
- return L.length === 1 ? L[0] : void 0;
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, lt = (o) => o !== void 0 && Dt.includes(o);
182
- function oe(o, T, U) {
183
- const L = (t, r, u) => (ut(o, t, r, u), Tt()), v = (t, r) => L("unsupported", t, r), a = (t) => o.sql.slice(t.start, t.end), c = (t) => tt(t), i = (t) => {
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)) ? It(r.name, o.functions) : It(t, o.functions);
186
- }, N = (t, r) => L("unknownFunction", `${t} isn't a function the query can use`, r), I = (t, r, u, h = {}) => {
187
- const C = i(t);
188
- return !C && !o.allowUnknownFunctions ? N(t, u) : h.distinct && C && !C.aggregate ? v(`DISTINCT goes only in an aggregate, not in ${C.name}`, u) : {
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: (C == null ? void 0 : C.name) ?? t,
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), Q = (t) => {
196
- var F, V;
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 q = (F = U.alias) == null ? void 0 : F.call(U, r);
200
- if (q) return q;
199
+ const x = ($ = D.alias) == null ? void 0 : $.call(D, r);
200
+ if (x) return x;
201
201
  }
202
- for (let q = U.scope, n = !1; q; q = q.outer, n = !0) {
203
- const d = (p, g) => {
204
- const y = { type: "field", table: p, column: g };
205
- return n && o.outerFields.add(y), y;
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 p = st(q.tables.map((y) => y.ref), u.value), g = q.tables.find((y) => y.ref === p);
209
- if (g) {
210
- const y = st(g.columns, r.value);
211
- return y ? d(g.ref, y) : L("unknownColumn", `${g.ref} has no column "${r.value}"`, r.span);
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 p = q.tables.flatMap((g) => {
215
- const y = st(g.columns, r.value);
216
- return y ? [{ ref: g.ref, col: y }] : [];
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 (p.length > 1) {
219
- const g = p.map((y) => y.ref).join(" and ");
220
- return L(
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 ${g}: name its table`,
222
+ `"${r.value}" is a column of ${f}: name its table`,
223
223
  t.span
224
224
  );
225
225
  }
226
- if (p.length === 1) return d(p[0].ref, p[0].col);
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 ((V = U.scope.around) == null ? void 0 : V.tables.some((q) => u ? st([q.ref], u.value) : st(q.columns, r.value)))
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 X = u && U.scope.tables.find((q) => q.table !== void 0 && q.ref !== q.table && st([q.table], u.value));
237
- if (X) {
238
- const q = `${X.table} goes by its alias here: ${X.ref}`;
239
- return L("unknownTable", q, u.span);
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 ? L("unknownTable", `No table here is named "${u.value}"`, u.span) : L("unknownColumn", `No table here has a column "${r.value}"`, r.span);
242
- }, x = (t) => {
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 = x(t.right);
245
- return h && ae(h.amount) ? { amount: c(t.left), unit: h.unit } : void 0;
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: c(t.value.left), unit: "MILLISECOND" };
250
- if (t.unit) return lt(t.unit) ? { amount: c(t.value), unit: t.unit } : void 0;
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 && lt(u) ? { amount: rt(Number(r[1])), unit: u } : void 0;
253
- }, k = (t) => {
254
- const r = x(t.right);
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
- c(t.left),
257
+ p(t.left),
258
258
  r.amount,
259
259
  M(r.unit)
260
260
  ], t.span);
261
- const u = t.op === "+" ? x(t.left) : void 0;
262
- return u && I("DATE_ADD", [c(t.right), u.amount, M(u.unit)], t.span);
263
- }, f = (t, r) => {
264
- const [u, h, C] = t.args, X = t.args.length;
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 F = X === 2 ? x(h) : void 0;
271
- return F && I(r === "DATE_ADD" || r === "ADDDATE" ? "DATE_ADD" : "DATE_SUB", [
272
- c(u),
273
- F.amount,
274
- M(F.unit)
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 F = ht(u);
279
- if (X !== 3 || !lt(F)) return;
280
- const V = h.kind === "unary" && h.op === "-";
281
- return I(V ? "DATE_SUB" : "DATE_ADD", [
282
- c(C),
283
- c(V ? h.operand : h),
284
- M(F)
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 F = ht(u);
290
- return X === 3 && lt(F) ? I("DATEDIFF", [c(h), c(C), M(F)], t.span) : r === "DATEDIFF" && X === 2 ? I("DATEDIFF", [c(h), c(u), M("DAY")], t.span) : void 0;
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 F = ht(u);
294
- return X === 2 && lt(F) ? I("EXTRACT", [c(h), M(F)], t.span) : void 0;
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 F = (u == null ? void 0 : u.kind) === "string" ? u.value.toUpperCase() : void 0;
298
- return X === 2 && lt(F) ? I("DATE_TRUNC", [c(h), M(F)], t.span) : void 0;
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
- }, $ = (t, r) => t.map((u) => (u.nulls && ut(o, "unsupported", "NULLS FIRST / LAST can't be imported", u.span, "leftOut"), { expression: c(r(u.expr)), direction: u.direction ?? "asc" })), G = (t, r, u) => {
304
- const h = Jt[o.dialect], C = (p) => p.kind === "cast" && p.type.toUpperCase() === h ? p.operand : p, X = r === "GROUP_CONCAT", [F, V, ...q] = t.args;
305
- if (!F || q.length > 0 || X && V)
306
- return v(`${r} takes one value${X ? "" : " and a separator"}`, u);
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 n = X ? t.separator ?? { kind: "string", value: ",", span: u } : V;
310
- if (n && n.kind !== "string")
311
- return v("A separator can only be a text", n.span);
312
- const d = t.orderBy ?? t.withinGroup;
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
- c(C(F)),
314
+ p(U($)),
315
315
  // Without one, STRING_AGG's separator is ', '.
316
- ...n && n.value !== ", " ? [rt(n.value)] : []
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
- ...d && { orderBy: $(d, C) }
320
+ ...s && { orderBy: F(s, U) }
321
321
  });
322
322
  }, w = (t) => {
323
- var V, q;
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 [n, d, p] = t.args.map(c);
328
+ const [z, s, c] = t.args.map(p);
329
329
  return {
330
330
  type: "if",
331
- condition: n,
332
- then: d,
333
- else: p
331
+ condition: z,
332
+ then: s,
333
+ else: c
334
334
  };
335
335
  }
336
- const C = f(t, u);
337
- if (C) return C;
336
+ const U = d(t, u);
337
+ if (U) return U;
338
338
  if (u === "STRING_AGG" || u === "GROUP_CONCAT" || u === "LISTAGG")
339
- return G(t, u, t.span);
340
- const X = ((V = t.orderBy) == null ? void 0 : V[0]) ?? ((q = t.withinGroup) == null ? void 0 : q[0]);
341
- if (X) return v(`${r} takes no order of its own`, X.span);
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 N(r, h);
347
- const F = t.args.map((n) => n.kind === "star" ? v("`*` only goes in COUNT(*)", n.span) : c(n));
348
- return I(r, F, h, { distinct: t.distinct });
349
- }, z = (t, r, u) => ({
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, C, X) => {
356
- !X && h.kind === "binary" && h.op === t.op && h.span.start === C.span.start ? (u(h.left, h, !1), u(h.right, h, !0)) : r.push(c(h));
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
- }, at = (t) => {
360
- const r = c(t.left), u = c(t.right);
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 z(t.op, ...r());
371
+ return V(t.op, ...r());
386
372
  case "<>":
387
373
  case "!=":
388
- return z("!=", ...r());
374
+ return V("!=", ...r());
389
375
  case "LIKE":
390
- return z("LIKE", ...r());
376
+ return V("LIKE", ...r());
391
377
  case "NOT LIKE":
392
- return { type: "not", operand: z("LIKE", ...r()) };
378
+ return { type: "not", operand: V("LIKE", ...r()) };
393
379
  case "REGEXP":
394
380
  case "RLIKE":
395
381
  case "~":
396
- return z("REGEXP", ...r());
382
+ return V("REGEXP", ...r());
397
383
  case "NOT REGEXP":
398
384
  case "NOT RLIKE":
399
385
  case "!~":
400
- return { type: "not", operand: z("REGEXP", ...r()) };
386
+ return { type: "not", operand: V("REGEXP", ...r()) };
401
387
  case "<=>":
402
388
  case "IS NOT DISTINCT FROM":
403
- return at(t);
389
+ return V("IS NOT DISTINCT FROM", ...r());
404
390
  case "IS DISTINCT FROM":
405
- return { type: "not", operand: at(t) };
391
+ return { type: "not", operand: V("IS NOT DISTINCT FROM", ...r()) };
406
392
  case "+":
407
393
  case "-": {
408
- const u = k(t);
394
+ const u = C(t);
409
395
  if (u) return u;
410
- const h = [t.left, t.right].find((C) => C.kind === "interval");
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 tt(t) {
409
+ function ct(t) {
424
410
  switch (t.kind) {
425
411
  case "name":
426
- return Q(t);
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 } : L("positionalParam", `${a(t.span)} has no name: write :name`, t.span);
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: c(t.operand) };
448
- if (t.op === "+") return c(t.operand);
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 pt(t);
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: c(t.operand)
446
+ left: p(t.operand)
461
447
  };
462
448
  case "in": {
463
- const r = c(t.operand);
449
+ const r = p(t.operand);
464
450
  let u;
465
451
  if (t.query) {
466
- const C = o.inQuery(t.query, U.scope);
467
- u = C ? { type: "subquery", id: nt(), query: C } : Tt();
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(c) };
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: c(t.operand),
483
- right: { type: "list", items: [c(t.low), c(t.high)] }
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", [c(t.operand), rt(t.type)], t.span) : v(`The type ${t.type} can't be imported`, t.span);
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 lt(t.unit) ? I("EXTRACT", [c(t.operand), M(t.unit)], t.span) : v(`EXTRACT of ${t.unit} can't be imported`, t.span);
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((C) => ({ when: c(C.when), then: c(C.then) })), u = t.subject && c(t.subject), h = t.else && c(t.else);
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, U.scope, t.span) ?? Tt();
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 tt(T);
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 U = Zt(o, T);
605
- let L = 0, v = 0;
606
- const a = (e = 0) => U[Math.min(L + e, U.length - 1)], c = () => {
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" && (L += 1), v = e.end, e;
609
- }, i = (e) => ({ start: e, end: v }), N = (e) => e.kind === "word" ? e.value.toUpperCase() : "", I = (e, ...s) => s.includes(N(e)), M = (e, s) => e.kind === "op" && e.value === s, Q = (e) => e.kind === "end" ? "the end" : `"${o.slice(e.start, e.end)}"`, x = (e, s = a()) => {
610
- throw new ft(e, o, s.start, s.end);
611
- }, k = (e) => x(`Expected ${e} but found ${Q(a())}`), f = (...e) => I(a(), ...e) && c(), $ = (e) => M(a(), e) && c(), G = (e) => f(e) || k(e), w = (e) => $(e) || k(`"${e}"`), z = (e) => {
612
- const s = [e()];
613
- for (; $(","); ) s.push(e());
614
- return s;
615
- }, ot = (e) => I(e, "SELECT", "WITH"), at = (e) => ({
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
- }), pt = (e) => e.kind === "quoted" || e.kind === "word" && !St.has(N(e)), tt = () => pt(a()) ? at(c()) : k("a name"), t = (e = !1) => {
620
- const s = f("AS"), m = a();
621
- return pt(m) || e && m.kind === "string" ? at(c()) : s ? k("a name") : void 0;
622
- }, r = () => {
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 s = 1; s > 0; ) {
625
- const m = c();
626
- m.kind === "end" && k('")"'), M(m, "(") && (s += 1), M(m, ")") && (s -= 1);
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
- }, u = () => {
630
- const { start: e } = a(), s = E(), m = f("ASC", "DESC"), S = f("NULLS") && (f("FIRST", "LAST") || k("FIRST or LAST"));
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: s,
618
+ expr: n,
633
619
  ...m && { direction: m.value.toLowerCase() },
634
- ...S && { nulls: S.value.toLowerCase() },
620
+ ...A && { nulls: A.value.toLowerCase() },
635
621
  span: i(e)
636
622
  };
637
- }, h = () => (G("BY"), z(u)), C = (e) => {
638
- a().kind !== "word" && k("a type");
639
- const s = [c().value];
640
- for (; a().kind === "word" && (e || le.has(N(a()))); )
641
- s.push(c().value);
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 b = z(() => {
645
- const S = a();
646
- return S.kind === "number" || I(S, "MAX") ? c().value : k("a size");
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 = `(${b.join(", ")})`;
634
+ w(")"), m = `(${g.join(", ")})`;
649
635
  }
650
- return s.join(" ") + m;
651
- }, X = (e, s) => {
636
+ return n.join(" ") + m;
637
+ }, $ = (e, n) => {
652
638
  w("(");
653
- const m = !!f("DISTINCT");
654
- m || f("ALL");
655
- let b = [], S, _;
656
- M(a(), ")") || (b = z(E), f("ORDER") && (S = h()), T === "mysql" && f("SEPARATOR") && (_ = E())), w(")");
657
- let Z;
658
- f("WITHIN") && (G("GROUP"), w("("), G("ORDER"), Z = h(), w(")"));
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), "(") && (c(), dt = r());
646
+ I(a(), "FILTER") && M(a(1), "(") && (p(), dt = u());
661
647
  let Et;
662
- const vt = f("OVER");
663
- return vt && (M(a(), "(") ? r() : tt(), Et = i(vt.start)), {
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: b,
652
+ args: g,
667
653
  distinct: m,
668
- ...S && { orderBy: S },
669
- ..._ && { separator: _ },
670
- ...Z && { withinGroup: Z },
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(s)
659
+ span: i(n)
674
660
  };
675
- }, F = () => {
676
- const { start: e } = a(), s = [at(c())];
677
- for (; $("."); ) {
678
- if ($("*")) return { kind: "star", table: s, span: i(e) };
679
- s.push(tt());
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(), "(") ? X(s, e) : { kind: "name", parts: s, span: i(e) };
682
- }, V = (e) => {
683
- const s = I(a(), "WHEN") ? void 0 : E(), m = [];
684
- for (; f("WHEN"); ) {
685
- const S = E();
686
- G("THEN"), m.push({ when: S, then: E() });
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 && k("WHEN");
689
- const b = f("ELSE") ? E() : void 0;
690
- return G("END"), {
674
+ m.length === 0 && C("WHEN");
675
+ const g = d("ELSE") ? E() : void 0;
676
+ return H("END"), {
691
677
  kind: "case",
692
- ...s && { subject: s },
678
+ ...n && { subject: n },
693
679
  branches: m,
694
- ...b && { else: b },
680
+ ...g && { else: g },
695
681
  span: i(e)
696
682
  };
697
- }, q = () => {
698
- const e = a(), { start: s } = e, m = N(e);
699
- if (e.kind === "number") return { kind: "number", text: c().value, span: i(s) };
700
- if (e.kind === "string") return { kind: "string", value: c().value, span: i(s) };
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 c(), {
688
+ return p(), {
703
689
  kind: "param",
704
690
  style: e.style,
705
691
  name: e.value,
706
- span: i(s)
692
+ span: i(n)
707
693
  };
708
- if (e.kind === "quoted") return F();
709
- if ($("*")) return { kind: "star", span: i(s) };
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 S = et();
713
- return w(")"), { kind: "subquery", query: S, span: i(s) };
698
+ const A = et();
699
+ return w(")"), { kind: "subquery", query: A, span: i(n) };
714
700
  }
715
- const b = E();
716
- return M(a(), ",") && x("A list of values in parentheses (a row) can't be imported"), w(")"), b;
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 k("a value");
704
+ if (e.kind !== "word") return C("a value");
719
705
  if (m === "NULL" || m === "TRUE" || m === "FALSE")
720
- return c(), { kind: m.toLowerCase(), span: i(s) };
721
- if (f("CASE")) return V(s);
722
- if (f("EXISTS")) {
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 b = et();
725
- return w(")"), { kind: "exists", query: b, span: i(s) };
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 (c(), c(), m === "CAST") {
729
- const _ = E();
730
- G("AS");
731
- const Z = C(!0);
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: Z,
736
- span: i(s)
720
+ operand: G,
721
+ type: Q,
722
+ span: i(n)
737
723
  };
738
724
  }
739
- const b = a().kind === "word" ? N(c()) : k("a unit");
740
- G("FROM");
741
- const S = E();
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: b,
745
- operand: S,
746
- span: i(s)
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 c(), {
736
+ return p(), {
751
737
  kind: "typed",
752
738
  type: m,
753
- value: c().value,
754
- span: i(s)
739
+ value: p().value,
740
+ span: i(n)
755
741
  };
756
- if (f("INTERVAL")) {
757
- const b = D(), S = ce.has(N(a())) ? N(c()) : void 0;
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: b,
761
- ...S && { unit: S },
762
- span: i(s)
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), "(")) ? k("a value") : F();
766
- }, n = () => {
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 s = q();
769
- for (; $("::"); )
770
- s = {
754
+ let n = z();
755
+ for (; F("::"); )
756
+ n = {
771
757
  kind: "cast",
772
- operand: s,
773
- type: C(!1),
758
+ operand: n,
759
+ type: P(!1),
774
760
  span: i(e)
775
761
  };
776
- return s;
777
- }, d = () => {
778
- const { start: e } = a(), s = $("-") || $("+");
779
- if (!s) return n();
780
- const m = d();
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: s.value,
769
+ op: n.value,
784
770
  operand: m,
785
771
  span: i(e)
786
772
  };
787
- }, p = (e, s) => () => {
773
+ }, f = (e, n) => () => {
788
774
  const { start: m } = a();
789
- let b = e();
790
- for (let S = s(a()); S; S = s(a())) {
791
- c();
792
- const _ = e();
793
- b = {
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: S,
796
- left: b,
797
- right: _,
781
+ op: A,
782
+ left: g,
783
+ right: G,
798
784
  span: i(m)
799
785
  };
800
786
  }
801
- return b;
802
- }, g = (...e) => (s) => s.kind === "op" && e.includes(s.value) ? s.value : void 0, y = p(d, g("*", "/", "%")), D = p(y, g("+", "-")), B = p(D, g(...T === "mysql" ? [] : ["||"])), P = () => {
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 s = B();
790
+ let n = B();
805
791
  for (; ; ) {
806
792
  const m = a();
807
793
  if (m.kind === "op" && ue.has(m.value))
808
- c(), s = {
794
+ p(), n = {
809
795
  kind: "binary",
810
796
  op: m.value,
811
- left: s,
797
+ left: n,
812
798
  right: B(),
813
799
  span: i(e)
814
800
  };
815
- else if (f("IS")) {
816
- const b = !!f("NOT");
817
- f("NULL") ? s = {
801
+ else if (d("IS")) {
802
+ const g = !!d("NOT");
803
+ d("NULL") ? n = {
818
804
  kind: "isNull",
819
- operand: s,
820
- not: b,
805
+ operand: n,
806
+ not: g,
821
807
  span: i(e)
822
- } : f("DISTINCT") ? (G("FROM"), s = {
808
+ } : d("DISTINCT") ? (H("FROM"), n = {
823
809
  kind: "binary",
824
- op: `IS ${b ? "NOT " : ""}DISTINCT FROM`,
825
- left: s,
810
+ op: `IS ${g ? "NOT " : ""}DISTINCT FROM`,
811
+ left: n,
826
812
  right: B(),
827
813
  span: i(e)
828
- }) : I(a(), "TRUE", "FALSE") ? s = {
814
+ }) : I(a(), "TRUE", "FALSE") ? n = {
829
815
  kind: "binary",
830
- op: b ? "IS NOT" : "IS",
831
- left: s,
832
- right: q(),
816
+ op: g ? "IS NOT" : "IS",
817
+ left: n,
818
+ right: z(),
833
819
  span: i(e)
834
- } : k("NULL");
820
+ } : C("NULL");
835
821
  } else {
836
- const b = I(m, "NOT");
837
- b && c();
838
- const S = a();
839
- if (f("IN")) {
822
+ const g = I(m, "NOT");
823
+ g && p();
824
+ const A = a();
825
+ if (d("IN")) {
840
826
  w("(");
841
- const _ = ot(a()) ? { query: et() } : { list: z(E) };
842
- w(")"), s = {
827
+ const G = ot(a()) ? { query: et() } : { list: V(E) };
828
+ w(")"), n = {
843
829
  kind: "in",
844
- operand: s,
845
- not: b,
846
- ..._,
830
+ operand: n,
831
+ not: g,
832
+ ...G,
847
833
  span: i(e)
848
834
  };
849
- } else if (f("BETWEEN")) {
850
- const _ = B();
851
- G("AND"), s = {
835
+ } else if (d("BETWEEN")) {
836
+ const G = B();
837
+ H("AND"), n = {
852
838
  kind: "between",
853
- operand: s,
854
- not: b,
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 (f("LIKE", "ILIKE", "REGEXP", "RLIKE")) {
860
- const _ = B(), Z = f("ESCAPE") && q();
861
- s = {
845
+ } else if (d("LIKE", "ILIKE", "REGEXP", "RLIKE")) {
846
+ const G = B(), Q = d("ESCAPE") && z();
847
+ n = {
862
848
  kind: "binary",
863
- op: `${b ? "NOT " : ""}${N(S)}`,
864
- left: s,
865
- right: _,
866
- ...Z && { escape: Z },
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 b ? k("IN, BETWEEN or LIKE") : s;
856
+ return g ? C("IN, BETWEEN or LIKE") : n;
871
857
  }
872
858
  }
873
- }, H = () => {
859
+ }, X = () => {
874
860
  const { start: e } = a();
875
- return f("NOT") ? {
861
+ return d("NOT") ? {
876
862
  kind: "unary",
877
863
  op: "NOT",
878
- operand: H(),
864
+ operand: X(),
879
865
  span: i(e)
880
- } : P();
881
- }, j = p(H, (e) => I(e, "AND") || T === "mysql" && M(e, "&&") ? "AND" : void 0), E = p(j, (e) => I(e, "OR") || T === "mysql" && M(e, "||") ? "OR" : void 0), l = () => {
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(), "(") && k("a sub query (SELECT …)");
885
- const b = et();
869
+ if (F("(")) {
870
+ !ot(a()) && !M(a(), "(") && C("a sub query (SELECT …)");
871
+ const g = et();
886
872
  w(")");
887
- const S = t();
873
+ const A = r();
888
874
  return {
889
875
  kind: "derived",
890
- query: b,
891
- ...S && { alias: S },
876
+ query: g,
877
+ ...A && { alias: A },
892
878
  span: i(e)
893
879
  };
894
880
  }
895
- const s = [tt()];
896
- for (; $("."); ) s.push(tt());
897
- const m = t();
881
+ const n = [t()];
882
+ for (; F("."); ) n.push(t());
883
+ const m = r();
898
884
  return {
899
885
  kind: "table",
900
- name: s,
886
+ name: n,
901
887
  ...m && { alias: m },
902
888
  span: i(e)
903
889
  };
904
- }, A = () => {
905
- if (f("JOIN")) return "inner";
906
- const e = f("INNER", "LEFT", "RIGHT", "FULL", "CROSS");
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") && x("NATURAL JOIN can't be imported: name its ON");
894
+ I(a(), "NATURAL") && q("NATURAL JOIN can't be imported: name its ON");
909
895
  return;
910
896
  }
911
- I(e, "INNER", "CROSS") || f("OUTER"), G("JOIN");
912
- const s = e.value.toLowerCase();
913
- return s === "inner" || s === "cross" || s === "left" || s === "right" ? s : "full";
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(), s = [];
901
+ const e = l(), n = [];
916
902
  for (; ; ) {
917
- const { start: m } = a(), b = A();
918
- if (!b) return { table: e, joins: s };
919
- const S = l();
920
- let _, Z;
921
- b !== "cross" && (f("USING") ? (w("("), Z = z(tt), w(")")) : (G("ON"), _ = E())), s.push({
922
- type: b,
923
- table: S,
924
- ..._ && { on: _ },
925
- ...Z && { using: Z },
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(), s = E(), m = t(T !== "postgres");
931
- return { expr: s, ...m && { alias: m }, span: i(e) };
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
- G("SELECT");
935
- const s = !!f("DISTINCT");
936
- s || f("ALL");
920
+ H("SELECT");
921
+ const n = !!d("DISTINCT");
922
+ n || d("ALL");
937
923
  let m;
938
- T === "mssql" && f("TOP") && (m = M(a(), "(") ? q() : d(), I(a(), "PERCENT") && x("TOP … PERCENT can't be imported"), I(a(), "WITH") && x("TOP … WITH TIES can't be imported"));
939
- const b = z(W), S = f("FROM") ? z(K) : [], _ = f("WHERE") ? E() : void 0;
940
- let Z = [];
941
- f("GROUP") && (G("BY"), Z = z(E));
942
- const dt = f("HAVING") ? E() : void 0;
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: s,
931
+ distinct: n,
946
932
  ...m && { top: m },
947
- columns: b,
948
- from: S,
949
- ..._ && { where: _ },
950
- groupBy: Z,
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 (!$("(")) return O();
957
- const s = et();
958
- return w(")"), { kind: "nested", query: s, span: i(e) };
959
- }, Y = (e, ...s) => () => {
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 b = e();
962
- for (let S = f(...s); S; S = f(...s)) {
963
- const _ = !!f("ALL");
964
- _ || f("DISTINCT"), b = {
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: N(S),
967
- all: _,
968
- left: b,
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 b;
974
- }, J = Y(Y(R, "INTERSECT"), "UNION", "EXCEPT"), it = () => {
975
- const { start: e } = a(), s = tt();
959
+ return g;
960
+ }, tt = Y(Y(R, "INTERSECT"), "UNION", "EXCEPT"), at = () => {
961
+ const { start: e } = a(), n = t();
976
962
  let m;
977
- $("(") && (m = z(tt), w(")")), G("AS");
978
- const b = a().start, S = f("NOT");
979
- !f("MATERIALIZED") && S && k("MATERIALIZED");
980
- const _ = b === a().start ? void 0 : i(b);
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 Z = et();
968
+ const Q = et();
983
969
  return w(")"), {
984
- name: s,
970
+ name: n,
985
971
  ...m && { columns: m },
986
- ..._ && { hint: _ },
987
- query: Z,
972
+ ...G && { hint: G },
973
+ query: Q,
988
974
  span: i(e)
989
975
  };
990
- }, ct = () => {
991
- let e, s;
992
- return T !== "mssql" && f("LIMIT") && (T === "postgres" && f("ALL") || (e = E(), T === "mysql" && $(",") && (s = e, e = E()))), f("OFFSET") && (s = E(), !f("ROWS", "ROW") && T === "mssql" && k("ROWS")), T !== "mysql" && f("FETCH") && (f("FIRST", "NEXT") || k("NEXT"), e = E(), f("ROWS", "ROW") || k("ROWS"), G("ONLY")), { ...e && { limit: e }, ...s && { offset: s } };
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 s, m = !1;
997
- f("WITH") && (m = !!f("RECURSIVE"), s = z(it));
998
- const b = J(), S = f("ORDER") ? h() : [];
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
- ...s && { with: s },
986
+ ...n && { with: n },
1001
987
  ...m && { recursive: m },
1002
- body: b,
1003
- orderBy: S,
1004
- ...ct(),
988
+ body: g,
989
+ orderBy: A,
990
+ ...lt(),
1005
991
  span: i(e)
1006
992
  };
1007
993
  }
1008
994
  const mt = et();
1009
- return $(";"), a().kind !== "end" && x(ot(a()) ? "Only one query can be imported" : `Unexpected ${Q(a())} after the query`), mt;
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, U) {
1013
- const L = T.filter((a) => wt(a.expression, U)), v = (a) => {
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 c = L.find((i) => Rt(i.expression, a));
1016
- return c ? { ...a, fromSelect: c.id } : Bt(a, v);
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: U, schema: L } = T, v = T.functions ?? Ut;
1007
+ const { dialect: D, schema: k } = T, v = T.functions ?? Ut;
1022
1008
  let a;
1023
1009
  try {
1024
- a = pe(o, U);
1025
- } catch (n) {
1026
- if (!(n instanceof ft)) throw n;
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: n.message,
1032
- at: n.at
1017
+ message: s.message,
1018
+ at: s.at
1033
1019
  }]
1034
1020
  };
1035
1021
  }
1036
- const c = T.mode === "lenient", i = {
1022
+ const p = T.mode === "lenient", i = {
1037
1023
  sql: o,
1038
- lenient: c,
1039
- dialect: U,
1040
- schema: L,
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: (n, d, p) => h(n, d, p),
1046
- inQuery: (n, d) => {
1047
- const p = r(n, d, "IN");
1048
- if (!p || !("union" in p)) return p;
1049
- N("A UNION after IN can't be imported", n.span);
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
- }, N = (n, d, p) => ut(i, "unsupported", n, d, p), I = (n, d) => oe(i, n, d), M = (n) => {
1052
- const d = i.problems.length, p = n(), g = i.problems.slice(d).filter((y) => y.severity === "error");
1053
- return { value: p, failed: g };
1054
- }, Q = (n, d) => n.forEach((p) => {
1055
- Object.assign(p, { severity: "warning", message: `${p.message} (${d} is left out)` });
1056
- }), x = /* @__PURE__ */ new Map(), k = [], f = /* @__PURE__ */ new Map(), $ = [], G = /* @__PURE__ */ new Set(), w = (n) => $.reduceRight(
1057
- (d, p) => d ?? p.get(n.toLowerCase()),
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
- ), z = (n) => {
1060
- const d = n.name.value;
1061
- n.hint && ut(i, "unsupported", "MATERIALIZED only changes how the database plans it, not its rows (left out)", n.hint, "warning"), G.add(d.toLowerCase());
1062
- const p = r(n.query, { tables: [] }, "WITH");
1063
- if (G.delete(d.toLowerCase()), !p) return !1;
1064
- const g = bt(p);
1065
- let y = p;
1066
- if (n.columns) {
1067
- if (n.columns.length !== g.select.length) {
1068
- const E = `${d} names ${n.columns.length} column(s), its query gives ${g.select.length}`;
1069
- return ut(i, "unsupported", E, n.span), !1;
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 = g.select.map((E, l) => ({ ...E, alias: n.columns[l].value }));
1072
- y = "union" in p ? { ...p, union: [{ ...g, select: j }, ...p.union.slice(1)] } : { ...g, select: j };
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 D = new Set(k.map((j) => j.name));
1075
- let B = d;
1076
- for (let j = 2; D.has(B); j += 1) B = `${d}_${j}`;
1077
- const P = nt();
1078
- k.push({ id: P, name: B, query: y });
1079
- const H = tt(y);
1080
- return f.set(P, H), $[$.length - 1].set(d.toLowerCase(), { id: P, columns: H }), !0;
1081
- }, ot = (n, d) => {
1082
- if (!n.with) return d();
1083
- if (n.recursive) {
1084
- N("WITH RECURSIVE can't be imported", n.with[0].span);
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
- $.push(/* @__PURE__ */ new Map());
1073
+ F.push(/* @__PURE__ */ new Map());
1088
1074
  try {
1089
- return n.with.every(z) ? d() : void 0;
1075
+ return s.with.every(V) ? c() : void 0;
1090
1076
  } finally {
1091
- $.pop();
1077
+ F.pop();
1092
1078
  }
1093
- }, at = (n) => {
1094
- const d = xt(n, v) ? n : {
1095
- ...n,
1096
- ...n.groupBy && {
1097
- groupBy: n.groupBy.map(({ fromSelect: y, ...D }) => D)
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
- }, p = Ft(d, v), g = new Set((d.groupBy ?? []).map((y) => y.id));
1100
- return (p.groupBy ?? []).filter((y) => !g.has(y.id)).forEach((y) => {
1101
- const D = x.get(y.fromSelect), B = `${o.slice(D.start, D.end)} isn't in GROUP BY: it's added, as a column next to an aggregate is one value per group`;
1102
- ut(i, "groupedBy", B, D, "warning");
1103
- }), p;
1104
- }, pt = (n, d) => {
1105
- const p = d.tables, g = (E) => {
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 A, K;
1094
+ let N, K;
1109
1095
  if (E.kind === "derived") {
1110
1096
  if (!l) {
1111
- N("A sub query in FROM needs a name: (SELECT …) AS name", E.span);
1097
+ b("A sub query in FROM needs a name: (SELECT …) AS name", E.span);
1112
1098
  return;
1113
1099
  }
1114
- const R = r(E.query, d, "FROM");
1100
+ const R = u(E.query, c, "FROM");
1115
1101
  if (!R) return;
1116
- A = { id: nt(), alias: l, query: R }, K = tt(R);
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
- A = { shared: R.id, alias: l ?? E.name[0].value }, { columns: K } = R;
1120
- } else if (E.name.length === 1 && G.has(E.name[0].value.toLowerCase())) {
1121
- N("A WITH query naming itself (recursive) can't be imported", E.span);
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(L.tables.map((J) => J.name), R.value);
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
- ut(i, "unknownTable", `The schema has no table "${R.value}"`, R.span);
1112
+ it(i, "unknownTable", `The schema has no table "${R.value}"`, R.span);
1127
1113
  return;
1128
1114
  }
1129
- A = { table: Y, ...l && { alias: l } }, K = Nt(L, Y).columns.map((J) => J.name);
1115
+ N = { table: Y, ...l && { alias: l } }, K = Nt(k, Y).columns.map((tt) => tt.name);
1130
1116
  }
1131
- const W = $t(A);
1132
- if (p.some((R) => R.ref.toLowerCase() === W.toLowerCase())) {
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
- ut(i, "duplicateTable", R, E.span);
1120
+ it(i, "duplicateTable", R, E.span);
1135
1121
  return;
1136
1122
  }
1137
- return p.push({
1123
+ return f.push({
1138
1124
  ref: W,
1139
1125
  columns: K,
1140
- ...A.table !== void 0 && { table: A.table }
1141
- }), A;
1142
- }, [y, ...D] = n, B = g(y.table), P = [];
1143
- let H = B !== void 0;
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
- ...y.joins,
1146
- ...D.flatMap((E) => [
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
- N("JOIN … USING can't be imported: write ON", E.span), H = !1;
1138
+ b("JOIN … USING can't be imported: write ON", E.span), X = !1;
1153
1139
  return;
1154
1140
  }
1155
- const l = g(E.table);
1141
+ const l = y(E.table);
1156
1142
  if (!l) {
1157
- H = !1;
1143
+ X = !1;
1158
1144
  return;
1159
1145
  }
1160
- const A = E.on && I(E.on, { scope: d }), K = { joinType: E.type, ...A && { on: A } };
1161
- l.query ? P.push({
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 ? P.push({
1152
+ }) : l.shared !== void 0 ? _.push({
1167
1153
  id: nt(),
1168
1154
  shared: l.shared,
1169
1155
  alias: l.alias,
1170
1156
  ...K
1171
- }) : P.push({
1157
+ }) : _.push({
1172
1158
  id: nt(),
1173
1159
  table: l.table,
1174
1160
  ...l.alias && { alias: l.alias },
1175
1161
  ...K
1176
1162
  });
1177
- }), H ? { from: B, joins: P } : void 0;
1163
+ }), X ? { from: B, joins: _ } : void 0;
1178
1164
  };
1179
- function tt(n) {
1180
- const d = bt(n);
1181
- return d.select.length > 0 ? d.select.flatMap((p) => Mt(p) ?? []) : qt(d).flatMap((p) => p.query ? tt(p.query) : p.shared !== void 0 ? f.get(p.shared) : Nt(L, p.table).columns.map((g) => g.name));
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 t(n, d, p) {
1184
- if (n.top && N(`TOP goes only in the query itself, not in ${p}`, n.top.span, "leftOut"), n.from.length === 0) {
1185
- N("A query without FROM can't be imported", n.span);
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 g = pt(n.from, d);
1189
- if (!g) return;
1190
- const y = { scope: d }, [D] = n.columns, B = n.columns.length === 1 && D.expr.kind === "star" && !D.expr.table, P = (O) => {
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, J = (R = Y.table) == null ? void 0 : R[Y.table.length - 1], it = J ? d.tables.filter((ct) => st([ct.ref], J.value)) : d.tables;
1194
- return J && it.length === 0 && ut(i, "unknownTable", `No table here is named "${J.value}"`, J.span), it.flatMap((ct) => ct.columns.map((et) => ({
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: ct.ref, column: et }
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, y),
1187
+ expression: I(O.expr, S),
1202
1188
  ...O.alias && { alias: O.alias.value }
1203
1189
  }];
1204
- }, H = B ? [] : n.columns.flatMap((O) => {
1205
- const { value: R, failed: Y } = M(() => P(O));
1206
- return c && Y.length > 0 ? (Q(Y, "its SELECT column"), []) : (R.forEach((J) => x.set(J.id, O.span)), R);
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(H.flatMap((Y) => Y.alias ?? []), O.value);
1209
- return R === void 0 ? void 0 : H.find((Y) => Y.alias === R);
1210
- }, E = (O) => O.kind === "number" && /^\d+$/.test(O.text) ? H[Number(O.text) - 1] : void 0, l = n.where && I(n.where, y), A = n.groupBy.map((O) => {
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: d,
1213
- alias: (it) => {
1198
+ scope: c,
1199
+ alias: (at) => {
1214
1200
  var et;
1215
- return d.tables.some((mt) => st(mt.columns, it.value)) || (et = j(it)) == null ? void 0 : et.expression;
1201
+ return c.tables.some((mt) => st(mt.columns, at.value)) || (et = j(at)) == null ? void 0 : et.expression;
1216
1202
  }
1217
- }), J = H.find((it) => Rt(it.expression, Y));
1218
- return { id: nt(), expression: Y, ...J && { fromSelect: J.id } };
1219
- }), K = A.flatMap(({ expression: O }) => O.type === "field" && O.column ? [O.column.toLowerCase()] : []), W = n.having && Ot(I(n.having, {
1220
- scope: d,
1221
- ...U === "mysql" && {
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
- }), H, v);
1214
+ }), X, v);
1229
1215
  return {
1230
- from: g.from,
1231
- joins: g.joins,
1216
+ from: y.from,
1217
+ joins: y.joins,
1232
1218
  ...l && { where: l },
1233
- select: H,
1234
- ...A.length > 0 && { groupBy: A },
1219
+ select: X,
1220
+ ...N.length > 0 && { groupBy: N },
1235
1221
  ...W && { having: W },
1236
- ...n.distinct && { distinct: !0 }
1222
+ ...s.distinct && { distinct: !0 }
1237
1223
  };
1238
1224
  }
1239
- function r(n, d, p) {
1240
- return ot(n, () => u(n, d, p));
1225
+ function u(s, c, f) {
1226
+ return ot(s, () => h(s, c, f));
1241
1227
  }
1242
- function u(n, d, p) {
1243
- var H, j, E;
1244
- const g = ((H = n.orderBy[0]) == null ? void 0 : H.span) ?? ((j = n.limit) == null ? void 0 : j.span) ?? ((E = n.offset) == null ? void 0 : E.span);
1245
- if (g) {
1246
- const l = `ORDER BY, LIMIT and OFFSET go only in the query itself, not in ${p}`;
1247
- N(l, g, "leftOut");
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 y = [], D = /* @__PURE__ */ new Set(), B = (l) => {
1235
+ const S = [], L = /* @__PURE__ */ new Set(), B = (l) => {
1250
1236
  if (l.kind === "select")
1251
- return y.push(l), !0;
1237
+ return S.push(l), !0;
1252
1238
  if (l.kind === "nested") {
1253
- const A = l.query;
1254
- return A.with || A.orderBy.length > 0 || A.limit || A.offset ? (N(
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(A.body);
1243
+ ), !1) : B(N.body);
1258
1244
  }
1259
- return l.op !== "UNION" ? (N(`${l.op} can't be imported`, l.span), !1) : (D.add(l.all), B(l.left) && B(l.right));
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(n.body)) return;
1262
- if (D.size > 1) {
1263
- N("UNION and UNION ALL mixed can't be imported", n.body.span);
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 P = y.map((l) => {
1267
- const A = t(l, { tables: [], around: d }, p);
1268
- return A && at(A);
1252
+ const _ = S.map((l) => {
1253
+ const N = r(l, { tables: [], around: c }, f);
1254
+ return N && ut(N);
1269
1255
  });
1270
- if (!P.some((l) => l === void 0))
1271
- return P.length === 1 ? P[0] : { union: P, all: D.has(!0) };
1256
+ if (!_.some((l) => l === void 0))
1257
+ return _.length === 1 ? _[0] : { union: _, all: L.has(!0) };
1272
1258
  }
1273
- function h(n, d, p) {
1274
- var A, K;
1275
- const g = n.body.kind === "select" ? n.body : void 0;
1276
- if (!g || n.with || n.orderBy.length > 0 || n.limit || n.offset || g.groupBy.length > 0 || g.having || g.distinct || g.top) {
1277
- N("An EXISTS can only be SELECT … FROM … WHERE …", p);
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 y = (A = g.from[0]) == null ? void 0 : A.table;
1281
- if ((y == null ? void 0 : y.kind) === "derived") {
1282
- N("An EXISTS's first table can't be a sub query", y.span);
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 ((y == null ? void 0 : y.kind) === "table" && y.name.length === 1 && w(y.name[0].value)) {
1286
- N("An EXISTS's first table can't be a WITH query", y.span);
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 D = t({ ...g, columns: [] }, { tables: [], outer: d }, "EXISTS");
1290
- if (!D) return;
1291
- const B = (W) => W.type === "field" ? [W] : Wt(W).flatMap(B), P = ((K = D.where) == null ? void 0 : K.type) === "logical" && D.where.operator === "AND" ? D.where.operands : D.where && [D.where], H = (W) => B(W).some((O) => i.outerFields.has(O)), j = (P == null ? void 0 : P.filter(H)) ?? [], E = (P == null ? void 0 : P.filter((W) => !H(W))) ?? [];
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
- N("An EXISTS must name the query around it in its WHERE", p);
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: D.from,
1301
- joins: D.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 C = (n, d) => {
1308
- if (n) {
1309
- if (n.kind === "number" && /^\d+$/.test(n.text)) return Number(n.text);
1310
- N(`${d} can only be a whole number`, n.span, "leftOut");
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
- }, X = () => ot(a, () => F()), F = () => {
1298
+ }, $ = () => ot(a, () => Z()), Z = () => {
1313
1299
  var E;
1314
- const n = { tables: [] };
1315
- let d, p;
1300
+ const s = { tables: [] };
1301
+ let c, f;
1316
1302
  if (a.body.kind === "select") {
1317
1303
  const l = a.body;
1318
- p = l.top && C(l.top, "TOP"), d = t({ ...l, top: void 0 }, n, "the query");
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 }, A = r(l, n, "a UNION");
1321
- if (A) {
1322
- const K = L.tables.map((R) => R.name);
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
- n.tables.push({ ref: O, columns: tt(A) }), d = {
1327
- from: { id: nt(), alias: O, query: A },
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 (!d) return;
1334
- const g = ((E = a.limit) == null ? void 0 : E.kind) === "number" && a.limit.text === de ? void 0 : p ?? C(a.limit, "LIMIT"), y = C(a.offset, "OFFSET") || void 0, D = d.select, B = D.length > 0 ? D.map((l) => yt(l, v)) : n.tables.flatMap((l) => l.columns.map((A) => ({
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: A
1338
- }))), P = (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", H = (l) => {
1339
- l.nulls && N("NULLS FIRST / LAST can't be imported", l.span, "leftOut");
1340
- const { expr: A } = l, K = A.kind === "name" && A.parts.length === 1 ? st(D.flatMap((Y) => Y.alias ?? []), A.parts[0].value) : void 0, W = K !== void 0 && D.find((Y) => Y.alias === K), O = A.kind === "number" && /^\d+$/.test(A.text) ? B[Number(A.text) - 1] : void 0, R = W && yt(W, v) || O || I(A, { scope: n });
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, D, v),
1329
+ expression: Ot(R, L, v),
1344
1330
  direction: l.direction ?? "asc"
1345
1331
  };
1346
- }, j = a.orderBy.filter((l) => !P(l.expr)).flatMap((l) => {
1347
- const { value: A, failed: K } = M(() => H(l));
1348
- return c && K.length > 0 && g === void 0 && y === void 0 ? (Q(K, "its sort key"), []) : [A];
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 at({
1336
+ return ut({
1351
1337
  id: nt(),
1352
- ...k.length > 0 && { shared: k },
1353
- ...d,
1338
+ ...C.length > 0 && { shared: C },
1339
+ ...c,
1354
1340
  ...j.length > 0 && { orderBy: j },
1355
- ...g !== void 0 && { limit: g },
1356
- ...y && { offset: y }
1341
+ ...y !== void 0 && { limit: y },
1342
+ ...S && { offset: S }
1357
1343
  });
1358
- }, V = X(), q = i.problems.some((n) => n.severity === "error");
1344
+ }, x = $(), z = i.problems.some((s) => s.severity === "error");
1359
1345
  return {
1360
- ...V && (c || !q) && { query: V },
1346
+ ...x && (p || !z) && { query: x },
1361
1347
  problems: i.problems
1362
1348
  };
1363
1349
  }