typesql-cli 0.9.6 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/README.md +2 -3
  2. package/cli.js +76 -78
  3. package/cli.js.map +1 -1
  4. package/code-generator.d.ts +7 -7
  5. package/code-generator.d.ts.map +1 -1
  6. package/code-generator.js +193 -194
  7. package/code-generator.js.map +1 -1
  8. package/describe-dynamic-query.d.ts +1 -1
  9. package/describe-dynamic-query.d.ts.map +1 -1
  10. package/describe-dynamic-query.js +47 -42
  11. package/describe-dynamic-query.js.map +1 -1
  12. package/describe-nested-query.d.ts +2 -2
  13. package/describe-nested-query.d.ts.map +1 -1
  14. package/describe-nested-query.js +28 -25
  15. package/describe-nested-query.js.map +1 -1
  16. package/describe-query.d.ts +4 -4
  17. package/describe-query.d.ts.map +1 -1
  18. package/describe-query.js +24 -25
  19. package/describe-query.js.map +1 -1
  20. package/drivers/libsql.d.ts +2 -2
  21. package/drivers/libsql.d.ts.map +1 -1
  22. package/drivers/libsql.js +4 -5
  23. package/drivers/libsql.js.map +1 -1
  24. package/mysql-mapping.d.ts +1 -1
  25. package/mysql-mapping.d.ts.map +1 -1
  26. package/mysql-mapping.js +13 -13
  27. package/mysql-mapping.js.map +1 -1
  28. package/mysql-query-analyzer/collect-constraints.d.ts +4 -4
  29. package/mysql-query-analyzer/collect-constraints.d.ts.map +1 -1
  30. package/mysql-query-analyzer/collect-constraints.js +29 -31
  31. package/mysql-query-analyzer/collect-constraints.js.map +1 -1
  32. package/mysql-query-analyzer/infer-column-nullability.d.ts +2 -2
  33. package/mysql-query-analyzer/infer-column-nullability.d.ts.map +1 -1
  34. package/mysql-query-analyzer/infer-column-nullability.js +61 -63
  35. package/mysql-query-analyzer/infer-column-nullability.js.map +1 -1
  36. package/mysql-query-analyzer/infer-param-nullability.d.ts +2 -2
  37. package/mysql-query-analyzer/infer-param-nullability.d.ts.map +1 -1
  38. package/mysql-query-analyzer/infer-param-nullability.js +13 -14
  39. package/mysql-query-analyzer/infer-param-nullability.js.map +1 -1
  40. package/mysql-query-analyzer/parse.d.ts +2 -2
  41. package/mysql-query-analyzer/parse.d.ts.map +1 -1
  42. package/mysql-query-analyzer/parse.js +42 -40
  43. package/mysql-query-analyzer/parse.js.map +1 -1
  44. package/mysql-query-analyzer/select-columns.d.ts +4 -4
  45. package/mysql-query-analyzer/select-columns.d.ts.map +1 -1
  46. package/mysql-query-analyzer/select-columns.js +70 -65
  47. package/mysql-query-analyzer/select-columns.js.map +1 -1
  48. package/mysql-query-analyzer/traverse.d.ts +4 -4
  49. package/mysql-query-analyzer/traverse.d.ts.map +1 -1
  50. package/mysql-query-analyzer/traverse.js +211 -188
  51. package/mysql-query-analyzer/traverse.js.map +1 -1
  52. package/mysql-query-analyzer/types.d.ts +5 -5
  53. package/mysql-query-analyzer/types.d.ts.map +1 -1
  54. package/mysql-query-analyzer/unify.d.ts +2 -2
  55. package/mysql-query-analyzer/unify.d.ts.map +1 -1
  56. package/mysql-query-analyzer/unify.js +485 -487
  57. package/mysql-query-analyzer/unify.js.map +1 -1
  58. package/mysql-query-analyzer/util.d.ts.map +1 -1
  59. package/mysql-query-analyzer/util.js +4 -6
  60. package/mysql-query-analyzer/util.js.map +1 -1
  61. package/mysql-query-analyzer/verify-multiple-result.d.ts +2 -2
  62. package/mysql-query-analyzer/verify-multiple-result.d.ts.map +1 -1
  63. package/mysql-query-analyzer/verify-multiple-result.js +9 -9
  64. package/mysql-query-analyzer/verify-multiple-result.js.map +1 -1
  65. package/package.json +46 -44
  66. package/queryExectutor.d.ts +4 -4
  67. package/queryExectutor.d.ts.map +1 -1
  68. package/queryExectutor.js +48 -51
  69. package/queryExectutor.js.map +1 -1
  70. package/sql-generator.d.ts +2 -2
  71. package/sql-generator.d.ts.map +1 -1
  72. package/sql-generator.js +30 -29
  73. package/sql-generator.js.map +1 -1
  74. package/sqlite-query-analyzer/code-generator.d.ts +6 -6
  75. package/sqlite-query-analyzer/code-generator.d.ts.map +1 -1
  76. package/sqlite-query-analyzer/code-generator.js +226 -177
  77. package/sqlite-query-analyzer/code-generator.js.map +1 -1
  78. package/sqlite-query-analyzer/parser.d.ts +4 -4
  79. package/sqlite-query-analyzer/parser.d.ts.map +1 -1
  80. package/sqlite-query-analyzer/parser.js +17 -18
  81. package/sqlite-query-analyzer/parser.js.map +1 -1
  82. package/sqlite-query-analyzer/query-executor.d.ts +6 -6
  83. package/sqlite-query-analyzer/query-executor.d.ts.map +1 -1
  84. package/sqlite-query-analyzer/query-executor.js +42 -33
  85. package/sqlite-query-analyzer/query-executor.js.map +1 -1
  86. package/sqlite-query-analyzer/replace-list-params.d.ts +1 -1
  87. package/sqlite-query-analyzer/replace-list-params.d.ts.map +1 -1
  88. package/sqlite-query-analyzer/replace-list-params.js +5 -5
  89. package/sqlite-query-analyzer/replace-list-params.js.map +1 -1
  90. package/sqlite-query-analyzer/sqlite-describe-nested-query.d.ts +4 -4
  91. package/sqlite-query-analyzer/sqlite-describe-nested-query.d.ts.map +1 -1
  92. package/sqlite-query-analyzer/sqlite-describe-nested-query.js +19 -17
  93. package/sqlite-query-analyzer/sqlite-describe-nested-query.js.map +1 -1
  94. package/sqlite-query-analyzer/traverse.d.ts +5 -5
  95. package/sqlite-query-analyzer/traverse.d.ts.map +1 -1
  96. package/sqlite-query-analyzer/traverse.js +185 -155
  97. package/sqlite-query-analyzer/traverse.js.map +1 -1
  98. package/ts-dynamic-query-descriptor.d.ts +1 -1
  99. package/ts-dynamic-query-descriptor.d.ts.map +1 -1
  100. package/ts-dynamic-query-descriptor.js +7 -8
  101. package/ts-dynamic-query-descriptor.js.map +1 -1
  102. package/ts-nested-descriptor.d.ts +4 -4
  103. package/ts-nested-descriptor.d.ts.map +1 -1
  104. package/ts-nested-descriptor.js +13 -14
  105. package/ts-nested-descriptor.js.map +1 -1
  106. package/types.d.ts +18 -15
  107. package/types.d.ts.map +1 -1
  108. package/util.d.ts.map +1 -1
  109. package/util.js.map +1 -1
  110. package/utility-types.d.ts.map +1 -1
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isMultipleRowResult = exports.isNotNull = exports.tryTraverse_Sql_stmtContext = void 0;
3
+ exports.tryTraverse_Sql_stmtContext = tryTraverse_Sql_stmtContext;
4
+ exports.isNotNull = isNotNull;
5
+ exports.isMultipleRowResult = isMultipleRowResult;
4
6
  const sqlite_1 = require("@wsporto/ts-mysql-parser/dist/sqlite");
