typesql-cli 0.17.7 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +50 -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 +381 -92
  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 +15 -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,7 +729,8 @@ 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();
@@ -659,7 +740,8 @@ function traversec_expr(c_expr, context, traverseResult) {
659
740
  column_name: '?column?',
660
741
  is_nullable: false,
661
742
  table_schema: '',
662
- table_name: ''
743
+ table_name: '',
744
+ type: 'unknow'
663
745
  };
664
746
  }
665
747
  }
@@ -670,12 +752,14 @@ function traversec_expr(c_expr, context, traverseResult) {
670
752
  }
671
753
  const aexprconst = c_expr.aexprconst();
672
754
  if (aexprconst) {
755
+ const { name, type } = getNameAndTypeIdFromAExprConst(aexprconst);
673
756
  const is_nullable = aexprconst.NULL_P() != null;
674
757
  return {
675
- column_name: aexprconst.getText(),
758
+ column_name: name,
676
759
  is_nullable,
677
760
  table_name: '',
678
- table_schema: ''
761
+ table_schema: '',
762
+ type
679
763
  };
680
764
  }
681
765
  if (c_expr.PARAM()) {
@@ -687,37 +771,36 @@ function traversec_expr(c_expr, context, traverseResult) {
687
771
  column_name: c_expr.PARAM().getText(),
688
772
  is_nullable: !!context.propagatesNull,
689
773
  table_name: '',
690
- table_schema: ''
774
+ table_schema: '',
775
+ type: 'unknow'
691
776
  };
692
777
  }
693
778
  const func_application = (_a = c_expr.func_expr()) === null || _a === void 0 ? void 0 : _a.func_application();
694
779
  if (func_application) {
780
+ if (is_json_build_object_func(func_application)) {
781
+ return traverse_json_build_obj_func(func_application, context, traverseResult);
782
+ }
783
+ if (is_json_agg(func_application)) {
784
+ return traverse_json_agg(func_application, context, traverseResult);
785
+ }
695
786
  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
- };
787
+ return Object.assign(Object.assign({}, isNotNull), { table_name: '', table_schema: '' });
702
788
  }
703
- const func_expr_common_subexpr = (_c = c_expr.func_expr()) === null || _c === void 0 ? void 0 : _c.func_expr_common_subexpr();
789
+ const func_expr_common_subexpr = (_b = c_expr.func_expr()) === null || _b === void 0 ? void 0 : _b.func_expr_common_subexpr();
704
790
  if (func_expr_common_subexpr) {
705
791
  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
- };
792
+ 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
793
  }
713
794
  const select_with_parens = c_expr.select_with_parens();
714
795
  if (select_with_parens) {
715
- traverse_select_with_parens(select_with_parens, context, traverseResult);
796
+ const result = traverse_select_with_parens(select_with_parens, context, traverseResult);
716
797
  return {
717
798
  column_name: '?column?',
718
799
  is_nullable: true,
719
800
  table_name: '',
720
- table_schema: ''
801
+ table_schema: '',
802
+ type: result[0].type,
803
+ jsonType: result[0].jsonType
721
804
  };
722
805
  }
723
806
  const a_expr_in_parens = c_expr._a_expr_in_parens;
@@ -732,7 +815,8 @@ function traversec_expr(c_expr, context, traverseResult) {
732
815
  column_name: '?column?',
733
816
  is_nullable: expr_list.some(col => col.is_nullable),
734
817
  table_name: '',
735
- table_schema: ''
818
+ table_schema: '',
819
+ type: 'unknow'
736
820
  };
737
821
  }
738
822
  const implicit_row = c_expr.implicit_row();
@@ -743,18 +827,14 @@ function traversec_expr(c_expr, context, traverseResult) {
743
827
  column_name: '?column?',
744
828
  is_nullable: expr_list.some(col => col.is_nullable),
745
829
  table_name: '',
746
- table_schema: ''
830
+ table_schema: '',
831
+ type: 'unknow'
747
832
  };
