typesql-cli 0.8.0-alpha.5 → 0.8.0-alpha.7

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 (60) hide show
  1. package/cli.js +36 -39
  2. package/cli.js.map +1 -1
  3. package/code-generator.d.ts +4 -4
  4. package/code-generator.d.ts.map +1 -1
  5. package/code-generator.js +17 -19
  6. package/code-generator.js.map +1 -1
  7. package/describe-query.d.ts +2 -3
  8. package/describe-query.d.ts.map +1 -1
  9. package/describe-query.js +3 -2
  10. package/describe-query.js.map +1 -1
  11. package/mysql-query-analyzer/collect-constraints.d.ts +2 -2
  12. package/mysql-query-analyzer/collect-constraints.d.ts.map +1 -1
  13. package/mysql-query-analyzer/collect-constraints.js +1 -1
  14. package/mysql-query-analyzer/collect-constraints.js.map +1 -1
  15. package/mysql-query-analyzer/traverse.d.ts +19 -0
  16. package/mysql-query-analyzer/traverse.d.ts.map +1 -1
  17. package/mysql-query-analyzer/traverse.js +15 -15
  18. package/mysql-query-analyzer/traverse.js.map +1 -1
  19. package/mysql-query-analyzer/types.d.ts +1 -1
  20. package/mysql-query-analyzer/types.d.ts.map +1 -1
  21. package/mysql-query-analyzer/unify.d.ts.map +1 -1
  22. package/mysql-query-analyzer/unify.js +8 -0
  23. package/mysql-query-analyzer/unify.js.map +1 -1
  24. package/package.json +1 -1
  25. package/queryExectutor.d.ts +8 -15
  26. package/queryExectutor.d.ts.map +1 -1
  27. package/queryExectutor.js +119 -121
  28. package/queryExectutor.js.map +1 -1
  29. package/sql-generator.d.ts +5 -4
  30. package/sql-generator.d.ts.map +1 -1
  31. package/sql-generator.js +24 -21
  32. package/sql-generator.js.map +1 -1
  33. package/sqlite-query-analyzer/code-generator.d.ts +3 -1
  34. package/sqlite-query-analyzer/code-generator.d.ts.map +1 -1
  35. package/sqlite-query-analyzer/code-generator.js +88 -29
  36. package/sqlite-query-analyzer/code-generator.js.map +1 -1
  37. package/sqlite-query-analyzer/parser.d.ts +2 -0
  38. package/sqlite-query-analyzer/parser.d.ts.map +1 -1
  39. package/sqlite-query-analyzer/parser.js +155 -31
  40. package/sqlite-query-analyzer/parser.js.map +1 -1
  41. package/sqlite-query-analyzer/query-executor.d.ts +7 -3
  42. package/sqlite-query-analyzer/query-executor.d.ts.map +1 -1
  43. package/sqlite-query-analyzer/query-executor.js +65 -6
  44. package/sqlite-query-analyzer/query-executor.js.map +1 -1
  45. package/sqlite-query-analyzer/sql-generator.d.ts +6 -0
  46. package/sqlite-query-analyzer/sql-generator.d.ts.map +1 -0
  47. package/sqlite-query-analyzer/sql-generator.js +96 -0
  48. package/sqlite-query-analyzer/sql-generator.js.map +1 -0
  49. package/sqlite-query-analyzer/sqlite-crud-generator.d.ts +6 -0
  50. package/sqlite-query-analyzer/sqlite-crud-generator.d.ts.map +1 -0
  51. package/sqlite-query-analyzer/sqlite-crud-generator.js +96 -0
  52. package/sqlite-query-analyzer/sqlite-crud-generator.js.map +1 -0
  53. package/sqlite-query-analyzer/traverse.d.ts +4 -4
  54. package/sqlite-query-analyzer/traverse.d.ts.map +1 -1
  55. package/sqlite-query-analyzer/traverse.js +384 -82
  56. package/sqlite-query-analyzer/traverse.js.map +1 -1
  57. package/sqlite-query-analyzer/types.d.ts +1 -1
  58. package/sqlite-query-analyzer/types.d.ts.map +1 -1
  59. package/types.d.ts +15 -1
  60. package/types.d.ts.map +1 -1
