metal-orm 1.1.23 → 1.1.24
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.
- package/dist/index.cjs +319 -197
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +104 -16
- package/dist/index.d.ts +104 -16
- package/dist/index.js +314 -197
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/core/dialect/base/sql-dialect.ts +73 -211
- package/src/core/dialect/base/standard-delete-compiler.ts +37 -0
- package/src/core/dialect/base/standard-insert-compiler.ts +49 -0
- package/src/core/dialect/base/standard-select-compiler.ts +82 -0
- package/src/core/dialect/base/standard-sql-services.ts +44 -0
- package/src/core/dialect/base/standard-sql-source-compiler.ts +88 -0
- package/src/core/dialect/base/standard-update-compiler.ts +53 -0
- package/src/index.ts +6 -0
package/dist/index.js
CHANGED
|
@@ -1686,6 +1686,50 @@ var DialectBase = class _DialectBase {
|
|
|
1686
1686
|
}
|
|
1687
1687
|
};
|
|
1688
1688
|
|
|
1689
|
+
// src/core/dialect/base/pagination-strategy.ts
|
|
1690
|
+
var StandardLimitOffsetPagination = class {
|
|
1691
|
+
/**
|
|
1692
|
+
* Compiles LIMIT/OFFSET pagination clause.
|
|
1693
|
+
* @param limit - The maximum number of rows to return.
|
|
1694
|
+
* @param offset - The number of rows to skip.
|
|
1695
|
+
* @returns SQL pagination clause with LIMIT and/or OFFSET.
|
|
1696
|
+
*/
|
|
1697
|
+
compilePagination(limit, offset) {
|
|
1698
|
+
const parts = [];
|
|
1699
|
+
if (limit !== void 0) parts.push(`LIMIT ${limit}`);
|
|
1700
|
+
if (offset !== void 0) parts.push(`OFFSET ${offset}`);
|
|
1701
|
+
return parts.length ? ` ${parts.join(" ")}` : "";
|
|
1702
|
+
}
|
|
1703
|
+
};
|
|
1704
|
+
|
|
1705
|
+
// src/core/dialect/base/returning-strategy.ts
|
|
1706
|
+
var NoReturningStrategy = class {
|
|
1707
|
+
/**
|
|
1708
|
+
* Throws an error as RETURNING is not supported.
|
|
1709
|
+
* @param returning - Columns to return (causes error if non-empty).
|
|
1710
|
+
* @param _ctx - Compiler context (unused).
|
|
1711
|
+
* @throws Error indicating RETURNING is not supported.
|
|
1712
|
+
*/
|
|
1713
|
+
compileReturning(returning, _ctx) {
|
|
1714
|
+
void _ctx;
|
|
1715
|
+
if (!returning || returning.length === 0) return "";
|
|
1716
|
+
throw new Error("RETURNING is not supported by this dialect.");
|
|
1717
|
+
}
|
|
1718
|
+
/**
|
|
1719
|
+
* Formats column names for RETURNING clause.
|
|
1720
|
+
* @param returning - Columns to format.
|
|
1721
|
+
* @param quoteIdentifier - Function to quote identifiers according to dialect rules.
|
|
1722
|
+
* @returns Simple comma-separated column names.
|
|
1723
|
+
*/
|
|
1724
|
+
formatReturningColumns(returning, quoteIdentifier) {
|
|
1725
|
+
return returning.map((column) => {
|
|
1726
|
+
const tablePart = column.table ? `${quoteIdentifier(column.table)}.` : "";
|
|
1727
|
+
const aliasPart = column.alias ? ` AS ${quoteIdentifier(column.alias)}` : "";
|
|
1728
|
+
return `${tablePart}${quoteIdentifier(column.name)}${aliasPart}`;
|
|
1729
|
+
}).join(", ");
|
|
1730
|
+
}
|
|
1731
|
+
};
|
|
1732
|
+
|
|
1689
1733
|
// src/core/dialect/base/function-table-formatter.ts
|
|
1690
1734
|
var FunctionTableFormatter = class {
|
|
1691
1735
|
static format(fn9, ctx, formatter) {
|
|
@@ -1722,19 +1766,71 @@ var FunctionTableFormatter = class {
|
|
|
1722
1766
|
}
|
|
1723
1767
|
};
|
|
1724
1768
|
|
|
1725
|
-
// src/core/dialect/base/
|
|
1726
|
-
var
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1769
|
+
// src/core/dialect/base/standard-sql-source-compiler.ts
|
|
1770
|
+
var StandardSqlSourceCompiler = class {
|
|
1771
|
+
constructor(services) {
|
|
1772
|
+
this.services = services;
|
|
1773
|
+
}
|
|
1774
|
+
compileFrom(source, ctx) {
|
|
1775
|
+
if (source.type === "FunctionTable") return this.compileFunctionTable(source, ctx);
|
|
1776
|
+
if (source.type === "DerivedTable") return this.compileDerivedTable(source, ctx);
|
|
1777
|
+
return this.compileTableSource(source);
|
|
1778
|
+
}
|
|
1779
|
+
compileFunctionTable(fn9, ctx) {
|
|
1780
|
+
const key = fn9.key ?? fn9.name;
|
|
1781
|
+
if (ctx) {
|
|
1782
|
+
const renderer = this.services.getTableFunctionStrategy().getRenderer(key);
|
|
1783
|
+
if (renderer) {
|
|
1784
|
+
const compiledArgs = (fn9.args ?? []).map((arg) => this.services.compileOperand(arg, ctx));
|
|
1785
|
+
return renderer({
|
|
1786
|
+
node: fn9,
|
|
1787
|
+
compiledArgs,
|
|
1788
|
+
compileOperand: (operand) => this.services.compileOperand(operand, ctx),
|
|
1789
|
+
quoteIdentifier: (id) => this.services.quoteIdentifier(id)
|
|
1790
|
+
});
|
|
1791
|
+
}
|
|
1792
|
+
if (fn9.key) {
|
|
1793
|
+
throw new Error(
|
|
1794
|
+
`Table function "${key}" is not supported by dialect "${this.services.getDialectName()}".`
|
|
1795
|
+
);
|
|
1796
|
+
}
|
|
1797
|
+
}
|
|
1798
|
+
return FunctionTableFormatter.format(fn9, ctx, {
|
|
1799
|
+
quoteIdentifier: (id) => this.services.quoteIdentifier(id),
|
|
1800
|
+
compileOperand: (node, compilerContext) => this.services.compileOperand(node, compilerContext)
|
|
1801
|
+
});
|
|
1802
|
+
}
|
|
1803
|
+
compileDerivedTable(table, ctx) {
|
|
1804
|
+
if (!table.alias) throw new Error("Derived tables must have an alias.");
|
|
1805
|
+
if (!ctx) throw new Error("Derived table compilation requires a compiler context.");
|
|
1806
|
+
const normalized = this.services.normalizeSelectAst(table.query);
|
|
1807
|
+
const subquery = this.services.compileSelectAst(normalized, ctx).trim().replace(/;$/, "");
|
|
1808
|
+
const columns = table.columnAliases?.length ? ` (${table.columnAliases.map((column) => this.services.quoteIdentifier(column)).join(", ")})` : "";
|
|
1809
|
+
return `(${subquery}) AS ${this.services.quoteIdentifier(table.alias)}${columns}`;
|
|
1810
|
+
}
|
|
1811
|
+
compileTableSource(table) {
|
|
1812
|
+
if (table.type === "FunctionTable") return this.compileFunctionTable(table);
|
|
1813
|
+
if (table.type === "DerivedTable") {
|
|
1814
|
+
throw new Error("Derived table compilation requires a compiler context.");
|
|
1815
|
+
}
|
|
1816
|
+
const base = this.compileTableName(table);
|
|
1817
|
+
return table.alias ? `${base} AS ${this.services.quoteIdentifier(table.alias)}` : base;
|
|
1818
|
+
}
|
|
1819
|
+
compileTableName(table) {
|
|
1820
|
+
if (table.schema) {
|
|
1821
|
+
return `${this.services.quoteIdentifier(table.schema)}.${this.services.quoteIdentifier(table.name)}`;
|
|
1822
|
+
}
|
|
1823
|
+
return this.services.quoteIdentifier(table.name);
|
|
1824
|
+
}
|
|
1825
|
+
compileTableReference(table) {
|
|
1826
|
+
const base = this.compileTableName(table);
|
|
1827
|
+
return table.alias ? `${base} AS ${this.services.quoteIdentifier(table.alias)}` : base;
|
|
1828
|
+
}
|
|
1829
|
+
stripTrailingSemicolon(sql) {
|
|
1830
|
+
return sql.trim().replace(/;$/, "");
|
|
1831
|
+
}
|
|
1832
|
+
wrapSetOperand(sql) {
|
|
1833
|
+
return `(${this.stripTrailingSemicolon(sql)})`;
|
|
1738
1834
|
}
|
|
1739
1835
|
};
|
|
1740
1836
|
|
|
@@ -1764,34 +1860,6 @@ var CteCompiler = class {
|
|
|
1764
1860
|
}
|
|
1765
1861
|
};
|
|
1766
1862
|
|
|
1767
|
-
// src/core/dialect/base/returning-strategy.ts
|
|
1768
|
-
var NoReturningStrategy = class {
|
|
1769
|
-
/**
|
|
1770
|
-
* Throws an error as RETURNING is not supported.
|
|
1771
|
-
* @param returning - Columns to return (causes error if non-empty).
|
|
1772
|
-
* @param _ctx - Compiler context (unused).
|
|
1773
|
-
* @throws Error indicating RETURNING is not supported.
|
|
1774
|
-
*/
|
|
1775
|
-
compileReturning(returning, _ctx) {
|
|
1776
|
-
void _ctx;
|
|
1777
|
-
if (!returning || returning.length === 0) return "";
|
|
1778
|
-
throw new Error("RETURNING is not supported by this dialect.");
|
|
1779
|
-
}
|
|
1780
|
-
/**
|
|
1781
|
-
* Formats column names for RETURNING clause.
|
|
1782
|
-
* @param returning - Columns to format.
|
|
1783
|
-
* @param quoteIdentifier - Function to quote identifiers according to dialect rules.
|
|
1784
|
-
* @returns Simple comma-separated column names.
|
|
1785
|
-
*/
|
|
1786
|
-
formatReturningColumns(returning, quoteIdentifier) {
|
|
1787
|
-
return returning.map((column) => {
|
|
1788
|
-
const tablePart = column.table ? `${quoteIdentifier(column.table)}.` : "";
|
|
1789
|
-
const aliasPart = column.alias ? ` AS ${quoteIdentifier(column.alias)}` : "";
|
|
1790
|
-
return `${tablePart}${quoteIdentifier(column.name)}${aliasPart}`;
|
|
1791
|
-
}).join(", ");
|
|
1792
|
-
}
|
|
1793
|
-
};
|
|
1794
|
-
|
|
1795
1863
|
// src/core/dialect/base/join-compiler.ts
|
|
1796
1864
|
var JoinCompiler = class {
|
|
1797
1865
|
static compileJoins(joins, ctx, compileFrom, compileExpression) {
|
|
@@ -1842,108 +1910,229 @@ var OrderByCompiler = class {
|
|
|
1842
1910
|
}
|
|
1843
1911
|
};
|
|
1844
1912
|
|
|
1845
|
-
// src/core/dialect/base/
|
|
1846
|
-
var
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1913
|
+
// src/core/dialect/base/standard-select-compiler.ts
|
|
1914
|
+
var StandardSelectCompiler = class {
|
|
1915
|
+
constructor(services, sources) {
|
|
1916
|
+
this.services = services;
|
|
1917
|
+
this.sources = sources;
|
|
1918
|
+
}
|
|
1919
|
+
compile(ast, ctx) {
|
|
1850
1920
|
const hasSetOps = !!(ast.setOps && ast.setOps.length);
|
|
1851
1921
|
const ctes = CteCompiler.compileCtes(
|
|
1852
1922
|
ast,
|
|
1853
1923
|
ctx,
|
|
1854
|
-
this.quoteIdentifier
|
|
1855
|
-
this.compileSelectAst
|
|
1856
|
-
this.normalizeSelectAst
|
|
1857
|
-
this.stripTrailingSemicolon
|
|
1924
|
+
(id) => this.services.quoteIdentifier(id),
|
|
1925
|
+
(query, compilerContext) => this.services.compileSelectAst(query, compilerContext),
|
|
1926
|
+
(query) => this.services.normalizeSelectAst(query),
|
|
1927
|
+
(sql) => this.sources.stripTrailingSemicolon(sql)
|
|
1858
1928
|
);
|
|
1859
1929
|
const baseAst = hasSetOps ? { ...ast, setOps: void 0, orderBy: void 0, limit: void 0, offset: void 0 } : ast;
|
|
1860
|
-
const baseSelect = this.
|
|
1930
|
+
const baseSelect = this.compileCore(baseAst, ctx);
|
|
1861
1931
|
if (!hasSetOps) return `${ctes}${baseSelect}`;
|
|
1862
|
-
|
|
1932
|
+
const compound = ast.setOps.map((op) => `${op.operator} ${this.sources.wrapSetOperand(this.services.compileSelectAst(op.query, ctx))}`).join(" ");
|
|
1933
|
+
const orderBy = this.compileOrderBy(ast, ctx);
|
|
1934
|
+
const pagination = this.services.getPaginationStrategy().compilePagination(ast.limit, ast.offset);
|
|
1935
|
+
const combined = `${this.sources.wrapSetOperand(baseSelect)} ${compound}`;
|
|
1936
|
+
return `${ctes}${combined}${orderBy}${pagination}`;
|
|
1863
1937
|
}
|
|
1864
|
-
|
|
1865
|
-
const
|
|
1866
|
-
const
|
|
1938
|
+
compileCore(ast, ctx) {
|
|
1939
|
+
const columns = this.compileColumns(ast, ctx);
|
|
1940
|
+
const from = this.sources.compileFrom(ast.from, ctx);
|
|
1941
|
+
const joins = JoinCompiler.compileJoins(
|
|
1942
|
+
ast.joins,
|
|
1943
|
+
ctx,
|
|
1944
|
+
(source, compilerContext) => this.sources.compileFrom(source, compilerContext),
|
|
1945
|
+
(expression, compilerContext) => this.services.compileExpression(expression, compilerContext)
|
|
1946
|
+
);
|
|
1947
|
+
const where = ast.where ? ` WHERE ${this.services.compileExpression(ast.where, ctx)}` : "";
|
|
1948
|
+
const groupBy = GroupByCompiler.compileGroupBy(
|
|
1867
1949
|
ast,
|
|
1868
|
-
(term) => this.compileOrderingTerm(term, ctx)
|
|
1869
|
-
this.renderOrderByNulls.bind(this),
|
|
1870
|
-
this.renderOrderByCollation.bind(this)
|
|
1950
|
+
(term) => this.services.compileOrderingTerm(term, ctx)
|
|
1871
1951
|
);
|
|
1872
|
-
const
|
|
1873
|
-
const
|
|
1874
|
-
|
|
1952
|
+
const having = ast.having ? ` HAVING ${this.services.compileExpression(ast.having, ctx)}` : "";
|
|
1953
|
+
const orderBy = this.compileOrderBy(ast, ctx);
|
|
1954
|
+
const pagination = this.services.getPaginationStrategy().compilePagination(ast.limit, ast.offset);
|
|
1955
|
+
return `SELECT ${ast.distinct ? "DISTINCT " : ""}${columns} FROM ${from}${joins}${where}${groupBy}${having}${orderBy}${pagination}`;
|
|
1875
1956
|
}
|
|
1876
|
-
|
|
1957
|
+
compileColumns(ast, ctx) {
|
|
1958
|
+
if (!ast.columns || ast.columns.length === 0) return "*";
|
|
1959
|
+
return ast.columns.map((column) => {
|
|
1960
|
+
const expr = this.services.compileOperand(column, ctx);
|
|
1961
|
+
if (!column.alias) return expr;
|
|
1962
|
+
if (column.alias.includes("(")) return column.alias;
|
|
1963
|
+
return `${expr} AS ${this.services.quoteIdentifier(column.alias)}`;
|
|
1964
|
+
}).join(", ");
|
|
1965
|
+
}
|
|
1966
|
+
compileOrderBy(ast, ctx) {
|
|
1967
|
+
return OrderByCompiler.compileOrderBy(
|
|
1968
|
+
ast,
|
|
1969
|
+
(term) => this.services.compileOrderingTerm(term, ctx),
|
|
1970
|
+
(order) => this.services.renderOrderByNulls(order),
|
|
1971
|
+
(order) => this.services.renderOrderByCollation(order)
|
|
1972
|
+
);
|
|
1973
|
+
}
|
|
1974
|
+
};
|
|
1975
|
+
|
|
1976
|
+
// src/core/dialect/base/standard-insert-compiler.ts
|
|
1977
|
+
var StandardInsertCompiler = class {
|
|
1978
|
+
constructor(services, sources) {
|
|
1979
|
+
this.services = services;
|
|
1980
|
+
this.sources = sources;
|
|
1981
|
+
}
|
|
1982
|
+
compile(ast, ctx) {
|
|
1877
1983
|
if (!ast.columns.length) {
|
|
1878
1984
|
throw new Error("INSERT queries must specify columns.");
|
|
1879
1985
|
}
|
|
1880
|
-
const table = this.compileTableName(ast.into);
|
|
1881
|
-
const columnList = this.
|
|
1882
|
-
const source = this.
|
|
1883
|
-
const upsert = this.compileUpsertClause(ast, ctx);
|
|
1884
|
-
const returning = this.compileReturning(ast.returning, ctx);
|
|
1986
|
+
const table = this.sources.compileTableName(ast.into);
|
|
1987
|
+
const columnList = this.compileColumnList(ast.columns);
|
|
1988
|
+
const source = this.compileSource(ast.source, ctx);
|
|
1989
|
+
const upsert = this.services.compileUpsertClause(ast, ctx);
|
|
1990
|
+
const returning = this.services.compileReturning(ast.returning, ctx);
|
|
1885
1991
|
return `INSERT INTO ${table} (${columnList}) ${source}${upsert}${returning}`;
|
|
1886
1992
|
}
|
|
1887
|
-
|
|
1888
|
-
void _ctx;
|
|
1889
|
-
if (!ast.onConflict) return "";
|
|
1890
|
-
throw new Error(`UPSERT/ON CONFLICT is not supported by dialect "${this.dialect}".`);
|
|
1891
|
-
}
|
|
1892
|
-
compileReturning(returning, ctx) {
|
|
1893
|
-
return this.returningStrategy.compileReturning(returning, ctx);
|
|
1894
|
-
}
|
|
1895
|
-
compileInsertSource(source, ctx) {
|
|
1993
|
+
compileSource(source, ctx) {
|
|
1896
1994
|
if (source.type === "InsertValues") {
|
|
1897
1995
|
if (!source.rows.length) {
|
|
1898
1996
|
throw new Error("INSERT ... VALUES requires at least one row.");
|
|
1899
1997
|
}
|
|
1900
|
-
const values = source.rows.map((row) => `(${row.map((value) => this.compileOperand(value, ctx)).join(", ")})`).join(", ");
|
|
1998
|
+
const values = source.rows.map((row) => `(${row.map((value) => this.services.compileOperand(value, ctx)).join(", ")})`).join(", ");
|
|
1901
1999
|
return `VALUES ${values}`;
|
|
1902
2000
|
}
|
|
1903
|
-
const normalized = this.normalizeSelectAst(source.query);
|
|
1904
|
-
return this.compileSelectAst(normalized, ctx).trim();
|
|
2001
|
+
const normalized = this.services.normalizeSelectAst(source.query);
|
|
2002
|
+
return this.services.compileSelectAst(normalized, ctx).trim();
|
|
1905
2003
|
}
|
|
1906
|
-
|
|
1907
|
-
return columns.map((column) => this.quoteIdentifier(column.name)).join(", ");
|
|
2004
|
+
compileColumnList(columns) {
|
|
2005
|
+
return columns.map((column) => this.services.quoteIdentifier(column.name)).join(", ");
|
|
1908
2006
|
}
|
|
1909
2007
|
ensureConflictColumns(clause, message) {
|
|
1910
2008
|
if (!clause.target.columns.length) throw new Error(message);
|
|
1911
2009
|
}
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
2010
|
+
};
|
|
2011
|
+
|
|
2012
|
+
// src/core/dialect/base/standard-update-compiler.ts
|
|
2013
|
+
var StandardUpdateCompiler = class {
|
|
2014
|
+
constructor(services, sources) {
|
|
2015
|
+
this.services = services;
|
|
2016
|
+
this.sources = sources;
|
|
2017
|
+
}
|
|
2018
|
+
compile(ast, ctx) {
|
|
2019
|
+
const target = this.sources.compileTableReference(ast.table);
|
|
2020
|
+
const assignments = this.compileAssignments(ast.set, ast.table, ctx);
|
|
2021
|
+
const from = this.compileFromClause(ast, ctx);
|
|
2022
|
+
const where = ast.where ? ` WHERE ${this.services.compileExpression(ast.where, ctx)}` : "";
|
|
2023
|
+
const returning = this.services.compileReturning(ast.returning, ctx);
|
|
2024
|
+
return `UPDATE ${target} SET ${assignments}${from}${where}${returning}`;
|
|
2025
|
+
}
|
|
2026
|
+
compileAssignments(assignments, table, ctx) {
|
|
2027
|
+
return assignments.map((assignment) => {
|
|
2028
|
+
const target = this.services.compileSetTarget(assignment.column, table);
|
|
2029
|
+
const value = this.services.compileOperand(assignment.value, ctx);
|
|
2030
|
+
return `${target} = ${value}`;
|
|
2031
|
+
}).join(", ");
|
|
2032
|
+
}
|
|
2033
|
+
compileFromClause(ast, ctx) {
|
|
2034
|
+
if (!ast.from && (!ast.joins || ast.joins.length === 0)) return "";
|
|
2035
|
+
if (!ast.from) {
|
|
2036
|
+
throw new Error("UPDATE with JOINs requires an explicit FROM clause.");
|
|
2037
|
+
}
|
|
2038
|
+
const from = this.sources.compileFrom(ast.from, ctx);
|
|
1915
2039
|
const joins = JoinCompiler.compileJoins(
|
|
1916
2040
|
ast.joins,
|
|
1917
2041
|
ctx,
|
|
1918
|
-
this.compileFrom
|
|
1919
|
-
this.compileExpression
|
|
2042
|
+
(source, compilerContext) => this.sources.compileFrom(source, compilerContext),
|
|
2043
|
+
(expression, compilerContext) => this.services.compileExpression(expression, compilerContext)
|
|
1920
2044
|
);
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
2045
|
+
return ` FROM ${from}${joins}`;
|
|
2046
|
+
}
|
|
2047
|
+
};
|
|
2048
|
+
|
|
2049
|
+
// src/core/dialect/base/standard-delete-compiler.ts
|
|
2050
|
+
var StandardDeleteCompiler = class {
|
|
2051
|
+
constructor(services, sources) {
|
|
2052
|
+
this.services = services;
|
|
2053
|
+
this.sources = sources;
|
|
2054
|
+
}
|
|
2055
|
+
compile(ast, ctx) {
|
|
2056
|
+
const target = this.sources.compileTableReference(ast.from);
|
|
2057
|
+
const using = this.compileUsingClause(ast, ctx);
|
|
2058
|
+
const where = ast.where ? ` WHERE ${this.services.compileExpression(ast.where, ctx)}` : "";
|
|
2059
|
+
const returning = this.services.compileReturning(ast.returning, ctx);
|
|
2060
|
+
return `DELETE FROM ${target}${using}${where}${returning}`;
|
|
2061
|
+
}
|
|
2062
|
+
compileUsingClause(ast, ctx) {
|
|
2063
|
+
if (!ast.using && (!ast.joins || ast.joins.length === 0)) return "";
|
|
2064
|
+
if (!ast.using) {
|
|
2065
|
+
throw new Error("DELETE with JOINs requires a USING clause.");
|
|
2066
|
+
}
|
|
2067
|
+
const usingTable = this.sources.compileFrom(ast.using, ctx);
|
|
2068
|
+
const joins = JoinCompiler.compileJoins(
|
|
2069
|
+
ast.joins,
|
|
2070
|
+
ctx,
|
|
2071
|
+
(source, compilerContext) => this.sources.compileFrom(source, compilerContext),
|
|
2072
|
+
(expression, compilerContext) => this.services.compileExpression(expression, compilerContext)
|
|
1929
2073
|
);
|
|
1930
|
-
|
|
1931
|
-
|
|
2074
|
+
return ` USING ${usingTable}${joins}`;
|
|
2075
|
+
}
|
|
2076
|
+
};
|
|
2077
|
+
|
|
2078
|
+
// src/core/dialect/base/sql-dialect.ts
|
|
2079
|
+
var SqlDialectBase = class extends DialectBase {
|
|
2080
|
+
paginationStrategy = new StandardLimitOffsetPagination();
|
|
2081
|
+
returningStrategy = new NoReturningStrategy();
|
|
2082
|
+
sourceCompiler;
|
|
2083
|
+
selectCompiler;
|
|
2084
|
+
insertCompiler;
|
|
2085
|
+
updateCompiler;
|
|
2086
|
+
deleteCompiler;
|
|
2087
|
+
constructor(functionStrategy, tableFunctionStrategy) {
|
|
2088
|
+
super(functionStrategy, tableFunctionStrategy);
|
|
2089
|
+
const services = {
|
|
2090
|
+
getDialectName: () => this.dialect,
|
|
2091
|
+
getPaginationStrategy: () => this.paginationStrategy,
|
|
2092
|
+
getTableFunctionStrategy: () => this.tableFunctionStrategy,
|
|
2093
|
+
quoteIdentifier: (id) => this.quoteIdentifier(id),
|
|
2094
|
+
compileOperand: (node, ctx) => this.compileOperand(node, ctx),
|
|
2095
|
+
compileExpression: (node, ctx) => this.compileExpression(node, ctx),
|
|
2096
|
+
compileOrderingTerm: (term, ctx) => this.compileOrderingTerm(term, ctx),
|
|
2097
|
+
normalizeSelectAst: (ast) => this.normalizeSelectAst(ast),
|
|
2098
|
+
compileSelectAst: (ast, ctx) => this.compileSelectAst(ast, ctx),
|
|
2099
|
+
compileReturning: (returning, ctx) => this.compileReturning(returning, ctx),
|
|
2100
|
+
compileUpsertClause: (ast, ctx) => this.compileUpsertClause(ast, ctx),
|
|
2101
|
+
compileSetTarget: (column, table) => this.compileSetTarget(column, table),
|
|
2102
|
+
renderOrderByNulls: (order) => this.renderOrderByNulls(order),
|
|
2103
|
+
renderOrderByCollation: (order) => this.renderOrderByCollation(order)
|
|
2104
|
+
};
|
|
2105
|
+
this.sourceCompiler = new StandardSqlSourceCompiler(services);
|
|
2106
|
+
this.selectCompiler = new StandardSelectCompiler(services, this.sourceCompiler);
|
|
2107
|
+
this.insertCompiler = new StandardInsertCompiler(services, this.sourceCompiler);
|
|
2108
|
+
this.updateCompiler = new StandardUpdateCompiler(services, this.sourceCompiler);
|
|
2109
|
+
this.deleteCompiler = new StandardDeleteCompiler(services, this.sourceCompiler);
|
|
2110
|
+
}
|
|
2111
|
+
compileSelectAst(ast, ctx) {
|
|
2112
|
+
return this.selectCompiler.compile(ast, ctx);
|
|
2113
|
+
}
|
|
2114
|
+
compileInsertAst(ast, ctx) {
|
|
2115
|
+
return this.insertCompiler.compile(ast, ctx);
|
|
1932
2116
|
}
|
|
1933
2117
|
compileUpdateAst(ast, ctx) {
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
2118
|
+
return this.updateCompiler.compile(ast, ctx);
|
|
2119
|
+
}
|
|
2120
|
+
compileDeleteAst(ast, ctx) {
|
|
2121
|
+
return this.deleteCompiler.compile(ast, ctx);
|
|
2122
|
+
}
|
|
2123
|
+
compileUpsertClause(ast, _ctx) {
|
|
2124
|
+
void _ctx;
|
|
2125
|
+
if (!ast.onConflict) return "";
|
|
2126
|
+
throw new Error(`UPSERT/ON CONFLICT is not supported by dialect "${this.dialect}".`);
|
|
2127
|
+
}
|
|
2128
|
+
compileReturning(returning, ctx) {
|
|
2129
|
+
return this.returningStrategy.compileReturning(returning, ctx);
|
|
2130
|
+
}
|
|
2131
|
+
ensureConflictColumns(clause, message) {
|
|
2132
|
+
this.insertCompiler.ensureConflictColumns(clause, message);
|
|
1940
2133
|
}
|
|
1941
2134
|
compileUpdateAssignments(assignments, table, ctx) {
|
|
1942
|
-
return
|
|
1943
|
-
const target = this.compileSetTarget(assignment.column, table);
|
|
1944
|
-
const value = this.compileOperand(assignment.value, ctx);
|
|
1945
|
-
return `${target} = ${value}`;
|
|
1946
|
-
}).join(", ");
|
|
2135
|
+
return this.updateCompiler.compileAssignments(assignments, table, ctx);
|
|
1947
2136
|
}
|
|
1948
2137
|
compileSetTarget(column, table) {
|
|
1949
2138
|
return this.compileQualifiedColumn(column, table);
|
|
@@ -1956,112 +2145,35 @@ var SqlDialectBase = class extends DialectBase {
|
|
|
1956
2145
|
if (!tableQualifier) return this.quoteIdentifier(column.name);
|
|
1957
2146
|
return `${this.quoteIdentifier(tableQualifier)}.${this.quoteIdentifier(column.name)}`;
|
|
1958
2147
|
}
|
|
1959
|
-
compileDeleteAst(ast, ctx) {
|
|
1960
|
-
const target = this.compileTableReference(ast.from);
|
|
1961
|
-
const usingClause = this.compileDeleteUsingClause(ast, ctx);
|
|
1962
|
-
const whereClause = this.compileWhere(ast.where, ctx);
|
|
1963
|
-
const returning = this.compileReturning(ast.returning, ctx);
|
|
1964
|
-
return `DELETE FROM ${target}${usingClause}${whereClause}${returning}`;
|
|
1965
|
-
}
|
|
1966
2148
|
formatReturningColumns(returning) {
|
|
1967
|
-
return this.returningStrategy.formatReturningColumns(
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
}
|
|
1972
|
-
compileSelectColumns(ast, ctx) {
|
|
1973
|
-
if (!ast.columns || ast.columns.length === 0) return "*";
|
|
1974
|
-
return ast.columns.map((column) => {
|
|
1975
|
-
const expr = this.compileOperand(column, ctx);
|
|
1976
|
-
if (column.alias) {
|
|
1977
|
-
if (column.alias.includes("(")) return column.alias;
|
|
1978
|
-
return `${expr} AS ${this.quoteIdentifier(column.alias)}`;
|
|
1979
|
-
}
|
|
1980
|
-
return expr;
|
|
1981
|
-
}).join(", ");
|
|
2149
|
+
return this.returningStrategy.formatReturningColumns(
|
|
2150
|
+
returning,
|
|
2151
|
+
(id) => this.quoteIdentifier(id)
|
|
2152
|
+
);
|
|
1982
2153
|
}
|
|
1983
|
-
compileFrom(
|
|
1984
|
-
|
|
1985
|
-
if (ast.type === "DerivedTable") return this.compileDerivedTable(ast, ctx);
|
|
1986
|
-
return this.compileTableSource(ast);
|
|
2154
|
+
compileFrom(source, ctx) {
|
|
2155
|
+
return this.sourceCompiler.compileFrom(source, ctx);
|
|
1987
2156
|
}
|
|
1988
2157
|
compileFunctionTable(fn9, ctx) {
|
|
1989
|
-
|
|
1990
|
-
if (ctx) {
|
|
1991
|
-
const renderer = this.tableFunctionStrategy.getRenderer(key);
|
|
1992
|
-
if (renderer) {
|
|
1993
|
-
const compiledArgs = (fn9.args ?? []).map((arg) => this.compileOperand(arg, ctx));
|
|
1994
|
-
return renderer({
|
|
1995
|
-
node: fn9,
|
|
1996
|
-
compiledArgs,
|
|
1997
|
-
compileOperand: (operand) => this.compileOperand(operand, ctx),
|
|
1998
|
-
quoteIdentifier: this.quoteIdentifier.bind(this)
|
|
1999
|
-
});
|
|
2000
|
-
}
|
|
2001
|
-
if (fn9.key) {
|
|
2002
|
-
throw new Error(`Table function "${key}" is not supported by dialect "${this.dialect}".`);
|
|
2003
|
-
}
|
|
2004
|
-
}
|
|
2005
|
-
return FunctionTableFormatter.format(fn9, ctx, {
|
|
2006
|
-
quoteIdentifier: (id) => this.quoteIdentifier(id),
|
|
2007
|
-
compileOperand: (node, compilerContext) => this.compileOperand(node, compilerContext)
|
|
2008
|
-
});
|
|
2158
|
+
return this.sourceCompiler.compileFunctionTable(fn9, ctx);
|
|
2009
2159
|
}
|
|
2010
2160
|
compileDerivedTable(table, ctx) {
|
|
2011
|
-
|
|
2012
|
-
const subquery = this.compileSelectAst(this.normalizeSelectAst(table.query), ctx).trim().replace(/;$/, "");
|
|
2013
|
-
const columns = table.columnAliases?.length ? ` (${table.columnAliases.map((c) => this.quoteIdentifier(c)).join(", ")})` : "";
|
|
2014
|
-
return `(${subquery}) AS ${this.quoteIdentifier(table.alias)}${columns}`;
|
|
2161
|
+
return this.sourceCompiler.compileDerivedTable(table, ctx);
|
|
2015
2162
|
}
|
|
2016
2163
|
compileTableSource(table) {
|
|
2017
|
-
|
|
2018
|
-
if (table.type === "DerivedTable") return this.compileDerivedTable(table);
|
|
2019
|
-
const base = this.compileTableName(table);
|
|
2020
|
-
return table.alias ? `${base} AS ${this.quoteIdentifier(table.alias)}` : base;
|
|
2164
|
+
return this.sourceCompiler.compileTableSource(table);
|
|
2021
2165
|
}
|
|
2022
2166
|
compileTableName(table) {
|
|
2023
|
-
|
|
2024
|
-
return `${this.quoteIdentifier(table.schema)}.${this.quoteIdentifier(table.name)}`;
|
|
2025
|
-
}
|
|
2026
|
-
return this.quoteIdentifier(table.name);
|
|
2167
|
+
return this.sourceCompiler.compileTableName(table);
|
|
2027
2168
|
}
|
|
2028
2169
|
compileTableReference(table) {
|
|
2029
|
-
|
|
2030
|
-
return table.alias ? `${base} AS ${this.quoteIdentifier(table.alias)}` : base;
|
|
2031
|
-
}
|
|
2032
|
-
compileUpdateFromClause(ast, ctx) {
|
|
2033
|
-
if (!ast.from && (!ast.joins || ast.joins.length === 0)) return "";
|
|
2034
|
-
if (!ast.from) throw new Error("UPDATE with JOINs requires an explicit FROM clause.");
|
|
2035
|
-
const from = this.compileFrom(ast.from, ctx);
|
|
2036
|
-
const joins = JoinCompiler.compileJoins(
|
|
2037
|
-
ast.joins,
|
|
2038
|
-
ctx,
|
|
2039
|
-
this.compileFrom.bind(this),
|
|
2040
|
-
this.compileExpression.bind(this)
|
|
2041
|
-
);
|
|
2042
|
-
return ` FROM ${from}${joins}`;
|
|
2043
|
-
}
|
|
2044
|
-
compileDeleteUsingClause(ast, ctx) {
|
|
2045
|
-
if (!ast.using && (!ast.joins || ast.joins.length === 0)) return "";
|
|
2046
|
-
if (!ast.using) throw new Error("DELETE with JOINs requires a USING clause.");
|
|
2047
|
-
const usingTable = this.compileFrom(ast.using, ctx);
|
|
2048
|
-
const joins = JoinCompiler.compileJoins(
|
|
2049
|
-
ast.joins,
|
|
2050
|
-
ctx,
|
|
2051
|
-
this.compileFrom.bind(this),
|
|
2052
|
-
this.compileExpression.bind(this)
|
|
2053
|
-
);
|
|
2054
|
-
return ` USING ${usingTable}${joins}`;
|
|
2055
|
-
}
|
|
2056
|
-
compileHaving(ast, ctx) {
|
|
2057
|
-
if (!ast.having) return "";
|
|
2058
|
-
return ` HAVING ${this.compileExpression(ast.having, ctx)}`;
|
|
2170
|
+
return this.sourceCompiler.compileTableReference(table);
|
|
2059
2171
|
}
|
|
2060
2172
|
stripTrailingSemicolon(sql) {
|
|
2061
|
-
return
|
|
2173
|
+
return this.sourceCompiler.stripTrailingSemicolon(sql);
|
|
2062
2174
|
}
|
|
2063
2175
|
wrapSetOperand(sql) {
|
|
2064
|
-
return
|
|
2176
|
+
return this.sourceCompiler.wrapSetOperand(sql);
|
|
2065
2177
|
}
|
|
2066
2178
|
renderOrderByNulls(order) {
|
|
2067
2179
|
return order.nulls ? ` NULLS ${order.nulls}` : "";
|
|
@@ -21675,6 +21787,11 @@ export {
|
|
|
21675
21787
|
SelectQueryBuilder,
|
|
21676
21788
|
SqlServerDialect,
|
|
21677
21789
|
SqliteDialect,
|
|
21790
|
+
StandardDeleteCompiler,
|
|
21791
|
+
StandardInsertCompiler,
|
|
21792
|
+
StandardSelectCompiler,
|
|
21793
|
+
StandardSqlSourceCompiler,
|
|
21794
|
+
StandardUpdateCompiler,
|
|
21678
21795
|
StringTypeStrategy,
|
|
21679
21796
|
TagIndex,
|
|
21680
21797
|
Title,
|