@nongt/visql-core 0.3.0 → 0.3.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.
|
@@ -38,7 +38,7 @@ function j(t, e, r, n) {
|
|
|
38
38
|
if (!s || Z(e.where, r.condition)) return e;
|
|
39
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
40
|
if (f) throw new wt(n, s, f);
|
|
41
|
-
const { joins:
|
|
41
|
+
const { joins: u } = Nt(
|
|
42
42
|
t,
|
|
43
43
|
{
|
|
44
44
|
id: "",
|
|
@@ -47,9 +47,9 @@ function j(t, e, r, n) {
|
|
|
47
47
|
select: []
|
|
48
48
|
},
|
|
49
49
|
r.condition
|
|
50
|
-
), { where:
|
|
50
|
+
), { where: c } = e;
|
|
51
51
|
let l = r.condition;
|
|
52
|
-
return (
|
|
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
53
|
}
|
|
54
54
|
function le(t, e, r) {
|
|
55
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));
|
|
@@ -169,15 +169,15 @@ function ye(t, e, r) {
|
|
|
169
169
|
}
|
|
170
170
|
function Ut(t, e, r, n = {}) {
|
|
171
171
|
const s = Ot(e), i = V(r);
|
|
172
|
-
if (s && i.some((
|
|
172
|
+
if (s && i.some((u) => u !== s.alias)) {
|
|
173
173
|
if (i.includes(s.alias))
|
|
174
174
|
throw new Error("addCondition: a condition names both the union and tables of its branches: add it in two parts");
|
|
175
|
-
return s.query.union.forEach((
|
|
176
|
-
const
|
|
175
|
+
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
177
|
if (p)
|
|
178
|
-
throw new Error(`addCondition: "${p}" can't be reached from "${
|
|
179
|
-
}), s.query.union.reduce((
|
|
180
|
-
|
|
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(
|
|
180
|
+
u,
|
|
181
181
|
[St(s.id, l)],
|
|
182
182
|
($) => Ut(t, $, r, n)
|
|
183
183
|
), e);
|
|
@@ -321,7 +321,11 @@ function U(t, e) {
|
|
|
321
321
|
a && s.add(a);
|
|
322
322
|
return;
|
|
323
323
|
}
|
|
324
|
-
|
|
324
|
+
if (r.set(i, i), a) {
|
|
325
|
+
r.set(a, i), n.add(a), s.delete(a);
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
n.delete(i), s.delete(i);
|
|
325
329
|
}), { tableOf: r, aliases: n, derived: s };
|
|
326
330
|
}
|
|
327
331
|
function Gt(t, e, r) {
|
|
@@ -407,34 +411,34 @@ function tt(t, e) {
|
|
|
407
411
|
function et(t, e) {
|
|
408
412
|
const r = /* @__PURE__ */ new Map(), n = (s) => {
|
|
409
413
|
const i = /* @__PURE__ */ new Map();
|
|
410
|
-
d(s).forEach((
|
|
411
|
-
const a = (
|
|
412
|
-
if ((
|
|
413
|
-
const
|
|
414
|
+
d(s).forEach((u) => i.set(Q(u), u.table));
|
|
415
|
+
const a = (u) => {
|
|
416
|
+
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);
|
|
414
418
|
return $ == null ? void 0 : $.format;
|
|
415
|
-
}, o = (
|
|
416
|
-
switch (
|
|
419
|
+
}, o = (u, c) => {
|
|
420
|
+
switch (u.type) {
|
|
417
421
|
case "literal":
|
|
418
|
-
return { ...
|
|
422
|
+
return { ...u, value: tt(u.value, c) };
|
|
419
423
|
case "list":
|
|
420
|
-
return { ...
|
|
424
|
+
return { ...u, items: u.items.map((l) => o(l, c)) };
|
|
421
425
|
case "param":
|
|
422
|
-
return r.has(
|
|
426
|
+
return r.has(u.name) || r.set(u.name, c), u;
|
|
423
427
|
default:
|
|
424
|
-
return
|
|
428
|
+
return u;
|
|
425
429
|
}
|
|
426
|
-
}, f = (
|
|
427
|
-
if (
|
|
428
|
-
const
|
|
429
|
-
if (
|
|
430
|
-
const l = a(
|
|
430
|
+
}, f = (u) => {
|
|
431
|
+
if (u.type === "subquery") return { ...u, query: n(u.query) };
|
|
432
|
+
const c = X(u, f);
|
|
433
|
+
if (c.type !== "binary" || !c.right) return c;
|
|
434
|
+
const l = a(c.left), $ = a(c.right);
|
|
431
435
|
return {
|
|
432
|
-
...
|
|
433
|
-
left: $ ? o(
|
|
434
|
-
right: l ? o(
|
|
436
|
+
...c,
|
|
437
|
+
left: $ ? o(c.left, $) : c.left,
|
|
438
|
+
right: l ? o(c.right, l) : c.right
|
|
435
439
|
};
|
|
436
440
|
};
|
|
437
|
-
return k(s, f, (
|
|
441
|
+
return k(s, f, (u) => n(u));
|
|
438
442
|
};
|
|
439
443
|
return { query: n(t), params: r };
|
|
440
444
|
}
|
|
@@ -716,16 +720,16 @@ const w = {
|
|
|
716
720
|
// The separator goes into the SQL as a literal even in parameterized SQL:
|
|
717
721
|
// MySQL's SEPARATOR takes nothing else.
|
|
718
722
|
STRING_AGG: ([t = "??"], e, r, n, s, i) => {
|
|
719
|
-
const a = e[1] ?? { type: "literal", value: ", " }, o = a.type === "literal" && typeof a.value == "string" ? st(a.value, r) : "??", f = s.distinct ? "DISTINCT " : "",
|
|
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 = ($) => {
|
|
720
724
|
var p;
|
|
721
725
|
return (p = s.orderBy) != null && p.length ? `ORDER BY ${s.orderBy.map((A) => `${$(A)} ${A.direction === "desc" ? "DESC" : "ASC"}`).join(", ")}` : "";
|
|
722
|
-
},
|
|
726
|
+
}, c = ($) => i($.expression), l = ($) => $ ? ` ${$}` : "";
|
|
723
727
|
switch (r) {
|
|
724
728
|
case "mysql":
|
|
725
|
-
return `GROUP_CONCAT(${f}${t}${l(c
|
|
729
|
+
return `GROUP_CONCAT(${f}${t}${l(u(c))} SEPARATOR ${o})`;
|
|
726
730
|
case "postgres": {
|
|
727
|
-
const $ = `CAST(${t} AS TEXT)`, p =
|
|
728
|
-
if (!s.distinct) return
|
|
731
|
+
const $ = `CAST(${t} AS TEXT)`, p = u((A) => {
|
|
732
|
+
if (!s.distinct) return c(A);
|
|
729
733
|
if (F(A.expression, e[0])) return $;
|
|
730
734
|
throw new Error("PostgreSQL orders STRING_AGG of different values by the value only");
|
|
731
735
|
});
|
|
@@ -734,11 +738,11 @@ const w = {
|
|
|
734
738
|
case "mssql": {
|
|
735
739
|
if (s.distinct)
|
|
736
740
|
throw new Error("SQL Server's STRING_AGG can't join only different values");
|
|
737
|
-
const $ = c
|
|
741
|
+
const $ = u(c), p = $ ? ` WITHIN GROUP (${$})` : "";
|
|
738
742
|
return `STRING_AGG(CAST(${t} AS NVARCHAR(MAX)), ${o})${p}`;
|
|
739
743
|
}
|
|
740
744
|
default:
|
|
741
|
-
return `STRING_AGG(${f}${t}, ${o}${l(c
|
|
745
|
+
return `STRING_AGG(${f}${t}, ${o}${l(u(c))})`;
|
|
742
746
|
}
|
|
743
747
|
}
|
|
744
748
|
};
|
|
@@ -787,21 +791,21 @@ function zt(t, e, r, n) {
|
|
|
787
791
|
t,
|
|
788
792
|
(l) => E(l, e, r, n)
|
|
789
793
|
);
|
|
790
|
-
const
|
|
791
|
-
if (o.length === 0 &&
|
|
792
|
-
const
|
|
793
|
-
return `${
|
|
794
|
+
const u = /^[A-Za-z_][A-Za-z0-9_.]*$/.test(t.name) || t.name === "" ? t.name : "??";
|
|
795
|
+
if (o.length === 0 && u.toUpperCase() === "COUNT") return `${u}(*)`;
|
|
796
|
+
const c = t.distinct && o.length > 0 ? "DISTINCT " : "";
|
|
797
|
+
return `${u}(${c}${o.join(", ")})`;
|
|
794
798
|
}
|
|
795
799
|
case "binary": {
|
|
796
|
-
const o = t.operator ? Jt(t.operator, e) : "=", f = E(t.left, e, r, n),
|
|
797
|
-
if (Mt(t.operator)) return `${
|
|
798
|
-
if (!t.right) return `${
|
|
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}`;
|
|
802
|
+
if (!t.right) return `${u} ${o} ??`;
|
|
799
803
|
if (o === "BETWEEN")
|
|
800
|
-
return `${
|
|
804
|
+
return `${u} BETWEEN ${G(t.right, e, r, n)}`;
|
|
801
805
|
if (o === "IN")
|
|
802
|
-
return t.right.type === "list" ? `${
|
|
803
|
-
const
|
|
804
|
-
return `${
|
|
806
|
+
return t.right.type === "list" ? `${u} IN ${E(t.right, e, r, n)}` : `${u} IN (${E(t.right, e, r, n)})`;
|
|
807
|
+
const c = E(t.right, e, r, n), l = R(t.right) ? `(${c})` : c;
|
|
808
|
+
return `${u} ${o} ${l}`;
|
|
805
809
|
}
|
|
806
810
|
case "logical": {
|
|
807
811
|
if (t.operands.length === 0) return "/* empty */";
|
|
@@ -811,9 +815,9 @@ function zt(t, e, r, n) {
|
|
|
811
815
|
${s}${t.operator} `;
|
|
812
816
|
return t.operands.map((f) => {
|
|
813
817
|
if (f.type === "logical") {
|
|
814
|
-
const
|
|
818
|
+
const u = E(f, e, r + 1, n);
|
|
815
819
|
return `(
|
|
816
|
-
${i}${
|
|
820
|
+
${i}${u}
|
|
817
821
|
${s})`;
|
|
818
822
|
}
|
|
819
823
|
return E(f, e, r, n);
|
|
@@ -838,34 +842,34 @@ ${s})`;
|
|
|
838
842
|
return `NOT ${E(o, e, r, n)}`;
|
|
839
843
|
}
|
|
840
844
|
case "if": {
|
|
841
|
-
const o = t.condition ? E(t.condition, e, r, n) : "??", f = t.then ? E(t.then, e, r, n) : "??",
|
|
842
|
-
return e === "mysql" ? `IF(${o}, ${f}, ${
|
|
845
|
+
const o = t.condition ? E(t.condition, e, r, n) : "??", f = t.then ? E(t.then, e, r, n) : "??", u = t.else ? E(t.else, e, r, n) : "NULL";
|
|
846
|
+
return e === "mysql" ? `IF(${o}, ${f}, ${u})` : e === "mssql" ? `IIF(${o}, ${f}, ${u})` : `CASE WHEN ${o} THEN ${f} ELSE ${u} END`;
|
|
843
847
|
}
|
|
844
848
|
case "case": {
|
|
845
|
-
const o = t.subject ? ` ${E(t.subject, e, r, n)}` : "", f = t.branches.map((
|
|
846
|
-
const l = E(
|
|
849
|
+
const o = t.subject ? ` ${E(t.subject, e, r, n)}` : "", f = t.branches.map((c) => {
|
|
850
|
+
const l = E(c.when, e, r + 1, n), $ = E(c.then, e, r + 1, n);
|
|
847
851
|
return `${i}WHEN ${l} THEN ${$}`;
|
|
848
852
|
}).join(`
|
|
849
|
-
`),
|
|
853
|
+
`), u = t.else ? `
|
|
850
854
|
${i}ELSE ${E(t.else, e, r + 1, n)}` : "";
|
|
851
855
|
return `CASE${o}
|
|
852
|
-
${f}${
|
|
856
|
+
${f}${u}
|
|
853
857
|
${s}END`;
|
|
854
858
|
}
|
|
855
859
|
// EXISTS (SELECT 1 FROM … WHERE <link> AND <where>): the subquery written
|
|
856
860
|
// in place, one level in, so its params fall in reading order.
|
|
857
861
|
case "exists": {
|
|
858
|
-
const { query: o, link: f } = t,
|
|
862
|
+
const { query: o, link: f } = t, u = ($) => $.type === "logical" && $.operator === "AND" ? $.operands : [$], c = o.where ? { type: "logical", operator: "AND", operands: [...u(f), ...u(o.where)] } : f;
|
|
859
863
|
return `EXISTS (
|
|
860
864
|
${[
|
|
861
865
|
"SELECT 1",
|
|
862
866
|
// The clauses below write their expressions with formatNode.
|
|
863
867
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
864
|
-
|
|
868
|
+
ct(o.from, e),
|
|
865
869
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
866
870
|
...o.joins.map(($) => ft($, e, n)),
|
|
867
871
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
868
|
-
it(
|
|
872
|
+
it(c, e, n)
|
|
869
873
|
].join(`
|
|
870
874
|
`).replace(/^/gm, i)}
|
|
871
875
|
${s})`;
|
|
@@ -898,10 +902,10 @@ const Zt = {
|
|
|
898
902
|
let ot = /* @__PURE__ */ new Map();
|
|
899
903
|
function at(t, e, r) {
|
|
900
904
|
return t.shared !== void 0 ? m(ot.get(t.shared) ?? "??", e) : t.query ? `(
|
|
901
|
-
${
|
|
905
|
+
${ut(t.query, e, r).replace(/^/gm, " ")}
|
|
902
906
|
)` : m(t.table, e);
|
|
903
907
|
}
|
|
904
|
-
function
|
|
908
|
+
function ut(t, e, r) {
|
|
905
909
|
return gt(t).map((n) => _(n, e, r).join(`
|
|
906
910
|
`)).join(Dt(t) && !t.all ? `
|
|
907
911
|
UNION
|
|
@@ -912,14 +916,14 @@ UNION ALL
|
|
|
912
916
|
function te(t, e, r) {
|
|
913
917
|
return t.length === 0 ? [] : [`WITH
|
|
914
918
|
${t.map((s) => T(s, () => {
|
|
915
|
-
const i =
|
|
919
|
+
const i = ut(s.query, e, r).replace(/^/gm, " ");
|
|
916
920
|
return ` ${m(s.name, e)} AS (
|
|
917
921
|
${i}
|
|
918
922
|
)`;
|
|
919
923
|
})).join(`,
|
|
920
924
|
`)}`];
|
|
921
925
|
}
|
|
922
|
-
function
|
|
926
|
+
function ct(t, e, r) {
|
|
923
927
|
const n = t.alias ? ` AS ${m(t.alias, e)}` : "";
|
|
924
928
|
return `FROM ${T(t, () => `${at(t, e, r)}${n}`)}`;
|
|
925
929
|
}
|
|
@@ -968,7 +972,7 @@ const ie = "18446744073709551615";
|
|
|
968
972
|
function _(t, e, r, n = `SELECT${t.distinct ? " DISTINCT" : ""}`) {
|
|
969
973
|
const s = [
|
|
970
974
|
ee(t.select, e, r, n),
|
|
971
|
-
|
|
975
|
+
ct(t.from, e, r),
|
|
972
976
|
...t.joins.map((a) => ft(a, e, r))
|
|
973
977
|
];
|
|
974
978
|
t.where && s.push(it(t.where, e, r));
|
|
@@ -980,19 +984,19 @@ function _(t, e, r, n = `SELECT${t.distinct ? " DISTINCT" : ""}`) {
|
|
|
980
984
|
return s;
|
|
981
985
|
}
|
|
982
986
|
function $t(t, e, r) {
|
|
983
|
-
N && (N.writing = [{ value: t, path: [] }]), ot = new Map((t.shared ?? []).map((
|
|
984
|
-
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})`)}` : ""}`,
|
|
985
|
-
if (a.length > 0 &&
|
|
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") {
|
|
986
990
|
if (i !== void 0) {
|
|
987
|
-
a.length === 0 &&
|
|
988
|
-
const
|
|
989
|
-
|
|
991
|
+
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}`);
|
|
990
994
|
}
|
|
991
995
|
} else if (s !== void 0 || i !== void 0) {
|
|
992
|
-
const
|
|
993
|
-
|
|
996
|
+
const c = s !== void 0 || e === "mysql" ? T(s !== void 0 ? "limit" : {}, () => `LIMIT ${s ?? ie}`) : "", l = i !== void 0 ? T("offset", () => `OFFSET ${i}`) : "";
|
|
997
|
+
u.push([c, l].filter(Boolean).join(" "));
|
|
994
998
|
}
|
|
995
|
-
return [...n, ...
|
|
999
|
+
return [...n, ...u].join(`
|
|
996
1000
|
`);
|
|
997
1001
|
}
|
|
998
1002
|
function Ce(t, e, r = {}) {
|
|
@@ -1014,13 +1018,13 @@ function oe(t, e) {
|
|
|
1014
1018
|
function Le(t, e, r = {}) {
|
|
1015
1019
|
const n = r.schema ? et(t, r.schema) : { query: t, params: /* @__PURE__ */ new Map() }, s = {
|
|
1016
1020
|
values: [],
|
|
1017
|
-
named: new Map(Object.entries(r.values ?? {}).map(([
|
|
1018
|
-
const l = n.params.get(
|
|
1019
|
-
return [
|
|
1021
|
+
named: new Map(Object.entries(r.values ?? {}).map(([u, c]) => {
|
|
1022
|
+
const l = n.params.get(u);
|
|
1023
|
+
return [u, l ? tt(c, l) : c];
|
|
1020
1024
|
}))
|
|
1021
1025
|
}, i = lt(n.query, r), a = $t(i, e, s), o = [];
|
|
1022
|
-
return { sql: a.replace(Xt, (
|
|
1023
|
-
const l = s.values[Number(
|
|
1026
|
+
return { sql: a.replace(Xt, (u, c) => {
|
|
1027
|
+
const l = s.values[Number(c)];
|
|
1024
1028
|
return o.push(l instanceof Date ? J(l) : l), oe(e, o.length);
|
|
1025
1029
|
}), params: o };
|
|
1026
1030
|
}
|
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
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";
|
|
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-
|
|
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";
|
|
5
5
|
export {
|
|
6
6
|
e as DEFAULT_FUNCTIONS,
|
|
7
7
|
ea as DIALECTS,
|
package/package.json
CHANGED