@nongt/visql 0.3.0 → 0.4.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,68 +1,117 @@
1
- var Et = Object.defineProperty;
2
- var Tt = (t, e, r) => e in t ? Et(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
3
- var b = (t, e, r) => Tt(t, typeof e != "symbol" ? e + "" : e, r);
4
- import { B as x, q as d, F as yt, g as F, d as H, t as Q, h as pt, w as k, o as X, H as At, k as mt, b as gt, i as Dt } from "./schema-z95dxLxu.js";
5
- import { ag as D, aA as V, aB as Nt, aC as Ot, aD as ht, aj as Rt, V as St, ap as It, aE as y, al as bt, a7 as Ct, aF as Lt, aG as W, $ as Mt } from "./issueLocation-Cw5sttQL.js";
6
- import { s as J } from "./calendarDate-CYlr9TvX.js";
7
- class wt extends Error {
8
- constructor(r, n, s) {
9
- super(`scopeQuery: the query at ${r.length ? r.join(".") : "the top"} reads "${n}" but can't reach "${s}", which the condition names`);
1
+ var At = Object.defineProperty;
2
+ var mt = (t, e, r) => e in t ? At(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
3
+ var I = (t, e, r) => mt(t, typeof e != "symbol" ? e + "" : e, r);
4
+ import { q as b, F as gt, G as ht, g as w, o as Y, d as x, t as W, h as Dt, w as J, H as Nt, k as Ot, b as St, i as Rt } from "./schema-z95dxLxu.js";
5
+ import { ag as N, aA as K, aB as bt, aC as It, aD as Ct, aj as Lt, V as Mt, ap as wt, aE as p, al as Ft, a7 as qt, aF as Ut, aG as z, $ as vt } from "./issueLocation-CWUdTtCh.js";
6
+ import { s as Z } from "./calendarDate-CYlr9TvX.js";
7
+ class dt extends Error {
8
+ constructor(r, n, s, i) {
9
+ const a = i ? `"${n}" (as "${i}")` : `"${n}"`;
10
+ super(`scopeQuery: the query at ${r.length ? r.join(".") : "the top"} reads ${a} but can't reach "${s}", which the condition names`);
10
11
  /** Where the body is, as keys into the query (validateQuery's
11
12
  * `pathSegments` style). */
12
- b(this, "path");
13
+ I(this, "path");
13
14
  /** The table it reads that needs scope. */
14
- b(this, "table");
15
- this.name = "ScopeError", this.path = r, this.table = n;
15
+ I(this, "table");
16
+ /** The alias that copy of the table goes by, when it has one (a self
17
+ * join reads a table more than once, issue 216). */
18
+ I(this, "alias");
19
+ this.name = "ScopeError", this.path = r, this.table = n, i && (this.alias = i);
16
20
  }
17
21
  }
18
- const K = (t) => Object.prototype.toString.call(t) === "[object Object]", Ft = (t) => Array.isArray(t.joins) && K(t.from);
19
- function C(t, e, r) {
22
+ const P = (t) => Object.prototype.toString.call(t) === "[object Object]", Bt = (t) => Array.isArray(t.joins) && P(t.from);
23
+ function F(t, e, r) {
20
24
  if (Array.isArray(t)) {
21
- const a = t.map((o, f) => C(o, [...e, f], r));
25
+ const a = t.map((o, f) => F(o, [...e, f], r));
22
26
  return a.every((o, f) => o === t[f]) ? t : a;
23
27
  }
24
- if (!K(t)) return t;
28
+ if (!P(t)) return t;
25
29
  let n = !1;
26
30
  const s = Object.entries(t).map(([a, o]) => {
27
- const f = C(o, [...e, a], r);
31
+ const f = F(o, [...e, a], r);
28
32
  return f !== o && (n = !0), [a, f];
29
33
  }), i = n ? Object.fromEntries(s) : t;
30
- return Ft(i) ? r(i, e) : i;
31
- }
32
- const z = (t, e) => x(t).find(e.requires);
33
- function Z(t, e) {
34
- return t ? F(t, e) ? !0 : t.type === "logical" && t.operator === "AND" && t.operands.some((r) => F(r, e)) : !1;
35
- }
36
- function j(t, e, r, n) {
37
- const s = z(e, r);
38
- if (!s || Z(e.where, r.condition)) return e;
39
- const i = e, a = x(i), o = d(i).flatMap(($) => $.alias ? [$.alias] : []), f = V(r.condition).find(($) => !o.includes($) && yt(t, a, $) === void 0);
40
- if (f) throw new wt(n, s, f);
41
- const { joins: c } = Nt(
42
- t,
43
- {
44
- id: "",
45
- from: e.from,
46
- joins: e.joins,
47
- select: []
48
- },
49
- r.condition
50
- ), { where: u } = e;
51
- let l = r.condition;
52
- return (u == null ? void 0 : u.type) === "logical" && u.operator === "AND" ? l = { ...u, operands: [...u.operands, r.condition] } : u && (l = { type: "logical", operator: "AND", operands: [u, r.condition] }), { ...e, joins: c, where: l };
34
+ return Bt(i) ? r(i, e) : i;
53
35
  }
54
- function le(t, e, r) {
55
- const n = j(t, e, r, []), s = D(t, e, n), i = C(s, [], (a, o) => o.length === 0 ? a : j(t, a, r, o));
56
- return D(t, s, i);
36
+ const tt = (t, e) => b(t).flatMap((r) => r.table !== void 0 && e.requires(r.table) ? [{ table: r.table, ...r.alias ? { alias: r.alias } : {}, ref: r.alias ?? r.table }] : []), jt = (t) => b(t).flatMap((e) => {
37
+ const r = e.alias ?? e.table;
38
+ return r === void 0 ? [] : [r];
39
+ });
40
+ function d(t, e) {
41
+ if (t.type === "field") {
42
+ const r = t.table && e.get(t.table);
43
+ return r ? { ...t, table: r } : t;
44
+ }
45
+ return Y(t, (r) => d(r, e));
46
+ }
47
+ const et = (t) => t ? t.type === "logical" && t.operator === "AND" ? t.operands : [t] : [];
48
+ function _t(t, e) {
49
+ if (e.type !== "binary" || !e.left || !e.right) return !1;
50
+ const r = { ...e, left: e.right, right: e.left };
51
+ return t.joins.some((n) => et(n.on).some((s) => w(s, e) || w(s, r)));
52
+ }
53
+ function rt(t, e, r, n) {
54
+ const s = /* @__PURE__ */ new Map([[r.table, r.ref]]), i = [], a = new Set(jt(e)), o = b(e).flatMap((u) => u.alias ? [u.alias] : []);
55
+ let f;
56
+ return K(n.condition).forEach((u) => {
57
+ if (f || s.has(u) || o.includes(u)) return;
58
+ const c = gt(t, [...s.keys()], u);
59
+ if (!c) {
60
+ f = u;
61
+ return;
62
+ }
63
+ c.forEach((l) => {
64
+ const $ = ht(t, [...s.keys()], l);
65
+ if (!$) return;
66
+ const T = (m) => d($.on, new Map([...s, [l, m]])), A = b(e).flatMap((m) => m.table === l ? [m.alias ?? l] : []).find((m) => _t(e, T(m)));
67
+ if (A) {
68
+ s.set(l, A);
69
+ return;
70
+ }
71
+ let h = l;
72
+ for (let m = 2; a.has(h); m += 1) h = `${l}_${m}`;
73
+ a.add(h), s.set(l, h), i.push({
74
+ id: bt(),
75
+ table: l,
76
+ ...h !== l ? { alias: h } : {},
77
+ joinType: $.joinType,
78
+ on: T(h)
79
+ });
80
+ });
81
+ }), { joins: i, condition: d(n.condition, s), ...f ? { missing: f } : {} };
82
+ }
83
+ function nt(t, e) {
84
+ return et(t).some((r) => w(r, e));
85
+ }
86
+ function Ht(t, e) {
87
+ return (t == null ? void 0 : t.type) === "logical" && t.operator === "AND" ? { ...t, operands: [...t.operands, e] } : t ? { type: "logical", operator: "AND", operands: [t, e] } : e;
88
+ }
89
+ function Q(t, e, r, n) {
90
+ return tt(e, r).reduce((s, i) => {
91
+ const a = rt(t, s, i, r);
92
+ if (a.missing) throw new dt(n, i.table, a.missing, i.alias);
93
+ const o = nt(s.where, a.condition);
94
+ return a.joins.length === 0 && o ? s : {
95
+ ...s,
96
+ joins: [...s.joins, ...a.joins],
97
+ where: o ? s.where : Ht(s.where, a.condition)
98
+ };
99
+ }, e);
100
+ }
101
+ function ge(t, e, r) {
102
+ const n = Q(t, e, r, []), s = N(t, e, n), i = F(s, [], (a, o) => o.length === 0 ? a : Q(t, a, r, o));
103
+ return N(t, s, i);
57
104
  }
58
- function $e(t, e) {
59
- const r = [];
60
- return C(t, [], (n, s) => {
61
- const i = z(n, e);
62
- return i && !Z(n.where, e.condition) && r.push({ path: s, table: i }), n;
63
- }), r;
105
+ function he(t, e, r) {
106
+ if (!P(t) || !Array.isArray(t.tables))
107
+ throw new TypeError("unscopedBodies takes (schema, query, rule) since 0.4.0");
108
+ const n = [];
109
+ return F(e, [], (s, i) => (tt(s, r).forEach((a) => {
110
+ const o = rt(t, s, a, r);
111
+ (o.missing || o.joins.length > 0 || !nt(s.where, o.condition)) && n.push({ path: i, table: a.table, ...a.alias ? { alias: a.alias } : {} });
112
+ }), s)), n;
64
113
  }
65
- const qt = (t) => typeof t == "object" && t !== null && !(t instanceof Date), Y = {
114
+ const Yt = (t) => typeof t == "object" && t !== null && !(t instanceof Date), k = {
66
115
  /** A column: `expr.field('orders', 'status')`. */
67
116
  field: (t, e) => ({ type: "field", table: t, column: e }),
68
117
  /** A literal value, written into the SQL (as a bound parameter by
@@ -129,7 +178,7 @@ const qt = (t) => typeof t == "object" && t !== null && !(t instanceof Date), Y
129
178
  type: "binary",
130
179
  operator: "IN",
131
180
  left: t,
132
- right: Array.isArray(e) ? Y.list(...e) : e
181
+ right: Array.isArray(e) ? k.list(...e) : e
133
182
  }),
134
183
  /** `left BETWEEN low AND high` (issue 169), both bounds included: each a
135
184
  * value or an expression (a param, a relative date). NOT BETWEEN:
@@ -138,13 +187,13 @@ const qt = (t) => typeof t == "object" && t !== null && !(t instanceof Date), Y
138
187
  type: "binary",
139
188
  operator: "BETWEEN",
140
189
  left: t,
141
- right: Y.list(e, r)
190
+ right: k.list(e, r)
142
191
  }),
143
192
  /** IN's values: each a value (`expr.list(1, 2)`) or an expression
144
193
  * (`expr.list(expr.param('a'), expr.param('b'))`). */
145
194
  list: (...t) => ({
146
195
  type: "list",
147
- items: t.map((e) => qt(e) ? e : { type: "literal", value: e })
196
+ items: t.map((e) => Yt(e) ? e : { type: "literal", value: e })
148
197
  }),
149
198
  /** A query of its own for IN to compare with, selecting `select` — one
150
199
  * column. Its tables are its own: it sees nothing of the query around
@@ -153,91 +202,91 @@ const qt = (t) => typeof t == "object" && t !== null && !(t instanceof Date), Y
153
202
  * 'customer_id') }))`. */
154
203
  subquery: ({ select: t, ...e }) => ({
155
204
  type: "subquery",
156
- id: H(),
157
- query: { joins: [], ...e, select: [{ id: H(), expression: t }] }
205
+ id: x(),
206
+ query: { joins: [], ...e, select: [{ id: x(), expression: t }] }
158
207
  })
159
208
  };
160
- function Ee(t, e, r) {
161
- return y(t, e, { type: "setBaseTable", table: r });
209
+ function De(t, e, r) {
210
+ return p(t, e, { type: "setBaseTable", table: r });
162
211
  }
163
- function Te(t, e, r, n = {}) {
212
+ function Ne(t, e, r, n = {}) {
164
213
  const s = { type: "addJoin", table: r, ...n };
165
- return y(t, e, s);
214
+ return p(t, e, s);
166
215
  }
167
- function ye(t, e, r) {
168
- return y(t, e, { type: "removeJoin", joinId: r });
216
+ function Oe(t, e, r) {
217
+ return p(t, e, { type: "removeJoin", joinId: r });
169
218
  }
170
- function Ut(t, e, r, n = {}) {
171
- const s = Ot(e), i = V(r);
172
- if (s && i.some((c) => c !== s.alias)) {
219
+ function Pt(t, e, r, n = {}) {
220
+ const s = It(e), i = K(r);
221
+ if (s && i.some((u) => u !== s.alias)) {
173
222
  if (i.includes(s.alias))
174
223
  throw new Error("addCondition: a condition names both the union and tables of its branches: add it in two parts");
175
- return s.query.union.forEach((c) => {
176
- const u = c.from.table, l = new Set(d(c).map(Q)), $ = ht(t, u), p = i.find((A) => !l.has(A) && !$.has(A));
177
- if (p)
178
- throw new Error(`addCondition: "${p}" can't be reached from "${u}", one of the union's tables: a condition must reach every branch`);
179
- }), s.query.union.reduce((c, u, l) => Rt(
180
- c,
181
- [St(s.id, l)],
182
- ($) => Ut(t, $, r, n)
224
+ return s.query.union.forEach((u) => {
225
+ const c = u.from.table, l = new Set(b(u).map(W)), $ = Ct(t, c), T = i.find((A) => !l.has(A) && !$.has(A));
226
+ if (T)
227
+ throw new Error(`addCondition: "${T}" can't be reached from "${c}", one of the union's tables: a condition must reach every branch`);
228
+ }), s.query.union.reduce((u, c, l) => Lt(
229
+ u,
230
+ [Mt(s.id, l)],
231
+ ($) => Pt(t, $, r, n)
183
232
  ), e);
184
233
  }
185
234
  const a = n.operator ?? "AND", { where: o } = e;
186
235
  let f;
187
- return o ? o.type === "logical" && o.operator === a ? f = { ...o, operands: [...o.operands, r] } : f = { type: "logical", operator: a, operands: [o, r] } : f = r, D(t, e, It(t, e, f));
236
+ return o ? o.type === "logical" && o.operator === a ? f = { ...o, operands: [...o.operands, r] } : f = { type: "logical", operator: a, operands: [o, r] } : f = r, N(t, e, wt(t, e, f));
188
237
  }
189
- function pe(t, e, r, n) {
190
- const s = y(t, e, { type: "addSelectItem" }), i = s.select[s.select.length - 1].id, a = y(t, s, {
238
+ function Se(t, e, r, n) {
239
+ const s = p(t, e, { type: "addSelectItem" }), i = s.select[s.select.length - 1].id, a = p(t, s, {
191
240
  type: "setSelectExpression",
192
241
  selectId: i,
193
242
  expression: r
194
- }), o = n === void 0 ? a : y(t, a, { type: "setSelectAlias", selectId: i, alias: n });
195
- return D(t, e, o);
243
+ }), o = n === void 0 ? a : p(t, a, { type: "setSelectAlias", selectId: i, alias: n });
244
+ return N(t, e, o);
196
245
  }
197
- function Ae(t, e, r) {
198
- const n = y(t, e, { type: "addGroupByItem" }), { id: s } = n.groupBy[n.groupBy.length - 1], i = y(t, n, {
246
+ function Re(t, e, r) {
247
+ const n = p(t, e, { type: "addGroupByItem" }), { id: s } = n.groupBy[n.groupBy.length - 1], i = p(t, n, {
199
248
  type: "setGroupByExpression",
200
249
  groupById: s,
201
250
  expression: r
202
251
  });
203
- return D(t, e, i);
252
+ return N(t, e, i);
204
253
  }
205
- function me(t, e, r) {
206
- return D(t, e, bt(t, e, r));
254
+ function be(t, e, r) {
255
+ return N(t, e, Ft(t, e, r));
207
256
  }
208
- function ge(t, e, r, n = "asc") {
209
- const s = y(t, e, { type: "addOrderByItem" }), { id: i } = s.orderBy[s.orderBy.length - 1], a = y(t, s, {
257
+ function Ie(t, e, r, n = "asc") {
258
+ const s = p(t, e, { type: "addOrderByItem" }), { id: i } = s.orderBy[s.orderBy.length - 1], a = p(t, s, {
210
259
  type: "setOrderByExpression",
211
260
  orderById: i,
212
261
  expression: r
213
- }), o = y(t, a, {
262
+ }), o = p(t, a, {
214
263
  type: "setOrderByDirection",
215
264
  orderById: i,
216
265
  direction: n
217
266
  });
218
- return D(t, e, o);
267
+ return N(t, e, o);
219
268
  }
220
- function De(t, e, r) {
269
+ function Ce(t, e, r) {
221
270
  let n = e;
222
- return "limit" in r && (n = y(t, n, { type: "setLimit", limit: r.limit })), "offset" in r && (n = y(t, n, { type: "setOffset", offset: r.offset })), n;
271
+ return "limit" in r && (n = p(t, n, { type: "setLimit", limit: r.limit })), "offset" in r && (n = p(t, n, { type: "setOffset", offset: r.offset })), n;
223
272
  }
224
- function Ne(t, e, r) {
225
- return y(t, e, { type: "setDistinct", distinct: r });
273
+ function Le(t, e, r) {
274
+ return p(t, e, { type: "setDistinct", distinct: r });
226
275
  }
227
- function Oe(t) {
228
- if (!pt(t)) throw new Error(`query "${t.id}" has no base table yet`);
276
+ function Me(t) {
277
+ if (!Dt(t)) throw new Error(`query "${t.id}" has no base table yet`);
229
278
  return t;
230
279
  }
231
- const q = 4;
232
- class h extends Error {
280
+ const B = 4;
281
+ class R extends Error {
233
282
  constructor(r, n) {
234
283
  super(n);
235
- b(this, "kind");
284
+ I(this, "kind");
236
285
  this.name = "QueryFormatError", this.kind = r;
237
286
  }
238
287
  }
239
- const I = (t) => typeof t == "object" && t !== null && !Array.isArray(t);
240
- function vt(t) {
288
+ const M = (t) => typeof t == "object" && t !== null && !Array.isArray(t);
289
+ function Gt(t) {
241
290
  const e = [];
242
291
  let r = 0;
243
292
  for (; r < t.length; ) {
@@ -256,79 +305,83 @@ function vt(t) {
256
305
  }
257
306
  return e;
258
307
  }
259
- function Bt(t) {
308
+ function xt(t) {
260
309
  const e = (r) => {
261
310
  if (Array.isArray(r)) return r.map(e);
262
- if (!I(r)) return r;
311
+ if (!M(r)) return r;
263
312
  const n = Object.fromEntries(Object.entries(r).map(([a, o]) => [a, e(o)])), { right: s } = n;
264
- if (n.type !== "binary" || n.operator !== "IN" || !I(s) || s.type === "subquery" || s.type === "list") return n;
265
- const i = s.type === "literal" && typeof s.value == "string" ? vt(s.value).map((a) => ({ type: "literal", value: a })) : [s];
313
+ if (n.type !== "binary" || n.operator !== "IN" || !M(s) || s.type === "subquery" || s.type === "list") return n;
314
+ const i = s.type === "literal" && typeof s.value == "string" ? Gt(s.value).map((a) => ({ type: "literal", value: a })) : [s];
266
315
  return { ...n, right: { type: "list", items: i } };
267
316
  };
268
317
  return e(t);
269
318
  }
270
- const dt = (t) => t, _t = (t) => t, Ht = [Bt, dt, _t];
271
- function jt(t, e, r) {
319
+ const Qt = (t) => t, kt = (t) => t, Xt = [xt, Qt, kt];
320
+ function Vt(t, e, r) {
272
321
  return r.slice(e - 1).reduce((n, s) => s(n), t);
273
322
  }
274
- function L(t, e) {
275
- return Array.isArray(t) ? t.map((r) => L(r, e)) : I(t) ? t.type === "literal" ? e(t) : Object.fromEntries(Object.entries(t).map(([r, n]) => [r, L(n, e)])) : t;
323
+ function q(t, e) {
324
+ return Array.isArray(t) ? t.map((r) => q(r, e)) : M(t) ? t.type === "literal" ? e(t) : Object.fromEntries(Object.entries(t).map(([r, n]) => [r, q(n, e)])) : t;
276
325
  }
277
- function Yt(t) {
326
+ function Wt(t) {
278
327
  return t.value instanceof Date ? { ...t, value: t.value.toISOString(), valueType: "date" } : typeof t.value == "bigint" ? { ...t, value: String(t.value), valueType: "bigint" } : t;
279
328
  }
280
- function Pt(t) {
329
+ function Jt(t) {
281
330
  if (t.valueType === void 0) return t;
282
331
  const { valueType: e, ...r } = t, n = String(r.value);
283
332
  let s;
284
333
  if (e === "date") {
285
334
  if (s = new Date(n), Number.isNaN(s.getTime()))
286
- throw new h("invalid", `not a date: ${JSON.stringify(n)}`);
335
+ throw new R("invalid", `not a date: ${JSON.stringify(n)}`);
287
336
  } else if (e === "bigint" && /^-?\d+$/.test(n))
288
337
  s = BigInt(n);
289
338
  else {
290
339
  const i = String(e);
291
- throw new h("invalid", `can't read a ${i} from ${JSON.stringify(n)}`);
340
+ throw new R("invalid", `can't read a ${i} from ${JSON.stringify(n)}`);
292
341
  }
293
342
  return { ...r, value: s };
294
343
  }
295
- function he(t) {
344
+ function we(t) {
296
345
  return {
297
- version: q,
298
- query: L(t, Yt)
346
+ version: B,
347
+ query: q(t, Wt)
299
348
  };
300
349
  }
301
- function Re(t) {
302
- if (!I(t))
303
- throw new h("invalid", "a stored query is an object");
350
+ function Fe(t) {
351
+ if (!M(t))
352
+ throw new R("invalid", "a stored query is an object");
304
353
  const { version: e } = t;
305
354
  if (typeof e != "number" || !Number.isInteger(e) || e < 1)
306
- throw new h("invalid", "a stored query has a whole-number version from 1");
307
- if (e > q)
308
- throw new h(
355
+ throw new R("invalid", "a stored query has a whole-number version from 1");
356
+ if (e > B)
357
+ throw new R(
309
358
  "newerVersion",
310
- `stored in format ${e}; this version reads up to ${q}`
359
+ `stored in format ${e}; this version reads up to ${B}`
311
360
  );
312
- const r = jt(t.query, e, Ht);
313
- if (!I(r) || typeof r.id != "string")
314
- throw new h("invalid", "a stored query has a query with a string id");
315
- return L(r, Pt);
361
+ const r = Vt(t.query, e, Xt);
362
+ if (!M(r) || typeof r.id != "string")
363
+ throw new R("invalid", "a stored query has a query with a string id");
364
+ return q(r, Jt);
316
365
  }
317
- function U(t, e) {
366
+ function j(t, e) {
318
367
  const r = new Map(e == null ? void 0 : e.tableOf), n = new Set(e == null ? void 0 : e.aliases), s = new Set(e == null ? void 0 : e.derived);
319
368
  return t.forEach(({ table: i, alias: a }) => {
320
369
  if (i === void 0) {
321
370
  a && s.add(a);
322
371
  return;
323
372
  }
324
- r.set(i, i), n.delete(i), s.delete(i), a && (r.set(a, i), n.add(a), s.delete(a));
373
+ if (r.set(i, i), a) {
374
+ r.set(a, i), n.add(a), s.delete(a);
375
+ return;
376
+ }
377
+ n.delete(i), s.delete(i);
325
378
  }), { tableOf: r, aliases: n, derived: s };
326
379
  }
327
- function Gt(t, e, r) {
328
- const n = t.on && S(t.on, e, r);
329
- return t.query ? { ...t, on: n, query: At(t.query, (s) => M(s, r)) } : t.shared !== void 0 ? { ...t, on: n } : { ...t, on: n, table: r(t.table) };
380
+ function Kt(t, e, r) {
381
+ const n = t.on && L(t.on, e, r);
382
+ return t.query ? { ...t, on: n, query: Nt(t.query, (s) => U(s, r)) } : t.shared !== void 0 ? { ...t, on: n } : { ...t, on: n, table: r(t.table) };
330
383
  }
331
- function S(t, e, r) {
384
+ function L(t, e, r) {
332
385
  if (t.type === "field") {
333
386
  if (!t.table || e.derived.has(t.table)) return t;
334
387
  const n = e.tableOf.get(t.table) ?? t.table;
@@ -338,27 +391,27 @@ function S(t, e, r) {
338
391
  ...t.column ? { column: r(n, t.column) } : {}
339
392
  };
340
393
  }
341
- if (t.type === "subquery") return { ...t, query: M(t.query, r) };
394
+ if (t.type === "subquery") return { ...t, query: U(t.query, r) };
342
395
  if (t.type === "exists") {
343
- const { query: n } = t, s = [n.from, ...n.joins], i = U(s), a = U(s, e);
396
+ const { query: n } = t, s = [n.from, ...n.joins], i = j(s), a = j(s, e);
344
397
  return {
345
398
  ...t,
346
- link: S(t.link, a, r),
399
+ link: L(t.link, a, r),
347
400
  query: {
348
401
  ...n,
349
402
  from: { ...n.from, table: r(n.from.table) },
350
- joins: n.joins.map((o) => Gt(o, i, r)),
351
- ...n.where ? { where: S(n.where, i, r) } : {}
403
+ joins: n.joins.map((o) => Kt(o, i, r)),
404
+ ...n.where ? { where: L(n.where, i, r) } : {}
352
405
  }
353
406
  };
354
407
  }
355
- return X(t, (n) => S(n, e, r));
408
+ return Y(t, (n) => L(n, e, r));
356
409
  }
357
- function M(t, e) {
358
- const r = U([t.from, ...t.joins]), n = k(
410
+ function U(t, e) {
411
+ const r = j([t.from, ...t.joins]), n = J(
359
412
  t,
360
- (s) => S(s, r, e),
361
- (s) => M(s, e)
413
+ (s) => L(s, r, e),
414
+ (s) => U(s, e)
362
415
  );
363
416
  return {
364
417
  ...n,
@@ -366,11 +419,11 @@ function M(t, e) {
366
419
  joins: n.joins.map((s) => s.table === void 0 ? s : { ...s, table: e(s.table) })
367
420
  };
368
421
  }
369
- function v(t, e) {
370
- if (Array.isArray(t)) return t.map((s) => v(s, e));
422
+ function _(t, e) {
423
+ if (Array.isArray(t)) return t.map((s) => _(s, e));
371
424
  if (typeof t != "object" || t === null || t instanceof Date) return t;
372
425
  const r = Object.fromEntries(Object.entries(t).map(
373
- ([s, i]) => [s, v(i, e)]
426
+ ([s, i]) => [s, _(i, e)]
374
427
  ));
375
428
  if (!("from" in r) || !Array.isArray(r.joins)) return r;
376
429
  const n = (s) => s.table !== void 0 && !s.alias && e(s.table) !== s.table ? { ...s, alias: s.table } : s;
@@ -380,70 +433,70 @@ function v(t, e) {
380
433
  joins: r.joins.map(n)
381
434
  };
382
435
  }
383
- function xt(t, e) {
384
- return M(v(t, e), e);
436
+ function zt(t, e) {
437
+ return U(_(t, e), e);
385
438
  }
386
- const Qt = /^(\d{2}):(\d{2})(?::(\d{2}))?$/, P = /YYYY|MM|DD|HH|mm|ss/g, O = (t, e = 2) => String(t).padStart(e, "0");
387
- function tt(t, e) {
439
+ const Zt = /^(\d{2}):(\d{2})(?::(\d{2}))?$/, X = /YYYY|MM|DD|HH|mm|ss/g, S = (t, e = 2) => String(t).padStart(e, "0");
440
+ function st(t, e) {
388
441
  let r;
389
- const n = typeof t == "string" ? Qt.exec(t.trim()) : null;
442
+ const n = typeof t == "string" ? Zt.exec(t.trim()) : null;
390
443
  if (n)
391
444
  r = { HH: n[1], mm: n[2], ss: n[3] ?? "00" };
392
445
  else {
393
- const a = typeof t == "string" ? Ct(t, "dateTime") : t;
446
+ const a = typeof t == "string" ? qt(t, "dateTime") : t;
394
447
  a instanceof Date && (r = {
395
- YYYY: O(a.getUTCFullYear(), 4),
396
- MM: O(a.getUTCMonth() + 1),
397
- DD: O(a.getUTCDate()),
398
- HH: O(a.getUTCHours()),
399
- mm: O(a.getUTCMinutes()),
400
- ss: O(a.getUTCSeconds())
448
+ YYYY: S(a.getUTCFullYear(), 4),
449
+ MM: S(a.getUTCMonth() + 1),
450
+ DD: S(a.getUTCDate()),
451
+ HH: S(a.getUTCHours()),
452
+ mm: S(a.getUTCMinutes()),
453
+ ss: S(a.getUTCSeconds())
401
454
  });
402
455
  }
403
456
  if (!r) return t;
404
457
  const s = r;
405
- return (e.match(P) ?? []).some((a) => s[a] === void 0) ? t : e.replace(P, (a) => s[a]);
458
+ return (e.match(X) ?? []).some((a) => s[a] === void 0) ? t : e.replace(X, (a) => s[a]);
406
459
  }
407
- function et(t, e) {
460
+ function it(t, e) {
408
461
  const r = /* @__PURE__ */ new Map(), n = (s) => {
409
462
  const i = /* @__PURE__ */ new Map();
410
- d(s).forEach((c) => i.set(Q(c), c.table));
411
- const a = (c) => {
412
- if ((c == null ? void 0 : c.type) !== "field" || !c.table || !c.column) return;
413
- const u = i.has(c.table) ? i.get(c.table) : c.table, l = u === void 0 ? void 0 : mt(e, u), $ = l == null ? void 0 : l.columns.find((p) => p.name === c.column);
463
+ b(s).forEach((u) => i.set(W(u), u.table));
464
+ const a = (u) => {
465
+ if ((u == null ? void 0 : u.type) !== "field" || !u.table || !u.column) return;
466
+ const c = i.has(u.table) ? i.get(u.table) : u.table, l = c === void 0 ? void 0 : Ot(e, c), $ = l == null ? void 0 : l.columns.find((T) => T.name === u.column);
414
467
  return $ == null ? void 0 : $.format;
415
- }, o = (c, u) => {
416
- switch (c.type) {
468
+ }, o = (u, c) => {
469
+ switch (u.type) {
417
470
  case "literal":
418
- return { ...c, value: tt(c.value, u) };
471
+ return { ...u, value: st(u.value, c) };
419
472
  case "list":
420
- return { ...c, items: c.items.map((l) => o(l, u)) };
473
+ return { ...u, items: u.items.map((l) => o(l, c)) };
421
474
  case "param":
422
- return r.has(c.name) || r.set(c.name, u), c;
475
+ return r.has(u.name) || r.set(u.name, c), u;
423
476
  default:
424
- return c;
477
+ return u;
425
478
  }
426
- }, f = (c) => {
427
- if (c.type === "subquery") return { ...c, query: n(c.query) };
428
- const u = X(c, f);
429
- if (u.type !== "binary" || !u.right) return u;
430
- const l = a(u.left), $ = a(u.right);
479
+ }, f = (u) => {
480
+ if (u.type === "subquery") return { ...u, query: n(u.query) };
481
+ const c = Y(u, f);
482
+ if (c.type !== "binary" || !c.right) return c;
483
+ const l = a(c.left), $ = a(c.right);
431
484
  return {
432
- ...u,
433
- left: $ ? o(u.left, $) : u.left,
434
- right: l ? o(u.right, l) : u.right
485
+ ...c,
486
+ left: $ ? o(c.left, $) : c.left,
487
+ right: l ? o(c.right, l) : c.right
435
488
  };
436
489
  };
437
- return k(s, f, (c) => n(c));
490
+ return J(s, f, (u) => n(u));
438
491
  };
439
492
  return { query: n(t), params: r };
440
493
  }
441
- const Se = ["mysql", "mssql", "postgres"], Ie = {
494
+ const qe = ["mysql", "mssql", "postgres"], Ue = {
442
495
  mysql: "MySQL",
443
496
  mssql: "SQL Server",
444
497
  postgres: "PostgreSQL"
445
498
  };
446
- function m(t, e) {
499
+ function g(t, e) {
447
500
  const r = t.replace(/[\uE000-\uE003]/g, "");
448
501
  switch (e) {
449
502
  case "mysql":
@@ -456,27 +509,27 @@ function m(t, e) {
456
509
  return `"${r.replace(/"/g, '""')}"`;
457
510
  }
458
511
  }
459
- const kt = /^[A-Za-z_][A-Za-z0-9_]*$/, rt = (t) => `${t}`, Xt = /\uE000(\d+)\uE000/g;
460
- let N;
461
- function be(t, e) {
462
- N = { ...t, writing: [] };
512
+ const te = /^[A-Za-z_][A-Za-z0-9_]*$/, ot = (t) => `${t}`, ee = /\uE000(\d+)\uE000/g;
513
+ let O;
514
+ function ve(t, e) {
515
+ O = { ...t, writing: [] };
463
516
  try {
464
517
  return e();
465
518
  } finally {
466
- N = void 0;
519
+ O = void 0;
467
520
  }
468
521
  }
469
- function nt(t, e, r) {
522
+ function at(t, e, r) {
470
523
  if (typeof t != "object" || t === null || t instanceof Date) return;
471
524
  let n;
472
525
  return Object.entries(t).some(([s, i]) => {
473
526
  const a = [...r, Array.isArray(t) ? Number(s) : s];
474
- return n = i === e ? a : nt(i, e, a), n !== void 0;
527
+ return n = i === e ? a : at(i, e, a), n !== void 0;
475
528
  }), n;
476
529
  }
477
- function T(t, e) {
478
- if (!N) return e();
479
- const { writing: r, paths: n } = N, s = r[r.length - 1], i = typeof t == "string" ? [t] : nt(s.value, t, s.path);
530
+ function y(t, e) {
531
+ if (!O) return e();
532
+ const { writing: r, paths: n } = O, s = r[r.length - 1], i = typeof t == "string" ? [t] : at(s.value, t, s.path);
480
533
  if (!i) return e();
481
534
  r.push({ value: t, path: i });
482
535
  try {
@@ -485,7 +538,7 @@ function T(t, e) {
485
538
  r.pop();
486
539
  }
487
540
  }
488
- const Vt = {
541
+ const re = {
489
542
  mysql: {
490
543
  text: "CHAR",
491
544
  integer: "SIGNED",
@@ -514,24 +567,24 @@ const Vt = {
514
567
  boolean: "boolean"
515
568
  }
516
569
  };
517
- function Wt(t, e) {
570
+ function ne(t, e) {
518
571
  var n;
519
- const r = (n = Vt[e]) == null ? void 0 : n[t];
572
+ const r = (n = re[e]) == null ? void 0 : n[t];
520
573
  return r ? e === "postgres" ? `NULL::${r}` : `CAST(NULL AS ${r})` : "NULL";
521
574
  }
522
- function st(t, e, r) {
575
+ function ut(t, e, r) {
523
576
  if (t == null) return "NULL";
524
577
  if (r)
525
- return r.values.push(t), rt(r.values.length - 1);
578
+ return r.values.push(t), ot(r.values.length - 1);
526
579
  if (typeof t == "boolean")
527
580
  return e === "mssql" ? t ? "1" : "0" : t ? "TRUE" : "FALSE";
528
581
  if (typeof t == "string") {
529
- const n = N ? t.replace(/[\uE001-\uE003]/g, "") : t;
582
+ const n = O ? t.replace(/[\uE001-\uE003]/g, "") : t;
530
583
  return n === "" ? "''" : `'${n.replace(/'/g, "''")}'`;
531
584
  }
532
- return typeof t == "number" || typeof t == "bigint" ? String(t) : t instanceof Date ? `'${J(t)}'` : String(t);
585
+ return typeof t == "number" || typeof t == "bigint" ? String(t) : t instanceof Date ? `'${Z(t)}'` : String(t);
533
586
  }
534
- function Jt(t, e) {
587
+ function se(t, e) {
535
588
  switch (t) {
536
589
  case "!=":
537
590
  return e === "mssql" ? "<>" : "!=";
@@ -543,10 +596,10 @@ function Jt(t, e) {
543
596
  return t;
544
597
  }
545
598
  }
546
- function g(t) {
599
+ function D(t) {
547
600
  return (t == null ? void 0 : t.type) === "literal" && typeof t.value == "string" && /^[A-Za-z_]+$/.test(t.value) ? t.value.toUpperCase() : "DAY";
548
601
  }
549
- const w = {
602
+ const v = {
550
603
  YEAR: "year",
551
604
  QUARTER: "quarter",
552
605
  MONTH: "month",
@@ -556,43 +609,43 @@ const w = {
556
609
  MINUTE: "minute",
557
610
  SECOND: "second",
558
611
  MILLISECOND: "millisecond"
559
- }, Kt = {
612
+ }, ie = {
560
613
  // ---- Date arithmetic ----
561
614
  DATE_ADD: ([t, e], r, n, s) => {
562
615
  var a;
563
- const i = g(r[2]);
616
+ const i = D(r[2]);
564
617
  switch (n) {
565
618
  case "mysql":
566
619
  return i === "MILLISECOND" ? `DATE_ADD(${t}, INTERVAL (${e}) * 1000 MICROSECOND)` : `DATE_ADD(${t}, INTERVAL ${e} ${i})`;
567
620
  case "postgres": {
568
- const o = w[i] ?? i.toLowerCase();
621
+ const o = v[i] ?? i.toLowerCase();
569
622
  return !s && ((a = r[1]) == null ? void 0 : a.type) === "literal" && typeof r[1].value == "number" ? `(${t} + INTERVAL '${r[1].value} ${o}')` : `(${t} + (${e} * INTERVAL '1 ${o}'))`;
570
623
  }
571
624
  case "mssql":
572
625
  return `DATEADD(${i}, ${e}, ${t})`;
573
626
  default:
574
- return `DATE_ADD(${t}, ${e}, ${g(r[2])})`;
627
+ return `DATE_ADD(${t}, ${e}, ${D(r[2])})`;
575
628
  }
576
629
  },
577
630
  DATE_SUB: ([t, e], r, n, s) => {
578
631
  var a;
579
- const i = g(r[2]);
632
+ const i = D(r[2]);
580
633
  switch (n) {
581
634
  case "mysql":
582
635
  return i === "MILLISECOND" ? `DATE_SUB(${t}, INTERVAL (${e}) * 1000 MICROSECOND)` : `DATE_SUB(${t}, INTERVAL ${e} ${i})`;
583
636
  case "postgres": {
584
- const o = w[i] ?? i.toLowerCase();
637
+ const o = v[i] ?? i.toLowerCase();
585
638
  return !s && ((a = r[1]) == null ? void 0 : a.type) === "literal" && typeof r[1].value == "number" ? `(${t} - INTERVAL '${r[1].value} ${o}')` : `(${t} - (${e} * INTERVAL '1 ${o}'))`;
586
639
  }
587
640
  case "mssql":
588
641
  return `DATEADD(${i}, -(${e}), ${t})`;
589
642
  default:
590
- return `DATE_SUB(${t}, ${e}, ${g(r[2])})`;
643
+ return `DATE_SUB(${t}, ${e}, ${D(r[2])})`;
591
644
  }
592
645
  },
593
646
  // ---- Date diff ----
594
647
  DATEDIFF: ([t, e], r, n) => {
595
- const s = g(r[2]);
648
+ const s = D(r[2]);
596
649
  switch (n) {
597
650
  case "mysql":
598
651
  return `TIMESTAMPDIFF(${s}, ${t}, ${e})`;
@@ -631,7 +684,7 @@ const w = {
631
684
  },
632
685
  // ---- Date extract ----
633
686
  EXTRACT: ([t], e, r) => {
634
- const n = g(e[1]);
687
+ const n = D(e[1]);
635
688
  switch (r) {
636
689
  case "mysql":
637
690
  return n === "MILLISECOND" ? `FLOOR(MICROSECOND(${t}) / 1000)` : `EXTRACT(${n} FROM ${t})`;
@@ -645,7 +698,7 @@ const w = {
645
698
  },
646
699
  // ---- Date truncation ----
647
700
  DATE_TRUNC: ([t], e, r) => {
648
- const n = g(e[1]);
701
+ const n = D(e[1]);
649
702
  switch (r) {
650
703
  case "mysql":
651
704
  switch (n) {
@@ -671,7 +724,7 @@ const w = {
671
724
  return `/* unknown unit: ${n} */`;
672
725
  }
673
726
  case "postgres":
674
- return `DATE_TRUNC('${w[n] ?? n.toLowerCase()}', ${t})`;
727
+ return `DATE_TRUNC('${v[n] ?? n.toLowerCase()}', ${t})`;
675
728
  case "mssql":
676
729
  switch (n) {
677
730
  case "YEAR":
@@ -716,68 +769,68 @@ const w = {
716
769
  // The separator goes into the SQL as a literal even in parameterized SQL:
717
770
  // MySQL's SEPARATOR takes nothing else.
718
771
  STRING_AGG: ([t = "??"], e, r, n, s, i) => {
719
- const a = e[1] ?? { type: "literal", value: ", " }, o = a.type === "literal" && typeof a.value == "string" ? st(a.value, r) : "??", f = s.distinct ? "DISTINCT " : "", c = ($) => {
720
- var p;
721
- return (p = s.orderBy) != null && p.length ? `ORDER BY ${s.orderBy.map((A) => `${$(A)} ${A.direction === "desc" ? "DESC" : "ASC"}`).join(", ")}` : "";
722
- }, u = ($) => i($.expression), l = ($) => $ ? ` ${$}` : "";
772
+ const a = e[1] ?? { type: "literal", value: ", " }, o = a.type === "literal" && typeof a.value == "string" ? ut(a.value, r) : "??", f = s.distinct ? "DISTINCT " : "", u = ($) => {
773
+ var T;
774
+ return (T = s.orderBy) != null && T.length ? `ORDER BY ${s.orderBy.map((A) => `${$(A)} ${A.direction === "desc" ? "DESC" : "ASC"}`).join(", ")}` : "";
775
+ }, c = ($) => i($.expression), l = ($) => $ ? ` ${$}` : "";
723
776
  switch (r) {
724
777
  case "mysql":
725
- return `GROUP_CONCAT(${f}${t}${l(c(u))} SEPARATOR ${o})`;
778
+ return `GROUP_CONCAT(${f}${t}${l(u(c))} SEPARATOR ${o})`;
726
779
  case "postgres": {
727
- const $ = `CAST(${t} AS TEXT)`, p = c((A) => {
728
- if (!s.distinct) return u(A);
729
- if (F(A.expression, e[0])) return $;
780
+ const $ = `CAST(${t} AS TEXT)`, T = u((A) => {
781
+ if (!s.distinct) return c(A);
782
+ if (w(A.expression, e[0])) return $;
730
783
  throw new Error("PostgreSQL orders STRING_AGG of different values by the value only");
731
784
  });
732
- return `STRING_AGG(${f}${$}, ${o}${l(p)})`;
785
+ return `STRING_AGG(${f}${$}, ${o}${l(T)})`;
733
786
  }
734
787
  case "mssql": {
735
788
  if (s.distinct)
736
789
  throw new Error("SQL Server's STRING_AGG can't join only different values");
737
- const $ = c(u), p = $ ? ` WITHIN GROUP (${$})` : "";
738
- return `STRING_AGG(CAST(${t} AS NVARCHAR(MAX)), ${o})${p}`;
790
+ const $ = u(c), T = $ ? ` WITHIN GROUP (${$})` : "";
791
+ return `STRING_AGG(CAST(${t} AS NVARCHAR(MAX)), ${o})${T}`;
739
792
  }
740
793
  default:
741
- return `STRING_AGG(${f}${t}, ${o}${l(c(u))})`;
794
+ return `STRING_AGG(${f}${t}, ${o}${l(u(c))})`;
742
795
  }
743
796
  }
744
797
  };
745
- function R(t) {
798
+ function C(t) {
746
799
  return t.type === "binary" || t.type === "logical" || t.type === "if" || t.type === "case";
747
800
  }
748
801
  function E(t, e, r, n) {
749
- return T(t, () => zt(t, e, r, n));
802
+ return y(t, () => oe(t, e, r, n));
750
803
  }
751
- function G(t, e, r, n) {
804
+ function V(t, e, r, n) {
752
805
  const s = (i) => {
753
806
  if (!i) return "??";
754
807
  const a = E(i, e, r, n);
755
- return R(i) ? `(${a})` : a;
808
+ return C(i) ? `(${a})` : a;
756
809
  };
757
- return T(t, () => {
810
+ return y(t, () => {
758
811
  const [i, a] = t.type === "list" ? t.items : [t];
759
812
  return `${s(i)} AND ${s(a)}`;
760
813
  });
761
814
  }
762
- function zt(t, e, r, n) {
815
+ function oe(t, e, r, n) {
763
816
  var a;
764
817
  const s = " ".repeat(r), i = " ".repeat(r + 1);
765
818
  switch (t.type) {
766
819
  case "literal":
767
- return t.value === "" && !t.empty ? "??" : t.value === null && t.as ? Wt(t.as, e) : st(t.value, e, n);
820
+ return t.value === "" && !t.empty ? "??" : t.value === null && t.as ? ne(t.as, e) : ut(t.value, e, n);
768
821
  case "param": {
769
- if (!n) return kt.test(t.name) ? `:${t.name}` : "??";
822
+ if (!n) return te.test(t.name) ? `:${t.name}` : "??";
770
823
  if (!n.named.has(t.name))
771
824
  throw new Error(`no value for parameter "${t.name}"`);
772
- return n.values.push(n.named.get(t.name)), rt(n.values.length - 1);
825
+ return n.values.push(n.named.get(t.name)), ot(n.values.length - 1);
773
826
  }
774
827
  case "field": {
775
828
  if (!t.column) return "??";
776
- const o = m(t.column, e);
777
- return t.table ? `${m(t.table, e)}.${o}` : o;
829
+ const o = g(t.column, e);
830
+ return t.table ? `${g(t.table, e)}.${o}` : o;
778
831
  }
779
832
  case "function": {
780
- const o = t.args.map((l) => E(l, e, r, n)), f = Kt[t.name.toUpperCase()];
833
+ const o = t.args.map((l) => E(l, e, r, n)), f = ie[t.name.toUpperCase()];
781
834
  if (f)
782
835
  return f(
783
836
  o,
@@ -787,21 +840,21 @@ function zt(t, e, r, n) {
787
840
  t,
788
841
  (l) => E(l, e, r, n)
789
842
  );
790
- const c = /^[A-Za-z_][A-Za-z0-9_.]*$/.test(t.name) || t.name === "" ? t.name : "??";
791
- if (o.length === 0 && c.toUpperCase() === "COUNT") return `${c}(*)`;
792
- const u = t.distinct && o.length > 0 ? "DISTINCT " : "";
793
- return `${c}(${u}${o.join(", ")})`;
843
+ const u = /^[A-Za-z_][A-Za-z0-9_.]*$/.test(t.name) || t.name === "" ? t.name : "??";
844
+ if (o.length === 0 && u.toUpperCase() === "COUNT") return `${u}(*)`;
845
+ const c = t.distinct && o.length > 0 ? "DISTINCT " : "";
846
+ return `${u}(${c}${o.join(", ")})`;
794
847
  }
795
848
  case "binary": {
796
- const o = t.operator ? Jt(t.operator, e) : "=", f = E(t.left, e, r, n), c = R(t.left) ? `(${f})` : f;
797
- if (Mt(t.operator)) return `${c} ${o}`;
798
- if (!t.right) return `${c} ${o} ??`;
849
+ const o = t.operator ? se(t.operator, e) : "=", f = E(t.left, e, r, n), u = C(t.left) ? `(${f})` : f;
850
+ if (vt(t.operator)) return `${u} ${o}`;
851
+ if (!t.right) return `${u} ${o} ??`;
799
852
  if (o === "BETWEEN")
800
- return `${c} BETWEEN ${G(t.right, e, r, n)}`;
853
+ return `${u} BETWEEN ${V(t.right, e, r, n)}`;
801
854
  if (o === "IN")
802
- return t.right.type === "list" ? `${c} IN ${E(t.right, e, r, n)}` : `${c} IN (${E(t.right, e, r, n)})`;
803
- const u = E(t.right, e, r, n), l = R(t.right) ? `(${u})` : u;
804
- return `${c} ${o} ${l}`;
855
+ return t.right.type === "list" ? `${u} IN ${E(t.right, e, r, n)}` : `${u} IN (${E(t.right, e, r, n)})`;
856
+ const c = E(t.right, e, r, n), l = C(t.right) ? `(${c})` : c;
857
+ return `${u} ${o} ${l}`;
805
858
  }
806
859
  case "logical": {
807
860
  if (t.operands.length === 0) return "/* empty */";
@@ -811,9 +864,9 @@ function zt(t, e, r, n) {
811
864
  ${s}${t.operator} `;
812
865
  return t.operands.map((f) => {
813
866
  if (f.type === "logical") {
814
- const c = E(f, e, r + 1, n);
867
+ const u = E(f, e, r + 1, n);
815
868
  return `(
816
- ${i}${c}
869
+ ${i}${u}
817
870
  ${s})`;
818
871
  }
819
872
  return E(f, e, r, n);
@@ -823,11 +876,11 @@ ${s})`;
823
876
  const { operand: o } = t;
824
877
  if (o.type === "binary" && o.operator === "IN" && ((a = o.right) == null ? void 0 : a.type) === "subquery") {
825
878
  const f = E(o.left, e, r, n);
826
- return `${R(o.left) ? `(${f})` : f} NOT IN (${E(o.right, e, r, n)})`;
879
+ return `${C(o.left) ? `(${f})` : f} NOT IN (${E(o.right, e, r, n)})`;
827
880
  }
828
881
  if (o.type === "binary" && o.operator === "BETWEEN" && o.right) {
829
882
  const f = E(o.left, e, r, n);
830
- return `${R(o.left) ? `(${f})` : f} NOT BETWEEN ${G(o.right, e, r, n)}`;
883
+ return `${C(o.left) ? `(${f})` : f} NOT BETWEEN ${V(o.right, e, r, n)}`;
831
884
  }
832
885
  if (o.type === "logical" || o.type === "not") {
833
886
  const f = E(o, e, r + 1, n);
@@ -838,34 +891,34 @@ ${s})`;
838
891
  return `NOT ${E(o, e, r, n)}`;
839
892
  }
840
893
  case "if": {
841
- const o = t.condition ? E(t.condition, e, r, n) : "??", f = t.then ? E(t.then, e, r, n) : "??", c = t.else ? E(t.else, e, r, n) : "NULL";
842
- return e === "mysql" ? `IF(${o}, ${f}, ${c})` : e === "mssql" ? `IIF(${o}, ${f}, ${c})` : `CASE WHEN ${o} THEN ${f} ELSE ${c} END`;
894
+ const o = t.condition ? E(t.condition, e, r, n) : "??", f = t.then ? E(t.then, e, r, n) : "??", u = t.else ? E(t.else, e, r, n) : "NULL";
895
+ return e === "mysql" ? `IF(${o}, ${f}, ${u})` : e === "mssql" ? `IIF(${o}, ${f}, ${u})` : `CASE WHEN ${o} THEN ${f} ELSE ${u} END`;
843
896
  }
844
897
  case "case": {
845
- const o = t.subject ? ` ${E(t.subject, e, r, n)}` : "", f = t.branches.map((u) => {
846
- const l = E(u.when, e, r + 1, n), $ = E(u.then, e, r + 1, n);
898
+ const o = t.subject ? ` ${E(t.subject, e, r, n)}` : "", f = t.branches.map((c) => {
899
+ const l = E(c.when, e, r + 1, n), $ = E(c.then, e, r + 1, n);
847
900
  return `${i}WHEN ${l} THEN ${$}`;
848
901
  }).join(`
849
- `), c = t.else ? `
902
+ `), u = t.else ? `
850
903
  ${i}ELSE ${E(t.else, e, r + 1, n)}` : "";
851
904
  return `CASE${o}
852
- ${f}${c}
905
+ ${f}${u}
853
906
  ${s}END`;
854
907
  }
855
908
  // EXISTS (SELECT 1 FROM … WHERE <link> AND <where>): the subquery written
856
909
  // in place, one level in, so its params fall in reading order.
857
910
  case "exists": {
858
- const { query: o, link: f } = t, c = ($) => $.type === "logical" && $.operator === "AND" ? $.operands : [$], u = o.where ? { type: "logical", operator: "AND", operands: [...c(f), ...c(o.where)] } : f;
911
+ const { query: o, link: f } = t, u = ($) => $.type === "logical" && $.operator === "AND" ? $.operands : [$], c = o.where ? { type: "logical", operator: "AND", operands: [...u(f), ...u(o.where)] } : f;
859
912
  return `EXISTS (
860
913
  ${[
861
914
  "SELECT 1",
862
915
  // The clauses below write their expressions with formatNode.
863
916
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
864
- ut(o.from, e),
917
+ Et(o.from, e),
865
918
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
866
- ...o.joins.map(($) => ft($, e, n)),
919
+ ...o.joins.map(($) => Tt($, e, n)),
867
920
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
868
- it(u, e, n)
921
+ ct(c, e, n)
869
922
  ].join(`
870
923
  `).replace(/^/gm, i)}
871
924
  ${s})`;
@@ -877,129 +930,129 @@ ${s})`;
877
930
  return `(${t.items.map((o) => E(o, e, r, n)).join(", ") || "??"})`;
878
931
  case "subquery":
879
932
  return `
880
- ${_(t.query, e, n).join(`
933
+ ${G(t.query, e, n).join(`
881
934
  `).replace(/^/gm, i)}
882
935
  ${s}`;
883
936
  default:
884
937
  return "/* unknown */";
885
938
  }
886
939
  }
887
- function it(t, e, r) {
940
+ function ct(t, e, r) {
888
941
  return `WHERE
889
942
  ${E(t, e, 1, r)}`;
890
943
  }
891
- const Zt = {
944
+ const ae = {
892
945
  inner: "INNER JOIN",
893
946
  left: "LEFT JOIN",
894
947
  right: "RIGHT JOIN",
895
948
  full: "FULL JOIN",
896
949
  cross: "CROSS JOIN"
897
950
  };
898
- let ot = /* @__PURE__ */ new Map();
899
- function at(t, e, r) {
900
- return t.shared !== void 0 ? m(ot.get(t.shared) ?? "??", e) : t.query ? `(
901
- ${ct(t.query, e, r).replace(/^/gm, " ")}
902
- )` : m(t.table, e);
951
+ let ft = /* @__PURE__ */ new Map();
952
+ function lt(t, e, r) {
953
+ return t.shared !== void 0 ? g(ft.get(t.shared) ?? "??", e) : t.query ? `(
954
+ ${$t(t.query, e, r).replace(/^/gm, " ")}
955
+ )` : g(t.table, e);
903
956
  }
904
- function ct(t, e, r) {
905
- return gt(t).map((n) => _(n, e, r).join(`
906
- `)).join(Dt(t) && !t.all ? `
957
+ function $t(t, e, r) {
958
+ return St(t).map((n) => G(n, e, r).join(`
959
+ `)).join(Rt(t) && !t.all ? `
907
960
  UNION
908
961
  ` : `
909
962
  UNION ALL
910
963
  `);
911
964
  }
912
- function te(t, e, r) {
965
+ function ue(t, e, r) {
913
966
  return t.length === 0 ? [] : [`WITH
914
- ${t.map((s) => T(s, () => {
915
- const i = ct(s.query, e, r).replace(/^/gm, " ");
916
- return ` ${m(s.name, e)} AS (
967
+ ${t.map((s) => y(s, () => {
968
+ const i = $t(s.query, e, r).replace(/^/gm, " ");
969
+ return ` ${g(s.name, e)} AS (
917
970
  ${i}
918
971
  )`;
919
972
  })).join(`,
920
973
  `)}`];
921
974
  }
922
- function ut(t, e, r) {
923
- const n = t.alias ? ` AS ${m(t.alias, e)}` : "";
924
- return `FROM ${T(t, () => `${at(t, e, r)}${n}`)}`;
975
+ function Et(t, e, r) {
976
+ const n = t.alias ? ` AS ${g(t.alias, e)}` : "";
977
+ return `FROM ${y(t, () => `${lt(t, e, r)}${n}`)}`;
925
978
  }
926
- function ft(t, e, r) {
927
- return T(t, () => {
928
- const n = at(t, e, r), s = t.alias ? ` AS ${m(t.alias, e)}` : "";
979
+ function Tt(t, e, r) {
980
+ return y(t, () => {
981
+ const n = lt(t, e, r), s = t.alias ? ` AS ${g(t.alias, e)}` : "";
929
982
  if (t.joinType === "cross") return `CROSS JOIN ${n}${s}`;
930
983
  const i = t.on ? E(t.on, e, 1, r) : "/* unconfigured */";
931
- return `${Zt[t.joinType]} ${n}${s}
984
+ return `${ae[t.joinType]} ${n}${s}
932
985
  ON ${i}`;
933
986
  });
934
987
  }
935
- function ee(t, e, r, n = "SELECT") {
936
- if (t.length === 0) return `${n} ${T(t, () => "*")}`;
937
- const s = t.map((i) => T(i, () => {
938
- const a = E(i.expression, e, 1, r), o = i.alias ? ` AS ${m(i.alias, e)}` : "";
988
+ function ce(t, e, r, n = "SELECT") {
989
+ if (t.length === 0) return `${n} ${y(t, () => "*")}`;
990
+ const s = t.map((i) => y(i, () => {
991
+ const a = E(i.expression, e, 1, r), o = i.alias ? ` AS ${g(i.alias, e)}` : "";
939
992
  return `${a}${o}`;
940
993
  }));
941
994
  return `${n}
942
995
  ${s.join(`,
943
996
  `)}`;
944
997
  }
945
- function re(t, e, r) {
998
+ function fe(t, e, r) {
946
999
  return `GROUP BY
947
- ${t.map((s) => T(
1000
+ ${t.map((s) => y(
948
1001
  s,
949
1002
  () => E(s.expression, e, 1, r)
950
1003
  )).join(`,
951
1004
  `)}`;
952
1005
  }
953
- function ne(t, e, r) {
1006
+ function le(t, e, r) {
954
1007
  return `HAVING
955
1008
  ${E(t, e, 1, r)}`;
956
1009
  }
957
- function se(t, e, r, n) {
1010
+ function $e(t, e, r, n) {
958
1011
  return `ORDER BY
959
- ${t.map((i) => T(i, () => `${E(n(i.expression), e, 1, r)} ${i.direction === "desc" ? "DESC" : "ASC"}`)).join(`,
1012
+ ${t.map((i) => y(i, () => `${E(n(i.expression), e, 1, r)} ${i.direction === "desc" ? "DESC" : "ASC"}`)).join(`,
960
1013
  `)}`;
961
1014
  }
962
- const B = (t) => t !== void 0 && Number.isInteger(t) && t >= 0 ? t : void 0;
963
- function lt(t, e) {
964
- const r = e.resolveName ? xt(t, e.resolveName) : t;
965
- return e.defaultLimit !== void 0 && B(r.limit) === void 0 ? { ...r, limit: e.defaultLimit } : r;
1015
+ const H = (t) => t !== void 0 && Number.isInteger(t) && t >= 0 ? t : void 0;
1016
+ function yt(t, e) {
1017
+ const r = e.resolveName ? zt(t, e.resolveName) : t;
1018
+ return e.defaultLimit !== void 0 && H(r.limit) === void 0 ? { ...r, limit: e.defaultLimit } : r;
966
1019
  }
967
- const ie = "18446744073709551615";
968
- function _(t, e, r, n = `SELECT${t.distinct ? " DISTINCT" : ""}`) {
1020
+ const Ee = "18446744073709551615";
1021
+ function G(t, e, r, n = `SELECT${t.distinct ? " DISTINCT" : ""}`) {
969
1022
  const s = [
970
- ee(t.select, e, r, n),
971
- ut(t.from, e, r),
972
- ...t.joins.map((a) => ft(a, e, r))
1023
+ ce(t.select, e, r, n),
1024
+ Et(t.from, e, r),
1025
+ ...t.joins.map((a) => Tt(a, e, r))
973
1026
  ];
974
- t.where && s.push(it(t.where, e, r));
1027
+ t.where && s.push(ct(t.where, e, r));
975
1028
  const i = t.groupBy ?? [];
976
- if (i.length > 0 && s.push(re(i, e, r)), t.having) {
977
- const a = e === "mysql" ? W(t.having, t) : t.having;
978
- s.push(ne(a, e, r));
1029
+ if (i.length > 0 && s.push(fe(i, e, r)), t.having) {
1030
+ const a = e === "mysql" ? z(t.having, t) : t.having;
1031
+ s.push(le(a, e, r));
979
1032
  }
980
1033
  return s;
981
1034
  }
982
- function $t(t, e, r) {
983
- N && (N.writing = [{ value: t, path: [] }]), ot = new Map((t.shared ?? []).map((u) => [u.id, u.name]));
984
- const n = te(t.shared ?? [], e, r), s = B(t.limit), i = B(t.offset) || void 0, a = t.orderBy ?? [], o = e === "mssql" && s !== void 0 && i === void 0, f = `SELECT${t.distinct ? " DISTINCT" : ""}${o ? ` ${T("limit", () => `TOP (${s})`)}` : ""}`, c = _(t, e, r, f);
985
- if (a.length > 0 && c.push(se(a, e, r, (u) => e === "mysql" ? W(u, t) : u)), e === "mssql") {
1035
+ function pt(t, e, r) {
1036
+ O && (O.writing = [{ value: t, path: [] }]), ft = new Map((t.shared ?? []).map((c) => [c.id, c.name]));
1037
+ const n = ue(t.shared ?? [], e, r), s = H(t.limit), i = H(t.offset) || void 0, a = t.orderBy ?? [], o = e === "mssql" && s !== void 0 && i === void 0, f = `SELECT${t.distinct ? " DISTINCT" : ""}${o ? ` ${y("limit", () => `TOP (${s})`)}` : ""}`, u = G(t, e, r, f);
1038
+ if (a.length > 0 && u.push($e(a, e, r, (c) => e === "mysql" ? z(c, t) : c)), e === "mssql") {
986
1039
  if (i !== void 0) {
987
- a.length === 0 && c.push("ORDER BY (SELECT NULL)");
988
- const u = s !== void 0 ? ` ${T("limit", () => `FETCH NEXT ${s} ROWS ONLY`)}` : "";
989
- c.push(`${T("offset", () => `OFFSET ${i} ROWS`)}${u}`);
1040
+ a.length === 0 && u.push("ORDER BY (SELECT NULL)");
1041
+ const c = s !== void 0 ? ` ${y("limit", () => `FETCH NEXT ${s} ROWS ONLY`)}` : "";
1042
+ u.push(`${y("offset", () => `OFFSET ${i} ROWS`)}${c}`);
990
1043
  }
991
1044
  } else if (s !== void 0 || i !== void 0) {
992
- const u = s !== void 0 || e === "mysql" ? T(s !== void 0 ? "limit" : {}, () => `LIMIT ${s ?? ie}`) : "", l = i !== void 0 ? T("offset", () => `OFFSET ${i}`) : "";
993
- c.push([u, l].filter(Boolean).join(" "));
1045
+ const c = s !== void 0 || e === "mysql" ? y(s !== void 0 ? "limit" : {}, () => `LIMIT ${s ?? Ee}`) : "", l = i !== void 0 ? y("offset", () => `OFFSET ${i}`) : "";
1046
+ u.push([c, l].filter(Boolean).join(" "));
994
1047
  }
995
- return [...n, ...c].join(`
1048
+ return [...n, ...u].join(`
996
1049
  `);
997
1050
  }
998
- function Ce(t, e, r = {}) {
999
- const n = r.values ? Lt(t, r.values) : t, s = r.schema ? et(n, r.schema).query : n;
1000
- return $t(lt(s, r), e);
1051
+ function de(t, e, r = {}) {
1052
+ const n = r.values ? Ut(t, r.values) : t, s = r.schema ? it(n, r.schema).query : n;
1053
+ return pt(yt(s, r), e);
1001
1054
  }
1002
- function oe(t, e) {
1055
+ function Te(t, e) {
1003
1056
  switch (t) {
1004
1057
  case "mysql":
1005
1058
  return "?";
@@ -1011,42 +1064,42 @@ function oe(t, e) {
1011
1064
  return "?";
1012
1065
  }
1013
1066
  }
1014
- function Le(t, e, r = {}) {
1015
- const n = r.schema ? et(t, r.schema) : { query: t, params: /* @__PURE__ */ new Map() }, s = {
1067
+ function Be(t, e, r = {}) {
1068
+ const n = r.schema ? it(t, r.schema) : { query: t, params: /* @__PURE__ */ new Map() }, s = {
1016
1069
  values: [],
1017
- named: new Map(Object.entries(r.values ?? {}).map(([c, u]) => {
1018
- const l = n.params.get(c);
1019
- return [c, l ? tt(u, l) : u];
1070
+ named: new Map(Object.entries(r.values ?? {}).map(([u, c]) => {
1071
+ const l = n.params.get(u);
1072
+ return [u, l ? st(c, l) : c];
1020
1073
  }))
1021
- }, i = lt(n.query, r), a = $t(i, e, s), o = [];
1022
- return { sql: a.replace(Xt, (c, u) => {
1023
- const l = s.values[Number(u)];
1024
- return o.push(l instanceof Date ? J(l) : l), oe(e, o.length);
1074
+ }, i = yt(n.query, r), a = pt(i, e, s), o = [];
1075
+ return { sql: a.replace(ee, (u, c) => {
1076
+ const l = s.values[Number(c)];
1077
+ return o.push(l instanceof Date ? Z(l) : l), Te(e, o.length);
1025
1078
  }), params: o };
1026
1079
  }
1027
1080
  export {
1028
- Se as D,
1029
- q as Q,
1030
- wt as S,
1031
- Le as a,
1032
- Ce as b,
1033
- Ut as c,
1034
- Ae as d,
1035
- Te as e,
1036
- ge as f,
1037
- pe as g,
1038
- Y as h,
1039
- Oe as i,
1040
- Ee as j,
1041
- Ne as k,
1042
- me as l,
1043
- De as m,
1044
- h as n,
1045
- Re as o,
1046
- he as p,
1047
- Ie as q,
1048
- ye as r,
1049
- le as s,
1050
- $e as u,
1051
- be as w
1081
+ qe as D,
1082
+ B as Q,
1083
+ dt as S,
1084
+ Be as a,
1085
+ de as b,
1086
+ Pt as c,
1087
+ Re as d,
1088
+ Ne as e,
1089
+ Ie as f,
1090
+ Se as g,
1091
+ k as h,
1092
+ Me as i,
1093
+ De as j,
1094
+ Le as k,
1095
+ be as l,
1096
+ Ce as m,
1097
+ R as n,
1098
+ Fe as o,
1099
+ we as p,
1100
+ Ue as q,
1101
+ Oe as r,
1102
+ ge as s,
1103
+ he as u,
1104
+ ve as w
1052
1105
  };