typesql-cli 0.17.7 → 0.18.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 (54) hide show
  1. package/code-generator.js +1 -1
  2. package/code-generator.js.map +1 -1
  3. package/code-generator2.d.ts.map +1 -1
  4. package/code-generator2.js +80 -12
  5. package/code-generator2.js.map +1 -1
  6. package/describe-nested-query.js +1 -1
  7. package/describe-nested-query.js.map +1 -1
  8. package/describe-query.js +4 -4
  9. package/describe-query.js.map +1 -1
  10. package/dialects/postgres.d.ts.map +1 -1
  11. package/dialects/postgres.js +10 -1
  12. package/dialects/postgres.js.map +1 -1
  13. package/drivers/postgres.d.ts.map +1 -1
  14. package/drivers/postgres.js +6 -5
  15. package/drivers/postgres.js.map +1 -1
  16. package/drivers/types.d.ts +2 -2
  17. package/drivers/types.d.ts.map +1 -1
  18. package/mysql-mapping.d.ts +3 -3
  19. package/mysql-mapping.d.ts.map +1 -1
  20. package/mysql-query-analyzer/parse.js +1 -1
  21. package/mysql-query-analyzer/parse.js.map +1 -1
  22. package/mysql-query-analyzer/types.d.ts +3 -3
  23. package/mysql-query-analyzer/types.d.ts.map +1 -1
  24. package/package.json +1 -1
  25. package/postgres-query-analyzer/describe.d.ts +3 -5
  26. package/postgres-query-analyzer/describe.d.ts.map +1 -1
  27. package/postgres-query-analyzer/describe.js +8 -12
  28. package/postgres-query-analyzer/describe.js.map +1 -1
  29. package/postgres-query-analyzer/traverse.d.ts +3 -1
  30. package/postgres-query-analyzer/traverse.d.ts.map +1 -1
  31. package/postgres-query-analyzer/traverse.js +399 -102
  32. package/postgres-query-analyzer/traverse.js.map +1 -1
  33. package/postgres-query-analyzer/types.d.ts +29 -0
  34. package/postgres-query-analyzer/types.d.ts.map +1 -0
  35. package/postgres-query-analyzer/types.js +3 -0
  36. package/postgres-query-analyzer/types.js.map +1 -0
  37. package/sqlite-query-analyzer/code-generator.d.ts +4 -4
  38. package/sqlite-query-analyzer/code-generator.d.ts.map +1 -1
  39. package/sqlite-query-analyzer/code-generator.js +19 -13
  40. package/sqlite-query-analyzer/code-generator.js.map +1 -1
  41. package/sqlite-query-analyzer/parser.js +2 -2
  42. package/sqlite-query-analyzer/parser.js.map +1 -1
  43. package/sqlite-query-analyzer/sqlite-describe-nested-query.d.ts +6 -2
  44. package/sqlite-query-analyzer/sqlite-describe-nested-query.d.ts.map +1 -1
  45. package/sqlite-query-analyzer/sqlite-describe-nested-query.js +2 -2
  46. package/sqlite-query-analyzer/sqlite-describe-nested-query.js.map +1 -1
  47. package/sqlite-query-analyzer/traverse.js +1 -1
  48. package/sqlite-query-analyzer/traverse.js.map +1 -1
  49. package/sqlite-query-analyzer/types.d.ts +16 -1
  50. package/sqlite-query-analyzer/types.d.ts.map +1 -1
  51. package/ts-nested-descriptor.js +1 -1
  52. package/ts-nested-descriptor.js.map +1 -1
  53. package/types.d.ts +2 -3
  54. package/types.d.ts.map +1 -1
@@ -60,7 +60,7 @@ function traverseSmt(stmt, dbSchema, checkConstraints, options) {
60
60
  }
61
61
  throw Error('Stmt not supported: ' + stmt.getText());
62
62
  }
