@nongt/visql-core 0.10.1 → 0.11.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,12 +1,12 @@
1
- var bt = Object.defineProperty;
2
- var Mt = (t, r, e) => r in t ? bt(t, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[r] = e;
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
3
  var w = (t, r, e) => Mt(t, typeof r != "symbol" ? r + "" : r, e);
4
- import { q as _, d as F, a as Lt, s as wt, m as X, e as st, g as Ft, t as Ut, j as qt, b as _t, i as Yt } from "./schema-CgL10bid.js";
5
- import { p as K, t as Z, v as Ht, w as jt, x as Pt, y as ot, z as vt, A as Bt } from "./params-DkVFbMQn.js";
6
- import { s as it } from "./calendarDate-CYlr9TvX.js";
7
- class Gt extends Error {
8
- constructor(e, n, s, i) {
9
- const u = i ? `"${n}" (as "${i}")` : `"${n}"`;
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 {
8
+ constructor(e, n, s, o) {
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`);
11
11
  /** Where the body is, as keys into the query (validateQuery's
12
12
  * `pathSegments` style). */
@@ -16,156 +16,156 @@ class Gt extends Error {
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
18
  w(this, "alias");
19
- this.name = "ScopeError", this.path = e, this.table = n, i && (this.alias = i);
19
+ this.name = "ScopeError", this.path = e, this.table = n, o && (this.alias = o);
20
20
  }
21
21
  }
22
- const Y = (t) => Object.prototype.toString.call(t) === "[object Object]", ut = (t) => Array.isArray(t.joins) && Y(t.from);
22
+ const _ = (t) => Object.prototype.toString.call(t) === "[object Object]", ct = (t) => Array.isArray(t.joins) && _(t.from);
23
23
  function U(t, r, e) {
24
24
  if (Array.isArray(t)) {
25
- const u = t.map((o, a) => U(o, [...r, a], e));
26
- return u.every((o, a) => o === t[a]) ? t : u;
25
+ const u = t.map((i, a) => U(i, [...r, a], e));
26
+ return u.every((i, a) => i === t[a]) ? t : u;
27
27
  }
28
- if (!Y(t)) return t;
28
+ if (!_(t)) return t;
29
29
  let n = !1;
30
- const s = Object.entries(t).map(([u, o]) => {
31
- const a = U(o, [...r, u], e);
32
- return a !== o && (n = !0), [u, a];
33
- }), i = n ? Object.fromEntries(s) : t;
34
- return ut(i) ? e(i, r) : i;
30
+ const s = Object.entries(t).map(([u, i]) => {
31
+ const a = U(i, [...r, u], e);
32
+ return a !== i && (n = !0), [u, a];
33
+ }), o = n ? Object.fromEntries(s) : t;
34
+ return ct(o) ? e(o, r) : o;
35
35
  }
36
- const ct = (t, r) => _(t).flatMap((e) => e.table !== void 0 && r.requires(e.table) ? [{ table: e.table, ...e.alias ? { alias: e.alias } : {}, ref: e.alias ?? e.table }] : []), at = (t) => _(t).flatMap((r) => {
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) => {
37
37
  const e = r.alias ?? r.table;
38
38
  return e === void 0 ? [] : [e];
39
39
  });
40
- function B(t, r) {
40
+ function P(t, r) {
41
41
  if (t.type === "field") {
42
42
  const e = t.table && r.get(t.table);
43
43
  return e ? { ...t, table: e } : t;
44
44
  }
45
- return X(t, (e) => B(e, r));
45
+ return k(t, (e) => P(e, r));
46
46
  }
47
- const ft = (t) => t ? t.type === "logical" && t.operator === "AND" ? t.operands : [t] : [], xt = (t) => t.joinType === "inner" || t.joinType === "left";
47
+ const $t = (t) => t ? t.type === "logical" && t.operator === "AND" ? t.operands : [t] : [], Xt = (t) => t.joinType === "inner" || t.joinType === "left";
48
48
  function kt(t, r, e, n, s) {
49
- const i = r.type === "binary" && r.left && r.right ? { ...r, left: r.right, right: r.left } : r, u = { forward: !1, back: !1 };
50
- return t.joins.forEach((o) => {
51
- const a = o.alias ?? o.table;
52
- if (a !== e && a !== n || !xt(o) || !ft(o.on).some((f) => F(f, r) || F(f, i))) return;
53
- const c = o.joinType === "inner";
49
+ const o = r.type === "binary" && r.left && r.right ? { ...r, left: r.right, right: r.left } : r, u = { forward: !1, back: !1 };
50
+ return t.joins.forEach((i) => {
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;
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
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 $t(t, r, e, n, s = new Set(at(r))) {
59
- const i = Z(n.condition), u = _(r), o = (a) => {
60
- const c = /* @__PURE__ */ new Map([[e.table, e.ref]]), f = /* @__PURE__ */ new Map(), $ = [], l = new Set(s), A = (g) => {
58
+ function lt(t, r, e, n, s = new Set(ft(r))) {
59
+ const o = Z(n.condition), u = Y(r), i = (a) => {
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
- }, y = (g, O, j) => {
64
- if (O === g.length) return d(A(c.get(j)));
65
- const D = g[O], M = wt(t, [...c.keys()], D, { anyShape: !0 });
66
- if (!M) return !1;
67
- const Q = c.get(Z(M.on).find((T) => T !== D)), V = (T) => B(M.on, new Map([...c, [D, T]])), J = i.includes(D), z = (T, I) => {
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 });
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
68
  c.set(D, T), f.set(T, { ref: Q, tie: I });
69
- }, Ct = (T, I) => (z(T, I), d(A(T)) && y(g, O + 1, j) ? !0 : (c.delete(D), f.delete(T), !1));
69
+ }, bt = (T, I) => (z(T, I), d(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, P = kt(r, V(I), Q, I, J);
73
- return (P.forward || P.back) && Ct(I, P);
72
+ const I = T.alias ?? D, v = kt(r, V(I), Q, I, J);
73
+ return (v.forward || v.back) && bt(I, v);
74
74
  })) return !0;
75
75
  if (!a) return !1;
76
76
  let p = D;
77
- for (let T = 2; l.has(p); T += 1) p = `${D}_${T}`;
78
- return l.add(p), z(p, { back: !0, forward: M.joinType === "inner" || J }), $.push({
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
79
  id: Ht(),
80
80
  table: D,
81
81
  ...p !== D ? { alias: p } : {},
82
- joinType: M.joinType,
82
+ joinType: L.joinType,
83
83
  on: V(p)
84
- }), y(g, O + 1, j);
84
+ }), y(g, O + 1, H);
85
85
  };
86
86
  let R;
87
- if (i.every((g) => {
87
+ if (o.every((g) => {
88
88
  if (R || c.has(g)) return !0;
89
- const O = Lt(t, [...c.keys()], g, { anyShape: !0 });
89
+ const O = wt(t, [...c.keys()], g, { anyShape: !0 });
90
90
  return O ? y(O, 0, g) : (R = g, !0);
91
91
  }))
92
- return l.forEach((g) => s.add(g)), { joins: $, condition: B(n.condition, c), ...R ? { missing: R } : {} };
92
+ return $.forEach((g) => s.add(g)), { joins: E, condition: P(n.condition, c), ...R ? { missing: R } : {} };
93
93
  };
94
- return o(!1) ?? o(!0);
94
+ return i(!1) ?? i(!0);
95
95
  }
96
- function lt(t, r) {
97
- return ft(t).some((e) => F(e, r));
96
+ function Et(t, r) {
97
+ return $t(t).some((e) => F(e, r));
98
98
  }
99
- function Xt(t, r) {
99
+ function Wt(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
102
  function tt(t, r, e, n, s) {
103
- return ct(r, e).reduce((i, u) => {
104
- const o = $t(t, i, u, e, s);
105
- if (o.missing) throw new Gt(n, u.table, o.missing, u.alias);
106
- const a = lt(i.where, o.condition);
107
- return o.joins.length === 0 && a ? i : {
108
- ...i,
109
- joins: [...i.joins, ...o.joins],
110
- where: a ? i.where : Xt(i.where, o.condition)
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);
107
+ return i.joins.length === 0 && a ? o : {
108
+ ...o,
109
+ joins: [...o.joins, ...i.joins],
110
+ where: a ? o.where : Wt(o.where, i.condition)
111
111
  };
112
112
  }, r);
113
113
  }
114
114
  function rt(t) {
115
- const r = /* @__PURE__ */ new Map(), e = (n, s, i, u) => {
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));
118
118
  return;
119
119
  }
120
- if (!Y(n)) return;
121
- let o = u;
122
- ut(n) && (o = i ?? /* @__PURE__ */ new Set(), r.set(JSON.stringify(s), o), at(n).forEach((a) => o.add(a))), Object.entries(n).forEach(([a, c]) => e(
120
+ if (!_(n)) return;
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(
123
123
  c,
124
124
  [...s, a],
125
- n.type === "exists" && a === "query" ? o : void 0,
126
- o
125
+ n.type === "exists" && a === "query" ? i : void 0,
126
+ i
127
127
  ));
128
128
  };
129
129
  return e(t, [], void 0, /* @__PURE__ */ new Set()), r;
130
130
  }
131
- function Ar(t, r, e) {
131
+ function yr(t, r, e) {
132
132
  const n = tt(
133
133
  t,
134
134
  r,
135
135
  e,
136
136
  [],
137
137
  rt(r).get("[]")
138
- ), s = K(t, r, n), i = rt(s), u = U(s, [], (o, a) => a.length === 0 ? o : tt(t, o, e, a, i.get(JSON.stringify(a))));
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
139
  return K(t, s, u);
140
140
  }
141
- function mr(t, r, e) {
142
- if (!Y(t) || !Array.isArray(t.tables))
141
+ function Dr(t, r, e) {
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, i) => (ct(s, e).forEach((u) => {
146
- const o = $t(t, s, u, e);
147
- (o.missing || o.joins.length > 0 || !lt(s.where, o.condition)) && n.push({ path: i, table: u.table, ...u.alias ? { alias: u.alias } : {} });
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 } : {} });
148
148
  }), s)), n;
149
149
  }
150
150
  function G(t, r) {
151
151
  const e = new Map(r == null ? void 0 : r.tableOf), n = new Set(r == null ? void 0 : r.aliases), s = new Set(r == null ? void 0 : r.derived);
152
- return t.forEach(({ table: i, alias: u }) => {
153
- if (i === void 0) {
152
+ return t.forEach(({ table: o, alias: u }) => {
153
+ if (o === void 0) {
154
154
  u && s.add(u);
155
155
  return;
156
156
  }
157
- if (e.set(i, i), u) {
158
- e.set(u, i), n.add(u), s.delete(u);
157
+ if (e.set(o, o), u) {
158
+ e.set(u, o), n.add(u), s.delete(u);
159
159
  return;
160
160
  }
161
- n.delete(i), s.delete(i);
161
+ n.delete(o), s.delete(o);
162
162
  }), { tableOf: e, aliases: n, derived: s };
163
163
  }
164
- function Wt(t, r, e) {
165
- const n = t.on && L(t.on, r, e);
166
- return t.query ? { ...t, on: n, query: Ft(t.query, (s) => H(s, e)) } : t.shared !== void 0 ? { ...t, on: n } : { ...t, on: n, table: e(t.table) };
164
+ function Qt(t, r, e) {
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) };
167
167
  }
168
- function L(t, r, e) {
168
+ function M(t, r, e) {
169
169
  if (t.type === "field") {
170
170
  if (!t.table || r.derived.has(t.table)) return t;
171
171
  const n = r.tableOf.get(t.table) ?? t.table;
@@ -175,27 +175,27 @@ function L(t, r, e) {
175
175
  ...t.column ? { column: e(n, t.column) } : {}
176
176
  };
177
177
  }
178
- if (t.type === "subquery") return { ...t, query: H(t.query, e) };
178
+ if (t.type === "subquery") return { ...t, query: j(t.query, e) };
179
179
  if (t.type === "exists") {
180
- const { query: n } = t, s = [n.from, ...n.joins], i = G(s), u = G(s, r);
180
+ const { query: n } = t, s = [n.from, ...n.joins], o = G(s), u = G(s, r);
181
181
  return {
182
182
  ...t,
183
- link: L(t.link, u, e),
183
+ link: M(t.link, u, e),
184
184
  query: {
185
185
  ...n,
186
186
  from: { ...n.from, table: e(n.from.table) },
187
- joins: n.joins.map((o) => Wt(o, i, e)),
188
- ...n.where ? { where: L(n.where, i, e) } : {}
187
+ joins: n.joins.map((i) => Qt(i, o, e)),
188
+ ...n.where ? { where: M(n.where, o, e) } : {}
189
189
  }
190
190
  };
191
191
  }
192
- return X(t, (n) => L(n, r, e));
192
+ return k(t, (n) => M(n, r, e));
193
193
  }
194
- function H(t, r) {
195
- const e = G([t.from, ...t.joins]), n = st(
194
+ function j(t, r) {
195
+ const e = G([t.from, ...t.joins]), n = ot(
196
196
  t,
197
- (s) => L(s, e, r),
198
- (s) => H(s, r)
197
+ (s) => M(s, e, r),
198
+ (s) => j(s, r)
199
199
  );
200
200
  return {
201
201
  ...n,
@@ -207,7 +207,7 @@ function x(t, r) {
207
207
  if (Array.isArray(t)) return t.map((s) => x(s, r));
208
208
  if (typeof t != "object" || t === null || t instanceof Date) return t;
209
209
  const e = Object.fromEntries(Object.entries(t).map(
210
- ([s, i]) => [s, x(i, r)]
210
+ ([s, o]) => [s, x(o, r)]
211
211
  ));
212
212
  if (!("from" in e) || !Array.isArray(e.joins)) return e;
213
213
  const n = (s) => s.table !== void 0 && !s.alias && r(s.table) !== s.table ? { ...s, alias: s.table } : s;
@@ -217,17 +217,17 @@ function x(t, r) {
217
217
  joins: e.joins.map(n)
218
218
  };
219
219
  }
220
- function Qt(t, r) {
221
- return H(x(t, r), r);
220
+ function Vt(t, r) {
221
+ return j(x(t, r), r);
222
222
  }
223
- const Vt = /^(\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 Et(t, r) {
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) {
225
225
  let e;
226
- const n = typeof t == "string" ? Vt.exec(t.trim()) : null;
226
+ const n = typeof t == "string" ? Jt.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" ? jt(t, "dateTime") : t;
230
+ const u = typeof t == "string" ? vt(t, "dateTime") : t;
231
231
  u instanceof Date && (e = {
232
232
  YYYY: b(u.getUTCFullYear(), 4),
233
233
  MM: b(u.getUTCMonth() + 1),
@@ -241,20 +241,20 @@ function Et(t, r) {
241
241
  const s = e;
242
242
  return (r.match(et) ?? []).some((u) => s[u] === void 0) ? t : r.replace(et, (u) => s[u]);
243
243
  }
244
- function Tt(t, r) {
244
+ function At(t, r) {
245
245
  const e = /* @__PURE__ */ new Map(), n = (s) => {
246
- const i = /* @__PURE__ */ new Map();
247
- _(s).forEach((c) => i.set(Ut(c), c.table));
246
+ const o = /* @__PURE__ */ new Map();
247
+ Y(s).forEach((c) => o.set(qt(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 = i.has(c.table) ? i.get(c.table) : c.table, $ = f === void 0 ? void 0 : qt(r, f), l = $ == null ? void 0 : $.columns.find((A) => A.name === c.column);
251
- return l == null ? void 0 : l.format;
252
- }, o = (c, f) => {
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);
251
+ return $ == null ? void 0 : $.format;
252
+ }, i = (c, f) => {
253
253
  switch (c.type) {
254
254
  case "literal":
255
- return { ...c, value: Et(c.value, f) };
255
+ return { ...c, value: Tt(c.value, f) };
256
256
  case "list":
257
- return { ...c, items: c.items.map(($) => o($, f)) };
257
+ return { ...c, items: c.items.map((E) => i(E, f)) };
258
258
  case "param":
259
259
  return e.has(c.name) || e.set(c.name, f), c;
260
260
  default:
@@ -262,20 +262,20 @@ function Tt(t, r) {
262
262
  }
263
263
  }, a = (c) => {
264
264
  if (c.type === "subquery") return { ...c, query: n(c.query) };
265
- const f = X(c, a);
265
+ const f = k(c, a);
266
266
  if (f.type !== "binary" || !f.right) return f;
267
- const $ = u(f.left), l = u(f.right);
267
+ const E = u(f.left), $ = u(f.right);
268
268
  return {
269
269
  ...f,
270
- left: l ? o(f.left, l) : f.left,
271
- right: $ ? o(f.right, $) : f.right
270
+ left: $ ? i(f.left, $) : f.left,
271
+ right: E ? i(f.right, E) : f.right
272
272
  };
273
273
  };
274
- return st(s, a, (c) => n(c));
274
+ return ot(s, a, (c) => n(c));
275
275
  };
276
276
  return { query: n(t), params: e };
277
277
  }
278
- const gr = ["mysql", "mssql", "postgres"], yr = {
278
+ const Or = ["mysql", "mssql", "postgres"], Sr = {
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 Jt = /^[A-Za-z_][A-Za-z0-9_]*$/, At = (t) => `${t}`, zt = /\uE000(\d+)\uE000/g;
296
+ const zt = /^[A-Za-z_][A-Za-z0-9_]*$/, mt = (t) => `${t}`, Kt = /\uE000(\d+)\uE000/g;
297
297
  let C;
298
- function Dr(t, r) {
298
+ function Nr(t, r) {
299
299
  C = { ...t, writing: [] };
300
300
  try {
301
301
  return r();
@@ -303,26 +303,26 @@ function Dr(t, r) {
303
303
  C = void 0;
304
304
  }
305
305
  }
306
- function mt(t, r, e) {
306
+ function gt(t, r, e) {
307
307
  if (typeof t != "object" || t === null || t instanceof Date) return;
308
308
  let n;
309
- return Object.entries(t).some(([s, i]) => {
309
+ return Object.entries(t).some(([s, o]) => {
310
310
  const u = [...e, Array.isArray(t) ? Number(s) : s];
311
- return n = i === r ? u : mt(i, r, u), n !== void 0;
311
+ return n = o === r ? u : gt(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], i = typeof t == "string" ? [t] : mt(s.value, t, s.path);
317
- if (!i) return r();
318
- e.push({ value: t, path: i });
316
+ const { writing: e, paths: n } = C, s = e[e.length - 1], o = typeof t == "string" ? [t] : gt(s.value, t, s.path);
317
+ if (!o) return r();
318
+ e.push({ value: t, path: o });
319
319
  try {
320
- return `${n.push(i) - 1}${r()}`;
320
+ return `${n.push(o) - 1}${r()}`;
321
321
  } finally {
322
322
  e.pop();
323
323
  }
324
324
  }
325
- const Kt = {
325
+ const Zt = {
326
326
  mysql: {
327
327
  text: "CHAR",
328
328
  integer: "SIGNED",
@@ -351,25 +351,25 @@ const Kt = {
351
351
  boolean: "boolean"
352
352
  }
353
353
  };
354
- function Zt(t, r) {
354
+ function dt(t, r) {
355
355
  var n;
356
- const e = (n = Kt[r]) == null ? void 0 : n[t];
356
+ const e = (n = Zt[r]) == null ? void 0 : n[t];
357
357
  return e ? r === "postgres" ? `NULL::${e}` : `CAST(NULL AS ${e})` : "NULL";
358
358
  }
359
- function gt(t, r, e) {
359
+ function yt(t, r, e) {
360
360
  if (t == null) return "NULL";
361
361
  if (e)
362
- return e.values.push(t), At(e.values.length - 1);
362
+ return e.values.push(t), mt(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 ? `'${it(t)}'` : String(t);
369
+ return typeof t == "number" || typeof t == "bigint" ? String(t) : t instanceof Date ? `'${ut(t)}'` : String(t);
370
370
  }
371
371
  let q = "native";
372
- function yt(t, r) {
372
+ function Dt(t, r) {
373
373
  q = t ?? "native";
374
374
  try {
375
375
  return r();
@@ -377,7 +377,7 @@ function yt(t, r) {
377
377
  q = "native";
378
378
  }
379
379
  }
380
- function dt(t, r) {
380
+ function tr(t, r) {
381
381
  switch (t) {
382
382
  case "!=":
383
383
  return r === "mssql" ? "<>" : "!=";
@@ -388,7 +388,7 @@ function dt(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 (!Bt.includes(t))
391
+ if (!Gt.includes(t))
392
392
  throw new Error(`unknown operator ${JSON.stringify(t)}`);
393
393
  return t;
394
394
  }
@@ -396,7 +396,7 @@ function dt(t, r) {
396
396
  function h(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
- const v = {
399
+ const B = {
400
400
  YEAR: "year",
401
401
  QUARTER: "quarter",
402
402
  MONTH: "month",
@@ -406,36 +406,42 @@ const v = {
406
406
  MINUTE: "minute",
407
407
  SECOND: "second",
408
408
  MILLISECOND: "millisecond"
409
- }, tr = {
409
+ };
410
+ function nt(t, [r = "??", , e], n) {
411
+ if (n.length < 2) return `${t}(${r})`;
412
+ const s = n[1], o = s.type === "literal" && Number.isInteger(s.value) && s.value >= 0 ? String(s.value) : "??";
413
+ return `${t}(${r}, ${o}${e === void 0 ? "" : `, ${e}`})`;
414
+ }
415
+ const rr = {
410
416
  // ---- Date arithmetic ----
411
417
  DATE_ADD: ([t, r], e, n, s) => {
412
418
  var u;
413
- const i = h(e[2]);
419
+ const o = h(e[2]);
414
420
  switch (n) {
415
421
  case "mysql":
416
- return i === "MILLISECOND" ? `DATE_ADD(${t}, INTERVAL (${r}) * 1000 MICROSECOND)` : `DATE_ADD(${t}, INTERVAL ${r} ${i})`;
422
+ return o === "MILLISECOND" ? `DATE_ADD(${t}, INTERVAL (${r}) * 1000 MICROSECOND)` : `DATE_ADD(${t}, INTERVAL ${r} ${o})`;
417
423
  case "postgres": {
418
- const o = v[i] ?? i.toLowerCase();
419
- return !s && ((u = e[1]) == null ? void 0 : u.type) === "literal" && typeof e[1].value == "number" ? `(${t} + INTERVAL '${e[1].value} ${o}')` : `(${t} + (${r} * INTERVAL '1 ${o}'))`;
424
+ const i = B[o] ?? o.toLowerCase();
425
+ return !s && ((u = e[1]) == null ? void 0 : u.type) === "literal" && typeof e[1].value == "number" ? `(${t} + INTERVAL '${e[1].value} ${i}')` : `(${t} + (${r} * INTERVAL '1 ${i}'))`;
420
426
  }
421
427
  case "mssql":
422
- return `DATEADD(${i}, ${r}, ${t})`;
428
+ return `DATEADD(${o}, ${r}, ${t})`;
423
429
  default:
424
430
  return `DATE_ADD(${t}, ${r}, ${h(e[2])})`;
425
431
  }
426
432
  },
427
433
  DATE_SUB: ([t, r], e, n, s) => {
428
434
  var u;
429
- const i = h(e[2]);
435
+ const o = h(e[2]);
430
436
  switch (n) {
431
437
  case "mysql":
432
- return i === "MILLISECOND" ? `DATE_SUB(${t}, INTERVAL (${r}) * 1000 MICROSECOND)` : `DATE_SUB(${t}, INTERVAL ${r} ${i})`;
438
+ return o === "MILLISECOND" ? `DATE_SUB(${t}, INTERVAL (${r}) * 1000 MICROSECOND)` : `DATE_SUB(${t}, INTERVAL ${r} ${o})`;
433
439
  case "postgres": {
434
- const o = v[i] ?? i.toLowerCase();
435
- return !s && ((u = e[1]) == null ? void 0 : u.type) === "literal" && typeof e[1].value == "number" ? `(${t} - INTERVAL '${e[1].value} ${o}')` : `(${t} - (${r} * INTERVAL '1 ${o}'))`;
440
+ const i = B[o] ?? o.toLowerCase();
441
+ return !s && ((u = e[1]) == null ? void 0 : u.type) === "literal" && typeof e[1].value == "number" ? `(${t} - INTERVAL '${e[1].value} ${i}')` : `(${t} - (${r} * INTERVAL '1 ${i}'))`;
436
442
  }
437
443
  case "mssql":
438
- return `DATEADD(${i}, -(${r}), ${t})`;
444
+ return `DATEADD(${o}, -(${r}), ${t})`;
439
445
  default:
440
446
  return `DATE_SUB(${t}, ${r}, ${h(e[2])})`;
441
447
  }
@@ -453,12 +459,12 @@ const v = {
453
459
  case "YEAR":
454
460
  return `EXTRACT(YEAR FROM age(${r}, ${t}))::integer`;
455
461
  case "QUARTER": {
456
- const i = `EXTRACT(YEAR FROM age(${r}, ${t}))`, u = `EXTRACT(MONTH FROM age(${r}, ${t}))`;
457
- return `FLOOR((${i} * 12 + ${u}) / 3)::integer`;
462
+ const o = `EXTRACT(YEAR FROM age(${r}, ${t}))`, u = `EXTRACT(MONTH FROM age(${r}, ${t}))`;
463
+ return `FLOOR((${o} * 12 + ${u}) / 3)::integer`;
458
464
  }
459
465
  case "MONTH": {
460
- const i = `EXTRACT(YEAR FROM age(${r}, ${t}))`, u = `EXTRACT(MONTH FROM age(${r}, ${t}))`;
461
- return `(${i} * 12 + ${u})::integer`;
466
+ const o = `EXTRACT(YEAR FROM age(${r}, ${t}))`, u = `EXTRACT(MONTH FROM age(${r}, ${t}))`;
467
+ return `(${o} * 12 + ${u})::integer`;
462
468
  }
463
469
  case "WEEK":
464
470
  return `FLOOR((${r}::date - ${t}::date) / 7)`;
@@ -521,7 +527,7 @@ const v = {
521
527
  return `/* unknown unit: ${n} */`;
522
528
  }
523
529
  case "postgres":
524
- return `DATE_TRUNC('${v[n] ?? n.toLowerCase()}', ${t})`;
530
+ return `DATE_TRUNC('${B[n] ?? n.toLowerCase()}', ${t})`;
525
531
  case "mssql":
526
532
  switch (n) {
527
533
  case "YEAR":
@@ -562,308 +568,324 @@ const v = {
562
568
  const e = r[1], n = (e == null ? void 0 : e.type) === "literal" && typeof e.value == "string" && /^[A-Za-z][A-Za-z0-9_ ]*(\(\s*\d+\s*(,\s*\d+\s*)?\))?$/.test(e.value) ? e.value : "??";
563
569
  return `CAST(${t} AS ${n})`;
564
570
  },
571
+ // ---- Window functions (issue 20) ----
572
+ // The offset goes into the SQL as a whole number even in parameterized
573
+ // 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),
565
576
  // ---- String aggregation (issue 137) ----
566
577
  // The separator goes into the SQL as a literal even in parameterized SQL:
567
578
  // MySQL's SEPARATOR takes nothing else.
568
- STRING_AGG: ([t = "??"], r, e, n, s, i) => {
569
- const u = r[1] ?? { type: "literal", value: ", " }, o = u.type === "literal" && typeof u.value == "string" ? gt(u.value, e) : "??", a = s.distinct ? "DISTINCT " : "", c = (l) => {
579
+ 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 = ($) => {
570
581
  var A;
571
- return (A = s.orderBy) != null && A.length ? `ORDER BY ${s.orderBy.map((y) => `${l(y)} ${y.direction === "desc" ? "DESC" : "ASC"}`).join(", ")}` : "";
572
- }, f = (l) => i(l.expression), $ = (l) => l ? ` ${l}` : "";
582
+ return (A = s.orderBy) != null && A.length ? `ORDER BY ${s.orderBy.map((y) => `${$(y)} ${y.direction === "desc" ? "DESC" : "ASC"}`).join(", ")}` : "";
583
+ }, f = ($) => o($.expression), E = ($) => $ ? ` ${$}` : "";
573
584
  switch (e) {
574
585
  case "mysql":
575
- return `GROUP_CONCAT(${a}${t}${$(c(f))} SEPARATOR ${o})`;
586
+ return `GROUP_CONCAT(${a}${t}${E(c(f))} SEPARATOR ${i})`;
576
587
  case "postgres": {
577
- const l = `CAST(${t} AS TEXT)`, A = c((y) => {
588
+ const $ = `CAST(${t} AS TEXT)`, A = c((y) => {
578
589
  if (!s.distinct) return f(y);
579
- if (F(y.expression, r[0])) return l;
590
+ if (F(y.expression, r[0])) return $;
580
591
  throw new Error("PostgreSQL orders STRING_AGG of different values by the value only");
581
592
  });
582
- return `STRING_AGG(${a}${l}, ${o}${$(A)})`;
593
+ return `STRING_AGG(${a}${$}, ${i}${E(A)})`;
583
594
  }
584
595
  case "mssql": {
585
596
  if (s.distinct)
586
597
  throw new Error("SQL Server's STRING_AGG can't join only different values");
587
- const l = c(f), A = l ? ` WITHIN GROUP (${l})` : "";
588
- return `STRING_AGG(CAST(${t} AS NVARCHAR(MAX)), ${o})${A}`;
598
+ const $ = c(f), A = $ ? ` WITHIN GROUP (${$})` : "";
599
+ return `STRING_AGG(CAST(${t} AS NVARCHAR(MAX)), ${i})${A}`;
589
600
  }
590
601
  default:
591
- return `STRING_AGG(${a}${t}, ${o}${$(c(f))})`;
602
+ return `STRING_AGG(${a}${t}, ${i}${E(c(f))})`;
592
603
  }
593
604
  }
594
605
  };
606
+ function er(t, r) {
607
+ var n, s;
608
+ return `OVER (${[
609
+ ...(n = t.partitionBy) != null && n.length ? [`PARTITION BY ${t.partitionBy.map(r).join(", ")}`] : [],
610
+ ...(s = t.orderBy) != null && s.length ? [`ORDER BY ${t.orderBy.map((o) => `${r(o.expression)} ${o.direction === "desc" ? "DESC" : "ASC"}`).join(", ")}`] : []
611
+ ].join(" ")})`;
612
+ }
595
613
  function S(t) {
596
614
  return t.type === "binary" || t.type === "logical" || t.type === "if" || t.type === "case";
597
615
  }
598
- function E(t, r, e, n) {
599
- return m(t, () => rr(t, r, e, n));
600
- }
601
- function nt(t, r, e, n) {
602
- const s = (i) => {
603
- if (!i) return "??";
604
- const u = E(i, r, e, n);
605
- return S(i) ? `(${u})` : u;
616
+ function l(t, r, e, n) {
617
+ return m(t, () => sr(t, r, e, n));
618
+ }
619
+ function nr(t, r, e, n) {
620
+ const s = t.args.map((a) => l(a, r, e, n)), o = rr[t.name.toUpperCase()];
621
+ if (o)
622
+ return o(
623
+ s,
624
+ t.args,
625
+ r,
626
+ n !== void 0,
627
+ t,
628
+ (a) => l(a, r, e, n)
629
+ );
630
+ const u = /^[A-Za-z_][A-Za-z0-9_.]*$/.test(t.name) || t.name === "" ? t.name : "??";
631
+ if (s.length === 0 && u.toUpperCase() === "COUNT") return `${u}(*)`;
632
+ const i = t.distinct && s.length > 0 ? "DISTINCT " : "";
633
+ return `${u}(${i}${s.join(", ")})`;
634
+ }
635
+ function st(t, r, e, n) {
636
+ const s = (o) => {
637
+ if (!o) return "??";
638
+ const u = l(o, r, e, n);
639
+ return S(o) ? `(${u})` : u;
606
640
  };
607
641
  return m(t, () => {
608
- const [i, u] = t.type === "list" ? t.items : [t];
609
- return `${s(i)} AND ${s(u)}`;
642
+ const [o, u] = t.type === "list" ? t.items : [t];
643
+ return `${s(o)} AND ${s(u)}`;
610
644
  });
611
645
  }
612
- function rr(t, r, e, n) {
646
+ function sr(t, r, e, n) {
613
647
  var u;
614
- const s = " ".repeat(e), i = " ".repeat(e + 1);
648
+ const s = " ".repeat(e), o = " ".repeat(e + 1);
615
649
  switch (t.type) {
616
650
  case "literal":
617
- return t.value === "" && !t.empty ? "??" : t.value === null && t.as ? Zt(t.as, r) : gt(t.value, r, n);
651
+ return t.value === "" && !t.empty ? "??" : t.value === null && t.as ? dt(t.as, r) : yt(t.value, r, n);
618
652
  case "param": {
619
- if (!n) return Jt.test(t.name) ? `:${t.name}` : "??";
653
+ if (!n) return zt.test(t.name) ? `:${t.name}` : "??";
620
654
  if (!n.named.has(t.name))
621
655
  throw new Error(`no value for parameter "${t.name}"`);
622
- return n.values.push(n.named.get(t.name)), At(n.values.length - 1);
656
+ return n.values.push(n.named.get(t.name)), mt(n.values.length - 1);
623
657
  }
624
658
  case "field": {
625
659
  if (!t.column) return "??";
626
- const o = N(t.column, r);
627
- return t.table ? `${N(t.table, r)}.${o}` : o;
660
+ const i = N(t.column, r);
661
+ return t.table ? `${N(t.table, r)}.${i}` : i;
628
662
  }
629
663
  case "function": {
630
- const o = t.args.map(($) => E($, r, e, n)), a = tr[t.name.toUpperCase()];
631
- if (a)
632
- return a(
633
- o,
634
- t.args,
635
- r,
636
- n !== void 0,
637
- t,
638
- ($) => E($, r, e, n)
639
- );
640
- const c = /^[A-Za-z_][A-Za-z0-9_.]*$/.test(t.name) || t.name === "" ? t.name : "??";
641
- if (o.length === 0 && c.toUpperCase() === "COUNT") return `${c}(*)`;
642
- const f = t.distinct && o.length > 0 ? "DISTINCT " : "";
643
- return `${c}(${f}${o.join(", ")})`;
664
+ const i = nr(t, r, e, n);
665
+ return t.over ? `${i} ${er(t, (a) => l(a, r, e, n))}` : i;
644
666
  }
645
667
  case "binary": {
646
- const o = t.operator ? dt(t.operator, r) : "=", a = E(t.left, r, e, n), c = S(t.left) ? `(${a})` : a;
647
- if (vt(t.operator)) return `${c} ${o}`;
648
- if (!t.right) return `${c} ${o} ??`;
649
- if (o === "BETWEEN")
650
- return `${c} BETWEEN ${nt(t.right, r, e, n)}`;
651
- if (o === "IN")
652
- return t.right.type === "list" ? `${c} IN ${E(t.right, r, e, n)}` : `${c} IN (${E(t.right, r, e, n)})`;
653
- const f = E(t.right, r, e, n), $ = S(t.right) ? `(${f})` : f;
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}`;
670
+ if (!t.right) return `${c} ${i} ??`;
671
+ if (i === "BETWEEN")
672
+ return `${c} BETWEEN ${st(t.right, r, e, n)}`;
673
+ if (i === "IN")
674
+ return t.right.type === "list" ? `${c} IN ${l(t.right, r, e, n)}` : `${c} IN (${l(t.right, r, e, n)})`;
675
+ const f = l(t.right, r, e, n), E = S(t.right) ? `(${f})` : f;
654
676
  if (t.operator === "IS NOT DISTINCT FROM" && q === "expanded") {
655
- const l = (y) => {
656
- const R = E(y, r, e, n);
677
+ const $ = (y) => {
678
+ const R = l(y, r, e, n);
657
679
  return S(y) ? `(${R})` : R;
658
- }, A = `${l(t.left)} IS NULL AND ${l(t.right)} IS NULL`;
659
- return `(${c} = ${$} OR (${A}))`;
680
+ }, A = `${$(t.left)} IS NULL AND ${$(t.right)} IS NULL`;
681
+ return `(${c} = ${E} OR (${A}))`;
660
682
  }
661
- return `${c} ${o} ${$}`;
683
+ return `${c} ${i} ${E}`;
662
684
  }
663
685
  case "logical": {
664
686
  if (t.operands.length === 0) return "/* empty */";
665
687
  if (t.operands.length === 1)
666
- return E(t.operands[0], r, e, n);
688
+ return l(t.operands[0], r, e, n);
667
689
  if (t.operator !== "AND" && t.operator !== "OR")
668
690
  throw new Error(`unknown operator ${JSON.stringify(t.operator)}`);
669
- const o = `
691
+ const i = `
670
692
  ${s}${t.operator} `;
671
693
  return t.operands.map((a) => {
672
694
  if (a.type === "logical") {
673
- const c = E(a, r, e + 1, n);
695
+ const c = l(a, r, e + 1, n);
674
696
  return `(
675
- ${i}${c}
697
+ ${o}${c}
676
698
  ${s})`;
677
699
  }
678
- return E(a, r, e, n);
679
- }).join(o);
700
+ return l(a, r, e, n);
701
+ }).join(i);
680
702
  }
681
703
  case "not": {
682
- const { operand: o } = t;
683
- if (o.type === "binary" && o.operator === "IN" && ((u = o.right) == null ? void 0 : u.type) === "subquery") {
684
- const a = E(o.left, r, e, n);
685
- return `${S(o.left) ? `(${a})` : a} NOT IN (${E(o.right, r, e, n)})`;
704
+ const { operand: i } = t;
705
+ if (i.type === "binary" && i.operator === "IN" && ((u = i.right) == null ? void 0 : u.type) === "subquery") {
706
+ const a = l(i.left, r, e, n);
707
+ return `${S(i.left) ? `(${a})` : a} NOT IN (${l(i.right, r, e, n)})`;
686
708
  }
687
- if (o.type === "binary" && o.operator === "BETWEEN" && o.right) {
688
- const a = E(o.left, r, e, n);
689
- return `${S(o.left) ? `(${a})` : a} NOT BETWEEN ${nt(o.right, r, e, n)}`;
709
+ if (i.type === "binary" && i.operator === "BETWEEN" && i.right) {
710
+ const a = l(i.left, r, e, n);
711
+ return `${S(i.left) ? `(${a})` : a} NOT BETWEEN ${st(i.right, r, e, n)}`;
690
712
  }
691
- if (o.type === "binary" && o.operator === "IS NOT DISTINCT FROM" && o.right && q === "native") {
692
- if (r === "mysql") return `NOT (${E(o, r, e, n)})`;
693
- const a = E(o.left, r, e, n), c = S(o.left) ? `(${a})` : a, f = E(o.right, r, e, n), $ = S(o.right) ? `(${f})` : f;
694
- return `${c} IS DISTINCT FROM ${$}`;
713
+ if (i.type === "binary" && i.operator === "IS NOT DISTINCT FROM" && i.right && q === "native") {
714
+ if (r === "mysql") return `NOT (${l(i, r, e, n)})`;
715
+ 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
+ return `${c} IS DISTINCT FROM ${E}`;
695
717
  }
696
- if (o.type === "logical" || o.type === "not") {
697
- const a = E(o, r, e + 1, n);
718
+ if (i.type === "logical" || i.type === "not") {
719
+ const a = l(i, r, e + 1, n);
698
720
  return `NOT (
699
- ${i}${a}
721
+ ${o}${a}
700
722
  ${s})`;
701
723
  }
702
- return `NOT ${E(o, r, e, n)}`;
724
+ return `NOT ${l(i, r, e, n)}`;
703
725
  }
704
726
  case "if": {
705
- const o = t.condition ? E(t.condition, r, e, n) : "??", a = t.then ? E(t.then, r, e, n) : "??", c = t.else ? E(t.else, r, e, n) : "NULL";
706
- return r === "mysql" ? `IF(${o}, ${a}, ${c})` : r === "mssql" ? `IIF(${o}, ${a}, ${c})` : `CASE WHEN ${o} THEN ${a} ELSE ${c} END`;
727
+ const i = t.condition ? l(t.condition, r, e, n) : "??", a = t.then ? l(t.then, r, e, n) : "??", c = t.else ? l(t.else, r, e, n) : "NULL";
728
+ return r === "mysql" ? `IF(${i}, ${a}, ${c})` : r === "mssql" ? `IIF(${i}, ${a}, ${c})` : `CASE WHEN ${i} THEN ${a} ELSE ${c} END`;
707
729
  }
708
730
  case "case": {
709
- const o = t.subject ? ` ${E(t.subject, r, e, n)}` : "", a = t.branches.map((f) => {
710
- const $ = E(f.when, r, e + 1, n), l = E(f.then, r, e + 1, n);
711
- return `${i}WHEN ${$} THEN ${l}`;
731
+ const i = t.subject ? ` ${l(t.subject, r, e, n)}` : "", a = t.branches.map((f) => {
732
+ const E = l(f.when, r, e + 1, n), $ = l(f.then, r, e + 1, n);
733
+ return `${o}WHEN ${E} THEN ${$}`;
712
734
  }).join(`
713
735
  `), c = t.else ? `
714
- ${i}ELSE ${E(t.else, r, e + 1, n)}` : "";
715
- return `CASE${o}
736
+ ${o}ELSE ${l(t.else, r, e + 1, n)}` : "";
737
+ return `CASE${i}
716
738
  ${a}${c}
717
739
  ${s}END`;
718
740
  }
719
741
  // EXISTS (SELECT 1 FROM … WHERE <link> AND <where>): the subquery written
720
742
  // in place, one level in, so its params fall in reading order.
721
743
  case "exists": {
722
- const { query: o, link: a } = t, c = (l) => l.type === "logical" && l.operator === "AND" ? l.operands : [l], f = o.where ? { type: "logical", operator: "AND", operands: [...c(a), ...c(o.where)] } : a;
744
+ const { query: i, link: a } = t, c = ($) => $.type === "logical" && $.operator === "AND" ? $.operands : [$], f = i.where ? { type: "logical", operator: "AND", operands: [...c(a), ...c(i.where)] } : a;
723
745
  return `EXISTS (
724
746
  ${[
725
747
  "SELECT 1",
726
748
  // The clauses below write their expressions with formatNode.
727
749
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
728
- Rt(o.from, r),
750
+ pt(i.from, r),
729
751
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
730
- ...o.joins.map((l) => pt(l, r, n)),
752
+ ...i.joins.map(($) => It($, r, n)),
731
753
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
732
- Dt(f, r, n)
754
+ Ot(f, r, n)
733
755
  ].join(`
734
- `).replace(/^/gm, i)}
756
+ `).replace(/^/gm, o)}
735
757
  ${s})`;
736
758
  }
737
759
  // IN's right side (issue 122): its query, on lines of its own, inside the
738
760
  // parentheses IN writes around it.
739
761
  // An empty list is unfinished, like a value not filled in.
740
762
  case "list":
741
- return `(${t.items.map((o) => E(o, r, e, n)).join(", ") || "??"})`;
763
+ return `(${t.items.map((i) => l(i, r, e, n)).join(", ") || "??"})`;
742
764
  case "subquery":
743
765
  return `
744
766
  ${W(t.query, r, n).join(`
745
- `).replace(/^/gm, i)}
767
+ `).replace(/^/gm, o)}
746
768
  ${s}`;
747
769
  default:
748
770
  return "/* unknown */";
749
771
  }
750
772
  }
751
- function Dt(t, r, e) {
773
+ function Ot(t, r, e) {
752
774
  return `WHERE
753
- ${E(t, r, 1, e)}`;
775
+ ${l(t, r, 1, e)}`;
754
776
  }
755
- const er = {
777
+ const or = {
756
778
  inner: "INNER JOIN",
757
779
  left: "LEFT JOIN",
758
780
  right: "RIGHT JOIN",
759
781
  full: "FULL JOIN",
760
782
  cross: "CROSS JOIN"
761
783
  };
762
- let Ot = /* @__PURE__ */ new Map();
763
- function St(t, r, e) {
764
- return t.shared !== void 0 ? N(Ot.get(t.shared) ?? "??", r) : t.query ? `(
765
- ${Nt(t.query, r, e).replace(/^/gm, " ")}
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, " ")}
766
788
  )` : N(t.table, r);
767
789
  }
768
- function Nt(t, r, e) {
790
+ function Rt(t, r, e) {
769
791
  return _t(t).map((n) => W(n, r, e).join(`
770
- `)).join(Yt(t) && !t.all ? `
792
+ `)).join(jt(t) && !t.all ? `
771
793
  UNION
772
794
  ` : `
773
795
  UNION ALL
774
796
  `);
775
797
  }
776
- function nr(t, r, e) {
798
+ function ir(t, r, e) {
777
799
  return t.length === 0 ? [] : [`WITH
778
800
  ${t.map((s) => m(s, () => {
779
- const i = Nt(s.query, r, e).replace(/^/gm, " ");
801
+ const o = Rt(s.query, r, e).replace(/^/gm, " ");
780
802
  return ` ${N(s.name, r)} AS (
781
- ${i}
803
+ ${o}
782
804
  )`;
783
805
  })).join(`,
784
806
  `)}`];
785
807
  }
786
- function Rt(t, r, e) {
808
+ function pt(t, r, e) {
787
809
  const n = t.alias ? ` AS ${N(t.alias, r)}` : "";
788
- return `FROM ${m(t, () => `${St(t, r, e)}${n}`)}`;
810
+ return `FROM ${m(t, () => `${Nt(t, r, e)}${n}`)}`;
789
811
  }
790
- function pt(t, r, e) {
812
+ function It(t, r, e) {
791
813
  return m(t, () => {
792
- const n = St(t, r, e), s = t.alias ? ` AS ${N(t.alias, r)}` : "";
814
+ const n = Nt(t, r, e), s = t.alias ? ` AS ${N(t.alias, r)}` : "";
793
815
  if (t.joinType === "cross") return `CROSS JOIN ${n}${s}`;
794
- const i = t.on ? E(t.on, r, 1, e) : "/* unconfigured */";
795
- return `${er[t.joinType]} ${n}${s}
796
- ON ${i}`;
816
+ const o = t.on ? l(t.on, r, 1, e) : "/* unconfigured */";
817
+ return `${or[t.joinType]} ${n}${s}
818
+ ON ${o}`;
797
819
  });
798
820
  }
799
- function sr(t, r, e, n = "SELECT") {
821
+ function ur(t, r, e, n = "SELECT") {
800
822
  if (t.length === 0) return `${n} ${m(t, () => "*")}`;
801
- const s = t.map((i) => m(i, () => {
802
- const u = E(i.expression, r, 1, e), o = i.alias ? ` AS ${N(i.alias, r)}` : "";
803
- return `${u}${o}`;
823
+ const s = t.map((o) => m(o, () => {
824
+ const u = l(o.expression, r, 1, e), i = o.alias ? ` AS ${N(o.alias, r)}` : "";
825
+ return `${u}${i}`;
804
826
  }));
805
827
  return `${n}
806
828
  ${s.join(`,
807
829
  `)}`;
808
830
  }
809
- function or(t, r, e) {
831
+ function cr(t, r, e) {
810
832
  return `GROUP BY
811
833
  ${t.map((s) => m(
812
834
  s,
813
- () => E(s.expression, r, 1, e)
835
+ () => l(s.expression, r, 1, e)
814
836
  )).join(`,
815
837
  `)}`;
816
838
  }
817
- function ir(t, r, e) {
839
+ function ar(t, r, e) {
818
840
  return `HAVING
819
- ${E(t, r, 1, e)}`;
841
+ ${l(t, r, 1, e)}`;
820
842
  }
821
- function ur(t, r, e, n) {
843
+ function fr(t, r, e, n) {
822
844
  return `ORDER BY
823
- ${t.map((i) => m(i, () => `${E(n(i.expression), r, 1, e)} ${i.direction === "desc" ? "DESC" : "ASC"}`)).join(`,
845
+ ${t.map((o) => m(o, () => `${l(n(o.expression), r, 1, e)} ${o.direction === "desc" ? "DESC" : "ASC"}`)).join(`,
824
846
  `)}`;
825
847
  }
826
- const k = (t) => t !== void 0 && Number.isInteger(t) && t >= 0 ? t : void 0;
827
- function It(t, r) {
828
- const e = r.resolveName ? Qt(t, r.resolveName) : t;
829
- return r.defaultLimit !== void 0 && k(e.limit) === void 0 ? { ...e, limit: r.defaultLimit } : e;
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;
830
852
  }
831
- const cr = "18446744073709551615";
853
+ const $r = "18446744073709551615";
832
854
  function W(t, r, e, n = `SELECT${t.distinct ? " DISTINCT" : ""}`) {
833
855
  const s = [
834
- sr(t.select, r, e, n),
835
- Rt(t.from, r, e),
836
- ...t.joins.map((u) => pt(u, r, e))
856
+ ur(t.select, r, e, n),
857
+ pt(t.from, r, e),
858
+ ...t.joins.map((u) => It(u, r, e))
837
859
  ];
838
- t.where && s.push(Dt(t.where, r, e));
839
- const i = t.groupBy ?? [];
840
- if (i.length > 0 && s.push(or(i, r, e)), t.having) {
841
- const u = r === "mysql" ? ot(t.having, t) : t.having;
842
- s.push(ir(u, r, e));
860
+ t.where && s.push(Ot(t.where, r, e));
861
+ 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));
843
865
  }
844
866
  return s;
845
867
  }
846
- function ht(t, r, e) {
847
- C && (C.writing = [{ value: t, path: [] }]), Ot = new Map((t.shared ?? []).map((f) => [f.id, f.name]));
848
- const n = nr(t.shared ?? [], r, e), s = k(t.limit), i = k(t.offset) || void 0, u = t.orderBy ?? [], o = r === "mssql" && s !== void 0 && i === void 0, a = `SELECT${t.distinct ? " DISTINCT" : ""}${o ? ` ${m("limit", () => `TOP (${s})`)}` : ""}`, c = W(t, r, e, a);
849
- if (u.length > 0 && c.push(ur(u, r, e, (f) => r === "mysql" ? ot(f, t) : f)), r === "mssql") {
850
- if (i !== void 0) {
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") {
872
+ if (o !== void 0) {
851
873
  u.length === 0 && c.push("ORDER BY (SELECT NULL)");
852
874
  const f = s !== void 0 ? ` ${m("limit", () => `FETCH NEXT ${s} ROWS ONLY`)}` : "";
853
- c.push(`${m("offset", () => `OFFSET ${i} ROWS`)}${f}`);
875
+ c.push(`${m("offset", () => `OFFSET ${o} ROWS`)}${f}`);
854
876
  }
855
- } else if (s !== void 0 || i !== void 0) {
856
- const f = s !== void 0 || r === "mysql" ? m(s !== void 0 ? "limit" : {}, () => `LIMIT ${s ?? cr}`) : "", $ = i !== void 0 ? m("offset", () => `OFFSET ${i}`) : "";
857
- c.push([f, $].filter(Boolean).join(" "));
877
+ } 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}`) : "";
879
+ c.push([f, E].filter(Boolean).join(" "));
858
880
  }
859
881
  return [...n, ...c].join(`
860
882
  `);
861
883
  }
862
- function Or(t, r, e = {}) {
863
- const n = e.values ? Pt(t, e.values) : t, s = e.schema ? Tt(n, e.schema).query : n, i = It(s, e);
864
- return yt(e.nullSafeEqual, () => ht(i, r));
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));
865
887
  }
866
- function ar(t, r) {
888
+ function lr(t, r) {
867
889
  switch (t) {
868
890
  case "mysql":
869
891
  return "?";
@@ -875,29 +897,29 @@ function ar(t, r) {
875
897
  return "?";
876
898
  }
877
899
  }
878
- function Sr(t, r, e = {}) {
879
- const n = e.schema ? Tt(t, e.schema) : { query: t, params: /* @__PURE__ */ new Map() }, s = {
900
+ function pr(t, r, e = {}) {
901
+ const n = e.schema ? At(t, e.schema) : { query: t, params: /* @__PURE__ */ new Map() }, s = {
880
902
  values: [],
881
903
  named: new Map(Object.entries(e.values ?? {}).map(([c, f]) => {
882
- const $ = n.params.get(c);
883
- return [c, $ ? Et(f, $) : f];
904
+ const E = n.params.get(c);
905
+ return [c, E ? Tt(f, E) : f];
884
906
  }))
885
- }, i = It(n.query, e), u = yt(
907
+ }, o = ht(n.query, e), u = Dt(
886
908
  e.nullSafeEqual,
887
- () => ht(i, r, s)
888
- ), o = [];
889
- return { sql: u.replace(zt, (c, f) => {
890
- const $ = s.values[Number(f)];
891
- return o.push($ instanceof Date ? it($) : $), ar(r, o.length);
892
- }), params: o };
909
+ () => Ct(o, r, s)
910
+ ), i = [];
911
+ return { sql: u.replace(Kt, (c, f) => {
912
+ const E = s.values[Number(f)];
913
+ return i.push(E instanceof Date ? ut(E) : E), lr(r, i.length);
914
+ }), params: i };
893
915
  }
894
916
  export {
895
- gr as D,
896
- Gt as S,
897
- Sr as a,
898
- Or as b,
899
- yr as c,
900
- Ar as s,
901
- mr as u,
902
- Dr as w
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
903
925
  };