@@ -8,7 +8,26 @@ function traverse_Sql_stmtContext(sql_stmt, traverseContext) {
8
8
  const select_stmt = sql_stmt.select_stmt();
9
9
  if (select_stmt) {
10
10
  const queryResult = traverse_select_stmt(select_stmt, traverseContext);
11
- return queryResult;
11
+ return {
12
+ queryType: 'Select',
13
+ columns: queryResult.columns,
14
+ multipleRowsResult: isMultipleRowResult(select_stmt, queryResult.fromColumns),
15
+ };
16
+ }
17
+ const insert_stmt = sql_stmt.insert_stmt();
18
+ if (insert_stmt) {
19
+ const insertResult = traverse_insert_stmt(insert_stmt, traverseContext);
20
+ return insertResult;
21
+ }
22
+ const update_stmt = sql_stmt.update_stmt();
23
+ if (update_stmt) {
24
+ const updateResult = traverse_update_stmt(update_stmt, traverseContext);
25
+ return updateResult;
26
+ }
27
+ const delete_stmt = sql_stmt.delete_stmt();
28
+ if (delete_stmt) {
29
+ const deleteResult = traverse_delete_stmt(delete_stmt, traverseContext);
30
+ return deleteResult;
12
31
  }
13
32
  throw Error("traverse_Sql_stmtContext");
14
33
  }
