typesql-cli 0.9.6 → 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 +87 -78
- 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 +2 -2
- package/drivers/libsql.d.ts.map +1 -1
- package/drivers/libsql.js +3 -3
- 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 +76 -53
- 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 +5 -5
- package/sqlite-query-analyzer/query-executor.d.ts.map +1 -1
- package/sqlite-query-analyzer/query-executor.js +39 -25
- 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 +249 -160
- 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 +13 -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,
|
@@ -281,21 +297,29 @@ function traverse_table_or_subquery(table_or_subquery_list, join_constraint_list
|
|
281
297
|
table_or_subquery_list.forEach((table_or_subquery, index) => {
|
282
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
|
+
: '';
|
299
323
|
const schema = ((_d = table_or_subquery.schema_name()) === null || _d === void 0 ? void 0 : _d.getText()) || '';
|
300
324
|
if (table_name) {
|
301
325
|
const tableName = {
|
@@ -303,10 +327,14 @@ function traverse_table_or_subquery(table_or_subquery_list, join_constraint_list
|
|
303
327
|
prefix: schema
|
304
328
|
};
|
305
329
|
tableOrSubqueryFields = (0, select_columns_1.filterColumns)(traverseContext.dbSchema, traverseContext.withSchema, table_alias, tableName);
|
306
|
-
const usingFields = (join_constraint === null || join_constraint === void 0 ? void 0 : join_constraint.USING_())
|
307
|
-
|
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;
|
308
336
|
if (isLeftJoin) {
|
309
|
-
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 }))));
|
310
338
|
}
|
311
339
|
else {
|
312
340
|
allFields.push(...filteredFields);
|
@@ -315,12 +343,12 @@ function traverse_table_or_subquery(table_or_subquery_list, join_constraint_list
|
|
315
343
|
const select_stmt = table_or_subquery.select_stmt();
|
316
344
|
if (select_stmt) {
|
317
345
|
const subQueryResult = traverse_select_stmt(select_stmt, Object.assign(Object.assign({}, traverseContext), { subQuery: true }));
|
318
|
-
tableOrSubqueryFields = subQueryResult.columns.map(t => {
|
346
|
+
tableOrSubqueryFields = subQueryResult.columns.map((t) => {
|
319
347
|
const colDef = {
|
320
348
|
table: t.table ? tableAlias || '' : '',
|
321
349
|
columnName: t.name,
|
322
350
|
columnType: t.type,
|
323
|
-
columnKey:
|
351
|
+
columnKey: '',
|
324
352
|
notNull: t.notNull,
|
325
353
|
tableAlias: tableAlias
|
326
354
|
};
|
@@ -333,7 +361,7 @@ function traverse_table_or_subquery(table_or_subquery_list, join_constraint_list
|
|
333
361
|
tableOrSubqueryFields = traverse_table_or_subquery(table_or_subquery_list2, null, null, traverseContext);
|
334
362
|
allFields.push(...tableOrSubqueryFields);
|
335
363
|
}
|
336
|
-
const idColumn = (_e = tableOrSubqueryFields.find(field => field.columnKey
|
364
|
+
const idColumn = (_e = tableOrSubqueryFields.find((field) => field.columnKey === 'PRI')) === null || _e === void 0 ? void 0 : _e.columnName;
|
337
365
|
const relation = {
|
338
366
|
name: asAlias ? table_alias : tableOrSubqueryName,
|
339
367
|
alias: table_alias,
|
@@ -342,23 +370,31 @@ function traverse_table_or_subquery(table_or_subquery_list, join_constraint_list
|
|
342
370
|
parentCardinality: 'one',
|
343
371
|
joinColumn: idColumn
|
344
372
|
};
|
345
|
-
if (join_constraint) {
|
373
|
+
if (join_constraint) {
|
374
|
+
//index 0 is the FROM (root relation)
|
346
375
|
const expr = join_constraint.expr(); //ON expr
|
347
376
|
if (expr) {
|
348
377
|
traverse_expr(expr, Object.assign(Object.assign({}, traverseContext), { fromColumns: allFields }));
|
349
378
|
const allJoinColumsn = getAllColumns(expr);
|
350
|
-
allJoinColumsn.forEach(joinColumn => {
|
351
|
-
const column = allFields.find(col => col.columnName
|
352
|
-
|
353
|
-
|
354
|
-
const
|
355
|
-
|
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) {
|
356
391
|
relation.parentRelation = joinColumn.prefix;
|
357
392
|
if (notUnique) {
|
358
393
|
relation.parentCardinality = 'many';
|
359
394
|
}
|
360
395
|
}
|
361
|
-
if (joinColumn.prefix
|
396
|
+
if (joinColumn.prefix === relation.name ||
|
397
|
+
joinColumn.prefix === relation.alias) {
|
362
398
|
if (notUnique) {
|
363
399
|
relation.cardinality = 'many';
|
364
400
|
}
|
@@ -369,16 +405,18 @@ function traverse_table_or_subquery(table_or_subquery_list, join_constraint_list
|
|
369
405
|
if (!traverseContext.subQuery) {
|
370
406
|
traverseContext.relations.push(relation);
|
371
407
|
//dynamic query
|
372
|
-
const fragment =
|
373
|
-
|
374
|
-
|
375
|
-
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);
|
376
414
|
traverseContext.dynamicSqlInfo2.from.push({
|
377
415
|
fragment: fragment,
|
378
416
|
relationName: relation.name,
|
379
417
|
relationAlias: relation.alias,
|
380
418
|
parentRelation: relation.parentRelation,
|
381
|
-
fields: tableOrSubqueryFields.map(field => field.columnName),
|
419
|
+
fields: tableOrSubqueryFields.map((field) => field.columnName),
|
382
420
|
parameters: params
|
383
421
|
});
|
384
422
|
}
|
@@ -388,11 +426,11 @@ function traverse_table_or_subquery(table_or_subquery_list, join_constraint_list
|
|
388
426
|
function traverse_expr(expr, traverseContext) {
|
389
427
|
var _a, _b;
|
390
428
|
const function_name = (_a = expr.function_name()) === null || _a === void 0 ? void 0 : _a.getText().toLowerCase();
|
391
|
-
if (function_name
|
429
|
+
if (function_name === 'avg') {
|
392
430
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'REAL');
|
393
431
|
const sumParamExpr = expr.expr(0);
|
394
432
|
const paramType = traverse_expr(sumParamExpr, traverseContext);
|
395
|
-
if (paramType.type.kind
|
433
|
+
if (paramType.type.kind === 'TypeVar') {
|
396
434
|
functionType.table = paramType.table;
|
397
435
|
}
|
398
436
|
return {
|
@@ -402,7 +440,7 @@ function traverse_expr(expr, traverseContext) {
|
|
402
440
|
table: functionType.table || ''
|
403
441
|
};
|
404
442
|
}
|
405
|
-
if (function_name
|
443
|
+
if (function_name === 'sum') {
|
406
444
|
const sumParamExpr = expr.expr(0);
|
407
445
|
const paramType = traverse_expr(sumParamExpr, traverseContext);
|
408
446
|
return {
|
@@ -412,7 +450,7 @@ function traverse_expr(expr, traverseContext) {
|
|
412
450
|
table: paramType.table || ''
|
413
451
|
};
|
414
452
|
}
|
415
|
-
if (function_name
|
453
|
+
if (function_name === 'min' || function_name === 'max') {
|
416
454
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
|
417
455
|
const sumParamExpr = expr.expr(0);
|
418
456
|
const paramType = traverse_expr(sumParamExpr, traverseContext);
|
@@ -428,12 +466,12 @@ function traverse_expr(expr, traverseContext) {
|
|
428
466
|
table: functionType.table || ''
|
429
467
|
};
|
430
468
|
}
|
431
|
-
if (function_name
|
469
|
+
if (function_name === 'count') {
|
432
470
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'INTEGER');
|
433
|
-
if (expr.expr_list().length
|
471
|
+
if (expr.expr_list().length === 1) {
|
434
472
|
const sumParamExpr = expr.expr(0);
|
435
473
|
const paramType = traverse_expr(sumParamExpr, traverseContext);
|
436
|
-
if (paramType.type.kind
|
474
|
+
if (paramType.type.kind === 'TypeVar') {
|
437
475
|
functionType.table = paramType.table;
|
438
476
|
}
|
439
477
|
}
|
@@ -444,16 +482,16 @@ function traverse_expr(expr, traverseContext) {
|
|
444
482
|
table: functionType.table || ''
|
445
483
|
};
|
446
484
|
}
|
447
|
-
if (function_name
|
485
|
+
if (function_name === 'concat') {
|
448
486
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'TEXT');
|
449
|
-
expr.expr_list().forEach(paramExpr => {
|
487
|
+
expr.expr_list().forEach((paramExpr) => {
|
450
488
|
const paramType = traverse_expr(paramExpr, traverseContext);
|
451
489
|
traverseContext.constraints.push({
|
452
490
|
expression: expr.getText(),
|
453
491
|
type1: functionType,
|
454
492
|
type2: paramType.type
|
455
493
|
});
|
456
|
-
if (paramType.type.kind
|
494
|
+
if (paramType.type.kind === 'TypeVar') {
|
457
495
|
functionType.table = paramType.table;
|
458
496
|
}
|
459
497
|
});
|
@@ -464,7 +502,7 @@ function traverse_expr(expr, traverseContext) {
|
|
464
502
|
table: functionType.table || ''
|
465
503
|
};
|
466
504
|
}
|
467
|
-
if (function_name
|
505
|
+
if (function_name === 'length') {
|
468
506
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'INTEGER');
|
469
507
|
const paramExpr = expr.expr(0);
|
470
508
|
const paramType = traverse_expr(paramExpr, traverseContext);
|
@@ -473,7 +511,7 @@ function traverse_expr(expr, traverseContext) {
|
|
473
511
|
type1: (0, collect_constraints_1.freshVar)(expr.getText(), '?'), //str or blob
|
474
512
|
type2: paramType.type
|
475
513
|
});
|
476
|
-
if (paramType.type.kind
|
514
|
+
if (paramType.type.kind === 'TypeVar') {
|
477
515
|
functionType.table = paramType.table;
|
478
516
|
}
|
479
517
|
return {
|
@@ -483,8 +521,8 @@ function traverse_expr(expr, traverseContext) {
|
|
483
521
|
table: functionType.table || ''
|
484
522
|
};
|
485
523
|
}
|
486
|
-
if (function_name
|
487
|
-
expr.expr_list().forEach(paramExpr => {
|
524
|
+
if (function_name === 'group_concat') {
|
525
|
+
expr.expr_list().forEach((paramExpr) => {
|
488
526
|
const param1Type = traverse_expr(paramExpr, traverseContext);
|
489
527
|
traverseContext.constraints.push({
|
490
528
|
expression: expr.getText(),
|
@@ -499,7 +537,16 @@ function traverse_expr(expr, traverseContext) {
|
|
499
537
|
table: ''
|
500
538
|
};
|
501
539
|
}
|
502
|
-
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') {
|
503
550
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'REAL');
|
504
551
|
const param1Expr = expr.expr(0);
|
505
552
|
const param1Type = traverse_expr(param1Expr, traverseContext);
|
@@ -524,9 +571,9 @@ function traverse_expr(expr, traverseContext) {
|
|
524
571
|
table: param1Type.table || ''
|
525
572
|
};
|
526
573
|
}
|
527
|
-
if (function_name
|
574
|
+
if (function_name === 'coalesce') {
|
528
575
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
|
529
|
-
const paramTypes = expr.expr_list().map(paramExpr => {
|
576
|
+
const paramTypes = expr.expr_list().map((paramExpr) => {
|
530
577
|
const paramType = traverse_expr(paramExpr, traverseContext);
|
531
578
|
traverseContext.constraints.push({
|
532
579
|
expression: expr.getText(),
|
@@ -538,11 +585,11 @@ function traverse_expr(expr, traverseContext) {
|
|
538
585
|
return {
|
539
586
|
name: functionType.name,
|
540
587
|
type: functionType,
|
541
|
-
notNull: paramTypes.some(param => param.notNull),
|
588
|
+
notNull: paramTypes.some((param) => param.notNull),
|
542
589
|
table: functionType.table || ''
|
543
590
|
};
|
544
591
|
}
|
545
|
-
if (function_name
|
592
|
+
if (function_name === 'strftime') {
|
546
593
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'TEXT');
|
547
594
|
const paramExpr = expr.expr(1);
|
548
595
|
const paramType = traverse_expr(paramExpr, traverseContext);
|
@@ -559,7 +606,9 @@ function traverse_expr(expr, traverseContext) {
|
|
559
606
|
table: functionType.table || ''
|
560
607
|
};
|
561
608
|
}
|
562
|
-
if (function_name
|
609
|
+
if (function_name === 'date' ||
|
610
|
+
function_name === 'time' ||
|
611
|
+
function_name === 'datetime') {
|
563
612
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'DATE');
|
564
613
|
const paramExpr = expr.expr(0);
|
565
614
|
const paramType = traverse_expr(paramExpr, traverseContext);
|
@@ -576,10 +625,10 @@ function traverse_expr(expr, traverseContext) {
|
|
576
625
|
table: functionType.table || ''
|
577
626
|
};
|
578
627
|
}
|
579
|
-
if (function_name
|
628
|
+
if (function_name === 'julianday') {
|
580
629
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'REAL');
|
581
630
|
const paramExpr = expr.expr(0);
|
582
|
-
const notNull = paramExpr.getText().toLowerCase()
|
631
|
+
const notNull = paramExpr.getText().toLowerCase() === `'now'`;
|
583
632
|
const paramType = traverse_expr(paramExpr, traverseContext);
|
584
633
|
traverseContext.constraints.push({
|
585
634
|
expression: paramExpr.getText(),
|
@@ -593,10 +642,10 @@ function traverse_expr(expr, traverseContext) {
|
|
593
642
|
table: functionType.table || ''
|
594
643
|
};
|
595
644
|
}
|
596
|
-
if (function_name
|
645
|
+
if (function_name === 'unixepoch') {
|
597
646
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'INTEGER');
|
598
647
|
const paramExpr = expr.expr(0);
|
599
|
-
const notNull = paramExpr.getText().toLowerCase()
|
648
|
+
const notNull = paramExpr.getText().toLowerCase() === `'now'`;
|
600
649
|
const paramType = traverse_expr(paramExpr, traverseContext);
|
601
650
|
traverseContext.constraints.push({
|
602
651
|
expression: paramExpr.getText(),
|
@@ -610,11 +659,11 @@ function traverse_expr(expr, traverseContext) {
|
|
610
659
|
table: functionType.table || ''
|
611
660
|
};
|
612
661
|
}
|
613
|
-
if (function_name
|
662
|
+
if (function_name === 'ifnull') {
|
614
663
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
|
615
|
-
const paramTypes = expr.expr_list().map(paramExpr => {
|
664
|
+
const paramTypes = expr.expr_list().map((paramExpr) => {
|
616
665
|
const paramType = traverse_expr(paramExpr, traverseContext);
|
617
|
-
if (paramType.name
|
666
|
+
if (paramType.name === '?') {
|
618
667
|
paramType.notNull = false;
|
619
668
|
}
|
620
669
|
traverseContext.constraints.push({
|
@@ -627,13 +676,13 @@ function traverse_expr(expr, traverseContext) {
|
|
627
676
|
return {
|
628
677
|
name: functionType.name,
|
629
678
|
type: functionType,
|
630
|
-
notNull: paramTypes.every(param => param.notNull),
|
679
|
+
notNull: paramTypes.every((param) => param.notNull),
|
631
680
|
table: functionType.table || ''
|
632
681
|
};
|
633
682
|
}
|
634
|
-
if (function_name
|
635
|
-
|
636
|
-
|
683
|
+
if (function_name === 'row_number' ||
|
684
|
+
function_name === 'rank' ||
|
685
|
+
function_name === 'dense_rank') {
|
637
686
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'INTEGER');
|
638
687
|
return {
|
639
688
|
name: functionType.name,
|
@@ -642,19 +691,17 @@ function traverse_expr(expr, traverseContext) {
|
|
642
691
|
table: functionType.table || ''
|
643
692
|
};
|
644
693
|
}
|
645
|
-
if (function_name
|
646
|
-
|| function_name == 'last_value') {
|
694
|
+
if (function_name === 'first_value' || function_name === 'last_value') {
|
647
695
|
const paramExpr = expr.expr(0);
|
648
696
|
const paramType = traverse_expr(paramExpr, traverseContext);
|
649
697
|
return paramType;
|
650
698
|
}
|
651
|
-
if (function_name
|
652
|
-
|| function_name == 'lag') {
|
699
|
+
if (function_name === 'lead' || function_name === 'lag') {
|
653
700
|
const paramExpr = expr.expr(0);
|
654
701
|
const paramType = traverse_expr(paramExpr, traverseContext);
|
655
702
|
return Object.assign(Object.assign({}, paramType), { notNull: false });
|
656
703
|
}
|
657
|
-
if (function_name
|
704
|
+
if (function_name === 'iif') {
|
658
705
|
const expr1 = expr.expr(0);
|
659
706
|
traverse_expr(expr1, traverseContext);
|
660
707
|
const expr2 = expr.expr(1);
|
@@ -668,7 +715,7 @@ function traverse_expr(expr, traverseContext) {
|
|
668
715
|
});
|
669
716
|
return Object.assign(Object.assign({}, expr2Type), { notNull: expr2Type.notNull && expr3Type.notNull });
|
670
717
|
}
|
671
|
-
if (function_name
|
718
|
+
if (function_name === 'vector') {
|
672
719
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'BLOB');
|
673
720
|
const param1Expr = expr.expr(0);
|
674
721
|
const param1Type = traverse_expr(param1Expr, traverseContext);
|
@@ -685,7 +732,7 @@ function traverse_expr(expr, traverseContext) {
|
|
685
732
|
table: ''
|
686
733
|
};
|
687
734
|
}
|
688
|
-
if (function_name
|
735
|
+
if (function_name === 'vector_extract') {
|
689
736
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'TEXT');
|
690
737
|
const param1Expr = expr.expr(0);
|
691
738
|
const param1Type = traverse_expr(param1Expr, traverseContext);
|
@@ -701,7 +748,7 @@ function traverse_expr(expr, traverseContext) {
|
|
701
748
|
table: ''
|
702
749
|
};
|
703
750
|
}
|
704
|
-
if (function_name
|
751
|
+
if (function_name === 'vector_distance_cos') {
|
705
752
|
const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'REAL');
|
706
753
|
const param1Expr = expr.expr(0);
|
707
754
|
if (param1Expr) {
|
@@ -731,7 +778,7 @@ function traverse_expr(expr, traverseContext) {
|
|
731
778
|
};
|
732
779
|
}
|
733
780
|
if (function_name) {
|
734
|
-
throw Error(
|
781
|
+
throw Error(`traverse_expr: function not supported:${function_name}`);
|
735
782
|
}
|
736
783
|
const column_name = expr.column_name();
|
737
784
|
const table_name = expr.table_name();
|
@@ -768,7 +815,7 @@ function traverse_expr(expr, traverseContext) {
|
|
768
815
|
return {
|
769
816
|
name: type.name,
|
770
817
|
type: type,
|
771
|
-
notNull: literal.NULL_()
|
818
|
+
notNull: literal.NULL_() == null,
|
772
819
|
table: type.table || ''
|
773
820
|
};
|
774
821
|
}
|
@@ -821,8 +868,8 @@ function traverse_expr(expr, traverseContext) {
|
|
821
868
|
type1: returnType,
|
822
869
|
type2: typeLeft.type
|
823
870
|
});
|
824
|
-
const isDateFunctionContext =
|
825
|
-
|
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';
|
826
873
|
if (!isDateFunctionContext) {
|
827
874
|
traverseContext.constraints.push({
|
828
875
|
expression: exprRight.getText(),
|
@@ -832,15 +879,24 @@ function traverse_expr(expr, traverseContext) {
|
|
832
879
|
}
|
833
880
|
return Object.assign(Object.assign({}, typeLeft), { notNull: typeLeft.notNull && typeRight.notNull });
|
834
881
|
}
|
835
|
-
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()) {
|
836
892
|
const exprLeft = expr.expr(0);
|
837
893
|
const exprRight = expr.expr(1);
|
838
894
|
const typeLeft = traverse_expr(exprLeft, traverseContext);
|
839
895
|
const typeRight = traverse_expr(exprRight, traverseContext);
|
840
|
-
if (typeLeft.name
|
896
|
+
if (typeLeft.name === '?') {
|
841
897
|
typeLeft.notNull = true;
|
842
898
|
}
|
843
|
-
if (typeRight.name
|
899
|
+
if (typeRight.name === '?') {
|
844
900
|
typeRight.notNull = true;
|
845
901
|
}
|
846
902
|
traverseContext.constraints.push({
|
@@ -856,7 +912,8 @@ function traverse_expr(expr, traverseContext) {
|
|
856
912
|
table: resultType.table || ''
|
857
913
|
};
|
858
914
|
}
|
859
|
-
if (expr.IS_()) {
|
915
|
+
if (expr.IS_()) {
|
916
|
+
//is null/is not null
|
860
917
|
const expr_ = expr.expr(0);
|
861
918
|
traverse_expr(expr_, traverseContext);
|
862
919
|
const type = (0, collect_constraints_1.freshVar)(expr.getText(), 'INTEGER');
|
@@ -867,15 +924,16 @@ function traverse_expr(expr, traverseContext) {
|
|
867
924
|
table: type.table || ''
|
868
925
|
};
|
869
926
|
}
|
870
|
-
if (expr.ASSIGN()) {
|
927
|
+
if (expr.ASSIGN()) {
|
928
|
+
//=
|
871
929
|
const exprLeft = expr.expr(0);
|
872
930
|
const exprRight = expr.expr(1);
|
873
931
|
const typeLeft = traverse_expr(exprLeft, traverseContext);
|
874
932
|
const typeRight = traverse_expr(exprRight, traverseContext);
|
875
|
-
if (typeLeft.name
|
933
|
+
if (typeLeft.name === '?') {
|
876
934
|
typeLeft.notNull = true;
|
877
935
|
}
|
878
|
-
if (typeRight.name
|
936
|
+
if (typeRight.name === '?') {
|
879
937
|
typeRight.notNull = true;
|
880
938
|
}
|
881
939
|
traverseContext.constraints.push({
|
@@ -895,10 +953,10 @@ function traverse_expr(expr, traverseContext) {
|
|
895
953
|
const exprType = traverse_expr(expr.expr(0), traverseContext);
|
896
954
|
const between1 = traverse_expr(expr.expr(1), traverseContext);
|
897
955
|
const between2 = traverse_expr(expr.expr(2), traverseContext);
|
898
|
-
if (between1.name
|
956
|
+
if (between1.name === '?') {
|
899
957
|
between1.notNull = true;
|
900
958
|
}
|
901
|
-
if (between2.name
|
959
|
+
if (between2.name === '?') {
|
902
960
|
between2.notNull = true;
|
903
961
|
}
|
904
962
|
traverseContext.constraints.push({
|
@@ -928,7 +986,7 @@ function traverse_expr(expr, traverseContext) {
|
|
928
986
|
const exprList = expr.expr_list();
|
929
987
|
const inExprLeft = exprList[0];
|
930
988
|
const typeLeft = traverse_expr(inExprLeft, traverseContext);
|
931
|
-
if (typeLeft.name
|
989
|
+
if (typeLeft.name === '?') {
|
932
990
|
typeLeft.notNull = true;
|
933
991
|
}
|
934
992
|
if (expr.NOT_()) {
|
@@ -944,9 +1002,9 @@ function traverse_expr(expr, traverseContext) {
|
|
944
1002
|
});
|
945
1003
|
}
|
946
1004
|
//NOT IN (1, 2, 3)
|
947
|
-
exprList.slice(1).forEach(inExpr => {
|
1005
|
+
exprList.slice(1).forEach((inExpr) => {
|
948
1006
|
const typeRight = traverse_expr(inExpr, traverseContext);
|
949
|
-
if (typeRight.name
|
1007
|
+
if (typeRight.name === '?') {
|
950
1008
|
typeRight.notNull = true;
|
951
1009
|
}
|
952
1010
|
traverseContext.constraints.push({
|
@@ -970,9 +1028,9 @@ function traverse_expr(expr, traverseContext) {
|
|
970
1028
|
});
|
971
1029
|
}
|
972
1030
|
//IN (1, 2, 3)
|
973
|
-
rightExpr.expr_list().forEach(inExpr2 => {
|
1031
|
+
rightExpr.expr_list().forEach((inExpr2) => {
|
974
1032
|
const typeRight = traverse_expr(inExpr2, traverseContext);
|
975
|
-
if (typeRight.name
|
1033
|
+
if (typeRight.name === '?') {
|
976
1034
|
typeRight.notNull = true;
|
977
1035
|
}
|
978
1036
|
traverseContext.constraints.push({
|
@@ -995,10 +1053,10 @@ function traverse_expr(expr, traverseContext) {
|
|
995
1053
|
const exprRight = expr.expr(1);
|
996
1054
|
const typeLeft = traverse_expr(exprLeft, traverseContext);
|
997
1055
|
const typeRight = traverse_expr(exprRight, traverseContext);
|
998
|
-
if (typeLeft.name
|
1056
|
+
if (typeLeft.name === '?') {
|
999
1057
|
typeLeft.notNull = true;
|
1000
1058
|
}
|
1001
|
-
if (typeRight.name
|
1059
|
+
if (typeRight.name === '?') {
|
1002
1060
|
typeRight.notNull = true;
|
1003
1061
|
}
|
1004
1062
|
traverseContext.constraints.push({
|
@@ -1032,7 +1090,7 @@ function traverse_expr(expr, traverseContext) {
|
|
1032
1090
|
}
|
1033
1091
|
if (expr.OPEN_PAR() && expr.CLOSE_PAR()) {
|
1034
1092
|
const type = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
|
1035
|
-
const exprTypes = expr.expr_list().map(innerExpr => {
|
1093
|
+
const exprTypes = expr.expr_list().map((innerExpr) => {
|
1036
1094
|
const exprType = traverse_expr(innerExpr, traverseContext);
|
1037
1095
|
traverseContext.constraints.push({
|
1038
1096
|
expression: innerExpr.getText(),
|
@@ -1044,7 +1102,7 @@ function traverse_expr(expr, traverseContext) {
|
|
1044
1102
|
return {
|
1045
1103
|
name: type.name,
|
1046
1104
|
type: type,
|
1047
|
-
notNull: exprTypes.every(type => type.notNull),
|
1105
|
+
notNull: exprTypes.every((type) => type.notNull),
|
1048
1106
|
table: type.table || ''
|
1049
1107
|
};
|
1050
1108
|
}
|
@@ -1053,7 +1111,8 @@ function traverse_expr(expr, traverseContext) {
|
|
1053
1111
|
const whenTypes = [];
|
1054
1112
|
expr.expr_list().forEach((expr_, index) => {
|
1055
1113
|
const type = traverse_expr(expr_, traverseContext);
|
1056
|
-
if (index % 2
|
1114
|
+
if (index % 2 === 0 &&
|
1115
|
+
(!expr.ELSE_() || index < expr.expr_list().length - 1)) {
|
1057
1116
|
whenTypes.push(type);
|
1058
1117
|
}
|
1059
1118
|
else {
|
@@ -1080,11 +1139,11 @@ function traverse_expr(expr, traverseContext) {
|
|
1080
1139
|
return {
|
1081
1140
|
name: extractOriginalSql(expr),
|
1082
1141
|
type: type.type,
|
1083
|
-
notNull: expr.ELSE_() ? resultTypes.every(type => type.notNull) : false,
|
1142
|
+
notNull: expr.ELSE_() ? resultTypes.every((type) => type.notNull) : false,
|
1084
1143
|
table: type.table || ''
|
1085
1144
|
};
|
1086
1145
|
}
|
1087
|
-
throw Error(
|
1146
|
+
throw Error(`traverse_expr not supported:${expr.getText()}`);
|
1088
1147
|
}
|
1089
1148
|
function extractOriginalSql(rule) {
|
1090
1149
|
var _a, _b;
|
@@ -1094,7 +1153,10 @@ function extractOriginalSql(rule) {
|
|
1094
1153
|
return result;
|
1095
1154
|
}
|
1096
1155
|
function traverse_column_name(column_name, table_name, traverseContext) {
|
1097
|
-
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
|
+
};
|
1098
1160
|
const column = (0, select_columns_1.findColumn)(fieldName, traverseContext.fromColumns);
|
1099
1161
|
// const typeVar = freshVar(column.columnName, column.columnType.type, column.tableAlias || column.table);
|
1100
1162
|
return column;
|
@@ -1104,18 +1166,20 @@ function isNotNull(columnName, where) {
|
|
1104
1166
|
return false;
|
1105
1167
|
}
|
1106
1168
|
if (where.AND_()) {
|
1107
|
-
const ifNullList = where
|
1108
|
-
|
1169
|
+
const ifNullList = where
|
1170
|
+
.expr_list()
|
1171
|
+
.map((expr) => isNotNull(columnName, expr));
|
1172
|
+
const result = ifNullList.some((v) => v);
|
1109
1173
|
return result;
|
1110
1174
|
}
|
1111
|
-
|
1112
|
-
const possibleNullList = where
|
1113
|
-
|
1175
|
+
if (where.OR_()) {
|
1176
|
+
const possibleNullList = where
|
1177
|
+
.expr_list()
|
1178
|
+
.map((expr) => isNotNull(columnName, expr));
|
1179
|
+
const result = possibleNullList.every((v) => v);
|
1114
1180
|
return result;
|
1115
1181
|
}
|
1116
|
-
|
1117
|
-
return isNotNullExpr(columnName, where);
|
1118
|
-
}
|
1182
|
+
return isNotNullExpr(columnName, where);
|
1119
1183
|
}
|
1120
1184
|
exports.isNotNull = isNotNull;
|
1121
1185
|
function isNotNullExpr(columnName, expr) {
|
@@ -1123,10 +1187,14 @@ function isNotNullExpr(columnName, expr) {
|
|
1123
1187
|
const innerExpr = expr.expr(0);
|
1124
1188
|
return isNotNull(columnName, innerExpr);
|
1125
1189
|
}
|
1126
|
-
if (expr.ASSIGN()
|
1127
|
-
|
1128
|
-
|
1129
|
-
|
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')) {
|
1130
1198
|
const exprLeft = expr.expr(0);
|
1131
1199
|
const exprRight = expr.expr(1);
|
1132
1200
|
const column_name_left = exprLeft.column_name();
|
@@ -1134,7 +1202,7 @@ function isNotNullExpr(columnName, expr) {
|
|
1134
1202
|
if (column_name_left || column_name_right) {
|
1135
1203
|
const columnLeft = column_name_left === null || column_name_left === void 0 ? void 0 : column_name_left.getText();
|
1136
1204
|
const columnRight = column_name_right === null || column_name_right === void 0 ? void 0 : column_name_right.getText();
|
1137
|
-
if (columnLeft
|
1205
|
+
if (columnLeft === columnName || columnRight === columnName) {
|
1138
1206
|
return true;
|
1139
1207
|
}
|
1140
1208
|
}
|
@@ -1142,23 +1210,30 @@ function isNotNullExpr(columnName, expr) {
|
|
1142
1210
|
return false;
|
1143
1211
|
}
|
1144
1212
|
function isMultipleRowResult(select_stmt, fromColumns) {
|
1145
|
-
if (select_stmt.select_core_list().length
|
1213
|
+
if (select_stmt.select_core_list().length === 1) {
|
1214
|
+
//UNION queries are multipleRowsResult = true
|
1146
1215
|
const select_core = select_stmt.select_core(0);
|
1147
1216
|
const from = select_core.FROM_();
|
1148
1217
|
if (!from) {
|
1149
1218
|
return false;
|
1150
1219
|
}
|
1151
|
-
const groupBy = select_stmt
|
1220
|
+
const groupBy = select_stmt
|
1221
|
+
.select_core_list()
|
1222
|
+
.some((select_core) => select_core.GROUP_() != null);
|
1152
1223
|
if (groupBy) {
|
1153
1224
|
return true;
|
1154
1225
|
}
|
1155
|
-
const agreegateFunction = select_core
|
1226
|
+
const agreegateFunction = select_core
|
1227
|
+
.result_column_list()
|
1228
|
+
.some((result_column) => isAgregateFunction(result_column));
|
1156
1229
|
if (agreegateFunction) {
|
1157
1230
|
return false;
|
1158
1231
|
}
|
1159
1232
|
const _whereExpr = select_core._whereExpr;
|
1160
|
-
const isSingleResult = select_core.join_clause() == null &&
|
1161
|
-
|
1233
|
+
const isSingleResult = select_core.join_clause() == null &&
|
1234
|
+
_whereExpr &&
|
1235
|
+
where_is_single_result(_whereExpr, fromColumns);
|
1236
|
+
if (isSingleResult === true) {
|
1162
1237
|
return false;
|
1163
1238
|
}
|
1164
1239
|
}
|
@@ -1170,7 +1245,8 @@ function isMultipleRowResult(select_stmt, fromColumns) {
|
|
1170
1245
|
exports.isMultipleRowResult = isMultipleRowResult;
|
1171
1246
|
function isAgregateFunction(result_column) {
|
1172
1247
|
var _a;
|
1173
|
-
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
|
1174
1250
|
return false;
|
1175
1251
|
}
|
1176
1252
|
const expr = result_column.expr();
|
@@ -1180,21 +1256,21 @@ function isAgregateFunction(result_column) {
|
|
1180
1256
|
function isAgregateFunctionExpr(expr) {
|
1181
1257
|
var _a;
|
1182
1258
|
//ex. min(value)/100, 100/min(value)
|
1183
|
-
const isAgrr = expr.expr_list().some(expr => isAgregateFunctionExpr(expr));
|
1259
|
+
const isAgrr = expr.expr_list().some((expr) => isAgregateFunctionExpr(expr));
|
1184
1260
|
if (isAgrr) {
|
1185
1261
|
return isAgrr;
|
1186
1262
|
}
|
1187
1263
|
const function_name = (_a = expr.function_name()) === null || _a === void 0 ? void 0 : _a.getText().toLowerCase();
|
1188
|
-
return function_name
|
1189
|
-
|
1190
|
-
|
1191
|
-
|
1192
|
-
|
1193
|
-
|
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');
|
1194
1270
|
}
|
1195
1271
|
function isLimitOne(select_stmt) {
|
1196
1272
|
const limit_stmt = select_stmt.limit_stmt();
|
1197
|
-
if (limit_stmt && limit_stmt.expr(0).getText()
|
1273
|
+
if (limit_stmt && limit_stmt.expr(0).getText() === '1') {
|
1198
1274
|
return true;
|
1199
1275
|
}
|
1200
1276
|
return false;
|
@@ -1206,7 +1282,7 @@ function where_is_single_result(whereExpr, fromColumns) {
|
|
1206
1282
|
}
|
1207
1283
|
const expr_list = whereExpr.expr_list();
|
1208
1284
|
const onlyAnd = !whereExpr.OR_();
|
1209
|
-
const oneSingle = expr_list.some(expr => is_single_result(expr, fromColumns));
|
1285
|
+
const oneSingle = expr_list.some((expr) => is_single_result(expr, fromColumns));
|
1210
1286
|
if (onlyAnd && oneSingle) {
|
1211
1287
|
return true;
|
1212
1288
|
}
|
@@ -1219,7 +1295,7 @@ function is_single_result(expr, fromColumns) {
|
|
1219
1295
|
if (column_name && expr.ASSIGN()) {
|
1220
1296
|
const fieldName = (0, select_columns_1.splitName)(column_name.getText());
|
1221
1297
|
const column = (0, select_columns_1.findColumn)(fieldName, fromColumns);
|
1222
|
-
if (column.columnKey
|
1298
|
+
if (column.columnKey === 'PRI') {
|
1223
1299
|
return true;
|
1224
1300
|
}
|
1225
1301
|
}
|
@@ -1229,7 +1305,7 @@ function traverse_insert_stmt(insert_stmt, traverseContext) {
|
|
1229
1305
|
var _a;
|
1230
1306
|
const table_name = insert_stmt.table_name();
|
1231
1307
|
const fromColumns = (0, select_columns_1.filterColumns)(traverseContext.dbSchema, [], '', (0, select_columns_1.splitName)(table_name.getText()));
|
1232
|
-
const columns = insert_stmt.column_name_list().map(column_name => {
|
1308
|
+
const columns = insert_stmt.column_name_list().map((column_name) => {
|
1233
1309
|
return traverse_column_name(column_name, null, Object.assign(Object.assign({}, traverseContext), { fromColumns }));
|
1234
1310
|
});
|
1235
1311
|
const insertColumns = [];
|
@@ -1245,8 +1321,10 @@ function traverse_insert_stmt(insert_stmt, traverseContext) {
|
|
1245
1321
|
type1: col.columnType,
|
1246
1322
|
type2: exprType.type
|
1247
1323
|
});
|
1248
|
-
const notNullColumn =
|
1249
|
-
|
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 }));
|
1250
1328
|
});
|
1251
1329
|
});
|
1252
1330
|
});
|
@@ -1261,11 +1339,15 @@ function traverse_insert_stmt(insert_stmt, traverseContext) {
|
|
1261
1339
|
type1: col.columnType,
|
1262
1340
|
type2: selectColumn.type
|
1263
1341
|
});
|
1264
|
-
const notNullColumn =
|
1342
|
+
const notNullColumn = col.columnKey === 'PRI' && col.columnType.type === 'INTEGER'
|
1343
|
+
? false
|
1344
|
+
: col.notNull;
|
1265
1345
|
columnNullability.set(selectColumn.type.id, notNullColumn);
|
1266
1346
|
});
|
1267
|
-
traverseContext.parameters.forEach(param => {
|
1268
|
-
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 }));
|
1269
1351
|
});
|
1270
1352
|
}
|
1271
1353
|
const upsert_clause = insert_stmt.upsert_clause();
|
@@ -1276,19 +1358,25 @@ function traverse_insert_stmt(insert_stmt, traverseContext) {
|
|
1276
1358
|
const column_name = upsert_clause.column_name(index);
|
1277
1359
|
const col = traverse_column_name(column_name, null, Object.assign(Object.assign({}, traverseContext), { fromColumns }));
|
1278
1360
|
const expr = upsert_clause.expr(index);
|
1279
|
-
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) }));
|
1280
1366
|
traverseContext.constraints.push({
|
1281
1367
|
expression: column_name.getText(),
|
1282
1368
|
type1: col.columnType,
|
1283
1369
|
type2: exprType.type
|
1284
1370
|
});
|
1285
1371
|
});
|
1286
|
-
traverseContext.parameters.slice(paramsBefore).forEach(param => {
|
1372
|
+
traverseContext.parameters.slice(paramsBefore).forEach((param) => {
|
1287
1373
|
insertColumns.push(Object.assign(Object.assign({}, param), { notNull: param.notNull }));
|
1288
1374
|
});
|
1289
1375
|
}
|
1290
1376
|
const returning_clause = insert_stmt.returning_clause();
|
1291
|
-
const returninColumns = returning_clause
|
1377
|
+
const returninColumns = returning_clause
|
1378
|
+
? traverse_returning_clause(returning_clause, fromColumns)
|
1379
|
+
: [];
|
1292
1380
|
const queryResult = {
|
1293
1381
|
queryType: 'Insert',
|
1294
1382
|
constraints: traverseContext.constraints,
|
@@ -1300,9 +1388,9 @@ function traverse_insert_stmt(insert_stmt, traverseContext) {
|
|
1300
1388
|
}
|
1301
1389
|
function traverse_returning_clause(returning_clause, fromColumns) {
|
1302
1390
|
const result_column_list = returning_clause.result_column_list();
|
1303
|
-
const result = result_column_list.flatMap(result_column => {
|
1391
|
+
const result = result_column_list.flatMap((result_column) => {
|
1304
1392
|
if (result_column.STAR()) {
|
1305
|
-
return fromColumns.map(col => {
|
1393
|
+
return fromColumns.map((col) => {
|
1306
1394
|
const newCol = {
|
1307
1395
|
name: col.columnName,
|
1308
1396
|
type: col.columnType,
|
@@ -1319,9 +1407,10 @@ function traverse_returning_clause(returning_clause, fromColumns) {
|
|
1319
1407
|
function traverse_update_stmt(update_stmt, traverseContext) {
|
1320
1408
|
const table_name = update_stmt.qualified_table_name().getText();
|
1321
1409
|
const fromColumns = (0, select_columns_1.filterColumns)(traverseContext.dbSchema, [], '', (0, select_columns_1.splitName)(table_name));
|
1322
|
-
const column_name_list = Array.from({
|
1323
|
-
|
1324
|
-
|
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) => {
|
1325
1414
|
return traverse_column_name(column_name, null, Object.assign(Object.assign({}, traverseContext), { fromColumns }));
|
1326
1415
|
});
|
1327
1416
|
const updateColumns = [];
|
@@ -1331,7 +1420,8 @@ function traverse_update_stmt(update_stmt, traverseContext) {
|
|
1331
1420
|
expr_list.forEach((expr, index) => {
|
1332
1421
|
paramsBefore = traverseContext.parameters.length;
|
1333
1422
|
const exprType = traverse_expr(expr, Object.assign(Object.assign({}, traverseContext), { fromColumns }));
|
1334
|
-
if (!update_stmt.WHERE_() ||
|
1423
|
+
if (!update_stmt.WHERE_() ||
|
1424
|
+
expr.start.start < update_stmt.WHERE_().symbol.start) {
|
1335
1425
|
const col = columns[index];
|
1336
1426
|
traverseContext.constraints.push({
|
1337
1427
|
expression: expr.getText(),
|
@@ -1377,11 +1467,10 @@ function getAllColumns(expr) {
|
|
1377
1467
|
columns.push((0, select_columns_1.splitName)(expr1.getText()));
|
1378
1468
|
columns.push((0, select_columns_1.splitName)(expr2.getText()));
|
1379
1469
|
}
|
1380
|
-
;
|
1381
1470
|
return columns;
|
1382
1471
|
}
|
1383
1472
|
function filterUsingFields(fields, usingFields) {
|
1384
|
-
const result = fields.filter(field => !usingFields.includes(field.columnName));
|
1473
|
+
const result = fields.filter((field) => !usingFields.includes(field.columnName));
|
1385
1474
|
return result;
|
1386
1475
|
}
|
1387
1476
|
function getParamsIndexes(parameters, paramsIds) {
|
@@ -1389,7 +1478,7 @@ function getParamsIndexes(parameters, paramsIds) {
|
|
1389
1478
|
parameters.forEach((param, index) => {
|
1390
1479
|
map.set(param.paramIndex, index);
|
1391
1480
|
});
|
1392
|
-
return paramsIds.map(id => map.get(id));
|
1481
|
+
return paramsIds.map((id) => map.get(id));
|
1393
1482
|
}
|
1394
1483
|
function getWhereFragmentExpressions(whereExpr) {
|
1395
1484
|
const exprList = [];
|