@nongt/visql-core 0.4.1 → 0.4.2

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