5
7
  const select_columns_1 = require("../mysql-query-analyzer/select-columns");
6
8
  const collect_constraints_1 = require("../mysql-query-analyzer/collect-constraints");
@@ -27,7 +29,7 @@ function traverse_Sql_stmtContext(sql_stmt, traverseContext) {
27
29
  const deleteResult = traverse_delete_stmt(delete_stmt, traverseContext);
28
30
  return deleteResult;
29
31
  }
30
- throw Error("traverse_Sql_stmtContext");
32
+ throw Error('traverse_Sql_stmtContext');
31
33
  }
32
34
  function tryTraverse_Sql_stmtContext(sql_stmt, traverseContext) {
33
35
  try {
@@ -42,22 +44,21 @@ function tryTraverse_Sql_stmtContext(sql_stmt, traverseContext) {
42
44
  });
43
45
  }
44
46
  }
45
- exports.tryTraverse_Sql_stmtContext = tryTraverse_Sql_stmtContext;
46
47
  function traverse_select_stmt(select_stmt, traverseContext, subQuery = false, recursive = false, recursiveNames = []) {
47
48
  const common_table_stmt = select_stmt.common_table_stmt();
48
49
  if (common_table_stmt) {
49
50
  const recursive = common_table_stmt.RECURSIVE_() != null;
50
51
  const common_table_expression = common_table_stmt.common_table_expression_list();
51
- common_table_expression.forEach(common_table_expression => {
52
+ common_table_expression.forEach((common_table_expression) => {
52
53
  const table_name = common_table_expression.table_name();
53
- const recursiveNames = common_table_expression.column_name_list().map(column_name => column_name.getText());
54
+ const recursiveNames = common_table_expression.column_name_list().map((column_name) => column_name.getText());
54
55
  const select_stmt = common_table_expression.select_stmt();
55
56
  const select_stmt_result = traverse_select_stmt(select_stmt, Object.assign(Object.assign({}, traverseContext), { subQuery: true }), subQuery, recursive, recursiveNames);
56
57
  select_stmt_result.columns.forEach((col, index) => {
57
58
  var _a;
58
59
  traverseContext.withSchema.push({
59
60
  table: table_name.getText(),
60
- columnName: recursive ? (_a = recursiveNames[index]) !== null && _a !== void 0 ? _a : col.name : col.name,
61
+ columnName: recursive ? ((_a = recursiveNames[index]) !== null && _a !== void 0 ? _a : col.name) : col.name,
61
62
  columnType: col.type,
62
63
  columnKey: '',
63
64
  notNull: col.notNull
@@ -72,8 +73,10 @@ function traverse_select_stmt(select_stmt, traverseContext, subQuery = false, re
72
73
  }
73
74
  const [mainSelect, ...unionSelect] = select_stmt.select_core_list();
74
75
  const mainQueryResult = traverse_select_core(mainSelect, traverseContext, subQuery, recursive, recursiveNames);
75
- const fromColumns = recursive ? mainQueryResult.columns.map((col, index) => mapTypeAndNullInferToColumnDef(col, recursiveNames[index])) : traverseContext.fromColumns;
76
- unionSelect.forEach(select_core => {
76
+ const fromColumns = recursive
77
+ ? mainQueryResult.columns.map((col, index) => mapTypeAndNullInferToColumnDef(col, recursiveNames[index]))
78
+ : traverseContext.fromColumns;
79
+ unionSelect.forEach((select_core) => {
77
80
  const unionResult = traverse_select_core(select_core, Object.assign(Object.assign({}, traverseContext), { fromColumns }), subQuery, recursive);
78
81
  unionResult.columns.forEach((col, colIndex) => {
79
82
  mainQueryResult.columns[colIndex].table = '';
@@ -99,9 +102,9 @@ function traverse_select_stmt(select_stmt, traverseContext, subQuery = false, re
99
102
  if (order_by_stmt) {
100
103
  const selectColumns = mainQueryResult.columns.map((col, index) => mapTypeAndNullInferToColumnDef(col, recursiveNames[index]));
101
104
  const ordering_term_list = order_by_stmt.ordering_term_list();
102
- ordering_term_list.forEach(ordering_term => {
105
+ ordering_term_list.forEach((ordering_term) => {
103
106
  const expr = ordering_term.expr();
104
- if (expr.getText() == '?') {
107
+ if (expr.getText() === '?') {
105
108
  hasOrderByParameter = true;
106
109
  }
107
110
  else {
@@ -129,7 +132,7 @@ function traverse_select_stmt(select_stmt, traverseContext, subQuery = false, re
129
132
  type1: exrp1Type.type,
130
133
  type2: (0, collect_constraints_1.freshVar)('INTEGER', 'INTEGER')
131
134
  });
132
- if (expr_list.length == 2) {
135
+ if (expr_list.length === 2) {
133
136
  const expr2 = expr_list[1];
134
137
  const exrp2Type = traverse_expr(expr2, traverseContext);
135
138
  exrp2Type.notNull = true;
@@ -174,11 +177,11 @@ function traverse_select_core(select_core, traverseContext, subQuery = false, re
174
177
  }
175
178
  const result_column = select_core.result_column_list();
176
179
  const fromColumns = subQuery || recursive ? traverseContext.fromColumns.concat(columnsResult) : columnsResult;
177
- result_column.forEach(result_column => {
180
+ result_column.forEach((result_column) => {
178
181
  var _a, _b;
179
182
  if (result_column.STAR()) {
180
183
  const tableName = (_a = result_column.table_name()) === null || _a === void 0 ? void 0 : _a.getText();
181
- columnsResult.forEach(col => {
184
+ columnsResult.forEach((col) => {
182
185
  const table = col.tableAlias || col.table;
183
186
  if (!tableName || (0, select_columns_1.includeColumn)(col, tableName)) {
184
187
  listType.push({
@@ -203,11 +206,13 @@ function traverse_select_core(select_core, traverseContext, subQuery = false, re
203
206
  if (expr) {
204
207
  const exprType = traverse_expr(expr, Object.assign(Object.assign({}, traverseContext), { fromColumns: fromColumns }));
205
208
  if (alias) {
206
- traverseContext.relations.filter(relation => relation.joinColumn == exprType.name && (relation.name == exprType.table || relation.alias == exprType.table)).forEach(relation => {
209
+ traverseContext.relations
210
+ .filter((relation) => relation.joinColumn === exprType.name && (relation.name === exprType.table || relation.alias === exprType.table))
211
+ .forEach((relation) => {
207
212
  relation.joinColumn = alias;
208
213
  });
209
214
  }
210
- if (exprType.type.kind == 'TypeVar') {
215
+ if (exprType.type.kind === 'TypeVar') {
211
216
  if (alias) {
212
217
  exprType.name = alias;
213
218
  }
@@ -236,40 +241,44 @@ function traverse_select_core(select_core, traverseContext, subQuery = false, re
236
241
  });
237
242
  }
238
243
  }
239
- const newColumns = listType.map(selectField => {
244
+ const newColumns = listType.map((selectField) => {
240
245
  const col = {
241
246
  columnName: selectField.name,
242
247
  table: selectField.table,
243
248
  columnType: selectField.type,
244
249
  notNull: selectField.notNull,
245
- columnKey: ""
250
+ columnKey: ''
246
251
  };
247
252
  return col;
248
253
  });
249
254
  const groupByExprList = select_core._groupByExpr || [];
250
- groupByExprList.forEach(groupByExpr => {
255
+ groupByExprList.forEach((groupByExpr) => {
251
256
  traverse_expr(groupByExpr, Object.assign(Object.assign({}, traverseContext), { fromColumns: newColumns.concat(fromColumns) }));
252
257
  });
253
258
  const havingExpr = select_core._havingExpr;
254
259
  if (havingExpr) {
255
- //select have precedence: newColumns.concat(fromColumns)
260
+ //select have precedence: newColumns.concat(fromColumns)
256
261
  traverse_expr(havingExpr, Object.assign(Object.assign({}, traverseContext), { fromColumns: newColumns.concat(fromColumns) }));
257
262
  }
258
263
  const querySpecification = {
259
- columns: listType.map(col => (Object.assign(Object.assign({}, col), { notNull: col.notNull || isNotNull(col.name, whereExpr) || isNotNull(col.name, havingExpr) }))),
264
+ columns: listType.map((col) => (Object.assign(Object.assign({}, col), { notNull: col.notNull || isNotNull(col.name, whereExpr) || isNotNull(col.name, havingExpr) }))),
260
265
  fromColumns: columnsResult //TODO - return isMultipleRowResult instead
261
266
  };
262
267
  return querySpecification;
263
268
  }
264
269
  function extractRelationsAndParams(expr, fromColumns, parameters) {
265
270
  const columnsRef = (0, select_columns_1.getExpressions)(expr, sqlite_1.Column_nameContext);
266
- const relations = columnsRef.filter(expr => !expr.isSubQuery).map(colRef => {
271
+ const relations = columnsRef
272
+ .filter((expr) => !expr.isSubQuery)
273
+ .map((colRef) => {
267
274
  const fieldName = (0, select_columns_1.splitName)(colRef.expr.parentCtx.getText());
268
275
  const column = (0, select_columns_1.findColumn)(fieldName, fromColumns);
269
276
  return column.tableAlias || column.table;
270
277
  });
271
278
  const expressionList = (0, select_columns_1.getExpressions)(expr, sqlite_1.ExprContext);
272
- const paramsIds = expressionList.filter(expr => expr.expr.BIND_PARAMETER() != null).map(expr => expr.expr.BIND_PARAMETER().symbol.start);
279
+ const paramsIds = expressionList
280
+ .filter((expr) => expr.expr.BIND_PARAMETER() != null)
281
+ .map((expr) => expr.expr.BIND_PARAMETER().symbol.start);
273
282
  const params = getParamsIndexes(parameters, paramsIds);
274
283
  return {
275
284
  relations,
@@ -286,12 +295,14 @@ function traverse_table_or_subquery(table_or_subquery_list, join_constraint_list
286
295
  const table_alias_temp = ((_b = table_or_subquery.table_alias()) === null || _b === void 0 ? void 0 : _b.getText()) || '';
287
296
  let tableOrSubqueryFields = [];
288
297
  //grammar error: select * from table1 inner join table2....; inner is parsed as table_alias
289
- let table_alias = table_alias_temp.toLowerCase() == 'left'
290
- || table_alias_temp.toLowerCase() == 'right'
291
- || table_alias_temp.toLowerCase() == 'full'
292
- || table_alias_temp.toLowerCase() == 'outer'
293
- || table_alias_temp.toLowerCase() == 'inner'
294
- || table_alias_temp.toLowerCase() == 'cross' ? '' : table_alias_temp;
298
+ const table_alias = table_alias_temp.toLowerCase() === 'left' ||
299
+ table_alias_temp.toLowerCase() === 'right' ||
300
+ table_alias_temp.toLowerCase() === 'full' ||
301
+ table_alias_temp.toLowerCase() === 'outer' ||
302
+ table_alias_temp.toLowerCase() === 'inner' ||
303
+ table_alias_temp.toLowerCase() === 'cross'
304
+ ? ''
305
+ : table_alias_temp;
295
306
  const join_constraint = join_constraint_list && index > 0 ? join_constraint_list[index - 1] : undefined;
296
307
  const asAlias = table_or_subquery.AS_() || false;
297
308
  const tableAlias = (_c = table_or_subquery.table_alias()) === null || _c === void 0 ? void 0 : _c.getText();
@@ -303,10 +314,10 @@ function traverse_table_or_subquery(table_or_subquery_list, join_constraint_list
303
314
  prefix: schema
304
315
  };
305
316
  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_()) ? join_constraint === null || join_constraint === void 0 ? void 0 : join_constraint.column_name_list().map(column_name => column_name.getText()) : [];
317
+ const usingFields = (join_constraint === null || join_constraint === void 0 ? void 0 : join_constraint.USING_()) ? join_constraint === null || join_constraint === void 0 ? void 0 : join_constraint.column_name_list().map((column_name) => column_name.getText()) : [];
307
318
  const filteredFields = usingFields.length > 0 ? filterUsingFields(tableOrSubqueryFields, usingFields) : tableOrSubqueryFields;
308
319
  if (isLeftJoin) {
309
- allFields.push(...filteredFields.map(field => (Object.assign(Object.assign({}, field), { notNull: false }))));
320
+ allFields.push(...filteredFields.map((field) => (Object.assign(Object.assign({}, field), { notNull: false }))));
310
321
  }
311
322
  else {
312
323
  allFields.push(...filteredFields);
@@ -315,12 +326,12 @@ function traverse_table_or_subquery(table_or_subquery_list, join_constraint_list
315
326
  const select_stmt = table_or_subquery.select_stmt();
316
327
  if (select_stmt) {
317
328
  const subQueryResult = traverse_select_stmt(select_stmt, Object.assign(Object.assign({}, traverseContext), { subQuery: true }));
318
- tableOrSubqueryFields = subQueryResult.columns.map(t => {
329
+ tableOrSubqueryFields = subQueryResult.columns.map((t) => {
319
330
  const colDef = {
320
331
  table: t.table ? tableAlias || '' : '',
321
332
  columnName: t.name,
322
333
  columnType: t.type,
323
- columnKey: "",
334
+ columnKey: '',
324
335
  notNull: t.notNull,
325
336
  tableAlias: tableAlias
326
337
  };
@@ -333,7 +344,7 @@ function traverse_table_or_subquery(table_or_subquery_list, join_constraint_list
333
344
  tableOrSubqueryFields = traverse_table_or_subquery(table_or_subquery_list2, null, null, traverseContext);
334
345
  allFields.push(...tableOrSubqueryFields);
335
346
  }
336
- const idColumn = (_e = tableOrSubqueryFields.find(field => field.columnKey == 'PRI')) === null || _e === void 0 ? void 0 : _e.columnName;
347
+ const idColumn = (_e = tableOrSubqueryFields.find((field) => field.columnKey === 'PRI')) === null || _e === void 0 ? void 0 : _e.columnName;
337
348
  const relation = {
338
349
  name: asAlias ? table_alias : tableOrSubqueryName,
339
350
  alias: table_alias,
@@ -342,23 +353,24 @@ function traverse_table_or_subquery(table_or_subquery_list, join_constraint_list
342
353
  parentCardinality: 'one',
343
354
  joinColumn: idColumn
344
355
  };
345
- if (join_constraint) { //index 0 is the FROM (root relation)
356
+ if (join_constraint) {
357
+ //index 0 is the FROM (root relation)
346
358
  const expr = join_constraint.expr(); //ON expr
347
359
  if (expr) {
348
360
  traverse_expr(expr, Object.assign(Object.assign({}, traverseContext), { fromColumns: allFields }));
349
361
  const allJoinColumsn = getAllColumns(expr);
350
- allJoinColumsn.forEach(joinColumn => {
351
- const column = allFields.find(col => col.columnName == joinColumn.name && (col.tableAlias == joinColumn.prefix || col.table == joinColumn.prefix));
352
- const filterUniqueKeys = allFields.filter(col => (joinColumn.prefix == col.table || joinColumn.prefix == col.tableAlias) && (col.columnKey == 'PRI'));
353
- const compositeKey = filterUniqueKeys.find(uni => uni.columnName == column.columnName);
354
- const notUnique = (filterUniqueKeys.length > 1 && compositeKey) || ((column === null || column === void 0 ? void 0 : column.columnKey) != 'UNI' && (column === null || column === void 0 ? void 0 : column.columnKey) != 'PRI');
355
- if (joinColumn.prefix != relation.name && joinColumn.prefix != relation.alias) {
362
+ allJoinColumsn.forEach((joinColumn) => {
363
+ const column = allFields.find((col) => col.columnName === joinColumn.name && (col.tableAlias === joinColumn.prefix || col.table === joinColumn.prefix));
364
+ const filterUniqueKeys = allFields.filter((col) => (joinColumn.prefix === col.table || joinColumn.prefix === col.tableAlias) && col.columnKey === 'PRI');
365
+ const compositeKey = filterUniqueKeys.find((uni) => uni.columnName === column.columnName);
366
+ const notUnique = (filterUniqueKeys.length > 1 && compositeKey) || ((column === null || column === void 0 ? void 0 : column.columnKey) !== 'UNI' && (column === null || column === void 0 ? void 0 : column.columnKey) !== 'PRI');
367
+ if (joinColumn.prefix !== relation.name && joinColumn.prefix !== relation.alias) {
356
368
  relation.parentRelation = joinColumn.prefix;
357
369
  if (notUnique) {
358
370
  relation.parentCardinality = 'many';
359
371
  }
360
372
  }
361
- if (joinColumn.prefix == relation.name || joinColumn.prefix == relation.alias) {
373
+ if (joinColumn.prefix === relation.name || joinColumn.prefix === relation.alias) {
362
374
  if (notUnique) {
363
375
  relation.cardinality = 'many';
364
376
  }
@@ -369,16 +381,14 @@ function traverse_table_or_subquery(table_or_subquery_list, join_constraint_list
369
381
  if (!traverseContext.subQuery) {
370
382
  traverseContext.relations.push(relation);
371
383
  //dynamic query
372
- const fragment = (join_operator_list != null && index > 0 ? extractOriginalSql(join_operator_list[index - 1]) : 'FROM')
373
- + ' ' + extractOriginalSql(table_or_subquery_list[index])
374
- + (join_constraint != null ? ' ' + extractOriginalSql(join_constraint) : '');
384
+ const fragment = `${join_operator_list != null && index > 0 ? extractOriginalSql(join_operator_list[index - 1]) : 'FROM'} ${extractOriginalSql(table_or_subquery_list[index])}${join_constraint != null ? ` ${extractOriginalSql(join_constraint)}` : ''}`;
375
385
  const params = traverseContext.parameters.slice(numParamsBefore).map((_, index) => index + numParamsBefore);
376
386
  traverseContext.dynamicSqlInfo2.from.push({
377
387
  fragment: fragment,
378
388
  relationName: relation.name,
379
389
  relationAlias: relation.alias,
380
390
  parentRelation: relation.parentRelation,
381
- fields: tableOrSubqueryFields.map(field => field.columnName),
391
+ fields: tableOrSubqueryFields.map((field) => field.columnName),
382
392
  parameters: params
383
393
  });
384
394
  }
@@ -388,11 +398,11 @@ function traverse_table_or_subquery(table_or_subquery_list, join_constraint_list
388
398
  function traverse_expr(expr, traverseContext) {
389
399
  var _a, _b;
390
400
  const function_name = (_a = expr.function_name()) === null || _a === void 0 ? void 0 : _a.getText().toLowerCase();
391
- if (function_name == 'avg') {
401
+ if (function_name === 'avg') {
392
402
  const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'REAL');
393
403
  const sumParamExpr = expr.expr(0);
394
404
  const paramType = traverse_expr(sumParamExpr, traverseContext);
395
- if (paramType.type.kind == 'TypeVar') {
405
+ if (paramType.type.kind === 'TypeVar') {
396
406
  functionType.table = paramType.table;
397
407
  }
398
408
  return {
@@ -402,7 +412,7 @@ function traverse_expr(expr, traverseContext) {
402
412
  table: functionType.table || ''
403
413
  };
404
414
  }
405
- if (function_name == 'sum') {
415
+ if (function_name === 'sum') {
406
416
  const sumParamExpr = expr.expr(0);
407
417
  const paramType = traverse_expr(sumParamExpr, traverseContext);
408
418
  return {
@@ -412,7 +422,7 @@ function traverse_expr(expr, traverseContext) {
412
422
  table: paramType.table || ''
413
423
  };
414
424
  }
415
- if (function_name == 'min' || function_name == 'max') {
425
+ if (function_name === 'min' || function_name === 'max') {
416
426
  const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
417
427
  const sumParamExpr = expr.expr(0);
418
428
  const paramType = traverse_expr(sumParamExpr, traverseContext);
@@ -428,12 +438,12 @@ function traverse_expr(expr, traverseContext) {
428
438
  table: functionType.table || ''
429
439
  };
430
440
  }
431
- if (function_name == 'count') {
441
+ if (function_name === 'count') {
432
442
  const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'INTEGER');
433
- if (expr.expr_list().length == 1) {
443
+ if (expr.expr_list().length === 1) {
434
444
  const sumParamExpr = expr.expr(0);
435
445
  const paramType = traverse_expr(sumParamExpr, traverseContext);
436
- if (paramType.type.kind == 'TypeVar') {
446
+ if (paramType.type.kind === 'TypeVar') {
437
447
  functionType.table = paramType.table;
438
448
  }
439
449
  }
@@ -444,16 +454,16 @@ function traverse_expr(expr, traverseContext) {
444
454
  table: functionType.table || ''
445
455
  };
446
456
  }
447
- if (function_name == 'concat') {
457
+ if (function_name === 'concat') {
448
458
  const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'TEXT');
449
- expr.expr_list().forEach(paramExpr => {
459
+ expr.expr_list().forEach((paramExpr) => {
450
460
  const paramType = traverse_expr(paramExpr, traverseContext);
451
461
  traverseContext.constraints.push({
452
462
  expression: expr.getText(),
453
463
  type1: functionType,
454
464
  type2: paramType.type
455
465
  });
456
- if (paramType.type.kind == 'TypeVar') {
466
+ if (paramType.type.kind === 'TypeVar') {
457
467
  functionType.table = paramType.table;
458
468
  }
459
469
  });
@@ -464,7 +474,7 @@ function traverse_expr(expr, traverseContext) {
464
474
  table: functionType.table || ''
465
475
  };
466
476
  }
467
- if (function_name == 'length') {
477
+ if (function_name === 'length') {
468
478
  const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'INTEGER');
469
479
  const paramExpr = expr.expr(0);
470
480
  const paramType = traverse_expr(paramExpr, traverseContext);
@@ -473,7 +483,7 @@ function traverse_expr(expr, traverseContext) {
473
483
  type1: (0, collect_constraints_1.freshVar)(expr.getText(), '?'), //str or blob
474
484
  type2: paramType.type
475
485
  });
476
- if (paramType.type.kind == 'TypeVar') {
486
+ if (paramType.type.kind === 'TypeVar') {
477
487
  functionType.table = paramType.table;
478
488
  }
479
489
  return {
@@ -483,8 +493,8 @@ function traverse_expr(expr, traverseContext) {
483
493
  table: functionType.table || ''
484
494
  };
485
495
  }
486
- if (function_name == 'group_concat') {
487
- expr.expr_list().forEach(paramExpr => {
496
+ if (function_name === 'group_concat') {
497
+ expr.expr_list().forEach((paramExpr) => {
488
498
  const param1Type = traverse_expr(paramExpr, traverseContext);
489
499
  traverseContext.constraints.push({
490
500
  expression: expr.getText(),
@@ -499,7 +509,16 @@ function traverse_expr(expr, traverseContext) {
499
509
  table: ''
500
510
  };
501
511
  }
502
- if (function_name == 'round') {
512
+ if (function_name === 'random') {
513
+ const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'INTEGER');
514
+ return {
515
+ name: functionType.name,
516
+ type: functionType,
517
+ notNull: true,
518
+ table: ''
519
+ };
520
+ }
521
+ if (function_name === 'round') {
503
522
  const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'REAL');
504
523
  const param1Expr = expr.expr(0);
505
524
  const param1Type = traverse_expr(param1Expr, traverseContext);
@@ -524,9 +543,9 @@ function traverse_expr(expr, traverseContext) {
524
543
  table: param1Type.table || ''
525
544
  };
526
545
  }
527
- if (function_name == 'coalesce') {
546
+ if (function_name === 'coalesce') {
528
547
  const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
529
- const paramTypes = expr.expr_list().map(paramExpr => {
548
+ const paramTypes = expr.expr_list().map((paramExpr) => {
530
549
  const paramType = traverse_expr(paramExpr, traverseContext);
531
550
  traverseContext.constraints.push({
532
551
  expression: expr.getText(),
@@ -538,11 +557,11 @@ function traverse_expr(expr, traverseContext) {
538
557
  return {
539
558
  name: functionType.name,
540
559
  type: functionType,
541
- notNull: paramTypes.some(param => param.notNull),
560
+ notNull: paramTypes.some((param) => param.notNull),
542
561
  table: functionType.table || ''
543
562
  };
544
563
  }
545
- if (function_name == 'strftime') {
564
+ if (function_name === 'strftime') {
546
565
  const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'TEXT');
547
566
  const paramExpr = expr.expr(1);
548
567
  const paramType = traverse_expr(paramExpr, traverseContext);
@@ -559,7 +578,7 @@ function traverse_expr(expr, traverseContext) {
559
578
  table: functionType.table || ''
560
579
  };
561
580
  }
562
- if (function_name == 'date' || function_name == 'time' || function_name == 'datetime') {
581
+ if (function_name === 'date' || function_name === 'time' || function_name === 'datetime') {
563
582
  const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'DATE');
564
583
  const paramExpr = expr.expr(0);
565
584
  const paramType = traverse_expr(paramExpr, traverseContext);
@@ -576,10 +595,10 @@ function traverse_expr(expr, traverseContext) {
576
595
  table: functionType.table || ''
577
596
  };
578
597
  }
579
- if (function_name == 'julianday') {
598
+ if (function_name === 'julianday') {
580
599
  const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'REAL');
581
600
  const paramExpr = expr.expr(0);
582
- const notNull = paramExpr.getText().toLowerCase() == `'now'` ? true : false;
601
+ const notNull = paramExpr.getText().toLowerCase() === `'now'`;
583
602
  const paramType = traverse_expr(paramExpr, traverseContext);
584
603
  traverseContext.constraints.push({
585
604
  expression: paramExpr.getText(),
@@ -593,10 +612,10 @@ function traverse_expr(expr, traverseContext) {
593
612
  table: functionType.table || ''
594
613
  };
595
614
  }
596
- if (function_name == 'unixepoch') {
615
+ if (function_name === 'unixepoch') {
597
616
  const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'INTEGER');
598
617
  const paramExpr = expr.expr(0);
599
- const notNull = paramExpr.getText().toLowerCase() == `'now'` ? true : false;
618
+ const notNull = paramExpr.getText().toLowerCase() === `'now'`;
600
619
  const paramType = traverse_expr(paramExpr, traverseContext);
601
620
  traverseContext.constraints.push({
602
621
  expression: paramExpr.getText(),
@@ -610,11 +629,11 @@ function traverse_expr(expr, traverseContext) {
610
629
  table: functionType.table || ''
611
630
  };
612
631
  }
613
- if (function_name == 'ifnull') {
632
+ if (function_name === 'ifnull') {
614
633
  const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
615
- const paramTypes = expr.expr_list().map(paramExpr => {
634
+ const paramTypes = expr.expr_list().map((paramExpr) => {
616
635
  const paramType = traverse_expr(paramExpr, traverseContext);
617
- if (paramType.name == '?') {
636
+ if (paramType.name === '?') {
618
637
  paramType.notNull = false;
619
638
  }
620
639
  traverseContext.constraints.push({
@@ -627,13 +646,11 @@ function traverse_expr(expr, traverseContext) {
627
646
  return {
628
647
  name: functionType.name,
629
648
  type: functionType,
630
- notNull: paramTypes.every(param => param.notNull),
649
+ notNull: paramTypes.every((param) => param.notNull),
631
650
  table: functionType.table || ''
632
651
  };
633
652
  }
634
- if (function_name == 'row_number'
635
- || function_name == 'rank'
636
- || function_name == 'dense_rank') {
653
+ if (function_name === 'row_number' || function_name === 'rank' || function_name === 'dense_rank') {
637
654
  const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'INTEGER');
638
655
  return {
639
656
  name: functionType.name,
@@ -642,19 +659,17 @@ function traverse_expr(expr, traverseContext) {
642
659
  table: functionType.table || ''
643
660
  };
644
661
  }
645
- if (function_name == 'first_value'
646
- || function_name == 'last_value') {
662
+ if (function_name === 'first_value' || function_name === 'last_value') {
647
663
  const paramExpr = expr.expr(0);
648
664
  const paramType = traverse_expr(paramExpr, traverseContext);
649
665
  return paramType;
650
666
  }
651
- if (function_name == 'lead'
652
- || function_name == 'lag') {
667
+ if (function_name === 'lead' || function_name === 'lag') {
653
668
  const paramExpr = expr.expr(0);
654
669
  const paramType = traverse_expr(paramExpr, traverseContext);
655
670
  return Object.assign(Object.assign({}, paramType), { notNull: false });
656
671
  }
657
- if (function_name == 'iif') {
672
+ if (function_name === 'iif') {
658
673
  const expr1 = expr.expr(0);
659
674
  traverse_expr(expr1, traverseContext);
660
675
  const expr2 = expr.expr(1);
@@ -668,7 +683,7 @@ function traverse_expr(expr, traverseContext) {
668
683
  });
669
684
  return Object.assign(Object.assign({}, expr2Type), { notNull: expr2Type.notNull && expr3Type.notNull });
670
685
  }
671
- if (function_name == 'vector') {
686
+ if (function_name === 'vector') {
672
687
  const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'BLOB');
673
688
  const param1Expr = expr.expr(0);
674
689
  const param1Type = traverse_expr(param1Expr, traverseContext);
@@ -685,7 +700,7 @@ function traverse_expr(expr, traverseContext) {
685
700
  table: ''
686
701
  };
687
702
  }
688
- if (function_name == 'vector_extract') {
703
+ if (function_name === 'vector_extract') {
689
704
  const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'TEXT');
690
705
  const param1Expr = expr.expr(0);
691
706
  const param1Type = traverse_expr(param1Expr, traverseContext);
@@ -701,7 +716,7 @@ function traverse_expr(expr, traverseContext) {
701
716
  table: ''
702
717
  };
703
718
  }
704
- if (function_name == 'vector_distance_cos') {
719
+ if (function_name === 'vector_distance_cos') {
705
720
  const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'REAL');
706
721
  const param1Expr = expr.expr(0);
707
722
  if (param1Expr) {
@@ -731,7 +746,7 @@ function traverse_expr(expr, traverseContext) {
731
746
  };
732
747
  }
733
748
  if (function_name) {
734
- throw Error('traverse_expr: function not supported:' + function_name);
749
+ throw Error(`traverse_expr: function not supported:${function_name}`);
735
750
  }
736
751
  const column_name = expr.column_name();
737
752
  const table_name = expr.table_name();
@@ -768,7 +783,7 @@ function traverse_expr(expr, traverseContext) {
768
783
  return {
769
784
  name: type.name,
770
785
  type: type,
771
- notNull: literal.NULL_() != null ? false : true,
786
+ notNull: literal.NULL_() == null,
772
787
  table: type.table || ''
773
788
  };
774
789
  }
@@ -821,8 +836,7 @@ function traverse_expr(expr, traverseContext) {
821
836
  type1: returnType,
822
837
  type2: typeLeft.type
823
838
  });
824
- const isDateFunctionContext = (expr.parentCtx instanceof sqlite_1.ExprContext)
825
- && ((_b = expr.parentCtx.function_name()) === null || _b === void 0 ? void 0 : _b.getText().toLowerCase()) == 'date';
839
+ const isDateFunctionContext = expr.parentCtx instanceof sqlite_1.ExprContext && ((_b = expr.parentCtx.function_name()) === null || _b === void 0 ? void 0 : _b.getText().toLowerCase()) === 'date';
826
840
  if (!isDateFunctionContext) {
827
841
  traverseContext.constraints.push({
828
842
  expression: exprRight.getText(),
@@ -832,15 +846,24 @@ function traverse_expr(expr, traverseContext) {
832
846
  }
833
847
  return Object.assign(Object.assign({}, typeLeft), { notNull: typeLeft.notNull && typeRight.notNull });
834
848
  }
835
- if (expr.LT2() || expr.GT2() || expr.AMP() || expr.PIPE() || expr.LT() || expr.LT_EQ() || expr.GT() || expr.GT_EQ() || expr.NOT_EQ1() || expr.NOT_EQ2()) {
849
+ if (expr.LT2() ||
850
+ expr.GT2() ||
851
+ expr.AMP() ||
852
+ expr.PIPE() ||
853
+ expr.LT() ||
854
+ expr.LT_EQ() ||
855
+ expr.GT() ||
856
+ expr.GT_EQ() ||
857
+ expr.NOT_EQ1() ||
858
+ expr.NOT_EQ2()) {
836
859
  const exprLeft = expr.expr(0);
837
860
  const exprRight = expr.expr(1);
838
861
  const typeLeft = traverse_expr(exprLeft, traverseContext);
839
862
  const typeRight = traverse_expr(exprRight, traverseContext);
840
- if (typeLeft.name == '?') {
863
+ if (typeLeft.name === '?') {
841
864
  typeLeft.notNull = true;
842
865
  }
843
- if (typeRight.name == '?') {
866
+ if (typeRight.name === '?') {
844
867
  typeRight.notNull = true;
845
868
  }
846
869
  traverseContext.constraints.push({
@@ -856,7 +879,8 @@ function traverse_expr(expr, traverseContext) {
856
879
  table: resultType.table || ''
857
880
  };
858
881
  }
859
- if (expr.IS_()) { //is null/is not null
882
+ if (expr.IS_()) {
883
+ //is null/is not null
860
884
  const expr_ = expr.expr(0);
861
885
  traverse_expr(expr_, traverseContext);
862
886
  const type = (0, collect_constraints_1.freshVar)(expr.getText(), 'INTEGER');
@@ -867,15 +891,16 @@ function traverse_expr(expr, traverseContext) {
867
891
  table: type.table || ''
868
892
  };
869
893
  }
870
- if (expr.ASSIGN()) { //=
894
+ if (expr.ASSIGN()) {
895
+ //=
871
896
  const exprLeft = expr.expr(0);
872
897
  const exprRight = expr.expr(1);
873
898
  const typeLeft = traverse_expr(exprLeft, traverseContext);
874
899
  const typeRight = traverse_expr(exprRight, traverseContext);
875
- if (typeLeft.name == '?') {
900
+ if (typeLeft.name === '?') {
876
901
  typeLeft.notNull = true;
877
902
  }
878
- if (typeRight.name == '?') {
903
+ if (typeRight.name === '?') {
879
904
  typeRight.notNull = true;
880
905
  }
881
906
  traverseContext.constraints.push({
@@ -895,10 +920,10 @@ function traverse_expr(expr, traverseContext) {
895
920
  const exprType = traverse_expr(expr.expr(0), traverseContext);
896
921
  const between1 = traverse_expr(expr.expr(1), traverseContext);
897
922
  const between2 = traverse_expr(expr.expr(2), traverseContext);
898
- if (between1.name == '?') {
923
+ if (between1.name === '?') {
899
924
  between1.notNull = true;
900
925
  }
901
- if (between2.name == '?') {
926
+ if (between2.name === '?') {
902
927
  between2.notNull = true;
903
928
  }
904
929
  traverseContext.constraints.push({
@@ -928,7 +953,7 @@ function traverse_expr(expr, traverseContext) {
928
953
  const exprList = expr.expr_list();
929
954
  const inExprLeft = exprList[0];
930
955
  const typeLeft = traverse_expr(inExprLeft, traverseContext);
931
- if (typeLeft.name == '?') {
956
+ if (typeLeft.name === '?') {
932
957
  typeLeft.notNull = true;
933
958
  }
934
959
  if (expr.NOT_()) {
@@ -944,9 +969,9 @@ function traverse_expr(expr, traverseContext) {
944
969
  });
945
970
  }
946
971
  //NOT IN (1, 2, 3)
947
- exprList.slice(1).forEach(inExpr => {
972
+ exprList.slice(1).forEach((inExpr) => {
948
973
  const typeRight = traverse_expr(inExpr, traverseContext);
949
- if (typeRight.name == '?') {
974
+ if (typeRight.name === '?') {
950
975
  typeRight.notNull = true;
951
976
  }
952
977
  traverseContext.constraints.push({
@@ -970,9 +995,9 @@ function traverse_expr(expr, traverseContext) {
970
995
  });
971
996
  }
972
997
  //IN (1, 2, 3)
973
- rightExpr.expr_list().forEach(inExpr2 => {
998
+ rightExpr.expr_list().forEach((inExpr2) => {
974
999
  const typeRight = traverse_expr(inExpr2, traverseContext);
975
- if (typeRight.name == '?') {
1000
+ if (typeRight.name === '?') {
976
1001
  typeRight.notNull = true;
977
1002
  }
978
1003
  traverseContext.constraints.push({
@@ -995,10 +1020,10 @@ function traverse_expr(expr, traverseContext) {
995
1020
  const exprRight = expr.expr(1);
996
1021
  const typeLeft = traverse_expr(exprLeft, traverseContext);
997
1022
  const typeRight = traverse_expr(exprRight, traverseContext);
998
- if (typeLeft.name == '?') {
1023
+ if (typeLeft.name === '?') {
999
1024
  typeLeft.notNull = true;
1000
1025
  }
1001
- if (typeRight.name == '?') {
1026
+ if (typeRight.name === '?') {
1002
1027
  typeRight.notNull = true;
1003
1028
  }
1004
1029
  traverseContext.constraints.push({
@@ -1032,7 +1057,7 @@ function traverse_expr(expr, traverseContext) {
1032
1057
  }
1033
1058
  if (expr.OPEN_PAR() && expr.CLOSE_PAR()) {
1034
1059
  const type = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
1035
- const exprTypes = expr.expr_list().map(innerExpr => {
1060
+ const exprTypes = expr.expr_list().map((innerExpr) => {
1036
1061
  const exprType = traverse_expr(innerExpr, traverseContext);
1037
1062
  traverseContext.constraints.push({
1038
1063
  expression: innerExpr.getText(),
@@ -1044,7 +1069,7 @@ function traverse_expr(expr, traverseContext) {
1044
1069
  return {
1045
1070
  name: type.name,
1046
1071
  type: type,
1047
- notNull: exprTypes.every(type => type.notNull),
1072
+ notNull: exprTypes.every((type) => type.notNull),
1048
1073
  table: type.table || ''
1049
1074
  };
1050
1075
  }
@@ -1053,7 +1078,7 @@ function traverse_expr(expr, traverseContext) {
1053
1078
  const whenTypes = [];
1054
1079
  expr.expr_list().forEach((expr_, index) => {
1055
1080
  const type = traverse_expr(expr_, traverseContext);
1056
- if (index % 2 == 0 && (!expr.ELSE_() || index < expr.expr_list().length - 1)) {
1081
+ if (index % 2 === 0 && (!expr.ELSE_() || index < expr.expr_list().length - 1)) {
1057
1082
  whenTypes.push(type);
1058
1083
  }
1059
1084
  else {
@@ -1080,11 +1105,11 @@ function traverse_expr(expr, traverseContext) {
1080
1105
  return {
1081
1106
  name: extractOriginalSql(expr),
1082
1107
  type: type.type,
1083
- notNull: expr.ELSE_() ? resultTypes.every(type => type.notNull) : false,
1108
+ notNull: expr.ELSE_() ? resultTypes.every((type) => type.notNull) : false,
1084
1109
  table: type.table || ''
1085
1110
  };
1086
1111
  }
1087
- throw Error('traverse_expr not supported:' + expr.getText());
1112
+ throw Error(`traverse_expr not supported:${expr.getText()}`);
1088
1113
  }
1089
1114
  function extractOriginalSql(rule) {
1090
1115
  var _a, _b;
@@ -1094,7 +1119,10 @@ function extractOriginalSql(rule) {
1094
1119
  return result;
1095
1120
  }
1096
1121
  function traverse_column_name(column_name, table_name, traverseContext) {
1097
- const fieldName = { name: column_name.getText(), prefix: (table_name === null || table_name === void 0 ? void 0 : table_name.getText()) || '' };
1122
+ const fieldName = {
1123
+ name: column_name.getText(),
1124
+ prefix: (table_name === null || table_name === void 0 ? void 0 : table_name.getText()) || ''
1125
+ };
1098
1126
  const column = (0, select_columns_1.findColumn)(fieldName, traverseContext.fromColumns);
1099
1127
  // const typeVar = freshVar(column.columnName, column.columnType.type, column.tableAlias || column.table);
1100
1128
  return column;
@@ -1104,29 +1132,28 @@ function isNotNull(columnName, where) {
1104
1132
  return false;
1105
1133
  }
1106
1134
  if (where.AND_()) {
1107
- const ifNullList = where.expr_list().map(expr => isNotNull(columnName, expr));
1108
- const result = ifNullList.some(v => v);
1135
+ const ifNullList = where.expr_list().map((expr) => isNotNull(columnName, expr));
1136
+ const result = ifNullList.some((v) => v);
1109
1137
  return result;
1110
1138
  }
1111
- else if (where.OR_()) {
1112
- const possibleNullList = where.expr_list().map(expr => isNotNull(columnName, expr));
1113
- const result = possibleNullList.every(v => v);
1139
+ if (where.OR_()) {
1140
+ const possibleNullList = where.expr_list().map((expr) => isNotNull(columnName, expr));
1141
+ const result = possibleNullList.every((v) => v);
1114
1142
  return result;
1115
1143
  }
1116
- else {
1117
- return isNotNullExpr(columnName, where);
1118
- }
1144
+ return isNotNullExpr(columnName, where);
1119
1145
  }
1120
- exports.isNotNull = isNotNull;
1121
1146
  function isNotNullExpr(columnName, expr) {
1122
1147
  if (expr.OPEN_PAR() && expr.CLOSE_PAR()) {
1123
1148
  const innerExpr = expr.expr(0);
1124
1149
  return isNotNull(columnName, innerExpr);
1125
1150
  }
1126
- if (expr.ASSIGN()
1127
- || expr.GT() || expr.GT_EQ()
1128
- || expr.LT() || expr.LT_EQ()
1129
- || (expr.IS_() && expr.expr_list().length == 2 && expr.expr(1).getText() == 'notnull')) {
1151
+ if (expr.ASSIGN() ||
1152
+ expr.GT() ||
1153
+ expr.GT_EQ() ||
1154
+ expr.LT() ||
1155
+ expr.LT_EQ() ||
1156
+ (expr.IS_() && expr.expr_list().length === 2 && expr.expr(1).getText() === 'notnull')) {
1130
1157
  const exprLeft = expr.expr(0);
1131
1158
  const exprRight = expr.expr(1);
1132
1159
  const column_name_left = exprLeft.column_name();
@@ -1134,7 +1161,7 @@ function isNotNullExpr(columnName, expr) {
1134
1161
  if (column_name_left || column_name_right) {
1135
1162
  const columnLeft = column_name_left === null || column_name_left === void 0 ? void 0 : column_name_left.getText();
1136
1163
  const columnRight = column_name_right === null || column_name_right === void 0 ? void 0 : column_name_right.getText();
1137
- if (columnLeft == columnName || columnRight == columnName) {
1164
+ if (columnLeft === columnName || columnRight === columnName) {
1138
1165
  return true;
1139
1166
  }
1140
1167
  }
@@ -1142,23 +1169,24 @@ function isNotNullExpr(columnName, expr) {
1142
1169
  return false;
1143
1170
  }
1144
1171
  function isMultipleRowResult(select_stmt, fromColumns) {
1145
- if (select_stmt.select_core_list().length == 1) { //UNION queries are multipleRowsResult = true
1172
+ if (select_stmt.select_core_list().length === 1) {
1173
+ //UNION queries are multipleRowsResult = true
1146
1174
  const select_core = select_stmt.select_core(0);
1147
1175
  const from = select_core.FROM_();
1148
1176
  if (!from) {
1149
1177
  return false;
1150
1178
  }
1151
- const groupBy = select_stmt.select_core_list().some(select_core => select_core.GROUP_() != null);
1179
+ const groupBy = select_stmt.select_core_list().some((select_core) => select_core.GROUP_() != null);
1152
1180
  if (groupBy) {
1153
1181
  return true;
1154
1182
  }
1155
- const agreegateFunction = select_core.result_column_list().some(result_column => isAgregateFunction(result_column));
1183
+ const agreegateFunction = select_core.result_column_list().some((result_column) => isAgregateFunction(result_column));
1156
1184
  if (agreegateFunction) {
1157
1185
  return false;
1158
1186
  }
1159
1187
  const _whereExpr = select_core._whereExpr;
1160
1188
  const isSingleResult = select_core.join_clause() == null && _whereExpr && where_is_single_result(_whereExpr, fromColumns);
1161
- if (isSingleResult == true) {
1189
+ if (isSingleResult === true) {
1162
1190
  return false;
1163
1191
  }
1164
1192
  }
@@ -1167,10 +1195,10 @@ function isMultipleRowResult(select_stmt, fromColumns) {
1167
1195
  }
1168
1196
  return true;
1169
1197
  }
1170
- exports.isMultipleRowResult = isMultipleRowResult;
1171
1198
  function isAgregateFunction(result_column) {
1172
1199
  var _a;
1173
- if (((_a = result_column.expr()) === null || _a === void 0 ? void 0 : _a.over_clause()) != null) { //window function isMultipleRow = true
1200
+ if (((_a = result_column.expr()) === null || _a === void 0 ? void 0 : _a.over_clause()) != null) {
1201
+ //window function isMultipleRow = true
1174
1202
  return false;
1175
1203
  }
1176
1204
  const expr = result_column.expr();
@@ -1180,21 +1208,21 @@ function isAgregateFunction(result_column) {
1180
1208
  function isAgregateFunctionExpr(expr) {
1181
1209
  var _a;
1182
1210
  //ex. min(value)/100, 100/min(value)
1183
- const isAgrr = expr.expr_list().some(expr => isAgregateFunctionExpr(expr));
1211
+ const isAgrr = expr.expr_list().some((expr) => isAgregateFunctionExpr(expr));
1184
1212
  if (isAgrr) {
1185
1213
  return isAgrr;
1186
1214
  }
1187
1215
  const function_name = (_a = expr.function_name()) === null || _a === void 0 ? void 0 : _a.getText().toLowerCase();
1188
- return function_name == 'count'
1189
- || function_name == 'sum'
1190
- || function_name == 'avg'
1191
- || function_name == 'min'
1192
- || function_name == 'max'
1193
- || function_name == 'group_concat';
1216
+ return (function_name === 'count' ||
1217
+ function_name === 'sum' ||
1218
+ function_name === 'avg' ||
1219
+ function_name === 'min' ||
1220
+ function_name === 'max' ||
1221
+ function_name === 'group_concat');
1194
1222
  }
1195
1223
  function isLimitOne(select_stmt) {
1196
1224
  const limit_stmt = select_stmt.limit_stmt();
1197
- if (limit_stmt && limit_stmt.expr(0).getText() == '1') {
1225
+ if (limit_stmt && limit_stmt.expr(0).getText() === '1') {
1198
1226
  return true;
1199
1227
  }
1200
1228
  return false;
@@ -1206,7 +1234,7 @@ function where_is_single_result(whereExpr, fromColumns) {
1206
1234
  }
1207
1235
  const expr_list = whereExpr.expr_list();
1208
1236
  const onlyAnd = !whereExpr.OR_();
1209
- const oneSingle = expr_list.some(expr => is_single_result(expr, fromColumns));
1237
+ const oneSingle = expr_list.some((expr) => is_single_result(expr, fromColumns));
1210
1238
  if (onlyAnd && oneSingle) {
1211
1239
  return true;
1212
1240
  }
@@ -1219,7 +1247,7 @@ function is_single_result(expr, fromColumns) {
1219
1247
  if (column_name && expr.ASSIGN()) {
1220
1248
  const fieldName = (0, select_columns_1.splitName)(column_name.getText());
1221
1249
  const column = (0, select_columns_1.findColumn)(fieldName, fromColumns);
1222
- if (column.columnKey == 'PRI') {
1250
+ if (column.columnKey === 'PRI') {
1223
1251
  return true;
1224
1252
  }
1225
1253
  }
@@ -1229,7 +1257,7 @@ function traverse_insert_stmt(insert_stmt, traverseContext) {
1229
1257
  var _a;
1230
1258
  const table_name = insert_stmt.table_name();
1231
1259
  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 => {
1260
+ const columns = insert_stmt.column_name_list().map((column_name) => {
1233
1261
  return traverse_column_name(column_name, null, Object.assign(Object.assign({}, traverseContext), { fromColumns }));
1234
1262
  });
1235
1263
  const insertColumns = [];
@@ -1245,8 +1273,8 @@ function traverse_insert_stmt(insert_stmt, traverseContext) {
1245
1273
  type1: col.columnType,
1246
1274
  type2: exprType.type
1247
1275
  });
1248
- const notNullColumn = (col.columnKey == 'PRI' && col.columnType.type == 'INTEGER') ? false : col.notNull;
1249
- insertColumns.push(Object.assign(Object.assign({}, param), { notNull: exprType.name == '?' ? notNullColumn : param.notNull }));
1276
+ const notNullColumn = col.columnKey === 'PRI' && col.columnType.type === 'INTEGER' ? false : col.notNull;
1277
+ insertColumns.push(Object.assign(Object.assign({}, param), { notNull: exprType.name === '?' ? notNullColumn : param.notNull }));
1250
1278
  });
1251
1279
  });
1252
1280
  });
@@ -1261,10 +1289,10 @@ function traverse_insert_stmt(insert_stmt, traverseContext) {
1261
1289
  type1: col.columnType,
1262
1290
  type2: selectColumn.type
1263
1291
  });
1264
- const notNullColumn = (col.columnKey == 'PRI' && col.columnType.type == 'INTEGER') ? false : col.notNull;
1292
+ const notNullColumn = col.columnKey === 'PRI' && col.columnType.type === 'INTEGER' ? false : col.notNull;
1265
1293
  columnNullability.set(selectColumn.type.id, notNullColumn);
1266
1294
  });
1267
- traverseContext.parameters.forEach(param => {
1295
+ traverseContext.parameters.forEach((param) => {
1268
1296
  insertColumns.push(Object.assign(Object.assign({}, param), { notNull: columnNullability.get(param.type.id) != null ? columnNullability.get(param.type.id) : param.notNull }));
1269
1297
  });
1270
1298
  }
@@ -1276,14 +1304,16 @@ function traverse_insert_stmt(insert_stmt, traverseContext) {
1276
1304
  const column_name = upsert_clause.column_name(index);
1277
1305
  const col = traverse_column_name(column_name, null, Object.assign(Object.assign({}, traverseContext), { fromColumns }));
1278
1306
  const expr = upsert_clause.expr(index);
1279
- const exprType = traverse_expr(expr, Object.assign(Object.assign({}, traverseContext), { fromColumns }));
1307
+ const table_name = expr.table_name();
1308
+ const excludedColumns = table_name && table_name.getText() === 'excluded' ? fromColumns.map((col) => (Object.assign(Object.assign({}, col), { table: 'excluded' }))) : [];
1309
+ const exprType = traverse_expr(expr, Object.assign(Object.assign({}, traverseContext), { fromColumns: fromColumns.concat(excludedColumns) }));
1280
1310
  traverseContext.constraints.push({
1281
1311
  expression: column_name.getText(),
1282
1312
  type1: col.columnType,
1283
1313
  type2: exprType.type
1284
1314
  });
1285
1315
  });
1286
- traverseContext.parameters.slice(paramsBefore).forEach(param => {
1316
+ traverseContext.parameters.slice(paramsBefore).forEach((param) => {
1287
1317
  insertColumns.push(Object.assign(Object.assign({}, param), { notNull: param.notNull }));
1288
1318
  });
1289
1319
  }
@@ -1300,9 +1330,9 @@ function traverse_insert_stmt(insert_stmt, traverseContext) {
1300
1330
  }
1301
1331
  function traverse_returning_clause(returning_clause, fromColumns) {
1302
1332
  const result_column_list = returning_clause.result_column_list();
1303
- const result = result_column_list.flatMap(result_column => {
1333
+ const result = result_column_list.flatMap((result_column) => {
1304
1334
  if (result_column.STAR()) {
1305
- return fromColumns.map(col => {
1335
+ return fromColumns.map((col) => {
1306
1336
  const newCol = {
1307
1337
  name: col.columnName,
1308
1338
  type: col.columnType,
@@ -1319,9 +1349,10 @@ function traverse_returning_clause(returning_clause, fromColumns) {
1319
1349
  function traverse_update_stmt(update_stmt, traverseContext) {
1320
1350
  const table_name = update_stmt.qualified_table_name().getText();
1321
1351
  const fromColumns = (0, select_columns_1.filterColumns)(traverseContext.dbSchema, [], '', (0, select_columns_1.splitName)(table_name));
1322
- const column_name_list = Array.from({ length: update_stmt.ASSIGN_list().length })
1323
- .map((_, i) => update_stmt.column_name(i));
1324
- const columns = column_name_list.map(column_name => {
1352
+ const column_name_list = Array.from({
1353
+ length: update_stmt.ASSIGN_list().length
1354
+ }).map((_, i) => update_stmt.column_name(i));
1355
+ const columns = column_name_list.map((column_name) => {
1325
1356
  return traverse_column_name(column_name, null, Object.assign(Object.assign({}, traverseContext), { fromColumns }));
1326
1357
  });
1327
1358
  const updateColumns = [];
@@ -1377,11 +1408,10 @@ function getAllColumns(expr) {
1377
1408
  columns.push((0, select_columns_1.splitName)(expr1.getText()));
1378
1409
  columns.push((0, select_columns_1.splitName)(expr2.getText()));
1379
1410
  }
1380
- ;
1381
1411
  return columns;
1382
1412
  }
1383
1413
  function filterUsingFields(fields, usingFields) {
1384
- const result = fields.filter(field => !usingFields.includes(field.columnName));
1414
+ const result = fields.filter((field) => !usingFields.includes(field.columnName));
1385
1415
  return result;
1386
1416
  }
1387
1417
  function getParamsIndexes(parameters, paramsIds) {
@@ -1389,7 +1419,7 @@ function getParamsIndexes(parameters, paramsIds) {
1389
1419
  parameters.forEach((param, index) => {
1390
1420
  map.set(param.paramIndex, index);
1391
1421
  });
1392
- return paramsIds.map(id => map.get(id));
1422
+ return paramsIds.map((id) => map.get(id));
1393
1423
  }
1394
1424
  function getWhereFragmentExpressions(whereExpr) {
1395
1425
  const exprList = [];