@@ -36,7 +55,6 @@ function traverse_select_stmt(select_stmt, traverseContext) {
36
55
  const querySpecResult = select_coreList.map(select_core => {
37
56
  const columnsResult = [];
38
57
  const listType = [];
39
- const columnNullability = [];
40
58
  const table_or_subquery = select_core.table_or_subquery_list();
41
59
  if (table_or_subquery) {
42
60
  const fields = traverse_table_or_subquery(table_or_subquery, traverseContext);
@@ -56,8 +74,12 @@ function traverse_select_stmt(select_stmt, traverseContext) {
56
74
  columnsResult.forEach(col => {
57
75
  if (!tableName || (0, select_columns_1.includeColumn)(col, tableName)) {
58
76
  const columnType = (0, collect_constraints_1.createColumnType)(col);
59
- listType.push(columnType);
60
- columnNullability.push(col.notNull);
77
+ listType.push({
78
+ name: columnType.name,
79
+ type: columnType,
80
+ notNull: col.notNull,
81
+ table: col.tableAlias || col.table
82
+ });
61
83
  }
62
84
  });
63
85
  }
@@ -65,12 +87,11 @@ function traverse_select_stmt(select_stmt, traverseContext) {
65
87
  const alias = (_b = result_column.column_alias()) === null || _b === void 0 ? void 0 : _b.getText();
66
88
  if (expr) {
67
89
  const exprType = traverse_expr(expr, Object.assign(Object.assign({}, traverseContext), { fromColumns: columnsResult }));
68
- if (exprType.kind == 'TypeVar') {
90
+ if (exprType.type.kind == 'TypeVar') {
69
91
  if (alias) {
70
92
  exprType.name = alias;
71
93
  }
72
94
  listType.push(exprType);
73
- columnNullability.push(inferNotNull_expr(expr, columnsResult));
74
95
  }
75
96
  }
76
97
  });
@@ -78,17 +99,8 @@ function traverse_select_stmt(select_stmt, traverseContext) {
78
99
  whereList.forEach(where => {
79
100
  traverse_expr(where, Object.assign(Object.assign({}, traverseContext), { fromColumns: columnsResult }));
80
101
  });
81
- const columns = listType.map((t, index) => {
82
- const resultType = {
83
- name: t.name,
84
- type: t,
85
- notNull: columnNullability[index],
86
- table: t.table || ''
87
- };
88
- return resultType;
89
- });
90
102
  const querySpecification = {
91
- columns,
103
+ columns: listType,
92
104
  fromColumns: columnsResult //TODO - return isMultipleRowResult instead
93
105
  };
94
106
  return querySpecification;
@@ -141,24 +153,50 @@ function traverse_expr(expr, traverseContext) {
141
153
  var _a, _b;
142
154
  const function_name = (_a = expr.function_name()) === null || _a === void 0 ? void 0 : _a.getText().toLowerCase();
143
155
  if (function_name == 'sum' || function_name == 'avg') {
144
- const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'NUMERIC');
156
+ const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), function_name == 'sum' ? 'NUMERIC' : 'REAL');
145
157
  const sumParamExpr = expr.expr(0);
146
158
  const paramType = traverse_expr(sumParamExpr, traverseContext);
147
- if (paramType.kind == 'TypeVar') {
159
+ if (paramType.type.kind == 'TypeVar') {
148
160
  functionType.table = paramType.table;
149
161
  }
150
- return functionType;
162
+ return {
163
+ name: functionType.name,
164
+ type: functionType,
165
+ notNull: paramType.notNull,
166
+ table: functionType.table || ''
167
+ };
168
+ }
169
+ if (function_name == 'min' || function_name == 'max') {
170
+ const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
171
+ const sumParamExpr = expr.expr(0);
172
+ const paramType = traverse_expr(sumParamExpr, traverseContext);
173
+ traverseContext.constraints.push({
174
+ expression: expr.getText(),
175
+ type1: functionType,
176
+ type2: paramType.type
177
+ });
178
+ return {
179
+ name: functionType.name,
180
+ type: functionType,
181
+ notNull: paramType.notNull,
182
+ table: functionType.table || ''
183
+ };
151
184
  }
152
185
  if (function_name == 'count') {
153
186
  const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'INTEGER');
154
187
  if (expr.expr_list().length == 1) {
155
188
  const sumParamExpr = expr.expr(0);
156
189
  const paramType = traverse_expr(sumParamExpr, traverseContext);
157
- if (paramType.kind == 'TypeVar') {
190
+ if (paramType.type.kind == 'TypeVar') {
158
191
  functionType.table = paramType.table;
159
192
  }
160
193
  }
161
- return functionType;
194
+ return {
195
+ name: functionType.name,
196
+ type: functionType,
197
+ notNull: true,
198
+ table: functionType.table || ''
199
+ };
162
200
  }
163
201
  if (function_name == 'concat') {
164
202
  const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'TEXT');
@@ -167,88 +205,243 @@ function traverse_expr(expr, traverseContext) {
167
205
  traverseContext.constraints.push({
168
206
  expression: expr.getText(),
169
207
  type1: functionType,
170
- type2: paramType
208
+ type2: paramType.type
171
209
  });
172
- if (paramType.kind == 'TypeVar') {
210
+ if (paramType.type.kind == 'TypeVar') {
173
211
  functionType.table = paramType.table;
174
212
  }
175
213
  });
176
- return functionType;
214
+ return {
215
+ name: functionType.name,
216
+ type: functionType,
217
+ notNull: true,
218
+ table: functionType.table || ''
219
+ };
220
+ }
221
+ if (function_name == 'coalesce') {
222
+ const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
223
+ const paramTypes = expr.expr_list().map(paramExpr => {
224
+ const paramType = traverse_expr(paramExpr, traverseContext);
225
+ traverseContext.constraints.push({
226
+ expression: expr.getText(),
227
+ type1: functionType,
228
+ type2: paramType.type
229
+ });
230
+ return paramType;
231
+ });
232
+ return {
233
+ name: functionType.name,
234
+ type: functionType,
235
+ notNull: paramTypes.some(param => param.notNull),
236
+ table: functionType.table || ''
237
+ };
238
+ }
239
+ if (function_name == 'strftime') {
240
+ const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'TEXT');
241
+ const paramExpr = expr.expr(1);
242
+ const paramType = traverse_expr(paramExpr, traverseContext);
243
+ paramType.notNull = true;
244
+ traverseContext.constraints.push({
245
+ expression: paramExpr.getText(),
246
+ type1: (0, collect_constraints_1.freshVar)(paramExpr.getText(), 'DATE'),
247
+ type2: paramType.type
248
+ });
249
+ return {
250
+ name: functionType.name,
251
+ type: functionType,
252
+ notNull: false,
253
+ table: functionType.table || ''
254
+ };
255
+ }
256
+ if (function_name == 'date' || function_name == 'time' || function_name == 'datetime') {
257
+ const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), 'TEXT');
258
+ const paramExpr = expr.expr(0);
259
+ const paramType = traverse_expr(paramExpr, traverseContext);
260
+ paramType.notNull = true;
261
+ traverseContext.constraints.push({
262
+ expression: paramExpr.getText(),
263
+ type1: (0, collect_constraints_1.freshVar)(paramExpr.getText(), 'DATE'),
264
+ type2: paramType.type
265
+ });
266
+ return {
267
+ name: functionType.name,
268
+ type: functionType,
269
+ notNull: false,
270
+ table: functionType.table || ''
271
+ };
272
+ }
273
+ if (function_name == 'ifnull') {
274
+ const functionType = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
275
+ const paramTypes = expr.expr_list().map(paramExpr => {
276
+ const paramType = traverse_expr(paramExpr, traverseContext);
277
+ if (paramType.name == '?') {
278
+ paramType.notNull = false;
279
+ }
280
+ traverseContext.constraints.push({
281
+ expression: expr.getText(),
282
+ type1: functionType,
283
+ type2: paramType.type
284
+ });
285
+ return paramType;
286
+ });
287
+ return {
288
+ name: functionType.name,
289
+ type: functionType,
290
+ notNull: paramTypes.every(param => param.notNull),
291
+ table: functionType.table || ''
292
+ };
177
293
  }