748
833
  }
749
834
  }
750
835
  if (c_expr instanceof PostgreSQLParser_1.C_expr_caseContext) {
751
836
  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
- };
837
+ return isNotNull;
758
838
  }
759
839
  if (c_expr instanceof PostgreSQLParser_1.C_expr_existsContext) {
760
840
  const select_with_parens = c_expr.select_with_parens();
@@ -763,7 +843,8 @@ function traversec_expr(c_expr, context, traverseResult) {
763
843
  column_name: '?column?',
764
844
  is_nullable: false,
765
845
  table_name: '',
766
- table_schema: ''
846
+ table_schema: '',
847
+ type: 'bool'
767
848
  };
768
849
  }
769
850
  throw Error('traversec_expr - Not expected:' + c_expr.getText());
@@ -775,7 +856,8 @@ function filterColumns(fromColumns, fieldName) {
775
856
  column_name: col.column_name,
776
857
  is_nullable: col.is_nullable,
777
858
  table_name: col.table_name,
778
- table_schema: col.table_schema
859
+ table_schema: col.table_schema,
860
+ type: col.type
779
861
  };
780
862
  return result;
781
863
  });
@@ -788,13 +870,20 @@ function excludeColumns(fromColumns, excludeList) {
788
870
  });
789
871
  }
790
872
  function traversec_expr_case(c_expr_case, context, traverseResult) {
791
- var _a;
873
+ var _a, _b;
792
874
  const case_expr = c_expr_case.case_expr();
793
875
  const whenResult = case_expr.when_clause_list().when_clause_list().map(when_clause => traversewhen_clause(when_clause, context, traverseResult));
794
876
  const whenIsNotNull = whenResult.every(when => when);
795
877
  const elseExpr = (_a = case_expr.case_default()) === null || _a === void 0 ? void 0 : _a.a_expr();
796
878
  const elseIsNotNull = elseExpr ? !traverse_a_expr(elseExpr, context, traverseResult).is_nullable : false;
797
- return elseIsNotNull && whenIsNotNull;
879
+ const notNull = elseIsNotNull && whenIsNotNull;
880
+ return {
881
+ column_name: '?column?',
882
+ is_nullable: !notNull,
883
+ table_name: '',
884
+ table_schema: '',
885
+ type: (_b = whenResult[0].type) !== null && _b !== void 0 ? _b : 'unknow'
886
+ };
798
887
  }
799
888
  function traversewhen_clause(when_clause, context, traverseResult) {
800
889
  const a_expr_list = when_clause.a_expr_list();
@@ -804,7 +893,14 @@ function traversewhen_clause(when_clause, context, traverseResult) {
804
893
  const thenExprResult = traverse_a_expr(thenExpr, context, traverseResult);
805
894
  return thenExprResult;
806
895
  });
807
- return whenExprResult.every(res => res);
896
+ const notNull = whenExprResult.every(res => res);
897
+ return {
898
+ column_name: '?column?',
899
+ is_nullable: !notNull,
900
+ table_name: '',
901
+ table_schema: '',
902
+ type: whenExprResult[0].type
903
+ };
808
904
  }