63
- function collectContextsOfType(ctx, targetType) {
63
+ function collectContextsOfType(ctx, targetType, includeSubQuery = true) {
64
64
  var _a;
65
65
  const results = [];
66
66
  if (ctx instanceof targetType) {
@@ -68,7 +68,9 @@ function collectContextsOfType(ctx, targetType) {
68
68
  }
69
69
  (_a = ctx.children) === null || _a === void 0 ? void 0 : _a.forEach(child => {
70
70
  if (child instanceof typesql_parser_1.ParserRuleContext) {
71
- results.push(...collectContextsOfType(child, targetType));
71
+ if (includeSubQuery || !(child instanceof PostgreSQLParser_1.Select_with_parensContext)) {
72
+ results.push(...collectContextsOfType(child, targetType, includeSubQuery));
73
+ }
72
74
  }
73
75
  });
74
76
  return results;
@@ -179,7 +181,7 @@ function traverse_select_clause(select_clause, context, traverseResult) {
179
181
  for (let index = 1; index < simple_select_intersect_list.length; index++) {
180
182
  const unionNotNull = traverse_simple_select_intersect(simple_select_intersect_list[index], context, traverseResult);
181
183
  selectColumns = selectColumns.map((value, columnIndex) => {
182
- const col = Object.assign({ column_name: value.column_name, is_nullable: value.is_nullable || unionNotNull[columnIndex].is_nullable, table_name: '', table_schema: '' }, (value.column_default && { column_default: value.column_default }));
184
+ const col = Object.assign({ column_name: value.column_name, is_nullable: value.is_nullable || unionNotNull[columnIndex].is_nullable, table_name: '', table_schema: '', type: value.type }, (value.column_default && { column_default: value.column_default }));
183
185
  return col;
184
186
  });
185
187
  }
@@ -249,7 +251,18 @@ function extractRelations(a_expr) {
249
251
  }
250
252
  function traverse_values_clause(values_clause, context, traverseResult) {
251
253
  const expr_list_list = values_clause.expr_list_list();
252
- return expr_list_list.flatMap(expr_list => traverse_expr_list(expr_list, context, traverseResult));
254
+ const values_result = expr_list_list.map(expr_list => traverse_expr_list(expr_list, context, traverseResult));
255
+ return computeNullability(values_result);
256
+ }
257
+ function computeNullability(values_result) {
258
+ const result = values_result[0].map((_, i) => ({
259
+ column_name: `column${i + 1}`,
260
+ is_nullable: values_result.some(row => row[i].is_nullable),
261
+ table_name: '',
262
+ table_schema: '',
263
+ type: values_result[0][i].type
264
+ }));
265
+ return result;
253
266
  }
254
267
  function traverse_expr_list(expr_list, context, traverseResult) {
255
268
  const columns = expr_list.a_expr_list().map((a_expr, index) => {
@@ -327,12 +340,7 @@ function traverse_target_el(target_el, context, traverseResult) {
327
340
  parameters
328
341
  });
329
342
  }
330
- return {
331
- column_name: alias || exprResult.column_name,
332
- is_nullable: exprResult.is_nullable,
333
- table_name: exprResult.table_name,
334
- table_schema: exprResult.table_schema
335
- };
343
+ return Object.assign({ column_name: alias || exprResult.column_name, is_nullable: exprResult.is_nullable, table_name: exprResult.table_name, table_schema: exprResult.table_schema, type: exprResult.type }, (exprResult.jsonType != null && { jsonType: exprResult.jsonType }));
336
344
  }
337
345
  throw Error('Column not found');
338
346
  }
@@ -346,7 +354,8 @@ function traverse_a_expr(a_expr, context, traverseResult) {
346
354
  column_name: '',
347
355
  is_nullable: true,
348
356
  table_name: '',
349
- table_schema: ''
357
+ table_schema: '',
358
+ type: 'unknow'
350
359
  };
351
360
  }
352
361
  function traverse_a_expr_qual(a_expr_qual, context, traverseResult) {
@@ -373,7 +382,8 @@ function traverse_expr_or(a_expr_or, context, traverseResult) {
373
382
  column_name: '?column?',
374
383
  is_nullable: result.some(col => col.is_nullable),
375
384
  table_name: '',
376
- table_schema: ''
385
+ table_schema: '',
386
+ type: 'bool'
377
387
  };
378
388
  }
379
389
  function traverse_expr_and(a_expr_and, context, traverseResult) {
@@ -385,7 +395,8 @@ function traverse_expr_and(a_expr_and, context, traverseResult) {
385
395
  column_name: '?column?',
386
396
  is_nullable: result.some(col => col.is_nullable),
387
397
  table_name: '',
388
- table_schema: ''
398
+ table_schema: '',
399
+ type: 'bool'
389
400
  };
390
401
  }
391
402
  function traverse_expr_between(a_expr_between, context, traverseResult) {
@@ -401,7 +412,8 @@ function traverse_expr_between(a_expr_between, context, traverseResult) {
401
412
  column_name: a_expr_between.getText(),
402
413
  is_nullable: false,
403
414
  table_name: '',
404
- table_schema: ''
415
+ table_schema: '',
416
+ type: 'bool'
405
417
  };
406
418
  }
407
419
  function traverse_expr_in(a_expr_in, context, traverseResult) {
@@ -423,7 +435,8 @@ function traverse_expr_in(a_expr_in, context, traverseResult) {
423
435
  // value -> is_nullable = leftExprResult.is_nullable
424
436
  is_nullable: in_expr != null ? false : leftExprResult.is_nullable,
425
437
  table_name: '',
426
- table_schema: ''
438
+ table_schema: '',
439
+ type: 'bool'
427
440
  };
428
441
  }
429
442
  function traverse_in_expr(in_expr, context, traverseResult) {
@@ -496,7 +509,8 @@ function traverse_expr_compare(a_expr_compare, context, traverseResult) {
496
509
  column_name: '?column?',
497
510
  is_nullable: result.some(col => col.is_nullable),
498
511
  table_name: '',
499
- table_schema: ''
512
+ table_schema: '',
513
+ type: 'bool'
500
514
  };
501
515
  }
502
516
  const select_with_parens = a_expr_compare.select_with_parens();
@@ -506,7 +520,8 @@ function traverse_expr_compare(a_expr_compare, context, traverseResult) {
506
520
  column_name: '?column?',
507
521
  is_nullable: result.some(col => col.is_nullable),
508
522
  table_name: '',
509
- table_schema: ''
523
+ table_schema: '',
524
+ type: result[0].type
510
525
  };
511
526
  }
512
527
  const a_expr = a_expr_compare.a_expr();
@@ -516,7 +531,8 @@ function traverse_expr_compare(a_expr_compare, context, traverseResult) {
516
531
  column_name: '?column?',
517
532
  is_nullable: result.is_nullable,
518
533
  table_name: '',
519
- table_schema: ''
534
+ table_schema: '',
535
+ type: result.type
520
536
  };
521
537
  }
522
538
  throw Error('traverse_expr_compare - Not expected:' + a_expr_compare.getText());
@@ -532,7 +548,8 @@ function traverse_expr_like(a_expr_like, context, traverseResult) {
532
548
  column_name: '?column?',
533
549
  is_nullable: result.some(col => col.is_nullable),
534
550
  table_name: '',
535
- table_schema: ''
551
+ table_schema: '',
552
+ type: 'unknow'
536
553
  };
537
554
  }