178
294
  if (function_name) {
179
295
  throw Error('traverse_expr: function not supported:' + function_name);
180
296
  }
181
297
  const column_name = expr.column_name();
182
298
  if (column_name) {
183
- const fieldName = (0, select_columns_1.splitName)(column_name.getText());
184
- const column = (0, select_columns_1.findColumn)(fieldName, traverseContext.fromColumns);
185
- const typeVar = (0, collect_constraints_1.freshVar)(column.columnName, column.columnType.type, column.tableAlias || column.table);
186
- return typeVar;
299
+ const type = traverse_column_name(column_name, traverseContext);
300
+ return type;
187
301
  }
188
302
  const literal = expr.literal_value();
189
303
  if (literal) {
190
304
  if (literal.STRING_LITERAL()) {
191
- return (0, collect_constraints_1.freshVar)(literal.getText(), 'TEXT');
305
+ const type = (0, collect_constraints_1.freshVar)(literal.getText(), 'TEXT');
306
+ return {
307
+ name: type.name,
308
+ type: type,
309
+ notNull: true,
310
+ table: type.table || ''
311
+ };
192
312
  }
193
313
  if (literal.NUMERIC_LITERAL()) {
194
- return (0, collect_constraints_1.freshVar)(literal.getText(), 'INTEGER');
314
+ const type = (0, collect_constraints_1.freshVar)(literal.getText(), 'INTEGER');
315
+ return {
316
+ name: type.name,
317
+ type: type,
318
+ notNull: true,
319
+ table: type.table || ''
320
+ };
195
321
  }
196
- return (0, collect_constraints_1.freshVar)(literal.getText(), '?');
322
+ const type = (0, collect_constraints_1.freshVar)(literal.getText(), '?');
323
+ return {
324
+ name: type.name,
325
+ type: type,
326
+ notNull: true,
327
+ table: type.table || ''
328
+ };
197
329
  }
198
330
  const parameter = expr.BIND_PARAMETER();
