@nongt/visql-core 0.11.0 → 0.12.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,10 +1,10 @@
1
- var Lt = Object.defineProperty;
2
- var Mt = (t, r, e) => r in t ? Lt(t, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[r] = e;
3
- var w = (t, r, e) => Mt(t, typeof r != "symbol" ? r + "" : r, e);
4
- import { q as Y, d as F, a as wt, s as Ft, m as k, e as ot, g as Ut, t as qt, j as Yt, b as _t, i as jt } from "./schema-CzKt6cN2.js";
5
- import { p as K, t as Z, v as Ht, w as vt, x as Bt, y as it, z as Pt, A as Gt } from "./params-DVkfcD6D.js";
6
- import { s as ut } from "./calendarDate-CYlr9TvX.js";
7
- class xt extends Error {
1
+ var qt = Object.defineProperty;
2
+ var Ut = (t, r, e) => r in t ? qt(t, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[r] = e;
3
+ var w = (t, r, e) => Ut(t, typeof r != "symbol" ? r + "" : r, e);
4
+ import { q as Y, d as F, a as Yt, s as _t, m as W, e as at, g as jt, t as Ht, j as vt, b as Bt, i as Pt } from "./schema-DsMO9oFE.js";
5
+ import { p as d, t as tt, v as Gt, w as xt, x as Xt, y as ft, z as kt, A as Vt } from "./params-Btjlj45_.js";
6
+ import { s as $t } from "./calendarDate-CYlr9TvX.js";
7
+ class Wt extends Error {
8
8
  constructor(e, n, s, o) {
9
9
  const u = o ? `"${n}" (as "${o}")` : `"${n}"`;
10
10
  super(`scopeQuery: the query at ${e.length ? e.join(".") : "the top"} reads ${u} but can't reach "${s}", which the condition names`);
@@ -19,21 +19,21 @@ class xt extends Error {
19
19
  this.name = "ScopeError", this.path = e, this.table = n, o && (this.alias = o);
20
20
  }
21
21
  }
22
- const _ = (t) => Object.prototype.toString.call(t) === "[object Object]", ct = (t) => Array.isArray(t.joins) && _(t.from);
23
- function U(t, r, e) {
22
+ const _ = (t) => Object.prototype.toString.call(t) === "[object Object]", lt = (t) => Array.isArray(t.joins) && _(t.from);
23
+ function q(t, r, e) {
24
24
  if (Array.isArray(t)) {
25
- const u = t.map((i, a) => U(i, [...r, a], e));
25
+ const u = t.map((i, a) => q(i, [...r, a], e));
26
26
  return u.every((i, a) => i === t[a]) ? t : u;
27
27
  }
28
28
  if (!_(t)) return t;
29
29
  let n = !1;
30
30
  const s = Object.entries(t).map(([u, i]) => {
31
- const a = U(i, [...r, u], e);
31
+ const a = q(i, [...r, u], e);
32
32
  return a !== i && (n = !0), [u, a];
33
33
  }), o = n ? Object.fromEntries(s) : t;
34
- return ct(o) ? e(o, r) : o;
34
+ return lt(o) ? e(o, r) : o;
35
35
  }
36
- const at = (t, r) => Y(t).flatMap((e) => e.table !== void 0 && r.requires(e.table) ? [{ table: e.table, ...e.alias ? { alias: e.alias } : {}, ref: e.alias ?? e.table }] : []), ft = (t) => Y(t).flatMap((r) => {
36
+ const Et = (t, r) => Y(t).flatMap((e) => e.table !== void 0 && r.requires(e.table) ? [{ table: e.table, ...e.alias ? { alias: e.alias } : {}, ref: e.alias ?? e.table }] : []), Tt = (t) => Y(t).flatMap((r) => {
37
37
  const e = r.alias ?? r.table;
38
38
  return e === void 0 ? [] : [e];
39
39
  });
@@ -42,76 +42,76 @@ function P(t, r) {
42
42
  const e = t.table && r.get(t.table);
43
43
  return e ? { ...t, table: e } : t;
44
44
  }
45
- return k(t, (e) => P(e, r));
45
+ return W(t, (e) => P(e, r));
46
46
  }
47
- const $t = (t) => t ? t.type === "logical" && t.operator === "AND" ? t.operands : [t] : [], Xt = (t) => t.joinType === "inner" || t.joinType === "left";
48
- function kt(t, r, e, n, s) {
47
+ const At = (t) => t ? t.type === "logical" && t.operator === "AND" ? t.operands : [t] : [], Qt = (t) => t.joinType === "inner" || t.joinType === "left";
48
+ function Jt(t, r, e, n, s) {
49
49
  const o = r.type === "binary" && r.left && r.right ? { ...r, left: r.right, right: r.left } : r, u = { forward: !1, back: !1 };
50
50
  return t.joins.forEach((i) => {
51
51
  const a = i.alias ?? i.table;
52
- if (a !== e && a !== n || !Xt(i) || !$t(i.on).some((f) => F(f, r) || F(f, o))) return;
52
+ if (a !== e && a !== n || !Qt(i) || !At(i.on).some((f) => F(f, r) || F(f, o))) return;
53
53
  const c = i.joinType === "inner";
54
54
  a === e ? (u.forward = !0, u.back || (u.back = c)) : (u.back = !0, u.forward || (u.forward = c || s));
55
55
  }), u;
56
56
  }
57
- const d = (t) => t.some((r, e) => t.slice(0, e).every((n) => n.forward) && t.slice(e).every((n) => n.back)) || t.every((r) => r.forward);
58
- function lt(t, r, e, n, s = new Set(ft(r))) {
59
- const o = Z(n.condition), u = Y(r), i = (a) => {
57
+ const rt = (t) => t.some((r, e) => t.slice(0, e).every((n) => n.forward) && t.slice(e).every((n) => n.back)) || t.every((r) => r.forward);
58
+ function mt(t, r, e, n, s = new Set(Tt(r))) {
59
+ const o = tt(n.condition), u = Y(r), i = (a) => {
60
60
  const c = /* @__PURE__ */ new Map([[e.table, e.ref]]), f = /* @__PURE__ */ new Map(), E = [], $ = new Set(s), A = (g) => {
61
61
  const O = f.get(g);
62
62
  return O ? [...A(O.ref), O.tie] : [];
63
63
  }, y = (g, O, H) => {
64
- if (O === g.length) return d(A(c.get(H)));
65
- const D = g[O], L = Ft(t, [...c.keys()], D, { anyShape: !0 });
64
+ if (O === g.length) return rt(A(c.get(H)));
65
+ const D = g[O], L = _t(t, [...c.keys()], D, { anyShape: !0 });
66
66
  if (!L) return !1;
67
- const Q = c.get(Z(L.on).find((T) => T !== D)), V = (T) => P(L.on, new Map([...c, [D, T]])), J = o.includes(D), z = (T, I) => {
68
- c.set(D, T), f.set(T, { ref: Q, tie: I });
69
- }, bt = (T, I) => (z(T, I), d(A(T)) && y(g, O + 1, H) ? !0 : (c.delete(D), f.delete(T), !1));
67
+ const J = c.get(tt(L.on).find((T) => T !== D)), z = (T) => P(L.on, new Map([...c, [D, T]])), K = o.includes(D), Z = (T, h) => {
68
+ c.set(D, T), f.set(T, { ref: J, tie: h });
69
+ }, Ft = (T, h) => (Z(T, h), rt(A(T)) && y(g, O + 1, H) ? !0 : (c.delete(D), f.delete(T), !1));
70
70
  if (u.some((T) => {
71
71
  if (T.table !== D) return !1;
72
- const I = T.alias ?? D, v = kt(r, V(I), Q, I, J);
73
- return (v.forward || v.back) && bt(I, v);
72
+ const h = T.alias ?? D, v = Jt(r, z(h), J, h, K);
73
+ return (v.forward || v.back) && Ft(h, v);
74
74
  })) return !0;
75
75
  if (!a) return !1;
76
76
  let p = D;
77
77
  for (let T = 2; $.has(p); T += 1) p = `${D}_${T}`;
78
- return $.add(p), z(p, { back: !0, forward: L.joinType === "inner" || J }), E.push({
79
- id: Ht(),
78
+ return $.add(p), Z(p, { back: !0, forward: L.joinType === "inner" || K }), E.push({
79
+ id: Gt(),
80
80
  table: D,
81
81
  ...p !== D ? { alias: p } : {},
82
82
  joinType: L.joinType,
83
- on: V(p)
83
+ on: z(p)
84
84
  }), y(g, O + 1, H);
85
85
  };
86
86
  let R;
87
87
  if (o.every((g) => {
88
88
  if (R || c.has(g)) return !0;
89
- const O = wt(t, [...c.keys()], g, { anyShape: !0 });
89
+ const O = Yt(t, [...c.keys()], g, { anyShape: !0 });
90
90
  return O ? y(O, 0, g) : (R = g, !0);
91
91
  }))
92
92
  return $.forEach((g) => s.add(g)), { joins: E, condition: P(n.condition, c), ...R ? { missing: R } : {} };
93
93
  };
94
94
  return i(!1) ?? i(!0);
95
95
  }
96
- function Et(t, r) {
97
- return $t(t).some((e) => F(e, r));
96
+ function gt(t, r) {
97
+ return At(t).some((e) => F(e, r));
98
98
  }
99
- function Wt(t, r) {
99
+ function zt(t, r) {
100
100
  return (t == null ? void 0 : t.type) === "logical" && t.operator === "AND" ? { ...t, operands: [...t.operands, r] } : t ? { type: "logical", operator: "AND", operands: [t, r] } : r;
101
101
  }
102
- function tt(t, r, e, n, s) {
103
- return at(r, e).reduce((o, u) => {
104
- const i = lt(t, o, u, e, s);
105
- if (i.missing) throw new xt(n, u.table, i.missing, u.alias);
106
- const a = Et(o.where, i.condition);
102
+ function et(t, r, e, n, s) {
103
+ return Et(r, e).reduce((o, u) => {
104
+ const i = mt(t, o, u, e, s);
105
+ if (i.missing) throw new Wt(n, u.table, i.missing, u.alias);
106
+ const a = gt(o.where, i.condition);
107
107
  return i.joins.length === 0 && a ? o : {
108
108
  ...o,
109
109
  joins: [...o.joins, ...i.joins],
110
- where: a ? o.where : Wt(o.where, i.condition)
110
+ where: a ? o.where : zt(o.where, i.condition)
111
111
  };
112
112
  }, r);
113
113
  }
114
- function rt(t) {
114
+ function nt(t) {
115
115
  const r = /* @__PURE__ */ new Map(), e = (n, s, o, u) => {
116
116
  if (Array.isArray(n)) {
117
117
  n.forEach((a, c) => e(a, [...s, c], void 0, u));
@@ -119,7 +119,7 @@ function rt(t) {
119
119
  }
120
120
  if (!_(n)) return;
121
121
  let i = u;
122
- ct(n) && (i = o ?? /* @__PURE__ */ new Set(), r.set(JSON.stringify(s), i), ft(n).forEach((a) => i.add(a))), Object.entries(n).forEach(([a, c]) => e(
122
+ lt(n) && (i = o ?? /* @__PURE__ */ new Set(), r.set(JSON.stringify(s), i), Tt(n).forEach((a) => i.add(a))), Object.entries(n).forEach(([a, c]) => e(
123
123
  c,
124
124
  [...s, a],
125
125
  n.type === "exists" && a === "query" ? i : void 0,
@@ -128,23 +128,23 @@ function rt(t) {
128
128
  };
129
129
  return e(t, [], void 0, /* @__PURE__ */ new Set()), r;
130
130
  }
131
- function yr(t, r, e) {
132
- const n = tt(
131
+ function Rr(t, r, e) {
132
+ const n = et(
133
133
  t,
134
134
  r,
135
135
  e,
136
136
  [],
137
- rt(r).get("[]")
138
- ), s = K(t, r, n), o = rt(s), u = U(s, [], (i, a) => a.length === 0 ? i : tt(t, i, e, a, o.get(JSON.stringify(a))));
139
- return K(t, s, u);
137
+ nt(r).get("[]")
138
+ ), s = d(t, r, n), o = nt(s), u = q(s, [], (i, a) => a.length === 0 ? i : et(t, i, e, a, o.get(JSON.stringify(a))));
139
+ return d(t, s, u);
140
140
  }
141
- function Dr(t, r, e) {
141
+ function pr(t, r, e) {
142
142
  if (!_(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 U(r, [], (s, o) => (at(s, e).forEach((u) => {
146
- const i = lt(t, s, u, e);
147
- (i.missing || i.joins.length > 0 || !Et(s.where, i.condition)) && n.push({ path: o, table: u.table, ...u.alias ? { alias: u.alias } : {} });
145
+ return q(r, [], (s, o) => (Et(s, e).forEach((u) => {
146
+ const i = mt(t, s, u, e);
147
+ (i.missing || i.joins.length > 0 || !gt(s.where, i.condition)) && n.push({ path: o, table: u.table, ...u.alias ? { alias: u.alias } : {} });
148
148
  }), s)), n;
149
149
  }
150
150
  function G(t, r) {
@@ -161,9 +161,9 @@ function G(t, r) {
161
161
  n.delete(o), s.delete(o);
162
162
  }), { tableOf: e, aliases: n, derived: s };
163
163
  }
164
- function Qt(t, r, e) {
164
+ function Kt(t, r, e) {
165
165
  const n = t.on && M(t.on, r, e);
166
- return t.query ? { ...t, on: n, query: Ut(t.query, (s) => j(s, e)) } : t.shared !== void 0 ? { ...t, on: n } : { ...t, on: n, table: e(t.table) };
166
+ return t.query ? { ...t, on: n, query: jt(t.query, (s) => j(s, e)) } : t.shared !== void 0 ? { ...t, on: n } : { ...t, on: n, table: e(t.table) };
167
167
  }
168
168
  function M(t, r, e) {
169
169
  if (t.type === "field") {
@@ -184,15 +184,15 @@ function M(t, r, e) {
184
184
  query: {
185
185
  ...n,
186
186
  from: { ...n.from, table: e(n.from.table) },
187
- joins: n.joins.map((i) => Qt(i, o, e)),
187
+ joins: n.joins.map((i) => Kt(i, o, e)),
188
188
  ...n.where ? { where: M(n.where, o, e) } : {}
189
189
  }
190
190
  };
191
191
  }
192
- return k(t, (n) => M(n, r, e));
192
+ return W(t, (n) => M(n, r, e));
193
193
  }
194
194
  function j(t, r) {
195
- const e = G([t.from, ...t.joins]), n = ot(
195
+ const e = G([t.from, ...t.joins]), n = at(
196
196
  t,
197
197
  (s) => M(s, e, r),
198
198
  (s) => j(s, r)
@@ -217,17 +217,17 @@ function x(t, r) {
217
217
  joins: e.joins.map(n)
218
218
  };
219
219
  }
220
- function Vt(t, r) {
220
+ function Zt(t, r) {
221
221
  return j(x(t, r), r);
222
222
  }
223
- const Jt = /^(\d{2}):(\d{2})(?::(\d{2}))?$/, et = /YYYY|MM|DD|HH|mm|ss/g, b = (t, r = 2) => String(t).padStart(r, "0");
224
- function Tt(t, r) {
223
+ const dt = /^(\d{2}):(\d{2})(?::(\d{2}))?$/, st = /YYYY|MM|DD|HH|mm|ss/g, b = (t, r = 2) => String(t).padStart(r, "0");
224
+ function yt(t, r) {
225
225
  let e;
226
- const n = typeof t == "string" ? Jt.exec(t.trim()) : null;
226
+ const n = typeof t == "string" ? dt.exec(t.trim()) : null;
227
227
  if (n)
228
228
  e = { HH: n[1], mm: n[2], ss: n[3] ?? "00" };
229
229
  else {
230
- const u = typeof t == "string" ? vt(t, "dateTime") : t;
230
+ const u = typeof t == "string" ? xt(t, "dateTime") : t;
231
231
  u instanceof Date && (e = {
232
232
  YYYY: b(u.getUTCFullYear(), 4),
233
233
  MM: b(u.getUTCMonth() + 1),
@@ -239,20 +239,20 @@ function Tt(t, r) {
239
239
  }
240
240
  if (!e) return t;
241
241
  const s = e;
242
- return (r.match(et) ?? []).some((u) => s[u] === void 0) ? t : r.replace(et, (u) => s[u]);
242
+ return (r.match(st) ?? []).some((u) => s[u] === void 0) ? t : r.replace(st, (u) => s[u]);
243
243
  }
244
- function At(t, r) {
244
+ function Dt(t, r) {
245
245
  const e = /* @__PURE__ */ new Map(), n = (s) => {
246
246
  const o = /* @__PURE__ */ new Map();
247
- Y(s).forEach((c) => o.set(qt(c), c.table));
247
+ Y(s).forEach((c) => o.set(Ht(c), c.table));
248
248
  const u = (c) => {
249
249
  if ((c == null ? void 0 : c.type) !== "field" || !c.table || !c.column) return;
250
- const f = o.has(c.table) ? o.get(c.table) : c.table, E = f === void 0 ? void 0 : Yt(r, f), $ = E == null ? void 0 : E.columns.find((A) => A.name === c.column);
250
+ const f = o.has(c.table) ? o.get(c.table) : c.table, E = f === void 0 ? void 0 : vt(r, f), $ = E == null ? void 0 : E.columns.find((A) => A.name === c.column);
251
251
  return $ == null ? void 0 : $.format;
252
252
  }, i = (c, f) => {
253
253
  switch (c.type) {
254
254
  case "literal":
255
- return { ...c, value: Tt(c.value, f) };
255
+ return { ...c, value: yt(c.value, f) };
256
256
  case "list":
257
257
  return { ...c, items: c.items.map((E) => i(E, f)) };
258
258
  case "param":
@@ -262,7 +262,7 @@ function At(t, r) {
262
262
  }
263
263
  }, a = (c) => {
264
264
  if (c.type === "subquery") return { ...c, query: n(c.query) };
265
- const f = k(c, a);
265
+ const f = W(c, a);
266
266
  if (f.type !== "binary" || !f.right) return f;
267
267
  const E = u(f.left), $ = u(f.right);
268
268
  return {
@@ -271,11 +271,11 @@ function At(t, r) {
271
271
  right: E ? i(f.right, E) : f.right
272
272
  };
273
273
  };
274
- return ot(s, a, (c) => n(c));
274
+ return at(s, a, (c) => n(c));
275
275
  };
276
276
  return { query: n(t), params: e };
277
277
  }
278
- const Or = ["mysql", "mssql", "postgres"], Sr = {
278
+ const hr = ["mysql", "mssql", "postgres"], Ir = {
279
279
  mysql: "MySQL",
280
280
  mssql: "SQL Server",
281
281
  postgres: "PostgreSQL"
@@ -293,9 +293,9 @@ function N(t, r) {
293
293
  return `"${e.replace(/"/g, '""')}"`;
294
294
  }
295
295
  }
296
- const zt = /^[A-Za-z_][A-Za-z0-9_]*$/, mt = (t) => `${t}`, Kt = /\uE000(\d+)\uE000/g;
296
+ const tr = /^[A-Za-z_][A-Za-z0-9_]*$/, Ot = (t) => `${t}`, rr = /\uE000(\d+)\uE000/g;
297
297
  let C;
298
- function Nr(t, r) {
298
+ function Cr(t, r) {
299
299
  C = { ...t, writing: [] };
300
300
  try {
301
301
  return r();
@@ -303,17 +303,17 @@ function Nr(t, r) {
303
303
  C = void 0;
304
304
  }
305
305
  }
306
- function gt(t, r, e) {
306
+ function St(t, r, e) {
307
307
  if (typeof t != "object" || t === null || t instanceof Date) return;
308
308
  let n;
309
309
  return Object.entries(t).some(([s, o]) => {
310
310
  const u = [...e, Array.isArray(t) ? Number(s) : s];
311
- return n = o === r ? u : gt(o, r, u), n !== void 0;
311
+ return n = o === r ? u : St(o, r, u), n !== void 0;
312
312
  }), n;
313
313
  }
314
314
  function m(t, r) {
315
315
  if (!C) return r();
316
- const { writing: e, paths: n } = C, s = e[e.length - 1], o = typeof t == "string" ? [t] : gt(s.value, t, s.path);
316
+ const { writing: e, paths: n } = C, s = e[e.length - 1], o = typeof t == "string" ? [t] : St(s.value, t, s.path);
317
317
  if (!o) return r();
318
318
  e.push({ value: t, path: o });
319
319
  try {
@@ -322,7 +322,7 @@ function m(t, r) {
322
322
  e.pop();
323
323
  }
324
324
  }
325
- const Zt = {
325
+ const er = {
326
326
  mysql: {
327
327
  text: "CHAR",
328
328
  integer: "SIGNED",
@@ -351,33 +351,33 @@ const Zt = {
351
351
  boolean: "boolean"
352
352
  }
353
353
  };
354
- function dt(t, r) {
354
+ function nr(t, r) {
355
355
  var n;
356
- const e = (n = Zt[r]) == null ? void 0 : n[t];
356
+ const e = (n = er[r]) == null ? void 0 : n[t];
357
357
  return e ? r === "postgres" ? `NULL::${e}` : `CAST(NULL AS ${e})` : "NULL";
358
358
  }
359
- function yt(t, r, e) {
359
+ function Nt(t, r, e) {
360
360
  if (t == null) return "NULL";
361
361
  if (e)
362
- return e.values.push(t), mt(e.values.length - 1);
362
+ return e.values.push(t), Ot(e.values.length - 1);
363
363
  if (typeof t == "boolean")
364
364
  return r === "mssql" ? t ? "1" : "0" : t ? "TRUE" : "FALSE";
365
365
  if (typeof t == "string") {
366
366
  const n = C ? t.replace(/[\uE001-\uE003]/g, "") : t;
367
367
  return n === "" ? "''" : `'${n.replace(/'/g, "''")}'`;
368
368
  }
369
- return typeof t == "number" || typeof t == "bigint" ? String(t) : t instanceof Date ? `'${ut(t)}'` : String(t);
369
+ return typeof t == "number" || typeof t == "bigint" ? String(t) : t instanceof Date ? `'${$t(t)}'` : String(t);
370
370
  }
371
- let q = "native";
372
- function Dt(t, r) {
373
- q = t ?? "native";
371
+ let U = "native", X = "decimal";
372
+ function Rt(t, r) {
373
+ U = t.nullSafeEqual ?? "native", X = t.division ?? "decimal";
374
374
  try {
375
375
  return r();
376
376
  } finally {
377
- q = "native";
377
+ U = "native", X = "decimal";
378
378
  }
379
379
  }
380
- function tr(t, r) {
380
+ function sr(t, r) {
381
381
  switch (t) {
382
382
  case "!=":
383
383
  return r === "mssql" ? "<>" : "!=";
@@ -388,12 +388,12 @@ function tr(t, r) {
388
388
  if (r === "mssql") throw new Error("REGEXP isn't available in SQL Server");
389
389
  return "REGEXP";
390
390
  default:
391
- if (!Gt.includes(t))
391
+ if (!Vt.includes(t))
392
392
  throw new Error(`unknown operator ${JSON.stringify(t)}`);
393
393
  return t;
394
394
  }
395
395
  }
396
- function h(t) {
396
+ function I(t) {
397
397
  return (t == null ? void 0 : t.type) === "literal" && typeof t.value == "string" && /^[A-Za-z_]+$/.test(t.value) ? t.value.toUpperCase() : "DAY";
398
398
  }
399
399
  const B = {
@@ -407,16 +407,22 @@ const B = {
407
407
  SECOND: "second",
408
408
  MILLISECOND: "millisecond"
409
409
  };
410
- function nt(t, [r = "??", , e], n) {
410
+ function S(t) {
411
+ return t.type === "binary" || t.type === "logical" || t.type === "if" || t.type === "case";
412
+ }
413
+ const ot = (t, r) => (r == null ? void 0 : r.type) === "arithmetic" || r && S(r) ? `(${t})` : t;
414
+ function it(t, [r = "??", , e], n) {
411
415
  if (n.length < 2) return `${t}(${r})`;
412
416
  const s = n[1], o = s.type === "literal" && Number.isInteger(s.value) && s.value >= 0 ? String(s.value) : "??";
413
417
  return `${t}(${r}, ${o}${e === void 0 ? "" : `, ${e}`})`;
414
418
  }
415
- const rr = {
419
+ const or = {
420
+ // SQL Server has no MOD(), only `%`.
421
+ MOD: ([t = "??", r = "??"], e, n) => n === "mssql" ? `(${ot(t, e[0])} % ${ot(r, e[1])})` : `MOD(${t}, ${r})`,
416
422
  // ---- Date arithmetic ----
417
423
  DATE_ADD: ([t, r], e, n, s) => {
418
424
  var u;
419
- const o = h(e[2]);
425
+ const o = I(e[2]);
420
426
  switch (n) {
421
427
  case "mysql":
422
428
  return o === "MILLISECOND" ? `DATE_ADD(${t}, INTERVAL (${r}) * 1000 MICROSECOND)` : `DATE_ADD(${t}, INTERVAL ${r} ${o})`;
@@ -427,12 +433,12 @@ const rr = {
427
433
  case "mssql":
428
434
  return `DATEADD(${o}, ${r}, ${t})`;
429
435
  default:
430
- return `DATE_ADD(${t}, ${r}, ${h(e[2])})`;
436
+ return `DATE_ADD(${t}, ${r}, ${I(e[2])})`;
431
437
  }
432
438
  },
433
439
  DATE_SUB: ([t, r], e, n, s) => {
434
440
  var u;
435
- const o = h(e[2]);
441
+ const o = I(e[2]);
436
442
  switch (n) {
437
443
  case "mysql":
438
444
  return o === "MILLISECOND" ? `DATE_SUB(${t}, INTERVAL (${r}) * 1000 MICROSECOND)` : `DATE_SUB(${t}, INTERVAL ${r} ${o})`;
@@ -443,12 +449,12 @@ const rr = {
443
449
  case "mssql":
444
450
  return `DATEADD(${o}, -(${r}), ${t})`;
445
451
  default:
446
- return `DATE_SUB(${t}, ${r}, ${h(e[2])})`;
452
+ return `DATE_SUB(${t}, ${r}, ${I(e[2])})`;
447
453
  }
448
454
  },
449
455
  // ---- Date diff ----
450
456
  DATEDIFF: ([t, r], e, n) => {
451
- const s = h(e[2]);
457
+ const s = I(e[2]);
452
458
  switch (n) {
453
459
  case "mysql":
454
460
  return `TIMESTAMPDIFF(${s}, ${t}, ${r})`;
@@ -487,7 +493,7 @@ const rr = {
487
493
  },
488
494
  // ---- Date extract ----
489
495
  EXTRACT: ([t], r, e) => {
490
- const n = h(r[1]);
496
+ const n = I(r[1]);
491
497
  switch (e) {
492
498
  case "mysql":
493
499
  return n === "MILLISECOND" ? `FLOOR(MICROSECOND(${t}) / 1000)` : `EXTRACT(${n} FROM ${t})`;
@@ -501,7 +507,7 @@ const rr = {
501
507
  },
502
508
  // ---- Date truncation ----
503
509
  DATE_TRUNC: ([t], r, e) => {
504
- const n = h(r[1]);
510
+ const n = I(r[1]);
505
511
  switch (e) {
506
512
  case "mysql":
507
513
  switch (n) {
@@ -571,13 +577,13 @@ const rr = {
571
577
  // ---- Window functions (issue 20) ----
572
578
  // The offset goes into the SQL as a whole number even in parameterized
573
579
  // SQL: MySQL takes nothing else there. OVER is added by formatNode.
574
- LAG: (t, r) => nt("LAG", t, r),
575
- LEAD: (t, r) => nt("LEAD", t, r),
580
+ LAG: (t, r) => it("LAG", t, r),
581
+ LEAD: (t, r) => it("LEAD", t, r),
576
582
  // ---- String aggregation (issue 137) ----
577
583
  // The separator goes into the SQL as a literal even in parameterized SQL:
578
584
  // MySQL's SEPARATOR takes nothing else.
579
585
  STRING_AGG: ([t = "??"], r, e, n, s, o) => {
580
- const u = r[1] ?? { type: "literal", value: ", " }, i = u.type === "literal" && typeof u.value == "string" ? yt(u.value, e) : "??", a = s.distinct ? "DISTINCT " : "", c = ($) => {
586
+ const u = r[1] ?? { type: "literal", value: ", " }, i = u.type === "literal" && typeof u.value == "string" ? Nt(u.value, e) : "??", a = s.distinct ? "DISTINCT " : "", c = ($) => {
581
587
  var A;
582
588
  return (A = s.orderBy) != null && A.length ? `ORDER BY ${s.orderBy.map((y) => `${$(y)} ${y.direction === "desc" ? "DESC" : "ASC"}`).join(", ")}` : "";
583
589
  }, f = ($) => o($.expression), E = ($) => $ ? ` ${$}` : "";
@@ -603,21 +609,18 @@ const rr = {
603
609
  }
604
610
  }
605
611
  };
606
- function er(t, r) {
612
+ function ir(t, r) {
607
613
  var n, s;
608
614
  return `OVER (${[
609
615
  ...(n = t.partitionBy) != null && n.length ? [`PARTITION BY ${t.partitionBy.map(r).join(", ")}`] : [],
610
616
  ...(s = t.orderBy) != null && s.length ? [`ORDER BY ${t.orderBy.map((o) => `${r(o.expression)} ${o.direction === "desc" ? "DESC" : "ASC"}`).join(", ")}`] : []
611
617
  ].join(" ")})`;
612
618
  }
613
- function S(t) {
614
- return t.type === "binary" || t.type === "logical" || t.type === "if" || t.type === "case";
615
- }
616
619
  function l(t, r, e, n) {
617
- return m(t, () => sr(t, r, e, n));
620
+ return m(t, () => ar(t, r, e, n));
618
621
  }
619
- function nr(t, r, e, n) {
620
- const s = t.args.map((a) => l(a, r, e, n)), o = rr[t.name.toUpperCase()];
622
+ function ur(t, r, e, n) {
623
+ const s = t.args.map((a) => l(a, r, e, n)), o = or[t.name.toUpperCase()];
621
624
  if (o)
622
625
  return o(
623
626
  s,
@@ -632,7 +635,7 @@ function nr(t, r, e, n) {
632
635
  const i = t.distinct && s.length > 0 ? "DISTINCT " : "";
633
636
  return `${u}(${i}${s.join(", ")})`;
634
637
  }
635
- function st(t, r, e, n) {
638
+ function ut(t, r, e, n) {
636
639
  const s = (o) => {
637
640
  if (!o) return "??";
638
641
  const u = l(o, r, e, n);
@@ -643,17 +646,31 @@ function st(t, r, e, n) {
643
646
  return `${s(o)} AND ${s(u)}`;
644
647
  });
645
648
  }
646
- function sr(t, r, e, n) {
649
+ const k = {
650
+ "+": 1,
651
+ "-": 1,
652
+ "*": 2,
653
+ "/": 2
654
+ };
655
+ function ct(t, r, e, n, s, o) {
656
+ const u = l(t, n, s, o);
657
+ return t.type === "arithmetic" && (k[t.operator] < r || e && k[t.operator] === r) || S(t) ? `(${u})` : u;
658
+ }
659
+ function cr(t, r, e, n) {
660
+ const s = k[t.operator], o = ct(t.left, s, !1, r, e, n), u = ct(t.right, s, !0, r, e, n);
661
+ return t.operator === "/" && X === "decimal" && r !== "mysql" ? `${o} * 1.0 / ${u}` : `${o} ${t.operator} ${u}`;
662
+ }
663
+ function ar(t, r, e, n) {
647
664
  var u;
648
665
  const s = " ".repeat(e), o = " ".repeat(e + 1);
649
666
  switch (t.type) {
650
667
  case "literal":
651
- return t.value === "" && !t.empty ? "??" : t.value === null && t.as ? dt(t.as, r) : yt(t.value, r, n);
668
+ return t.value === "" && !t.empty ? "??" : t.value === null && t.as ? nr(t.as, r) : Nt(t.value, r, n);
652
669
  case "param": {
653
- if (!n) return zt.test(t.name) ? `:${t.name}` : "??";
670
+ if (!n) return tr.test(t.name) ? `:${t.name}` : "??";
654
671
  if (!n.named.has(t.name))
655
672
  throw new Error(`no value for parameter "${t.name}"`);
656
- return n.values.push(n.named.get(t.name)), mt(n.values.length - 1);
673
+ return n.values.push(n.named.get(t.name)), Ot(n.values.length - 1);
657
674
  }
658
675
  case "field": {
659
676
  if (!t.column) return "??";
@@ -661,19 +678,19 @@ function sr(t, r, e, n) {
661
678
  return t.table ? `${N(t.table, r)}.${i}` : i;
662
679
  }
663
680
  case "function": {
664
- const i = nr(t, r, e, n);
665
- return t.over ? `${i} ${er(t, (a) => l(a, r, e, n))}` : i;
681
+ const i = ur(t, r, e, n);
682
+ return t.over ? `${i} ${ir(t, (a) => l(a, r, e, n))}` : i;
666
683
  }
667
684
  case "binary": {
668
- const i = t.operator ? tr(t.operator, r) : "=", a = l(t.left, r, e, n), c = S(t.left) ? `(${a})` : a;
669
- if (Pt(t.operator)) return `${c} ${i}`;
685
+ const i = t.operator ? sr(t.operator, r) : "=", a = l(t.left, r, e, n), c = S(t.left) ? `(${a})` : a;
686
+ if (kt(t.operator)) return `${c} ${i}`;
670
687
  if (!t.right) return `${c} ${i} ??`;
671
688
  if (i === "BETWEEN")
672
- return `${c} BETWEEN ${st(t.right, r, e, n)}`;
689
+ return `${c} BETWEEN ${ut(t.right, r, e, n)}`;
673
690
  if (i === "IN")
674
691
  return t.right.type === "list" ? `${c} IN ${l(t.right, r, e, n)}` : `${c} IN (${l(t.right, r, e, n)})`;
675
692
  const f = l(t.right, r, e, n), E = S(t.right) ? `(${f})` : f;
676
- if (t.operator === "IS NOT DISTINCT FROM" && q === "expanded") {
693
+ if (t.operator === "IS NOT DISTINCT FROM" && U === "expanded") {
677
694
  const $ = (y) => {
678
695
  const R = l(y, r, e, n);
679
696
  return S(y) ? `(${R})` : R;
@@ -682,6 +699,8 @@ function sr(t, r, e, n) {
682
699
  }
683
700
  return `${c} ${i} ${E}`;
684
701
  }
702
+ case "arithmetic":
703
+ return cr(t, r, e, n);
685
704
  case "logical": {
686
705
  if (t.operands.length === 0) return "/* empty */";
687
706
  if (t.operands.length === 1)
@@ -708,9 +727,9 @@ ${s})`;
708
727
  }
709
728
  if (i.type === "binary" && i.operator === "BETWEEN" && i.right) {
710
729
  const a = l(i.left, r, e, n);
711
- return `${S(i.left) ? `(${a})` : a} NOT BETWEEN ${st(i.right, r, e, n)}`;
730
+ return `${S(i.left) ? `(${a})` : a} NOT BETWEEN ${ut(i.right, r, e, n)}`;
712
731
  }
713
- if (i.type === "binary" && i.operator === "IS NOT DISTINCT FROM" && i.right && q === "native") {
732
+ if (i.type === "binary" && i.operator === "IS NOT DISTINCT FROM" && i.right && U === "native") {
714
733
  if (r === "mysql") return `NOT (${l(i, r, e, n)})`;
715
734
  const a = l(i.left, r, e, n), c = S(i.left) ? `(${a})` : a, f = l(i.right, r, e, n), E = S(i.right) ? `(${f})` : f;
716
735
  return `${c} IS DISTINCT FROM ${E}`;
@@ -747,11 +766,11 @@ ${[
747
766
  "SELECT 1",
748
767
  // The clauses below write their expressions with formatNode.
749
768
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
750
- pt(i.from, r),
769
+ bt(i.from, r),
751
770
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
752
- ...i.joins.map(($) => It($, r, n)),
771
+ ...i.joins.map(($) => Lt($, r, n)),
753
772
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
754
- Ot(f, r, n)
773
+ pt(f, r, n)
755
774
  ].join(`
756
775
  `).replace(/^/gm, o)}
757
776
  ${s})`;
@@ -763,62 +782,62 @@ ${s})`;
763
782
  return `(${t.items.map((i) => l(i, r, e, n)).join(", ") || "??"})`;
764
783
  case "subquery":
765
784
  return `
766
- ${W(t.query, r, n).join(`
785
+ ${Q(t.query, r, n).join(`
767
786
  `).replace(/^/gm, o)}
768
787
  ${s}`;
769
788
  default:
770
789
  return "/* unknown */";
771
790
  }
772
791
  }
773
- function Ot(t, r, e) {
792
+ function pt(t, r, e) {
774
793
  return `WHERE
775
794
  ${l(t, r, 1, e)}`;
776
795
  }
777
- const or = {
796
+ const fr = {
778
797
  inner: "INNER JOIN",
779
798
  left: "LEFT JOIN",
780
799
  right: "RIGHT JOIN",
781
800
  full: "FULL JOIN",
782
801
  cross: "CROSS JOIN"
783
802
  };
784
- let St = /* @__PURE__ */ new Map();
785
- function Nt(t, r, e) {
786
- return t.shared !== void 0 ? N(St.get(t.shared) ?? "??", r) : t.query ? `(
787
- ${Rt(t.query, r, e).replace(/^/gm, " ")}
803
+ let ht = /* @__PURE__ */ new Map();
804
+ function It(t, r, e) {
805
+ return t.shared !== void 0 ? N(ht.get(t.shared) ?? "??", r) : t.query ? `(
806
+ ${Ct(t.query, r, e).replace(/^/gm, " ")}
788
807
  )` : N(t.table, r);
789
808
  }
790
- function Rt(t, r, e) {
791
- return _t(t).map((n) => W(n, r, e).join(`
792
- `)).join(jt(t) && !t.all ? `
809
+ function Ct(t, r, e) {
810
+ return Bt(t).map((n) => Q(n, r, e).join(`
811
+ `)).join(Pt(t) && !t.all ? `
793
812
  UNION
794
813
  ` : `
795
814
  UNION ALL
796
815
  `);
797
816
  }
798
- function ir(t, r, e) {
817
+ function $r(t, r, e) {
799
818
  return t.length === 0 ? [] : [`WITH
800
819
  ${t.map((s) => m(s, () => {
801
- const o = Rt(s.query, r, e).replace(/^/gm, " ");
820
+ const o = Ct(s.query, r, e).replace(/^/gm, " ");
802
821
  return ` ${N(s.name, r)} AS (
803
822
  ${o}
804
823
  )`;
805
824
  })).join(`,
806
825
  `)}`];
807
826
  }
808
- function pt(t, r, e) {
827
+ function bt(t, r, e) {
809
828
  const n = t.alias ? ` AS ${N(t.alias, r)}` : "";
810
- return `FROM ${m(t, () => `${Nt(t, r, e)}${n}`)}`;
829
+ return `FROM ${m(t, () => `${It(t, r, e)}${n}`)}`;
811
830
  }
812
- function It(t, r, e) {
831
+ function Lt(t, r, e) {
813
832
  return m(t, () => {
814
- const n = Nt(t, r, e), s = t.alias ? ` AS ${N(t.alias, r)}` : "";
833
+ const n = It(t, r, e), s = t.alias ? ` AS ${N(t.alias, r)}` : "";
815
834
  if (t.joinType === "cross") return `CROSS JOIN ${n}${s}`;
816
835
  const o = t.on ? l(t.on, r, 1, e) : "/* unconfigured */";
817
- return `${or[t.joinType]} ${n}${s}
836
+ return `${fr[t.joinType]} ${n}${s}
818
837
  ON ${o}`;
819
838
  });
820
839
  }
821
- function ur(t, r, e, n = "SELECT") {
840
+ function lr(t, r, e, n = "SELECT") {
822
841
  if (t.length === 0) return `${n} ${m(t, () => "*")}`;
823
842
  const s = t.map((o) => m(o, () => {
824
843
  const u = l(o.expression, r, 1, e), i = o.alias ? ` AS ${N(o.alias, r)}` : "";
@@ -828,7 +847,7 @@ function ur(t, r, e, n = "SELECT") {
828
847
  ${s.join(`,
829
848
  `)}`;
830
849
  }
831
- function cr(t, r, e) {
850
+ function Er(t, r, e) {
832
851
  return `GROUP BY
833
852
  ${t.map((s) => m(
834
853
  s,
@@ -836,56 +855,56 @@ function cr(t, r, e) {
836
855
  )).join(`,
837
856
  `)}`;
838
857
  }
839
- function ar(t, r, e) {
858
+ function Tr(t, r, e) {
840
859
  return `HAVING
841
860
  ${l(t, r, 1, e)}`;
842
861
  }
843
- function fr(t, r, e, n) {
862
+ function Ar(t, r, e, n) {
844
863
  return `ORDER BY
845
864
  ${t.map((o) => m(o, () => `${l(n(o.expression), r, 1, e)} ${o.direction === "desc" ? "DESC" : "ASC"}`)).join(`,
846
865
  `)}`;
847
866
  }
848
- const X = (t) => t !== void 0 && Number.isInteger(t) && t >= 0 ? t : void 0;
849
- function ht(t, r) {
850
- const e = r.resolveName ? Vt(t, r.resolveName) : t;
851
- return r.defaultLimit !== void 0 && X(e.limit) === void 0 ? { ...e, limit: r.defaultLimit } : e;
867
+ const V = (t) => t !== void 0 && Number.isInteger(t) && t >= 0 ? t : void 0;
868
+ function Mt(t, r) {
869
+ const e = r.resolveName ? Zt(t, r.resolveName) : t;
870
+ return r.defaultLimit !== void 0 && V(e.limit) === void 0 ? { ...e, limit: r.defaultLimit } : e;
852
871
  }
853
- const $r = "18446744073709551615";
854
- function W(t, r, e, n = `SELECT${t.distinct ? " DISTINCT" : ""}`) {
872
+ const mr = "18446744073709551615";
873
+ function Q(t, r, e, n = `SELECT${t.distinct ? " DISTINCT" : ""}`) {
855
874
  const s = [
856
- ur(t.select, r, e, n),
857
- pt(t.from, r, e),
858
- ...t.joins.map((u) => It(u, r, e))
875
+ lr(t.select, r, e, n),
876
+ bt(t.from, r, e),
877
+ ...t.joins.map((u) => Lt(u, r, e))
859
878
  ];
860
- t.where && s.push(Ot(t.where, r, e));
879
+ t.where && s.push(pt(t.where, r, e));
861
880
  const o = t.groupBy ?? [];
862
- if (o.length > 0 && s.push(cr(o, r, e)), t.having) {
863
- const u = r === "mysql" ? it(t.having, t) : t.having;
864
- s.push(ar(u, r, e));
881
+ if (o.length > 0 && s.push(Er(o, r, e)), t.having) {
882
+ const u = r === "mysql" ? ft(t.having, t) : t.having;
883
+ s.push(Tr(u, r, e));
865
884
  }
866
885
  return s;
867
886
  }
868
- function Ct(t, r, e) {
869
- C && (C.writing = [{ value: t, path: [] }]), St = new Map((t.shared ?? []).map((f) => [f.id, f.name]));
870
- const n = ir(t.shared ?? [], r, e), s = X(t.limit), o = X(t.offset) || void 0, u = t.orderBy ?? [], i = r === "mssql" && s !== void 0 && o === void 0, a = `SELECT${t.distinct ? " DISTINCT" : ""}${i ? ` ${m("limit", () => `TOP (${s})`)}` : ""}`, c = W(t, r, e, a);
871
- if (u.length > 0 && c.push(fr(u, r, e, (f) => r === "mysql" ? it(f, t) : f)), r === "mssql") {
887
+ function wt(t, r, e) {
888
+ C && (C.writing = [{ value: t, path: [] }]), ht = new Map((t.shared ?? []).map((f) => [f.id, f.name]));
889
+ const n = $r(t.shared ?? [], r, e), s = V(t.limit), o = V(t.offset) || void 0, u = t.orderBy ?? [], i = r === "mssql" && s !== void 0 && o === void 0, a = `SELECT${t.distinct ? " DISTINCT" : ""}${i ? ` ${m("limit", () => `TOP (${s})`)}` : ""}`, c = Q(t, r, e, a);
890
+ if (u.length > 0 && c.push(Ar(u, r, e, (f) => r === "mysql" ? ft(f, t) : f)), r === "mssql") {
872
891
  if (o !== void 0) {
873
892
  u.length === 0 && c.push("ORDER BY (SELECT NULL)");
874
893
  const f = s !== void 0 ? ` ${m("limit", () => `FETCH NEXT ${s} ROWS ONLY`)}` : "";
875
894
  c.push(`${m("offset", () => `OFFSET ${o} ROWS`)}${f}`);
876
895
  }
877
896
  } else if (s !== void 0 || o !== void 0) {
878
- const f = s !== void 0 || r === "mysql" ? m(s !== void 0 ? "limit" : {}, () => `LIMIT ${s ?? $r}`) : "", E = o !== void 0 ? m("offset", () => `OFFSET ${o}`) : "";
897
+ const f = s !== void 0 || r === "mysql" ? m(s !== void 0 ? "limit" : {}, () => `LIMIT ${s ?? mr}`) : "", E = o !== void 0 ? m("offset", () => `OFFSET ${o}`) : "";
879
898
  c.push([f, E].filter(Boolean).join(" "));
880
899
  }
881
900
  return [...n, ...c].join(`
882
901
  `);
883
902
  }
884
- function Rr(t, r, e = {}) {
885
- const n = e.values ? Bt(t, e.values) : t, s = e.schema ? At(n, e.schema).query : n, o = ht(s, e);
886
- return Dt(e.nullSafeEqual, () => Ct(o, r));
903
+ function br(t, r, e = {}) {
904
+ const n = e.values ? Xt(t, e.values) : t, s = e.schema ? Dt(n, e.schema).query : n, o = Mt(s, e);
905
+ return Rt(e, () => wt(o, r));
887
906
  }
888
- function lr(t, r) {
907
+ function gr(t, r) {
889
908
  switch (t) {
890
909
  case "mysql":
891
910
  return "?";
@@ -897,29 +916,26 @@ function lr(t, r) {
897
916
  return "?";
898
917
  }
899
918
  }
900
- function pr(t, r, e = {}) {
901
- const n = e.schema ? At(t, e.schema) : { query: t, params: /* @__PURE__ */ new Map() }, s = {
919
+ function Lr(t, r, e = {}) {
920
+ const n = e.schema ? Dt(t, e.schema) : { query: t, params: /* @__PURE__ */ new Map() }, s = {
902
921
  values: [],
903
922
  named: new Map(Object.entries(e.values ?? {}).map(([c, f]) => {
904
923
  const E = n.params.get(c);
905
- return [c, E ? Tt(f, E) : f];
924
+ return [c, E ? yt(f, E) : f];
906
925
  }))
907
- }, o = ht(n.query, e), u = Dt(
908
- e.nullSafeEqual,
909
- () => Ct(o, r, s)
910
- ), i = [];
911
- return { sql: u.replace(Kt, (c, f) => {
926
+ }, o = Mt(n.query, e), u = Rt(e, () => wt(o, r, s)), i = [];
927
+ return { sql: u.replace(rr, (c, f) => {
912
928
  const E = s.values[Number(f)];
913
- return i.push(E instanceof Date ? ut(E) : E), lr(r, i.length);
929
+ return i.push(E instanceof Date ? $t(E) : E), gr(r, i.length);
914
930
  }), params: i };
915
931
  }
916
932
  export {
917
- Or as D,
918
- xt as S,
919
- pr as a,
920
- Rr as b,
921
- Sr as c,
922
- yr as s,
923
- Dr as u,
924
- Nr as w
933
+ hr as D,
934
+ Wt as S,
935
+ Lr as a,
936
+ br as b,
937
+ Ir as c,
938
+ Rr as s,
939
+ pr as u,
940
+ Cr as w
925
941
  };