typesql-cli 0.23.0 → 0.25.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.
- package/codegen/mysql2.d.ts.map +1 -1
- package/codegen/mysql2.js +7 -4
- package/codegen/mysql2.js.map +1 -1
- package/codegen/shared/codegen-util.d.ts +1 -0
- package/codegen/shared/codegen-util.d.ts.map +1 -1
- package/codegen/shared/codegen-util.js +10 -0
- package/codegen/shared/codegen-util.js.map +1 -1
- package/codegen/sqlite.d.ts.map +1 -1
- package/codegen/sqlite.js +75 -92
- package/codegen/sqlite.js.map +1 -1
- package/drivers/postgres.d.ts +1 -0
- package/drivers/postgres.d.ts.map +1 -1
- package/drivers/postgres.js +17 -0
- package/drivers/postgres.js.map +1 -1
- package/mysql-query-analyzer/traverse.d.ts +3 -0
- package/mysql-query-analyzer/traverse.d.ts.map +1 -1
- package/mysql-query-analyzer/traverse.js.map +1 -1
- package/mysql-query-analyzer/types.d.ts +1 -0
- package/mysql-query-analyzer/types.d.ts.map +1 -1
- package/package.json +13 -9
- package/postgres-query-analyzer/describe.js +2 -2
- package/postgres-query-analyzer/describe.js.map +1 -1
- package/postgres-query-analyzer/parser.d.ts +2 -2
- package/postgres-query-analyzer/parser.d.ts.map +1 -1
- package/postgres-query-analyzer/parser.js +4 -4
- package/postgres-query-analyzer/parser.js.map +1 -1
- package/postgres-query-analyzer/traverse.d.ts +1 -1
- package/postgres-query-analyzer/traverse.d.ts.map +1 -1
- package/postgres-query-analyzer/traverse.js +179 -58
- package/postgres-query-analyzer/traverse.js.map +1 -1
- package/schema-info.d.ts +1 -0
- package/schema-info.d.ts.map +1 -1
- package/schema-info.js +5 -3
- package/schema-info.js.map +1 -1
- package/sqlite-query-analyzer/parser.js +23 -18
- package/sqlite-query-analyzer/parser.js.map +1 -1
- package/sqlite-query-analyzer/traverse.d.ts.map +1 -1
- package/sqlite-query-analyzer/traverse.js +31 -12
- package/sqlite-query-analyzer/traverse.js.map +1 -1
- package/ts-dynamic-query-descriptor.d.ts +0 -1
- package/ts-dynamic-query-descriptor.d.ts.map +1 -1
- package/ts-dynamic-query-descriptor.js +0 -12
- package/ts-dynamic-query-descriptor.js.map +1 -1
|
@@ -15,7 +15,7 @@ function defaultOptions() {
|
|
|
15
15
|
collectDynamicQueryInfo: false
|
|
16
16
|
};
|
|
17
17
|
}
|
|
18
|
-
function traverseSmt(stmt, dbSchema, checkConstraints, userFunctions, options) {
|
|
18
|
+
function traverseSmt(stmt, dbSchema, checkConstraints, userFunctions, options, functionStrictness = new Map()) {
|
|
19
19
|
const { collectNestedInfo = false, collectDynamicQueryInfo = false } = options;
|
|
20
20
|
const traverseResult = {
|
|
21
21
|
columnsNullability: [],
|
|
@@ -41,7 +41,8 @@ function traverseSmt(stmt, dbSchema, checkConstraints, userFunctions, options) {
|
|
|
41
41
|
parentColumns: [],
|
|
42
42
|
withColumns: [],
|
|
43
43
|
checkConstraints,
|
|
44
|
-
userFunctions
|
|
44
|
+
userFunctions,
|
|
45
|
+
functionStrictness
|
|
45
46
|
};
|
|
46
47
|
const selectstmt = stmt.selectstmt();
|
|
47
48
|
if (selectstmt) {
|
|
@@ -124,19 +125,34 @@ function traverse_selectstmt(selectstmt, context, traverseResult) {
|
|
|
124
125
|
singleRow: true
|
|
125
126
|
};
|
|
126
127
|
}
|
|
128
|
+
//traverses each CTE of the WITH clause and pushes its columns into context.withColumns
|
|
129
|
+
function traverse_with_clause(with_clause, context, traverseResult) {
|
|
130
|
+
with_clause.cte_list().common_table_expr_list()
|
|
131
|
+
.forEach(common_table_expr => {
|
|
132
|
+
var _a, _b, _c;
|
|
133
|
+
const recursiveTableName = with_clause.RECURSIVE() ? common_table_expr.name().getText() : undefined;
|
|
134
|
+
const recursiveColumnNames = (_c = (_b = (_a = common_table_expr.name_list_()) === null || _a === void 0 ? void 0 : _a.name_list()) === null || _b === void 0 ? void 0 : _b.name_list()) === null || _c === void 0 ? void 0 : _c.map(name => name.getText());
|
|
135
|
+
const withResult = traverse_common_table_expr(common_table_expr, Object.assign(Object.assign({}, context), { recursiveTableName, recursiveColumnNames }), traverseResult);
|
|
136
|
+
const columns = recursiveColumnNames ? withResult.map((col, index) => { var _a; return (Object.assign(Object.assign({}, col), { column_name: (_a = recursiveColumnNames[index]) !== null && _a !== void 0 ? _a : col.column_name })); }) : withResult;
|
|
137
|
+
context.withColumns.push(...columns);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
//A data-modifying statement (INSERT/UPDATE/DELETE) may carry its own leading WITH
|
|
141
|
+
//clause. Traverse it into a child scope and return a context whose withColumns include
|
|
142
|
+
//those CTEs; returns the context unchanged when there is no WITH clause.
|
|
143
|
+
function traverse_dml_with_clause(with_clause_, context, traverseResult) {
|
|
144
|
+
if (!with_clause_) {
|
|
145
|
+
return context;
|
|
146
|
+
}
|
|
147
|
+
const scopedContext = Object.assign(Object.assign({}, context), { withColumns: [...context.withColumns] });
|
|
148
|
+
traverse_with_clause(with_clause_.with_clause(), scopedContext, traverseResult);
|
|
149
|
+
return scopedContext;
|
|
150
|
+
}
|
|
127
151
|
function traverse_select_no_parens(select_no_parens, context, traverseResult) {
|
|
128
152
|
var _a, _b;
|
|
129
153
|
const with_clause = select_no_parens.with_clause();
|
|
130
154
|
if (with_clause) {
|
|
131
|
-
with_clause
|
|
132
|
-
.forEach(common_table_expr => {
|
|
133
|
-
var _a, _b, _c;
|
|
134
|
-
const recursiveTableName = with_clause.RECURSIVE() ? common_table_expr.name().getText() : undefined;
|
|
135
|
-
const recursiveColumnNames = (_c = (_b = (_a = common_table_expr.name_list_()) === null || _a === void 0 ? void 0 : _a.name_list()) === null || _b === void 0 ? void 0 : _b.name_list()) === null || _c === void 0 ? void 0 : _c.map(name => name.getText());
|
|
136
|
-
const withResult = traverse_common_table_expr(common_table_expr, Object.assign(Object.assign({}, context), { recursiveTableName, recursiveColumnNames }), traverseResult);
|
|
137
|
-
const columns = recursiveColumnNames ? withResult.map((col, index) => { var _a; return (Object.assign(Object.assign({}, col), { column_name: (_a = recursiveColumnNames[index]) !== null && _a !== void 0 ? _a : col.column_name })); }) : withResult;
|
|
138
|
-
context.withColumns.push(...columns);
|
|
139
|
-
});
|
|
155
|
+
traverse_with_clause(with_clause, context, traverseResult);
|
|
140
156
|
}
|
|
141
157
|
const select_clause = select_no_parens.select_clause();
|
|
142
158
|
const selectResult = traverse_select_clause(select_clause, context, traverseResult);
|
|
@@ -184,10 +200,30 @@ function traverse_select_no_parens(select_no_parens, context, traverseResult) {
|
|
|
184
200
|
function traverse_common_table_expr(common_table_expr, context, traverseResult) {
|
|
185
201
|
var _a;
|
|
186
202
|
const tableName = common_table_expr.name().getText();
|
|
187
|
-
const
|
|
203
|
+
const preparablestmt = common_table_expr.preparablestmt();
|
|
204
|
+
const cteContext = Object.assign(Object.assign({}, context), { collectDynamicQueryInfo: false });
|
|
188
205
|
const numParamsBefore = traverseResult.parameters.length;
|
|
189
|
-
|
|
190
|
-
|
|
206
|
+
// The CTE body may be a SELECT or a data-modifying statement (INSERT/UPDATE/DELETE
|
|
207
|
+
// with an optional RETURNING). Dispatch on which one the grammar node holds; a
|
|
208
|
+
// data-modifying body without RETURNING contributes no columns but still collects
|
|
209
|
+
// its parameters into the shared traverseResult.
|
|
210
|
+
let columns;
|
|
211
|
+
if (preparablestmt.selectstmt()) {
|
|
212
|
+
columns = traverse_selectstmt(preparablestmt.selectstmt(), cteContext, traverseResult).columns;
|
|
213
|
+
}
|
|
214
|
+
else if (preparablestmt.deletestmt()) {
|
|
215
|
+
columns = traverse_delete_body(preparablestmt.deletestmt(), cteContext, traverseResult);
|
|
216
|
+
}
|
|
217
|
+
else if (preparablestmt.insertstmt()) {
|
|
218
|
+
columns = traverse_insert_body(preparablestmt.insertstmt(), cteContext, traverseResult);
|
|
219
|
+
}
|
|
220
|
+
else if (preparablestmt.updatestmt()) {
|
|
221
|
+
columns = traverse_update_body(preparablestmt.updatestmt(), cteContext, traverseResult).columns;
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
throw Error('traverse_common_table_expr - CTE body not supported: ' + preparablestmt.getText());
|
|
225
|
+
}
|
|
226
|
+
const columnsWithTalbeName = columns.map(col => (Object.assign(Object.assign({}, col), { table: tableName })));
|
|
191
227
|
if (context.collectDynamicQueryInfo) {
|
|
192
228
|
const parameters = getParametersIndexes(traverseResult.parameters.slice(numParamsBefore));
|
|
193
229
|
(_a = traverseResult.dynamicQueryInfo) === null || _a === void 0 ? void 0 : _a.with.push({
|
|
@@ -1299,6 +1335,15 @@ function traversefunc_application(func_application, context, traverseResult) {
|
|
|
1299
1335
|
schema: ''
|
|
1300
1336
|
};
|
|
1301
1337
|
}
|
|
1338
|
+
if (context.functionStrictness.get(functionName)) {
|
|
1339
|
+
return {
|
|
1340
|
+
column_name: functionName,
|
|
1341
|
+
is_nullable: argsResult.length > 0 ? argsResult.some(col => col.is_nullable) : true,
|
|
1342
|
+
table: '',
|
|
1343
|
+
schema: '',
|
|
1344
|
+
type: 'unknown'
|
|
1345
|
+
};
|
|
1346
|
+
}
|
|
1302
1347
|
return {
|
|
1303
1348
|
column_name: functionName,
|
|
1304
1349
|
is_nullable: true,
|
|
@@ -1336,7 +1381,7 @@ function traversefunc_expr_common_subexpr(func_expr_common_subexpr, context, tra
|
|
|
1336
1381
|
is_nullable: result.is_nullable,
|
|
1337
1382
|
table: '',
|
|
1338
1383
|
schema: '',
|
|
1339
|
-
type: '
|
|
1384
|
+
type: 'numeric'
|
|
1340
1385
|
};
|
|
1341
1386
|
}
|
|
1342
1387
|
func_expr_common_subexpr.a_expr_list().forEach(a_expr => {
|
|
@@ -1768,6 +1813,7 @@ function parseArgumentList(argString) {
|
|
|
1768
1813
|
});
|
|
1769
1814
|
}
|
|
1770
1815
|
function traverse_func_expr_windowless(func_expr_windowless, context, traverseResult) {
|
|
1816
|
+
var _a;
|
|
1771
1817
|
const func_application = func_expr_windowless.func_application();
|
|
1772
1818
|
if (func_application) {
|
|
1773
1819
|
const func_name = (0, select_columns_1.splitName)(func_application.func_name().getText().toLowerCase());
|
|
@@ -1789,6 +1835,9 @@ function traverse_func_expr_windowless(func_expr_windowless, context, traverseRe
|
|
|
1789
1835
|
};
|
|
1790
1836
|
}
|
|
1791
1837
|
else {
|
|
1838
|
+
// Non-sql body isn't parsed, so collect the call-site args here as nullable params
|
|
1839
|
+
const func_arg_expr_list = ((_a = func_application.func_arg_list()) === null || _a === void 0 ? void 0 : _a.func_arg_expr_list()) || [];
|
|
1840
|
+
func_arg_expr_list.forEach(func_arg_expr => traversefunc_arg_expr(func_arg_expr, Object.assign(Object.assign({}, context), { propagatesNull: true }), traverseResult));
|
|
1792
1841
|
return {
|
|
1793
1842
|
columns: functionResult.columns.map((c) => (Object.assign(Object.assign({}, c), { table: funcSchema.function_name, schema: funcSchema.schema }))),
|
|
1794
1843
|
singleRow: false
|
|
@@ -1884,37 +1933,70 @@ function paramIsList(c_expr) {
|
|
|
1884
1933
|
const in_expr_list = expr_list === null || expr_list === void 0 ? void 0 : expr_list.parentCtx;
|
|
1885
1934
|
return in_expr_list instanceof PostgreSQLParser_1.In_expr_listContext;
|
|
1886
1935
|
}
|
|
1936
|
+
// Traverses an INSERT statement, collecting its parameters into the shared
|
|
1937
|
+
// `traverseResult` and resolving columns against the passed `context` (including
|
|
1938
|
+
// any accumulated CTE columns). Returns the RETURNING columns ([] when absent).
|
|
1939
|
+
// Reusable both at the top level and inside a data-modifying CTE.
|
|
1940
|
+
function traverse_insert_body(insertstmt, context, traverseResult) {
|
|
1941
|
+
var _a, _b, _c;
|
|
1942
|
+
const bodyContext = traverse_dml_with_clause(insertstmt.with_clause_(), context, traverseResult);
|
|
1943
|
+
const insert_target = insertstmt.insert_target();
|
|
1944
|
+
const tableName = (0, select_columns_1.splitName)(insert_target.qualified_name().getText());
|
|
1945
|
+
const tableAlias = ((_a = insert_target.colid()) === null || _a === void 0 ? void 0 : _a.getText()) || '';
|
|
1946
|
+
const insertColumns = filterSchemaColumns(bodyContext.dbSchema, tableName);
|
|
1947
|
+
//RETURNING / ON CONFLICT reference the target table, honoring its alias when present
|
|
1948
|
+
const aliasedInsertColumns = tableAlias
|
|
1949
|
+
? insertColumns.map(col => (Object.assign(Object.assign({}, col), { table: tableAlias })))
|
|
1950
|
+
: insertColumns;
|
|
1951
|
+
const insert_rest = insertstmt.insert_rest();
|
|
1952
|
+
//no explicit column list (VALUES / DEFAULT VALUES) defaults to all table columns
|
|
1953
|
+
const insert_column_list = insert_rest.insert_column_list();
|
|
1954
|
+
const insertColumnsList = insert_column_list
|
|
1955
|
+
? insert_column_list.insert_column_item_list().map(insert_column_item => traverse_insert_column_item(insert_column_item, insertColumns))
|
|
1956
|
+
: insertColumns;
|
|
1957
|
+
const insertContext = Object.assign(Object.assign({}, bodyContext), { fromColumns: aliasedInsertColumns, parentColumns: [], collectNestedInfo: false, collectDynamicQueryInfo: false });
|
|
1958
|
+
//null for DEFAULT VALUES (no source query to traverse)
|
|
1959
|
+
const selectstmt = insert_rest.selectstmt();
|
|
1960
|
+
if (selectstmt) {
|
|
1961
|
+
traverse_selectstmt(selectstmt, Object.assign(Object.assign({}, insertContext), { fromColumns: insertColumnsList }), traverseResult);
|
|
1962
|
+
}
|
|
1963
|
+
const on_conflict = insertstmt.on_conflict_();
|
|
1964
|
+
if (on_conflict) {
|
|
1965
|
+
//set_clause_list is absent for ON CONFLICT DO NOTHING
|
|
1966
|
+
const set_clause_list = ((_b = on_conflict.set_clause_list()) === null || _b === void 0 ? void 0 : _b.set_clause_list()) || [];
|
|
1967
|
+
set_clause_list.forEach(set_clause => traverse_set_clause(set_clause, insertContext, traverseResult));
|
|
1968
|
+
//the DO UPDATE ... WHERE predicate can reference the target table and EXCLUDED
|
|
1969
|
+
const conflict_where = (_c = on_conflict.where_clause()) === null || _c === void 0 ? void 0 : _c.a_expr();
|
|
1970
|
+
if (conflict_where) {
|
|
1971
|
+
const excludedColumns = insertContext.fromColumns.map(col => (Object.assign(Object.assign({}, col), { table: 'excluded' })));
|
|
1972
|
+
traverse_a_expr(conflict_where, Object.assign(Object.assign({}, insertContext), { fromColumns: insertContext.fromColumns.concat(excludedColumns) }), traverseResult);
|
|
1973
|
+
}
|
|
1974
|
+
}
|
|
1975
|
+
const returning_clause = insertstmt.returning_clause();
|
|
1976
|
+
const returninColumns = returning_clause ? traverse_target_list(returning_clause.target_list(), insertContext, traverseResult) : [];
|
|
1977
|
+
return returninColumns;
|
|
1978
|
+
}
|
|
1887
1979
|
function traverseInsertstmt(insertstmt, dbSchema) {
|
|
1888
1980
|
const traverseResult = {
|
|
1889
1981
|
columnsNullability: [],
|
|
1890
1982
|
parameters: []
|
|
1891
1983
|
};
|
|
1892
|
-
const insert_target = insertstmt.insert_target();
|
|
1893
|
-
const tableName = (0, select_columns_1.splitName)(insert_target.getText());
|
|
1894
|
-
const insertColumns = filterSchemaColumns(dbSchema, tableName);
|
|
1895
|
-
const insert_rest = insertstmt.insert_rest();
|
|
1896
|
-
const insertColumnsList = insert_rest.insert_column_list()
|
|
1897
|
-
.insert_column_item_list()
|
|
1898
|
-
.map(insert_column_item => traverse_insert_column_item(insert_column_item, insertColumns));
|
|
1899
1984
|
const context = {
|
|
1900
1985
|
dbSchema,
|
|
1901
|
-
fromColumns:
|
|
1986
|
+
fromColumns: [],
|
|
1902
1987
|
parentColumns: [],
|
|
1903
1988
|
withColumns: [],
|
|
1904
1989
|
checkConstraints: {},
|
|
1905
1990
|
userFunctions: [],
|
|
1991
|
+
functionStrictness: new Map(),
|
|
1906
1992
|
collectNestedInfo: false,
|
|
1907
1993
|
collectDynamicQueryInfo: false
|
|
1908
1994
|
};
|
|
1909
|
-
const
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
const set_clause_list = on_conflict.set_clause_list().set_clause_list() || [];
|
|
1914
|
-
set_clause_list.forEach(set_clause => traverse_set_clause(set_clause, context, traverseResult));
|
|
1915
|
-
}
|
|
1995
|
+
const returninColumns = traverse_insert_body(insertstmt, context, traverseResult);
|
|
1996
|
+
//INSERT ... SELECT collects FROM/WHERE parameters before target-list parameters;
|
|
1997
|
+
//sort by text position so parametersNullability aligns with textual placeholder order.
|
|
1998
|
+
traverseResult.parameters.sort((param1, param2) => param1.paramPos - param2.paramPos);
|
|
1916
1999
|
const returning_clause = insertstmt.returning_clause();
|
|
1917
|
-
const returninColumns = returning_clause ? traverse_target_list(returning_clause.target_list(), context, traverseResult) : [];
|
|
1918
2000
|
const paramIsListResult = getInParameterList(insertstmt);
|
|
1919
2001
|
const result = {
|
|
1920
2002
|
queryType: 'Insert',
|
|
@@ -1928,28 +2010,46 @@ function traverseInsertstmt(insertstmt, dbSchema) {
|
|
|
1928
2010
|
}
|
|
1929
2011
|
return result;
|
|
1930
2012
|
}
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
2013
|
+
// Traverses a DELETE statement, collecting its parameters into the shared
|
|
2014
|
+
// `traverseResult` and resolving columns against the passed `context` (including
|
|
2015
|
+
// any accumulated CTE columns, so `USING <cte>` resolves). Returns the RETURNING
|
|
2016
|
+
// columns ([] when absent). Reusable both at the top level and inside a CTE.
|
|
2017
|
+
function traverse_delete_body(deleteStmt, context, traverseResult) {
|
|
2018
|
+
var _a, _b;
|
|
2019
|
+
const bodyContext = traverse_dml_with_clause(deleteStmt.with_clause_(), context, traverseResult);
|
|
2020
|
+
const relation_expr_opt_alias = deleteStmt.relation_expr_opt_alias();
|
|
2021
|
+
const tableName = (0, select_columns_1.splitName)(relation_expr_opt_alias.relation_expr().getText());
|
|
2022
|
+
const tableAlias = ((_a = relation_expr_opt_alias.colid()) === null || _a === void 0 ? void 0 : _a.getText()) || '';
|
|
2023
|
+
const deleteColumns = filterSchemaColumns(bodyContext.dbSchema, tableName)
|
|
2024
|
+
.map(col => (Object.assign(Object.assign({}, col), { table: tableAlias || col.table })));
|
|
2025
|
+
const deleteContext = Object.assign(Object.assign({}, bodyContext), { fromColumns: deleteColumns, parentColumns: [], collectNestedInfo: false, collectDynamicQueryInfo: false });
|
|
2026
|
+
// USING <from_list> — resolves against dbSchema and accumulated CTE columns
|
|
2027
|
+
const using_clause = deleteStmt.using_clause();
|
|
2028
|
+
const usingResult = using_clause ? traverse_from_list(using_clause.from_list(), deleteContext, traverseResult) : { columns: [], singleRow: true };
|
|
2029
|
+
const scopedContext = Object.assign(Object.assign({}, deleteContext), { fromColumns: deleteColumns.concat(usingResult.columns) });
|
|
2030
|
+
const whereExpr = (_b = deleteStmt.where_or_current_clause()) === null || _b === void 0 ? void 0 : _b.a_expr();
|
|
2031
|
+
if (whereExpr) {
|
|
2032
|
+
traverse_a_expr(whereExpr, scopedContext, traverseResult);
|
|
2033
|
+
}
|
|
1937
2034
|
const returning_clause = deleteStmt.returning_clause();
|
|
2035
|
+
const returninColumns = returning_clause ? traverse_target_list(returning_clause.target_list(), scopedContext, traverseResult) : [];
|
|
2036
|
+
return returninColumns;
|
|
2037
|
+
}
|
|
2038
|
+
function traverseDeletestmt(deleteStmt, dbSchema, traverseResult) {
|
|
1938
2039
|
const context = {
|
|
1939
2040
|
dbSchema,
|
|
1940
|
-
fromColumns:
|
|
2041
|
+
fromColumns: [],
|
|
1941
2042
|
parentColumns: [],
|
|
1942
2043
|
withColumns: [],
|
|
1943
2044
|
checkConstraints: {},
|
|
1944
2045
|
userFunctions: [],
|
|
2046
|
+
functionStrictness: new Map(),
|
|
1945
2047
|
collectNestedInfo: false,
|
|
1946
2048
|
collectDynamicQueryInfo: false
|
|
1947
2049
|
};
|
|
1948
|
-
const
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
}
|
|
1952
|
-
const returninColumns = returning_clause ? traverse_target_list(returning_clause.target_list(), context, traverseResult) : [];
|
|
2050
|
+
const returninColumns = traverse_delete_body(deleteStmt, context, traverseResult);
|
|
2051
|
+
const returning_clause = deleteStmt.returning_clause();
|
|
2052
|
+
const paramIsListResult = getInParameterList(deleteStmt);
|
|
1953
2053
|
const result = {
|
|
1954
2054
|
queryType: 'Delete',
|
|
1955
2055
|
multipleRowsResult: false,
|
|
@@ -1969,32 +2069,53 @@ function filterSchemaColumns(dbSchema, tableName) {
|
|
|
1969
2069
|
return dbSchema.filter(col => col.table.toLowerCase() === tableName.name.toLowerCase()
|
|
1970
2070
|
&& (tableName.prefix === '' || col.schema.toLowerCase() === tableName.prefix.toLowerCase()));
|
|
1971
2071
|
}
|
|
1972
|
-
|
|
1973
|
-
|
|
2072
|
+
// Traverses an UPDATE statement, collecting its parameters into the shared
|
|
2073
|
+
// `traverseResult` and resolving columns against the passed `context` (including
|
|
2074
|
+
// any accumulated CTE columns). Returns the RETURNING columns ([] when absent) and
|
|
2075
|
+
// `whereParamStart` — the index in `traverseResult.parameters` where the WHERE-clause
|
|
2076
|
+
// parameters begin (SET/FROM parameters precede it). Reusable at the top level and
|
|
2077
|
+
// inside a data-modifying CTE.
|
|
2078
|
+
function traverse_update_body(updatestmt, context, traverseResult) {
|
|
2079
|
+
var _a, _b;
|
|
2080
|
+
const bodyContext = traverse_dml_with_clause(updatestmt.with_clause_(), context, traverseResult);
|
|
1974
2081
|
const relation_expr_opt_alias = updatestmt.relation_expr_opt_alias();
|
|
1975
2082
|
const tableName = (0, select_columns_1.splitName)(relation_expr_opt_alias.relation_expr().getText());
|
|
1976
2083
|
const tableAlias = ((_a = relation_expr_opt_alias.colid()) === null || _a === void 0 ? void 0 : _a.getText()) || '';
|
|
1977
|
-
const updateColumns = filterSchemaColumns(
|
|
2084
|
+
const updateColumns = filterSchemaColumns(bodyContext.dbSchema, tableName)
|
|
1978
2085
|
.map(col => (Object.assign(Object.assign({}, col), { table: tableAlias || col.table })));
|
|
1979
|
-
const
|
|
1980
|
-
|
|
1981
|
-
.forEach(set_clause => traverse_set_clause(set_clause, context, traverseResult));
|
|
2086
|
+
const updateContext = Object.assign(Object.assign({}, bodyContext), { fromColumns: updateColumns, collectNestedInfo: false, collectDynamicQueryInfo: false });
|
|
2087
|
+
//resolve FROM first so SET / WHERE / RETURNING can reference its columns (Postgres allows this)
|
|
1982
2088
|
const from_clause = updatestmt.from_clause();
|
|
1983
|
-
const fromResult = from_clause ? traverse_from_clause(from_clause,
|
|
1984
|
-
const
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
2089
|
+
const fromResult = from_clause ? traverse_from_clause(from_clause, updateContext, traverseResult) : { columns: [], singleRow: true };
|
|
2090
|
+
const scopedContext = Object.assign(Object.assign({}, updateContext), { fromColumns: updateColumns.concat(fromResult.columns) });
|
|
2091
|
+
updatestmt.set_clause_list().set_clause_list()
|
|
2092
|
+
.forEach(set_clause => traverse_set_clause(set_clause, scopedContext, traverseResult));
|
|
2093
|
+
//a_expr is null for WHERE CURRENT OF <cursor> (guarded like the DELETE path)
|
|
2094
|
+
const where_a_expr = (_b = updatestmt.where_or_current_clause()) === null || _b === void 0 ? void 0 : _b.a_expr();
|
|
2095
|
+
const whereParamPositions = new Set();
|
|
2096
|
+
if (where_a_expr) {
|
|
2097
|
+
const paramsBeforeWhere = traverseResult.parameters.length;
|
|
2098
|
+
traverse_a_expr(where_a_expr, scopedContext, traverseResult);
|
|
2099
|
+
traverseResult.parameters.slice(paramsBeforeWhere).forEach(param => whereParamPositions.add(param.paramPos));
|
|
1989
2100
|
}
|
|
1990
|
-
const whereParameters = traverseResult.parameters.slice(parametersBefore);
|
|
1991
2101
|
const returning_clause = updatestmt.returning_clause();
|
|
1992
|
-
const returninColumns = returning_clause ? traverse_target_list(returning_clause.target_list(),
|
|
2102
|
+
const returninColumns = returning_clause ? traverse_target_list(returning_clause.target_list(), scopedContext, traverseResult) : [];
|
|
2103
|
+
//FROM parameters are collected before SET parameters but appear later in the SQL text;
|
|
2104
|
+
//sort by text position so data parameters are reported in textual (placeholder) order.
|
|
2105
|
+
traverseResult.parameters.sort((param1, param2) => param1.paramPos - param2.paramPos);
|
|
2106
|
+
const firstWhereIndex = traverseResult.parameters.findIndex(param => whereParamPositions.has(param.paramPos));
|
|
2107
|
+
const whereParamStart = firstWhereIndex === -1 ? traverseResult.parameters.length : firstWhereIndex;
|
|
2108
|
+
return { columns: returninColumns, whereParamStart };
|
|
2109
|
+
}
|
|
2110
|
+
function traverseUpdatestmt(updatestmt, traverseContext, traverseResult) {
|
|
2111
|
+
const { columns: returninColumns, whereParamStart } = traverse_update_body(updatestmt, traverseContext, traverseResult);
|
|
2112
|
+
const whereParameters = traverseResult.parameters.slice(whereParamStart);
|
|
2113
|
+
const returning_clause = updatestmt.returning_clause();
|
|
1993
2114
|
const paramIsListResult = getInParameterList(updatestmt);
|
|
1994
2115
|
const result = {
|
|
1995
2116
|
queryType: 'Update',
|
|
1996
2117
|
multipleRowsResult: false,
|
|
1997
|
-
parametersNullability: traverseResult.parameters.slice(0,
|
|
2118
|
+
parametersNullability: traverseResult.parameters.slice(0, whereParamStart).map(param => (Object.assign({ isNotNull: param.isNotNull }, addConstraintIfNotNull(param.checkConstraint)))),
|
|
1998
2119
|
columns: returninColumns,
|
|
1999
2120
|
parameterList: paramIsListResult,
|
|
2000
2121
|
whereParamtersNullability: whereParameters.map(param => (Object.assign({ isNotNull: param.isNotNull }, addConstraintIfNotNull(param.checkConstraint))))
|