199
331
  if (parameter) {
200
332
  const param = (0, collect_constraints_1.freshVar)('?', '?');
201
- traverseContext.parameters.push(param);
202
- return param;
333
+ const type = {
334
+ name: param.name,
335
+ type: param,
336
+ notNull: false,
337
+ table: param.table || ''
338
+ };
339
+ traverseContext.parameters.push(type);
340
+ return type;
203
341
  }
204
342
  if (expr.STAR() || expr.DIV() || expr.MOD()) {
205
343
  const exprLeft = expr.expr(0);
206
344
  const exprRight = expr.expr(1);
207
345
  const typeLeft = traverse_expr(exprLeft, traverseContext);
208
- return (0, collect_constraints_1.freshVar)(expr.getText(), 'tinyint');
346
+ const typeRight = traverse_expr(exprRight, traverseContext);
347
+ const type = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
348
+ return {
349
+ name: type.name,
350
+ type: type,
351
+ notNull: typeLeft.notNull && typeRight.notNull,
352
+ table: type.table || ''
353
+ };
354
+ }
355
+ if (expr.PLUS() || expr.MINUS()) {
356
+ const exprLeft = expr.expr(0);
357
+ const exprRight = expr.expr(1);
358
+ const typeLeft = traverse_expr(exprLeft, traverseContext);
359
+ const typeRight = traverse_expr(exprRight, traverseContext);
360
+ return Object.assign(Object.assign({}, typeRight), { notNull: typeLeft.notNull && typeRight.notNull });
209
361
  }
210
362
  if (expr.LT2() || expr.GT2() || expr.AMP() || expr.PIPE() || expr.LT() || expr.LT_EQ() || expr.GT() || expr.GT_EQ()) {
211
363
  const exprLeft = expr.expr(0);
212
364
  const exprRight = expr.expr(1);
213
365
  const typeLeft = traverse_expr(exprLeft, traverseContext);
214
366
  const typeRight = traverse_expr(exprRight, traverseContext);
367
+ if (typeLeft.name == '?') {
368
+ typeLeft.notNull = true;
369
+ }
370
+ if (typeRight.name == '?') {
371
+ typeRight.notNull = true;
372
+ }
215
373
  traverseContext.constraints.push({
216
374
  expression: expr.getText(),
217
- type1: typeLeft,
218
- type2: typeRight
375
+ type1: typeLeft.type,
376
+ type2: typeRight.type
219
377
  });
220
- return (0, collect_constraints_1.freshVar)(expr.getText(), 'tinyint');
378
+ const type = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
379
+ return {
380
+ name: type.name,
381
+ type: type,
382
+ notNull: true,
383
+ table: type.table || ''
384
+ };
385
+ }
386
+ if (expr.IS_()) { //is null/is not null
387
+ const expr_ = expr.expr(0);
388
+ traverse_expr(expr_, traverseContext);
389
+ const type = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
390
+ return {
391
+ name: type.name,
392
+ type: type,
393
+ notNull: true,
394
+ table: type.table || ''
395
+ };
221
396
  }
222
397
  if (expr.ASSIGN()) { //=
223
398
  const exprLeft = expr.expr(0);
224
399
  const exprRight = expr.expr(1);
225
400
  const typeLeft = traverse_expr(exprLeft, traverseContext);
226
401
  const typeRight = traverse_expr(exprRight, traverseContext);
402
+ if (typeLeft.name == '?') {
403
+ typeLeft.notNull = true;
404
+ }
405
+ if (typeRight.name == '?') {
406
+ typeRight.notNull = true;
407
+ }
227
408
  traverseContext.constraints.push({
228
409
  expression: expr.getText(),
229
- type1: typeLeft,
230
- type2: typeRight
410
+ type1: typeLeft.type,
411
+ type2: typeRight.type
231
412
  });
232
- return (0, collect_constraints_1.freshVar)(expr.getText(), 'tinyint');
413
+ const type = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
414
+ return {
415
+ name: type.name,
416
+ type: type,
417
+ notNull: true,
418
+ table: type.table || ''
419
+ };
233
420
  }
