typesql-cli 0.17.6 → 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 (55) 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 +25 -2
  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 +4 -4
  19. package/mysql-mapping.d.ts.map +1 -1
  20. package/mysql-mapping.js.map +1 -1
  21. package/mysql-query-analyzer/parse.js +1 -1
  22. package/mysql-query-analyzer/parse.js.map +1 -1
  23. package/mysql-query-analyzer/types.d.ts +3 -3
  24. package/mysql-query-analyzer/types.d.ts.map +1 -1
  25. package/package.json +1 -1
  26. package/postgres-query-analyzer/describe.d.ts +3 -5
  27. package/postgres-query-analyzer/describe.d.ts.map +1 -1
  28. package/postgres-query-analyzer/describe.js +8 -12
  29. package/postgres-query-analyzer/describe.js.map +1 -1
  30. package/postgres-query-analyzer/traverse.d.ts +3 -1
  31. package/postgres-query-analyzer/traverse.d.ts.map +1 -1
  32. package/postgres-query-analyzer/traverse.js +419 -85
  33. package/postgres-query-analyzer/traverse.js.map +1 -1
  34. package/postgres-query-analyzer/types.d.ts +29 -0
  35. package/postgres-query-analyzer/types.d.ts.map +1 -0
  36. package/postgres-query-analyzer/types.js +3 -0
  37. package/postgres-query-analyzer/types.js.map +1 -0
  38. package/sqlite-query-analyzer/code-generator.d.ts +4 -4
  39. package/sqlite-query-analyzer/code-generator.d.ts.map +1 -1
  40. package/sqlite-query-analyzer/code-generator.js +19 -13
  41. package/sqlite-query-analyzer/code-generator.js.map +1 -1
  42. package/sqlite-query-analyzer/parser.js +2 -2
  43. package/sqlite-query-analyzer/parser.js.map +1 -1
  44. package/sqlite-query-analyzer/sqlite-describe-nested-query.d.ts +6 -2
  45. package/sqlite-query-analyzer/sqlite-describe-nested-query.d.ts.map +1 -1
  46. package/sqlite-query-analyzer/sqlite-describe-nested-query.js +2 -2
  47. package/sqlite-query-analyzer/sqlite-describe-nested-query.js.map +1 -1
  48. package/sqlite-query-analyzer/traverse.js +1 -1
  49. package/sqlite-query-analyzer/traverse.js.map +1 -1
  50. package/sqlite-query-analyzer/types.d.ts +15 -1
  51. package/sqlite-query-analyzer/types.d.ts.map +1 -1
  52. package/ts-nested-descriptor.js +1 -1
  53. package/ts-nested-descriptor.js.map +1 -1
  54. package/types.d.ts +2 -3
  55. 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,9 +661,90 @@ 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) {
724
+ if (c_expr.ARRAY()) {
725
+ const select_with_parens = c_expr.select_with_parens();
726
+ if (select_with_parens) {
727
+ traverse_select_with_parens(select_with_parens, context, traverseResult);
728
+ return {
729
+ column_name: '?column?',
730
+ is_nullable: true,
731
+ table_schema: '',
732
+ table_name: '',
733
+ type: 'unknow'
734
+ };
735
+ }
736
+ const array_expr = c_expr.array_expr();
737
+ if (array_expr) {
738
+ traverse_array_expr(array_expr, context, traverseResult);
739
+ return {
740
+ column_name: '?column?',
741
+ is_nullable: false,
742
+ table_schema: '',
743
+ table_name: '',
744
+ type: 'unknow'
745
+ };
746
+ }
747
+ }
644
748
  const columnref = c_expr.columnref();
645
749
  if (columnref) {
646
750
  const col = traverseColumnRef(columnref, context.fromColumns);
@@ -648,12 +752,14 @@ function traversec_expr(c_expr, context, traverseResult) {
648
752
  }
649
753
  const aexprconst = c_expr.aexprconst();
650
754
  if (aexprconst) {
755
+ const { name, type } = getNameAndTypeIdFromAExprConst(aexprconst);
651
756
  const is_nullable = aexprconst.NULL_P() != null;
652
757
  return {
653
- column_name: aexprconst.getText(),
758
+ column_name: name,
654
759
  is_nullable,
655
760
  table_name: '',
656
- table_schema: ''
761
+ table_schema: '',
762
+ type
657
763
  };
658
764
  }
659
765
  if (c_expr.PARAM()) {
@@ -665,37 +771,36 @@ function traversec_expr(c_expr, context, traverseResult) {
665
771
  column_name: c_expr.PARAM().getText(),
666
772
  is_nullable: !!context.propagatesNull,
667
773
  table_name: '',
668
- table_schema: ''
774
+ table_schema: '',
775
+ type: 'unknow'
669
776
  };
670
777
  }
671
778
  const func_application = (_a = c_expr.func_expr()) === null || _a === void 0 ? void 0 : _a.func_application();
672
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
+ }
673
786
  const isNotNull = traversefunc_application(func_application, context, traverseResult);
674
- return {
675
- column_name: ((_b = func_application.func_name()) === null || _b === void 0 ? void 0 : _b.getText()) || func_application.getText(),
676
- is_nullable: !isNotNull,
677
- table_name: '',
678
- table_schema: ''
679
- };
787
+ return Object.assign(Object.assign({}, isNotNull), { table_name: '', table_schema: '' });
680
788
  }
