@nongt/visql 0.3.1 → 0.4.1

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.
@@ -3,7 +3,7 @@ import { createContext as _e, useContext as qe, useMemo as be, useState as Ie, u
3
3
  import te from "clsx";
4
4
  import { NavArrowDownSolid as xt, Trash as ct, Plus as Pn, DatabaseScript as io, EditPencil as Oc, Xmark as ao, Expand as Ln, NavArrowUpSolid as St, NetworkLeft as lo, MinusSquare as is, PlusCircleSolid as Mt, CheckSquareSolid as Na, Square as Ia, WarningTriangle as co, KeyframesPlus as qc, Collapse as Dc, MinusSquareDashed as Lc, Search as Fc, XmarkCircleSolid as Sa, Play as Rc } from "iconoir-react";
5
5
  import { Position as xe, Handle as ve, useReactFlow as gr, useStore as di, useUpdateNodeInternals as uo, useStoreApi as Pc, useInternalNode as Mc, ViewportPortal as _c, useNodesState as Ca, useEdgesState as Ta, applyNodeChanges as Ea, ReactFlow as ka, Controls as Ba, Background as Aa, ReactFlowProvider as Oa } from "@xyflow/react";
6
- import { L as $c, N as ht, O as Us, P as qa, R as Da, T as La, U as Fa, V as as, D as Ra, W as jc, X as Uc, Y as zc, Z as Gc, _ as Hc, $ as Qc, a0 as ui, a1 as Vc, a2 as Wc, a3 as Yc, a4 as Kc, b as Jc, a as ho, a5 as Xc, a6 as Zc, M as ed, a7 as Pa, a8 as Ma, H as hi, a9 as Dr, aa as fi, ab as td, ac as nd, ad as rd, ae as sd, K as _a, af as od, c as Ts, ag as id, J as ad, ah as rt, ai as ld, aj as rr, ak as cd, al as pi, am as dd, an as ud, ao as hd, ap as mi, aq as gi, A as fd, z as pd, p as md, ar as $a, n as gd, m as yd, as as yi, at as bd, au as bi, e as xi, s as xd, f as vd, l as wd, j as vi, h as Nd, u as Id, r as Sd, g as Cd, w as Td, x as Ed, v as kd, t as Bd, av as wi, aw as Es, ax as ks, ay as Ni, az as Ad, q as Od, d as qd } from "./issueLocation-Cw5sttQL.js";
6
+ import { L as $c, N as ht, O as Us, P as qa, R as Da, T as La, U as Fa, V as as, D as Ra, W as jc, X as Uc, Y as zc, Z as Gc, _ as Hc, $ as Qc, a0 as ui, a1 as Vc, a2 as Wc, a3 as Yc, a4 as Kc, b as Jc, a as ho, a5 as Xc, a6 as Zc, M as ed, a7 as Pa, a8 as Ma, H as hi, a9 as Dr, aa as fi, ab as td, ac as nd, ad as rd, ae as sd, K as _a, af as od, c as Ts, ag as id, J as ad, ah as rt, ai as ld, aj as rr, ak as cd, al as pi, am as dd, an as ud, ao as hd, ap as mi, aq as gi, A as fd, z as pd, p as md, ar as $a, n as gd, m as yd, as as yi, at as bd, au as bi, e as xi, s as xd, f as vd, l as wd, j as vi, h as Nd, u as Id, r as Sd, g as Cd, w as Td, x as Ed, v as kd, t as Bd, av as wi, aw as Es, ax as ks, ay as Ni, az as Ad, q as Od, d as qd } from "./issueLocation-CWUdTtCh.js";
7
7
  import { t as Dd, d as ja, a as Ld, s as Ua, i as za, b as Fd } from "./calendarDate-CYlr9TvX.js";
8
8
  import { t as qn, R as Ga, a as Rd, D as Ii, r as Pd, d as Md } from "./tokens-BTaVMSOx.js";
9
9
  import { D as Mn, J as Gt, P as ls, t as Nt, k as Ve, y as Ha, E as Qa, a as _d, R as $d, S as jd, d as Jt, A as lr, T as Ud, M as yr, p as Va, U as Wa, q as jr, i as zs, l as zd, V as Ya, e as Ka, j as Gd, r as Hd, f as Qd, L as Vd, v as Wd, W as Si, X as Yd, u as Kd, b as Jd, h as Ja } from "./schema-z95dxLxu.js";
@@ -1,68 +1,119 @@
1
- var Et = Object.defineProperty;
2
- var Tt = (t, e, r) => e in t ? Et(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
3
- var b = (t, e, r) => Tt(t, typeof e != "symbol" ? e + "" : e, r);
4
- import { B as x, q as d, F as yt, g as F, d as H, t as Q, h as pt, w as k, o as X, H as At, k as mt, b as gt, i as Dt } from "./schema-z95dxLxu.js";
5
- import { ag as D, aA as V, aB as Nt, aC as Ot, aD as ht, aj as Rt, V as St, ap as It, aE as y, al as bt, a7 as Ct, aF as Lt, aG as W, $ as Mt } from "./issueLocation-Cw5sttQL.js";
6
- import { s as J } from "./calendarDate-CYlr9TvX.js";
7
- class wt extends Error {
8
- constructor(r, n, s) {
9
- super(`scopeQuery: the query at ${r.length ? r.join(".") : "the top"} reads "${n}" but can't reach "${s}", which the condition names`);
1
+ var pt = Object.defineProperty;
2
+ var At = (t, e, r) => e in t ? pt(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
3
+ var b = (t, e, r) => At(t, typeof e != "symbol" ? e + "" : e, r);
4
+ import { q as L, F as mt, G as gt, g as M, o as H, d as G, t as V, h as ht, w as W, H as Dt, k as Nt, b as Ot, i as St } from "./schema-z95dxLxu.js";
5
+ import { ag as D, aA as J, aB as bt, aC as Rt, aD as It, aj as Ct, V as Lt, ap as Mt, aE as A, al as wt, a7 as Ft, aF as qt, aG as K, $ as Ut } from "./issueLocation-CWUdTtCh.js";
6
+ import { s as z } from "./calendarDate-CYlr9TvX.js";
7
+ class vt extends Error {
8
+ constructor(r, n, s, i) {
9
+ const a = i ? `"${n}" (as "${i}")` : `"${n}"`;
10
+ super(`scopeQuery: the query at ${r.length ? r.join(".") : "the top"} reads ${a} but can't reach "${s}", which the condition names`);
10
11
  /** Where the body is, as keys into the query (validateQuery's
11
12
  * `pathSegments` style). */
12
13
  b(this, "path");
13
14
  /** The table it reads that needs scope. */
14
15
  b(this, "table");
15
- this.name = "ScopeError", this.path = r, this.table = n;
16
+ /** The alias that copy of the table goes by, when it has one (a self
17
+ * join reads a table more than once, issue 216). */
18
+ b(this, "alias");
19
+ this.name = "ScopeError", this.path = r, this.table = n, i && (this.alias = i);
16
20
  }
17
21
  }
18
- const K = (t) => Object.prototype.toString.call(t) === "[object Object]", Ft = (t) => Array.isArray(t.joins) && K(t.from);
19
- function C(t, e, r) {
22
+ const Y = (t) => Object.prototype.toString.call(t) === "[object Object]", dt = (t) => Array.isArray(t.joins) && Y(t.from);
23
+ function w(t, e, r) {
20
24
  if (Array.isArray(t)) {
21
- const a = t.map((o, f) => C(o, [...e, f], r));
25
+ const a = t.map((o, f) => w(o, [...e, f], r));
22
26
  return a.every((o, f) => o === t[f]) ? t : a;
23
27
  }
24
- if (!K(t)) return t;
28
+ if (!Y(t)) return t;
25
29
  let n = !1;
26
30
  const s = Object.entries(t).map(([a, o]) => {
27
- const f = C(o, [...e, a], r);
31
+ const f = w(o, [...e, a], r);
28
32
  return f !== o && (n = !0), [a, f];
29
33
  }), i = n ? Object.fromEntries(s) : t;
30
- return Ft(i) ? r(i, e) : i;
31
- }
32
- const z = (t, e) => x(t).find(e.requires);
33
- function Z(t, e) {
34
- return t ? F(t, e) ? !0 : t.type === "logical" && t.operator === "AND" && t.operands.some((r) => F(r, e)) : !1;
35
- }
36
- function j(t, e, r, n) {
37
- const s = z(e, r);
38
- if (!s || Z(e.where, r.condition)) return e;
39
- const i = e, a = x(i), o = d(i).flatMap(($) => $.alias ? [$.alias] : []), f = V(r.condition).find(($) => !o.includes($) && yt(t, a, $) === void 0);
40
- if (f) throw new wt(n, s, f);
41
- const { joins: u } = Nt(
42
- t,
43
- {
44
- id: "",
45
- from: e.from,
46
- joins: e.joins,
47
- select: []
48
- },
49
- r.condition
50
- ), { where: c } = e;
51
- let l = r.condition;
52
- return (c == null ? void 0 : c.type) === "logical" && c.operator === "AND" ? l = { ...c, operands: [...c.operands, r.condition] } : c && (l = { type: "logical", operator: "AND", operands: [c, r.condition] }), { ...e, joins: u, where: l };
53
- }
54
- function le(t, e, r) {
55
- const n = j(t, e, r, []), s = D(t, e, n), i = C(s, [], (a, o) => o.length === 0 ? a : j(t, a, r, o));
34
+ return dt(i) ? r(i, e) : i;
35
+ }
36
+ const Z = (t, e) => L(t).flatMap((r) => r.table !== void 0 && e.requires(r.table) ? [{ table: r.table, ...r.alias ? { alias: r.alias } : {}, ref: r.alias ?? r.table }] : []), Bt = (t) => L(t).flatMap((e) => {
37
+ const r = e.alias ?? e.table;
38
+ return r === void 0 ? [] : [r];
39
+ });
40
+ function v(t, e) {
41
+ if (t.type === "field") {
42
+ const r = t.table && e.get(t.table);
43
+ return r ? { ...t, table: r } : t;
44
+ }
45
+ return H(t, (r) => v(r, e));
46
+ }
47
+ const tt = (t) => t ? t.type === "logical" && t.operator === "AND" ? t.operands : [t] : [];
48
+ function jt(t, e) {
49
+ var s, i;
50
+ if (e.type !== "binary" || ((s = e.left) == null ? void 0 : s.type) !== "field" || ((i = e.right) == null ? void 0 : i.type) !== "field")
51
+ return !1;
52
+ const r = [e.left.table, e.right.table], n = { ...e, left: e.right, right: e.left };
53
+ return t.joins.some((a) => r.includes(a.alias ?? a.table) && tt(a.on).some((o) => M(o, e) || M(o, n)));
54
+ }
55
+ function et(t, e, r, n) {
56
+ const s = /* @__PURE__ */ new Map([[r.table, r.ref]]), i = [], a = new Set(Bt(e));
57
+ let o;
58
+ return J(n.condition).forEach((f) => {
59
+ if (o || s.has(f)) return;
60
+ const u = mt(t, [...s.keys()], f);
61
+ if (!u) {
62
+ o = f;
63
+ return;
64
+ }
65
+ u.forEach((c) => {
66
+ const l = gt(t, [...s.keys()], c);
67
+ if (!l) return;
68
+ const $ = (m) => v(l.on, new Map([...s, [c, m]])), y = L(e).flatMap((m) => m.table === c ? [m.alias ?? c] : []).find((m) => jt(e, $(m)));
69
+ if (y) {
70
+ s.set(c, y);
71
+ return;
72
+ }
73
+ let T = c;
74
+ for (let m = 2; a.has(T); m += 1) T = `${c}_${m}`;
75
+ a.add(T), s.set(c, T), i.push({
76
+ id: bt(),
77
+ table: c,
78
+ ...T !== c ? { alias: T } : {},
79
+ joinType: l.joinType,
80
+ on: $(T)
81
+ });
82
+ });
83
+ }), { joins: i, condition: v(n.condition, s), ...o ? { missing: o } : {} };
84
+ }
85
+ function rt(t, e) {
86
+ return tt(t).some((r) => M(r, e));
87
+ }
88
+ function _t(t, e) {
89
+ return (t == null ? void 0 : t.type) === "logical" && t.operator === "AND" ? { ...t, operands: [...t.operands, e] } : t ? { type: "logical", operator: "AND", operands: [t, e] } : e;
90
+ }
91
+ function x(t, e, r, n) {
92
+ return Z(e, r).reduce((s, i) => {
93
+ const a = et(t, s, i, r);
94
+ if (a.missing) throw new vt(n, i.table, a.missing, i.alias);
95
+ const o = rt(s.where, a.condition);
96
+ return a.joins.length === 0 && o ? s : {
97
+ ...s,
98
+ joins: [...s.joins, ...a.joins],
99
+ where: o ? s.where : _t(s.where, a.condition)
100
+ };
101
+ }, e);
102
+ }
103
+ function me(t, e, r) {
104
+ const n = x(t, e, r, []), s = D(t, e, n), i = w(s, [], (a, o) => o.length === 0 ? a : x(t, a, r, o));
56
105
  return D(t, s, i);
57
106
  }
58
- function $e(t, e) {
59
- const r = [];
60
- return C(t, [], (n, s) => {
61
- const i = z(n, e);
62
- return i && !Z(n.where, e.condition) && r.push({ path: s, table: i }), n;
63
- }), r;
107
+ function ge(t, e, r) {
108
+ if (!Y(t) || !Array.isArray(t.tables))
109
+ throw new TypeError("unscopedBodies takes (schema, query, rule) since 0.4.0");
110
+ const n = [];
111
+ return w(e, [], (s, i) => (Z(s, r).forEach((a) => {
112
+ const o = et(t, s, a, r);
113
+ (o.missing || o.joins.length > 0 || !rt(s.where, o.condition)) && n.push({ path: i, table: a.table, ...a.alias ? { alias: a.alias } : {} });
114
+ }), s)), n;
64
115
  }
65
- const qt = (t) => typeof t == "object" && t !== null && !(t instanceof Date), Y = {
116
+ const Ht = (t) => typeof t == "object" && t !== null && !(t instanceof Date), Q = {
66
117
  /** A column: `expr.field('orders', 'status')`. */
67
118
  field: (t, e) => ({ type: "field", table: t, column: e }),
68
119
  /** A literal value, written into the SQL (as a bound parameter by
@@ -129,7 +180,7 @@ const qt = (t) => typeof t == "object" && t !== null && !(t instanceof Date), Y
129
180
  type: "binary",
130
181
  operator: "IN",
131
182
  left: t,
132
- right: Array.isArray(e) ? Y.list(...e) : e
183
+ right: Array.isArray(e) ? Q.list(...e) : e
133
184
  }),
134
185
  /** `left BETWEEN low AND high` (issue 169), both bounds included: each a
135
186
  * value or an expression (a param, a relative date). NOT BETWEEN:
@@ -138,13 +189,13 @@ const qt = (t) => typeof t == "object" && t !== null && !(t instanceof Date), Y
138
189
  type: "binary",
139
190
  operator: "BETWEEN",
140
191
  left: t,
141
- right: Y.list(e, r)
192
+ right: Q.list(e, r)
142
193
  }),
143
194
  /** IN's values: each a value (`expr.list(1, 2)`) or an expression
144
195
  * (`expr.list(expr.param('a'), expr.param('b'))`). */
145
196
  list: (...t) => ({
146
197
  type: "list",
147
- items: t.map((e) => qt(e) ? e : { type: "literal", value: e })
198
+ items: t.map((e) => Ht(e) ? e : { type: "literal", value: e })
148
199
  }),
149
200
  /** A query of its own for IN to compare with, selecting `select` — one
150
201
  * column. Its tables are its own: it sees nothing of the query around
@@ -153,91 +204,91 @@ const qt = (t) => typeof t == "object" && t !== null && !(t instanceof Date), Y
153
204
  * 'customer_id') }))`. */
154
205
  subquery: ({ select: t, ...e }) => ({
155
206
  type: "subquery",
156
- id: H(),
157
- query: { joins: [], ...e, select: [{ id: H(), expression: t }] }
207
+ id: G(),
208
+ query: { joins: [], ...e, select: [{ id: G(), expression: t }] }
158
209
  })
159
210
  };
160
- function Ee(t, e, r) {
161
- return y(t, e, { type: "setBaseTable", table: r });
211
+ function he(t, e, r) {
212
+ return A(t, e, { type: "setBaseTable", table: r });
162
213
  }
163
- function Te(t, e, r, n = {}) {
214
+ function De(t, e, r, n = {}) {
164
215
  const s = { type: "addJoin", table: r, ...n };
165
- return y(t, e, s);
216
+ return A(t, e, s);
166
217
  }
167
- function ye(t, e, r) {
168
- return y(t, e, { type: "removeJoin", joinId: r });
218
+ function Ne(t, e, r) {
219
+ return A(t, e, { type: "removeJoin", joinId: r });
169
220
  }
170
- function Ut(t, e, r, n = {}) {
171
- const s = Ot(e), i = V(r);
221
+ function Yt(t, e, r, n = {}) {
222
+ const s = Rt(e), i = J(r);
172
223
  if (s && i.some((u) => u !== s.alias)) {
173
224
  if (i.includes(s.alias))
174
225
  throw new Error("addCondition: a condition names both the union and tables of its branches: add it in two parts");
175
226
  return s.query.union.forEach((u) => {
176
- const c = u.from.table, l = new Set(d(u).map(Q)), $ = ht(t, c), p = i.find((A) => !l.has(A) && !$.has(A));
177
- if (p)
178
- throw new Error(`addCondition: "${p}" can't be reached from "${c}", one of the union's tables: a condition must reach every branch`);
179
- }), s.query.union.reduce((u, c, l) => Rt(
227
+ const c = u.from.table, l = new Set(L(u).map(V)), $ = It(t, c), y = i.find((T) => !l.has(T) && !$.has(T));
228
+ if (y)
229
+ throw new Error(`addCondition: "${y}" can't be reached from "${c}", one of the union's tables: a condition must reach every branch`);
230
+ }), s.query.union.reduce((u, c, l) => Ct(
180
231
  u,
181
- [St(s.id, l)],
182
- ($) => Ut(t, $, r, n)
232
+ [Lt(s.id, l)],
233
+ ($) => Yt(t, $, r, n)
183
234
  ), e);
184
235
  }
185
236
  const a = n.operator ?? "AND", { where: o } = e;
186
237
  let f;
187
- return o ? o.type === "logical" && o.operator === a ? f = { ...o, operands: [...o.operands, r] } : f = { type: "logical", operator: a, operands: [o, r] } : f = r, D(t, e, It(t, e, f));
238
+ return o ? o.type === "logical" && o.operator === a ? f = { ...o, operands: [...o.operands, r] } : f = { type: "logical", operator: a, operands: [o, r] } : f = r, D(t, e, Mt(t, e, f));
188
239
  }
189
- function pe(t, e, r, n) {
190
- const s = y(t, e, { type: "addSelectItem" }), i = s.select[s.select.length - 1].id, a = y(t, s, {
240
+ function Oe(t, e, r, n) {
241
+ const s = A(t, e, { type: "addSelectItem" }), i = s.select[s.select.length - 1].id, a = A(t, s, {
191
242
  type: "setSelectExpression",
192
243
  selectId: i,
193
244
  expression: r
194
- }), o = n === void 0 ? a : y(t, a, { type: "setSelectAlias", selectId: i, alias: n });
245
+ }), o = n === void 0 ? a : A(t, a, { type: "setSelectAlias", selectId: i, alias: n });
195
246
  return D(t, e, o);
196
247
  }
197
- function Ae(t, e, r) {
198
- const n = y(t, e, { type: "addGroupByItem" }), { id: s } = n.groupBy[n.groupBy.length - 1], i = y(t, n, {
248
+ function Se(t, e, r) {
249
+ const n = A(t, e, { type: "addGroupByItem" }), { id: s } = n.groupBy[n.groupBy.length - 1], i = A(t, n, {
199
250
  type: "setGroupByExpression",
200
251
  groupById: s,
201
252
  expression: r
202
253
  });
203
254
  return D(t, e, i);
204
255
  }
205
- function me(t, e, r) {
206
- return D(t, e, bt(t, e, r));
256
+ function be(t, e, r) {
257
+ return D(t, e, wt(t, e, r));
207
258
  }
208
- function ge(t, e, r, n = "asc") {
209
- const s = y(t, e, { type: "addOrderByItem" }), { id: i } = s.orderBy[s.orderBy.length - 1], a = y(t, s, {
259
+ function Re(t, e, r, n = "asc") {
260
+ const s = A(t, e, { type: "addOrderByItem" }), { id: i } = s.orderBy[s.orderBy.length - 1], a = A(t, s, {
210
261
  type: "setOrderByExpression",
211
262
  orderById: i,
212
263
  expression: r
213
- }), o = y(t, a, {
264
+ }), o = A(t, a, {
214
265
  type: "setOrderByDirection",
215
266
  orderById: i,
216
267
  direction: n
217
268
  });
218
269
  return D(t, e, o);
219
270
  }
220
- function De(t, e, r) {
271
+ function Ie(t, e, r) {
221
272
  let n = e;
222
- return "limit" in r && (n = y(t, n, { type: "setLimit", limit: r.limit })), "offset" in r && (n = y(t, n, { type: "setOffset", offset: r.offset })), n;
273
+ 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;
223
274
  }
224
- function Ne(t, e, r) {
225
- return y(t, e, { type: "setDistinct", distinct: r });
275
+ function Ce(t, e, r) {
276
+ return A(t, e, { type: "setDistinct", distinct: r });
226
277
  }
227
- function Oe(t) {
228
- if (!pt(t)) throw new Error(`query "${t.id}" has no base table yet`);
278
+ function Le(t) {
279
+ if (!ht(t)) throw new Error(`query "${t.id}" has no base table yet`);
229
280
  return t;
230
281
  }
231
- const q = 4;
232
- class h extends Error {
282
+ const d = 4;
283
+ class S extends Error {
233
284
  constructor(r, n) {
234
285
  super(n);
235
286
  b(this, "kind");
236
287
  this.name = "QueryFormatError", this.kind = r;
237
288
  }
238
289
  }
239
- const I = (t) => typeof t == "object" && t !== null && !Array.isArray(t);
240
- function vt(t) {
290
+ const C = (t) => typeof t == "object" && t !== null && !Array.isArray(t);
291
+ function Pt(t) {
241
292
  const e = [];
242
293
  let r = 0;
243
294
  for (; r < t.length; ) {
@@ -256,65 +307,65 @@ function vt(t) {
256
307
  }
257
308
  return e;
258
309
  }
259
- function Bt(t) {
310
+ function Gt(t) {
260
311
  const e = (r) => {
261
312
  if (Array.isArray(r)) return r.map(e);
262
- if (!I(r)) return r;
313
+ if (!C(r)) return r;
263
314
  const n = Object.fromEntries(Object.entries(r).map(([a, o]) => [a, e(o)])), { right: s } = n;
264
- if (n.type !== "binary" || n.operator !== "IN" || !I(s) || s.type === "subquery" || s.type === "list") return n;
265
- const i = s.type === "literal" && typeof s.value == "string" ? vt(s.value).map((a) => ({ type: "literal", value: a })) : [s];
315
+ if (n.type !== "binary" || n.operator !== "IN" || !C(s) || s.type === "subquery" || s.type === "list") return n;
316
+ const i = s.type === "literal" && typeof s.value == "string" ? Pt(s.value).map((a) => ({ type: "literal", value: a })) : [s];
266
317
  return { ...n, right: { type: "list", items: i } };
267
318
  };
268
319
  return e(t);
269
320
  }
270
- const dt = (t) => t, _t = (t) => t, Ht = [Bt, dt, _t];
271
- function jt(t, e, r) {
321
+ const xt = (t) => t, Qt = (t) => t, Xt = [Gt, xt, Qt];
322
+ function kt(t, e, r) {
272
323
  return r.slice(e - 1).reduce((n, s) => s(n), t);
273
324
  }
274
- function L(t, e) {
275
- return Array.isArray(t) ? t.map((r) => L(r, e)) : I(t) ? t.type === "literal" ? e(t) : Object.fromEntries(Object.entries(t).map(([r, n]) => [r, L(n, e)])) : t;
325
+ function F(t, e) {
326
+ return Array.isArray(t) ? t.map((r) => F(r, e)) : C(t) ? t.type === "literal" ? e(t) : Object.fromEntries(Object.entries(t).map(([r, n]) => [r, F(n, e)])) : t;
276
327
  }
277
- function Yt(t) {
328
+ function Vt(t) {
278
329
  return t.value instanceof Date ? { ...t, value: t.value.toISOString(), valueType: "date" } : typeof t.value == "bigint" ? { ...t, value: String(t.value), valueType: "bigint" } : t;
279
330
  }
280
- function Pt(t) {
331
+ function Wt(t) {
281
332
  if (t.valueType === void 0) return t;
282
333
  const { valueType: e, ...r } = t, n = String(r.value);
283
334
  let s;
284
335
  if (e === "date") {
285
336
  if (s = new Date(n), Number.isNaN(s.getTime()))
286
- throw new h("invalid", `not a date: ${JSON.stringify(n)}`);
337
+ throw new S("invalid", `not a date: ${JSON.stringify(n)}`);
287
338
  } else if (e === "bigint" && /^-?\d+$/.test(n))
288
339
  s = BigInt(n);
289
340
  else {
290
341
  const i = String(e);
291
- throw new h("invalid", `can't read a ${i} from ${JSON.stringify(n)}`);
342
+ throw new S("invalid", `can't read a ${i} from ${JSON.stringify(n)}`);
292
343
  }
293
344
  return { ...r, value: s };
294
345
  }
295
- function he(t) {
346
+ function Me(t) {
296
347
  return {
297
- version: q,
298
- query: L(t, Yt)
348
+ version: d,
349
+ query: F(t, Vt)
299
350
  };
300
351
  }
301
- function Re(t) {
302
- if (!I(t))
303
- throw new h("invalid", "a stored query is an object");
352
+ function we(t) {
353
+ if (!C(t))
354
+ throw new S("invalid", "a stored query is an object");
304
355
  const { version: e } = t;
305
356
  if (typeof e != "number" || !Number.isInteger(e) || e < 1)
306
- throw new h("invalid", "a stored query has a whole-number version from 1");
307
- if (e > q)
308
- throw new h(
357
+ throw new S("invalid", "a stored query has a whole-number version from 1");
358
+ if (e > d)
359
+ throw new S(
309
360
  "newerVersion",
310
- `stored in format ${e}; this version reads up to ${q}`
361
+ `stored in format ${e}; this version reads up to ${d}`
311
362
  );
312
- const r = jt(t.query, e, Ht);
313
- if (!I(r) || typeof r.id != "string")
314
- throw new h("invalid", "a stored query has a query with a string id");
315
- return L(r, Pt);
363
+ const r = kt(t.query, e, Xt);
364
+ if (!C(r) || typeof r.id != "string")
365
+ throw new S("invalid", "a stored query has a query with a string id");
366
+ return F(r, Wt);
316
367
  }
317
- function U(t, e) {
368
+ function B(t, e) {
318
369
  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);
319
370
  return t.forEach(({ table: i, alias: a }) => {
320
371
  if (i === void 0) {
@@ -328,11 +379,11 @@ function U(t, e) {
328
379
  n.delete(i), s.delete(i);
329
380
  }), { tableOf: r, aliases: n, derived: s };
330
381
  }
331
- function Gt(t, e, r) {
332
- const n = t.on && S(t.on, e, r);
333
- return t.query ? { ...t, on: n, query: At(t.query, (s) => M(s, r)) } : t.shared !== void 0 ? { ...t, on: n } : { ...t, on: n, table: r(t.table) };
382
+ function Jt(t, e, r) {
383
+ const n = t.on && I(t.on, e, r);
384
+ return t.query ? { ...t, on: n, query: Dt(t.query, (s) => q(s, r)) } : t.shared !== void 0 ? { ...t, on: n } : { ...t, on: n, table: r(t.table) };
334
385
  }
335
- function S(t, e, r) {
386
+ function I(t, e, r) {
336
387
  if (t.type === "field") {
337
388
  if (!t.table || e.derived.has(t.table)) return t;
338
389
  const n = e.tableOf.get(t.table) ?? t.table;
@@ -342,27 +393,27 @@ function S(t, e, r) {
342
393
  ...t.column ? { column: r(n, t.column) } : {}
343
394
  };
344
395
  }
345
- if (t.type === "subquery") return { ...t, query: M(t.query, r) };
396
+ if (t.type === "subquery") return { ...t, query: q(t.query, r) };
346
397
  if (t.type === "exists") {
347
- const { query: n } = t, s = [n.from, ...n.joins], i = U(s), a = U(s, e);
398
+ const { query: n } = t, s = [n.from, ...n.joins], i = B(s), a = B(s, e);
348
399
  return {
349
400
  ...t,
350
- link: S(t.link, a, r),
401
+ link: I(t.link, a, r),
351
402
  query: {
352
403
  ...n,
353
404
  from: { ...n.from, table: r(n.from.table) },
354
- joins: n.joins.map((o) => Gt(o, i, r)),
355
- ...n.where ? { where: S(n.where, i, r) } : {}
405
+ joins: n.joins.map((o) => Jt(o, i, r)),
406
+ ...n.where ? { where: I(n.where, i, r) } : {}
356
407
  }
357
408
  };
358
409
  }
359
- return X(t, (n) => S(n, e, r));
410
+ return H(t, (n) => I(n, e, r));
360
411
  }
361
- function M(t, e) {
362
- const r = U([t.from, ...t.joins]), n = k(
412
+ function q(t, e) {
413
+ const r = B([t.from, ...t.joins]), n = W(
363
414
  t,
364
- (s) => S(s, r, e),
365
- (s) => M(s, e)
415
+ (s) => I(s, r, e),
416
+ (s) => q(s, e)
366
417
  );
367
418
  return {
368
419
  ...n,
@@ -370,11 +421,11 @@ function M(t, e) {
370
421
  joins: n.joins.map((s) => s.table === void 0 ? s : { ...s, table: e(s.table) })
371
422
  };
372
423
  }
373
- function v(t, e) {
374
- if (Array.isArray(t)) return t.map((s) => v(s, e));
424
+ function j(t, e) {
425
+ if (Array.isArray(t)) return t.map((s) => j(s, e));
375
426
  if (typeof t != "object" || t === null || t instanceof Date) return t;
376
427
  const r = Object.fromEntries(Object.entries(t).map(
377
- ([s, i]) => [s, v(i, e)]
428
+ ([s, i]) => [s, j(i, e)]
378
429
  ));
379
430
  if (!("from" in r) || !Array.isArray(r.joins)) return r;
380
431
  const n = (s) => s.table !== void 0 && !s.alias && e(s.table) !== s.table ? { ...s, alias: s.table } : s;
@@ -384,17 +435,17 @@ function v(t, e) {
384
435
  joins: r.joins.map(n)
385
436
  };
386
437
  }
387
- function xt(t, e) {
388
- return M(v(t, e), e);
438
+ function Kt(t, e) {
439
+ return q(j(t, e), e);
389
440
  }
390
- const Qt = /^(\d{2}):(\d{2})(?::(\d{2}))?$/, P = /YYYY|MM|DD|HH|mm|ss/g, O = (t, e = 2) => String(t).padStart(e, "0");
391
- function tt(t, e) {
441
+ const zt = /^(\d{2}):(\d{2})(?::(\d{2}))?$/, X = /YYYY|MM|DD|HH|mm|ss/g, O = (t, e = 2) => String(t).padStart(e, "0");
442
+ function nt(t, e) {
392
443
  let r;
393
- const n = typeof t == "string" ? Qt.exec(t.trim()) : null;
444
+ const n = typeof t == "string" ? zt.exec(t.trim()) : null;
394
445
  if (n)
395
446
  r = { HH: n[1], mm: n[2], ss: n[3] ?? "00" };
396
447
  else {
397
- const a = typeof t == "string" ? Ct(t, "dateTime") : t;
448
+ const a = typeof t == "string" ? Ft(t, "dateTime") : t;
398
449
  a instanceof Date && (r = {
399
450
  YYYY: O(a.getUTCFullYear(), 4),
400
451
  MM: O(a.getUTCMonth() + 1),
@@ -406,20 +457,20 @@ function tt(t, e) {
406
457
  }
407
458
  if (!r) return t;
408
459
  const s = r;
409
- return (e.match(P) ?? []).some((a) => s[a] === void 0) ? t : e.replace(P, (a) => s[a]);
460
+ return (e.match(X) ?? []).some((a) => s[a] === void 0) ? t : e.replace(X, (a) => s[a]);
410
461
  }
411
- function et(t, e) {
462
+ function st(t, e) {
412
463
  const r = /* @__PURE__ */ new Map(), n = (s) => {
413
464
  const i = /* @__PURE__ */ new Map();
414
- d(s).forEach((u) => i.set(Q(u), u.table));
465
+ L(s).forEach((u) => i.set(V(u), u.table));
415
466
  const a = (u) => {
416
467
  if ((u == null ? void 0 : u.type) !== "field" || !u.table || !u.column) return;
417
- const c = i.has(u.table) ? i.get(u.table) : u.table, l = c === void 0 ? void 0 : mt(e, c), $ = l == null ? void 0 : l.columns.find((p) => p.name === u.column);
468
+ const c = i.has(u.table) ? i.get(u.table) : u.table, l = c === void 0 ? void 0 : Nt(e, c), $ = l == null ? void 0 : l.columns.find((y) => y.name === u.column);
418
469
  return $ == null ? void 0 : $.format;
419
470
  }, o = (u, c) => {
420
471
  switch (u.type) {
421
472
  case "literal":
422
- return { ...u, value: tt(u.value, c) };
473
+ return { ...u, value: nt(u.value, c) };
423
474
  case "list":
424
475
  return { ...u, items: u.items.map((l) => o(l, c)) };
425
476
  case "param":
@@ -429,7 +480,7 @@ function et(t, e) {
429
480
  }
430
481
  }, f = (u) => {
431
482
  if (u.type === "subquery") return { ...u, query: n(u.query) };
432
- const c = X(u, f);
483
+ const c = H(u, f);
433
484
  if (c.type !== "binary" || !c.right) return c;
434
485
  const l = a(c.left), $ = a(c.right);
435
486
  return {
@@ -438,16 +489,16 @@ function et(t, e) {
438
489
  right: l ? o(c.right, l) : c.right
439
490
  };
440
491
  };
441
- return k(s, f, (u) => n(u));
492
+ return W(s, f, (u) => n(u));
442
493
  };
443
494
  return { query: n(t), params: r };
444
495
  }
445
- const Se = ["mysql", "mssql", "postgres"], Ie = {
496
+ const Fe = ["mysql", "mssql", "postgres"], qe = {
446
497
  mysql: "MySQL",
447
498
  mssql: "SQL Server",
448
499
  postgres: "PostgreSQL"
449
500
  };
450
- function m(t, e) {
501
+ function g(t, e) {
451
502
  const r = t.replace(/[\uE000-\uE003]/g, "");
452
503
  switch (e) {
453
504
  case "mysql":
@@ -460,9 +511,9 @@ function m(t, e) {
460
511
  return `"${r.replace(/"/g, '""')}"`;
461
512
  }
462
513
  }
463
- const kt = /^[A-Za-z_][A-Za-z0-9_]*$/, rt = (t) => `${t}`, Xt = /\uE000(\d+)\uE000/g;
514
+ const Zt = /^[A-Za-z_][A-Za-z0-9_]*$/, it = (t) => `${t}`, te = /\uE000(\d+)\uE000/g;
464
515
  let N;
465
- function be(t, e) {
516
+ function Ue(t, e) {
466
517
  N = { ...t, writing: [] };
467
518
  try {
468
519
  return e();
@@ -470,17 +521,17 @@ function be(t, e) {
470
521
  N = void 0;
471
522
  }
472
523
  }
473
- function nt(t, e, r) {
524
+ function ot(t, e, r) {
474
525
  if (typeof t != "object" || t === null || t instanceof Date) return;
475
526
  let n;
476
527
  return Object.entries(t).some(([s, i]) => {
477
528
  const a = [...r, Array.isArray(t) ? Number(s) : s];
478
- return n = i === e ? a : nt(i, e, a), n !== void 0;
529
+ return n = i === e ? a : ot(i, e, a), n !== void 0;
479
530
  }), n;
480
531
  }
481
- function T(t, e) {
532
+ function p(t, e) {
482
533
  if (!N) return e();
483
- const { writing: r, paths: n } = N, s = r[r.length - 1], i = typeof t == "string" ? [t] : nt(s.value, t, s.path);
534
+ const { writing: r, paths: n } = N, s = r[r.length - 1], i = typeof t == "string" ? [t] : ot(s.value, t, s.path);
484
535
  if (!i) return e();
485
536
  r.push({ value: t, path: i });
486
537
  try {
@@ -489,7 +540,7 @@ function T(t, e) {
489
540
  r.pop();
490
541
  }
491
542
  }
492
- const Vt = {
543
+ const ee = {
493
544
  mysql: {
494
545
  text: "CHAR",
495
546
  integer: "SIGNED",
@@ -518,24 +569,24 @@ const Vt = {
518
569
  boolean: "boolean"
519
570
  }
520
571
  };
521
- function Wt(t, e) {
572
+ function re(t, e) {
522
573
  var n;
523
- const r = (n = Vt[e]) == null ? void 0 : n[t];
574
+ const r = (n = ee[e]) == null ? void 0 : n[t];
524
575
  return r ? e === "postgres" ? `NULL::${r}` : `CAST(NULL AS ${r})` : "NULL";
525
576
  }
526
- function st(t, e, r) {
577
+ function at(t, e, r) {
527
578
  if (t == null) return "NULL";
528
579
  if (r)
529
- return r.values.push(t), rt(r.values.length - 1);
580
+ return r.values.push(t), it(r.values.length - 1);
530
581
  if (typeof t == "boolean")
531
582
  return e === "mssql" ? t ? "1" : "0" : t ? "TRUE" : "FALSE";
532
583
  if (typeof t == "string") {
533
584
  const n = N ? t.replace(/[\uE001-\uE003]/g, "") : t;
534
585
  return n === "" ? "''" : `'${n.replace(/'/g, "''")}'`;
535
586
  }
536
- return typeof t == "number" || typeof t == "bigint" ? String(t) : t instanceof Date ? `'${J(t)}'` : String(t);
587
+ return typeof t == "number" || typeof t == "bigint" ? String(t) : t instanceof Date ? `'${z(t)}'` : String(t);
537
588
  }
538
- function Jt(t, e) {
589
+ function ne(t, e) {
539
590
  switch (t) {
540
591
  case "!=":
541
592
  return e === "mssql" ? "<>" : "!=";
@@ -547,10 +598,10 @@ function Jt(t, e) {
547
598
  return t;
548
599
  }
549
600
  }
550
- function g(t) {
601
+ function h(t) {
551
602
  return (t == null ? void 0 : t.type) === "literal" && typeof t.value == "string" && /^[A-Za-z_]+$/.test(t.value) ? t.value.toUpperCase() : "DAY";
552
603
  }
553
- const w = {
604
+ const U = {
554
605
  YEAR: "year",
555
606
  QUARTER: "quarter",
556
607
  MONTH: "month",
@@ -560,43 +611,43 @@ const w = {
560
611
  MINUTE: "minute",
561
612
  SECOND: "second",
562
613
  MILLISECOND: "millisecond"
563
- }, Kt = {
614
+ }, se = {
564
615
  // ---- Date arithmetic ----
565
616
  DATE_ADD: ([t, e], r, n, s) => {
566
617
  var a;
567
- const i = g(r[2]);
618
+ const i = h(r[2]);
568
619
  switch (n) {
569
620
  case "mysql":
570
621
  return i === "MILLISECOND" ? `DATE_ADD(${t}, INTERVAL (${e}) * 1000 MICROSECOND)` : `DATE_ADD(${t}, INTERVAL ${e} ${i})`;
571
622
  case "postgres": {
572
- const o = w[i] ?? i.toLowerCase();
623
+ const o = U[i] ?? i.toLowerCase();
573
624
  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}'))`;
574
625
  }
575
626
  case "mssql":
576
627
  return `DATEADD(${i}, ${e}, ${t})`;
577
628
  default:
578
- return `DATE_ADD(${t}, ${e}, ${g(r[2])})`;
629
+ return `DATE_ADD(${t}, ${e}, ${h(r[2])})`;
579
630
  }
580
631
  },
581
632
  DATE_SUB: ([t, e], r, n, s) => {
582
633
  var a;
583
- const i = g(r[2]);
634
+ const i = h(r[2]);
584
635
  switch (n) {
585
636
  case "mysql":
586
637
  return i === "MILLISECOND" ? `DATE_SUB(${t}, INTERVAL (${e}) * 1000 MICROSECOND)` : `DATE_SUB(${t}, INTERVAL ${e} ${i})`;
587
638
  case "postgres": {
588
- const o = w[i] ?? i.toLowerCase();
639
+ const o = U[i] ?? i.toLowerCase();
589
640
  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}'))`;
590
641
  }
591
642
  case "mssql":
592
643
  return `DATEADD(${i}, -(${e}), ${t})`;
593
644
  default:
594
- return `DATE_SUB(${t}, ${e}, ${g(r[2])})`;
645
+ return `DATE_SUB(${t}, ${e}, ${h(r[2])})`;
595
646
  }
596
647
  },
597
648
  // ---- Date diff ----
598
649
  DATEDIFF: ([t, e], r, n) => {
599
- const s = g(r[2]);
650
+ const s = h(r[2]);
600
651
  switch (n) {
601
652
  case "mysql":
602
653
  return `TIMESTAMPDIFF(${s}, ${t}, ${e})`;
@@ -635,7 +686,7 @@ const w = {
635
686
  },
636
687
  // ---- Date extract ----
637
688
  EXTRACT: ([t], e, r) => {
638
- const n = g(e[1]);
689
+ const n = h(e[1]);
639
690
  switch (r) {
640
691
  case "mysql":
641
692
  return n === "MILLISECOND" ? `FLOOR(MICROSECOND(${t}) / 1000)` : `EXTRACT(${n} FROM ${t})`;
@@ -649,7 +700,7 @@ const w = {
649
700
  },
650
701
  // ---- Date truncation ----
651
702
  DATE_TRUNC: ([t], e, r) => {
652
- const n = g(e[1]);
703
+ const n = h(e[1]);
653
704
  switch (r) {
654
705
  case "mysql":
655
706
  switch (n) {
@@ -675,7 +726,7 @@ const w = {
675
726
  return `/* unknown unit: ${n} */`;
676
727
  }
677
728
  case "postgres":
678
- return `DATE_TRUNC('${w[n] ?? n.toLowerCase()}', ${t})`;
729
+ return `DATE_TRUNC('${U[n] ?? n.toLowerCase()}', ${t})`;
679
730
  case "mssql":
680
731
  switch (n) {
681
732
  case "YEAR":
@@ -720,26 +771,26 @@ const w = {
720
771
  // The separator goes into the SQL as a literal even in parameterized SQL:
721
772
  // MySQL's SEPARATOR takes nothing else.
722
773
  STRING_AGG: ([t = "??"], e, r, n, s, i) => {
723
- const a = e[1] ?? { type: "literal", value: ", " }, o = a.type === "literal" && typeof a.value == "string" ? st(a.value, r) : "??", f = s.distinct ? "DISTINCT " : "", u = ($) => {
724
- var p;
725
- return (p = s.orderBy) != null && p.length ? `ORDER BY ${s.orderBy.map((A) => `${$(A)} ${A.direction === "desc" ? "DESC" : "ASC"}`).join(", ")}` : "";
774
+ const a = e[1] ?? { type: "literal", value: ", " }, o = a.type === "literal" && typeof a.value == "string" ? at(a.value, r) : "??", f = s.distinct ? "DISTINCT " : "", u = ($) => {
775
+ var y;
776
+ return (y = s.orderBy) != null && y.length ? `ORDER BY ${s.orderBy.map((T) => `${$(T)} ${T.direction === "desc" ? "DESC" : "ASC"}`).join(", ")}` : "";
726
777
  }, c = ($) => i($.expression), l = ($) => $ ? ` ${$}` : "";
727
778
  switch (r) {
728
779
  case "mysql":
729
780
  return `GROUP_CONCAT(${f}${t}${l(u(c))} SEPARATOR ${o})`;
730
781
  case "postgres": {
731
- const $ = `CAST(${t} AS TEXT)`, p = u((A) => {
732
- if (!s.distinct) return c(A);
733
- if (F(A.expression, e[0])) return $;
782
+ const $ = `CAST(${t} AS TEXT)`, y = u((T) => {
783
+ if (!s.distinct) return c(T);
784
+ if (M(T.expression, e[0])) return $;
734
785
  throw new Error("PostgreSQL orders STRING_AGG of different values by the value only");
735
786
  });
736
- return `STRING_AGG(${f}${$}, ${o}${l(p)})`;
787
+ return `STRING_AGG(${f}${$}, ${o}${l(y)})`;
737
788
  }
738
789
  case "mssql": {
739
790
  if (s.distinct)
740
791
  throw new Error("SQL Server's STRING_AGG can't join only different values");
741
- const $ = u(c), p = $ ? ` WITHIN GROUP (${$})` : "";
742
- return `STRING_AGG(CAST(${t} AS NVARCHAR(MAX)), ${o})${p}`;
792
+ const $ = u(c), y = $ ? ` WITHIN GROUP (${$})` : "";
793
+ return `STRING_AGG(CAST(${t} AS NVARCHAR(MAX)), ${o})${y}`;
743
794
  }
744
795
  default:
745
796
  return `STRING_AGG(${f}${t}, ${o}${l(u(c))})`;
@@ -750,38 +801,38 @@ function R(t) {
750
801
  return t.type === "binary" || t.type === "logical" || t.type === "if" || t.type === "case";
751
802
  }
752
803
  function E(t, e, r, n) {
753
- return T(t, () => zt(t, e, r, n));
804
+ return p(t, () => ie(t, e, r, n));
754
805
  }
755
- function G(t, e, r, n) {
806
+ function k(t, e, r, n) {
756
807
  const s = (i) => {
757
808
  if (!i) return "??";
758
809
  const a = E(i, e, r, n);
759
810
  return R(i) ? `(${a})` : a;
760
811
  };
761
- return T(t, () => {
812
+ return p(t, () => {
762
813
  const [i, a] = t.type === "list" ? t.items : [t];
763
814
  return `${s(i)} AND ${s(a)}`;
764
815
  });
765
816
  }
766
- function zt(t, e, r, n) {
817
+ function ie(t, e, r, n) {
767
818
  var a;
768
819
  const s = " ".repeat(r), i = " ".repeat(r + 1);
769
820
  switch (t.type) {
770
821
  case "literal":
771
- return t.value === "" && !t.empty ? "??" : t.value === null && t.as ? Wt(t.as, e) : st(t.value, e, n);
822
+ return t.value === "" && !t.empty ? "??" : t.value === null && t.as ? re(t.as, e) : at(t.value, e, n);
772
823
  case "param": {
773
- if (!n) return kt.test(t.name) ? `:${t.name}` : "??";
824
+ if (!n) return Zt.test(t.name) ? `:${t.name}` : "??";
774
825
  if (!n.named.has(t.name))
775
826
  throw new Error(`no value for parameter "${t.name}"`);
776
- return n.values.push(n.named.get(t.name)), rt(n.values.length - 1);
827
+ return n.values.push(n.named.get(t.name)), it(n.values.length - 1);
777
828
  }
778
829
  case "field": {
779
830
  if (!t.column) return "??";
780
- const o = m(t.column, e);
781
- return t.table ? `${m(t.table, e)}.${o}` : o;
831
+ const o = g(t.column, e);
832
+ return t.table ? `${g(t.table, e)}.${o}` : o;
782
833
  }
783
834
  case "function": {
784
- const o = t.args.map((l) => E(l, e, r, n)), f = Kt[t.name.toUpperCase()];
835
+ const o = t.args.map((l) => E(l, e, r, n)), f = se[t.name.toUpperCase()];
785
836
  if (f)
786
837
  return f(
787
838
  o,
@@ -797,11 +848,11 @@ function zt(t, e, r, n) {
797
848
  return `${u}(${c}${o.join(", ")})`;
798
849
  }
799
850
  case "binary": {
800
- const o = t.operator ? Jt(t.operator, e) : "=", f = E(t.left, e, r, n), u = R(t.left) ? `(${f})` : f;
801
- if (Mt(t.operator)) return `${u} ${o}`;
851
+ const o = t.operator ? ne(t.operator, e) : "=", f = E(t.left, e, r, n), u = R(t.left) ? `(${f})` : f;
852
+ if (Ut(t.operator)) return `${u} ${o}`;
802
853
  if (!t.right) return `${u} ${o} ??`;
803
854
  if (o === "BETWEEN")
804
- return `${u} BETWEEN ${G(t.right, e, r, n)}`;
855
+ return `${u} BETWEEN ${k(t.right, e, r, n)}`;
805
856
  if (o === "IN")
806
857
  return t.right.type === "list" ? `${u} IN ${E(t.right, e, r, n)}` : `${u} IN (${E(t.right, e, r, n)})`;
807
858
  const c = E(t.right, e, r, n), l = R(t.right) ? `(${c})` : c;
@@ -831,7 +882,7 @@ ${s})`;
831
882
  }
832
883
  if (o.type === "binary" && o.operator === "BETWEEN" && o.right) {
833
884
  const f = E(o.left, e, r, n);
834
- return `${R(o.left) ? `(${f})` : f} NOT BETWEEN ${G(o.right, e, r, n)}`;
885
+ return `${R(o.left) ? `(${f})` : f} NOT BETWEEN ${k(o.right, e, r, n)}`;
835
886
  }
836
887
  if (o.type === "logical" || o.type === "not") {
837
888
  const f = E(o, e, r + 1, n);
@@ -865,11 +916,11 @@ ${[
865
916
  "SELECT 1",
866
917
  // The clauses below write their expressions with formatNode.
867
918
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
868
- ct(o.from, e),
919
+ $t(o.from, e),
869
920
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
870
- ...o.joins.map(($) => ft($, e, n)),
921
+ ...o.joins.map(($) => Et($, e, n)),
871
922
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
872
- it(c, e, n)
923
+ ut(c, e, n)
873
924
  ].join(`
874
925
  `).replace(/^/gm, i)}
875
926
  ${s})`;
@@ -881,129 +932,129 @@ ${s})`;
881
932
  return `(${t.items.map((o) => E(o, e, r, n)).join(", ") || "??"})`;
882
933
  case "subquery":
883
934
  return `
884
- ${_(t.query, e, n).join(`
935
+ ${P(t.query, e, n).join(`
885
936
  `).replace(/^/gm, i)}
886
937
  ${s}`;
887
938
  default:
888
939
  return "/* unknown */";
889
940
  }
890
941
  }
891
- function it(t, e, r) {
942
+ function ut(t, e, r) {
892
943
  return `WHERE
893
944
  ${E(t, e, 1, r)}`;
894
945
  }
895
- const Zt = {
946
+ const oe = {
896
947
  inner: "INNER JOIN",
897
948
  left: "LEFT JOIN",
898
949
  right: "RIGHT JOIN",
899
950
  full: "FULL JOIN",
900
951
  cross: "CROSS JOIN"
901
952
  };
902
- let ot = /* @__PURE__ */ new Map();
903
- function at(t, e, r) {
904
- return t.shared !== void 0 ? m(ot.get(t.shared) ?? "??", e) : t.query ? `(
905
- ${ut(t.query, e, r).replace(/^/gm, " ")}
906
- )` : m(t.table, e);
953
+ let ct = /* @__PURE__ */ new Map();
954
+ function ft(t, e, r) {
955
+ return t.shared !== void 0 ? g(ct.get(t.shared) ?? "??", e) : t.query ? `(
956
+ ${lt(t.query, e, r).replace(/^/gm, " ")}
957
+ )` : g(t.table, e);
907
958
  }
908
- function ut(t, e, r) {
909
- return gt(t).map((n) => _(n, e, r).join(`
910
- `)).join(Dt(t) && !t.all ? `
959
+ function lt(t, e, r) {
960
+ return Ot(t).map((n) => P(n, e, r).join(`
961
+ `)).join(St(t) && !t.all ? `
911
962
  UNION
912
963
  ` : `
913
964
  UNION ALL
914
965
  `);
915
966
  }
916
- function te(t, e, r) {
967
+ function ae(t, e, r) {
917
968
  return t.length === 0 ? [] : [`WITH
918
- ${t.map((s) => T(s, () => {
919
- const i = ut(s.query, e, r).replace(/^/gm, " ");
920
- return ` ${m(s.name, e)} AS (
969
+ ${t.map((s) => p(s, () => {
970
+ const i = lt(s.query, e, r).replace(/^/gm, " ");
971
+ return ` ${g(s.name, e)} AS (
921
972
  ${i}
922
973
  )`;
923
974
  })).join(`,
924
975
  `)}`];
925
976
  }
926
- function ct(t, e, r) {
927
- const n = t.alias ? ` AS ${m(t.alias, e)}` : "";
928
- return `FROM ${T(t, () => `${at(t, e, r)}${n}`)}`;
977
+ function $t(t, e, r) {
978
+ const n = t.alias ? ` AS ${g(t.alias, e)}` : "";
979
+ return `FROM ${p(t, () => `${ft(t, e, r)}${n}`)}`;
929
980
  }
930
- function ft(t, e, r) {
931
- return T(t, () => {
932
- const n = at(t, e, r), s = t.alias ? ` AS ${m(t.alias, e)}` : "";
981
+ function Et(t, e, r) {
982
+ return p(t, () => {
983
+ const n = ft(t, e, r), s = t.alias ? ` AS ${g(t.alias, e)}` : "";
933
984
  if (t.joinType === "cross") return `CROSS JOIN ${n}${s}`;
934
985
  const i = t.on ? E(t.on, e, 1, r) : "/* unconfigured */";
935
- return `${Zt[t.joinType]} ${n}${s}
986
+ return `${oe[t.joinType]} ${n}${s}
936
987
  ON ${i}`;
937
988
  });
938
989
  }
939
- function ee(t, e, r, n = "SELECT") {
940
- if (t.length === 0) return `${n} ${T(t, () => "*")}`;
941
- const s = t.map((i) => T(i, () => {
942
- const a = E(i.expression, e, 1, r), o = i.alias ? ` AS ${m(i.alias, e)}` : "";
990
+ function ue(t, e, r, n = "SELECT") {
991
+ if (t.length === 0) return `${n} ${p(t, () => "*")}`;
992
+ const s = t.map((i) => p(i, () => {
993
+ const a = E(i.expression, e, 1, r), o = i.alias ? ` AS ${g(i.alias, e)}` : "";
943
994
  return `${a}${o}`;
944
995
  }));
945
996
  return `${n}
946
997
  ${s.join(`,
947
998
  `)}`;
948
999
  }
949
- function re(t, e, r) {
1000
+ function ce(t, e, r) {
950
1001
  return `GROUP BY
951
- ${t.map((s) => T(
1002
+ ${t.map((s) => p(
952
1003
  s,
953
1004
  () => E(s.expression, e, 1, r)
954
1005
  )).join(`,
955
1006
  `)}`;
956
1007
  }
957
- function ne(t, e, r) {
1008
+ function fe(t, e, r) {
958
1009
  return `HAVING
959
1010
  ${E(t, e, 1, r)}`;
960
1011
  }
961
- function se(t, e, r, n) {
1012
+ function le(t, e, r, n) {
962
1013
  return `ORDER BY
963
- ${t.map((i) => T(i, () => `${E(n(i.expression), e, 1, r)} ${i.direction === "desc" ? "DESC" : "ASC"}`)).join(`,
1014
+ ${t.map((i) => p(i, () => `${E(n(i.expression), e, 1, r)} ${i.direction === "desc" ? "DESC" : "ASC"}`)).join(`,
964
1015
  `)}`;
965
1016
  }
966
- const B = (t) => t !== void 0 && Number.isInteger(t) && t >= 0 ? t : void 0;
967
- function lt(t, e) {
968
- const r = e.resolveName ? xt(t, e.resolveName) : t;
969
- return e.defaultLimit !== void 0 && B(r.limit) === void 0 ? { ...r, limit: e.defaultLimit } : r;
1017
+ const _ = (t) => t !== void 0 && Number.isInteger(t) && t >= 0 ? t : void 0;
1018
+ function Tt(t, e) {
1019
+ const r = e.resolveName ? Kt(t, e.resolveName) : t;
1020
+ return e.defaultLimit !== void 0 && _(r.limit) === void 0 ? { ...r, limit: e.defaultLimit } : r;
970
1021
  }
971
- const ie = "18446744073709551615";
972
- function _(t, e, r, n = `SELECT${t.distinct ? " DISTINCT" : ""}`) {
1022
+ const $e = "18446744073709551615";
1023
+ function P(t, e, r, n = `SELECT${t.distinct ? " DISTINCT" : ""}`) {
973
1024
  const s = [
974
- ee(t.select, e, r, n),
975
- ct(t.from, e, r),
976
- ...t.joins.map((a) => ft(a, e, r))
1025
+ ue(t.select, e, r, n),
1026
+ $t(t.from, e, r),
1027
+ ...t.joins.map((a) => Et(a, e, r))
977
1028
  ];
978
- t.where && s.push(it(t.where, e, r));
1029
+ t.where && s.push(ut(t.where, e, r));
979
1030
  const i = t.groupBy ?? [];
980
- if (i.length > 0 && s.push(re(i, e, r)), t.having) {
981
- const a = e === "mysql" ? W(t.having, t) : t.having;
982
- s.push(ne(a, e, r));
1031
+ if (i.length > 0 && s.push(ce(i, e, r)), t.having) {
1032
+ const a = e === "mysql" ? K(t.having, t) : t.having;
1033
+ s.push(fe(a, e, r));
983
1034
  }
984
1035
  return s;
985
1036
  }
986
- function $t(t, e, r) {
987
- N && (N.writing = [{ value: t, path: [] }]), ot = new Map((t.shared ?? []).map((c) => [c.id, c.name]));
988
- const n = te(t.shared ?? [], e, r), s = B(t.limit), i = B(t.offset) || void 0, a = t.orderBy ?? [], o = e === "mssql" && s !== void 0 && i === void 0, f = `SELECT${t.distinct ? " DISTINCT" : ""}${o ? ` ${T("limit", () => `TOP (${s})`)}` : ""}`, u = _(t, e, r, f);
989
- if (a.length > 0 && u.push(se(a, e, r, (c) => e === "mysql" ? W(c, t) : c)), e === "mssql") {
1037
+ function yt(t, e, r) {
1038
+ N && (N.writing = [{ value: t, path: [] }]), ct = new Map((t.shared ?? []).map((c) => [c.id, c.name]));
1039
+ const n = ae(t.shared ?? [], e, r), s = _(t.limit), i = _(t.offset) || void 0, a = t.orderBy ?? [], o = e === "mssql" && s !== void 0 && i === void 0, f = `SELECT${t.distinct ? " DISTINCT" : ""}${o ? ` ${p("limit", () => `TOP (${s})`)}` : ""}`, u = P(t, e, r, f);
1040
+ if (a.length > 0 && u.push(le(a, e, r, (c) => e === "mysql" ? K(c, t) : c)), e === "mssql") {
990
1041
  if (i !== void 0) {
991
1042
  a.length === 0 && u.push("ORDER BY (SELECT NULL)");
992
- const c = s !== void 0 ? ` ${T("limit", () => `FETCH NEXT ${s} ROWS ONLY`)}` : "";
993
- u.push(`${T("offset", () => `OFFSET ${i} ROWS`)}${c}`);
1043
+ const c = s !== void 0 ? ` ${p("limit", () => `FETCH NEXT ${s} ROWS ONLY`)}` : "";
1044
+ u.push(`${p("offset", () => `OFFSET ${i} ROWS`)}${c}`);
994
1045
  }
995
1046
  } else if (s !== void 0 || i !== void 0) {
996
- const c = s !== void 0 || e === "mysql" ? T(s !== void 0 ? "limit" : {}, () => `LIMIT ${s ?? ie}`) : "", l = i !== void 0 ? T("offset", () => `OFFSET ${i}`) : "";
1047
+ const c = s !== void 0 || e === "mysql" ? p(s !== void 0 ? "limit" : {}, () => `LIMIT ${s ?? $e}`) : "", l = i !== void 0 ? p("offset", () => `OFFSET ${i}`) : "";
997
1048
  u.push([c, l].filter(Boolean).join(" "));
998
1049
  }
999
1050
  return [...n, ...u].join(`
1000
1051
  `);
1001
1052
  }
1002
- function Ce(t, e, r = {}) {
1003
- const n = r.values ? Lt(t, r.values) : t, s = r.schema ? et(n, r.schema).query : n;
1004
- return $t(lt(s, r), e);
1053
+ function ve(t, e, r = {}) {
1054
+ const n = r.values ? qt(t, r.values) : t, s = r.schema ? st(n, r.schema).query : n;
1055
+ return yt(Tt(s, r), e);
1005
1056
  }
1006
- function oe(t, e) {
1057
+ function Ee(t, e) {
1007
1058
  switch (t) {
1008
1059
  case "mysql":
1009
1060
  return "?";
@@ -1015,42 +1066,42 @@ function oe(t, e) {
1015
1066
  return "?";
1016
1067
  }
1017
1068
  }
1018
- function Le(t, e, r = {}) {
1019
- const n = r.schema ? et(t, r.schema) : { query: t, params: /* @__PURE__ */ new Map() }, s = {
1069
+ function de(t, e, r = {}) {
1070
+ const n = r.schema ? st(t, r.schema) : { query: t, params: /* @__PURE__ */ new Map() }, s = {
1020
1071
  values: [],
1021
1072
  named: new Map(Object.entries(r.values ?? {}).map(([u, c]) => {
1022
1073
  const l = n.params.get(u);
1023
- return [u, l ? tt(c, l) : c];
1074
+ return [u, l ? nt(c, l) : c];
1024
1075
  }))
1025
- }, i = lt(n.query, r), a = $t(i, e, s), o = [];
1026
- return { sql: a.replace(Xt, (u, c) => {
1076
+ }, i = Tt(n.query, r), a = yt(i, e, s), o = [];
1077
+ return { sql: a.replace(te, (u, c) => {
1027
1078
  const l = s.values[Number(c)];
1028
- return o.push(l instanceof Date ? J(l) : l), oe(e, o.length);
1079
+ return o.push(l instanceof Date ? z(l) : l), Ee(e, o.length);
1029
1080
  }), params: o };
1030
1081
  }
1031
1082
  export {
1032
- Se as D,
1033
- q as Q,
1034
- wt as S,
1035
- Le as a,
1036
- Ce as b,
1037
- Ut as c,
1038
- Ae as d,
1039
- Te as e,
1040
- ge as f,
1041
- pe as g,
1042
- Y as h,
1043
- Oe as i,
1044
- Ee as j,
1045
- Ne as k,
1046
- me as l,
1047
- De as m,
1048
- h as n,
1049
- Re as o,
1050
- he as p,
1051
- Ie as q,
1052
- ye as r,
1053
- le as s,
1054
- $e as u,
1055
- be as w
1083
+ Fe as D,
1084
+ d as Q,
1085
+ vt as S,
1086
+ de as a,
1087
+ ve as b,
1088
+ Yt as c,
1089
+ Se as d,
1090
+ De as e,
1091
+ Re as f,
1092
+ Oe as g,
1093
+ Q as h,
1094
+ Le as i,
1095
+ he as j,
1096
+ Ce as k,
1097
+ be as l,
1098
+ Ie as m,
1099
+ S as n,
1100
+ we as o,
1101
+ Me as p,
1102
+ qe as q,
1103
+ Ne as r,
1104
+ me as s,
1105
+ ge as u,
1106
+ Ue as w
1056
1107
  };
package/dist/core.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { D as e, E as r, M as o, b as n, f as i, a as d, h as t, i as u, v as l } from "./schema-z95dxLxu.js";
2
- import { M as m, a as E, b as T, Q as y, I as c, S as h, d as p, p as Q, e as _, f as U, g as b, c as A, h as M, K as N, B as D, q as I, k as B, t as f, v, m as L, G as C, H as R, w as F, r as G, x as q, i as x, C as H, E as P, F as g, y as k, s as O, z as X, A as Y, j, o as J, l as w, n as K, u as z, J as W } from "./issueLocation-Cw5sttQL.js";
2
+ import { M as m, a as E, b as T, Q as y, I as c, S as h, d as p, p as Q, e as _, f as U, g as b, c as A, h as M, K as N, B as D, q as I, k as B, t as f, v, m as L, G as C, H as R, w as F, r as G, x as q, i as x, C as H, E as P, F as g, y as k, s as O, z as X, A as Y, j, o as J, l as w, n as K, u as z, J as W } from "./issueLocation-CWUdTtCh.js";
3
3
  import { c as Z, f as $, i as aa } from "./calendarDate-CYlr9TvX.js";
4
- import { D as ea, q as ra, Q as oa, n as na, S as ia, c as da, d as ta, e as ua, f as la, g as Sa, a as ma, b as Ea, h as Ta, o as ya, r as ca, i as ha, p as pa, s as Qa, j as _a, k as Ua, l as ba, m as Aa, u as Ma } from "./core-api-BDCnbFHY.js";
4
+ import { D as ea, q as ra, Q as oa, n as na, S as ia, c as da, d as ta, e as ua, f as la, g as Sa, a as ma, b as Ea, h as Ta, o as ya, r as ca, i as ha, p as pa, s as Qa, j as _a, k as Ua, l as ba, m as Aa, u as Ma } from "./core-api-BfLqMVQf.js";
5
5
  export {
6
6
  e as DEFAULT_FUNCTIONS,
7
7
  ea as DIALECTS,
package/dist/index.js CHANGED
@@ -1,9 +1,9 @@
1
- import { P as Q, f as U, a as D } from "./PipelineEditor-DNZkDSaY.js";
1
+ import { P as Q, f as U, a as D } from "./PipelineEditor-BoFPDUmi.js";
2
2
  import { D as b, E as x, M as B, b as F, f as I, a as v, h as L, i as P, v as R } from "./schema-z95dxLxu.js";
3
- import { D as k, M as q, a as C, b as G, Q as H, I as w, S as X, d as W, p as Y, e as j, f as J, g as z, c as K, h as V, K as Z, B as $, q as ee, k as ae, t as se, v as te, m as re, G as ne, H as oe, w as ie, r as de, x as ce, i as ue, C as le, E as fe, F as me, y as he, s as pe, z as Se, A as Ee, j as Te, o as ge, l as Ne, n as _e, u as ye, J as Ae } from "./issueLocation-Cw5sttQL.js";
3
+ import { D as k, M as q, a as C, b as G, Q as H, I as w, S as X, d as W, p as Y, e as j, f as J, g as z, c as K, h as V, K as Z, B as $, q as ee, k as ae, t as se, v as te, m as re, G as ne, H as oe, w as ie, r as de, x as ce, i as ue, C as le, E as fe, F as me, y as he, s as pe, z as Se, A as Ee, j as Te, o as ge, l as Ne, n as _e, u as ye, J as Ae } from "./issueLocation-CWUdTtCh.js";
4
4
  import { c as Ue, f as De, i as Me } from "./calendarDate-CYlr9TvX.js";
5
- import { w as m, a as h, b as p } from "./core-api-BDCnbFHY.js";
6
- import { D as xe, q as Be, Q as Fe, n as Ie, S as ve, c as Le, d as Pe, e as Re, f as Oe, g as ke, h as qe, o as Ce, r as Ge, i as He, p as we, s as Xe, j as We, k as Ye, l as je, m as Je, u as ze } from "./core-api-BDCnbFHY.js";
5
+ import { w as m, a as h, b as p } from "./core-api-BfLqMVQf.js";
6
+ import { D as xe, q as Be, Q as Fe, n as Ie, S as ve, c as Le, d as Pe, e as Re, f as Oe, g as ke, h as qe, o as Ce, r as Ge, i as He, p as we, s as Xe, j as We, k as Ye, l as je, m as Je, u as ze } from "./core-api-BfLqMVQf.js";
7
7
  import { D as Ve } from "./tokens-BTaVMSOx.js";
8
8
  function T(n, s, e = {}) {
9
9
  const a = { paths: [] }, t = m(a, () => e.parameterized ? h(n, s, e).sql : p(n, s, e));
@@ -3123,7 +3123,7 @@ export {
3123
3123
  ti as a8,
3124
3124
  ls as a9,
3125
3125
  ne as aA,
3126
- de as aB,
3126
+ Ee as aB,
3127
3127
  Ve as aC,
3128
3128
  ke as aD,
3129
3129
  en as aE,
package/dist/mui.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { jsx as n, jsxs as P } from "react/jsx-runtime";
2
2
  import { useMemo as H, useState as w, useRef as A, useLayoutEffect as G } from "react";
3
- import { P as K } from "./PipelineEditor-DNZkDSaY.js";
3
+ import { P as K } from "./PipelineEditor-BoFPDUmi.js";
4
4
  import Q from "@emotion/cache";
5
5
  import { CacheProvider as U } from "@emotion/react";
6
6
  import { TextField as R, Menu as J, ListSubheader as _, MenuItem as $, Box as y, createFilterOptions as X, Autocomplete as D, Select as Y, InputBase as Z, Button as ee, IconButton as te, Tooltip as oe, Chip as re } from "@mui/material";
@@ -131,6 +131,7 @@ export type PipelineAction = {
131
131
  } | {
132
132
  type: 'clearPaging';
133
133
  };
134
+ export declare function generateJoinId(): string;
134
135
  /** Every distinct table referenced by a field in `ast` (deduplicated, in first-seen
135
136
  * order) — used to auto-add tables to the pipeline as a WHERE condition references
136
137
  * them. Only fields with an explicit `.table` count — an unqualified field doesn't
@@ -18,7 +18,10 @@ export declare class ScopeError extends Error {
18
18
  readonly path: (string | number)[];
19
19
  /** The table it reads that needs scope. */
20
20
  readonly table: string;
21
- constructor(path: (string | number)[], table: string, missing: string);
21
+ /** The alias that copy of the table goes by, when it has one (a self
22
+ * join reads a table more than once, issue 216). */
23
+ readonly alias?: string;
24
+ constructor(path: (string | number)[], table: string, missing: string, alias?: string);
22
25
  }
23
26
  /** `query` with `rule.condition` in **every** body that reads a table
24
27
  * `rule.requires` accepts (issue 151): the query itself, each derived
@@ -35,11 +38,14 @@ export declare class ScopeError extends Error {
35
38
  * left out is exactly the leak.
36
39
  * Call it after validateQuery, on the checked query. */
37
40
  export declare function scopeQuery(schema: DatabaseSchema, query: QueryPipeline, rule: ScopeRule): QueryPipeline;
38
- /** Every body of `query` that reads a table `rule.requires` accepts without
39
- * `rule.condition` ANDed at the top of its WHERE — where it is, and the
40
- * table. Empty: `query` is scoped everywhere (see scopeQuery). For a query
41
- * built or received some other way. */
42
- export declare function unscopedBodies(query: QueryPipeline, rule: ScopeRule): {
41
+ /** Every copy of a table `rule.requires` accepts, in every body of `query`,
42
+ * that isn't scoped: the condition's tables not joined through it, or the
43
+ * condition naming them not ANDed at the top of its WHERE — where it is,
44
+ * the table, and the alias that copy goes by (a self join reads a table
45
+ * more than once, issue 216). Empty: `query` is scoped everywhere (see
46
+ * scopeQuery). For a query built or received some other way. */
47
+ export declare function unscopedBodies(schema: DatabaseSchema, query: QueryPipeline, rule: ScopeRule): {
43
48
  path: (string | number)[];
44
49
  table: string;
50
+ alias?: string;
45
51
  }[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nongt/visql",
3
- "version": "0.3.1",
3
+ "version": "0.4.1",
4
4
  "description": "A visual SQL query editor for React: build a whole query on a node canvas, store it as JSON, turn it into SQL.",
5
5
  "keywords": [
6
6
  "sql",