@nongt/visql-core 0.10.0 → 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,7 +1,12 @@
1
- function k(e) {
1
+ function v(e) {
2
2
  switch (e.type) {
3
+ // In the order the SQL writes them: arguments, PARTITION BY, ORDER BY.
3
4
  case "function":
4
- return e.orderBy ? [...e.args, ...e.orderBy.map((t) => t.expression)] : e.args;
5
+ return e.orderBy || e.partitionBy ? [
6
+ ...e.args,
7
+ ...e.partitionBy ?? [],
8
+ ...(e.orderBy ?? []).map((t) => t.expression)
9
+ ] : e.args;
5
10
  case "binary":
6
11
  return e.right ? [e.left, e.right] : [e.left];
7
12
  case "logical":
@@ -31,22 +36,22 @@ function k(e) {
31
36
  return [];
32
37
  }
33
38
  }
34
- const le = ["inner", "left", "right", "full", "cross"];
35
- function M(e) {
39
+ const oe = ["inner", "left", "right", "full", "cross"];
40
+ function D(e) {
36
41
  return "union" in e;
37
42
  }
38
- function ie(e) {
39
- return M(e) ? e.union : [e];
43
+ function fe(e) {
44
+ return D(e) ? e.union : [e];
40
45
  }
41
- function ue(e) {
42
- return M(e) ? e.union[0] : e;
46
+ function ce(e) {
47
+ return D(e) ? e.union[0] : e;
43
48
  }
44
- function N(e, t) {
45
- if (!M(e)) return t(e, 0);
49
+ function O(e, t) {
50
+ if (!D(e)) return t(e, 0);
46
51
  const n = e.union.map((r, i) => t(r, i));
47
52
  return n.every((r, i) => r === e.union[i]) ? e : { ...e, union: n };
48
53
  }
49
- function oe(e, t) {
54
+ function pe(e, t) {
50
55
  if (!e.shared) return e;
51
56
  let n = !1;
52
57
  const r = e.shared.map((i) => {
@@ -55,29 +60,30 @@ function oe(e, t) {
55
60
  });
56
61
  return n ? { ...e, shared: r } : e;
57
62
  }
58
- function fe(e) {
63
+ function de(e) {
59
64
  return "from" in e;
60
65
  }
61
- const ce = 63;
62
- function K(e) {
66
+ const me = 63;
67
+ function Q(e) {
63
68
  return e.alias || e.table || "";
64
69
  }
65
- function pe(e) {
70
+ function ge(e) {
66
71
  return e.alias ? e.alias : e.expression.type === "field" ? e.expression.column : void 0;
67
72
  }
68
- function Q(e) {
73
+ function J(e) {
69
74
  return [e.from, ...e.joins];
70
75
  }
71
- function de(e) {
72
- return Q(e).flatMap((t) => t.table === void 0 ? [] : [t.table]);
76
+ function be(e) {
77
+ return J(e).flatMap((t) => t.table === void 0 ? [] : [t.table]);
73
78
  }
74
- function Y(e, t) {
79
+ function j(e, t) {
75
80
  const n = t;
76
81
  switch (e.type) {
77
82
  case "function":
78
83
  return {
79
84
  ...e,
80
85
  args: e.args.map(n),
86
+ ...e.partitionBy ? { partitionBy: e.partitionBy.map(n) } : {},
81
87
  ...e.orderBy ? { orderBy: e.orderBy.map((r) => ({ ...r, expression: n(r.expression) })) } : {}
82
88
  };
83
89
  case "binary":
@@ -121,10 +127,10 @@ function Y(e, t) {
121
127
  }
122
128
  }
123
129
  function $(e, t) {
124
- const n = Y(e, (r) => $(r, t));
130
+ const n = j(e, (r) => $(r, t));
125
131
  return n === e ? t(e) : n;
126
132
  }
127
- function me(e) {
133
+ function he(e) {
128
134
  return [
129
135
  ...e.where ? [e.where] : [],
130
136
  ...e.joins.flatMap((t) => t.on ? [t.on] : []),
@@ -134,42 +140,42 @@ function me(e) {
134
140
  ...(e.orderBy ?? []).map((t) => t.expression)
135
141
  ];
136
142
  }
137
- function j(e, t, n = (r) => j(r, t)) {
143
+ function F(e, t, n = (r) => F(r, t)) {
138
144
  var a, l;
139
145
  const r = t, { shared: i } = e;
140
146
  return {
141
147
  ...e,
142
148
  ...i ? {
143
- shared: i.map((s) => ({ ...s, query: N(s.query, n) }))
149
+ shared: i.map((s) => ({ ...s, query: O(s.query, n) }))
144
150
  } : {},
145
- from: e.from.query ? { ...e.from, query: N(e.from.query, n) } : e.from,
151
+ from: e.from.query ? { ...e.from, query: O(e.from.query, n) } : e.from,
146
152
  where: e.where && r(e.where),
147
- joins: e.joins.map((s) => s.query ? { ...s, on: s.on && r(s.on), query: N(s.query, n) } : { ...s, on: s.on && r(s.on) }),
153
+ joins: e.joins.map((s) => s.query ? { ...s, on: s.on && r(s.on), query: O(s.query, n) } : { ...s, on: s.on && r(s.on) }),
148
154
  select: e.select.map((s) => ({ ...s, expression: r(s.expression) })),
149
155
  groupBy: (a = e.groupBy) == null ? void 0 : a.map((s) => ({ ...s, expression: r(s.expression) })),
150
156
  having: e.having && r(e.having),
151
157
  orderBy: (l = e.orderBy) == null ? void 0 : l.map((s) => ({ ...s, expression: r(s.expression) }))
152
158
  };
153
159
  }
154
- function J(e, t) {
155
- const n = (r) => r.type === "subquery" ? { ...r, query: J(r.query, t) } : Y(r, n);
156
- return j(e, (r) => $(n(r), t));
157
- }
158
- function z(e) {
159
- return new Set([e.from, ...e.joins].map(K));
160
+ function z(e, t) {
161
+ const n = (r) => r.type === "subquery" ? { ...r, query: z(r.query, t) } : j(r, n);
162
+ return F(e, (r) => $(n(r), t));
160
163
  }
161
164
  function Z(e) {
162
- return e !== void 0 && (e.type === "exists" || k(e).some(Z));
165
+ return new Set([e.from, ...e.joins].map(Q));
163
166
  }
164
167
  function q(e) {
165
- const t = z(e.query), n = (r) => r.type === "field" ? [r] : k(r).flatMap(n);
168
+ return e !== void 0 && (e.type === "exists" || v(e).some(q));
169
+ }
170
+ function ee(e) {
171
+ const t = Z(e.query), n = (r) => r.type === "field" ? [r] : v(r).flatMap(n);
166
172
  return n(e.link).filter((r) => r.table !== void 0 && !t.has(r.table));
167
173
  }
168
- const ge = {
174
+ const xe = {
169
175
  "date-expr": ["date", "dateTime"],
170
176
  "num-expr": ["integer", "decimal"],
171
177
  "str-expr": ["varchar"]
172
- }, u = { type: "field" }, ee = [
178
+ }, u = { type: "field" }, te = [
173
179
  "YEAR",
174
180
  "QUARTER",
175
181
  "MONTH",
@@ -180,20 +186,22 @@ const ge = {
180
186
  "SECOND",
181
187
  "MILLISECOND"
182
188
  ];
183
- function be(e, t) {
189
+ function ve(e, t) {
184
190
  if (t < e.args.length) return e.args[t];
185
191
  const n = e.args[e.args.length - 1];
186
192
  return n != null && n.variadic ? n : void 0;
187
193
  }
188
- function he(e) {
189
- return e.args.map((t) => t.default ?? { type: "literal", value: "" });
194
+ function ne(e) {
195
+ let t = e.args.length;
196
+ for (; t > 0 && e.args[t - 1].optional && !e.args[t - 1].default; ) t -= 1;
197
+ return e.args.slice(0, t).map((n) => n.default ?? { type: "literal", value: "" });
190
198
  }
191
199
  const T = (e, t = "DAY") => ({
192
200
  label: e,
193
201
  kind: "unit",
194
- choices: ee,
202
+ choices: te,
195
203
  default: { type: "literal", value: t }
196
- }), S = {
204
+ }), A = {
197
205
  // ---- Aggregate ----
198
206
  // COUNT with no argument is COUNT(*), and `distinct` on the call makes it
199
207
  // COUNT(DISTINCT x) — see the function sub-canvas's count choice.
@@ -251,6 +259,57 @@ const T = (e, t = "DAY") => ({
251
259
  }
252
260
  ]
253
261
  },
262
+ // ---- Window (issue 20) ----
263
+ // Each is written with OVER (PARTITION BY … ORDER BY …): see sqlGen.
264
+ // LAG / LEAD's offset is written into the SQL, MySQL taking nothing else.
265
+ LAG: {
266
+ name: "LAG",
267
+ returns: "arg",
268
+ window: !0,
269
+ args: [
270
+ { label: "value", kind: "expr", default: u },
271
+ {
272
+ label: "rows back",
273
+ kind: "fixed-count",
274
+ optional: !0,
275
+ default: { type: "literal", value: 1 }
276
+ },
277
+ { label: "default", kind: "expr", optional: !0 }
278
+ ]
279
+ },
280
+ LEAD: {
281
+ name: "LEAD",
282
+ returns: "arg",
283
+ window: !0,
284
+ args: [
285
+ { label: "value", kind: "expr", default: u },
286
+ {
287
+ label: "rows ahead",
288
+ kind: "fixed-count",
289
+ optional: !0,
290
+ default: { type: "literal", value: 1 }
291
+ },
292
+ { label: "default", kind: "expr", optional: !0 }
293
+ ]
294
+ },
295
+ ROW_NUMBER: {
296
+ name: "ROW_NUMBER",
297
+ returns: "integer",
298
+ window: !0,
299
+ args: []
300
+ },
301
+ RANK: {
302
+ name: "RANK",
303
+ returns: "integer",
304
+ window: !0,
305
+ args: []
306
+ },
307
+ DENSE_RANK: {
308
+ name: "DENSE_RANK",
309
+ returns: "integer",
310
+ window: !0,
311
+ args: []
312
+ },
254
313
  // ---- Date arithmetic ----
255
314
  DATE_ADD: {
256
315
  name: "DATE_ADD",
@@ -577,31 +636,42 @@ const T = (e, t = "DAY") => ({
577
636
  ]
578
637
  }
579
638
  };
580
- function D(e, t = S) {
639
+ function M(e, t = A) {
581
640
  return t[e.toUpperCase()];
582
641
  }
583
- function xe(e, t) {
642
+ function Te(e, t) {
584
643
  const n = e.toUpperCase();
585
- return D(e, t) ?? Object.values(t).find((r) => {
644
+ return M(e, t) ?? Object.values(t).find((r) => {
586
645
  var i;
587
646
  return (i = r.aliases) == null ? void 0 : i.some((a) => a.toUpperCase() === n);
588
647
  });
589
648
  }
590
- function te(e = S) {
649
+ function re(e = A) {
591
650
  return Object.keys(e).sort();
592
651
  }
593
- te(S);
594
- const Te = S;
595
- function ve(e) {
652
+ re(A);
653
+ const Ee = A;
654
+ function ke(e) {
596
655
  return Object.fromEntries(Object.values(e).map((t) => {
597
656
  const n = t.name.toUpperCase();
598
657
  return [n, { ...t, name: n }];
599
658
  }));
600
659
  }
601
- function ke(e) {
660
+ function we(e) {
602
661
  return Object.fromEntries(Object.entries(e).filter(([, t]) => !t.aggregate));
603
662
  }
604
- function Ee(e, t) {
663
+ function Ae(e, t) {
664
+ return {
665
+ type: "function",
666
+ name: t,
667
+ args: ne(e),
668
+ ...e.window && { over: !0 }
669
+ };
670
+ }
671
+ function Re(e) {
672
+ return Object.fromEntries(Object.entries(e).filter(([, t]) => !t.window));
673
+ }
674
+ function Se(e, t) {
605
675
  return Object.fromEntries(Object.entries(e).filter(([, n]) => !n.dialects || n.dialects.includes(t)));
606
676
  }
607
677
  function U(e, t) {
@@ -611,75 +681,79 @@ function U(e, t) {
611
681
  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
682
  return i.length === a.length && i.every((l) => U(n[l], r[l]));
613
683
  }
614
- function ne() {
684
+ function ae() {
615
685
  const e = globalThis.crypto.getRandomValues(new Uint8Array(12));
616
686
  return Array.from(e, (t) => t.toString(16).padStart(2, "0")).join("");
617
687
  }
618
- function _(e, t) {
688
+ function H(e, t) {
619
689
  var n;
620
- return e.type === "function" && ((n = D(e.name, t)) == null ? void 0 : n.aggregate) === !0;
690
+ return e.type === "function" && ((n = M(e.name, t)) == null ? void 0 : n.aggregate) === !0;
621
691
  }
622
692
  function x(e, t) {
623
- return e ? _(e, t) || k(e).some((n) => x(n, t)) : !1;
693
+ return e ? H(e, t) || v(e).some((n) => x(n, t)) : !1;
624
694
  }
625
- function F(e, t) {
626
- return e.type === "function" && e.name.trim() !== "" && D(e.name, t) === void 0;
695
+ const se = (e) => e.type === "function" && e.over === !0;
696
+ function I(e) {
697
+ return e ? se(e) || v(e).some(I) : !1;
627
698
  }
628
- function H(e, t) {
629
- return e ? F(e, t) || k(e).some((n) => H(n, t)) : !1;
699
+ function P(e, t) {
700
+ return e.type === "function" && e.name.trim() !== "" && M(e.name, t) === void 0;
630
701
  }
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)) : _(e, n) || F(e, n) ? !0 : k(e).every((r) => v(r, t, n));
702
+ function W(e, t) {
703
+ return e ? P(e, t) || v(e).some((n) => W(n, t)) : !1;
633
704
  }
634
- function P(e, t) {
705
+ function E(e, t, n) {
706
+ return t.some((r) => U(r, e)) ? !0 : e.type === "field" ? !e.column : e.type === "exists" ? ee(e).every((r) => E(r, t, n)) : H(e, n) || P(e, n) ? !0 : v(e).every((r) => E(r, t, n));
707
+ }
708
+ function K(e, t) {
635
709
  return (e.groupBy ?? []).length > 0 || e.having !== void 0 || [...e.select, ...e.orderBy ?? []].some((n) => x(n.expression, t));
636
710
  }
637
- function Ae(e, t) {
638
- if (P(e, t))
711
+ function ye(e, t) {
712
+ if (K(e, t))
639
713
  return (e.groupBy ?? []).flatMap(({ expression: n }) => n.type === "field" && n.table && n.column ? [`${n.table}.${n.column}`] : []);
640
714
  }
641
- function re(e, t) {
715
+ function le(e, t) {
642
716
  return x(e.having, t) || [...e.select, ...e.orderBy ?? []].some((n) => x(n.expression, t));
643
717
  }
644
- function Re(e, t) {
718
+ function Ce(e, t) {
645
719
  return e.select.some((n) => x(n.expression, t));
646
720
  }
647
- function Se(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) || H(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])), f = [];
721
+ function Oe(e, t) {
722
+ const n = e.groupBy ?? [], r = n.filter((o) => o.fromSelect === void 0), i = le(e, t), a = r.map((o) => o.expression), l = i ? e.select.filter((o) => x(o.expression, t) || I(o.expression) || W(o.expression, t) || E(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
723
  n.forEach((o) => {
650
724
  if (o.fromSelect === void 0) {
651
- f.push(o);
725
+ c.push(o);
652
726
  return;
653
727
  }
654
- const p = l.find((d) => d.id === o.fromSelect);
655
- p && f.push(U(o.expression, p.expression) ? o : { ...o, expression: p.expression });
728
+ const p = l.find((m) => m.id === o.fromSelect);
729
+ p && c.push(U(o.expression, p.expression) ? o : { ...o, expression: p.expression });
656
730
  });
657
- const m = l.filter((o) => !s.has(o.id)).map((o) => ({
658
- id: ne(),
731
+ const h = l.filter((o) => !s.has(o.id)).map((o) => ({
732
+ id: ae(),
659
733
  expression: o.expression,
660
734
  fromSelect: o.id
661
- })), h = [...f, ...m];
662
- return h.length === n.length && h.every((o, p) => o === n[p]) ? e : { ...e, groupBy: h };
735
+ })), g = [...c, ...h];
736
+ return g.length === n.length && g.every((o, p) => o === n[p]) ? e : { ...e, groupBy: g };
663
737
  }
664
- function Ce(e, t) {
738
+ function Ne(e, t) {
665
739
  const n = (e.groupBy ?? []).map((a) => a.expression), r = e.orderBy ?? [];
666
- if (!P(e, t))
740
+ if (!K(e, t))
667
741
  return {
668
742
  select: [],
669
743
  selectAll: !1,
670
744
  orderBy: [],
671
745
  having: !1
672
746
  };
673
- const i = (a) => a.filter((l) => !v(l.expression, n, t)).map((l) => l.id);
747
+ const i = (a) => a.filter((l) => !I(l.expression) && !E(l.expression, n, t)).map((l) => l.id);
674
748
  return {
675
749
  select: i(e.select),
676
750
  selectAll: e.select.length === 0,
677
751
  orderBy: i(r),
678
- having: e.having !== void 0 && !v(e.having, n, t)
752
+ having: e.having !== void 0 && !E(e.having, n, t)
679
753
  };
680
754
  }
681
- const Oe = { tables: [], relations: [] };
682
- function ye(e, t, n) {
755
+ const De = { tables: [], relations: [] };
756
+ function Me(e, t, n) {
683
757
  if (!t && !n) return e;
684
758
  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
759
  return {
@@ -687,10 +761,10 @@ function ye(e, t, n) {
687
761
  relations: e.relations.filter((a) => i.has(a.fromTable) && i.has(a.toTable))
688
762
  };
689
763
  }
690
- function we(e) {
764
+ function Ue(e) {
691
765
  return [...e.tables].sort((t, n) => t.label.localeCompare(n.label));
692
766
  }
693
- function Ne(e, t = (r) => r.group, n = e) {
767
+ function Ie(e, t = (r) => r.group, n = e) {
694
768
  const r = /* @__PURE__ */ new Map();
695
769
  n.forEach((a) => {
696
770
  const l = t(a) || void 0;
@@ -705,116 +779,119 @@ function Ne(e, t = (r) => r.group, n = e) {
705
779
  ...[...r].filter(([a]) => a === void 0)
706
780
  ].filter(([, a]) => a.length > 0).map(([a, l]) => ({ group: a, tables: i(l) }));
707
781
  }
708
- function W(e, t) {
782
+ function V(e, t) {
709
783
  return e.tables.find((n) => n.name === t);
710
784
  }
711
- function A(e, t, n) {
785
+ function N(e, t, n) {
712
786
  return e.relations.find((r) => r.fromTable === t && r.toTable === n || r.fromTable === n && r.toTable === t);
713
787
  }
714
- const C = (e, t) => {
788
+ const R = (e, t) => {
715
789
  var n;
716
- return ((n = W(e, t)) == null ? void 0 : n.passThrough) !== !1;
790
+ return ((n = V(e, t)) == null ? void 0 : n.passThrough) !== !1;
717
791
  };
718
- function I(e, t) {
719
- const n = t.filter((r) => C(e, r));
792
+ function B(e, t) {
793
+ const n = t.filter((r) => R(e, r));
720
794
  return n.length > 0 ? n : [...t];
721
795
  }
722
- function V(e, t) {
796
+ function L(e, t) {
723
797
  const n = /* @__PURE__ */ new Set();
724
798
  return t.forEach((r, i) => {
725
- const l = t.slice(0, i).map((f) => ({ earlier: f, r: A(e, f, r) })).find(({ r: f }) => f !== void 0);
799
+ const l = t.slice(0, i).map((c) => ({ earlier: c, r: N(e, c, r) })).find(({ r: c }) => c !== void 0);
726
800
  if (!l) return;
727
801
  (l.r.fromTable === l.earlier && l.r.toTable === r || n.has(l.earlier)) && n.add(r);
728
802
  }), n;
729
803
  }
730
- const X = (e, t) => e.fanOuts - t.fanOuts || e.steps - t.steps || e.order - t.order, R = (e, t) => `${t ? "^" : "_"}${e}`;
731
- function L(e, t) {
804
+ const X = (e, t) => e.fanOuts - t.fanOuts || e.steps - t.steps || e.order - t.order, w = (e, t) => `${t ? "^" : "_"}${e}`;
805
+ function Y(e, t) {
732
806
  let n;
733
807
  return e.forEach((r, i) => {
734
808
  !t.has(i) && (n === void 0 || X(r, e.get(n)) < 0) && (n = i);
735
809
  }), n;
736
810
  }
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() : V(e, t), s = /* @__PURE__ */ new Map();
739
- t.forEach((d, g) => {
740
- const b = R(d, l.has(d));
811
+ function ie(e, t, n, r = {}) {
812
+ const i = new Set(t), a = new Set(B(e, t)), l = r.anyShape ? /* @__PURE__ */ new Set() : L(e, t), s = /* @__PURE__ */ new Map();
813
+ t.forEach((m, d) => {
814
+ const b = w(m, l.has(m));
741
815
  s.has(b) || s.set(b, {
742
- table: d,
743
- climbed: l.has(d),
816
+ table: m,
817
+ climbed: l.has(m),
744
818
  fanOuts: 0,
745
819
  steps: 0,
746
- order: g
820
+ order: d
747
821
  });
748
822
  });
749
- const f = new Set([...s].filter(([, d]) => !a.has(d.table)).map(([d]) => d));
750
- let m = t.length;
751
- const h = (d) => {
752
- const g = s.get(d);
753
- g.steps > 0 && !C(e, g.table) || e.relations.forEach((b) => {
754
- if (b.fromTable !== g.table && b.toTable !== g.table) return;
755
- const E = b.fromTable === g.table, O = E ? b.toTable : b.fromTable;
756
- if (i.has(O) || !E && g.climbed) return;
823
+ const c = new Set([...s].filter(([, m]) => !a.has(m.table)).map(([m]) => m));
824
+ let h = t.length;
825
+ const g = (m) => {
826
+ const d = s.get(m);
827
+ d.steps > 0 && !R(e, d.table) || e.relations.forEach((b) => {
828
+ if (b.fromTable !== d.table && b.toTable !== d.table) return;
829
+ const k = b.fromTable === d.table, S = k ? b.toTable : b.fromTable;
830
+ if (i.has(S) || !k && d.climbed) return;
757
831
  const y = {
758
- table: O,
759
- climbed: !r.anyShape && (g.climbed || E),
760
- fanOuts: g.fanOuts + (E ? 0 : 1),
761
- steps: g.steps + 1,
762
- order: m,
763
- from: d
764
- }, w = R(O, y.climbed), B = s.get(w);
765
- f.has(w) || B && X(y, { ...B, order: m }) >= 0 || (s.set(w, y), m += 1);
832
+ table: S,
833
+ climbed: !r.anyShape && (d.climbed || k),
834
+ fanOuts: d.fanOuts + (k ? 0 : 1),
835
+ steps: d.steps + 1,
836
+ order: h,
837
+ from: m
838
+ }, C = w(S, y.climbed), G = s.get(C);
839
+ c.has(C) || G && X(y, { ...G, order: h }) >= 0 || (s.set(C, y), h += 1);
766
840
  });
767
841
  };
768
- let c = L(s, f);
769
- for (; c !== void 0 && s.get(c).table !== n; )
770
- f.add(c), h(c), c = L(s, f);
771
- if (c === void 0) return;
842
+ let f = Y(s, c);
843
+ for (; f !== void 0 && s.get(f).table !== n; )
844
+ c.add(f), g(f), f = Y(s, c);
845
+ if (f === void 0) return;
772
846
  const o = [];
773
- let p = s.get(c);
847
+ let p = s.get(f);
774
848
  for (; p.from !== void 0; )
775
849
  o.unshift(p.table), p = s.get(p.from);
776
850
  return { start: p.table, tables: o };
777
851
  }
778
- function Me(e, t, n, r = {}) {
852
+ function Be(e, t, n, r = {}) {
779
853
  var i;
780
- return t.includes(n) ? [] : (i = ae(e, t, n, r)) == null ? void 0 : i.tables;
854
+ return t.includes(n) ? [] : (i = ie(e, t, n, r)) == null ? void 0 : i.tables;
781
855
  }
782
- function De(e, t, n = {}) {
783
- const r = /* @__PURE__ */ new Map(), i = (c, o, p) => r.set(c, [
784
- ...r.get(c) ?? [],
856
+ function Le(e, t, n = {}) {
857
+ const r = /* @__PURE__ */ new Map(), i = (f, o, p) => r.set(f, [
858
+ ...r.get(f) ?? [],
785
859
  { table: o, up: p }
786
860
  ]);
787
- e.relations.forEach((c) => {
788
- i(c.fromTable, c.toTable, !0), i(c.toTable, c.fromTable, !1);
861
+ e.relations.forEach((f) => {
862
+ i(f.fromTable, f.toTable, !0), i(f.toTable, f.fromTable, !1);
789
863
  });
790
- const { skip: a = /* @__PURE__ */ new Set() } = n, l = n.anyShape ? /* @__PURE__ */ new Set() : V(e, t), s = new Set(t), f = new Set(t), m = I(e, t).map((c) => ({ table: c, climbed: l.has(c) })), h = new Set(m.map((c) => R(c.table, c.climbed)));
791
- for (let c = m.shift(); c !== void 0; c = m.shift()) {
792
- const o = c;
793
- (r.get(o.table) ?? []).forEach(({ table: p, up: d }) => {
794
- if (s.has(p) || a.has(p) || !d && o.climbed) return;
795
- const g = { table: p, climbed: !n.anyShape && (o.climbed || d) }, b = R(p, g.climbed);
796
- h.has(b) || (h.add(b), f.add(p), C(e, p) && m.push(g));
864
+ const { skip: a = /* @__PURE__ */ new Set() } = n, l = n.anyShape ? /* @__PURE__ */ new Set() : L(e, t), s = new Set(t), c = new Set(t), h = B(e, t).map((f) => ({ table: f, climbed: l.has(f) })), g = new Set(h.map((f) => w(f.table, f.climbed)));
865
+ for (let f = h.shift(); f !== void 0; f = h.shift()) {
866
+ const o = f;
867
+ (r.get(o.table) ?? []).forEach(({ table: p, up: m }) => {
868
+ if (s.has(p) || a.has(p) || !m && o.climbed) return;
869
+ const d = { table: p, climbed: !n.anyShape && (o.climbed || m) }, b = w(p, d.climbed);
870
+ g.has(b) || (g.add(b), c.add(p), R(e, p) && h.push(d));
797
871
  });
798
872
  }
799
- return f;
800
- }
801
- function Ue(e, t, n, r = {}) {
802
- var o;
803
- const { after: i, leftJoined: a = [] } = r;
804
- let l = C(e, n) ? I(e, t) : t;
805
- i !== void 0 && A(e, i, n) && (l = [i]);
806
- const s = l.find((p) => A(e, p, n) !== void 0);
807
- if (s === void 0) return;
808
- const f = A(e, s, n), m = (o = W(e, f.fromTable)) == null ? void 0 : o.columns.find((p) => p.name === f.fromColumn), h = m != null && m.nullable || a.includes(s) ? "left" : "inner", c = {
873
+ return c;
874
+ }
875
+ function Ge(e, t, n, r = {}) {
876
+ var m;
877
+ const { after: i, leftJoined: a = [] } = r, l = r.anyShape ? /* @__PURE__ */ new Set() : L(e, t), s = (d) => {
878
+ const b = N(e, d, n);
879
+ return b !== void 0 && !(l.has(d) && b.toTable === d && b.fromTable === n);
880
+ };
881
+ let c = R(e, n) ? B(e, t) : t;
882
+ i !== void 0 && s(i) && (c = [i]);
883
+ const h = c.find(s);
884
+ if (h === void 0) return;
885
+ 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 = {
809
886
  type: "binary",
810
887
  operator: "=",
811
- left: { type: "field", table: f.fromTable, column: f.fromColumn },
812
- right: { type: "field", table: f.toTable, column: f.toColumn }
888
+ left: { type: "field", table: g.fromTable, column: g.fromColumn },
889
+ right: { type: "field", table: g.toTable, column: g.toColumn }
813
890
  };
814
- return { joinType: h, on: c };
891
+ return { joinType: o, on: p };
815
892
  }
816
- const G = (e, t) => `${e}.${t}`;
817
- function se({ type: e, format: t }) {
893
+ const _ = (e, t) => `${e}.${t}`;
894
+ function ue({ type: e, format: t }) {
818
895
  if (t === void 0) return;
819
896
  if (e !== "date" && e !== "dateTime" && e !== "time")
820
897
  return `a format is for a date, date-and-time or time column, not ${e}`;
@@ -823,7 +900,7 @@ function se({ type: e, format: t }) {
823
900
  if (e === "time" && n.some((r) => r === "YYYY" || r === "MM" || r === "DD"))
824
901
  return `a time has no day: format "${t}" can't be written from one`;
825
902
  }
826
- function Ie(e) {
903
+ function Ye(e) {
827
904
  const t = [], n = /* @__PURE__ */ new Map();
828
905
  e.tables.forEach((a) => {
829
906
  n.has(a.name) && t.push({
@@ -836,16 +913,16 @@ function Ie(e) {
836
913
  kind: "duplicateColumn",
837
914
  message: `Column "${a.name}.${s.name}" is declared more than once`
838
915
  }), l.add(s.name);
839
- const f = se(s);
840
- f && t.push({
916
+ const c = ue(s);
917
+ c && t.push({
841
918
  kind: "invalidFormat",
842
- message: `Column "${a.name}.${s.name}": ${f}`
919
+ message: `Column "${a.name}.${s.name}": ${c}`
843
920
  });
844
921
  });
845
922
  });
846
923
  const r = (a, l) => {
847
924
  const s = n.get(a);
848
- s ? s.columns.some((f) => f.name === l) || t.push({
925
+ s ? s.columns.some((c) => c.name === l) || t.push({
849
926
  kind: "unknownColumn",
850
927
  message: `Relation references unknown column "${a}.${l}"`
851
928
  }) : t.push({
@@ -855,64 +932,68 @@ function Ie(e) {
855
932
  }, i = /* @__PURE__ */ new Set();
856
933
  return e.relations.forEach((a) => {
857
934
  r(a.fromTable, a.fromColumn), r(a.toTable, a.toColumn);
858
- const l = G(a.fromTable, a.fromColumn), s = G(a.toTable, a.toColumn), f = [l, s].sort().join(" <-> ");
859
- i.has(f) && t.push({
935
+ const l = _(a.fromTable, a.fromColumn), s = _(a.toTable, a.toColumn), c = [l, s].sort().join(" <-> ");
936
+ i.has(c) && t.push({
860
937
  kind: "duplicateRelation",
861
938
  message: `Relation ${l} -> ${s} is declared more than once (declare each foreign key once)`
862
- }), i.add(f);
939
+ }), i.add(c);
863
940
  }), t;
864
941
  }
865
942
  export {
866
- ve as A,
867
- P as B,
868
- le as C,
869
- Te as D,
870
- Oe as E,
871
- Ce as F,
872
- be as G,
873
- H,
874
- D as I,
875
- me as J,
876
- oe as K,
877
- z as L,
878
- ce as M,
943
+ se as $,
944
+ ke as A,
945
+ K as B,
946
+ oe as C,
947
+ Ee as D,
948
+ De as E,
949
+ Ne as F,
950
+ ve as G,
951
+ I as H,
952
+ W as I,
953
+ M as J,
954
+ he as K,
955
+ pe as L,
956
+ me as M,
879
957
  Z as N,
880
- de as O,
881
- ae as P,
882
- $ as Q,
883
- J as R,
884
- ke as S,
885
- te as T,
886
- ge as U,
887
- he as V,
888
- Ne as W,
889
- _ as X,
890
- Ae as Y,
891
- Re as Z,
892
- Me as a,
893
- ie as b,
894
- Ee as c,
958
+ q as O,
959
+ be as P,
960
+ ie as Q,
961
+ $ as R,
962
+ z as S,
963
+ Re as T,
964
+ we as U,
965
+ re as V,
966
+ xe as W,
967
+ Ae as X,
968
+ Ie as Y,
969
+ ne as Z,
970
+ H as _,
971
+ Be as a,
972
+ ye as a0,
973
+ Ce as a1,
974
+ fe as b,
975
+ Se as c,
895
976
  U as d,
896
- j as e,
897
- ye as f,
898
- N as g,
899
- fe as h,
900
- M as i,
901
- W as j,
902
- ee as k,
903
- ne as l,
904
- Y as m,
905
- ue as n,
906
- pe as o,
907
- xe as p,
908
- Q as q,
909
- De as r,
910
- Ue as s,
911
- K as t,
977
+ F as e,
978
+ Me as f,
979
+ O as g,
980
+ de as h,
981
+ D as i,
982
+ V as j,
983
+ te as k,
984
+ ae as l,
985
+ j as m,
986
+ ce as n,
987
+ ge as o,
988
+ Te as p,
989
+ J as q,
990
+ Le as r,
991
+ Ge as s,
992
+ Q as t,
912
993
  x as u,
913
- Ie as v,
914
- re as w,
915
- Se as x,
916
- k as y,
917
- we as z
994
+ Ye as v,
995
+ le as w,
996
+ Oe as x,
997
+ v as y,
998
+ Ue as z
918
999
  };