234
421
  if (expr.BETWEEN_()) {
235
422
  const exprType = traverse_expr(expr.expr(0), traverseContext);
236
423
  const between1 = traverse_expr(expr.expr(1), traverseContext);
237
424
  const between2 = traverse_expr(expr.expr(2), traverseContext);
425
+ if (between1.name == '?') {
426
+ between1.notNull = true;
427
+ }
428
+ if (between2.name == '?') {
429
+ between2.notNull = true;
430
+ }
238
431
  traverseContext.constraints.push({
239
432
  expression: expr.getText(),
240
- type1: exprType,
241
- type2: between1
433
+ type1: exprType.type,
434
+ type2: between1.type
242
435
  });
243
436
  traverseContext.constraints.push({
244
437
  expression: expr.getText(),
245
- type1: exprType,
246
- type2: between2
438
+ type1: exprType.type,
439
+ type2: between2.type
247
440
  });
248
441
  traverseContext.constraints.push({
249
442
  expression: expr.getText(),
250
- type1: between1,
251
- type2: between2
443
+ type1: between1.type,
444
+ type2: between2.type
252
445
  });
253
446
  return exprType;
254
447
  }
@@ -267,39 +460,58 @@ function traverse_expr(expr, traverseContext) {
267
460
  const typeRight = traverse_expr(exprRight, traverseContext);
268
461
  traverseContext.constraints.push({
269
462
  expression: expr.getText(),
270
- type1: typeLeft,
271
- type2: typeRight
463
+ type1: typeLeft.type,
464
+ type2: typeRight.type
272
465
  });
273
466
  }
274
467
  });
275
- return (0, collect_constraints_1.freshVar)(expr.getText(), 'tinyint');
276
- }
277
- if (expr.OPEN_PAR() && expr.CLOSE_PAR()) {
278
- expr.expr_list().forEach(innerExpr => {
279
- traverse_expr(innerExpr, traverseContext);
280
- });
281
- return (0, collect_constraints_1.freshVar)(expr.getText(), 'tinyint');
468
+ const type = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
469
+ return {
470
+ name: type.name,
471
+ type: type,
472
+ notNull: true,
473
+ table: type.table || ''
474
+ };
282
475
  }
283
476
  const select_stmt = expr.select_stmt();
284
477
  if (select_stmt) {
285
478
  const subQueryType = traverse_select_stmt(select_stmt, traverseContext);
286
- return Object.assign(Object.assign({}, subQueryType.columns[0].type), { table: '' });
479
+ const type = Object.assign(Object.assign({}, subQueryType.columns[0].type), { table: '' });
480
+ return {
481
+ name: type.name,
482
+ type: type,
483
+ notNull: subQueryType.columns[0].notNull,
484
+ table: type.table || ''
485
+ };
486
+ }
487
+ if (expr.OPEN_PAR() && expr.CLOSE_PAR()) {
488
+ const type = (0, collect_constraints_1.freshVar)(expr.getText(), '?');
489
+ const exprTypes = expr.expr_list().map(innerExpr => {
490
+ const exprType = traverse_expr(innerExpr, traverseContext);
491
+ traverseContext.constraints.push({
492
+ expression: innerExpr.getText(),
493
+ type1: exprType.type,
494
+ type2: type
495
+ });
496
+ return exprType;
497
+ });
498
+ return {
499
+ name: type.name,
500
+ type: type,
501
+ notNull: exprTypes.every(type => type.notNull),
502
+ table: type.table || ''
503
+ };
287
504
  }