681
- 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();
682
790
  if (func_expr_common_subexpr) {
683
791
  const isNotNull = traversefunc_expr_common_subexpr(func_expr_common_subexpr, context, traverseResult);
684
- return {
685
- 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(),
686
- is_nullable: !isNotNull,
687
- table_name: '',
688
- table_schema: ''
689
- };
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() });
690
793
  }
691
794
  const select_with_parens = c_expr.select_with_parens();
692
795
  if (select_with_parens) {
693
- traverse_select_with_parens(select_with_parens, context, traverseResult);
796
+ const result = traverse_select_with_parens(select_with_parens, context, traverseResult);
694
797
  return {
695
798
  column_name: '?column?',
696
799
  is_nullable: true,
697
800
  table_name: '',
698
- table_schema: ''
801
+ table_schema: '',
802
+ type: result[0].type,
803
+ jsonType: result[0].jsonType
699
804
  };
700
805
  }
701
806
  const a_expr_in_parens = c_expr._a_expr_in_parens;
@@ -710,7 +815,8 @@ function traversec_expr(c_expr, context, traverseResult) {
710
815
  column_name: '?column?',
711
816
  is_nullable: expr_list.some(col => col.is_nullable),
712
817
  table_name: '',
713
- table_schema: ''
818
+ table_schema: '',
819
+ type: 'unknow'
714
820
  };
715
821
  }
716
822
  const implicit_row = c_expr.implicit_row();
@@ -721,18 +827,14 @@ function traversec_expr(c_expr, context, traverseResult) {
721
827
  column_name: '?column?',
722
828
  is_nullable: expr_list.some(col => col.is_nullable),
723
829
  table_name: '',
724
- table_schema: ''
830
+ table_schema: '',
831
+ type: 'unknow'
725
832
  };
726
833
  }
727
834
  }
728
835
  if (c_expr instanceof PostgreSQLParser_1.C_expr_caseContext) {
729
836
  const isNotNull = traversec_expr_case(c_expr, context, traverseResult);
730
- return {
731
- column_name: '?column?',
732
- is_nullable: !isNotNull,
733
- table_name: '',
734
- table_schema: ''
735
- };
837
+ return isNotNull;
736
838
  }
737
839
  if (c_expr instanceof PostgreSQLParser_1.C_expr_existsContext) {
738
840
  const select_with_parens = c_expr.select_with_parens();
@@ -741,7 +843,8 @@ function traversec_expr(c_expr, context, traverseResult) {
741
843
  column_name: '?column?',
742
844
  is_nullable: false,
743
845
  table_name: '',
744
- table_schema: ''
846
+ table_schema: '',
847
+ type: 'bool'
745
848
  };
746
849
  }
747
850
  throw Error('traversec_expr - Not expected:' + c_expr.getText());
@@ -753,7 +856,8 @@ function filterColumns(fromColumns, fieldName) {
753
856
  column_name: col.column_name,
754
857
  is_nullable: col.is_nullable,
755
858
  table_name: col.table_name,
756
- table_schema: col.table_schema
859
+ table_schema: col.table_schema,
860
+ type: col.type
757
861
  };
758
862
  return result;
759
863
  });
@@ -766,13 +870,20 @@ function excludeColumns(fromColumns, excludeList) {
766
870
  });
767
871
  }
768
872
  function traversec_expr_case(c_expr_case, context, traverseResult) {
769
- var _a;
873
+ var _a, _b;
770
874
  const case_expr = c_expr_case.case_expr();
771
875
  const whenResult = case_expr.when_clause_list().when_clause_list().map(when_clause => traversewhen_clause(when_clause, context, traverseResult));
772
876
  const whenIsNotNull = whenResult.every(when => when);
773
877
  const elseExpr = (_a = case_expr.case_default()) === null || _a === void 0 ? void 0 : _a.a_expr();
774
878
  const elseIsNotNull = elseExpr ? !traverse_a_expr(elseExpr, context, traverseResult).is_nullable : false;
775
- 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
+ };
776
887
  }
