@nongt/visql-core 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,153 +1,153 @@
1
- var qt = Object.defineProperty;
2
- var Ut = (t, e, r) => e in t ? qt(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
3
- var M = (t, e, r) => Ut(t, typeof e != "symbol" ? e + "" : e, r);
4
- import { q as U, g as B, F as vt, G as Bt, o as K, d as nt, t as ft, h as jt, w as lt, H as _t, k as Ht, b as Yt, i as Pt } from "./schema-DFC26AhZ.js";
5
- import { aj as R, aD as k, aE as Gt, aF as xt, aG as kt, am as Qt, V as Xt, as as Jt, aH as A, ao as Vt, a8 as Wt, aI as Kt, aJ as $t, $ as zt } from "./issueLocation-732-e0B8.js";
6
- import { s as Et } from "./calendarDate-CYlr9TvX.js";
7
- class Zt extends Error {
8
- constructor(r, n, s, o) {
9
- const a = o ? `"${n}" (as "${o}")` : `"${n}"`;
1
+ var dt = Object.defineProperty;
2
+ var Bt = (t, e, r) => e in t ? dt(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
3
+ var q = (t, e, r) => Bt(t, typeof e != "symbol" ? e + "" : e, r);
4
+ import { q as d, g as B, F as jt, G as _t, o as z, d as st, t as lt, h as Ht, w as $t, H as Yt, k as Pt, b as Gt, i as xt } from "./schema-DFC26AhZ.js";
5
+ import { aj as C, aD as Q, aE as kt, aF as Qt, aG as Jt, am as Wt, V as Xt, as as Vt, aH as m, ao as Kt, a8 as zt, aI as Zt, aJ as Et, $ as te, a0 as ee } from "./issueLocation-DEIwB1DW.js";
6
+ import { s as yt } from "./calendarDate-CYlr9TvX.js";
7
+ class re extends Error {
8
+ constructor(r, n, s, i) {
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
- M(this, "path");
13
+ q(this, "path");
14
14
  /** The table it reads that needs scope. */
15
- M(this, "table");
15
+ q(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
- M(this, "alias");
19
- this.name = "ScopeError", this.path = r, this.table = n, o && (this.alias = o);
18
+ q(this, "alias");
19
+ this.name = "ScopeError", this.path = r, this.table = n, i && (this.alias = i);
20
20
  }
21
21
  }
22
- const H = (t) => Object.prototype.toString.call(t) === "[object Object]", yt = (t) => Array.isArray(t.joins) && H(t.from);
22
+ const Y = (t) => Object.prototype.toString.call(t) === "[object Object]", Tt = (t) => Array.isArray(t.joins) && Y(t.from);
23
23
  function j(t, e, r) {
24
24
  if (Array.isArray(t)) {
25
- const a = t.map((i, c) => j(i, [...e, c], r));
26
- return a.every((i, c) => i === t[c]) ? t : a;
25
+ const a = t.map((o, c) => j(o, [...e, c], r));
26
+ return a.every((o, c) => o === t[c]) ? t : a;
27
27
  }
28
- if (!H(t)) return t;
28
+ if (!Y(t)) return t;
29
29
  let n = !1;
30
- const s = Object.entries(t).map(([a, i]) => {
31
- const c = j(i, [...e, a], r);
32
- return c !== i && (n = !0), [a, c];
33
- }), o = n ? Object.fromEntries(s) : t;
34
- return yt(o) ? r(o, e) : o;
30
+ const s = Object.entries(t).map(([a, o]) => {
31
+ const c = j(o, [...e, a], r);
32
+ return c !== o && (n = !0), [a, c];
33
+ }), i = n ? Object.fromEntries(s) : t;
34
+ return Tt(i) ? r(i, e) : i;
35
35
  }
36
- const pt = (t, e) => U(t).flatMap((r) => r.table !== void 0 && e.requires(r.table) ? [{ table: r.table, ...r.alias ? { alias: r.alias } : {}, ref: r.alias ?? r.table }] : []), Tt = (t) => U(t).flatMap((e) => {
36
+ const pt = (t, e) => d(t).flatMap((r) => r.table !== void 0 && e.requires(r.table) ? [{ table: r.table, ...r.alias ? { alias: r.alias } : {}, ref: r.alias ?? r.table }] : []), At = (t) => d(t).flatMap((e) => {
37
37
  const r = e.alias ?? e.table;
38
38
  return r === void 0 ? [] : [r];
39
39
  });
40
- function Q(t, e) {
40
+ function J(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 K(t, (r) => Q(r, e));
46
- }
47
- const At = (t) => t ? t.type === "logical" && t.operator === "AND" ? t.operands : [t] : [], te = (t) => t.joinType === "inner" || t.joinType === "left";
48
- function ee(t, e, r, n, s) {
49
- const o = e.type === "binary" && e.left && e.right ? { ...e, left: e.right, right: e.left } : e, a = { forward: !1, back: !1 };
50
- return t.joins.forEach((i) => {
51
- const c = i.alias ?? i.table;
52
- if (c !== r && c !== n || !te(i) || !At(i.on).some((f) => B(f, e) || B(f, o))) return;
53
- const u = i.joinType === "inner";
45
+ return z(t, (r) => J(r, e));
46
+ }
47
+ const mt = (t) => t ? t.type === "logical" && t.operator === "AND" ? t.operands : [t] : [], ne = (t) => t.joinType === "inner" || t.joinType === "left";
48
+ function se(t, e, r, n, s) {
49
+ const i = e.type === "binary" && e.left && e.right ? { ...e, left: e.right, right: e.left } : e, a = { forward: !1, back: !1 };
50
+ return t.joins.forEach((o) => {
51
+ const c = o.alias ?? o.table;
52
+ if (c !== r && c !== n || !ne(o) || !mt(o.on).some((f) => B(f, e) || B(f, i))) return;
53
+ const u = o.joinType === "inner";
54
54
  c === r ? (a.forward = !0, a.back || (a.back = u)) : (a.back = !0, a.forward || (a.forward = u || s));
55
55
  }), a;
56
56
  }
57
- const st = (t) => t.some((e, r) => t.slice(0, r).every((n) => n.forward) && t.slice(r).every((n) => n.back)) || t.every((e) => e.forward);
58
- function mt(t, e, r, n, s = new Set(Tt(e))) {
59
- const o = k(n.condition), a = U(e), i = (c) => {
60
- const u = /* @__PURE__ */ new Map([[r.table, r.ref]]), f = /* @__PURE__ */ new Map(), l = [], $ = new Set(s), p = (m) => {
61
- const O = f.get(m);
62
- return O ? [...p(O.ref), O.tie] : [];
63
- }, g = (m, O, P) => {
64
- if (O === m.length) return st(p(u.get(P)));
65
- const h = m[O], L = Bt(t, [...u.keys()], h);
66
- if (!L) return !1;
67
- const Z = u.get(k(L.on).find((y) => y !== h)), tt = (y) => Q(L.on, new Map([...u, [h, y]])), et = o.includes(h), rt = (y, S) => {
68
- u.set(h, y), f.set(y, { ref: Z, tie: S });
69
- }, Ft = (y, S) => (rt(y, S), st(p(y)) && g(m, O + 1, P) ? !0 : (u.delete(h), f.delete(y), !1));
70
- if (a.some((y) => {
71
- if (y.table !== h) return !1;
72
- const S = y.alias ?? h, G = ee(e, tt(S), Z, S, et);
73
- return (G.forward || G.back) && Ft(S, G);
57
+ const ot = (t) => t.some((e, r) => t.slice(0, r).every((n) => n.forward) && t.slice(r).every((n) => n.back)) || t.every((e) => e.forward);
58
+ function gt(t, e, r, n, s = new Set(At(e))) {
59
+ const i = Q(n.condition), a = d(e), o = (c) => {
60
+ const u = /* @__PURE__ */ new Map([[r.table, r.ref]]), f = /* @__PURE__ */ new Map(), l = [], $ = new Set(s), y = (g) => {
61
+ const N = f.get(g);
62
+ return N ? [...y(N.ref), N.tie] : [];
63
+ }, p = (g, N, G) => {
64
+ if (N === g.length) return ot(y(u.get(G)));
65
+ const O = g[N], F = _t(t, [...u.keys()], O);
66
+ if (!F) return !1;
67
+ const tt = u.get(Q(F.on).find((T) => T !== O)), et = (T) => J(F.on, new Map([...u, [O, T]])), rt = i.includes(O), nt = (T, b) => {
68
+ u.set(O, T), f.set(T, { ref: tt, tie: b });
69
+ }, vt = (T, b) => (nt(T, b), ot(y(T)) && p(g, N + 1, G) ? !0 : (u.delete(O), f.delete(T), !1));
70
+ if (a.some((T) => {
71
+ if (T.table !== O) return !1;
72
+ const b = T.alias ?? O, x = se(e, et(b), tt, b, rt);
73
+ return (x.forward || x.back) && vt(b, x);
74
74
  })) return !0;
75
75
  if (!c) return !1;
76
- let N = h;
77
- for (let y = 2; $.has(N); y += 1) N = `${h}_${y}`;
78
- return $.add(N), rt(N, { back: !0, forward: L.joinType === "inner" || et }), l.push({
79
- id: Gt(),
80
- table: h,
81
- ...N !== h ? { alias: N } : {},
82
- joinType: L.joinType,
83
- on: tt(N)
84
- }), g(m, O + 1, P);
76
+ let I = O;
77
+ for (let T = 2; $.has(I); T += 1) I = `${O}_${T}`;
78
+ return $.add(I), nt(I, { back: !0, forward: F.joinType === "inner" || rt }), l.push({
79
+ id: kt(),
80
+ table: O,
81
+ ...I !== O ? { alias: I } : {},
82
+ joinType: F.joinType,
83
+ on: et(I)
84
+ }), p(g, N + 1, G);
85
85
  };
86
- let v;
87
- if (o.every((m) => {
88
- if (v || u.has(m)) return !0;
89
- const O = vt(t, [...u.keys()], m);
90
- return O ? g(O, 0, m) : (v = m, !0);
86
+ let D;
87
+ if (i.every((g) => {
88
+ if (D || u.has(g)) return !0;
89
+ const N = jt(t, [...u.keys()], g);
90
+ return N ? p(N, 0, g) : (D = g, !0);
91
91
  }))
92
- return $.forEach((m) => s.add(m)), { joins: l, condition: Q(n.condition, u), ...v ? { missing: v } : {} };
92
+ return $.forEach((g) => s.add(g)), { joins: l, condition: J(n.condition, u), ...D ? { missing: D } : {} };
93
93
  };
94
- return i(!1) ?? i(!0);
94
+ return o(!1) ?? o(!0);
95
95
  }
96
- function gt(t, e) {
97
- return At(t).some((r) => B(r, e));
96
+ function Ot(t, e) {
97
+ return mt(t).some((r) => B(r, e));
98
98
  }
99
- function re(t, e) {
99
+ function oe(t, e) {
100
100
  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;
101
101
  }
102
- function ot(t, e, r, n, s) {
103
- return pt(e, r).reduce((o, a) => {
104
- const i = mt(t, o, a, r, s);
105
- if (i.missing) throw new Zt(n, a.table, i.missing, a.alias);
106
- const c = gt(o.where, i.condition);
107
- return i.joins.length === 0 && c ? o : {
108
- ...o,
109
- joins: [...o.joins, ...i.joins],
110
- where: c ? o.where : re(o.where, i.condition)
102
+ function it(t, e, r, n, s) {
103
+ return pt(e, r).reduce((i, a) => {
104
+ const o = gt(t, i, a, r, s);
105
+ if (o.missing) throw new re(n, a.table, o.missing, a.alias);
106
+ const c = Ot(i.where, o.condition);
107
+ return o.joins.length === 0 && c ? i : {
108
+ ...i,
109
+ joins: [...i.joins, ...o.joins],
110
+ where: c ? i.where : oe(i.where, o.condition)
111
111
  };
112
112
  }, e);
113
113
  }
114
- function it(t) {
115
- const e = /* @__PURE__ */ new Map(), r = (n, s, o, a) => {
114
+ function at(t) {
115
+ const e = /* @__PURE__ */ new Map(), r = (n, s, i, a) => {
116
116
  if (Array.isArray(n)) {
117
117
  n.forEach((c, u) => r(c, [...s, u], void 0, a));
118
118
  return;
119
119
  }
120
- if (!H(n)) return;
121
- let i = a;
122
- yt(n) && (i = o ?? /* @__PURE__ */ new Set(), e.set(JSON.stringify(s), i), Tt(n).forEach((c) => i.add(c))), Object.entries(n).forEach(([c, u]) => r(
120
+ if (!Y(n)) return;
121
+ let o = a;
122
+ Tt(n) && (o = i ?? /* @__PURE__ */ new Set(), e.set(JSON.stringify(s), o), At(n).forEach((c) => o.add(c))), Object.entries(n).forEach(([c, u]) => r(
123
123
  u,
124
124
  [...s, c],
125
- n.type === "exists" && c === "query" ? i : void 0,
126
- i
125
+ n.type === "exists" && c === "query" ? o : void 0,
126
+ o
127
127
  ));
128
128
  };
129
129
  return r(t, [], void 0, /* @__PURE__ */ new Set()), e;
130
130
  }
131
- function ve(t, e, r) {
132
- const n = ot(
131
+ function _e(t, e, r) {
132
+ const n = it(
133
133
  t,
134
134
  e,
135
135
  r,
136
136
  [],
137
- it(e).get("[]")
138
- ), s = R(t, e, n), o = it(s), a = j(s, [], (i, c) => c.length === 0 ? i : ot(t, i, r, c, o.get(JSON.stringify(c))));
139
- return R(t, s, a);
137
+ at(e).get("[]")
138
+ ), s = C(t, e, n), i = at(s), a = j(s, [], (o, c) => c.length === 0 ? o : it(t, o, r, c, i.get(JSON.stringify(c))));
139
+ return C(t, s, a);
140
140
  }
141
- function Be(t, e, r) {
142
- if (!H(t) || !Array.isArray(t.tables))
141
+ function He(t, e, r) {
142
+ if (!Y(t) || !Array.isArray(t.tables))
143
143
  throw new TypeError("unscopedBodies takes (schema, query, rule) since 0.4.0");
144
144
  const n = [];
145
- return j(e, [], (s, o) => (pt(s, r).forEach((a) => {
146
- const i = mt(t, s, a, r);
147
- (i.missing || i.joins.length > 0 || !gt(s.where, i.condition)) && n.push({ path: o, table: a.table, ...a.alias ? { alias: a.alias } : {} });
145
+ return j(e, [], (s, i) => (pt(s, r).forEach((a) => {
146
+ const o = gt(t, s, a, r);
147
+ (o.missing || o.joins.length > 0 || !Ot(s.where, o.condition)) && n.push({ path: i, table: a.table, ...a.alias ? { alias: a.alias } : {} });
148
148
  }), s)), n;
149
149
  }
150
- const ne = (t) => typeof t == "object" && t !== null && !(t instanceof Date), at = {
150
+ const ie = (t) => typeof t == "object" && t !== null && !(t instanceof Date), ut = {
151
151
  /** A column: `expr.field('orders', 'status')`. */
152
152
  field: (t, e) => ({ type: "field", table: t, column: e }),
153
153
  /** A literal value, written into the SQL (as a bound parameter by
@@ -214,7 +214,7 @@ const ne = (t) => typeof t == "object" && t !== null && !(t instanceof Date), at
214
214
  type: "binary",
215
215
  operator: "IN",
216
216
  left: t,
217
- right: Array.isArray(e) ? at.list(...e) : e
217
+ right: Array.isArray(e) ? ut.list(...e) : e
218
218
  }),
219
219
  /** `left BETWEEN low AND high` (issue 169), both bounds included: each a
220
220
  * value or an expression (a param, a relative date). NOT BETWEEN:
@@ -223,13 +223,13 @@ const ne = (t) => typeof t == "object" && t !== null && !(t instanceof Date), at
223
223
  type: "binary",
224
224
  operator: "BETWEEN",
225
225
  left: t,
226
- right: at.list(e, r)
226
+ right: ut.list(e, r)
227
227
  }),
228
228
  /** IN's values: each a value (`expr.list(1, 2)`) or an expression
229
229
  * (`expr.list(expr.param('a'), expr.param('b'))`). */
230
230
  list: (...t) => ({
231
231
  type: "list",
232
- items: t.map((e) => ne(e) ? e : { type: "literal", value: e })
232
+ items: t.map((e) => ie(e) ? e : { type: "literal", value: e })
233
233
  }),
234
234
  /** A query of its own for IN to compare with, selecting `select` — one
235
235
  * column. Its tables are its own: it sees nothing of the query around
@@ -238,91 +238,91 @@ const ne = (t) => typeof t == "object" && t !== null && !(t instanceof Date), at
238
238
  * 'customer_id') }))`. */
239
239
  subquery: ({ select: t, ...e }) => ({
240
240
  type: "subquery",
241
- id: nt(),
242
- query: { joins: [], ...e, select: [{ id: nt(), expression: t }] }
241
+ id: st(),
242
+ query: { joins: [], ...e, select: [{ id: st(), expression: t }] }
243
243
  })
244
244
  };
245
- function je(t, e, r) {
246
- return A(t, e, { type: "setBaseTable", table: r });
245
+ function Ye(t, e, r) {
246
+ return m(t, e, { type: "setBaseTable", table: r });
247
247
  }
248
- function _e(t, e, r, n = {}) {
248
+ function Pe(t, e, r, n = {}) {
249
249
  const s = { type: "addJoin", table: r, ...n };
250
- return A(t, e, s);
250
+ return m(t, e, s);
251
251
  }
252
- function He(t, e, r) {
253
- return A(t, e, { type: "removeJoin", joinId: r });
252
+ function Ge(t, e, r) {
253
+ return m(t, e, { type: "removeJoin", joinId: r });
254
254
  }
255
- function se(t, e, r, n = {}) {
256
- const s = xt(e), o = k(r);
257
- if (s && o.some((u) => u !== s.alias)) {
258
- if (o.includes(s.alias))
255
+ function ae(t, e, r, n = {}) {
256
+ const s = Qt(e), i = Q(r);
257
+ if (s && i.some((u) => u !== s.alias)) {
258
+ if (i.includes(s.alias))
259
259
  throw new Error("addCondition: a condition names both the union and tables of its branches: add it in two parts");
260
260
  return s.query.union.forEach((u) => {
261
- const f = u.from.table, l = new Set(U(u).map(ft)), $ = kt(t, f), p = o.find((g) => !l.has(g) && !$.has(g));
262
- if (p)
263
- throw new Error(`addCondition: "${p}" can't be reached from "${f}", one of the union's tables: a condition must reach every branch`);
264
- }), s.query.union.reduce((u, f, l) => Qt(
261
+ const f = u.from.table, l = new Set(d(u).map(lt)), $ = Jt(t, f), y = i.find((p) => !l.has(p) && !$.has(p));
262
+ if (y)
263
+ throw new Error(`addCondition: "${y}" can't be reached from "${f}", one of the union's tables: a condition must reach every branch`);
264
+ }), s.query.union.reduce((u, f, l) => Wt(
265
265
  u,
266
266
  [Xt(s.id, l)],
267
- ($) => se(t, $, r, n)
267
+ ($) => ae(t, $, r, n)
268
268
  ), e);
269
269
  }
270
- const a = n.operator ?? "AND", { where: i } = e;
270
+ const a = n.operator ?? "AND", { where: o } = e;
271
271
  let c;
272
- return i ? i.type === "logical" && i.operator === a ? c = { ...i, operands: [...i.operands, r] } : c = { type: "logical", operator: a, operands: [i, r] } : c = r, R(t, e, Jt(t, e, c));
272
+ return o ? o.type === "logical" && o.operator === a ? c = { ...o, operands: [...o.operands, r] } : c = { type: "logical", operator: a, operands: [o, r] } : c = r, C(t, e, Vt(t, e, c));
273
273
  }
274
- function Ye(t, e, r, n) {
275
- const s = A(t, e, { type: "addSelectItem" }), o = s.select[s.select.length - 1].id, a = A(t, s, {
274
+ function xe(t, e, r, n) {
275
+ const s = m(t, e, { type: "addSelectItem" }), i = s.select[s.select.length - 1].id, a = m(t, s, {
276
276
  type: "setSelectExpression",
277
- selectId: o,
277
+ selectId: i,
278
278
  expression: r
279
- }), i = n === void 0 ? a : A(t, a, { type: "setSelectAlias", selectId: o, alias: n });
280
- return R(t, e, i);
279
+ }), o = n === void 0 ? a : m(t, a, { type: "setSelectAlias", selectId: i, alias: n });
280
+ return C(t, e, o);
281
281
  }
282
- function Pe(t, e, r) {
283
- const n = A(t, e, { type: "addGroupByItem" }), { id: s } = n.groupBy[n.groupBy.length - 1], o = A(t, n, {
282
+ function ke(t, e, r) {
283
+ const n = m(t, e, { type: "addGroupByItem" }), { id: s } = n.groupBy[n.groupBy.length - 1], i = m(t, n, {
284
284
  type: "setGroupByExpression",
285
285
  groupById: s,
286
286
  expression: r
287
287
  });
288
- return R(t, e, o);
288
+ return C(t, e, i);
289
289
  }
290
- function Ge(t, e, r) {
291
- return R(t, e, Vt(t, e, r));
290
+ function Qe(t, e, r) {
291
+ return C(t, e, Kt(t, e, r));
292
292
  }
293
- function xe(t, e, r, n = "asc") {
294
- const s = A(t, e, { type: "addOrderByItem" }), { id: o } = s.orderBy[s.orderBy.length - 1], a = A(t, s, {
293
+ function Je(t, e, r, n = "asc") {
294
+ const s = m(t, e, { type: "addOrderByItem" }), { id: i } = s.orderBy[s.orderBy.length - 1], a = m(t, s, {
295
295
  type: "setOrderByExpression",
296
- orderById: o,
296
+ orderById: i,
297
297
  expression: r
298
- }), i = A(t, a, {
298
+ }), o = m(t, a, {
299
299
  type: "setOrderByDirection",
300
- orderById: o,
300
+ orderById: i,
301
301
  direction: n
302
302
  });
303
- return R(t, e, i);
303
+ return C(t, e, o);
304
304
  }
305
- function ke(t, e, r) {
305
+ function We(t, e, r) {
306
306
  let n = e;
307
- 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;
307
+ return "limit" in r && (n = m(t, n, { type: "setLimit", limit: r.limit })), "offset" in r && (n = m(t, n, { type: "setOffset", offset: r.offset })), n;
308
308
  }
309
- function Qe(t, e, r) {
310
- return A(t, e, { type: "setDistinct", distinct: r });
309
+ function Xe(t, e, r) {
310
+ return m(t, e, { type: "setDistinct", distinct: r });
311
311
  }
312
- function Xe(t) {
313
- if (!jt(t)) throw new Error(`query "${t.id}" has no base table yet`);
312
+ function Ve(t) {
313
+ if (!Ht(t)) throw new Error(`query "${t.id}" has no base table yet`);
314
314
  return t;
315
315
  }
316
- const X = 4;
317
- class w extends Error {
316
+ const W = 4;
317
+ class M extends Error {
318
318
  constructor(r, n) {
319
319
  super(n);
320
- M(this, "kind");
320
+ q(this, "kind");
321
321
  this.name = "QueryFormatError", this.kind = r;
322
322
  }
323
323
  }
324
- const q = (t) => typeof t == "object" && t !== null && !Array.isArray(t);
325
- function oe(t) {
324
+ const v = (t) => typeof t == "object" && t !== null && !Array.isArray(t);
325
+ function ue(t) {
326
326
  const e = [];
327
327
  let r = 0;
328
328
  for (; r < t.length; ) {
@@ -341,83 +341,83 @@ function oe(t) {
341
341
  }
342
342
  return e;
343
343
  }
344
- function ie(t) {
344
+ function ce(t) {
345
345
  const e = (r) => {
346
346
  if (Array.isArray(r)) return r.map(e);
347
- if (!q(r)) return r;
348
- const n = Object.fromEntries(Object.entries(r).map(([a, i]) => [a, e(i)])), { right: s } = n;
349
- if (n.type !== "binary" || n.operator !== "IN" || !q(s) || s.type === "subquery" || s.type === "list") return n;
350
- const o = s.type === "literal" && typeof s.value == "string" ? oe(s.value).map((a) => ({ type: "literal", value: a })) : [s];
351
- return { ...n, right: { type: "list", items: o } };
347
+ if (!v(r)) return r;
348
+ const n = Object.fromEntries(Object.entries(r).map(([a, o]) => [a, e(o)])), { right: s } = n;
349
+ if (n.type !== "binary" || n.operator !== "IN" || !v(s) || s.type === "subquery" || s.type === "list") return n;
350
+ const i = s.type === "literal" && typeof s.value == "string" ? ue(s.value).map((a) => ({ type: "literal", value: a })) : [s];
351
+ return { ...n, right: { type: "list", items: i } };
352
352
  };
353
353
  return e(t);
354
354
  }
355
- const ae = (t) => t, ue = (t) => t, ce = [ie, ae, ue];
356
- function fe(t, e, r) {
355
+ const fe = (t) => t, le = (t) => t, $e = [ce, fe, le];
356
+ function Ee(t, e, r) {
357
357
  return r.slice(e - 1).reduce((n, s) => s(n), t);
358
358
  }
359
359
  function _(t, e) {
360
- return Array.isArray(t) ? t.map((r) => _(r, e)) : q(t) ? t.type === "literal" ? e(t) : Object.fromEntries(Object.entries(t).map(([r, n]) => [r, _(n, e)])) : t;
360
+ return Array.isArray(t) ? t.map((r) => _(r, e)) : v(t) ? t.type === "literal" ? e(t) : Object.fromEntries(Object.entries(t).map(([r, n]) => [r, _(n, e)])) : t;
361
361
  }
362
- function le(t) {
362
+ function ye(t) {
363
363
  return t.value instanceof Date ? { ...t, value: t.value.toISOString(), valueType: "date" } : typeof t.value == "bigint" ? { ...t, value: String(t.value), valueType: "bigint" } : t;
364
364
  }
365
- function $e(t) {
365
+ function Te(t) {
366
366
  if (t.valueType === void 0) return t;
367
367
  const { valueType: e, ...r } = t, n = String(r.value);
368
368
  let s;
369
369
  if (e === "date") {
370
370
  if (s = new Date(n), Number.isNaN(s.getTime()))
371
- throw new w("invalid", `not a date: ${JSON.stringify(n)}`);
371
+ throw new M("invalid", `not a date: ${JSON.stringify(n)}`);
372
372
  } else if (e === "bigint" && /^-?\d+$/.test(n))
373
373
  s = BigInt(n);
374
374
  else {
375
- const o = String(e);
376
- throw new w("invalid", `can't read a ${o} from ${JSON.stringify(n)}`);
375
+ const i = String(e);
376
+ throw new M("invalid", `can't read a ${i} from ${JSON.stringify(n)}`);
377
377
  }
378
378
  return { ...r, value: s };
379
379
  }
380
- function Je(t) {
380
+ function Ke(t) {
381
381
  return {
382
- version: X,
383
- query: _(t, le)
382
+ version: W,
383
+ query: _(t, ye)
384
384
  };
385
385
  }
386
- function Ve(t) {
387
- if (!q(t))
388
- throw new w("invalid", "a stored query is an object");
386
+ function ze(t) {
387
+ if (!v(t))
388
+ throw new M("invalid", "a stored query is an object");
389
389
  const { version: e } = t;
390
390
  if (typeof e != "number" || !Number.isInteger(e) || e < 1)
391
- throw new w("invalid", "a stored query has a whole-number version from 1");
392
- if (e > X)
393
- throw new w(
391
+ throw new M("invalid", "a stored query has a whole-number version from 1");
392
+ if (e > W)
393
+ throw new M(
394
394
  "newerVersion",
395
- `stored in format ${e}; this version reads up to ${X}`
395
+ `stored in format ${e}; this version reads up to ${W}`
396
396
  );
397
- const r = fe(t.query, e, ce);
398
- if (!q(r) || typeof r.id != "string")
399
- throw new w("invalid", "a stored query has a query with a string id");
400
- return _(r, $e);
397
+ const r = Ee(t.query, e, $e);
398
+ if (!v(r) || typeof r.id != "string")
399
+ throw new M("invalid", "a stored query has a query with a string id");
400
+ return _(r, Te);
401
401
  }
402
- function J(t, e) {
402
+ function X(t, e) {
403
403
  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);
404
- return t.forEach(({ table: o, alias: a }) => {
405
- if (o === void 0) {
404
+ return t.forEach(({ table: i, alias: a }) => {
405
+ if (i === void 0) {
406
406
  a && s.add(a);
407
407
  return;
408
408
  }
409
- if (r.set(o, o), a) {
410
- r.set(a, o), n.add(a), s.delete(a);
409
+ if (r.set(i, i), a) {
410
+ r.set(a, i), n.add(a), s.delete(a);
411
411
  return;
412
412
  }
413
- n.delete(o), s.delete(o);
413
+ n.delete(i), s.delete(i);
414
414
  }), { tableOf: r, aliases: n, derived: s };
415
415
  }
416
- function Ee(t, e, r) {
417
- const n = t.on && F(t.on, e, r);
418
- return t.query ? { ...t, on: n, query: _t(t.query, (s) => Y(s, r)) } : t.shared !== void 0 ? { ...t, on: n } : { ...t, on: n, table: r(t.table) };
416
+ function pe(t, e, r) {
417
+ const n = t.on && U(t.on, e, r);
418
+ return t.query ? { ...t, on: n, query: Yt(t.query, (s) => P(s, r)) } : t.shared !== void 0 ? { ...t, on: n } : { ...t, on: n, table: r(t.table) };
419
419
  }
420
- function F(t, e, r) {
420
+ function U(t, e, r) {
421
421
  if (t.type === "field") {
422
422
  if (!t.table || e.derived.has(t.table)) return t;
423
423
  const n = e.tableOf.get(t.table) ?? t.table;
@@ -427,27 +427,27 @@ function F(t, e, r) {
427
427
  ...t.column ? { column: r(n, t.column) } : {}
428
428
  };
429
429
  }
430
- if (t.type === "subquery") return { ...t, query: Y(t.query, r) };
430
+ if (t.type === "subquery") return { ...t, query: P(t.query, r) };
431
431
  if (t.type === "exists") {
432
- const { query: n } = t, s = [n.from, ...n.joins], o = J(s), a = J(s, e);
432
+ const { query: n } = t, s = [n.from, ...n.joins], i = X(s), a = X(s, e);
433
433
  return {
434
434
  ...t,
435
- link: F(t.link, a, r),
435
+ link: U(t.link, a, r),
436
436
  query: {
437
437
  ...n,
438
438
  from: { ...n.from, table: r(n.from.table) },
439
- joins: n.joins.map((i) => Ee(i, o, r)),
440
- ...n.where ? { where: F(n.where, o, r) } : {}
439
+ joins: n.joins.map((o) => pe(o, i, r)),
440
+ ...n.where ? { where: U(n.where, i, r) } : {}
441
441
  }
442
442
  };
443
443
  }
444
- return K(t, (n) => F(n, e, r));
444
+ return z(t, (n) => U(n, e, r));
445
445
  }
446
- function Y(t, e) {
447
- const r = J([t.from, ...t.joins]), n = lt(
446
+ function P(t, e) {
447
+ const r = X([t.from, ...t.joins]), n = $t(
448
448
  t,
449
- (s) => F(s, r, e),
450
- (s) => Y(s, e)
449
+ (s) => U(s, r, e),
450
+ (s) => P(s, e)
451
451
  );
452
452
  return {
453
453
  ...n,
@@ -459,7 +459,7 @@ function V(t, e) {
459
459
  if (Array.isArray(t)) return t.map((s) => V(s, e));
460
460
  if (typeof t != "object" || t === null || t instanceof Date) return t;
461
461
  const r = Object.fromEntries(Object.entries(t).map(
462
- ([s, o]) => [s, V(o, e)]
462
+ ([s, i]) => [s, V(i, e)]
463
463
  ));
464
464
  if (!("from" in r) || !Array.isArray(r.joins)) return r;
465
465
  const n = (s) => s.table !== void 0 && !s.alias && e(s.table) !== s.table ? { ...s, alias: s.table } : s;
@@ -469,44 +469,44 @@ function V(t, e) {
469
469
  joins: r.joins.map(n)
470
470
  };
471
471
  }
472
- function ye(t, e) {
473
- return Y(V(t, e), e);
472
+ function Ae(t, e) {
473
+ return P(V(t, e), e);
474
474
  }
475
- const pe = /^(\d{2}):(\d{2})(?::(\d{2}))?$/, ut = /YYYY|MM|DD|HH|mm|ss/g, C = (t, e = 2) => String(t).padStart(e, "0");
476
- function ht(t, e) {
475
+ const me = /^(\d{2}):(\d{2})(?::(\d{2}))?$/, ct = /YYYY|MM|DD|HH|mm|ss/g, L = (t, e = 2) => String(t).padStart(e, "0");
476
+ function Nt(t, e) {
477
477
  let r;
478
- const n = typeof t == "string" ? pe.exec(t.trim()) : null;
478
+ const n = typeof t == "string" ? me.exec(t.trim()) : null;
479
479
  if (n)
480
480
  r = { HH: n[1], mm: n[2], ss: n[3] ?? "00" };
481
481
  else {
482
- const a = typeof t == "string" ? Wt(t, "dateTime") : t;
482
+ const a = typeof t == "string" ? zt(t, "dateTime") : t;
483
483
  a instanceof Date && (r = {
484
- YYYY: C(a.getUTCFullYear(), 4),
485
- MM: C(a.getUTCMonth() + 1),
486
- DD: C(a.getUTCDate()),
487
- HH: C(a.getUTCHours()),
488
- mm: C(a.getUTCMinutes()),
489
- ss: C(a.getUTCSeconds())
484
+ YYYY: L(a.getUTCFullYear(), 4),
485
+ MM: L(a.getUTCMonth() + 1),
486
+ DD: L(a.getUTCDate()),
487
+ HH: L(a.getUTCHours()),
488
+ mm: L(a.getUTCMinutes()),
489
+ ss: L(a.getUTCSeconds())
490
490
  });
491
491
  }
492
492
  if (!r) return t;
493
493
  const s = r;
494
- return (e.match(ut) ?? []).some((a) => s[a] === void 0) ? t : e.replace(ut, (a) => s[a]);
494
+ return (e.match(ct) ?? []).some((a) => s[a] === void 0) ? t : e.replace(ct, (a) => s[a]);
495
495
  }
496
- function Ot(t, e) {
496
+ function St(t, e) {
497
497
  const r = /* @__PURE__ */ new Map(), n = (s) => {
498
- const o = /* @__PURE__ */ new Map();
499
- U(s).forEach((u) => o.set(ft(u), u.table));
498
+ const i = /* @__PURE__ */ new Map();
499
+ d(s).forEach((u) => i.set(lt(u), u.table));
500
500
  const a = (u) => {
501
501
  if ((u == null ? void 0 : u.type) !== "field" || !u.table || !u.column) return;
502
- const f = o.has(u.table) ? o.get(u.table) : u.table, l = f === void 0 ? void 0 : Ht(e, f), $ = l == null ? void 0 : l.columns.find((p) => p.name === u.column);
502
+ const f = i.has(u.table) ? i.get(u.table) : u.table, l = f === void 0 ? void 0 : Pt(e, f), $ = l == null ? void 0 : l.columns.find((y) => y.name === u.column);
503
503
  return $ == null ? void 0 : $.format;
504
- }, i = (u, f) => {
504
+ }, o = (u, f) => {
505
505
  switch (u.type) {
506
506
  case "literal":
507
- return { ...u, value: ht(u.value, f) };
507
+ return { ...u, value: Nt(u.value, f) };
508
508
  case "list":
509
- return { ...u, items: u.items.map((l) => i(l, f)) };
509
+ return { ...u, items: u.items.map((l) => o(l, f)) };
510
510
  case "param":
511
511
  return r.has(u.name) || r.set(u.name, f), u;
512
512
  default:
@@ -514,25 +514,25 @@ function Ot(t, e) {
514
514
  }
515
515
  }, c = (u) => {
516
516
  if (u.type === "subquery") return { ...u, query: n(u.query) };
517
- const f = K(u, c);
517
+ const f = z(u, c);
518
518
  if (f.type !== "binary" || !f.right) return f;
519
519
  const l = a(f.left), $ = a(f.right);
520
520
  return {
521
521
  ...f,
522
- left: $ ? i(f.left, $) : f.left,
523
- right: l ? i(f.right, l) : f.right
522
+ left: $ ? o(f.left, $) : f.left,
523
+ right: l ? o(f.right, l) : f.right
524
524
  };
525
525
  };
526
- return lt(s, c, (u) => n(u));
526
+ return $t(s, c, (u) => n(u));
527
527
  };
528
528
  return { query: n(t), params: r };
529
529
  }
530
- const We = ["mysql", "mssql", "postgres"], Ke = {
530
+ const Ze = ["mysql", "mssql", "postgres"], tr = {
531
531
  mysql: "MySQL",
532
532
  mssql: "SQL Server",
533
533
  postgres: "PostgreSQL"
534
534
  };
535
- function D(t, e) {
535
+ function h(t, e) {
536
536
  const r = t.replace(/[\uE000-\uE003]/g, "");
537
537
  switch (e) {
538
538
  case "mysql":
@@ -545,36 +545,36 @@ function D(t, e) {
545
545
  return `"${r.replace(/"/g, '""')}"`;
546
546
  }
547
547
  }
548
- const Te = /^[A-Za-z_][A-Za-z0-9_]*$/, Dt = (t) => `${t}`, Ae = /\uE000(\d+)\uE000/g;
549
- let I;
550
- function ze(t, e) {
551
- I = { ...t, writing: [] };
548
+ const ge = /^[A-Za-z_][A-Za-z0-9_]*$/, ht = (t) => `${t}`, Oe = /\uE000(\d+)\uE000/g;
549
+ let w;
550
+ function er(t, e) {
551
+ w = { ...t, writing: [] };
552
552
  try {
553
553
  return e();
554
554
  } finally {
555
- I = void 0;
555
+ w = void 0;
556
556
  }
557
557
  }
558
- function Nt(t, e, r) {
558
+ function Dt(t, e, r) {
559
559
  if (typeof t != "object" || t === null || t instanceof Date) return;
560
560
  let n;
561
- return Object.entries(t).some(([s, o]) => {
561
+ return Object.entries(t).some(([s, i]) => {
562
562
  const a = [...r, Array.isArray(t) ? Number(s) : s];
563
- return n = o === e ? a : Nt(o, e, a), n !== void 0;
563
+ return n = i === e ? a : Dt(i, e, a), n !== void 0;
564
564
  }), n;
565
565
  }
566
- function T(t, e) {
567
- if (!I) return e();
568
- const { writing: r, paths: n } = I, s = r[r.length - 1], o = typeof t == "string" ? [t] : Nt(s.value, t, s.path);
569
- if (!o) return e();
570
- r.push({ value: t, path: o });
566
+ function A(t, e) {
567
+ if (!w) return e();
568
+ const { writing: r, paths: n } = w, s = r[r.length - 1], i = typeof t == "string" ? [t] : Dt(s.value, t, s.path);
569
+ if (!i) return e();
570
+ r.push({ value: t, path: i });
571
571
  try {
572
- return `${n.push(o) - 1}${e()}`;
572
+ return `${n.push(i) - 1}${e()}`;
573
573
  } finally {
574
574
  r.pop();
575
575
  }
576
576
  }
577
- const me = {
577
+ const Ne = {
578
578
  mysql: {
579
579
  text: "CHAR",
580
580
  integer: "SIGNED",
@@ -603,39 +603,52 @@ const me = {
603
603
  boolean: "boolean"
604
604
  }
605
605
  };
606
- function ge(t, e) {
606
+ function Se(t, e) {
607
607
  var n;
608
- const r = (n = me[e]) == null ? void 0 : n[t];
608
+ const r = (n = Ne[e]) == null ? void 0 : n[t];
609
609
  return r ? e === "postgres" ? `NULL::${r}` : `CAST(NULL AS ${r})` : "NULL";
610
610
  }
611
- function St(t, e, r) {
611
+ function It(t, e, r) {
612
612
  if (t == null) return "NULL";
613
613
  if (r)
614
- return r.values.push(t), Dt(r.values.length - 1);
614
+ return r.values.push(t), ht(r.values.length - 1);
615
615
  if (typeof t == "boolean")
616
616
  return e === "mssql" ? t ? "1" : "0" : t ? "TRUE" : "FALSE";
617
617
  if (typeof t == "string") {
618
- const n = I ? t.replace(/[\uE001-\uE003]/g, "") : t;
618
+ const n = w ? t.replace(/[\uE001-\uE003]/g, "") : t;
619
619
  return n === "" ? "''" : `'${n.replace(/'/g, "''")}'`;
620
620
  }
621
- return typeof t == "number" || typeof t == "bigint" ? String(t) : t instanceof Date ? `'${Et(t)}'` : String(t);
621
+ return typeof t == "number" || typeof t == "bigint" ? String(t) : t instanceof Date ? `'${yt(t)}'` : String(t);
622
+ }
623
+ let H = "native";
624
+ function bt(t, e) {
625
+ H = t ?? "native";
626
+ try {
627
+ return e();
628
+ } finally {
629
+ H = "native";
630
+ }
622
631
  }
623
632
  function he(t, e) {
624
633
  switch (t) {
625
634
  case "!=":
626
635
  return e === "mssql" ? "<>" : "!=";
636
+ case "IS NOT DISTINCT FROM":
637
+ return e === "mysql" ? "<=>" : t;
627
638
  case "REGEXP":
628
639
  if (e === "postgres") return "~";
629
640
  if (e === "mssql") throw new Error("REGEXP isn't available in SQL Server");
630
641
  return "REGEXP";
631
642
  default:
643
+ if (!ee.includes(t))
644
+ throw new Error(`unknown operator ${JSON.stringify(t)}`);
632
645
  return t;
633
646
  }
634
647
  }
635
- function b(t) {
648
+ function R(t) {
636
649
  return (t == null ? void 0 : t.type) === "literal" && typeof t.value == "string" && /^[A-Za-z_]+$/.test(t.value) ? t.value.toUpperCase() : "DAY";
637
650
  }
638
- const x = {
651
+ const k = {
639
652
  YEAR: "year",
640
653
  QUARTER: "quarter",
641
654
  MONTH: "month",
@@ -645,43 +658,43 @@ const x = {
645
658
  MINUTE: "minute",
646
659
  SECOND: "second",
647
660
  MILLISECOND: "millisecond"
648
- }, Oe = {
661
+ }, De = {
649
662
  // ---- Date arithmetic ----
650
663
  DATE_ADD: ([t, e], r, n, s) => {
651
664
  var a;
652
- const o = b(r[2]);
665
+ const i = R(r[2]);
653
666
  switch (n) {
654
667
  case "mysql":
655
- return o === "MILLISECOND" ? `DATE_ADD(${t}, INTERVAL (${e}) * 1000 MICROSECOND)` : `DATE_ADD(${t}, INTERVAL ${e} ${o})`;
668
+ return i === "MILLISECOND" ? `DATE_ADD(${t}, INTERVAL (${e}) * 1000 MICROSECOND)` : `DATE_ADD(${t}, INTERVAL ${e} ${i})`;
656
669
  case "postgres": {
657
- const i = x[o] ?? o.toLowerCase();
658
- return !s && ((a = r[1]) == null ? void 0 : a.type) === "literal" && typeof r[1].value == "number" ? `(${t} + INTERVAL '${r[1].value} ${i}')` : `(${t} + (${e} * INTERVAL '1 ${i}'))`;
670
+ const o = k[i] ?? i.toLowerCase();
671
+ 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}'))`;
659
672
  }
660
673
  case "mssql":
661
- return `DATEADD(${o}, ${e}, ${t})`;
674
+ return `DATEADD(${i}, ${e}, ${t})`;
662
675
  default:
663
- return `DATE_ADD(${t}, ${e}, ${b(r[2])})`;
676
+ return `DATE_ADD(${t}, ${e}, ${R(r[2])})`;
664
677
  }
665
678
  },
666
679
  DATE_SUB: ([t, e], r, n, s) => {
667
680
  var a;
668
- const o = b(r[2]);
681
+ const i = R(r[2]);
669
682
  switch (n) {
670
683
  case "mysql":
671
- return o === "MILLISECOND" ? `DATE_SUB(${t}, INTERVAL (${e}) * 1000 MICROSECOND)` : `DATE_SUB(${t}, INTERVAL ${e} ${o})`;
684
+ return i === "MILLISECOND" ? `DATE_SUB(${t}, INTERVAL (${e}) * 1000 MICROSECOND)` : `DATE_SUB(${t}, INTERVAL ${e} ${i})`;
672
685
  case "postgres": {
673
- const i = x[o] ?? o.toLowerCase();
674
- return !s && ((a = r[1]) == null ? void 0 : a.type) === "literal" && typeof r[1].value == "number" ? `(${t} - INTERVAL '${r[1].value} ${i}')` : `(${t} - (${e} * INTERVAL '1 ${i}'))`;
686
+ const o = k[i] ?? i.toLowerCase();
687
+ 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}'))`;
675
688
  }
676
689
  case "mssql":
677
- return `DATEADD(${o}, -(${e}), ${t})`;
690
+ return `DATEADD(${i}, -(${e}), ${t})`;
678
691
  default:
679
- return `DATE_SUB(${t}, ${e}, ${b(r[2])})`;
692
+ return `DATE_SUB(${t}, ${e}, ${R(r[2])})`;
680
693
  }
681
694
  },
682
695
  // ---- Date diff ----
683
696
  DATEDIFF: ([t, e], r, n) => {
684
- const s = b(r[2]);
697
+ const s = R(r[2]);
685
698
  switch (n) {
686
699
  case "mysql":
687
700
  return `TIMESTAMPDIFF(${s}, ${t}, ${e})`;
@@ -692,12 +705,12 @@ const x = {
692
705
  case "YEAR":
693
706
  return `EXTRACT(YEAR FROM age(${e}, ${t}))::integer`;
694
707
  case "QUARTER": {
695
- const o = `EXTRACT(YEAR FROM age(${e}, ${t}))`, a = `EXTRACT(MONTH FROM age(${e}, ${t}))`;
696
- return `FLOOR((${o} * 12 + ${a}) / 3)::integer`;
708
+ const i = `EXTRACT(YEAR FROM age(${e}, ${t}))`, a = `EXTRACT(MONTH FROM age(${e}, ${t}))`;
709
+ return `FLOOR((${i} * 12 + ${a}) / 3)::integer`;
697
710
  }
698
711
  case "MONTH": {
699
- const o = `EXTRACT(YEAR FROM age(${e}, ${t}))`, a = `EXTRACT(MONTH FROM age(${e}, ${t}))`;
700
- return `(${o} * 12 + ${a})::integer`;
712
+ const i = `EXTRACT(YEAR FROM age(${e}, ${t}))`, a = `EXTRACT(MONTH FROM age(${e}, ${t}))`;
713
+ return `(${i} * 12 + ${a})::integer`;
701
714
  }
702
715
  case "WEEK":
703
716
  return `FLOOR((${e}::date - ${t}::date) / 7)`;
@@ -720,7 +733,7 @@ const x = {
720
733
  },
721
734
  // ---- Date extract ----
722
735
  EXTRACT: ([t], e, r) => {
723
- const n = b(e[1]);
736
+ const n = R(e[1]);
724
737
  switch (r) {
725
738
  case "mysql":
726
739
  return n === "MILLISECOND" ? `FLOOR(MICROSECOND(${t}) / 1000)` : `EXTRACT(${n} FROM ${t})`;
@@ -734,7 +747,7 @@ const x = {
734
747
  },
735
748
  // ---- Date truncation ----
736
749
  DATE_TRUNC: ([t], e, r) => {
737
- const n = b(e[1]);
750
+ const n = R(e[1]);
738
751
  switch (r) {
739
752
  case "mysql":
740
753
  switch (n) {
@@ -760,7 +773,7 @@ const x = {
760
773
  return `/* unknown unit: ${n} */`;
761
774
  }
762
775
  case "postgres":
763
- return `DATE_TRUNC('${x[n] ?? n.toLowerCase()}', ${t})`;
776
+ return `DATE_TRUNC('${k[n] ?? n.toLowerCase()}', ${t})`;
764
777
  case "mssql":
765
778
  switch (n) {
766
779
  case "YEAR":
@@ -804,72 +817,72 @@ const x = {
804
817
  // ---- String aggregation (issue 137) ----
805
818
  // The separator goes into the SQL as a literal even in parameterized SQL:
806
819
  // MySQL's SEPARATOR takes nothing else.
807
- STRING_AGG: ([t = "??"], e, r, n, s, o) => {
808
- const a = e[1] ?? { type: "literal", value: ", " }, i = a.type === "literal" && typeof a.value == "string" ? St(a.value, r) : "??", c = s.distinct ? "DISTINCT " : "", u = ($) => {
809
- var p;
810
- return (p = s.orderBy) != null && p.length ? `ORDER BY ${s.orderBy.map((g) => `${$(g)} ${g.direction === "desc" ? "DESC" : "ASC"}`).join(", ")}` : "";
811
- }, f = ($) => o($.expression), l = ($) => $ ? ` ${$}` : "";
820
+ STRING_AGG: ([t = "??"], e, r, n, s, i) => {
821
+ const a = e[1] ?? { type: "literal", value: ", " }, o = a.type === "literal" && typeof a.value == "string" ? It(a.value, r) : "??", c = s.distinct ? "DISTINCT " : "", u = ($) => {
822
+ var y;
823
+ return (y = s.orderBy) != null && y.length ? `ORDER BY ${s.orderBy.map((p) => `${$(p)} ${p.direction === "desc" ? "DESC" : "ASC"}`).join(", ")}` : "";
824
+ }, f = ($) => i($.expression), l = ($) => $ ? ` ${$}` : "";
812
825
  switch (r) {
813
826
  case "mysql":
814
- return `GROUP_CONCAT(${c}${t}${l(u(f))} SEPARATOR ${i})`;
827
+ return `GROUP_CONCAT(${c}${t}${l(u(f))} SEPARATOR ${o})`;
815
828
  case "postgres": {
816
- const $ = `CAST(${t} AS TEXT)`, p = u((g) => {
817
- if (!s.distinct) return f(g);
818
- if (B(g.expression, e[0])) return $;
829
+ const $ = `CAST(${t} AS TEXT)`, y = u((p) => {
830
+ if (!s.distinct) return f(p);
831
+ if (B(p.expression, e[0])) return $;
819
832
  throw new Error("PostgreSQL orders STRING_AGG of different values by the value only");
820
833
  });
821
- return `STRING_AGG(${c}${$}, ${i}${l(p)})`;
834
+ return `STRING_AGG(${c}${$}, ${o}${l(y)})`;
822
835
  }
823
836
  case "mssql": {
824
837
  if (s.distinct)
825
838
  throw new Error("SQL Server's STRING_AGG can't join only different values");
826
- const $ = u(f), p = $ ? ` WITHIN GROUP (${$})` : "";
827
- return `STRING_AGG(CAST(${t} AS NVARCHAR(MAX)), ${i})${p}`;
839
+ const $ = u(f), y = $ ? ` WITHIN GROUP (${$})` : "";
840
+ return `STRING_AGG(CAST(${t} AS NVARCHAR(MAX)), ${o})${y}`;
828
841
  }
829
842
  default:
830
- return `STRING_AGG(${c}${t}, ${i}${l(u(f))})`;
843
+ return `STRING_AGG(${c}${t}, ${o}${l(u(f))})`;
831
844
  }
832
845
  }
833
846
  };
834
- function d(t) {
847
+ function S(t) {
835
848
  return t.type === "binary" || t.type === "logical" || t.type === "if" || t.type === "case";
836
849
  }
837
850
  function E(t, e, r, n) {
838
- return T(t, () => De(t, e, r, n));
851
+ return A(t, () => Ie(t, e, r, n));
839
852
  }
840
- function ct(t, e, r, n) {
841
- const s = (o) => {
842
- if (!o) return "??";
843
- const a = E(o, e, r, n);
844
- return d(o) ? `(${a})` : a;
853
+ function ft(t, e, r, n) {
854
+ const s = (i) => {
855
+ if (!i) return "??";
856
+ const a = E(i, e, r, n);
857
+ return S(i) ? `(${a})` : a;
845
858
  };
846
- return T(t, () => {
847
- const [o, a] = t.type === "list" ? t.items : [t];
848
- return `${s(o)} AND ${s(a)}`;
859
+ return A(t, () => {
860
+ const [i, a] = t.type === "list" ? t.items : [t];
861
+ return `${s(i)} AND ${s(a)}`;
849
862
  });
850
863
  }
851
- function De(t, e, r, n) {
864
+ function Ie(t, e, r, n) {
852
865
  var a;
853
- const s = " ".repeat(r), o = " ".repeat(r + 1);
866
+ const s = " ".repeat(r), i = " ".repeat(r + 1);
854
867
  switch (t.type) {
855
868
  case "literal":
856
- return t.value === "" && !t.empty ? "??" : t.value === null && t.as ? ge(t.as, e) : St(t.value, e, n);
869
+ return t.value === "" && !t.empty ? "??" : t.value === null && t.as ? Se(t.as, e) : It(t.value, e, n);
857
870
  case "param": {
858
- if (!n) return Te.test(t.name) ? `:${t.name}` : "??";
871
+ if (!n) return ge.test(t.name) ? `:${t.name}` : "??";
859
872
  if (!n.named.has(t.name))
860
873
  throw new Error(`no value for parameter "${t.name}"`);
861
- return n.values.push(n.named.get(t.name)), Dt(n.values.length - 1);
874
+ return n.values.push(n.named.get(t.name)), ht(n.values.length - 1);
862
875
  }
863
876
  case "field": {
864
877
  if (!t.column) return "??";
865
- const i = D(t.column, e);
866
- return t.table ? `${D(t.table, e)}.${i}` : i;
878
+ const o = h(t.column, e);
879
+ return t.table ? `${h(t.table, e)}.${o}` : o;
867
880
  }
868
881
  case "function": {
869
- const i = t.args.map((l) => E(l, e, r, n)), c = Oe[t.name.toUpperCase()];
882
+ const o = t.args.map((l) => E(l, e, r, n)), c = De[t.name.toUpperCase()];
870
883
  if (c)
871
884
  return c(
872
- i,
885
+ o,
873
886
  t.args,
874
887
  e,
875
888
  n !== void 0,
@@ -877,218 +890,232 @@ function De(t, e, r, n) {
877
890
  (l) => E(l, e, r, n)
878
891
  );
879
892
  const u = /^[A-Za-z_][A-Za-z0-9_.]*$/.test(t.name) || t.name === "" ? t.name : "??";
880
- if (i.length === 0 && u.toUpperCase() === "COUNT") return `${u}(*)`;
881
- const f = t.distinct && i.length > 0 ? "DISTINCT " : "";
882
- return `${u}(${f}${i.join(", ")})`;
893
+ if (o.length === 0 && u.toUpperCase() === "COUNT") return `${u}(*)`;
894
+ const f = t.distinct && o.length > 0 ? "DISTINCT " : "";
895
+ return `${u}(${f}${o.join(", ")})`;
883
896
  }
884
897
  case "binary": {
885
- const i = t.operator ? he(t.operator, e) : "=", c = E(t.left, e, r, n), u = d(t.left) ? `(${c})` : c;
886
- if (zt(t.operator)) return `${u} ${i}`;
887
- if (!t.right) return `${u} ${i} ??`;
888
- if (i === "BETWEEN")
889
- return `${u} BETWEEN ${ct(t.right, e, r, n)}`;
890
- if (i === "IN")
898
+ const o = t.operator ? he(t.operator, e) : "=", c = E(t.left, e, r, n), u = S(t.left) ? `(${c})` : c;
899
+ if (te(t.operator)) return `${u} ${o}`;
900
+ if (!t.right) return `${u} ${o} ??`;
901
+ if (o === "BETWEEN")
902
+ return `${u} BETWEEN ${ft(t.right, e, r, n)}`;
903
+ if (o === "IN")
891
904
  return t.right.type === "list" ? `${u} IN ${E(t.right, e, r, n)}` : `${u} IN (${E(t.right, e, r, n)})`;
892
- const f = E(t.right, e, r, n), l = d(t.right) ? `(${f})` : f;
893
- return `${u} ${i} ${l}`;
905
+ const f = E(t.right, e, r, n), l = S(t.right) ? `(${f})` : f;
906
+ if (t.operator === "IS NOT DISTINCT FROM" && H === "expanded") {
907
+ const $ = (p) => {
908
+ const D = E(p, e, r, n);
909
+ return S(p) ? `(${D})` : D;
910
+ }, y = `${$(t.left)} IS NULL AND ${$(t.right)} IS NULL`;
911
+ return `(${u} = ${l} OR (${y}))`;
912
+ }
913
+ return `${u} ${o} ${l}`;
894
914
  }
895
915
  case "logical": {
896
916
  if (t.operands.length === 0) return "/* empty */";
897
917
  if (t.operands.length === 1)
898
918
  return E(t.operands[0], e, r, n);
899
- const i = `
919
+ if (t.operator !== "AND" && t.operator !== "OR")
920
+ throw new Error(`unknown operator ${JSON.stringify(t.operator)}`);
921
+ const o = `
900
922
  ${s}${t.operator} `;
901
923
  return t.operands.map((c) => {
902
924
  if (c.type === "logical") {
903
925
  const u = E(c, e, r + 1, n);
904
926
  return `(
905
- ${o}${u}
927
+ ${i}${u}
906
928
  ${s})`;
907
929
  }
908
930
  return E(c, e, r, n);
909
- }).join(i);
931
+ }).join(o);
910
932
  }
911
933
  case "not": {
912
- const { operand: i } = t;
913
- if (i.type === "binary" && i.operator === "IN" && ((a = i.right) == null ? void 0 : a.type) === "subquery") {
914
- const c = E(i.left, e, r, n);
915
- return `${d(i.left) ? `(${c})` : c} NOT IN (${E(i.right, e, r, n)})`;
934
+ const { operand: o } = t;
935
+ if (o.type === "binary" && o.operator === "IN" && ((a = o.right) == null ? void 0 : a.type) === "subquery") {
936
+ const c = E(o.left, e, r, n);
937
+ return `${S(o.left) ? `(${c})` : c} NOT IN (${E(o.right, e, r, n)})`;
938
+ }
939
+ if (o.type === "binary" && o.operator === "BETWEEN" && o.right) {
940
+ const c = E(o.left, e, r, n);
941
+ return `${S(o.left) ? `(${c})` : c} NOT BETWEEN ${ft(o.right, e, r, n)}`;
916
942
  }
917
- if (i.type === "binary" && i.operator === "BETWEEN" && i.right) {
918
- const c = E(i.left, e, r, n);
919
- return `${d(i.left) ? `(${c})` : c} NOT BETWEEN ${ct(i.right, e, r, n)}`;
943
+ if (o.type === "binary" && o.operator === "IS NOT DISTINCT FROM" && o.right && H === "native") {
944
+ if (e === "mysql") return `NOT (${E(o, e, r, n)})`;
945
+ const c = E(o.left, e, r, n), u = S(o.left) ? `(${c})` : c, f = E(o.right, e, r, n), l = S(o.right) ? `(${f})` : f;
946
+ return `${u} IS DISTINCT FROM ${l}`;
920
947
  }
921
- if (i.type === "logical" || i.type === "not") {
922
- const c = E(i, e, r + 1, n);
948
+ if (o.type === "logical" || o.type === "not") {
949
+ const c = E(o, e, r + 1, n);
923
950
  return `NOT (
924
- ${o}${c}
951
+ ${i}${c}
925
952
  ${s})`;
926
953
  }
927
- return `NOT ${E(i, e, r, n)}`;
954
+ return `NOT ${E(o, e, r, n)}`;
928
955
  }
929
956
  case "if": {
930
- const i = 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";
931
- return e === "mysql" ? `IF(${i}, ${c}, ${u})` : e === "mssql" ? `IIF(${i}, ${c}, ${u})` : `CASE WHEN ${i} THEN ${c} ELSE ${u} END`;
957
+ 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";
958
+ return e === "mysql" ? `IF(${o}, ${c}, ${u})` : e === "mssql" ? `IIF(${o}, ${c}, ${u})` : `CASE WHEN ${o} THEN ${c} ELSE ${u} END`;
932
959
  }
933
960
  case "case": {
934
- const i = t.subject ? ` ${E(t.subject, e, r, n)}` : "", c = t.branches.map((f) => {
961
+ const o = t.subject ? ` ${E(t.subject, e, r, n)}` : "", c = t.branches.map((f) => {
935
962
  const l = E(f.when, e, r + 1, n), $ = E(f.then, e, r + 1, n);
936
- return `${o}WHEN ${l} THEN ${$}`;
963
+ return `${i}WHEN ${l} THEN ${$}`;
937
964
  }).join(`
938
965
  `), u = t.else ? `
939
- ${o}ELSE ${E(t.else, e, r + 1, n)}` : "";
940
- return `CASE${i}
966
+ ${i}ELSE ${E(t.else, e, r + 1, n)}` : "";
967
+ return `CASE${o}
941
968
  ${c}${u}
942
969
  ${s}END`;
943
970
  }
944
971
  // EXISTS (SELECT 1 FROM … WHERE <link> AND <where>): the subquery written
945
972
  // in place, one level in, so its params fall in reading order.
946
973
  case "exists": {
947
- const { query: i, link: c } = t, u = ($) => $.type === "logical" && $.operator === "AND" ? $.operands : [$], f = i.where ? { type: "logical", operator: "AND", operands: [...u(c), ...u(i.where)] } : c;
974
+ 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;
948
975
  return `EXISTS (
949
976
  ${[
950
977
  "SELECT 1",
951
978
  // The clauses below write their expressions with formatNode.
952
979
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
953
- wt(i.from, e),
980
+ Mt(o.from, e),
954
981
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
955
- ...i.joins.map(($) => Lt($, e, n)),
982
+ ...o.joins.map(($) => Ft($, e, n)),
956
983
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
957
- bt(f, e, n)
984
+ Rt(f, e, n)
958
985
  ].join(`
959
- `).replace(/^/gm, o)}
986
+ `).replace(/^/gm, i)}
960
987
  ${s})`;
961
988
  }
962
989
  // IN's right side (issue 122): its query, on lines of its own, inside the
963
990
  // parentheses IN writes around it.
964
991
  // An empty list is unfinished, like a value not filled in.
965
992
  case "list":
966
- return `(${t.items.map((i) => E(i, e, r, n)).join(", ") || "??"})`;
993
+ return `(${t.items.map((o) => E(o, e, r, n)).join(", ") || "??"})`;
967
994
  case "subquery":
968
995
  return `
969
- ${z(t.query, e, n).join(`
970
- `).replace(/^/gm, o)}
996
+ ${Z(t.query, e, n).join(`
997
+ `).replace(/^/gm, i)}
971
998
  ${s}`;
972
999
  default:
973
1000
  return "/* unknown */";
974
1001
  }
975
1002
  }
976
- function bt(t, e, r) {
1003
+ function Rt(t, e, r) {
977
1004
  return `WHERE
978
1005
  ${E(t, e, 1, r)}`;
979
1006
  }
980
- const Ne = {
1007
+ const be = {
981
1008
  inner: "INNER JOIN",
982
1009
  left: "LEFT JOIN",
983
1010
  right: "RIGHT JOIN",
984
1011
  full: "FULL JOIN",
985
1012
  cross: "CROSS JOIN"
986
1013
  };
987
- let Rt = /* @__PURE__ */ new Map();
988
- function It(t, e, r) {
989
- return t.shared !== void 0 ? D(Rt.get(t.shared) ?? "??", e) : t.query ? `(
990
- ${Ct(t.query, e, r).replace(/^/gm, " ")}
991
- )` : D(t.table, e);
1014
+ let Ct = /* @__PURE__ */ new Map();
1015
+ function wt(t, e, r) {
1016
+ return t.shared !== void 0 ? h(Ct.get(t.shared) ?? "??", e) : t.query ? `(
1017
+ ${Lt(t.query, e, r).replace(/^/gm, " ")}
1018
+ )` : h(t.table, e);
992
1019
  }
993
- function Ct(t, e, r) {
994
- return Yt(t).map((n) => z(n, e, r).join(`
995
- `)).join(Pt(t) && !t.all ? `
1020
+ function Lt(t, e, r) {
1021
+ return Gt(t).map((n) => Z(n, e, r).join(`
1022
+ `)).join(xt(t) && !t.all ? `
996
1023
  UNION
997
1024
  ` : `
998
1025
  UNION ALL
999
1026
  `);
1000
1027
  }
1001
- function Se(t, e, r) {
1028
+ function Re(t, e, r) {
1002
1029
  return t.length === 0 ? [] : [`WITH
1003
- ${t.map((s) => T(s, () => {
1004
- const o = Ct(s.query, e, r).replace(/^/gm, " ");
1005
- return ` ${D(s.name, e)} AS (
1006
- ${o}
1030
+ ${t.map((s) => A(s, () => {
1031
+ const i = Lt(s.query, e, r).replace(/^/gm, " ");
1032
+ return ` ${h(s.name, e)} AS (
1033
+ ${i}
1007
1034
  )`;
1008
1035
  })).join(`,
1009
1036
  `)}`];
1010
1037
  }
1011
- function wt(t, e, r) {
1012
- const n = t.alias ? ` AS ${D(t.alias, e)}` : "";
1013
- return `FROM ${T(t, () => `${It(t, e, r)}${n}`)}`;
1038
+ function Mt(t, e, r) {
1039
+ const n = t.alias ? ` AS ${h(t.alias, e)}` : "";
1040
+ return `FROM ${A(t, () => `${wt(t, e, r)}${n}`)}`;
1014
1041
  }
1015
- function Lt(t, e, r) {
1016
- return T(t, () => {
1017
- const n = It(t, e, r), s = t.alias ? ` AS ${D(t.alias, e)}` : "";
1042
+ function Ft(t, e, r) {
1043
+ return A(t, () => {
1044
+ const n = wt(t, e, r), s = t.alias ? ` AS ${h(t.alias, e)}` : "";
1018
1045
  if (t.joinType === "cross") return `CROSS JOIN ${n}${s}`;
1019
- const o = t.on ? E(t.on, e, 1, r) : "/* unconfigured */";
1020
- return `${Ne[t.joinType]} ${n}${s}
1021
- ON ${o}`;
1046
+ const i = t.on ? E(t.on, e, 1, r) : "/* unconfigured */";
1047
+ return `${be[t.joinType]} ${n}${s}
1048
+ ON ${i}`;
1022
1049
  });
1023
1050
  }
1024
- function be(t, e, r, n = "SELECT") {
1025
- if (t.length === 0) return `${n} ${T(t, () => "*")}`;
1026
- const s = t.map((o) => T(o, () => {
1027
- const a = E(o.expression, e, 1, r), i = o.alias ? ` AS ${D(o.alias, e)}` : "";
1028
- return `${a}${i}`;
1051
+ function Ce(t, e, r, n = "SELECT") {
1052
+ if (t.length === 0) return `${n} ${A(t, () => "*")}`;
1053
+ const s = t.map((i) => A(i, () => {
1054
+ const a = E(i.expression, e, 1, r), o = i.alias ? ` AS ${h(i.alias, e)}` : "";
1055
+ return `${a}${o}`;
1029
1056
  }));
1030
1057
  return `${n}
1031
1058
  ${s.join(`,
1032
1059
  `)}`;
1033
1060
  }
1034
- function Re(t, e, r) {
1061
+ function we(t, e, r) {
1035
1062
  return `GROUP BY
1036
- ${t.map((s) => T(
1063
+ ${t.map((s) => A(
1037
1064
  s,
1038
1065
  () => E(s.expression, e, 1, r)
1039
1066
  )).join(`,
1040
1067
  `)}`;
1041
1068
  }
1042
- function Ie(t, e, r) {
1069
+ function Le(t, e, r) {
1043
1070
  return `HAVING
1044
1071
  ${E(t, e, 1, r)}`;
1045
1072
  }
1046
- function Ce(t, e, r, n) {
1073
+ function Me(t, e, r, n) {
1047
1074
  return `ORDER BY
1048
- ${t.map((o) => T(o, () => `${E(n(o.expression), e, 1, r)} ${o.direction === "desc" ? "DESC" : "ASC"}`)).join(`,
1075
+ ${t.map((i) => A(i, () => `${E(n(i.expression), e, 1, r)} ${i.direction === "desc" ? "DESC" : "ASC"}`)).join(`,
1049
1076
  `)}`;
1050
1077
  }
1051
- const W = (t) => t !== void 0 && Number.isInteger(t) && t >= 0 ? t : void 0;
1052
- function Mt(t, e) {
1053
- const r = e.resolveName ? ye(t, e.resolveName) : t;
1054
- return e.defaultLimit !== void 0 && W(r.limit) === void 0 ? { ...r, limit: e.defaultLimit } : r;
1078
+ const K = (t) => t !== void 0 && Number.isInteger(t) && t >= 0 ? t : void 0;
1079
+ function qt(t, e) {
1080
+ const r = e.resolveName ? Ae(t, e.resolveName) : t;
1081
+ return e.defaultLimit !== void 0 && K(r.limit) === void 0 ? { ...r, limit: e.defaultLimit } : r;
1055
1082
  }
1056
- const we = "18446744073709551615";
1057
- function z(t, e, r, n = `SELECT${t.distinct ? " DISTINCT" : ""}`) {
1083
+ const Fe = "18446744073709551615";
1084
+ function Z(t, e, r, n = `SELECT${t.distinct ? " DISTINCT" : ""}`) {
1058
1085
  const s = [
1059
- be(t.select, e, r, n),
1060
- wt(t.from, e, r),
1061
- ...t.joins.map((a) => Lt(a, e, r))
1086
+ Ce(t.select, e, r, n),
1087
+ Mt(t.from, e, r),
1088
+ ...t.joins.map((a) => Ft(a, e, r))
1062
1089
  ];
1063
- t.where && s.push(bt(t.where, e, r));
1064
- const o = t.groupBy ?? [];
1065
- if (o.length > 0 && s.push(Re(o, e, r)), t.having) {
1066
- const a = e === "mysql" ? $t(t.having, t) : t.having;
1067
- s.push(Ie(a, e, r));
1090
+ t.where && s.push(Rt(t.where, e, r));
1091
+ const i = t.groupBy ?? [];
1092
+ if (i.length > 0 && s.push(we(i, e, r)), t.having) {
1093
+ const a = e === "mysql" ? Et(t.having, t) : t.having;
1094
+ s.push(Le(a, e, r));
1068
1095
  }
1069
1096
  return s;
1070
1097
  }
1071
- function dt(t, e, r) {
1072
- I && (I.writing = [{ value: t, path: [] }]), Rt = new Map((t.shared ?? []).map((f) => [f.id, f.name]));
1073
- const n = Se(t.shared ?? [], e, r), s = W(t.limit), o = W(t.offset) || void 0, a = t.orderBy ?? [], i = e === "mssql" && s !== void 0 && o === void 0, c = `SELECT${t.distinct ? " DISTINCT" : ""}${i ? ` ${T("limit", () => `TOP (${s})`)}` : ""}`, u = z(t, e, r, c);
1074
- if (a.length > 0 && u.push(Ce(a, e, r, (f) => e === "mysql" ? $t(f, t) : f)), e === "mssql") {
1075
- if (o !== void 0) {
1098
+ function Ut(t, e, r) {
1099
+ w && (w.writing = [{ value: t, path: [] }]), Ct = new Map((t.shared ?? []).map((f) => [f.id, f.name]));
1100
+ const n = Re(t.shared ?? [], e, r), s = K(t.limit), i = K(t.offset) || void 0, a = t.orderBy ?? [], o = e === "mssql" && s !== void 0 && i === void 0, c = `SELECT${t.distinct ? " DISTINCT" : ""}${o ? ` ${A("limit", () => `TOP (${s})`)}` : ""}`, u = Z(t, e, r, c);
1101
+ if (a.length > 0 && u.push(Me(a, e, r, (f) => e === "mysql" ? Et(f, t) : f)), e === "mssql") {
1102
+ if (i !== void 0) {
1076
1103
  a.length === 0 && u.push("ORDER BY (SELECT NULL)");
1077
- const f = s !== void 0 ? ` ${T("limit", () => `FETCH NEXT ${s} ROWS ONLY`)}` : "";
1078
- u.push(`${T("offset", () => `OFFSET ${o} ROWS`)}${f}`);
1104
+ const f = s !== void 0 ? ` ${A("limit", () => `FETCH NEXT ${s} ROWS ONLY`)}` : "";
1105
+ u.push(`${A("offset", () => `OFFSET ${i} ROWS`)}${f}`);
1079
1106
  }
1080
- } else if (s !== void 0 || o !== void 0) {
1081
- const f = s !== void 0 || e === "mysql" ? T(s !== void 0 ? "limit" : {}, () => `LIMIT ${s ?? we}`) : "", l = o !== void 0 ? T("offset", () => `OFFSET ${o}`) : "";
1107
+ } else if (s !== void 0 || i !== void 0) {
1108
+ const f = s !== void 0 || e === "mysql" ? A(s !== void 0 ? "limit" : {}, () => `LIMIT ${s ?? Fe}`) : "", l = i !== void 0 ? A("offset", () => `OFFSET ${i}`) : "";
1082
1109
  u.push([f, l].filter(Boolean).join(" "));
1083
1110
  }
1084
1111
  return [...n, ...u].join(`
1085
1112
  `);
1086
1113
  }
1087
- function Ze(t, e, r = {}) {
1088
- const n = r.values ? Kt(t, r.values) : t, s = r.schema ? Ot(n, r.schema).query : n;
1089
- return dt(Mt(s, r), e);
1114
+ function rr(t, e, r = {}) {
1115
+ const n = r.values ? Zt(t, r.values) : t, s = r.schema ? St(n, r.schema).query : n, i = qt(s, r);
1116
+ return bt(r.nullSafeEqual, () => Ut(i, e));
1090
1117
  }
1091
- function Le(t, e) {
1118
+ function qe(t, e) {
1092
1119
  switch (t) {
1093
1120
  case "mysql":
1094
1121
  return "?";
@@ -1100,42 +1127,45 @@ function Le(t, e) {
1100
1127
  return "?";
1101
1128
  }
1102
1129
  }
1103
- function tr(t, e, r = {}) {
1104
- const n = r.schema ? Ot(t, r.schema) : { query: t, params: /* @__PURE__ */ new Map() }, s = {
1130
+ function nr(t, e, r = {}) {
1131
+ const n = r.schema ? St(t, r.schema) : { query: t, params: /* @__PURE__ */ new Map() }, s = {
1105
1132
  values: [],
1106
1133
  named: new Map(Object.entries(r.values ?? {}).map(([u, f]) => {
1107
1134
  const l = n.params.get(u);
1108
- return [u, l ? ht(f, l) : f];
1135
+ return [u, l ? Nt(f, l) : f];
1109
1136
  }))
1110
- }, o = Mt(n.query, r), a = dt(o, e, s), i = [];
1111
- return { sql: a.replace(Ae, (u, f) => {
1137
+ }, i = qt(n.query, r), a = bt(
1138
+ r.nullSafeEqual,
1139
+ () => Ut(i, e, s)
1140
+ ), o = [];
1141
+ return { sql: a.replace(Oe, (u, f) => {
1112
1142
  const l = s.values[Number(f)];
1113
- return i.push(l instanceof Date ? Et(l) : l), Le(e, i.length);
1114
- }), params: i };
1143
+ return o.push(l instanceof Date ? yt(l) : l), qe(e, o.length);
1144
+ }), params: o };
1115
1145
  }
1116
1146
  export {
1117
- We as D,
1118
- X as Q,
1119
- Zt as S,
1120
- tr as a,
1121
- Ze as b,
1122
- se as c,
1123
- Pe as d,
1124
- _e as e,
1125
- xe as f,
1126
- Ye as g,
1127
- at as h,
1128
- Xe as i,
1129
- je as j,
1130
- Qe as k,
1131
- Ge as l,
1132
- ke as m,
1133
- w as n,
1134
- Ve as o,
1135
- Je as p,
1136
- Ke as q,
1137
- He as r,
1138
- ve as s,
1139
- Be as u,
1140
- ze as w
1147
+ Ze as D,
1148
+ W as Q,
1149
+ re as S,
1150
+ nr as a,
1151
+ rr as b,
1152
+ ae as c,
1153
+ ke as d,
1154
+ Pe as e,
1155
+ Je as f,
1156
+ xe as g,
1157
+ ut as h,
1158
+ Ve as i,
1159
+ Ye as j,
1160
+ Xe as k,
1161
+ Qe as l,
1162
+ We as m,
1163
+ M as n,
1164
+ ze as o,
1165
+ Ke as p,
1166
+ tr as q,
1167
+ Ge as r,
1168
+ _e as s,
1169
+ He as u,
1170
+ er as w
1141
1171
  };