@nongt/visql-core 0.4.0 → 0.4.1

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