809
905
  function partition(array, predicate) {
810
906
  return array.reduce((acc, curr, index) => {
@@ -817,37 +913,158 @@ function partition(array, predicate) {
817
913
  return acc;
818
914
  }, [[], []]);
819
915
  }
916
+ function getFunctionName(func_application) {
917
+ const functionName = func_application.func_name().getText().toLowerCase();
918
+ return functionName;
919
+ }
920
+ function is_json_build_object_func(func_application) {
921
+ const functionName = getFunctionName(func_application);
922
+ return functionName === 'json_build_object'
923
+ || functionName === 'jsonb_build_object';
924
+ }
925
+ function is_json_agg(func_application) {
926
+ const functionName = getFunctionName(func_application);
927
+ return functionName === 'json_agg'
928
+ || functionName === 'jsonb_agg';
929
+ }
930
+ function transformToJsonProperty(args) {
931
+ const pairs = [];
932
+ for (let i = 0; i < args.length; i += 2) {
933
+ const key = args[i];
934
+ const value = args[i + 1];
935
+ if (value !== undefined) {
936
+ const type = value.jsonType ? value.jsonType : value.type;
937
+ pairs.push({ key: key.column_name, type, notNull: !value.is_nullable });
938
+ }
939
+ }
940
+ return pairs;
941
+ }
942
+ function traverse_json_build_obj_func(func_application, context, traverseResult) {
943
+ var _a, _b;
944
+ const columnName = ((_a = func_application.func_name()) === null || _a === void 0 ? void 0 : _a.getText()) || func_application.getText();
945
+ const func_arg_expr_list = ((_b = func_application.func_arg_list()) === null || _b === void 0 ? void 0 : _b.func_arg_expr_list()) || [];
946
+ const argsResult = func_arg_expr_list.map(func_arg_expr => traversefunc_arg_expr(func_arg_expr, context, traverseResult));
947
+ return {
948
+ column_name: columnName,
949
+ is_nullable: true,
950
+ table_name: '',
951
+ table_schema: '',
952
+ type: 'json',
953
+ jsonType: {
954
+ name: 'json',
955
+ properties: transformToJsonProperty(argsResult),
956
+ }
957
+ };
958
+ }
959
+ function traverse_json_agg(func_application, context, traverseResult) {
960
+ var _a, _b, _c;
961
+ const columnName = ((_a = func_application.func_name()) === null || _a === void 0 ? void 0 : _a.getText()) || func_application.getText();
962
+ const func_arg_expr_list = ((_b = func_application.func_arg_list()) === null || _b === void 0 ? void 0 : _b.func_arg_expr_list()) || [];
963
+ const argsResult = func_arg_expr_list.map(func_arg_expr => traversefunc_arg_expr(func_arg_expr, context, traverseResult));
964
+ return {
965
+ column_name: columnName,
966
+ is_nullable: true,
967
+ table_name: '',
968
+ table_schema: '',
969
+ type: 'json[]',
970
+ jsonType: {
971
+ name: 'json[]',
972
+ properties: ((_c = argsResult[0].jsonType) === null || _c === void 0 ? void 0 : _c.properties) || []
973
+ }
974
+ };
975
+ }
820
976
  function traversefunc_application(func_application, context, traverseResult) {
821
977
  var _a;
822
- const functionName = func_application.func_name().getText().toLowerCase();
978
+ const functionName = getFunctionName(func_application);
823
979
  const func_arg_expr_list = ((_a = func_application.func_arg_list()) === null || _a === void 0 ? void 0 : _a.func_arg_expr_list()) || [];
824
980
  const argsResult = func_arg_expr_list.map(func_arg_expr => traversefunc_arg_expr(func_arg_expr, context, traverseResult));
825
981
  if (functionName === 'count') {
826
- return true;
982
+ return {
983
+ column_name: functionName,
984
+ is_nullable: false,
985
+ table_name: '',
986
+ table_schema: '',
987
+ type: 'int8'
988
+ };
827
989
  }
828
990
  if (functionName === 'concat'
829
- || functionName === 'concat_ws'
830
- || functionName === 'to_tsvector'
831
- || functionName === 'to_tsquery'
832
- || functionName === 'ts_rank'
991
+ || functionName === 'concat_ws') {
992
+ return {
993
+ column_name: functionName,
994
+ is_nullable: argsResult.some(col => col.is_nullable),
995
+ table_name: '',
996
+ table_schema: '',
997
+ type: 'text'
998
+ };
999
+ }
1000
+ if (functionName === 'to_tsvector') {
1001
+ return {
1002
+ column_name: functionName,
1003
+ is_nullable: argsResult.some(col => col.is_nullable),
1004
+ table_name: '',
1005
+ table_schema: '',
1006
+ type: 'tsvector'
1007
+ };
1008
+ }
1009
+ if (functionName === 'ts_rank') {
1010
+ return {
1011
+ column_name: functionName,
1012
+ is_nullable: argsResult.some(col => col.is_nullable),
1013
+ table_name: '',
1014
+ table_schema: '',
1015
+ type: 'float4'
1016
+ };
1017
+ }
1018
+ if (functionName === 'to_tsquery'
833
1019
  || functionName === 'plainto_tsquery'
834
1020
  || functionName === 'phraseto_tsquery'
835
1021
  || functionName === 'websearch_to_tsquery') {
836
- return argsResult.every(col => col);
1022
+ return {
1023
+ column_name: functionName,
1024
+ is_nullable: argsResult.some(col => col.is_nullable),
1025
+ table_name: '',
1026
+ table_schema: '',
1027
+ type: 'tsquery'
1028
+ };
837
1029
  }
838
1030
  if (functionName === 'to_date') {
839
- const firstArg = argsResult[0];
840
- return firstArg;
1031
+ return {
1032
+ column_name: functionName,
1033
+ is_nullable: argsResult.some(col => col.is_nullable),
1034
+ table_name: '',
1035
+ table_schema: '',
1036
+ type: 'date'
1037
+ };
841
1038
  }
842
1039
  if (functionName === 'generate_series') {
843
- return true;
1040
+ return {
1041
+ column_name: functionName,
1042
+ is_nullable: false,
1043
+ table_name: '',
1044
+ table_schema: '',
1045
+ type: 'unknow'
1046
+ };
844
1047
  }
845
1048
  if (functionName === 'row_number'
846
1049
  || functionName === 'rank'
847
- || functionName === 'dense_rank'
848
- || functionName === 'percent_rank'
1050
+ || functionName === 'dense_rank') {
1051
+ return {
1052
+ column_name: functionName,
1053
+ is_nullable: false,
1054
+ table_name: '',
1055
+ table_schema: '',
1056
+ type: 'int4'
1057
+ };
1058
+ }
1059
+ if (functionName === 'percent_rank'
849
1060
  || functionName === 'cume_dist') {
850
- return true;
1061
+ return {
1062
+ column_name: functionName,
1063
+ is_nullable: false,
1064
+ table_name: '',
1065
+ table_schema: '',
1066
+ type: 'float8'
1067
+ };
851
1068
  }
852
1069
  if (functionName === 'first_value'
853
1070
  || functionName === 'last_value'
@@ -855,18 +1072,47 @@ function traversefunc_application(func_application, context, traverseResult) {
855
1072
  const firstArg = argsResult[0];
856
1073
  return firstArg;
857
1074
  }
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;
1075
+ if (functionName === 'json_build_array'
1076
+ || functionName === 'jsonb_build_array') {
1077
+ return {
1078
+ column_name: functionName,
1079
+ is_nullable: true,
1080
+ table_name: '',
1081
+ table_schema: '',
1082
+ type: 'json'
1083
+ };
865
1084
  }
866
1085
  if (functionName === 'to_json' || functionName === 'to_jsonb') {
867
- return argsResult.every(col => col);
1086
+ return {
1087
+ column_name: functionName,
1088
+ is_nullable: true,
1089
+ table_name: '',
1090
+ table_schema: '',
1091
+ type: 'json'
1092
+ };
1093
+ }
1094
+ if (functionName === 'sum') {
1095
+ return Object.assign(Object.assign({}, argsResult[0]), { column_name: functionName, is_nullable: true, type: argsResult[0].type ? mapSumType(argsResult[0].type) : 'unknow' });
1096
+ }
1097
+ return {
1098
+ column_name: functionName,
1099
+ is_nullable: true,
1100
+ table_name: '',
1101
+ table_schema: '',
1102
+ type: 'unknow'
1103
+ };
1104
+ }
1105
+ function mapSumType(type) {
1106
+ switch (type) {
1107
+ case 'int2':
1108
+ case 'int4':
1109
+ return 'int8';
1110
+ case 'int8':
1111
+ case 'numeric':
1112
+ return 'numeric';
1113
+ default:
1114
+ return type;
868
1115
  }
869
- return false;
870
1116
  }
871
1117
  function traversefunc_expr_common_subexpr(func_expr_common_subexpr, context, traverseResult) {
872
1118
  if (func_expr_common_subexpr.COALESCE() || func_expr_common_subexpr.GREATEST() || func_expr_common_subexpr.LEAST()) {
@@ -875,21 +1121,33 @@ function traversefunc_expr_common_subexpr(func_expr_common_subexpr, context, tra
875
1121
  const paramResult = traverse_a_expr(func_arg_expr, Object.assign(Object.assign({}, context), { propagatesNull: true }), traverseResult);
876
1122
  return paramResult;
877
1123
  });
878
- return result.some(col => !col.is_nullable);
1124
+ return Object.assign(Object.assign({}, result[0]), { is_nullable: result.every(col => col.is_nullable), table_name: '', table_schema: '' });
879
1125
  }
880
1126
  if (func_expr_common_subexpr.EXTRACT()) {
881
1127
  const a_expr = func_expr_common_subexpr.extract_list().a_expr();
882
1128
  const result = traverse_a_expr(a_expr, context, traverseResult);
883
- return !result.is_nullable;
1129
+ return {
1130
+ column_name: 'extract',
1131
+ is_nullable: result.is_nullable,
1132
+ table_name: '',
1133
+ table_schema: '',
1134
+ type: 'float8'
1135
+ };
884
1136
  }
885
1137
  func_expr_common_subexpr.a_expr_list().forEach(a_expr => {
886
1138
  traverse_a_expr(a_expr, context, traverseResult);
887
1139
  });
888
- return false;
1140
+ return {
1141
+ column_name: func_expr_common_subexpr.getText(),
1142
+ is_nullable: true,
1143
+ table_name: '',
1144
+ table_schema: '',
1145
+ type: 'unknow'
1146
+ };
889
1147
  }
890
1148
  function traversefunc_arg_expr(func_arg_expr, context, traverseResult) {
891
1149
  const a_expr = func_arg_expr.a_expr();
892
- return !traverse_a_expr(a_expr, context, traverseResult).is_nullable;
1150
+ return traverse_a_expr(a_expr, context, traverseResult);
893
1151
  }
894
1152
  function traverse_array_expr(array_expr, context, traverseResult) {
895
1153
  const expr_list = array_expr.expr_list();
@@ -933,11 +1191,12 @@ function traverse_from_list(from_list, context, traverseResult) {
933
1191
  return newColumns;
934
1192
  }
935
1193
  function traverse_table_ref(table_ref, context, traverseResult) {
936
- var _a, _b;
1194
+ var _a, _b, _c, _d;
937
1195
  const { fromColumns, dbSchema } = context;
938
1196
  const allColumns = [];
939
1197
  const relation_expr = table_ref.relation_expr();
940
1198
  const aliasClause = table_ref.alias_clause();
1199
+ 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
1200
  const alias = aliasClause ? aliasClause.colid().getText() : '';
942
1201
  if (relation_expr) {
943
1202
  const tableName = traverse_relation_expr(relation_expr, dbSchema);
@@ -953,11 +1212,11 @@ function traverse_table_ref(table_ref, context, traverseResult) {
953
1212
  alias: alias,
954
1213
  renameAs,
955
1214
  parentRelation: '',
956
- joinColumn: ((_a = key[0]) === null || _a === void 0 ? void 0 : _a.column_name) || '',
1215
+ joinColumn: ((_c = key[0]) === null || _c === void 0 ? void 0 : _c.column_name) || '',
957
1216
  cardinality: 'one',
958
1217
  parentCardinality: 'one'
959
1218
  };
960
- (_b = traverseResult.relations) === null || _b === void 0 ? void 0 : _b.push(relation);
1219
+ (_d = traverseResult.relations) === null || _d === void 0 ? void 0 : _d.push(relation);
961
1220
  }
962
1221
  const table_ref_list = table_ref.table_ref_list();
963
1222
  const join_type_list = table_ref.join_type_list();
@@ -990,7 +1249,13 @@ function traverse_table_ref(table_ref, context, traverseResult) {
990
1249
  const select_with_parens = table_ref.select_with_parens();
991
1250
  if (select_with_parens) {
992
1251
  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 })));
1252
+ const withAlias = columns.map((col, i) => ({
1253
+ column_name: (aliasNameList === null || aliasNameList === void 0 ? void 0 : aliasNameList[i]) || col.column_name,
1254
+ is_nullable: col.is_nullable,
1255
+ table_name: alias || col.table_name,
1256
+ table_schema: col.table_schema,
1257
+ type: col.type
1258
+ }));
994
1259
  return withAlias;
995
1260
  }