538
555
  throw Error('traverse_expr_like - Not expected:' + a_expr_like.getText());
@@ -548,7 +565,8 @@ function traverse_expr_qual_op(a_expr_qual_op, context, traverseResult) {
548
565
  column_name: '?column?',
549
566
  is_nullable: result.some(col => col.is_nullable),
550
567
  table_name: '',
551
- table_schema: ''
568
+ table_schema: '',
569
+ type: 'unknow'
552
570
  };
553
571
  }
554
572
  throw Error('traverse_expr_qual_op - Not expected:' + a_expr_qual_op.getText());
@@ -556,6 +574,7 @@ function traverse_expr_qual_op(a_expr_qual_op, context, traverseResult) {
556
574
  function traverse_expr_unary_qualop(a_expr_unary_qualop, context, traverseResult) {
557
575
  const a_expr_add = a_expr_unary_qualop.a_expr_add();
558
576
  if (a_expr_add) {
577
+ //a_expr_mul ((MINUS | PLUS) a_expr_mul)*
559
578
  const exprResult = a_expr_add.a_expr_mul_list().map(a_expr_mul => traverse_expr_mul(a_expr_mul, context, traverseResult));
560
579
  if (exprResult.length === 1) {
561
580
  return exprResult[0];
@@ -564,7 +583,8 @@ function traverse_expr_unary_qualop(a_expr_unary_qualop, context, traverseResult
564
583
  column_name: '?column?',
565
584
  is_nullable: exprResult.some(col => col.is_nullable),
566
585
  table_name: '',
567
- table_schema: ''
586
+ table_schema: '',
587
+ type: mapAddExprType(exprResult.map(exprResult => exprResult.type))
568
588
  };
569
589
  return result;
570
590
  }
@@ -573,15 +593,17 @@ function traverse_expr_unary_qualop(a_expr_unary_qualop, context, traverseResult
573
593
  function traverse_expr_mul(a_expr_mul, context, traverseResult) {
574
594
  const a_expr_mul_list = a_expr_mul.a_expr_caret_list();
575
595
  if (a_expr_mul_list) {
576
- const notNullInfo = a_expr_mul.a_expr_caret_list().map(a_expr_caret => traverse_expr_caret(a_expr_caret, context, traverseResult));
577
- if (notNullInfo.length === 1) {
578
- return notNullInfo[0];
596
+ // a_expr_caret ((STAR | SLASH | PERCENT) a_expr_caret)*
597
+ const exprResult = a_expr_mul.a_expr_caret_list().map(a_expr_caret => traverse_expr_caret(a_expr_caret, context, traverseResult));
598
+ if (exprResult.length === 1) {
599
+ return exprResult[0];
579
600
  }
580
601
  const result = {
581
602
  column_name: '?column?',
582
- is_nullable: notNullInfo.some(notNullInfo => notNullInfo.is_nullable),
603
+ is_nullable: exprResult.some(exprRes => exprRes.is_nullable),
583
604
  table_name: '',
584
- table_schema: ''
605
+ table_schema: '',
606
+ type: mapAddExprType(exprResult.map(exprResult => exprResult.type))
585
607
  };
586
608
  return result;
587
609
  }
@@ -599,7 +621,8 @@ function traverse_expr_caret(a_expr_caret, context, traverseResult) {
599
621
  column_name: '?column?',
600
622
  is_nullable: notNullInfo.some(notNullInfo => notNullInfo.is_nullable),
601
623
  table_name: '',
602
- table_schema: ''
624
+ table_schema: '',
625
+ type: 'unknow'
603
626
  };
604
627
  return result;
605
628
  }
@@ -638,8 +661,65 @@ function traverseColumnRef(columnref, fromColumns) {
638
661
  const col = findColumn(fieldName, fromColumns);
639
662
  return Object.assign(Object.assign({}, col), { is_nullable: col.is_nullable && col.column_default !== true });
640
663
  }
664
+ function getNameAndTypeIdFromAExprConst(a_expr_const) {
665
+ var _a, _b;
666
+ if (a_expr_const.iconst()) {
667
+ return {
668
+ name: a_expr_const.getText(),
669
+ type: 'int4'
670
+ };
671
+ }
672
+ if (a_expr_const.fconst()) {
673
+ return {
674
+ name: a_expr_const.getText(),
675
+ type: 'float4'
676
+ };
677
+ }
678
+ const type_function_name = (_b = (_a = a_expr_const.func_name()) === null || _a === void 0 ? void 0 : _a.type_function_name()) === null || _b === void 0 ? void 0 : _b.getText().toLowerCase();
679
+ if (type_function_name === 'date') {
680
+ return {
681
+ name: 'date',
682
+ type: 'date'
683
+ };
684
+ }
685
+ if (a_expr_const.sconst()) {
686
+ return {
687
+ name: a_expr_const.getText().slice(1, -1),
688
+ type: 'text'
689
+ };
690
+ }
691
+ //binary
692
+ if (a_expr_const.bconst()) {
693
+ return {
694
+ name: a_expr_const.getText(),
695
+ type: 'bit'
696
+ };
697
+ }
698
+ if (a_expr_const.xconst()) {
699
+ return {
700
+ name: a_expr_const.getText(),
701
+ type: 'bytea'
702
+ };
703
+ }
704
+ if (a_expr_const.TRUE_P() || a_expr_const.FALSE_P()) {
705
+ return {
706
+ name: a_expr_const.getText(),
707
+ type: 'bool'
708
+ };
709
+ }
710
+ if (a_expr_const.NULL_P()) {
711
+ return {
712
+ name: a_expr_const.getText(),
713
+ type: 'unknow'
714
+ };
715
+ }
716
+ return {
717
+ name: a_expr_const.getText(),
718
+ type: 'unknow'
719
+ };
720
+ }
641
721
  function traversec_expr(c_expr, context, traverseResult) {
642
- var _a, _b, _c, _d, _e;
722
+ var _a, _b, _c, _d;
643
723
  if (c_expr instanceof PostgreSQLParser_1.C_expr_exprContext) {
644
724
  if (c_expr.ARRAY()) {
645
725
  const select_with_parens = c_expr.select_with_parens();
@@ -649,18 +729,14 @@ function traversec_expr(c_expr, context, traverseResult) {
649
729
  column_name: '?column?',
650
730
  is_nullable: true,
651
731
  table_schema: '',
652
- table_name: ''
732
+ table_name: '',
733
+ type: 'unknow'
653
734
  };
654
735
  }
655
736
  const array_expr = c_expr.array_expr();
656
737
  if (array_expr) {
657
- traverse_array_expr(array_expr, context, traverseResult);
658
- return {
659
- column_name: '?column?',
660
- is_nullable: false,
661
- table_schema: '',
662
- table_name: ''
663
- };
738
+ const result = traverse_array_expr(array_expr, context, traverseResult);
739
+ return Object.assign(Object.assign({}, result), { type: `${result.type}[]` });
664
740
  }
665
741
  }
666
742
  const columnref = c_expr.columnref();
@@ -670,12 +746,14 @@ function traversec_expr(c_expr, context, traverseResult) {
670
746
  }
671
747
  const aexprconst = c_expr.aexprconst();
672
748
  if (aexprconst) {
749
+ const { name, type } = getNameAndTypeIdFromAExprConst(aexprconst);
673
750
  const is_nullable = aexprconst.NULL_P() != null;
674
751
  return {
675
- column_name: aexprconst.getText(),
752
+ column_name: name,
676
753
  is_nullable,
677
754
  table_name: '',
678
- table_schema: ''
755
+ table_schema: '',
756
+ type
679
757
  };
680
758
  }
681
759
  if (c_expr.PARAM()) {
@@ -687,37 +765,36 @@ function traversec_expr(c_expr, context, traverseResult) {
687
765
  column_name: c_expr.PARAM().getText(),
688
766
  is_nullable: !!context.propagatesNull,
689
767
  table_name: '',
690
- table_schema: ''
768
+ table_schema: '',
769
+ type: 'unknow'
691
770
  };
692
771
  }
693
772
  const func_application = (_a = c_expr.func_expr()) === null || _a === void 0 ? void 0 : _a.func_application();
694
773
  if (func_application) {
774
+ if (is_json_build_object_func(func_application)) {
775
+ return traverse_json_build_obj_func(func_application, context, traverseResult);
776
+ }
777
+ if (is_json_agg(func_application)) {
778
+ return traverse_json_agg(func_application, context, traverseResult);
779
+ }
695
780
  const isNotNull = traversefunc_application(func_application, context, traverseResult);
696
- return {
697
- column_name: ((_b = func_application.func_name()) === null || _b === void 0 ? void 0 : _b.getText()) || func_application.getText(),
698
- is_nullable: !isNotNull,
699
- table_name: '',
700
- table_schema: ''
701
- };
781
+ return Object.assign(Object.assign({}, isNotNull), { table_name: '', table_schema: '' });
702
782
  }
703
- const func_expr_common_subexpr = (_c = c_expr.func_expr()) === null || _c === void 0 ? void 0 : _c.func_expr_common_subexpr();
783
+ const func_expr_common_subexpr = (_b = c_expr.func_expr()) === null || _b === void 0 ? void 0 : _b.func_expr_common_subexpr();
704
784
  if (func_expr_common_subexpr) {
705
785
  const isNotNull = traversefunc_expr_common_subexpr(func_expr_common_subexpr, context, traverseResult);
706
- return {
707
- column_name: ((_e = (_d = func_expr_common_subexpr.getText().split('(')) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.trim()) || func_expr_common_subexpr.getText(),
708
- is_nullable: !isNotNull,
709
- table_name: '',
710
- table_schema: ''
711
- };
786
+ return Object.assign(Object.assign({}, isNotNull), { column_name: ((_d = (_c = func_expr_common_subexpr.getText().split('(')) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.trim()) || func_expr_common_subexpr.getText() });
712
787
  }
713
788
  const select_with_parens = c_expr.select_with_parens();
714
789
  if (select_with_parens) {
715
- traverse_select_with_parens(select_with_parens, context, traverseResult);
790
+ const result = traverse_select_with_parens(select_with_parens, context, traverseResult);
716
791
  return {
717
792
  column_name: '?column?',
718
793
  is_nullable: true,
719
794
  table_name: '',
720
- table_schema: ''
795
+ table_schema: '',
796
+ type: result[0].type,
797
+ jsonType: result[0].jsonType
721
798
  };
722
799
  }
723
800
  const a_expr_in_parens = c_expr._a_expr_in_parens;
@@ -732,7 +809,8 @@ function traversec_expr(c_expr, context, traverseResult) {
732
809
  column_name: '?column?',
733
810
  is_nullable: expr_list.some(col => col.is_nullable),
734
811
  table_name: '',
735
- table_schema: ''
812
+ table_schema: '',
813
+ type: 'unknow'
736
814
  };
737
815
  }
738
816
  const implicit_row = c_expr.implicit_row();
@@ -743,18 +821,14 @@ function traversec_expr(c_expr, context, traverseResult) {
743
821
  column_name: '?column?',
744
822
  is_nullable: expr_list.some(col => col.is_nullable),
745
823
  table_name: '',
746
- table_schema: ''
824
+ table_schema: '',
825
+ type: 'unknow'
747
826
  };
748
827
  }
749
828
  }
750
829
  if (c_expr instanceof PostgreSQLParser_1.C_expr_caseContext) {
751
830
  const isNotNull = traversec_expr_case(c_expr, context, traverseResult);
752
- return {
753
- column_name: '?column?',
754
- is_nullable: !isNotNull,
755
- table_name: '',
756
- table_schema: ''
757
- };
831
+ return isNotNull;
758
832
  }
759
833
  if (c_expr instanceof PostgreSQLParser_1.C_expr_existsContext) {
760
834
  const select_with_parens = c_expr.select_with_parens();
@@ -763,7 +837,8 @@ function traversec_expr(c_expr, context, traverseResult) {
763
837
  column_name: '?column?',
764
838
  is_nullable: false,
765
839
  table_name: '',
766
- table_schema: ''
840
+ table_schema: '',
841
+ type: 'bool'
767
842
  };
768
843
  }
769
844
  throw Error('traversec_expr - Not expected:' + c_expr.getText());
@@ -775,7 +850,8 @@ function filterColumns(fromColumns, fieldName) {
775
850
  column_name: col.column_name,
776
851
  is_nullable: col.is_nullable,
777
852
  table_name: col.table_name,
778
- table_schema: col.table_schema
853
+ table_schema: col.table_schema,
854
+ type: col.type
779
855
  };
780
856
  return result;
781
857
  });
@@ -788,13 +864,20 @@ function excludeColumns(fromColumns, excludeList) {
788
864
  });
789
865
  }
790
866
  function traversec_expr_case(c_expr_case, context, traverseResult) {
791
- var _a;
867
+ var _a, _b;
792
868
  const case_expr = c_expr_case.case_expr();
793
869
  const whenResult = case_expr.when_clause_list().when_clause_list().map(when_clause => traversewhen_clause(when_clause, context, traverseResult));
794
870
  const whenIsNotNull = whenResult.every(when => when);
795
871
  const elseExpr = (_a = case_expr.case_default()) === null || _a === void 0 ? void 0 : _a.a_expr();
796
872
  const elseIsNotNull = elseExpr ? !traverse_a_expr(elseExpr, context, traverseResult).is_nullable : false;
797
- return elseIsNotNull && whenIsNotNull;
873
+ const notNull = elseIsNotNull && whenIsNotNull;
874
+ return {
875
+ column_name: '?column?',
876
+ is_nullable: !notNull,
877
+ table_name: '',
878
+ table_schema: '',
879
+ type: (_b = whenResult[0].type) !== null && _b !== void 0 ? _b : 'unknow'
880
+ };
798
881
  }
799
882
  function traversewhen_clause(when_clause, context, traverseResult) {
800
883
  const a_expr_list = when_clause.a_expr_list();
@@ -804,7 +887,14 @@ function traversewhen_clause(when_clause, context, traverseResult) {
804
887
  const thenExprResult = traverse_a_expr(thenExpr, context, traverseResult);
805
888
  return thenExprResult;
806
889
  });
807
- return whenExprResult.every(res => res);
890
+ const notNull = whenExprResult.every(res => res);
891
+ return {
892
+ column_name: '?column?',
893
+ is_nullable: !notNull,
894
+ table_name: '',
895
+ table_schema: '',
896
+ type: whenExprResult[0].type
897
+ };
808
898
  }