288
505
  if (expr.CASE_()) {
289
506
  const resultTypes = []; //then and else
290
507
  const whenTypes = [];
291
508
  expr.expr_list().forEach((expr_, index) => {
292
509
  const type = traverse_expr(expr_, traverseContext);
293
- if (expr_.WHEN__list() || expr_.THEN__list()) {
294
- if (index % 2 == 0) {
295
- whenTypes.push(type);
296
- }
297
- else {
298
- resultTypes.push(type);
299
- }
510
+ if (index % 2 == 0 && (!expr.ELSE_() || index < expr.expr_list().length - 1)) {
511
+ whenTypes.push(type.type);
300
512
  }
301
- if (expr_.ELSE_()) {
302
- resultTypes.push(type);
513
+ else {
514
+ resultTypes.push(type.type);
303
515
  }
304
516
  });
305
517
  resultTypes.forEach((resultType, index) => {
@@ -311,26 +523,35 @@ function traverse_expr(expr, traverseContext) {
311
523
  });
312
524
  }
313
525
  });
314
- return resultTypes[0];
526
+ whenTypes.forEach((whenType) => {
527
+ traverseContext.constraints.push({
528
+ expression: expr.getText(),
529
+ type1: (0, collect_constraints_1.freshVar)('INTEGER', 'INTEGER'),
530
+ type2: whenType
531
+ });
532
+ });
533
+ const type = resultTypes[0];
534
+ return {
535
+ name: type.name,
536
+ type: type,
537
+ notNull: false,
538
+ table: type.table || ''
539
+ };
315
540
  }
316
541
  throw Error('traverse_expr not supported:' + expr.getText());
317
542
  }
