typesql-cli 0.9.5 → 0.10.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/README.md +2 -3
- package/cli.js +91 -82
- package/cli.js.map +1 -1
- package/code-generator.d.ts +7 -7
- package/code-generator.d.ts.map +1 -1
- package/code-generator.js +257 -193
- package/code-generator.js.map +1 -1
- package/describe-dynamic-query.d.ts +1 -1
- package/describe-dynamic-query.d.ts.map +1 -1
- package/describe-dynamic-query.js +51 -39
- package/describe-dynamic-query.js.map +1 -1
- package/describe-nested-query.d.ts +2 -2
- package/describe-nested-query.d.ts.map +1 -1
- package/describe-nested-query.js +43 -24
- package/describe-nested-query.js.map +1 -1
- package/describe-query.d.ts +4 -4
- package/describe-query.d.ts.map +1 -1
- package/describe-query.js +29 -21
- package/describe-query.js.map +1 -1
- package/drivers/libsql.d.ts +3 -3
- package/drivers/libsql.d.ts.map +1 -1
- package/drivers/libsql.js +5 -2
- package/drivers/libsql.js.map +1 -1
- package/mysql-mapping.d.ts +1 -1
- package/mysql-mapping.d.ts.map +1 -1
- package/mysql-mapping.js +9 -9
- package/mysql-mapping.js.map +1 -1
- package/mysql-query-analyzer/collect-constraints.d.ts +4 -4
- package/mysql-query-analyzer/collect-constraints.d.ts.map +1 -1
- package/mysql-query-analyzer/collect-constraints.js +21 -16
- package/mysql-query-analyzer/collect-constraints.js.map +1 -1
- package/mysql-query-analyzer/infer-column-nullability.d.ts +2 -2
- package/mysql-query-analyzer/infer-column-nullability.d.ts.map +1 -1
- package/mysql-query-analyzer/infer-column-nullability.js +81 -67
- package/mysql-query-analyzer/infer-column-nullability.js.map +1 -1
- package/mysql-query-analyzer/infer-param-nullability.d.ts +2 -2
- package/mysql-query-analyzer/infer-param-nullability.d.ts.map +1 -1
- package/mysql-query-analyzer/infer-param-nullability.js +11 -10
- package/mysql-query-analyzer/infer-param-nullability.js.map +1 -1
- package/mysql-query-analyzer/parse.d.ts +2 -2
- package/mysql-query-analyzer/parse.d.ts.map +1 -1
- package/mysql-query-analyzer/parse.js +39 -32
- package/mysql-query-analyzer/parse.js.map +1 -1
- package/mysql-query-analyzer/select-columns.d.ts +4 -4
- package/mysql-query-analyzer/select-columns.d.ts.map +1 -1
- package/mysql-query-analyzer/select-columns.js +77 -52
- package/mysql-query-analyzer/select-columns.js.map +1 -1
- package/mysql-query-analyzer/traverse.d.ts +4 -4
- package/mysql-query-analyzer/traverse.d.ts.map +1 -1
- package/mysql-query-analyzer/traverse.js +312 -206
- package/mysql-query-analyzer/traverse.js.map +1 -1
- package/mysql-query-analyzer/types.d.ts +5 -5
- package/mysql-query-analyzer/types.d.ts.map +1 -1
- package/mysql-query-analyzer/unify.d.ts +2 -2
- package/mysql-query-analyzer/unify.d.ts.map +1 -1
- package/mysql-query-analyzer/unify.js +533 -491
- package/mysql-query-analyzer/unify.js.map +1 -1
- package/mysql-query-analyzer/util.d.ts.map +1 -1
- package/mysql-query-analyzer/util.js +2 -3
- package/mysql-query-analyzer/util.js.map +1 -1
- package/mysql-query-analyzer/verify-multiple-result.d.ts +2 -2
- package/mysql-query-analyzer/verify-multiple-result.d.ts.map +1 -1
- package/mysql-query-analyzer/verify-multiple-result.js +12 -8
- package/mysql-query-analyzer/verify-multiple-result.js.map +1 -1
- package/package.json +46 -45
- package/queryExectutor.d.ts +4 -4
- package/queryExectutor.d.ts.map +1 -1
- package/queryExectutor.js +11 -13
- package/queryExectutor.js.map +1 -1
- package/sql-generator.d.ts +2 -2
- package/sql-generator.d.ts.map +1 -1
- package/sql-generator.js +29 -25
- package/sql-generator.js.map +1 -1
- package/sqlite-query-analyzer/code-generator.d.ts +5 -5
- package/sqlite-query-analyzer/code-generator.d.ts.map +1 -1
- package/sqlite-query-analyzer/code-generator.js +372 -221
- package/sqlite-query-analyzer/code-generator.js.map +1 -1
- package/sqlite-query-analyzer/parser.d.ts +4 -4
- package/sqlite-query-analyzer/parser.d.ts.map +1 -1
- package/sqlite-query-analyzer/parser.js +30 -17
- package/sqlite-query-analyzer/parser.js.map +1 -1
- package/sqlite-query-analyzer/query-executor.d.ts +17 -6
- package/sqlite-query-analyzer/query-executor.d.ts.map +1 -1
- package/sqlite-query-analyzer/query-executor.js +97 -60
- package/sqlite-query-analyzer/query-executor.js.map +1 -1
- package/sqlite-query-analyzer/replace-list-params.d.ts +1 -1
- package/sqlite-query-analyzer/replace-list-params.d.ts.map +1 -1
- package/sqlite-query-analyzer/replace-list-params.js +4 -3
- package/sqlite-query-analyzer/replace-list-params.js.map +1 -1
- package/sqlite-query-analyzer/sqlite-describe-nested-query.d.ts +4 -4
- package/sqlite-query-analyzer/sqlite-describe-nested-query.d.ts.map +1 -1
- package/sqlite-query-analyzer/sqlite-describe-nested-query.js +33 -18
- package/sqlite-query-analyzer/sqlite-describe-nested-query.js.map +1 -1
- package/sqlite-query-analyzer/traverse.d.ts +5 -5
- package/sqlite-query-analyzer/traverse.d.ts.map +1 -1
- package/sqlite-query-analyzer/traverse.js +255 -162
- package/sqlite-query-analyzer/traverse.js.map +1 -1
- package/ts-dynamic-query-descriptor.d.ts +1 -1
- package/ts-dynamic-query-descriptor.d.ts.map +1 -1
- package/ts-dynamic-query-descriptor.js +4 -4
- package/ts-dynamic-query-descriptor.js.map +1 -1
- package/ts-nested-descriptor.d.ts +4 -4
- package/ts-nested-descriptor.d.ts.map +1 -1
- package/ts-nested-descriptor.js +11 -11
- package/ts-nested-descriptor.js.map +1 -1
- package/types.d.ts +14 -14
- package/types.d.ts.map +1 -1
- package/util.d.ts.map +1 -1
- package/util.js.map +1 -1
- package/utility-types.d.ts.map +1 -1
@@ -27,7 +27,7 @@ function traverse_Sql_stmtContext(sql_stmt, traverseContext) {
|
|
27
27
|
const deleteResult = traverse_delete_stmt(delete_stmt, traverseContext);
|
28
28
|
return deleteResult;
|
29
29
|
}
|
30
|
-
throw Error(
|
30
|
+
throw Error('traverse_Sql_stmtContext');
|
31
31
|
}
|
32
32
|
function tryTraverse_Sql_stmtContext(sql_stmt, traverseContext) {
|
33
33
|
try {
|
@@ -48,9 +48,11 @@ function traverse_select_stmt(select_stmt, traverseContext, subQuery = false, re
|
|
48
48
|
if (common_table_stmt) {
|
49
49
|
const recursive = common_table_stmt.RECURSIVE_() != null;
|
50
50
|
const common_table_expression = common_table_stmt.common_table_expression_list();
|
51
|
-
common_table_expression.forEach(common_table_expression => {
|
51
|
+
common_table_expression.forEach((common_table_expression) => {
|
52
52
|
const table_name = common_table_expression.table_name();
|
53
|
-
const recursiveNames = common_table_expression
|
53
|
+
const recursiveNames = common_table_expression
|
54
|
+
.column_name_list()
|
55
|
+
.map((column_name) => column_name.getText());
|
54
56
|
const select_stmt = common_table_expression.select_stmt();
|
55
57
|
const select_stmt_result = traverse_select_stmt(select_stmt, Object.assign(Object.assign({}, traverseContext), { subQuery: true }), subQuery, recursive, recursiveNames);
|
56
58
|
select_stmt_result.columns.forEach((col, index) => {
|
@@ -72,8 +74,10 @@ function traverse_select_stmt(select_stmt, traverseContext, subQuery = false, re
|
|
72
74
|
}
|
73
75
|
const [mainSelect, ...unionSelect] = select_stmt.select_core_list();
|
74
76
|
const mainQueryResult = traverse_select_core(mainSelect, traverseContext, subQuery, recursive, recursiveNames);
|
75
|
-
const fromColumns = recursive
|
76
|
-
|
77
|
+
const fromColumns = recursive
|
78
|
+
? mainQueryResult.columns.map((col, index) => mapTypeAndNullInferToColumnDef(col, recursiveNames[index]))
|
79
|
+
: traverseContext.fromColumns;
|
80
|
+
unionSelect.forEach((select_core) => {
|
77
81
|
const unionResult = traverse_select_core(select_core, Object.assign(Object.assign({}, traverseContext), { fromColumns }), subQuery, recursive);
|
78
82
|
unionResult.columns.forEach((col, colIndex) => {
|
79
83
|
mainQueryResult.columns[colIndex].table = '';
|
@@ -99,9 +103,9 @@ function traverse_select_stmt(select_stmt, traverseContext, subQuery = false, re
|
|
99
103
|
if (order_by_stmt) {
|
100
104
|
const selectColumns = mainQueryResult.columns.map((col, index) => mapTypeAndNullInferToColumnDef(col, recursiveNames[index]));
|
101
105
|
const ordering_term_list = order_by_stmt.ordering_term_list();
|
102
|
-
ordering_term_list.forEach(ordering_term => {
|
106
|
+
ordering_term_list.forEach((ordering_term) => {
|
103
107
|
const expr = ordering_term.expr();
|
104
|
-
if (expr.getText()
|
108
|
+
if (expr.getText() === '?') {
|
105
109
|
hasOrderByParameter = true;
|
106
110
|
}
|
107
111
|
else {
|
@@ -129,7 +133,7 @@ function traverse_select_stmt(select_stmt, traverseContext, subQuery = false, re
|
|
129
133
|
type1: exrp1Type.type,
|
130
134
|
type2: (0, collect_constraints_1.freshVar)('INTEGER', 'INTEGER')
|
131
135
|
});
|
132
|
-
if (expr_list.length
|
136
|
+
if (expr_list.length === 2) {
|
133
137
|
const expr2 = expr_list[1];
|
134
138
|
const exrp2Type = traverse_expr(expr2, traverseContext);
|
135
139
|
exrp2Type.notNull = true;
|
@@ -173,12 +177,14 @@ function traverse_select_core(select_core, traverseContext, subQuery = false, re
|
|
173
177
|
columnsResult.push(...fields);
|
174
178
|
}
|
175
179
|
const result_column = select_core.result_column_list();
|
176
|
-
const fromColumns = subQuery || recursive
|
177
|
-
|
180
|
+
const fromColumns = subQuery || recursive
|
181
|
+
? traverseContext.fromColumns.concat(columnsResult)
|
182
|
+
: columnsResult;
|
183
|
+
result_column.forEach((result_column) => {
|
178
184
|
var _a, _b;
|
179
185
|
if (result_column.STAR()) {
|
180
186
|
const tableName = (_a = result_column.table_name()) === null || _a === void 0 ? void 0 : _a.getText();
|
181
|
-
columnsResult.forEach(col => {
|
187
|
+
columnsResult.forEach((col) => {
|
182
188
|
const table = col.tableAlias || col.table;
|
183
189
|
if (!tableName || (0, select_columns_1.includeColumn)(col, tableName)) {
|
184
190
|
listType.push({
|
@@ -203,11 +209,15 @@ function traverse_select_core(select_core, traverseContext, subQuery = false, re
|
|
203
209
|
if (expr) {
|
204
210
|
const exprType = traverse_expr(expr, Object.assign(Object.assign({}, traverseContext), { fromColumns: fromColumns }));
|
205
211
|
if (alias) {
|
206
|
-
traverseContext.relations
|
212
|
+
traverseContext.relations
|
213
|
+
.filter((relation) => relation.joinColumn === exprType.name &&
|
214
|
+
(relation.name === exprType.table ||
|
215
|
+
relation.alias === exprType.table))
|
216
|
+
.forEach((relation) => {
|
207
217
|
relation.joinColumn = alias;
|
208
218
|
});
|
209
219
|
}
|
210
|
-
if (exprType.type.kind
|
220
|
+
if (exprType.type.kind === 'TypeVar') {
|
211
221
|
if (alias) {
|
212
222
|
exprType.name = alias;
|
213
223
|
}
|
@@ -236,40 +246,46 @@ function traverse_select_core(select_core, traverseContext, subQuery = false, re
|
|
236
246
|
});
|
237
247
|
}
|
238
248
|
}
|
239
|
-
const newColumns = listType.map(selectField => {
|
249
|
+
const newColumns = listType.map((selectField) => {
|
240
250
|
const col = {
|
241
251
|
columnName: selectField.name,
|
242
252
|
table: selectField.table,
|
243
253
|
columnType: selectField.type,
|
244
254
|
notNull: selectField.notNull,
|
245
|
-
columnKey:
|
255
|
+
columnKey: ''
|
246
256
|
};
|
247
257
|
return col;
|
248
258
|
});
|
249
259
|
const groupByExprList = select_core._groupByExpr || [];
|
250
|
-
groupByExprList.forEach(groupByExpr => {
|
260
|
+
groupByExprList.forEach((groupByExpr) => {
|
251
261
|
traverse_expr(groupByExpr, Object.assign(Object.assign({}, traverseContext), { fromColumns: newColumns.concat(fromColumns) }));
|
252
262
|
});
|
253
263
|
const havingExpr = select_core._havingExpr;
|
254
264
|
if (havingExpr) {
|
255
|
-
//select have precedence: newColumns.concat(fromColumns)
|
265
|
+
//select have precedence: newColumns.concat(fromColumns)
|
256
266
|
traverse_expr(havingExpr, Object.assign(Object.assign({}, traverseContext), { fromColumns: newColumns.concat(fromColumns) }));
|
257
267
|
}
|
258
268
|
const querySpecification = {
|
259
|
-
columns: listType.map(col => (Object.assign(Object.assign({}, col), { notNull: col.notNull ||
|
269
|
+
columns: listType.map((col) => (Object.assign(Object.assign({}, col), { notNull: col.notNull ||
|
270
|
+
isNotNull(col.name, whereExpr) ||
|
271
|
+
isNotNull(col.name, havingExpr) }))),
|
260
272
|
fromColumns: columnsResult //TODO - return isMultipleRowResult instead
|
261
273
|
};
|
262
274
|
return querySpecification;
|
263
275
|
}
|
264
276
|
function extractRelationsAndParams(expr, fromColumns, parameters) {
|
265
277
|
const columnsRef = (0, select_columns_1.getExpressions)(expr, sqlite_1.Column_nameContext);
|
266
|
-
const relations = columnsRef
|
278
|
+
const relations = columnsRef
|
279
|
+
.filter((expr) => !expr.isSubQuery)
|
280
|
+
.map((colRef) => {
|
267
281
|
const fieldName = (0, select_columns_1.splitName)(colRef.expr.parentCtx.getText());
|
268
282
|
const column = (0, select_columns_1.findColumn)(fieldName, fromColumns);
|
269
283
|
return column.tableAlias || column.table;
|
270
284
|
});
|
271
285
|
const expressionList = (0, select_columns_1.getExpressions)(expr, sqlite_1.ExprContext);
|
272
|
-
const paramsIds = expressionList
|
286
|
+
const paramsIds = expressionList
|
287
|
+
.filter((expr) => expr.expr.BIND_PARAMETER() != null)
|
288
|
+
.map((expr) => expr.expr.BIND_PARAMETER().symbol.start);
|
273
289
|
const params = getParamsIndexes(parameters, paramsIds);
|
274
290
|
return {
|
275
291
|
relations,
|
@@ -279,30 +295,46 @@ function extractRelationsAndParams(expr, fromColumns, parameters) {
|
|
279
295
|
function traverse_table_or_subquery(table_or_subquery_list, join_constraint_list, join_operator_list, traverseContext) {
|
280
296
|
const allFields = [];
|
281
297
|
table_or_subquery_list.forEach((table_or_subquery, index) => {
|
282
|
-
var _a, _b, _c, _d;
|
298
|
+
var _a, _b, _c, _d, _e;
|
283
299
|
const numParamsBefore = traverseContext.parameters.length;
|
284
|
-
const isLeftJoin = index > 0 && join_operator_list
|
300
|
+
const isLeftJoin = index > 0 && join_operator_list
|
301
|
+
? ((_a = join_operator_list[index - 1]) === null || _a === void 0 ? void 0 : _a.LEFT_()) != null
|
302
|
+
: false;
|
285
303
|
const table_name = table_or_subquery.table_name();
|
286
304
|
const table_alias_temp = ((_b = table_or_subquery.table_alias()) === null || _b === void 0 ? void 0 : _b.getText()) || '';
|
287
305
|
let tableOrSubqueryFields = [];
|
288
306
|
//grammar error: select * from table1 inner join table2....; inner is parsed as table_alias
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
307
|
+
const table_alias = table_alias_temp.toLowerCase() === 'left' ||
|
308
|
+
table_alias_temp.toLowerCase() === 'right' ||
|
309
|
+
table_alias_temp.toLowerCase() === 'full' ||
|
310
|
+
table_alias_temp.toLowerCase() === 'outer' ||
|
311
|
+
table_alias_temp.toLowerCase() === 'inner' ||
|
312
|
+
table_alias_temp.toLowerCase() === 'cross'
|
313
|
+
? ''
|
314
|
+
: table_alias_temp;
|
315
|
+
const join_constraint = join_constraint_list && index > 0
|
316
|
+
? join_constraint_list[index - 1]
|
317
|
+
: undefined;
|
296
318
|
const asAlias = table_or_subquery.AS_() || false;
|
297
319
|
const tableAlias = (_c = table_or_subquery.table_alias()) === null || _c === void 0 ? void 0 : _c.getText();
|
298
|
-
const tableOrSubqueryName = table_name
|
320
|
+
const tableOrSubqueryName = table_name
|
321
|
+
? table_name.any_name().getText()
|
322
|
+
: '';
|
323
|
+
const schema = ((_d = table_or_subquery.schema_name()) === null || _d === void 0 ? void 0 : _d.getText()) || '';
|
299
324
|
if (table_name) {
|
300
|
-
const tableName =
|
325
|
+
const tableName = {
|
326
|
+
name: table_name.getText(),
|
327
|
+
prefix: schema
|
328
|
+
};
|
301
329
|
tableOrSubqueryFields = (0, select_columns_1.filterColumns)(traverseContext.dbSchema, traverseContext.withSchema, table_alias, tableName);
|
302
|
-
const usingFields = (join_constraint === null || join_constraint === void 0 ? void 0 : join_constraint.USING_())
|
303
|
-
|
330
|
+
const usingFields = (join_constraint === null || join_constraint === void 0 ? void 0 : join_constraint.USING_())
|
331
|
+
? join_constraint === null || join_constraint === void 0 ? void 0 : join_constraint.column_name_list().map((column_name) => column_name.getText())
|
332
|
+
: [];
|
333
|
+
const filteredFields = usingFields.length > 0
|
334
|
+
? filterUsingFields(tableOrSubqueryFields, usingFields)
|
335
|
+
: tableOrSubqueryFields;
|
304
336
|
if (isLeftJoin) {
|
305
|
-
allFields.push(...filteredFields.map(field => (Object.assign(Object.assign({}, field), { notNull: false }))));
|
337
|
+
allFields.push(...filteredFields.map((field) => (Object.assign(Object.assign({}, field), { notNull: false }))));
|
306
338
|
}
|
307
339
|
else {
|
308
340
|
allFields.push(...filteredFields);
|
@@ -311,12 +343,12 @@ function traverse_table_or_subquery(table_or_subquery_list, join_constraint_list
|
|
311
343
|
const select_stmt = table_or_subquery.select_stmt();
|
312
344
|
if (select_stmt) {
|
313
345
|
const subQueryResult = traverse_select_stmt(select_stmt, Object.assign(Object.assign({}, traverseContext), { subQuery: true }));
|
314
|
-
tableOrSubqueryFields = subQueryResult.columns.map(t => {
|
346
|
+
tableOrSubqueryFields = subQueryResult.columns.map((t) => {
|
315
347
|
const colDef = {
|
316
348
|
table: t.table ? tableAlias || '' : '',
|
317
349
|
columnName: t.name,
|
318
350
|
columnType: t.type,
|
319
|
-
columnKey:
|
351
|
+
columnKey: '',
|
320
352
|
notNull: t.notNull,
|
321
353
|
tableAlias: tableAlias
|
322
354
|
};
|
@@ -329,7 +361,7 @@ function traverse_table_or_subquery(table_or_subquery_list, join_constraint_list
|
|
329
361
|
tableOrSubqueryFields = traverse_table_or_subquery(table_or_subquery_list2, null, null, traverseContext);
|
330
362
|
allFields.push(...tableOrSubqueryFields);
|
331
363
|
}
|
332
|
-
const idColumn = (
|
364
|
+
const idColumn = (_e = tableOrSubqueryFields.find((field) => field.columnKey === 'PRI')) === null || _e === void 0 ? void 0 : _e.columnName;
|
333
365
|
const relation = {
|
334
366
|
name: asAlias ? table_alias : tableOrSubqueryName,
|
335
367
|
alias: table_alias,
|
@@ -338,23 +370,31 @@ function traverse_table_or_subquery(table_or_subquery_list, join_constraint_list
|
|
338
370
|
parentCardinality: 'one',
|
339
371
|
joinColumn: idColumn
|
340
372
|
};
|
341
|
-
if (join_constraint) {
|
373
|
+
if (join_constraint) {
|
374
|
+
//index 0 is the FROM (root relation)
|
342
375
|
const expr = join_constraint.expr(); //ON expr
|
343
376
|
if (expr) {
|
344
377
|
traverse_expr(expr, Object.assign(Object.assign({}, traverseContext), { fromColumns: allFields }));
|
345
378
|
const allJoinColumsn = getAllColumns(expr);
|
346
|
-
allJoinColumsn.forEach(joinColumn => {
|
347
|
-
const column = allFields.find(col => col.columnName
|
348
|
-
|
349
|
-
|
350
|
-
const
|
351
|
-
|
379
|
+
allJoinColumsn.forEach((joinColumn) => {
|
380
|
+
const column = allFields.find((col) => col.columnName === joinColumn.name &&
|
381
|
+
(col.tableAlias === joinColumn.prefix ||
|
382
|
+
col.table === joinColumn.prefix));
|
383
|
+
const filterUniqueKeys = allFields.filter((col) => (joinColumn.prefix === col.table ||
|
384
|
+
joinColumn.prefix === col.tableAlias) &&
|
385
|
+
col.columnKey === 'PRI');
|
386
|
+
const compositeKey = filterUniqueKeys.find((uni) => uni.columnName === column.columnName);
|
387
|
+
const notUnique = (filterUniqueKeys.length > 1 && compositeKey) ||
|
388
|
+
((column === null || column === void 0 ? void 0 : column.columnKey) !== 'UNI' && (column === null || column === void 0 ? void 0 : column.columnKey) !== 'PRI');
|
389
|
+
if (joinColumn.prefix !== relation.name &&
|
390
|
+
joinColumn.prefix !== relation.alias) {
|
352
391
|
relation.parentRelation = joinColumn.prefix;
|
353
392
|
if (notUnique) {
|
354
393
|
relation.parentCardinality = 'many';
|
355
394
|
}
|
356
395
|
}
|
357
|
-
if (joinColumn.prefix
|
396
|
+
if (joinColumn.prefix === relation.name ||
|
397
|
+
joinColumn.prefix === relation.alias) {
|
358
398
|
if (notUnique) {
|
359
399
|
relation.cardinality = 'many';
|
360
400
|
}
|
@@ -365,16 +405,18 @@ function traverse_table_or_subquery(table_or_subquery_list, join_constraint_list
|
|
365
405
|
if (!traverseContext.subQuery) {
|
366
406
|
traverseContext.relations.push(relation);
|
367
407
|
//dynamic query
|
368
|
-
const fragment =
|
369
|
-
|
370
|
-
|
371
|
-
const params = traverseContext.parameters
|
408
|
+
const fragment = `${join_operator_list != null && index > 0
|
409
|
+
? extractOriginalSql(join_operator_list[index - 1])
|
410
|
+
: 'FROM'} ${extractOriginalSql(table_or_subquery_list[index])}${join_constraint != null ? ` ${extractOriginalSql(join_constraint)}` : ''}`;
|
411
|
+
const params = traverseContext.parameters
|
412
|
+
.slice(numParamsBefore)
|
413
|
+
.map((_, index) => index + numParamsBefore);
|
372
414
|
traverseContext.dynamicSqlInfo2.from.push({
|
373
415
|
fragment: fragment,
|
374
416
|
relationName: relation.name,
|
375
417
|
relationAlias: relation.alias,
|
376
418
|
parentRelation: relation.parentRelation,
|
377
|
-
fields: tableOrSubqueryFields.map(field => field.columnName),
|
419
|
+
fields: tableOrSubqueryFields.map((field) => field.columnName),
|
378
420
|
parameters: params
|
379
421
|
});
|
380
422
|
}
|
@@ -384,11 +426,11 @@ function traverse_table_or_subquery(table_or_subquery_list, join_constraint_list
|
|
384
426
|
function traverse_expr(expr, traverseContext) {
|
385
427
|
var _a, _b;
|
386
428
|
const function_name = (_a = expr.function_name()) === null || _a === void 0 ? void 0 : _a.getText().toLowerCase();
|
387
|
-
if (function_name
|
429
|
+
if (function_name === 'avg') {
|
388
430
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'REAL');
|
389
431
|
const sumParamExpr = expr.expr(0);
|
390
432
|
const paramType = traverse_expr(sumParamExpr, traverseContext);
|
391
|
-
if (paramType.type.kind
|
433
|
+
if (paramType.type.kind === 'TypeVar') {
|
392
434
|
functionType.table = paramType.table;
|
393
435
|
}
|
394
436
|
return {
|
@@ -398,7 +440,7 @@ function traverse_expr(expr, traverseContext) {
|
|
398
440
|
table: functionType.table || ''
|
399
441
|
};
|
400
442
|
}
|
401
|
-
if (function_name
|
443
|
+
if (function_name === 'sum') {
|
402
444
|
const sumParamExpr = expr.expr(0);
|
403
445
|
const paramType = traverse_expr(sumParamExpr, traverseContext);
|
404
446
|
return {
|
@@ -408,7 +450,7 @@ function traverse_expr(expr, traverseContext) {
|
|
408
450
|
table: paramType.table || ''
|
409
451
|
};
|
410
452
|
}
|
411
|
-
if (function_name
|
453
|
+
if (function_name === 'min' || function_name === 'max') {
|
412
454
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
|
413
455
|
const sumParamExpr = expr.expr(0);
|
414
456
|
const paramType = traverse_expr(sumParamExpr, traverseContext);
|
@@ -424,12 +466,12 @@ function traverse_expr(expr, traverseContext) {
|
|
424
466
|
table: functionType.table || ''
|
425
467
|
};
|
426
468
|
}
|
427
|
-
if (function_name
|
469
|
+
if (function_name === 'count') {
|
428
470
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'INTEGER');
|
429
|
-
if (expr.expr_list().length
|
471
|
+
if (expr.expr_list().length === 1) {
|
430
472
|
const sumParamExpr = expr.expr(0);
|
431
473
|
const paramType = traverse_expr(sumParamExpr, traverseContext);
|
432
|
-
if (paramType.type.kind
|
474
|
+
if (paramType.type.kind === 'TypeVar') {
|
433
475
|
functionType.table = paramType.table;
|
434
476
|
}
|
435
477
|
}
|
@@ -440,16 +482,16 @@ function traverse_expr(expr, traverseContext) {
|
|
440
482
|
table: functionType.table || ''
|
441
483
|
};
|
442
484
|
}
|
443
|
-
if (function_name
|
485
|
+
if (function_name === 'concat') {
|
444
486
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'TEXT');
|
445
|
-
expr.expr_list().forEach(paramExpr => {
|
487
|
+
expr.expr_list().forEach((paramExpr) => {
|
446
488
|
const paramType = traverse_expr(paramExpr, traverseContext);
|
447
489
|
traverseContext.constraints.push({
|
448
490
|
expression: expr.getText(),
|
449
491
|
type1: functionType,
|
450
492
|
type2: paramType.type
|
451
493
|
});
|
452
|
-
if (paramType.type.kind
|
494
|
+
if (paramType.type.kind === 'TypeVar') {
|
453
495
|
functionType.table = paramType.table;
|
454
496
|
}
|
455
497
|
});
|
@@ -460,7 +502,7 @@ function traverse_expr(expr, traverseContext) {
|
|
460
502
|
table: functionType.table || ''
|
461
503
|
};
|
462
504
|
}
|
463
|
-
if (function_name
|
505
|
+
if (function_name === 'length') {
|
464
506
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'INTEGER');
|
465
507
|
const paramExpr = expr.expr(0);
|
466
508
|
const paramType = traverse_expr(paramExpr, traverseContext);
|
@@ -469,7 +511,7 @@ function traverse_expr(expr, traverseContext) {
|
|
469
511
|
type1: (0, collect_constraints_1.freshVar)(expr.getText(), '?'), //str or blob
|
470
512
|
type2: paramType.type
|
471
513
|
});
|
472
|
-
if (paramType.type.kind
|
514
|
+
if (paramType.type.kind === 'TypeVar') {
|
473
515
|
functionType.table = paramType.table;
|
474
516
|
}
|
475
517
|
return {
|
@@ -479,8 +521,8 @@ function traverse_expr(expr, traverseContext) {
|
|
479
521
|
table: functionType.table || ''
|
480
522
|
};
|
481
523
|
}
|
482
|
-
if (function_name
|
483
|
-
expr.expr_list().forEach(paramExpr => {
|
524
|
+
if (function_name === 'group_concat') {
|
525
|
+
expr.expr_list().forEach((paramExpr) => {
|
484
526
|
const param1Type = traverse_expr(paramExpr, traverseContext);
|
485
527
|
traverseContext.constraints.push({
|
486
528
|
expression: expr.getText(),
|
@@ -495,7 +537,16 @@ function traverse_expr(expr, traverseContext) {
|
|
495
537
|
table: ''
|
496
538
|
};
|
497
539
|
}
|
498
|
-
if (function_name
|
540
|
+
if (function_name === 'random') {
|
541
|
+
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'INTEGER');
|
542
|
+
return {
|
543
|
+
name: functionType.name,
|
544
|
+
type: functionType,
|
545
|
+
notNull: true,
|
546
|
+
table: ''
|
547
|
+
};
|
548
|
+
}
|
549
|
+
if (function_name === 'round') {
|
499
550
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'REAL');
|
500
551
|
const param1Expr = expr.expr(0);
|
501
552
|
const param1Type = traverse_expr(param1Expr, traverseContext);
|
@@ -520,9 +571,9 @@ function traverse_expr(expr, traverseContext) {
|
|
520
571
|
table: param1Type.table || ''
|
521
572
|
};
|
522
573
|
}
|
523
|
-
if (function_name
|
574
|
+
if (function_name === 'coalesce') {
|
524
575
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
|
525
|
-
const paramTypes = expr.expr_list().map(paramExpr => {
|
576
|
+
const paramTypes = expr.expr_list().map((paramExpr) => {
|
526
577
|
const paramType = traverse_expr(paramExpr, traverseContext);
|
527
578
|
traverseContext.constraints.push({
|
528
579
|
expression: expr.getText(),
|
@@ -534,11 +585,11 @@ function traverse_expr(expr, traverseContext) {
|
|
534
585
|
return {
|
535
586
|
name: functionType.name,
|
536
587
|
type: functionType,
|
537
|
-
notNull: paramTypes.some(param => param.notNull),
|
588
|
+
notNull: paramTypes.some((param) => param.notNull),
|
538
589
|
table: functionType.table || ''
|
539
590
|
};
|
540
591
|
}
|
541
|
-
if (function_name
|
592
|
+
if (function_name === 'strftime') {
|
542
593
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'TEXT');
|
543
594
|
const paramExpr = expr.expr(1);
|
544
595
|
const paramType = traverse_expr(paramExpr, traverseContext);
|
@@ -555,7 +606,9 @@ function traverse_expr(expr, traverseContext) {
|
|
555
606
|
table: functionType.table || ''
|
556
607
|
};
|
557
608
|
}
|
558
|
-
if (function_name
|
609
|
+
if (function_name === 'date' ||
|
610
|
+
function_name === 'time' ||
|
611
|
+
function_name === 'datetime') {
|
559
612
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'DATE');
|
560
613
|
const paramExpr = expr.expr(0);
|
561
614
|
const paramType = traverse_expr(paramExpr, traverseContext);
|
@@ -572,10 +625,10 @@ function traverse_expr(expr, traverseContext) {
|
|
572
625
|
table: functionType.table || ''
|
573
626
|
};
|
574
627
|
}
|
575
|
-
if (function_name
|
628
|
+
if (function_name === 'julianday') {
|
576
629
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'REAL');
|
577
630
|
const paramExpr = expr.expr(0);
|
578
|
-
const notNull = paramExpr.getText().toLowerCase()
|
631
|
+
const notNull = paramExpr.getText().toLowerCase() === `'now'`;
|
579
632
|
const paramType = traverse_expr(paramExpr, traverseContext);
|
580
633
|
traverseContext.constraints.push({
|
581
634
|
expression: paramExpr.getText(),
|
@@ -589,10 +642,10 @@ function traverse_expr(expr, traverseContext) {
|
|
589
642
|
table: functionType.table || ''
|
590
643
|
};
|
591
644
|
}
|
592
|
-
if (function_name
|
645
|
+
if (function_name === 'unixepoch') {
|
593
646
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'INTEGER');
|
594
647
|
const paramExpr = expr.expr(0);
|
595
|
-
const notNull = paramExpr.getText().toLowerCase()
|
648
|
+
const notNull = paramExpr.getText().toLowerCase() === `'now'`;
|
596
649
|
const paramType = traverse_expr(paramExpr, traverseContext);
|
597
650
|
traverseContext.constraints.push({
|
598
651
|
expression: paramExpr.getText(),
|
@@ -606,11 +659,11 @@ function traverse_expr(expr, traverseContext) {
|
|
606
659
|
table: functionType.table || ''
|
607
660
|
};
|
608
661
|
}
|
609
|
-
if (function_name
|
662
|
+
if (function_name === 'ifnull') {
|
610
663
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
|
611
|
-
const paramTypes = expr.expr_list().map(paramExpr => {
|
664
|
+
const paramTypes = expr.expr_list().map((paramExpr) => {
|
612
665
|
const paramType = traverse_expr(paramExpr, traverseContext);
|
613
|
-
if (paramType.name
|
666
|
+
if (paramType.name === '?') {
|
614
667
|
paramType.notNull = false;
|
615
668
|
}
|
616
669
|
traverseContext.constraints.push({
|
@@ -623,13 +676,13 @@ function traverse_expr(expr, traverseContext) {
|
|
623
676
|
return {
|
624
677
|
name: functionType.name,
|
625
678
|
type: functionType,
|
626
|
-
notNull: paramTypes.every(param => param.notNull),
|
679
|
+
notNull: paramTypes.every((param) => param.notNull),
|
627
680
|
table: functionType.table || ''
|
628
681
|
};
|
629
682
|
}
|
630
|
-
if (function_name
|
631
|
-
|
632
|
-
|
683
|
+
if (function_name === 'row_number' ||
|
684
|
+
function_name === 'rank' ||
|
685
|
+
function_name === 'dense_rank') {
|
633
686
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'INTEGER');
|
634
687
|
return {
|
635
688
|
name: functionType.name,
|
@@ -638,19 +691,17 @@ function traverse_expr(expr, traverseContext) {
|
|
638
691
|
table: functionType.table || ''
|
639
692
|
};
|
640
693
|
}
|
641
|
-
if (function_name
|
642
|
-
|| function_name == 'last_value') {
|
694
|
+
if (function_name === 'first_value' || function_name === 'last_value') {
|
643
695
|
const paramExpr = expr.expr(0);
|
644
696
|
const paramType = traverse_expr(paramExpr, traverseContext);
|
645
697
|
return paramType;
|
646
698
|
}
|
647
|
-
if (function_name
|
648
|
-
|| function_name == 'lag') {
|
699
|
+
if (function_name === 'lead' || function_name === 'lag') {
|
649
700
|
const paramExpr = expr.expr(0);
|
650
701
|
const paramType = traverse_expr(paramExpr, traverseContext);
|
651
702
|
return Object.assign(Object.assign({}, paramType), { notNull: false });
|
652
703
|
}
|
653
|
-
if (function_name
|
704
|
+
if (function_name === 'iif') {
|
654
705
|
const expr1 = expr.expr(0);
|
655
706
|
traverse_expr(expr1, traverseContext);
|
656
707
|
const expr2 = expr.expr(1);
|
@@ -664,7 +715,7 @@ function traverse_expr(expr, traverseContext) {
|
|
664
715
|
});
|
665
716
|
return Object.assign(Object.assign({}, expr2Type), { notNull: expr2Type.notNull && expr3Type.notNull });
|
666
717
|
}
|
667
|
-
if (function_name
|
718
|
+
if (function_name === 'vector') {
|
668
719
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'BLOB');
|
669
720
|
const param1Expr = expr.expr(0);
|
670
721
|
const param1Type = traverse_expr(param1Expr, traverseContext);
|
@@ -681,7 +732,7 @@ function traverse_expr(expr, traverseContext) {
|
|
681
732
|
table: ''
|
682
733
|
};
|
683
734
|
}
|
684
|
-
if (function_name
|
735
|
+
if (function_name === 'vector_extract') {
|
685
736
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'TEXT');
|
686
737
|
const param1Expr = expr.expr(0);
|
687
738
|
const param1Type = traverse_expr(param1Expr, traverseContext);
|
@@ -697,7 +748,7 @@ function traverse_expr(expr, traverseContext) {
|
|
697
748
|
table: ''
|
698
749
|
};
|
699
750
|
}
|
700
|
-
if (function_name
|
751
|
+
if (function_name === 'vector_distance_cos') {
|
701
752
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'REAL');
|
702
753
|
const param1Expr = expr.expr(0);
|
703
754
|
if (param1Expr) {
|
@@ -727,7 +778,7 @@ function traverse_expr(expr, traverseContext) {
|
|
727
778
|
};
|
728
779
|
}
|
729
780
|
if (function_name) {
|
730
|
-
throw Error(
|
781
|
+
throw Error(`traverse_expr: function not supported:${function_name}`);
|
731
782
|
}
|
732
783
|
const column_name = expr.column_name();
|
733
784
|
const table_name = expr.table_name();
|
@@ -764,7 +815,7 @@ function traverse_expr(expr, traverseContext) {
|
|
764
815
|
return {
|
765
816
|
name: type.name,
|
766
817
|
type: type,
|
767
|
-
notNull: literal.NULL_()
|
818
|
+
notNull: literal.NULL_() == null,
|
768
819
|
table: type.table || ''
|
769
820
|
};
|
770
821
|
}
|
@@ -817,8 +868,8 @@ function traverse_expr(expr, traverseContext) {
|
|
817
868
|
type1: returnType,
|
818
869
|
type2: typeLeft.type
|
819
870
|
});
|
820
|
-
const isDateFunctionContext =
|
821
|
-
|
871
|
+
const isDateFunctionContext = expr.parentCtx instanceof sqlite_1.ExprContext &&
|
872
|
+
((_b = expr.parentCtx.function_name()) === null || _b === void 0 ? void 0 : _b.getText().toLowerCase()) === 'date';
|
822
873
|
if (!isDateFunctionContext) {
|
823
874
|
traverseContext.constraints.push({
|
824
875
|
expression: exprRight.getText(),
|
@@ -828,15 +879,24 @@ function traverse_expr(expr, traverseContext) {
|
|
828
879
|
}
|
829
880
|
return Object.assign(Object.assign({}, typeLeft), { notNull: typeLeft.notNull && typeRight.notNull });
|
830
881
|
}
|
831
|
-
if (expr.LT2() ||
|
882
|
+
if (expr.LT2() ||
|
883
|
+
expr.GT2() ||
|
884
|
+
expr.AMP() ||
|
885
|
+
expr.PIPE() ||
|
886
|
+
expr.LT() ||
|
887
|
+
expr.LT_EQ() ||
|
888
|
+
expr.GT() ||
|
889
|
+
expr.GT_EQ() ||
|
890
|
+
expr.NOT_EQ1() ||
|
891
|
+
expr.NOT_EQ2()) {
|
832
892
|
const exprLeft = expr.expr(0);
|
833
893
|
const exprRight = expr.expr(1);
|
834
894
|
const typeLeft = traverse_expr(exprLeft, traverseContext);
|
835
895
|
const typeRight = traverse_expr(exprRight, traverseContext);
|
836
|
-
if (typeLeft.name
|
896
|
+
if (typeLeft.name === '?') {
|
837
897
|
typeLeft.notNull = true;
|
838
898
|
}
|
839
|
-
if (typeRight.name
|
899
|
+
if (typeRight.name === '?') {
|
840
900
|
typeRight.notNull = true;
|
841
901
|
}
|
842
902
|
traverseContext.constraints.push({
|
@@ -852,7 +912,8 @@ function traverse_expr(expr, traverseContext) {
|
|
852
912
|
table: resultType.table || ''
|
853
913
|
};
|
854
914
|
}
|
855
|
-
if (expr.IS_()) {
|
915
|
+
if (expr.IS_()) {
|
916
|
+
//is null/is not null
|
856
917
|
const expr_ = expr.expr(0);
|
857
918
|
traverse_expr(expr_, traverseContext);
|
858
919
|
const type = (0, collect_constraints_1.freshVar)(expr.getText(), 'INTEGER');
|
@@ -863,15 +924,16 @@ function traverse_expr(expr, traverseContext) {
|
|
863
924
|
table: type.table || ''
|
864
925
|
};
|
865
926
|
}
|
866
|
-
if (expr.ASSIGN()) {
|
927
|
+
if (expr.ASSIGN()) {
|
928
|
+
//=
|
867
929
|
const exprLeft = expr.expr(0);
|
868
930
|
const exprRight = expr.expr(1);
|
869
931
|
const typeLeft = traverse_expr(exprLeft, traverseContext);
|
870
932
|
const typeRight = traverse_expr(exprRight, traverseContext);
|
871
|
-
if (typeLeft.name
|
933
|
+
if (typeLeft.name === '?') {
|
872
934
|
typeLeft.notNull = true;
|
873
935
|
}
|
874
|
-
if (typeRight.name
|
936
|
+
if (typeRight.name === '?') {
|
875
937
|
typeRight.notNull = true;
|
876
938
|
}
|
877
939
|
traverseContext.constraints.push({
|
@@ -891,10 +953,10 @@ function traverse_expr(expr, traverseContext) {
|
|
891
953
|
const exprType = traverse_expr(expr.expr(0), traverseContext);
|
892
954
|
const between1 = traverse_expr(expr.expr(1), traverseContext);
|
893
955
|
const between2 = traverse_expr(expr.expr(2), traverseContext);
|
894
|
-
if (between1.name
|
956
|
+
if (between1.name === '?') {
|
895
957
|
between1.notNull = true;
|
896
958
|
}
|
897
|
-
if (between2.name
|
959
|
+
if (between2.name === '?') {
|
898
960
|
between2.notNull = true;
|
899
961
|
}
|
900
962
|
traverseContext.constraints.push({
|
@@ -924,7 +986,7 @@ function traverse_expr(expr, traverseContext) {
|
|
924
986
|
const exprList = expr.expr_list();
|
925
987
|
const inExprLeft = exprList[0];
|
926
988
|
const typeLeft = traverse_expr(inExprLeft, traverseContext);
|
927
|
-
if (typeLeft.name
|
989
|
+
if (typeLeft.name === '?') {
|
928
990
|
typeLeft.notNull = true;
|
929
991
|
}
|
930
992
|
if (expr.NOT_()) {
|
@@ -940,9 +1002,9 @@ function traverse_expr(expr, traverseContext) {
|
|
940
1002
|
});
|
941
1003
|
}
|
942
1004
|
//NOT IN (1, 2, 3)
|
943
|
-
exprList.slice(1).forEach(inExpr => {
|
1005
|
+
exprList.slice(1).forEach((inExpr) => {
|
944
1006
|
const typeRight = traverse_expr(inExpr, traverseContext);
|
945
|
-
if (typeRight.name
|
1007
|
+
if (typeRight.name === '?') {
|
946
1008
|
typeRight.notNull = true;
|
947
1009
|
}
|
948
1010
|
traverseContext.constraints.push({
|
@@ -966,9 +1028,9 @@ function traverse_expr(expr, traverseContext) {
|
|
966
1028
|
});
|
967
1029
|
}
|
968
1030
|
//IN (1, 2, 3)
|
969
|
-
rightExpr.expr_list().forEach(inExpr2 => {
|
1031
|
+
rightExpr.expr_list().forEach((inExpr2) => {
|
970
1032
|
const typeRight = traverse_expr(inExpr2, traverseContext);
|
971
|
-
if (typeRight.name
|
1033
|
+
if (typeRight.name === '?') {
|
972
1034
|
typeRight.notNull = true;
|
973
1035
|
}
|
974
1036
|
traverseContext.constraints.push({
|
@@ -991,10 +1053,10 @@ function traverse_expr(expr, traverseContext) {
|
|
991
1053
|
const exprRight = expr.expr(1);
|
992
1054
|
const typeLeft = traverse_expr(exprLeft, traverseContext);
|
993
1055
|
const typeRight = traverse_expr(exprRight, traverseContext);
|
994
|
-
if (typeLeft.name
|
1056
|
+
if (typeLeft.name === '?') {
|
995
1057
|
typeLeft.notNull = true;
|
996
1058
|
}
|
997
|
-
if (typeRight.name
|
1059
|
+
if (typeRight.name === '?') {
|
998
1060
|
typeRight.notNull = true;
|
999
1061
|
}
|
1000
1062
|
traverseContext.constraints.push({
|
@@ -1028,7 +1090,7 @@ function traverse_expr(expr, traverseContext) {
|
|
1028
1090
|
}
|
1029
1091
|
if (expr.OPEN_PAR() && expr.CLOSE_PAR()) {
|
1030
1092
|
const type = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
|
1031
|
-
const exprTypes = expr.expr_list().map(innerExpr => {
|
1093
|
+
const exprTypes = expr.expr_list().map((innerExpr) => {
|
1032
1094
|
const exprType = traverse_expr(innerExpr, traverseContext);
|
1033
1095
|
traverseContext.constraints.push({
|
1034
1096
|
expression: innerExpr.getText(),
|
@@ -1040,7 +1102,7 @@ function traverse_expr(expr, traverseContext) {
|
|
1040
1102
|
return {
|
1041
1103
|
name: type.name,
|
1042
1104
|
type: type,
|
1043
|
-
notNull: exprTypes.every(type => type.notNull),
|
1105
|
+
notNull: exprTypes.every((type) => type.notNull),
|
1044
1106
|
table: type.table || ''
|
1045
1107
|
};
|
1046
1108
|
}
|
@@ -1049,7 +1111,8 @@ function traverse_expr(expr, traverseContext) {
|
|
1049
1111
|
const whenTypes = [];
|
1050
1112
|
expr.expr_list().forEach((expr_, index) => {
|
1051
1113
|
const type = traverse_expr(expr_, traverseContext);
|
1052
|
-
if (index % 2
|
1114
|
+
if (index % 2 === 0 &&
|
1115
|
+
(!expr.ELSE_() || index < expr.expr_list().length - 1)) {
|
1053
1116
|
whenTypes.push(type);
|
1054
1117
|
}
|
1055
1118
|
else {
|
@@ -1076,11 +1139,11 @@ function traverse_expr(expr, traverseContext) {
|
|
1076
1139
|
return {
|
1077
1140
|
name: extractOriginalSql(expr),
|
1078
1141
|
type: type.type,
|
1079
|
-
notNull: expr.ELSE_() ? resultTypes.every(type => type.notNull) : false,
|
1142
|
+
notNull: expr.ELSE_() ? resultTypes.every((type) => type.notNull) : false,
|
1080
1143
|
table: type.table || ''
|
1081
1144
|
};
|
1082
1145
|
}
|
1083
|
-
throw Error(
|
1146
|
+
throw Error(`traverse_expr not supported:${expr.getText()}`);
|
1084
1147
|
}
|
1085
1148
|
function extractOriginalSql(rule) {
|
1086
1149
|
var _a, _b;
|
@@ -1090,7 +1153,10 @@ function extractOriginalSql(rule) {
|
|
1090
1153
|
return result;
|
1091
1154
|
}
|
1092
1155
|
function traverse_column_name(column_name, table_name, traverseContext) {
|
1093
|
-
const fieldName = {
|
1156
|
+
const fieldName = {
|
1157
|
+
name: column_name.getText(),
|
1158
|
+
prefix: (table_name === null || table_name === void 0 ? void 0 : table_name.getText()) || ''
|
1159
|
+
};
|
1094
1160
|
const column = (0, select_columns_1.findColumn)(fieldName, traverseContext.fromColumns);
|
1095
1161
|
// const typeVar = freshVar(column.columnName, column.columnType.type, column.tableAlias || column.table);
|
1096
1162
|
return column;
|
@@ -1100,18 +1166,20 @@ function isNotNull(columnName, where) {
|
|
1100
1166
|
return false;
|
1101
1167
|
}
|
1102
1168
|
if (where.AND_()) {
|
1103
|
-
const ifNullList = where
|
1104
|
-
|
1169
|
+
const ifNullList = where
|
1170
|
+
.expr_list()
|
1171
|
+
.map((expr) => isNotNull(columnName, expr));
|
1172
|
+
const result = ifNullList.some((v) => v);
|
1105
1173
|
return result;
|
1106
1174
|
}
|
1107
|
-
|
1108
|
-
const possibleNullList = where
|
1109
|
-
|
1175
|
+
if (where.OR_()) {
|
1176
|
+
const possibleNullList = where
|
1177
|
+
.expr_list()
|
1178
|
+
.map((expr) => isNotNull(columnName, expr));
|
1179
|
+
const result = possibleNullList.every((v) => v);
|
1110
1180
|
return result;
|
1111
1181
|
}
|
1112
|
-
|
1113
|
-
return isNotNullExpr(columnName, where);
|
1114
|
-
}
|
1182
|
+
return isNotNullExpr(columnName, where);
|
1115
1183
|
}
|
1116
1184
|
exports.isNotNull = isNotNull;
|
1117
1185
|
function isNotNullExpr(columnName, expr) {
|
@@ -1119,10 +1187,14 @@ function isNotNullExpr(columnName, expr) {
|
|
1119
1187
|
const innerExpr = expr.expr(0);
|
1120
1188
|
return isNotNull(columnName, innerExpr);
|
1121
1189
|
}
|
1122
|
-
if (expr.ASSIGN()
|
1123
|
-
|
1124
|
-
|
1125
|
-
|
1190
|
+
if (expr.ASSIGN() ||
|
1191
|
+
expr.GT() ||
|
1192
|
+
expr.GT_EQ() ||
|
1193
|
+
expr.LT() ||
|
1194
|
+
expr.LT_EQ() ||
|
1195
|
+
(expr.IS_() &&
|
1196
|
+
expr.expr_list().length === 2 &&
|
1197
|
+
expr.expr(1).getText() === 'notnull')) {
|
1126
1198
|
const exprLeft = expr.expr(0);
|
1127
1199
|
const exprRight = expr.expr(1);
|
1128
1200
|
const column_name_left = exprLeft.column_name();
|
@@ -1130,7 +1202,7 @@ function isNotNullExpr(columnName, expr) {
|
|
1130
1202
|
if (column_name_left || column_name_right) {
|
1131
1203
|
const columnLeft = column_name_left === null || column_name_left === void 0 ? void 0 : column_name_left.getText();
|
1132
1204
|
const columnRight = column_name_right === null || column_name_right === void 0 ? void 0 : column_name_right.getText();
|
1133
|
-
if (columnLeft
|
1205
|
+
if (columnLeft === columnName || columnRight === columnName) {
|
1134
1206
|
return true;
|
1135
1207
|
}
|
1136
1208
|
}
|
@@ -1138,23 +1210,30 @@ function isNotNullExpr(columnName, expr) {
|
|
1138
1210
|
return false;
|
1139
1211
|
}
|
1140
1212
|
function isMultipleRowResult(select_stmt, fromColumns) {
|
1141
|
-
if (select_stmt.select_core_list().length
|
1213
|
+
if (select_stmt.select_core_list().length === 1) {
|
1214
|
+
//UNION queries are multipleRowsResult = true
|
1142
1215
|
const select_core = select_stmt.select_core(0);
|
1143
1216
|
const from = select_core.FROM_();
|
1144
1217
|
if (!from) {
|
1145
1218
|
return false;
|
1146
1219
|
}
|
1147
|
-
const groupBy = select_stmt
|
1220
|
+
const groupBy = select_stmt
|
1221
|
+
.select_core_list()
|
1222
|
+
.some((select_core) => select_core.GROUP_() != null);
|
1148
1223
|
if (groupBy) {
|
1149
1224
|
return true;
|
1150
1225
|
}
|
1151
|
-
const agreegateFunction = select_core
|
1226
|
+
const agreegateFunction = select_core
|
1227
|
+
.result_column_list()
|
1228
|
+
.some((result_column) => isAgregateFunction(result_column));
|
1152
1229
|
if (agreegateFunction) {
|
1153
1230
|
return false;
|
1154
1231
|
}
|
1155
1232
|
const _whereExpr = select_core._whereExpr;
|
1156
|
-
const isSingleResult = select_core.join_clause() == null &&
|
1157
|
-
|
1233
|
+
const isSingleResult = select_core.join_clause() == null &&
|
1234
|
+
_whereExpr &&
|
1235
|
+
where_is_single_result(_whereExpr, fromColumns);
|
1236
|
+
if (isSingleResult === true) {
|
1158
1237
|
return false;
|
1159
1238
|
}
|
1160
1239
|
}
|
@@ -1166,7 +1245,8 @@ function isMultipleRowResult(select_stmt, fromColumns) {
|
|
1166
1245
|
exports.isMultipleRowResult = isMultipleRowResult;
|
1167
1246
|
function isAgregateFunction(result_column) {
|
1168
1247
|
var _a;
|
1169
|
-
if (((_a = result_column.expr()) === null || _a === void 0 ? void 0 : _a.over_clause()) != null) {
|
1248
|
+
if (((_a = result_column.expr()) === null || _a === void 0 ? void 0 : _a.over_clause()) != null) {
|
1249
|
+
//window function isMultipleRow = true
|
1170
1250
|
return false;
|
1171
1251
|
}
|
1172
1252
|
const expr = result_column.expr();
|
@@ -1176,21 +1256,21 @@ function isAgregateFunction(result_column) {
|
|
1176
1256
|
function isAgregateFunctionExpr(expr) {
|
1177
1257
|
var _a;
|
1178
1258
|
//ex. min(value)/100, 100/min(value)
|
1179
|
-
const isAgrr = expr.expr_list().some(expr => isAgregateFunctionExpr(expr));
|
1259
|
+
const isAgrr = expr.expr_list().some((expr) => isAgregateFunctionExpr(expr));
|
1180
1260
|
if (isAgrr) {
|
1181
1261
|
return isAgrr;
|
1182
1262
|
}
|
1183
1263
|
const function_name = (_a = expr.function_name()) === null || _a === void 0 ? void 0 : _a.getText().toLowerCase();
|
1184
|
-
return function_name
|
1185
|
-
|
1186
|
-
|
1187
|
-
|
1188
|
-
|
1189
|
-
|
1264
|
+
return (function_name === 'count' ||
|
1265
|
+
function_name === 'sum' ||
|
1266
|
+
function_name === 'avg' ||
|
1267
|
+
function_name === 'min' ||
|
1268
|
+
function_name === 'max' ||
|
1269
|
+
function_name === 'group_concat');
|
1190
1270
|
}
|
1191
1271
|
function isLimitOne(select_stmt) {
|
1192
1272
|
const limit_stmt = select_stmt.limit_stmt();
|
1193
|
-
if (limit_stmt && limit_stmt.expr(0).getText()
|
1273
|
+
if (limit_stmt && limit_stmt.expr(0).getText() === '1') {
|
1194
1274
|
return true;
|
1195
1275
|
}
|
1196
1276
|
return false;
|
@@ -1202,7 +1282,7 @@ function where_is_single_result(whereExpr, fromColumns) {
|
|
1202
1282
|
}
|
1203
1283
|
const expr_list = whereExpr.expr_list();
|
1204
1284
|
const onlyAnd = !whereExpr.OR_();
|
1205
|
-
const oneSingle = expr_list.some(expr => is_single_result(expr, fromColumns));
|
1285
|
+
const oneSingle = expr_list.some((expr) => is_single_result(expr, fromColumns));
|
1206
1286
|
if (onlyAnd && oneSingle) {
|
1207
1287
|
return true;
|
1208
1288
|
}
|
@@ -1215,7 +1295,7 @@ function is_single_result(expr, fromColumns) {
|
|
1215
1295
|
if (column_name && expr.ASSIGN()) {
|
1216
1296
|
const fieldName = (0, select_columns_1.splitName)(column_name.getText());
|
1217
1297
|
const column = (0, select_columns_1.findColumn)(fieldName, fromColumns);
|
1218
|
-
if (column.columnKey
|
1298
|
+
if (column.columnKey === 'PRI') {
|
1219
1299
|
return true;
|
1220
1300
|
}
|
1221
1301
|
}
|
@@ -1225,7 +1305,7 @@ function traverse_insert_stmt(insert_stmt, traverseContext) {
|
|
1225
1305
|
var _a;
|
1226
1306
|
const table_name = insert_stmt.table_name();
|
1227
1307
|
const fromColumns = (0, select_columns_1.filterColumns)(traverseContext.dbSchema, [], '', (0, select_columns_1.splitName)(table_name.getText()));
|
1228
|
-
const columns = insert_stmt.column_name_list().map(column_name => {
|
1308
|
+
const columns = insert_stmt.column_name_list().map((column_name) => {
|
1229
1309
|
return traverse_column_name(column_name, null, Object.assign(Object.assign({}, traverseContext), { fromColumns }));
|
1230
1310
|
});
|
1231
1311
|
const insertColumns = [];
|
@@ -1241,8 +1321,10 @@ function traverse_insert_stmt(insert_stmt, traverseContext) {
|
|
1241
1321
|
type1: col.columnType,
|
1242
1322
|
type2: exprType.type
|
1243
1323
|
});
|
1244
|
-
const notNullColumn =
|
1245
|
-
|
1324
|
+
const notNullColumn = col.columnKey === 'PRI' && col.columnType.type === 'INTEGER'
|
1325
|
+
? false
|
1326
|
+
: col.notNull;
|
1327
|
+
insertColumns.push(Object.assign(Object.assign({}, param), { notNull: exprType.name === '?' ? notNullColumn : param.notNull }));
|
1246
1328
|
});
|
1247
1329
|
});
|
1248
1330
|
});
|
@@ -1257,11 +1339,15 @@ function traverse_insert_stmt(insert_stmt, traverseContext) {
|
|
1257
1339
|
type1: col.columnType,
|
1258
1340
|
type2: selectColumn.type
|
1259
1341
|
});
|
1260
|
-
const notNullColumn =
|
1342
|
+
const notNullColumn = col.columnKey === 'PRI' && col.columnType.type === 'INTEGER'
|
1343
|
+
? false
|
1344
|
+
: col.notNull;
|
1261
1345
|
columnNullability.set(selectColumn.type.id, notNullColumn);
|
1262
1346
|
});
|
1263
|
-
traverseContext.parameters.forEach(param => {
|
1264
|
-
insertColumns.push(Object.assign(Object.assign({}, param), { notNull: columnNullability.get(param.type.id) != null
|
1347
|
+
traverseContext.parameters.forEach((param) => {
|
1348
|
+
insertColumns.push(Object.assign(Object.assign({}, param), { notNull: columnNullability.get(param.type.id) != null
|
1349
|
+
? columnNullability.get(param.type.id)
|
1350
|
+
: param.notNull }));
|
1265
1351
|
});
|
1266
1352
|
}
|
1267
1353
|
const upsert_clause = insert_stmt.upsert_clause();
|
@@ -1272,19 +1358,25 @@ function traverse_insert_stmt(insert_stmt, traverseContext) {
|
|
1272
1358
|
const column_name = upsert_clause.column_name(index);
|
1273
1359
|
const col = traverse_column_name(column_name, null, Object.assign(Object.assign({}, traverseContext), { fromColumns }));
|
1274
1360
|
const expr = upsert_clause.expr(index);
|
1275
|
-
const
|
1361
|
+
const table_name = expr.table_name();
|
1362
|
+
const excludedColumns = table_name && table_name.getText() === 'excluded'
|
1363
|
+
? fromColumns.map((col) => (Object.assign(Object.assign({}, col), { table: 'excluded' })))
|
1364
|
+
: [];
|
1365
|
+
const exprType = traverse_expr(expr, Object.assign(Object.assign({}, traverseContext), { fromColumns: fromColumns.concat(excludedColumns) }));
|
1276
1366
|
traverseContext.constraints.push({
|
1277
1367
|
expression: column_name.getText(),
|
1278
1368
|
type1: col.columnType,
|
1279
1369
|
type2: exprType.type
|
1280
1370
|
});
|
1281
1371
|
});
|
1282
|
-
traverseContext.parameters.slice(paramsBefore).forEach(param => {
|
1372
|
+
traverseContext.parameters.slice(paramsBefore).forEach((param) => {
|
1283
1373
|
insertColumns.push(Object.assign(Object.assign({}, param), { notNull: param.notNull }));
|
1284
1374
|
});
|
1285
1375
|
}
|
1286
1376
|
const returning_clause = insert_stmt.returning_clause();
|
1287
|
-
const returninColumns = returning_clause
|
1377
|
+
const returninColumns = returning_clause
|
1378
|
+
? traverse_returning_clause(returning_clause, fromColumns)
|
1379
|
+
: [];
|
1288
1380
|
const queryResult = {
|
1289
1381
|
queryType: 'Insert',
|
1290
1382
|
constraints: traverseContext.constraints,
|
@@ -1296,9 +1388,9 @@ function traverse_insert_stmt(insert_stmt, traverseContext) {
|
|
1296
1388
|
}
|
1297
1389
|
function traverse_returning_clause(returning_clause, fromColumns) {
|
1298
1390
|
const result_column_list = returning_clause.result_column_list();
|
1299
|
-
const result = result_column_list.flatMap(result_column => {
|
1391
|
+
const result = result_column_list.flatMap((result_column) => {
|
1300
1392
|
if (result_column.STAR()) {
|
1301
|
-
return fromColumns.map(col => {
|
1393
|
+
return fromColumns.map((col) => {
|
1302
1394
|
const newCol = {
|
1303
1395
|
name: col.columnName,
|
1304
1396
|
type: col.columnType,
|
@@ -1315,9 +1407,10 @@ function traverse_returning_clause(returning_clause, fromColumns) {
|
|
1315
1407
|
function traverse_update_stmt(update_stmt, traverseContext) {
|
1316
1408
|
const table_name = update_stmt.qualified_table_name().getText();
|
1317
1409
|
const fromColumns = (0, select_columns_1.filterColumns)(traverseContext.dbSchema, [], '', (0, select_columns_1.splitName)(table_name));
|
1318
|
-
const column_name_list = Array.from({
|
1319
|
-
|
1320
|
-
|
1410
|
+
const column_name_list = Array.from({
|
1411
|
+
length: update_stmt.ASSIGN_list().length
|
1412
|
+
}).map((_, i) => update_stmt.column_name(i));
|
1413
|
+
const columns = column_name_list.map((column_name) => {
|
1321
1414
|
return traverse_column_name(column_name, null, Object.assign(Object.assign({}, traverseContext), { fromColumns }));
|
1322
1415
|
});
|
1323
1416
|
const updateColumns = [];
|
@@ -1327,7 +1420,8 @@ function traverse_update_stmt(update_stmt, traverseContext) {
|
|
1327
1420
|
expr_list.forEach((expr, index) => {
|
1328
1421
|
paramsBefore = traverseContext.parameters.length;
|
1329
1422
|
const exprType = traverse_expr(expr, Object.assign(Object.assign({}, traverseContext), { fromColumns }));
|
1330
|
-
if (!update_stmt.WHERE_() ||
|
1423
|
+
if (!update_stmt.WHERE_() ||
|
1424
|
+
expr.start.start < update_stmt.WHERE_().symbol.start) {
|
1331
1425
|
const col = columns[index];
|
1332
1426
|
traverseContext.constraints.push({
|
1333
1427
|
expression: expr.getText(),
|
@@ -1373,11 +1467,10 @@ function getAllColumns(expr) {
|
|
1373
1467
|
columns.push((0, select_columns_1.splitName)(expr1.getText()));
|
1374
1468
|
columns.push((0, select_columns_1.splitName)(expr2.getText()));
|
1375
1469
|
}
|
1376
|
-
;
|
1377
1470
|
return columns;
|
1378
1471
|
}
|
1379
1472
|
function filterUsingFields(fields, usingFields) {
|
1380
|
-
const result = fields.filter(field => !usingFields.includes(field.columnName));
|
1473
|
+
const result = fields.filter((field) => !usingFields.includes(field.columnName));
|
1381
1474
|
return result;
|
1382
1475
|
}
|
1383
1476
|
function getParamsIndexes(parameters, paramsIds) {
|
@@ -1385,7 +1478,7 @@ function getParamsIndexes(parameters, paramsIds) {
|
|
1385
1478
|
parameters.forEach((param, index) => {
|
1386
1479
|
map.set(param.paramIndex, index);
|
1387
1480
|
});
|
1388
|
-
return paramsIds.map(id => map.get(id));
|
1481
|
+
return paramsIds.map((id) => map.get(id));
|
1389
1482
|
}
|
1390
1483
|
function getWhereFragmentExpressions(whereExpr) {
|
1391
1484
|
const exprList = [];
|