777
888
  function traversewhen_clause(when_clause, context, traverseResult) {
778
889
  const a_expr_list = when_clause.a_expr_list();
@@ -782,7 +893,14 @@ function traversewhen_clause(when_clause, context, traverseResult) {
782
893
  const thenExprResult = traverse_a_expr(thenExpr, context, traverseResult);
783
894
  return thenExprResult;
784
895
  });
785
- 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
+ };
786
904
  }
787
905
  function partition(array, predicate) {
788
906
  return array.reduce((acc, curr, index) => {
@@ -795,40 +913,158 @@ function partition(array, predicate) {
795
913
  return acc;
796
914
  }, [[], []]);
797
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
+ }
798
976
  function traversefunc_application(func_application, context, traverseResult) {
799
977
  var _a;
800
- const functionName = func_application.func_name().getText().toLowerCase();
978
+ const functionName = getFunctionName(func_application);
801
979
  const func_arg_expr_list = ((_a = func_application.func_arg_list()) === null || _a === void 0 ? void 0 : _a.func_arg_expr_list()) || [];
802
980
  const argsResult = func_arg_expr_list.map(func_arg_expr => traversefunc_arg_expr(func_arg_expr, context, traverseResult));
803
981
  if (functionName === 'count') {
804
- return true;
982
+ return {
983
+ column_name: functionName,
984
+ is_nullable: false,
985
+ table_name: '',
986
+ table_schema: '',
987
+ type: 'int8'
988
+ };
805
989
  }
806
990
  if (functionName === 'concat'
807
- || functionName === 'concat_ws'
808
- || functionName === 'to_tsvector'
809
- || functionName === 'to_tsquery'
810
- || 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'
811
1019
  || functionName === 'plainto_tsquery'
812
1020
  || functionName === 'phraseto_tsquery'
813
1021
  || functionName === 'websearch_to_tsquery') {
814
- if (func_arg_expr_list) {
815
- return argsResult.every(col => col);
816
- }
817
- return false;
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
+ };
818
1029
  }
819
1030
  if (functionName === 'to_date') {
820
- const firstArg = argsResult[0];
821
- 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
+ };
822
1038
  }
823
1039
  if (functionName === 'generate_series') {
824
- return true;
1040
+ return {
1041
+ column_name: functionName,
1042
+ is_nullable: false,
1043
+ table_name: '',
1044
+ table_schema: '',
1045
+ type: 'unknow'
1046
+ };
825
1047
  }
826
1048
  if (functionName === 'row_number'
827
1049
  || functionName === 'rank'
828
- || functionName === 'dense_rank'
829
- || 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'
830
1060
  || functionName === 'cume_dist') {
831
- return true;
1061
+ return {
1062
+ column_name: functionName,
1063
+ is_nullable: false,
1064
+ table_name: '',
1065
+ table_schema: '',
1066
+ type: 'float8'
1067
+ };
832
1068
  }
833
1069
  if (functionName === 'first_value'
834
1070
  || functionName === 'last_value'
@@ -836,7 +1072,47 @@ function traversefunc_application(func_application, context, traverseResult) {
836
1072
  const firstArg = argsResult[0];
837
1073
  return firstArg;
838
1074
  }
839
- return false;
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
+ };
1084
+ }
1085
+ if (functionName === 'to_json' || functionName === 'to_jsonb') {
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;
1115
+ }
840
1116
  }
841
1117
  function traversefunc_expr_common_subexpr(func_expr_common_subexpr, context, traverseResult) {
842
1118
  if (func_expr_common_subexpr.COALESCE() || func_expr_common_subexpr.GREATEST() || func_expr_common_subexpr.LEAST()) {
@@ -845,21 +1121,48 @@ function traversefunc_expr_common_subexpr(func_expr_common_subexpr, context, tra
845
1121
  const paramResult = traverse_a_expr(func_arg_expr, Object.assign(Object.assign({}, context), { propagatesNull: true }), traverseResult);
846
1122
  return paramResult;
847
1123
  });
848
- 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: '' });
849
1125
  }
850
1126
  if (func_expr_common_subexpr.EXTRACT()) {
851
1127
  const a_expr = func_expr_common_subexpr.extract_list().a_expr();
852
1128
  const result = traverse_a_expr(a_expr, context, traverseResult);
853
- 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
+ };
854
1136
  }
