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