809
899
  function partition(array, predicate) {
810
900
  return array.reduce((acc, curr, index) => {
@@ -817,37 +907,158 @@ function partition(array, predicate) {
817
907
  return acc;
818
908
  }, [[], []]);
819
909
  }
910
+ function getFunctionName(func_application) {
911
+ const functionName = func_application.func_name().getText().toLowerCase();
912
+ return functionName;
913
+ }
914
+ function is_json_build_object_func(func_application) {
915
+ const functionName = getFunctionName(func_application);
916
+ return functionName === 'json_build_object'
917
+ || functionName === 'jsonb_build_object';
918
+ }
919
+ function is_json_agg(func_application) {
920
+ const functionName = getFunctionName(func_application);
921
+ return functionName === 'json_agg'
922
+ || functionName === 'jsonb_agg';
923
+ }
924
+ function transformToJsonProperty(args) {
925
+ const pairs = [];
926
+ for (let i = 0; i < args.length; i += 2) {
927
+ const key = args[i];
928
+ const value = args[i + 1];
929
+ if (value !== undefined) {
930
+ const type = value.jsonType ? value.jsonType : value.type;
931
+ pairs.push({ key: key.column_name, type, notNull: !value.is_nullable });
932
+ }
933
+ }
934
+ return pairs;
935
+ }
936
+ function traverse_json_build_obj_func(func_application, context, traverseResult) {
937
+ var _a, _b;
938
+ const columnName = ((_a = func_application.func_name()) === null || _a === void 0 ? void 0 : _a.getText()) || func_application.getText();
939
+ const func_arg_expr_list = ((_b = func_application.func_arg_list()) === null || _b === void 0 ? void 0 : _b.func_arg_expr_list()) || [];
940
+ const argsResult = func_arg_expr_list.map(func_arg_expr => traversefunc_arg_expr(func_arg_expr, context, traverseResult));
941
+ return {
942
+ column_name: columnName,
943
+ is_nullable: true,
944
+ table_name: '',
945
+ table_schema: '',
946
+ type: 'json',
947
+ jsonType: {
948
+ name: 'json',
949
+ properties: transformToJsonProperty(argsResult),
950
+ }
951
+ };
952
+ }
953
+ function traverse_json_agg(func_application, context, traverseResult) {
954
+ var _a, _b;
955
+ const columnName = ((_a = func_application.func_name()) === null || _a === void 0 ? void 0 : _a.getText()) || func_application.getText();
956
+ const func_arg_expr_list = ((_b = func_application.func_arg_list()) === null || _b === void 0 ? void 0 : _b.func_arg_expr_list()) || [];
957
+ const argsResult = func_arg_expr_list.map(func_arg_expr => traversefunc_arg_expr(func_arg_expr, context, traverseResult));
958
+ return {
959
+ column_name: columnName,
960
+ is_nullable: true,
961
+ table_name: '',
962
+ table_schema: '',
963
+ type: 'json[]',
964
+ jsonType: {
965
+ name: 'json[]',
966
+ properties: argsResult.map(arg => arg.jsonType || arg.type)
967
+ }
968
+ };
969
+ }
820
970
  function traversefunc_application(func_application, context, traverseResult) {
821
971
  var _a;
822
- const functionName = func_application.func_name().getText().toLowerCase();
972
+ const functionName = getFunctionName(func_application);
823
973
  const func_arg_expr_list = ((_a = func_application.func_arg_list()) === null || _a === void 0 ? void 0 : _a.func_arg_expr_list()) || [];
824
974
  const argsResult = func_arg_expr_list.map(func_arg_expr => traversefunc_arg_expr(func_arg_expr, context, traverseResult));
825
975
  if (functionName === 'count') {
826
- return true;
976
+ return {
977
+ column_name: functionName,
978
+ is_nullable: false,
979
+ table_name: '',
980
+ table_schema: '',
981
+ type: 'int8'
982
+ };
827
983
  }
828
984
  if (functionName === 'concat'
829
- || functionName === 'concat_ws'
830
- || functionName === 'to_tsvector'
831
- || functionName === 'to_tsquery'
832
- || functionName === 'ts_rank'
985
+ || functionName === 'concat_ws') {
986
+ return {
987
+ column_name: functionName,
988
+ is_nullable: argsResult.some(col => col.is_nullable),
989
+ table_name: '',
990
+ table_schema: '',
991
+ type: 'text'
992
+ };
993
+ }
994
+ if (functionName === 'to_tsvector') {
995
+ return {
996
+ column_name: functionName,
997
+ is_nullable: argsResult.some(col => col.is_nullable),
998
+ table_name: '',
999
+ table_schema: '',
1000
+ type: 'tsvector'
1001
+ };
1002
+ }
1003
+ if (functionName === 'ts_rank') {
1004
+ return {
1005
+ column_name: functionName,
1006
+ is_nullable: argsResult.some(col => col.is_nullable),
1007
+ table_name: '',
1008
+ table_schema: '',
1009
+ type: 'float4'
1010
+ };
1011
+ }
1012
+ if (functionName === 'to_tsquery'
833
1013
  || functionName === 'plainto_tsquery'
834
1014
  || functionName === 'phraseto_tsquery'
835
1015
  || functionName === 'websearch_to_tsquery') {
836
- return argsResult.every(col => col);
1016
+ return {
1017
+ column_name: functionName,
1018
+ is_nullable: argsResult.some(col => col.is_nullable),
1019
+ table_name: '',
1020
+ table_schema: '',
1021
+ type: 'tsquery'
1022
+ };
837
1023
  }
838
1024
  if (functionName === 'to_date') {
839
- const firstArg = argsResult[0];
840
- return firstArg;
1025
+ return {
1026
+ column_name: functionName,
1027
+ is_nullable: argsResult.some(col => col.is_nullable),
1028
+ table_name: '',
1029
+ table_schema: '',
1030
+ type: 'date'
1031
+ };
841
1032
  }
842
1033
  if (functionName === 'generate_series') {
843
- return true;
1034
+ return {
1035
+ column_name: functionName,
1036
+ is_nullable: false,
1037
+ table_name: '',
1038
+ table_schema: '',
1039
+ type: 'unknow'
1040
+ };
844
1041
  }
845
1042
  if (functionName === 'row_number'
846
1043
  || functionName === 'rank'
847
- || functionName === 'dense_rank'
848
- || functionName === 'percent_rank'
1044
+ || functionName === 'dense_rank') {
1045
+ return {
1046
+ column_name: functionName,
1047
+ is_nullable: false,
1048
+ table_name: '',
1049
+ table_schema: '',
1050
+ type: 'int4'
1051
+ };
1052
+ }
1053
+ if (functionName === 'percent_rank'
849
1054
  || functionName === 'cume_dist') {
850
- return true;
1055
+ return {
1056
+ column_name: functionName,
1057
+ is_nullable: false,
1058
+ table_name: '',
1059
+ table_schema: '',
1060
+ type: 'float8'
1061
+ };
851
1062
  }
852
1063
  if (functionName === 'first_value'
853
1064
  || functionName === 'last_value'
@@ -855,18 +1066,51 @@ function traversefunc_application(func_application, context, traverseResult) {
855
1066
  const firstArg = argsResult[0];
856
1067
  return firstArg;
857
1068
  }
858
- if (functionName === 'json_build_object'
859
- || functionName === 'jsonb_build_object'
860
- || functionName === 'json_build_array'
861
- || functionName === 'jsonb_build_array'
862
- || functionName === 'json_agg'
863
- || functionName === 'jsonb_agg') {
864
- return true;
1069
+ if (functionName === 'json_build_array'
1070
+ || functionName === 'jsonb_build_array') {
1071
+ return {
1072
+ column_name: functionName,
1073
+ is_nullable: true,
1074
+ table_name: '',
1075
+ table_schema: '',
1076
+ type: 'json[]',
1077
+ jsonType: {
1078
+ name: 'json[]',
1079
+ properties: argsResult.map(arg => arg.jsonType || arg.type)
1080
+ }
1081
+ };
865
1082
  }
866
1083
  if (functionName === 'to_json' || functionName === 'to_jsonb') {
867
- return argsResult.every(col => col);
1084
+ return {
1085
+ column_name: functionName,
1086
+ is_nullable: true,
1087
+ table_name: '',
1088
+ table_schema: '',
1089
+ type: 'json'
1090
+ };
1091
+ }
1092
+ if (functionName === 'sum') {
1093
+ return Object.assign(Object.assign({}, argsResult[0]), { column_name: functionName, is_nullable: true, type: argsResult[0].type ? mapSumType(argsResult[0].type) : 'unknow' });
1094
+ }
1095
+ return {
1096
+ column_name: functionName,
1097
+ is_nullable: true,
1098
+ table_name: '',
1099
+ table_schema: '',
1100
+ type: 'unknow'
1101
+ };
1102
+ }
1103
+ function mapSumType(type) {
1104
+ switch (type) {
1105
+ case 'int2':
1106
+ case 'int4':
1107
+ return 'int8';
1108
+ case 'int8':
1109
+ case 'numeric':
1110
+ return 'numeric';
1111
+ default:
1112
+ return type;
868
1113
  }
869
- return false;
870
1114
  }
871
1115
  function traversefunc_expr_common_subexpr(func_expr_common_subexpr, context, traverseResult) {
872
1116
  if (func_expr_common_subexpr.COALESCE() || func_expr_common_subexpr.GREATEST() || func_expr_common_subexpr.LEAST()) {
@@ -875,36 +1119,58 @@ function traversefunc_expr_common_subexpr(func_expr_common_subexpr, context, tra
875
1119
  const paramResult = traverse_a_expr(func_arg_expr, Object.assign(Object.assign({}, context), { propagatesNull: true }), traverseResult);
876
1120
  return paramResult;
877
1121
  });
878
- return result.some(col => !col.is_nullable);
1122
+ return Object.assign(Object.assign({}, result[0]), { is_nullable: result.every(col => col.is_nullable), table_name: '', table_schema: '' });
879
1123
  }
880
1124
  if (func_expr_common_subexpr.EXTRACT()) {
881
1125
  const a_expr = func_expr_common_subexpr.extract_list().a_expr();
882
1126
  const result = traverse_a_expr(a_expr, context, traverseResult);
883
- return !result.is_nullable;
1127
+ return {
1128
+ column_name: 'extract',
1129
+ is_nullable: result.is_nullable,
1130
+ table_name: '',
1131
+ table_schema: '',
1132
+ type: 'float8'
1133
+ };
884
1134
  }
885
1135
  func_expr_common_subexpr.a_expr_list().forEach(a_expr => {
886
1136
  traverse_a_expr(a_expr, context, traverseResult);
887
1137
  });
888
- return false;
1138
+ return {
1139
+ column_name: func_expr_common_subexpr.getText(),
1140
+ is_nullable: true,
1141
+ table_name: '',
1142
+ table_schema: '',
1143
+ type: 'unknow'
1144
+ };
889
1145
  }
890
1146
  function traversefunc_arg_expr(func_arg_expr, context, traverseResult) {
891
1147
  const a_expr = func_arg_expr.a_expr();
892
- return !traverse_a_expr(a_expr, context, traverseResult).is_nullable;
1148
+ return traverse_a_expr(a_expr, context, traverseResult);
893
1149
  }
894
1150
  function traverse_array_expr(array_expr, context, traverseResult) {
895
1151
  const expr_list = array_expr.expr_list();
896
1152
  if (expr_list) {
897
- traverse_expr_list(expr_list, context, traverseResult);
1153
+ const traverse_expr_list_result = traverse_expr_list(expr_list, context, traverseResult);
1154
+ return Object.assign(Object.assign({}, traverse_expr_list_result[0]), { column_name: '?column?', table_name: '', table_schema: '' });
898
1155
  }
899
1156
  const array_expr_list = array_expr.array_expr_list();
900
1157
  if (array_expr_list) {
901
- traverse_array_expr_list(array_expr_list, context, traverseResult);
1158
+ const traverse_array_expr_list_result = traverse_array_expr_list(array_expr_list, context, traverseResult);
1159
+ return Object.assign(Object.assign({}, traverse_array_expr_list_result[0]), { column_name: '?column?', table_name: '', table_schema: '' });
902
1160
  }
1161
+ return {
1162
+ column_name: array_expr.getText(),
1163
+ is_nullable: true,
1164
+ table_name: '',
1165
+ table_schema: '',
1166
+ type: 'unknow'
1167
+ };
903
1168
  }
904
1169
  function traverse_array_expr_list(array_expr_list, context, traverseResult) {
905
- array_expr_list.array_expr_list().forEach(array_expr => {
906
- traverse_array_expr(array_expr, context, traverseResult);
1170
+ const result = array_expr_list.array_expr_list().map(array_expr => {
1171
+ return traverse_array_expr(array_expr, context, traverseResult);
907
1172
  });
1173
+ return result;
908
1174
  }
909
1175
  function findColumn(fieldName, fromColumns) {
910
1176
  const col = fromColumns.find(col => (fieldName.prefix === '' || col.table_name.toLowerCase() === fieldName.prefix.toLowerCase()) && col.column_name.toLowerCase() === fieldName.name.toLowerCase());
@@ -933,11 +1199,12 @@ function traverse_from_list(from_list, context, traverseResult) {
933
1199
  return newColumns;
934
1200
  }
935
1201
  function traverse_table_ref(table_ref, context, traverseResult) {
936
- var _a, _b;
1202
+ var _a, _b, _c, _d;
937
1203
  const { fromColumns, dbSchema } = context;
938
1204
  const allColumns = [];
939
1205
  const relation_expr = table_ref.relation_expr();
940
1206
  const aliasClause = table_ref.alias_clause();
1207
+ const aliasNameList = (_b = (_a = aliasClause === null || aliasClause === void 0 ? void 0 : aliasClause.name_list()) === null || _a === void 0 ? void 0 : _a.name_list()) === null || _b === void 0 ? void 0 : _b.map(name => name.getText());
941
1208
  const alias = aliasClause ? aliasClause.colid().getText() : '';
942
1209
  if (relation_expr) {
943
1210
  const tableName = traverse_relation_expr(relation_expr, dbSchema);
@@ -953,11 +1220,11 @@ function traverse_table_ref(table_ref, context, traverseResult) {
953
1220
  alias: alias,
954
1221
  renameAs,
955
1222
  parentRelation: '',
956
- joinColumn: ((_a = key[0]) === null || _a === void 0 ? void 0 : _a.column_name) || '',
1223
+ joinColumn: ((_c = key[0]) === null || _c === void 0 ? void 0 : _c.column_name) || '',
957
1224
  cardinality: 'one',
958
1225
  parentCardinality: 'one'
959
1226
  };
960
- (_b = traverseResult.relations) === null || _b === void 0 ? void 0 : _b.push(relation);
1227
+ (_d = traverseResult.relations) === null || _d === void 0 ? void 0 : _d.push(relation);
961
1228
  }
962
1229
  const table_ref_list = table_ref.table_ref_list();
963
1230
  const join_type_list = table_ref.join_type_list();
@@ -990,7 +1257,13 @@ function traverse_table_ref(table_ref, context, traverseResult) {
990
1257
  const select_with_parens = table_ref.select_with_parens();
991
1258
  if (select_with_parens) {
992
1259
  const columns = traverse_select_with_parens(select_with_parens, Object.assign(Object.assign({}, context), { collectDynamicQueryInfo: false }), traverseResult);
993
- const withAlias = columns.map(col => (Object.assign(Object.assign({}, col), { table_name: alias || col.table_name })));
1260
+ const withAlias = columns.map((col, i) => ({
1261
+ column_name: (aliasNameList === null || aliasNameList === void 0 ? void 0 : aliasNameList[i]) || col.column_name,
1262
+ is_nullable: col.is_nullable,
1263
+ table_name: alias || col.table_name,
1264
+ table_schema: col.table_schema,
1265
+ type: col.type
1266
+ }));
994
1267
  return withAlias;
995
1268
  }
996
1269
  return allColumns;
@@ -1385,6 +1658,30 @@ function isNotNul_a_expr_unary_qualop(a_expr_unary_qualop, field) {
1385
1658
  }
1386
1659
  return false;
1387
1660
  }
1661
+ function mapAddExprType(types) {
1662
+ const arithmeticMatrix = {
1663
+ int2: { int2: 'int2', int4: 'int4', int8: 'int8', float4: 'float4', float8: 'float8', numeric: 'numeric', date: 'date' },
1664
+ int4: { int2: 'int4', int4: 'int4', int8: 'int8', float4: 'float4', float8: 'float8', numeric: 'numeric', date: 'date' },
1665
+ int8: { int2: 'int8', int4: 'int8', int8: 'int8', float4: 'float8', float8: 'float8', numeric: 'numeric', date: 'date' },
1666
+ float4: { int2: 'float4', int4: 'float4', int8: 'float8', float4: 'float4', float8: 'float8', numeric: 'numeric' },
1667
+ float8: { int2: 'float8', int4: 'float8', int8: 'float8', float4: 'float8', float8: 'float8', numeric: 'numeric' },
1668
+ numeric: { int2: 'numeric', int4: 'numeric', int8: 'numeric', float4: 'numeric', float8: 'numeric', numeric: 'numeric' },
1669
+ date: { int2: 'date', int4: 'date', int8: 'date', date: 'int4', /*interval: 'timestamp'*/ }, // date - date = integer
1670
+ timestamp: { /*interval: 'timestamp', timestamp: 'interval'*/},
1671
+ // interval: { date: 'timestamp', interval: 'interval' }
1672
+ };
1673
+ let currentType = 'int2';
1674
+ for (const type of types) {
1675
+ if (type === 'unknow') {
1676
+ return 'unknow';
1677
+ }
1678
+ currentType = arithmeticMatrix[currentType][type];
1679
+ if (!currentType) {
1680
+ return 'unknow';
1681
+ }
1682
+ }
1683
+ return currentType;
1684
+ }
1388
1685
  function isNotNull_a_expr_add(a_expr_add, field) {
1389
1686
  const a_expr_mul_list = a_expr_add.a_expr_mul_list();
1390
1687
  if (a_expr_mul_list) {
@@ -1526,7 +1823,7 @@ function getTableName(table_ref) {
1526
1823
  }
1527
1824
  function isAggregateFunction_target_el(target_el) {
1528
1825
  if (target_el instanceof PostgreSQLParser_1.Target_labelContext) {
1529
- const c_expr_list = collectContextsOfType(target_el, PostgreSQLParser_1.Func_exprContext);
1826
+ const c_expr_list = collectContextsOfType(target_el, PostgreSQLParser_1.Func_exprContext, false);
1530
1827
  const aggrFunction = c_expr_list.some(func_expr => isAggregateFunction_c_expr(func_expr));
1531
1828
  return aggrFunction;
1532
1829
  }