855
1137
  func_expr_common_subexpr.a_expr_list().forEach(a_expr => {
856
1138
  traverse_a_expr(a_expr, context, traverseResult);
857
1139
  });
858
- 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
+ };
859
1147
  }
860
1148
  function traversefunc_arg_expr(func_arg_expr, context, traverseResult) {
861
1149
  const a_expr = func_arg_expr.a_expr();
862
- return !traverse_a_expr(a_expr, context, traverseResult).is_nullable;
1150
+ return traverse_a_expr(a_expr, context, traverseResult);
1151
+ }
1152
+ function traverse_array_expr(array_expr, context, traverseResult) {
1153
+ const expr_list = array_expr.expr_list();
1154
+ if (expr_list) {
1155
+ traverse_expr_list(expr_list, context, traverseResult);
1156
+ }
1157
+ const array_expr_list = array_expr.array_expr_list();
1158
+ if (array_expr_list) {
1159
+ traverse_array_expr_list(array_expr_list, context, traverseResult);
1160
+ }
1161
+ }
1162
+ function traverse_array_expr_list(array_expr_list, context, traverseResult) {
1163
+ array_expr_list.array_expr_list().forEach(array_expr => {
1164
+ traverse_array_expr(array_expr, context, traverseResult);
1165
+ });
863
1166
  }
864
1167
  function findColumn(fieldName, fromColumns) {
865
1168
  const col = fromColumns.find(col => (fieldName.prefix === '' || col.table_name.toLowerCase() === fieldName.prefix.toLowerCase()) && col.column_name.toLowerCase() === fieldName.name.toLowerCase());
@@ -888,11 +1191,12 @@ function traverse_from_list(from_list, context, traverseResult) {
888
1191
  return newColumns;
889
1192
  }
890
1193
  function traverse_table_ref(table_ref, context, traverseResult) {
891
- var _a, _b;
1194
+ var _a, _b, _c, _d;
892
1195
  const { fromColumns, dbSchema } = context;
893
1196
  const allColumns = [];
894
1197
  const relation_expr = table_ref.relation_expr();
895
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());
896
1200
  const alias = aliasClause ? aliasClause.colid().getText() : '';
897
1201
  if (relation_expr) {
898
1202
  const tableName = traverse_relation_expr(relation_expr, dbSchema);
@@ -908,11 +1212,11 @@ function traverse_table_ref(table_ref, context, traverseResult) {
908
1212
  alias: alias,
909
1213
  renameAs,
910
1214
  parentRelation: '',
911
- 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) || '',
912
1216
  cardinality: 'one',
913
1217
  parentCardinality: 'one'
914
1218
  };
915
- (_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);
916
1220
  }
917
1221
  const table_ref_list = table_ref.table_ref_list();
918
1222
  const join_type_list = table_ref.join_type_list();
@@ -945,7 +1249,13 @@ function traverse_table_ref(table_ref, context, traverseResult) {
945
1249
  const select_with_parens = table_ref.select_with_parens();
946
1250
  if (select_with_parens) {
947
1251
  const columns = traverse_select_with_parens(select_with_parens, Object.assign(Object.assign({}, context), { collectDynamicQueryInfo: false }), traverseResult);
948
- 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
+ }));
949
1259
  return withAlias;
950
1260
  }
951
1261
  return allColumns;
@@ -1340,6 +1650,30 @@ function isNotNul_a_expr_unary_qualop(a_expr_unary_qualop, field) {
1340
1650
  }
1341
1651
  return false;
1342
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
+ }
1343
1677
  function isNotNull_a_expr_add(a_expr_add, field) {
1344
1678
  const a_expr_mul_list = a_expr_add.a_expr_mul_list();
1345
1679
  if (a_expr_mul_list) {
@@ -1481,7 +1815,7 @@ function getTableName(table_ref) {
1481
1815
  }
1482
1816
  function isAggregateFunction_target_el(target_el) {
1483
1817
  if (target_el instanceof PostgreSQLParser_1.Target_labelContext) {
1484
- const c_expr_list = collectContextsOfType(target_el, PostgreSQLParser_1.Func_exprContext);
1818
+ const c_expr_list = collectContextsOfType(target_el, PostgreSQLParser_1.Func_exprContext, false);
1485
1819
  const aggrFunction = c_expr_list.some(func_expr => isAggregateFunction_c_expr(func_expr));
1486
1820
  return aggrFunction;
1487
1821
  }