996
1261
  return allColumns;
@@ -1385,6 +1650,30 @@ function isNotNul_a_expr_unary_qualop(a_expr_unary_qualop, field) {
1385
1650
  }
1386
1651
  return false;
1387
1652
  }
1653
+ function mapAddExprType(types) {
1654
+ const arithmeticMatrix = {
1655
+ int2: { int2: 'int2', int4: 'int4', int8: 'int8', float4: 'float4', float8: 'float8', numeric: 'numeric', date: 'date' },
1656
+ int4: { int2: 'int4', int4: 'int4', int8: 'int8', float4: 'float4', float8: 'float8', numeric: 'numeric', date: 'date' },
1657
+ int8: { int2: 'int8', int4: 'int8', int8: 'int8', float4: 'float8', float8: 'float8', numeric: 'numeric', date: 'date' },
1658
+ float4: { int2: 'float4', int4: 'float4', int8: 'float8', float4: 'float4', float8: 'float8', numeric: 'numeric' },
1659
+ float8: { int2: 'float8', int4: 'float8', int8: 'float8', float4: 'float8', float8: 'float8', numeric: 'numeric' },
1660
+ numeric: { int2: 'numeric', int4: 'numeric', int8: 'numeric', float4: 'numeric', float8: 'numeric', numeric: 'numeric' },
1661
+ date: { int2: 'date', int4: 'date', int8: 'date', date: 'int4', /*interval: 'timestamp'*/ }, // date - date = integer
1662
+ timestamp: { /*interval: 'timestamp', timestamp: 'interval'*/},
1663
+ // interval: { date: 'timestamp', interval: 'interval' }
1664
+ };
1665
+ let currentType = 'int2';
1666
+ for (const type of types) {
1667
+ if (type === 'unknow') {
1668
+ return 'unknow';
1669
+ }
1670
+ currentType = arithmeticMatrix[currentType][type];
1671
+ if (!currentType) {
1672
+ return 'unknow';
1673
+ }
1674
+ }
1675
+ return currentType;
1676
+ }
1388
1677
  function isNotNull_a_expr_add(a_expr_add, field) {
1389
1678
  const a_expr_mul_list = a_expr_add.a_expr_mul_list();
1390
1679
  if (a_expr_mul_list) {
@@ -1526,7 +1815,7 @@ function getTableName(table_ref) {
1526
1815
  }
1527
1816
  function isAggregateFunction_target_el(target_el) {
1528
1817
  if (target_el instanceof PostgreSQLParser_1.Target_labelContext) {
1529
- const c_expr_list = collectContextsOfType(target_el, PostgreSQLParser_1.Func_exprContext);
1818
+ const c_expr_list = collectContextsOfType(target_el, PostgreSQLParser_1.Func_exprContext, false);
1530
1819
  const aggrFunction = c_expr_list.some(func_expr => isAggregateFunction_c_expr(func_expr));
1531
1820
  return aggrFunction;
1532
1821
  }