318
- function inferNotNull_expr(expr, fromColumns) {
319
- var _a;
320
- const column_name = expr.column_name();
321
- if (column_name) {
322
- const fieldName = (0, select_columns_1.splitName)(column_name.getText());
323
- const column = (0, select_columns_1.findColumn)(fieldName, fromColumns);
324
- return column.notNull;
325
- }
326
- const function_name = (_a = expr.function_name()) === null || _a === void 0 ? void 0 : _a.getText().toLowerCase();
327
- if (function_name == 'count') {
328
- return true;
329
- }
330
- return false;
543
+ function traverse_column_name(column_name, traverseContext) {
544
+ const fieldName = (0, select_columns_1.splitName)(column_name.getText());
545
+ const column = (0, select_columns_1.findColumn)(fieldName, traverseContext.fromColumns);
546
+ const typeVar = (0, collect_constraints_1.freshVar)(column.columnName, column.columnType.type, column.tableAlias || column.table);
547
+ return {
548
+ name: typeVar.name,
549
+ type: typeVar,
550
+ table: column.tableAlias || column.table,
551
+ notNull: column.notNull
552
+ };
331
553
  }
332
- function isMultipleRowResult(sql_stmtContext, fromColumns) {
333
- const select_stmt = sql_stmtContext.select_stmt();
554
+ function isMultipleRowResult(select_stmt, fromColumns) {
334
555
  if (select_stmt.select_core_list().length == 1) { //UNION queries are multipleRowsResult = true
335
556
  const select_core = select_stmt.select_core(0);
336
557
  const from = select_core.FROM_();
@@ -358,7 +579,9 @@ function isAgregateFunction(result_column) {
358
579
  const function_name = (_b = (_a = result_column.expr()) === null || _a === void 0 ? void 0 : _a.function_name()) === null || _b === void 0 ? void 0 : _b.getText().toLowerCase();
359
580
  return function_name == 'count'
360
581
  || function_name == 'sum'
361
- || function_name == 'avg';
582
+ || function_name == 'avg'
583
+ || function_name == 'min'
584
+ || function_name == 'max';
362
585
  }
363
586
  function isLimitOne(select_stmt) {
364
587
  const limit_stmt = select_stmt.limit_stmt();
@@ -393,4 +616,83 @@ function is_single_result(expr, fromColumns) {
393
616
  }
394
617
  return false;
395
618
  }
619
+ function traverse_insert_stmt(insert_stmt, traverseContext) {
620
+ const table_name = insert_stmt.table_name();
621
+ const fromColumns = (0, select_columns_1.filterColumns)(traverseContext.dbSchema, [], '', (0, select_columns_1.splitName)(table_name.getText()));
622
+ const columns = insert_stmt.column_name_list().map(column_name => {
623
+ return traverse_column_name(column_name, Object.assign(Object.assign({}, traverseContext), { fromColumns }));
624
+ });
625
+ const insertColumns = [];
626
+ const value_row_list = insert_stmt.values_clause().value_row_list();
627
+ value_row_list.forEach((value_row) => {
628
+ value_row.expr_list().forEach((expr, index) => {
629
+ const numberParamsBefore = traverseContext.parameters.length;
630
+ const exprType = traverse_expr(expr, traverseContext);
631
+ traverseContext.parameters.slice(numberParamsBefore).forEach((param) => {
632
+ const col = columns[index];
633
+ traverseContext.constraints.push({
634
+ expression: expr.getText(),
635
+ type1: col.type,
636
+ type2: exprType.type
637
+ });
638
+ insertColumns.push(Object.assign(Object.assign({}, param), { notNull: exprType.name == '?' ? col.notNull : param.notNull }));
639
+ });
640
+ });
641
+ });
642
+ const queryResult = {
643
+ queryType: 'Insert',
644
+ columns: insertColumns
645
+ };
646
+ return queryResult;
647
+ }
648
+ function traverse_update_stmt(update_stmt, traverseContext) {
649
+ const table_name = update_stmt.qualified_table_name().getText();
650
+ const fromColumns = (0, select_columns_1.filterColumns)(traverseContext.dbSchema, [], '', (0, select_columns_1.splitName)(table_name));
651
+ const column_name_list = Array.from({ length: update_stmt.ASSIGN_list().length })
652
+ .map((_, i) => update_stmt.column_name(i));
653
+ const columns = column_name_list.map(column_name => {
654
+ return traverse_column_name(column_name, Object.assign(Object.assign({}, traverseContext), { fromColumns }));
655
+ });
656
+ const updateColumns = [];
657
+ const whereParams = [];
658
+ const expr_list = update_stmt.expr_list();
659
+ let paramsBefore = traverseContext.parameters.length;
660
+ expr_list.forEach((expr, index) => {
661
+ paramsBefore = traverseContext.parameters.length;
662
+ const exprType = traverse_expr(expr, Object.assign(Object.assign({}, traverseContext), { fromColumns }));
663
+ if (!update_stmt.WHERE_() || expr.start.start < update_stmt.WHERE_().symbol.start) {
664
+ const col = columns[index];
665
+ traverseContext.constraints.push({
666
+ expression: expr.getText(),
667
+ type1: col.type,
668
+ type2: exprType.type
669
+ });
670
+ traverseContext.parameters.slice(paramsBefore).forEach((param, index) => {
671
+ updateColumns.push(Object.assign(Object.assign({}, param), { notNull: param.notNull && col.notNull }));
672
+ });
673
+ }
674
+ else {
675
+ traverseContext.parameters.slice(paramsBefore).forEach((param, index) => {
676
+ whereParams.push(param);
677
+ });
678
+ }
679
+ });
680
+ const queryResult = {
681
+ queryType: 'Update',
682
+ columns: updateColumns,
683
+ params: whereParams
684
+ };
685
+ return queryResult;
686
+ }
687
+ function traverse_delete_stmt(delete_stmt, traverseContext) {
688
+ const table_name = delete_stmt.qualified_table_name().getText();
689
+ const fromColumns = (0, select_columns_1.filterColumns)(traverseContext.dbSchema, [], '', (0, select_columns_1.splitName)(table_name));
690
+ const expr = delete_stmt.expr();
691
+ traverse_expr(expr, Object.assign(Object.assign({}, traverseContext), { fromColumns }));
692
+ const queryResult = {
693
+ queryType: 'Delete',
694
+ params: traverseContext.parameters
695
+ };
696
+ return queryResult;
697
+ }
396
698
  //# sourceMappingURL=traverse.js.map