@nongt/visql-core 0.9.0 → 0.10.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.
@@ -1,4 +1,4 @@
1
- function x(e) {
1
+ function k(e) {
2
2
  switch (e.type) {
3
3
  case "function":
4
4
  return e.orderBy ? [...e.args, ...e.orderBy.map((t) => t.expression)] : e.args;
@@ -31,100 +31,100 @@ function x(e) {
31
31
  return [];
32
32
  }
33
33
  }
34
- const J = ["inner", "left", "right", "full", "cross"];
35
- function A(e) {
34
+ const le = ["inner", "left", "right", "full", "cross"];
35
+ function M(e) {
36
36
  return "union" in e;
37
37
  }
38
- function z(e) {
39
- return A(e) ? e.union : [e];
38
+ function ie(e) {
39
+ return M(e) ? e.union : [e];
40
40
  }
41
- function Z(e) {
42
- return A(e) ? e.union[0] : e;
41
+ function ue(e) {
42
+ return M(e) ? e.union[0] : e;
43
43
  }
44
- function E(e, t) {
45
- if (!A(e)) return t(e, 0);
46
- const r = e.union.map((n, l) => t(n, l));
47
- return r.every((n, l) => n === e.union[l]) ? e : { ...e, union: r };
44
+ function w(e, t) {
45
+ if (!M(e)) return t(e, 0);
46
+ const n = e.union.map((r, i) => t(r, i));
47
+ return n.every((r, i) => r === e.union[i]) ? e : { ...e, union: n };
48
48
  }
49
- function q(e, t) {
49
+ function oe(e, t) {
50
50
  if (!e.shared) return e;
51
- let r = !1;
52
- const n = e.shared.map((l) => {
53
- const a = t(l.query, l);
54
- return a === l.query ? l : (r = !0, { ...l, query: a });
51
+ let n = !1;
52
+ const r = e.shared.map((i) => {
53
+ const a = t(i.query, i);
54
+ return a === i.query ? i : (n = !0, { ...i, query: a });
55
55
  });
56
- return r ? { ...e, shared: n } : e;
56
+ return n ? { ...e, shared: r } : e;
57
57
  }
58
- function ee(e) {
58
+ function fe(e) {
59
59
  return "from" in e;
60
60
  }
61
- const te = 63;
62
- function Y(e) {
61
+ const ce = 63;
62
+ function K(e) {
63
63
  return e.alias || e.table || "";
64
64
  }
65
- function re(e) {
65
+ function pe(e) {
66
66
  return e.alias ? e.alias : e.expression.type === "field" ? e.expression.column : void 0;
67
67
  }
68
- function j(e) {
68
+ function Q(e) {
69
69
  return [e.from, ...e.joins];
70
70
  }
71
- function ne(e) {
72
- return j(e).flatMap((t) => t.table === void 0 ? [] : [t.table]);
71
+ function de(e) {
72
+ return Q(e).flatMap((t) => t.table === void 0 ? [] : [t.table]);
73
73
  }
74
- function M(e, t) {
75
- const r = t;
74
+ function $(e, t) {
75
+ const n = t;
76
76
  switch (e.type) {
77
77
  case "function":
78
78
  return {
79
79
  ...e,
80
- args: e.args.map(r),
81
- ...e.orderBy ? { orderBy: e.orderBy.map((n) => ({ ...n, expression: r(n.expression) })) } : {}
80
+ args: e.args.map(n),
81
+ ...e.orderBy ? { orderBy: e.orderBy.map((r) => ({ ...r, expression: n(r.expression) })) } : {}
82
82
  };
83
83
  case "binary":
84
84
  return {
85
85
  ...e,
86
- left: r(e.left),
87
- ...e.right ? { right: r(e.right) } : {}
86
+ left: n(e.left),
87
+ ...e.right ? { right: n(e.right) } : {}
88
88
  };
89
89
  case "logical":
90
- return { ...e, operands: e.operands.map(r) };
90
+ return { ...e, operands: e.operands.map(n) };
91
91
  case "list":
92
- return { ...e, items: e.items.map(r) };
92
+ return { ...e, items: e.items.map(n) };
93
93
  case "not":
94
- return { ...e, operand: r(e.operand) };
94
+ return { ...e, operand: n(e.operand) };
95
95
  case "if":
96
96
  return {
97
97
  ...e,
98
- condition: e.condition && r(e.condition),
99
- then: e.then && r(e.then),
100
- else: e.else && r(e.else)
98
+ condition: e.condition && n(e.condition),
99
+ then: e.then && n(e.then),
100
+ else: e.else && n(e.else)
101
101
  };
102
102
  case "case":
103
103
  return {
104
104
  ...e,
105
- subject: e.subject && r(e.subject),
106
- branches: e.branches.map((n) => ({ when: r(n.when), then: r(n.then) })),
107
- else: e.else && r(e.else)
105
+ subject: e.subject && n(e.subject),
106
+ branches: e.branches.map((r) => ({ when: n(r.when), then: n(r.then) })),
107
+ else: e.else && n(e.else)
108
108
  };
109
109
  case "exists":
110
110
  return {
111
111
  ...e,
112
- link: r(e.link),
112
+ link: n(e.link),
113
113
  query: {
114
114
  ...e.query,
115
- joins: e.query.joins.map((n) => n.on ? { ...n, on: r(n.on) } : n),
116
- ...e.query.where ? { where: r(e.query.where) } : {}
115
+ joins: e.query.joins.map((r) => r.on ? { ...r, on: n(r.on) } : r),
116
+ ...e.query.where ? { where: n(e.query.where) } : {}
117
117
  }
118
118
  };
119
119
  default:
120
120
  return e;
121
121
  }
122
122
  }
123
- function D(e, t) {
124
- const r = M(e, (n) => D(n, t));
125
- return r === e ? t(e) : r;
123
+ function j(e, t) {
124
+ const n = $(e, (r) => j(r, t));
125
+ return n === e ? t(e) : n;
126
126
  }
127
- function ae(e) {
127
+ function me(e) {
128
128
  return [
129
129
  ...e.where ? [e.where] : [],
130
130
  ...e.joins.flatMap((t) => t.on ? [t.on] : []),
@@ -134,42 +134,42 @@ function ae(e) {
134
134
  ...(e.orderBy ?? []).map((t) => t.expression)
135
135
  ];
136
136
  }
137
- function y(e, t, r = (n) => y(n, t)) {
138
- var a, s;
139
- const n = t, { shared: l } = e;
137
+ function _(e, t, n = (r) => _(r, t)) {
138
+ var a, l;
139
+ const r = t, { shared: i } = e;
140
140
  return {
141
141
  ...e,
142
- ...l ? {
143
- shared: l.map((i) => ({ ...i, query: E(i.query, r) }))
142
+ ...i ? {
143
+ shared: i.map((s) => ({ ...s, query: w(s.query, n) }))
144
144
  } : {},
145
- from: e.from.query ? { ...e.from, query: E(e.from.query, r) } : e.from,
146
- where: e.where && n(e.where),
147
- joins: e.joins.map((i) => i.query ? { ...i, on: i.on && n(i.on), query: E(i.query, r) } : { ...i, on: i.on && n(i.on) }),
148
- select: e.select.map((i) => ({ ...i, expression: n(i.expression) })),
149
- groupBy: (a = e.groupBy) == null ? void 0 : a.map((i) => ({ ...i, expression: n(i.expression) })),
150
- having: e.having && n(e.having),
151
- orderBy: (s = e.orderBy) == null ? void 0 : s.map((i) => ({ ...i, expression: n(i.expression) }))
145
+ from: e.from.query ? { ...e.from, query: w(e.from.query, n) } : e.from,
146
+ where: e.where && r(e.where),
147
+ joins: e.joins.map((s) => s.query ? { ...s, on: s.on && r(s.on), query: w(s.query, n) } : { ...s, on: s.on && r(s.on) }),
148
+ select: e.select.map((s) => ({ ...s, expression: r(s.expression) })),
149
+ groupBy: (a = e.groupBy) == null ? void 0 : a.map((s) => ({ ...s, expression: r(s.expression) })),
150
+ having: e.having && r(e.having),
151
+ orderBy: (l = e.orderBy) == null ? void 0 : l.map((s) => ({ ...s, expression: r(s.expression) }))
152
152
  };
153
153
  }
154
- function F(e, t) {
155
- const r = (n) => n.type === "subquery" ? { ...n, query: F(n.query, t) } : M(n, r);
156
- return y(e, (n) => D(r(n), t));
154
+ function J(e, t) {
155
+ const n = (r) => r.type === "subquery" ? { ...r, query: J(r.query, t) } : $(r, n);
156
+ return _(e, (r) => j(n(r), t));
157
157
  }
158
- function H(e) {
159
- return new Set([e.from, ...e.joins].map(Y));
158
+ function z(e) {
159
+ return new Set([e.from, ...e.joins].map(K));
160
160
  }
161
- function _(e) {
162
- return e !== void 0 && (e.type === "exists" || x(e).some(_));
161
+ function Z(e) {
162
+ return e !== void 0 && (e.type === "exists" || k(e).some(Z));
163
163
  }
164
- function $(e) {
165
- const t = H(e.query), r = (n) => n.type === "field" ? [n] : x(n).flatMap(r);
166
- return r(e.link).filter((n) => n.table !== void 0 && !t.has(n.table));
164
+ function q(e) {
165
+ const t = z(e.query), n = (r) => r.type === "field" ? [r] : k(r).flatMap(n);
166
+ return n(e.link).filter((r) => r.table !== void 0 && !t.has(r.table));
167
167
  }
168
- const se = {
168
+ const ge = {
169
169
  "date-expr": ["date", "dateTime"],
170
170
  "num-expr": ["integer", "decimal"],
171
171
  "str-expr": ["varchar"]
172
- }, u = { type: "field" }, P = [
172
+ }, u = { type: "field" }, ee = [
173
173
  "YEAR",
174
174
  "QUARTER",
175
175
  "MONTH",
@@ -180,20 +180,20 @@ const se = {
180
180
  "SECOND",
181
181
  "MILLISECOND"
182
182
  ];
183
- function le(e, t) {
183
+ function be(e, t) {
184
184
  if (t < e.args.length) return e.args[t];
185
- const r = e.args[e.args.length - 1];
186
- return r != null && r.variadic ? r : void 0;
185
+ const n = e.args[e.args.length - 1];
186
+ return n != null && n.variadic ? n : void 0;
187
187
  }
188
- function ie(e) {
188
+ function he(e) {
189
189
  return e.args.map((t) => t.default ?? { type: "literal", value: "" });
190
190
  }
191
- const b = (e, t = "DAY") => ({
191
+ const T = (e, t = "DAY") => ({
192
192
  label: e,
193
193
  kind: "unit",
194
- choices: P,
194
+ choices: ee,
195
195
  default: { type: "literal", value: t }
196
- }), v = {
196
+ }), A = {
197
197
  // ---- Aggregate ----
198
198
  // COUNT with no argument is COUNT(*), and `distinct` on the call makes it
199
199
  // COUNT(DISTINCT x) — see the function sub-canvas's count choice.
@@ -258,7 +258,7 @@ const b = (e, t = "DAY") => ({
258
258
  args: [
259
259
  { label: "date", kind: "date-expr", default: u },
260
260
  { label: "amount", kind: "num-expr", default: { type: "literal", value: 1 } },
261
- b("unit")
261
+ T("unit")
262
262
  ]
263
263
  },
264
264
  DATE_SUB: {
@@ -267,7 +267,7 @@ const b = (e, t = "DAY") => ({
267
267
  args: [
268
268
  { label: "date", kind: "date-expr", default: u },
269
269
  { label: "amount", kind: "num-expr", default: { type: "literal", value: 1 } },
270
- b("unit")
270
+ T("unit")
271
271
  ]
272
272
  },
273
273
  // ---- Date diff ----
@@ -277,7 +277,7 @@ const b = (e, t = "DAY") => ({
277
277
  args: [
278
278
  { label: "start", kind: "date-expr", default: u },
279
279
  { label: "end", kind: "date-expr", default: u },
280
- b("unit")
280
+ T("unit")
281
281
  ]
282
282
  },
283
283
  // ---- Date extract / trunc ----
@@ -286,7 +286,7 @@ const b = (e, t = "DAY") => ({
286
286
  returns: "integer",
287
287
  args: [
288
288
  { label: "date", kind: "date-expr", default: u },
289
- b("unit", "YEAR")
289
+ T("unit", "YEAR")
290
290
  ]
291
291
  },
292
292
  DATE_TRUNC: {
@@ -294,7 +294,7 @@ const b = (e, t = "DAY") => ({
294
294
  returns: "arg",
295
295
  args: [
296
296
  { label: "date", kind: "date-expr", default: u },
297
- b("unit")
297
+ T("unit")
298
298
  ]
299
299
  },
300
300
  // ---- Type cast ----
@@ -577,307 +577,345 @@ const b = (e, t = "DAY") => ({
577
577
  ]
578
578
  }
579
579
  };
580
- function R(e, t = v) {
580
+ function D(e, t = A) {
581
581
  return t[e.toUpperCase()];
582
582
  }
583
- function ue(e, t) {
584
- const r = e.toUpperCase();
585
- return R(e, t) ?? Object.values(t).find((n) => {
586
- var l;
587
- return (l = n.aliases) == null ? void 0 : l.some((a) => a.toUpperCase() === r);
583
+ function xe(e, t) {
584
+ const n = e.toUpperCase();
585
+ return D(e, t) ?? Object.values(t).find((r) => {
586
+ var i;
587
+ return (i = r.aliases) == null ? void 0 : i.some((a) => a.toUpperCase() === n);
588
588
  });
589
589
  }
590
- function W(e = v) {
590
+ function te(e = A) {
591
591
  return Object.keys(e).sort();
592
592
  }
593
- W(v);
594
- const oe = v;
595
- function fe(e) {
593
+ te(A);
594
+ const Te = A;
595
+ function ve(e) {
596
596
  return Object.fromEntries(Object.values(e).map((t) => {
597
- const r = t.name.toUpperCase();
598
- return [r, { ...t, name: r }];
597
+ const n = t.name.toUpperCase();
598
+ return [n, { ...t, name: n }];
599
599
  }));
600
600
  }
601
- function ce(e) {
601
+ function ke(e) {
602
602
  return Object.fromEntries(Object.entries(e).filter(([, t]) => !t.aggregate));
603
603
  }
604
- function pe(e, t) {
605
- return Object.fromEntries(Object.entries(e).filter(([, r]) => !r.dialects || r.dialects.includes(t)));
604
+ function Ee(e, t) {
605
+ return Object.fromEntries(Object.entries(e).filter(([, n]) => !n.dialects || n.dialects.includes(t)));
606
606
  }
607
- function C(e, t) {
607
+ function U(e, t) {
608
608
  if (e === t) return !0;
609
609
  if (e instanceof Date && t instanceof Date) return e.getTime() === t.getTime();
610
610
  if (typeof e != "object" || typeof t != "object" || e === null || t === null) return !1;
611
- const r = e, n = t, l = Object.keys(r).filter((s) => r[s] !== void 0), a = Object.keys(n).filter((s) => n[s] !== void 0);
612
- return l.length === a.length && l.every((s) => C(r[s], n[s]));
611
+ const n = e, r = t, i = Object.keys(n).filter((l) => n[l] !== void 0), a = Object.keys(r).filter((l) => r[l] !== void 0);
612
+ return i.length === a.length && i.every((l) => U(n[l], r[l]));
613
613
  }
614
- function V() {
614
+ function ne() {
615
615
  const e = globalThis.crypto.getRandomValues(new Uint8Array(12));
616
616
  return Array.from(e, (t) => t.toString(16).padStart(2, "0")).join("");
617
617
  }
618
- function U(e, t) {
619
- var r;
620
- return e.type === "function" && ((r = R(e.name, t)) == null ? void 0 : r.aggregate) === !0;
618
+ function F(e, t) {
619
+ var n;
620
+ return e.type === "function" && ((n = D(e.name, t)) == null ? void 0 : n.aggregate) === !0;
621
621
  }
622
- function g(e, t) {
623
- return e ? U(e, t) || x(e).some((r) => g(r, t)) : !1;
622
+ function x(e, t) {
623
+ return e ? F(e, t) || k(e).some((n) => x(n, t)) : !1;
624
624
  }
625
- function I(e, t) {
626
- return e.type === "function" && e.name.trim() !== "" && R(e.name, t) === void 0;
625
+ function H(e, t) {
626
+ return e.type === "function" && e.name.trim() !== "" && D(e.name, t) === void 0;
627
627
  }
628
- function w(e, t) {
629
- return e ? I(e, t) || x(e).some((r) => w(r, t)) : !1;
628
+ function P(e, t) {
629
+ return e ? H(e, t) || k(e).some((n) => P(n, t)) : !1;
630
630
  }
631
- function h(e, t, r) {
632
- return t.some((n) => C(n, e)) ? !0 : e.type === "field" ? !e.column : e.type === "exists" ? $(e).every((n) => h(n, t, r)) : U(e, r) || I(e, r) ? !0 : x(e).every((n) => h(n, t, r));
631
+ function v(e, t, n) {
632
+ return t.some((r) => U(r, e)) ? !0 : e.type === "field" ? !e.column : e.type === "exists" ? q(e).every((r) => v(r, t, n)) : F(e, n) || H(e, n) ? !0 : k(e).every((r) => v(r, t, n));
633
633
  }
634
- function B(e, t) {
635
- return (e.groupBy ?? []).length > 0 || e.having !== void 0 || [...e.select, ...e.orderBy ?? []].some((r) => g(r.expression, t));
634
+ function W(e, t) {
635
+ return (e.groupBy ?? []).length > 0 || e.having !== void 0 || [...e.select, ...e.orderBy ?? []].some((n) => x(n.expression, t));
636
636
  }
637
- function me(e, t) {
638
- if (B(e, t))
639
- return (e.groupBy ?? []).flatMap(({ expression: r }) => r.type === "field" && r.table && r.column ? [`${r.table}.${r.column}`] : []);
637
+ function Se(e, t) {
638
+ if (W(e, t))
639
+ return (e.groupBy ?? []).flatMap(({ expression: n }) => n.type === "field" && n.table && n.column ? [`${n.table}.${n.column}`] : []);
640
640
  }
641
- function X(e, t) {
642
- return g(e.having, t) || [...e.select, ...e.orderBy ?? []].some((r) => g(r.expression, t));
641
+ function re(e, t) {
642
+ return x(e.having, t) || [...e.select, ...e.orderBy ?? []].some((n) => x(n.expression, t));
643
643
  }
644
- function de(e, t) {
645
- return e.select.some((r) => g(r.expression, t));
644
+ function Ae(e, t) {
645
+ return e.select.some((n) => x(n.expression, t));
646
646
  }
647
- function ge(e, t) {
648
- const r = e.groupBy ?? [], n = r.filter((o) => o.fromSelect === void 0), l = X(e, t), a = n.map((o) => o.expression), s = l ? e.select.filter((o) => g(o.expression, t) || w(o.expression, t) || h(o.expression, a, t) ? !1 : (a.push(o.expression), !0)) : [], i = new Map(r.filter((o) => o.fromSelect !== void 0).map((o) => [o.fromSelect, o])), c = [];
649
- r.forEach((o) => {
647
+ function Re(e, t) {
648
+ const n = e.groupBy ?? [], r = n.filter((o) => o.fromSelect === void 0), i = re(e, t), a = r.map((o) => o.expression), l = i ? e.select.filter((o) => x(o.expression, t) || P(o.expression, t) || v(o.expression, a, t) ? !1 : (a.push(o.expression), !0)) : [], s = new Map(n.filter((o) => o.fromSelect !== void 0).map((o) => [o.fromSelect, o])), c = [];
649
+ n.forEach((o) => {
650
650
  if (o.fromSelect === void 0) {
651
651
  c.push(o);
652
652
  return;
653
653
  }
654
- const m = s.find((T) => T.id === o.fromSelect);
655
- m && c.push(C(o.expression, m.expression) ? o : { ...o, expression: m.expression });
654
+ const p = l.find((m) => m.id === o.fromSelect);
655
+ p && c.push(U(o.expression, p.expression) ? o : { ...o, expression: p.expression });
656
656
  });
657
- const f = s.filter((o) => !i.has(o.id)).map((o) => ({
658
- id: V(),
657
+ const h = l.filter((o) => !s.has(o.id)).map((o) => ({
658
+ id: ne(),
659
659
  expression: o.expression,
660
660
  fromSelect: o.id
661
- })), p = [...c, ...f];
662
- return p.length === r.length && p.every((o, m) => o === r[m]) ? e : { ...e, groupBy: p };
661
+ })), g = [...c, ...h];
662
+ return g.length === n.length && g.every((o, p) => o === n[p]) ? e : { ...e, groupBy: g };
663
663
  }
664
- function be(e, t) {
665
- const r = (e.groupBy ?? []).map((a) => a.expression), n = e.orderBy ?? [];
666
- if (!B(e, t))
664
+ function Ce(e, t) {
665
+ const n = (e.groupBy ?? []).map((a) => a.expression), r = e.orderBy ?? [];
666
+ if (!W(e, t))
667
667
  return {
668
668
  select: [],
669
669
  selectAll: !1,
670
670
  orderBy: [],
671
671
  having: !1
672
672
  };
673
- const l = (a) => a.filter((s) => !h(s.expression, r, t)).map((s) => s.id);
673
+ const i = (a) => a.filter((l) => !v(l.expression, n, t)).map((l) => l.id);
674
674
  return {
675
- select: l(e.select),
675
+ select: i(e.select),
676
676
  selectAll: e.select.length === 0,
677
- orderBy: l(n),
678
- having: e.having !== void 0 && !h(e.having, r, t)
677
+ orderBy: i(r),
678
+ having: e.having !== void 0 && !v(e.having, n, t)
679
679
  };
680
680
  }
681
- const he = { tables: [], relations: [] };
682
- function xe(e, t, r) {
683
- if (!t && !r) return e;
684
- const n = e.tables.filter((a) => !t || t(a)).map((a) => r ? { ...a, columns: a.columns.filter((s) => r(a, s)) } : a), l = new Set(n.map((a) => a.name));
681
+ const Oe = { tables: [], relations: [] };
682
+ function ye(e, t, n) {
683
+ if (!t && !n) return e;
684
+ const r = e.tables.filter((a) => !t || t(a)).map((a) => n ? { ...a, columns: a.columns.filter((l) => n(a, l)) } : a), i = new Set(r.map((a) => a.name));
685
685
  return {
686
- tables: n,
687
- relations: e.relations.filter((a) => l.has(a.fromTable) && l.has(a.toTable))
686
+ tables: r,
687
+ relations: e.relations.filter((a) => i.has(a.fromTable) && i.has(a.toTable))
688
688
  };
689
689
  }
690
- function Te(e) {
691
- return [...e.tables].sort((t, r) => t.label.localeCompare(r.label));
690
+ function we(e) {
691
+ return [...e.tables].sort((t, n) => t.label.localeCompare(n.label));
692
692
  }
693
- function ve(e, t = (n) => n.group, r = e) {
694
- const n = /* @__PURE__ */ new Map();
695
- r.forEach((a) => {
696
- const s = t(a) || void 0;
697
- s !== void 0 && !n.has(s) && n.set(s, []);
693
+ function Ne(e, t = (r) => r.group, n = e) {
694
+ const r = /* @__PURE__ */ new Map();
695
+ n.forEach((a) => {
696
+ const l = t(a) || void 0;
697
+ l !== void 0 && !r.has(l) && r.set(l, []);
698
698
  }), e.forEach((a) => {
699
- const s = t(a) || void 0;
700
- n.set(s, [...n.get(s) ?? [], a]);
699
+ const l = t(a) || void 0;
700
+ r.set(l, [...r.get(l) ?? [], a]);
701
701
  });
702
- const l = (a) => [...a].sort((s, i) => s.label.localeCompare(i.label));
702
+ const i = (a) => [...a].sort((l, s) => l.label.localeCompare(s.label));
703
703
  return [
704
- ...[...n].filter(([a]) => a !== void 0),
705
- ...[...n].filter(([a]) => a === void 0)
706
- ].filter(([, a]) => a.length > 0).map(([a, s]) => ({ group: a, tables: l(s) }));
704
+ ...[...r].filter(([a]) => a !== void 0),
705
+ ...[...r].filter(([a]) => a === void 0)
706
+ ].filter(([, a]) => a.length > 0).map(([a, l]) => ({ group: a, tables: i(l) }));
707
707
  }
708
- function L(e, t) {
709
- return e.tables.find((r) => r.name === t);
708
+ function V(e, t) {
709
+ return e.tables.find((n) => n.name === t);
710
710
  }
711
- function Q(e, t, r) {
712
- return e.relations.find((n) => n.fromTable === t && n.toTable === r || n.fromTable === r && n.toTable === t);
711
+ function N(e, t, n) {
712
+ return e.relations.find((r) => r.fromTable === t && r.toTable === n || r.fromTable === n && r.toTable === t);
713
713
  }
714
- const k = (e, t) => {
715
- var r;
716
- return ((r = L(e, t)) == null ? void 0 : r.passThrough) !== !1;
714
+ const R = (e, t) => {
715
+ var n;
716
+ return ((n = V(e, t)) == null ? void 0 : n.passThrough) !== !1;
717
717
  };
718
- function O(e, t) {
719
- const r = t.filter((n) => k(e, n));
720
- return r.length > 0 ? r : t;
721
- }
722
- const G = (e, t) => e.fanOuts - t.fanOuts || e.steps - t.steps || e.order - t.order;
723
- function S(e, t) {
724
- let r;
725
- return e.forEach((n, l) => {
726
- !t.has(l) && (r === void 0 || G(n, e.get(r)) < 0) && (r = l);
727
- }), r;
728
- }
729
- function ke(e, t, r) {
730
- if (t.includes(r)) return [];
731
- const n = O(e, t), l = new Map(t.map((f, p) => [f, {
732
- fanOuts: 0,
733
- steps: 0,
734
- order: p
735
- }])), a = new Set(t.filter((f) => !n.includes(f)));
736
- let s = t.length;
737
- const i = (f) => {
738
- const p = l.get(f);
739
- p.steps > 0 && !k(e, f) || e.relations.forEach((d) => {
740
- if (d.fromTable !== f && d.toTable !== f) return;
741
- const o = d.fromTable === f ? d.toTable : d.fromTable, m = {
742
- fanOuts: p.fanOuts + (d.fromTable === f ? 0 : 1),
743
- steps: p.steps + 1,
744
- order: s,
745
- from: f
746
- }, T = l.get(o);
747
- a.has(o) || T && G(m, { ...T, order: s }) >= 0 || (l.set(o, m), s += 1);
718
+ function I(e, t) {
719
+ const n = t.filter((r) => R(e, r));
720
+ return n.length > 0 ? n : [...t];
721
+ }
722
+ function B(e, t) {
723
+ const n = /* @__PURE__ */ new Set();
724
+ return t.forEach((r, i) => {
725
+ const l = t.slice(0, i).map((c) => ({ earlier: c, r: N(e, c, r) })).find(({ r: c }) => c !== void 0);
726
+ if (!l) return;
727
+ (l.r.fromTable === l.earlier && l.r.toTable === r || n.has(l.earlier)) && n.add(r);
728
+ }), n;
729
+ }
730
+ const X = (e, t) => e.fanOuts - t.fanOuts || e.steps - t.steps || e.order - t.order, S = (e, t) => `${t ? "^" : "_"}${e}`;
731
+ function G(e, t) {
732
+ let n;
733
+ return e.forEach((r, i) => {
734
+ !t.has(i) && (n === void 0 || X(r, e.get(n)) < 0) && (n = i);
735
+ }), n;
736
+ }
737
+ function ae(e, t, n, r = {}) {
738
+ const i = new Set(t), a = new Set(I(e, t)), l = r.anyShape ? /* @__PURE__ */ new Set() : B(e, t), s = /* @__PURE__ */ new Map();
739
+ t.forEach((m, d) => {
740
+ const b = S(m, l.has(m));
741
+ s.has(b) || s.set(b, {
742
+ table: m,
743
+ climbed: l.has(m),
744
+ fanOuts: 0,
745
+ steps: 0,
746
+ order: d
747
+ });
748
+ });
749
+ const c = new Set([...s].filter(([, m]) => !a.has(m.table)).map(([m]) => m));
750
+ let h = t.length;
751
+ const g = (m) => {
752
+ const d = s.get(m);
753
+ d.steps > 0 && !R(e, d.table) || e.relations.forEach((b) => {
754
+ if (b.fromTable !== d.table && b.toTable !== d.table) return;
755
+ const E = b.fromTable === d.table, C = E ? b.toTable : b.fromTable;
756
+ if (i.has(C) || !E && d.climbed) return;
757
+ const O = {
758
+ table: C,
759
+ climbed: !r.anyShape && (d.climbed || E),
760
+ fanOuts: d.fanOuts + (E ? 0 : 1),
761
+ steps: d.steps + 1,
762
+ order: h,
763
+ from: m
764
+ }, y = S(C, O.climbed), L = s.get(y);
765
+ c.has(y) || L && X(O, { ...L, order: h }) >= 0 || (s.set(y, O), h += 1);
748
766
  });
749
767
  };
750
- for (let f = S(l, a); f !== void 0 && f !== r; f = S(l, a))
751
- a.add(f), i(f);
752
- if (!l.has(r)) return;
753
- const c = [];
754
- for (let f = r; !t.includes(f); f = l.get(f).from)
755
- c.unshift(f);
756
- return c;
757
- }
758
- function Ee(e, t) {
759
- const r = /* @__PURE__ */ new Map(), n = (s, i) => r.set(s, [...r.get(s) ?? [], i]);
760
- e.relations.forEach((s) => {
761
- n(s.fromTable, s.toTable), n(s.toTable, s.fromTable);
768
+ let f = G(s, c);
769
+ for (; f !== void 0 && s.get(f).table !== n; )
770
+ c.add(f), g(f), f = G(s, c);
771
+ if (f === void 0) return;
772
+ const o = [];
773
+ let p = s.get(f);
774
+ for (; p.from !== void 0; )
775
+ o.unshift(p.table), p = s.get(p.from);
776
+ return { start: p.table, tables: o };
777
+ }
778
+ function Me(e, t, n, r = {}) {
779
+ var i;
780
+ return t.includes(n) ? [] : (i = ae(e, t, n, r)) == null ? void 0 : i.tables;
781
+ }
782
+ function De(e, t, n = {}) {
783
+ const r = /* @__PURE__ */ new Map(), i = (f, o, p) => r.set(f, [
784
+ ...r.get(f) ?? [],
785
+ { table: o, up: p }
786
+ ]);
787
+ e.relations.forEach((f) => {
788
+ i(f.fromTable, f.toTable, !0), i(f.toTable, f.fromTable, !1);
762
789
  });
763
- const l = new Set(t), a = O(e, t);
764
- for (let s = a.shift(); s !== void 0; s = a.shift())
765
- (r.get(s) ?? []).forEach((i) => {
766
- l.has(i) || (l.add(i), k(e, i) && a.push(i));
790
+ const { skip: a = /* @__PURE__ */ new Set() } = n, l = n.anyShape ? /* @__PURE__ */ new Set() : B(e, t), s = new Set(t), c = new Set(t), h = I(e, t).map((f) => ({ table: f, climbed: l.has(f) })), g = new Set(h.map((f) => S(f.table, f.climbed)));
791
+ for (let f = h.shift(); f !== void 0; f = h.shift()) {
792
+ const o = f;
793
+ (r.get(o.table) ?? []).forEach(({ table: p, up: m }) => {
794
+ if (s.has(p) || a.has(p) || !m && o.climbed) return;
795
+ const d = { table: p, climbed: !n.anyShape && (o.climbed || m) }, b = S(p, d.climbed);
796
+ g.has(b) || (g.add(b), c.add(p), R(e, p) && h.push(d));
767
797
  });
768
- return l;
798
+ }
799
+ return c;
769
800
  }
770
- function Ae(e, t, r) {
771
- var c;
772
- const l = (k(e, r) ? O(e, t) : t).map((f) => Q(e, f, r)).find((f) => f !== void 0);
773
- if (!l) return;
774
- const a = (c = L(e, l.fromTable)) == null ? void 0 : c.columns.find((f) => f.name === l.fromColumn), s = a != null && a.nullable ? "left" : "inner", i = {
801
+ function Ue(e, t, n, r = {}) {
802
+ var m;
803
+ const { after: i, leftJoined: a = [] } = r, l = r.anyShape ? /* @__PURE__ */ new Set() : B(e, t), s = (d) => {
804
+ const b = N(e, d, n);
805
+ return b !== void 0 && !(l.has(d) && b.toTable === d && b.fromTable === n);
806
+ };
807
+ let c = R(e, n) ? I(e, t) : t;
808
+ i !== void 0 && s(i) && (c = [i]);
809
+ const h = c.find(s);
810
+ if (h === void 0) return;
811
+ const g = N(e, h, n), f = (m = V(e, g.fromTable)) == null ? void 0 : m.columns.find((d) => d.name === g.fromColumn), o = f != null && f.nullable || a.includes(h) ? "left" : "inner", p = {
775
812
  type: "binary",
776
813
  operator: "=",
777
- left: { type: "field", table: l.fromTable, column: l.fromColumn },
778
- right: { type: "field", table: l.toTable, column: l.toColumn }
814
+ left: { type: "field", table: g.fromTable, column: g.fromColumn },
815
+ right: { type: "field", table: g.toTable, column: g.toColumn }
779
816
  };
780
- return { joinType: s, on: i };
817
+ return { joinType: o, on: p };
781
818
  }
782
- const N = (e, t) => `${e}.${t}`;
783
- function K({ type: e, format: t }) {
819
+ const Y = (e, t) => `${e}.${t}`;
820
+ function se({ type: e, format: t }) {
784
821
  if (t === void 0) return;
785
822
  if (e !== "date" && e !== "dateTime" && e !== "time")
786
823
  return `a format is for a date, date-and-time or time column, not ${e}`;
787
- const r = t.match(/YYYY|MM|DD|HH|mm|ss/g) ?? [];
788
- if (r.length === 0) return `format "${t}" has none of YYYY, MM, DD, HH, mm, ss`;
789
- if (e === "time" && r.some((n) => n === "YYYY" || n === "MM" || n === "DD"))
824
+ const n = t.match(/YYYY|MM|DD|HH|mm|ss/g) ?? [];
825
+ if (n.length === 0) return `format "${t}" has none of YYYY, MM, DD, HH, mm, ss`;
826
+ if (e === "time" && n.some((r) => r === "YYYY" || r === "MM" || r === "DD"))
790
827
  return `a time has no day: format "${t}" can't be written from one`;
791
828
  }
792
- function Re(e) {
793
- const t = [], r = /* @__PURE__ */ new Map();
829
+ function Ie(e) {
830
+ const t = [], n = /* @__PURE__ */ new Map();
794
831
  e.tables.forEach((a) => {
795
- r.has(a.name) && t.push({
832
+ n.has(a.name) && t.push({
796
833
  kind: "duplicateTable",
797
834
  message: `Table "${a.name}" is declared more than once`
798
- }), r.set(a.name, a);
799
- const s = /* @__PURE__ */ new Set();
800
- a.columns.forEach((i) => {
801
- s.has(i.name) && t.push({
835
+ }), n.set(a.name, a);
836
+ const l = /* @__PURE__ */ new Set();
837
+ a.columns.forEach((s) => {
838
+ l.has(s.name) && t.push({
802
839
  kind: "duplicateColumn",
803
- message: `Column "${a.name}.${i.name}" is declared more than once`
804
- }), s.add(i.name);
805
- const c = K(i);
840
+ message: `Column "${a.name}.${s.name}" is declared more than once`
841
+ }), l.add(s.name);
842
+ const c = se(s);
806
843
  c && t.push({
807
844
  kind: "invalidFormat",
808
- message: `Column "${a.name}.${i.name}": ${c}`
845
+ message: `Column "${a.name}.${s.name}": ${c}`
809
846
  });
810
847
  });
811
848
  });
812
- const n = (a, s) => {
813
- const i = r.get(a);
814
- i ? i.columns.some((c) => c.name === s) || t.push({
849
+ const r = (a, l) => {
850
+ const s = n.get(a);
851
+ s ? s.columns.some((c) => c.name === l) || t.push({
815
852
  kind: "unknownColumn",
816
- message: `Relation references unknown column "${a}.${s}"`
853
+ message: `Relation references unknown column "${a}.${l}"`
817
854
  }) : t.push({
818
855
  kind: "unknownTable",
819
856
  message: `Relation references unknown table "${a}"`
820
857
  });
821
- }, l = /* @__PURE__ */ new Set();
858
+ }, i = /* @__PURE__ */ new Set();
822
859
  return e.relations.forEach((a) => {
823
- n(a.fromTable, a.fromColumn), n(a.toTable, a.toColumn);
824
- const s = N(a.fromTable, a.fromColumn), i = N(a.toTable, a.toColumn), c = [s, i].sort().join(" <-> ");
825
- l.has(c) && t.push({
860
+ r(a.fromTable, a.fromColumn), r(a.toTable, a.toColumn);
861
+ const l = Y(a.fromTable, a.fromColumn), s = Y(a.toTable, a.toColumn), c = [l, s].sort().join(" <-> ");
862
+ i.has(c) && t.push({
826
863
  kind: "duplicateRelation",
827
- message: `Relation ${s} -> ${i} is declared more than once (declare each foreign key once)`
828
- }), l.add(c);
864
+ message: `Relation ${l} -> ${s} is declared more than once (declare each foreign key once)`
865
+ }), i.add(c);
829
866
  }), t;
830
867
  }
831
868
  export {
832
- B as A,
833
- J as B,
834
- be as C,
835
- oe as D,
836
- he as E,
837
- le as F,
838
- w as G,
839
- R as H,
840
- ae as I,
841
- q as J,
842
- H as K,
843
- _ as L,
844
- te as M,
845
- ne as N,
846
- Ee as O,
847
- D as P,
848
- F as Q,
849
- ce as R,
850
- W as S,
851
- se as T,
852
- ie as U,
853
- ve as V,
854
- U as W,
855
- me as X,
856
- de as Y,
857
- x as a,
858
- z as b,
859
- pe as c,
860
- C as d,
861
- ke as e,
862
- xe as f,
863
- y as g,
864
- ee as h,
865
- A as i,
866
- E as j,
867
- L as k,
868
- P as l,
869
- M as m,
870
- V as n,
871
- Z as o,
872
- ue as p,
873
- j as q,
874
- re as r,
875
- Ae as s,
876
- Y as t,
877
- g as u,
878
- Re as v,
879
- X as w,
880
- ge as x,
881
- Te as y,
882
- fe as z
869
+ ve as A,
870
+ W as B,
871
+ le as C,
872
+ Te as D,
873
+ Oe as E,
874
+ Ce as F,
875
+ be as G,
876
+ P as H,
877
+ D as I,
878
+ me as J,
879
+ oe as K,
880
+ z as L,
881
+ ce as M,
882
+ Z as N,
883
+ de as O,
884
+ ae as P,
885
+ j as Q,
886
+ J as R,
887
+ ke as S,
888
+ te as T,
889
+ ge as U,
890
+ he as V,
891
+ Ne as W,
892
+ F as X,
893
+ Se as Y,
894
+ Ae as Z,
895
+ Me as a,
896
+ ie as b,
897
+ Ee as c,
898
+ U as d,
899
+ _ as e,
900
+ ye as f,
901
+ w as g,
902
+ fe as h,
903
+ M as i,
904
+ V as j,
905
+ ee as k,
906
+ ne as l,
907
+ $ as m,
908
+ ue as n,
909
+ pe as o,
910
+ xe as p,
911
+ Q as q,
912
+ De as r,
913
+ Ue as s,
914
+ K as t,
915
+ x as u,
916
+ Ie as v,
917
+ re as w,
918
+ Re as x,
919
+ k as y,
920
+ we as z
883
921
  };