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