typesql-cli 0.19.0 → 0.19.2

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 (43) hide show
  1. package/cli.js +23 -19
  2. package/cli.js.map +1 -1
  3. package/code-generator.d.ts +3 -2
  4. package/code-generator.d.ts.map +1 -1
  5. package/code-generator.js +27 -20
  6. package/code-generator.js.map +1 -1
  7. package/code-generator2.d.ts +5 -4
  8. package/code-generator2.d.ts.map +1 -1
  9. package/code-generator2.js +74 -62
  10. package/code-generator2.js.map +1 -1
  11. package/dialects/postgres.d.ts +1 -0
  12. package/dialects/postgres.d.ts.map +1 -1
  13. package/dialects/postgres.js +25 -8
  14. package/dialects/postgres.js.map +1 -1
  15. package/drivers/postgres.d.ts +2 -4
  16. package/drivers/postgres.d.ts.map +1 -1
  17. package/drivers/postgres.js +44 -53
  18. package/drivers/postgres.js.map +1 -1
  19. package/drivers/types.d.ts +4 -8
  20. package/drivers/types.d.ts.map +1 -1
  21. package/package.json +1 -1
  22. package/postgres-query-analyzer/describe.d.ts +2 -1
  23. package/postgres-query-analyzer/describe.d.ts.map +1 -1
  24. package/postgres-query-analyzer/describe.js +18 -23
  25. package/postgres-query-analyzer/describe.js.map +1 -1
  26. package/postgres-query-analyzer/traverse.d.ts +7 -3
  27. package/postgres-query-analyzer/traverse.d.ts.map +1 -1
  28. package/postgres-query-analyzer/traverse.js +251 -178
  29. package/postgres-query-analyzer/traverse.js.map +1 -1
  30. package/postgres-query-analyzer/types.d.ts +1 -0
  31. package/postgres-query-analyzer/types.d.ts.map +1 -1
  32. package/schema-info.d.ts +13 -5
  33. package/schema-info.d.ts.map +1 -1
  34. package/schema-info.js +26 -52
  35. package/schema-info.js.map +1 -1
  36. package/sqlite-query-analyzer/code-generator.d.ts +2 -2
  37. package/sqlite-query-analyzer/code-generator.d.ts.map +1 -1
  38. package/sqlite-query-analyzer/code-generator.js +7 -4
  39. package/sqlite-query-analyzer/code-generator.js.map +1 -1
  40. package/sqlite-query-analyzer/types.d.ts +1 -1
  41. package/sqlite-query-analyzer/types.d.ts.map +1 -1
  42. package/types.d.ts +6 -0
  43. package/types.d.ts.map +1 -1
@@ -120,6 +120,7 @@ function traverse_selectstmt(selectstmt, context, traverseResult) {
120
120
  };
121
121
  }
122
122
  function traverse_select_no_parens(select_no_parens, context, traverseResult) {
123
+ var _a;
123
124
  const with_clause = select_no_parens.with_clause();
124
125
  if (with_clause) {
125
126
  with_clause.cte_list().common_table_expr_list()
@@ -137,11 +138,13 @@ function traverse_select_no_parens(select_no_parens, context, traverseResult) {
137
138
  const select_limit = select_no_parens.select_limit();
138
139
  if (select_limit) {
139
140
  const numParamsBefore = traverseResult.parameters.length;
140
- const limit_clause = select_limit.limit_clause();
141
- const limit_a_expr = limit_clause.select_limit_value().a_expr();
142
- traverse_a_expr(limit_a_expr, context, traverseResult);
143
141
  let fragment = '';
142
+ const limit_clause = select_limit.limit_clause();
144
143
  if (limit_clause) {
144
+ const limit_a_expr = (_a = limit_clause.select_limit_value()) === null || _a === void 0 ? void 0 : _a.a_expr();
145
+ if (limit_a_expr) {
146
+ traverse_a_expr(limit_a_expr, context, traverseResult);
147
+ }
145
148
  if (context.collectDynamicQueryInfo) {
146
149
  fragment += (0, select_columns_1.extractOriginalSql)(limit_clause);
147
150
  }
@@ -170,7 +173,7 @@ function traverse_common_table_expr(common_table_expr, context, traverseResult)
170
173
  const select_stmt = common_table_expr.preparablestmt().selectstmt();
171
174
  const numParamsBefore = traverseResult.parameters.length;
172
175
  const selectResult = traverse_selectstmt(select_stmt, Object.assign(Object.assign({}, context), { collectDynamicQueryInfo: false }), traverseResult);
173
- const columnsWithTalbeName = selectResult.columns.map(col => (Object.assign(Object.assign({}, col), { table_name: tableName })));
176
+ const columnsWithTalbeName = selectResult.columns.map(col => (Object.assign(Object.assign({}, col), { table: tableName })));
174
177
  if (context.collectDynamicQueryInfo) {
175
178
  const parameters = traverseResult.parameters.slice(numParamsBefore).map((_, index) => index + numParamsBefore);
176
179
  (_a = traverseResult.dynamicQueryInfo) === null || _a === void 0 ? void 0 : _a.with.push({
@@ -188,11 +191,11 @@ function traverse_select_clause(select_clause, context, traverseResult) {
188
191
  //union
189
192
  const { recursiveTableName, recursiveColumnNames } = context;
190
193
  const recursiveColumns = recursiveTableName ? mainSelectResult.columns
191
- .map((col, index) => { var _a; return (Object.assign(Object.assign({}, col), { table_name: recursiveTableName, column_name: (_a = recursiveColumnNames === null || recursiveColumnNames === void 0 ? void 0 : recursiveColumnNames[index]) !== null && _a !== void 0 ? _a : col.column_name })); }) : [];
194
+ .map((col, index) => { var _a; return (Object.assign(Object.assign({}, col), { table: recursiveTableName, column_name: (_a = recursiveColumnNames === null || recursiveColumnNames === void 0 ? void 0 : recursiveColumnNames[index]) !== null && _a !== void 0 ? _a : col.column_name })); }) : [];
192
195
  for (let index = 1; index < simple_select_intersect_list.length; index++) {
193
196
  const unionResult = traverse_simple_select_intersect(simple_select_intersect_list[index], Object.assign(Object.assign({}, context), { fromColumns: context.fromColumns.concat(recursiveColumns) }), traverseResult);
194
197
  columns = columns.map((value, columnIndex) => {
195
- const col = Object.assign({ column_name: value.column_name, is_nullable: value.is_nullable || unionResult.columns[columnIndex].is_nullable, table_name: '', table_schema: '', type: value.type }, (value.column_default && { column_default: value.column_default }));
198
+ const col = Object.assign({ column_name: value.column_name, is_nullable: value.is_nullable || unionResult.columns[columnIndex].is_nullable, table: '', schema: '', type: value.type }, (value.column_default && { column_default: value.column_default }));
196
199
  return col;
197
200
  });
198
201
  }
@@ -233,7 +236,7 @@ function traverse_simple_select_pramary(simple_select_pramary, context, traverse
233
236
  }
234
237
  const where_a_expr = (_a = simple_select_pramary.where_clause()) === null || _a === void 0 ? void 0 : _a.a_expr();
235
238
  //fromColumns has precedence
236
- //context.fromColumns only becase of insert. update
239
+ //context.fromColumns because of exists(... where), join lateral(... where).
237
240
  const newContext = Object.assign(Object.assign({}, context), { fromColumns: [...context.fromColumns, ...fromResult.columns, ...context.parentColumns] });
238
241
  if (where_a_expr) {
239
242
  const numParamsBefore = traverseResult.parameters.length;
@@ -262,7 +265,7 @@ function traverse_simple_select_pramary(simple_select_pramary, context, traverse
262
265
  if (having_expr) {
263
266
  traverse_a_expr(having_expr, newContext, traverseResult);
264
267
  }
265
- const filteredColumns = filterColumns_simple_select_pramary(simple_select_pramary, Object.assign(Object.assign({}, context), { fromColumns: context.fromColumns.concat(fromResult.columns) }), traverseResult);
268
+ const filteredColumns = filterColumns_simple_select_pramary(simple_select_pramary, Object.assign(Object.assign({}, context), { fromColumns: fromResult.columns, parentColumns: context.fromColumns }), traverseResult);
266
269
  return {
267
270
  columns: filteredColumns,
268
271
  singleRow: fromResult.singleRow
@@ -288,8 +291,8 @@ function computeNullability(values_result) {
288
291
  const result = values_result[0].map((_, i) => ({
289
292
  column_name: `column${i + 1}`,
290
293
  is_nullable: values_result.some(row => row[i].is_nullable),
291
- table_name: '',
292
- table_schema: '',
294
+ table: '',
295
+ schema: '',
293
296
  type: values_result[0][i].type
294
297
  }));
295
298
  return result;
@@ -327,9 +330,9 @@ function traverse_target_list(target_list, context, traverseResult) {
327
330
  columns.forEach(col => {
328
331
  var _a;
329
332
  (_a = traverseResult.dynamicQueryInfo) === null || _a === void 0 ? void 0 : _a.select.push({
330
- fragment: `${col.table_name}.${col.column_name}`,
331
- fragmentWitoutAlias: `${col.table_name}.${col.column_name}`,
332
- dependOnRelations: [col.table_name],
333
+ fragment: `${col.table}.${col.column_name}`,
334
+ fragmentWitoutAlias: `${col.table}.${col.column_name}`,
335
+ dependOnRelations: [col.table],
333
336
  parameters: []
334
337
  });
335
338
  });
@@ -338,7 +341,7 @@ function traverse_target_list(target_list, context, traverseResult) {
338
341
  }
339
342
  const column = traverse_target_el(target_el, context, traverseResult);
340
343
  if (isParameter(column.column_name)) {
341
- traverseResult.parameters.at(-1).isNotNull = !context.fromColumns[index].is_nullable;
344
+ traverseResult.parameters.at(-1).isNotNull = !context.parentColumns[index].is_nullable;
342
345
  }
343
346
  return [column];
344
347
  });
@@ -354,7 +357,7 @@ function traverse_target_el(target_el, context, traverseResult) {
354
357
  const alias = colLabel != null ? get_colid_text(colLabel) : '';
355
358
  if (alias) {
356
359
  (_a = traverseResult.relations) === null || _a === void 0 ? void 0 : _a.forEach(relation => {
357
- if ((relation.name === exprResult.table_name || relation.alias === exprResult.table_name)
360
+ if ((relation.name === exprResult.table || relation.alias === exprResult.table)
358
361
  && relation.joinColumn === exprResult.column_name) {
359
362
  relation.joinColumn = alias;
360
363
  }
@@ -370,7 +373,7 @@ function traverse_target_el(target_el, context, traverseResult) {
370
373
  parameters
371
374
  });
372
375
  }
373
- return Object.assign(Object.assign(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.column_default != null) && { column_default: exprResult.column_default }), (exprResult.column_key != null && { column_key: exprResult.column_key })), (exprResult.jsonType != null && { jsonType: exprResult.jsonType }));
376
+ return Object.assign(Object.assign(Object.assign({ column_name: alias || exprResult.column_name, is_nullable: exprResult.is_nullable, table: exprResult.table, schema: exprResult.schema, type: exprResult.type }, (exprResult.column_default != null) && { column_default: exprResult.column_default }), (exprResult.column_key != null && { column_key: exprResult.column_key })), (exprResult.jsonType != null && { jsonType: exprResult.jsonType }));
374
377
  }
375
378
  throw Error('Column not found');
376
379
  }
@@ -383,8 +386,8 @@ function traverse_a_expr(a_expr, context, traverseResult) {
383
386
  return {
384
387
  column_name: '',
385
388
  is_nullable: true,
386
- table_name: '',
387
- table_schema: '',
389
+ table: '',
390
+ schema: '',
388
391
  type: 'unknown'
389
392
  };
390
393
  }
@@ -411,8 +414,8 @@ function traverse_expr_or(a_expr_or, context, traverseResult) {
411
414
  return {
412
415
  column_name: '?column?',
413
416
  is_nullable: result.some(col => col.is_nullable),
414
- table_name: '',
415
- table_schema: '',
417
+ table: '',
418
+ schema: '',
416
419
  type: 'bool'
417
420
  };
418
421
  }
@@ -424,8 +427,8 @@ function traverse_expr_and(a_expr_and, context, traverseResult) {
424
427
  return {
425
428
  column_name: '?column?',
426
429
  is_nullable: result.some(col => col.is_nullable),
427
- table_name: '',
428
- table_schema: '',
430
+ table: '',
431
+ schema: '',
429
432
  type: 'bool'
430
433
  };
431
434
  }
@@ -441,8 +444,8 @@ function traverse_expr_between(a_expr_between, context, traverseResult) {
441
444
  return {
442
445
  column_name: a_expr_between.getText(),
443
446
  is_nullable: false,
444
- table_name: '',
445
- table_schema: '',
447
+ table: '',
448
+ schema: '',
446
449
  type: 'bool'
447
450
  };
448
451
  }
@@ -464,8 +467,8 @@ function traverse_expr_in(a_expr_in, context, traverseResult) {
464
467
  //id in (...) -> is_nullable: false
465
468
  // value -> is_nullable = leftExprResult.is_nullable
466
469
  is_nullable: in_expr != null ? false : leftExprResult.is_nullable,
467
- table_name: '',
468
- table_schema: '',
470
+ table: '',
471
+ schema: '',
469
472
  type: 'bool'
470
473
  };
471
474
  }
@@ -516,7 +519,7 @@ function checkParamterNullability(column, traverseResult) {
516
519
  }
517
520
  }
518
521
  function getCheckConstraint(col, checkConstraints) {
519
- const key = `[${col.table_schema}][${col.table_name}][${col.column_name}]`;
522
+ const key = `[${col.schema}][${col.table}][${col.column_name}]`;
520
523
  return checkConstraints[key];
521
524
  }
522
525
  function traverse_expr_compare(a_expr_compare, context, traverseResult) {
@@ -539,8 +542,8 @@ function traverse_expr_compare(a_expr_compare, context, traverseResult) {
539
542
  return {
540
543
  column_name: '?column?',
541
544
  is_nullable: result.some(col => col.is_nullable),
542
- table_name: '',
543
- table_schema: '',
545
+ table: '',
546
+ schema: '',
544
547
  type: 'bool'
545
548
  };
546
549
  }
@@ -550,8 +553,8 @@ function traverse_expr_compare(a_expr_compare, context, traverseResult) {
550
553
  return {
551
554
  column_name: '?column?',
552
555
  is_nullable: result.columns.some(col => col.is_nullable),
553
- table_name: '',
554
- table_schema: '',
556
+ table: '',
557
+ schema: '',
555
558
  type: result.columns[0].type
556
559
  };
557
560
  }
@@ -561,8 +564,8 @@ function traverse_expr_compare(a_expr_compare, context, traverseResult) {
561
564
  return {
562
565
  column_name: '?column?',
563
566
  is_nullable: result.is_nullable,
564
- table_name: '',
565
- table_schema: '',
567
+ table: '',
568
+ schema: '',
566
569
  type: result.type
567
570
  };
568
571
  }
@@ -578,8 +581,8 @@ function traverse_expr_like(a_expr_like, context, traverseResult) {
578
581
  return {
579
582
  column_name: '?column?',
580
583
  is_nullable: result.some(col => col.is_nullable),
581
- table_name: '',
582
- table_schema: '',
584
+ table: '',
585
+ schema: '',
583
586
  type: 'unknown'
584
587
  };
585
588
  }
@@ -595,8 +598,8 @@ function traverse_expr_qual_op(a_expr_qual_op, context, traverseResult) {
595
598
  return {
596
599
  column_name: '?column?',
597
600
  is_nullable: result.some(col => col.is_nullable),
598
- table_name: '',
599
- table_schema: '',
601
+ table: '',
602
+ schema: '',
600
603
  type: 'unknown'
601
604
  };
602
605
  }
@@ -613,8 +616,8 @@ function traverse_expr_unary_qualop(a_expr_unary_qualop, context, traverseResult
613
616
  const result = {
614
617
  column_name: '?column?',
615
618
  is_nullable: exprResult.some(col => col.is_nullable),
616
- table_name: '',
617
- table_schema: '',
619
+ table: '',
620
+ schema: '',
618
621
  type: mapAddExprType(exprResult.map(exprResult => exprResult.type))
619
622
  };
620
623
  return result;
@@ -632,8 +635,8 @@ function traverse_expr_mul(a_expr_mul, context, traverseResult) {
632
635
  const result = {
633
636
  column_name: '?column?',
634
637
  is_nullable: exprResult.some(exprRes => exprRes.is_nullable),
635
- table_name: '',
636
- table_schema: '',
638
+ table: '',
639
+ schema: '',
637
640
  type: mapAddExprType(exprResult.map(exprResult => exprResult.type))
638
641
  };
639
642
  return result;
@@ -651,8 +654,8 @@ function traverse_expr_caret(a_expr_caret, context, traverseResult) {
651
654
  const result = {
652
655
  column_name: '?column?',
653
656
  is_nullable: notNullInfo.some(notNullInfo => notNullInfo.is_nullable),
654
- table_name: '',
655
- table_schema: '',
657
+ table: '',
658
+ schema: '',
656
659
  type: 'unknown'
657
660
  };
658
661
  return result;
@@ -765,8 +768,8 @@ function traversec_expr(c_expr, context, traverseResult) {
765
768
  return {
766
769
  column_name: '?column?',
767
770
  is_nullable: false, //empty array
768
- table_schema: '',
769
- table_name: '',
771
+ schema: '',
772
+ table: '',
770
773
  type: 'unknown'
771
774
  };
772
775
  }
@@ -782,8 +785,8 @@ function traversec_expr(c_expr, context, traverseResult) {
782
785
  return {
783
786
  column_name: columnref.getText(),
784
787
  is_nullable: false,
785
- table_name: '',
786
- table_schema: '',
788
+ table: '',
789
+ schema: '',
787
790
  type: 'unknown'
788
791
  };
789
792
  }
@@ -793,8 +796,8 @@ function traversec_expr(c_expr, context, traverseResult) {
793
796
  return {
794
797
  column_name: columnref.getText(),
795
798
  is_nullable: false,
796
- table_name: '',
797
- table_schema: '',
799
+ table: '',
800
+ schema: '',
798
801
  type: 'record',
799
802
  recordTypes: columns
800
803
  };
@@ -811,8 +814,8 @@ function traversec_expr(c_expr, context, traverseResult) {
811
814
  return {
812
815
  column_name: name,
813
816
  is_nullable,
814
- table_name: '',
815
- table_schema: '',
817
+ table: '',
818
+ schema: '',
816
819
  type
817
820
  };
818
821
  }
@@ -824,8 +827,8 @@ function traversec_expr(c_expr, context, traverseResult) {
824
827
  return {
825
828
  column_name: c_expr.PARAM().getText(),
826
829
  is_nullable: !!context.propagatesNull,
827
- table_name: '',
828
- table_schema: '',
830
+ table: '',
831
+ schema: '',
829
832
  type: 'unknown'
830
833
  };
831
834
  }
@@ -847,7 +850,7 @@ function traversec_expr(c_expr, context, traverseResult) {
847
850
  const a_expr = filter_clause.a_expr();
848
851
  traverse_a_expr(a_expr, context, traverseResult);
849
852
  }
850
- return Object.assign(Object.assign({}, isNotNull), { table_name: '', table_schema: '' });
853
+ return Object.assign(Object.assign({}, isNotNull), { table: '', schema: '' });
851
854
  }
852
855
  const func_expr_common_subexpr = (_b = c_expr.func_expr()) === null || _b === void 0 ? void 0 : _b.func_expr_common_subexpr();
853
856
  if (func_expr_common_subexpr) {
@@ -863,8 +866,8 @@ function traversec_expr(c_expr, context, traverseResult) {
863
866
  return {
864
867
  column_name: '?column?',
865
868
  is_nullable,
866
- table_name: '',
867
- table_schema: '',
869
+ table: '',
870
+ schema: '',
868
871
  type: result.columns[0].type,
869
872
  jsonType: jsonType != null && jsonType.name === 'json' ? Object.assign(Object.assign({}, jsonType), { notNull: !is_nullable }) : jsonType
870
873
  };
@@ -880,8 +883,8 @@ function traversec_expr(c_expr, context, traverseResult) {
880
883
  return {
881
884
  column_name: '?column?',
882
885
  is_nullable: expr_list.some(col => col.is_nullable),
883
- table_name: '',
884
- table_schema: '',
886
+ table: '',
887
+ schema: '',
885
888
  type: 'record',
886
889
  recordTypes: expr_list.map(expr => (Object.assign(Object.assign({}, expr), { column_name: '' })))
887
890
  };
@@ -893,8 +896,8 @@ function traversec_expr(c_expr, context, traverseResult) {
893
896
  return {
894
897
  column_name: '?column?',
895
898
  is_nullable: expr_list.some(col => col.is_nullable),
896
- table_name: '',
897
- table_schema: '',
899
+ table: '',
900
+ schema: '',
898
901
  type: 'unknown'
899
902
  };
900
903
  }
@@ -909,17 +912,17 @@ function traversec_expr(c_expr, context, traverseResult) {
909
912
  return {
910
913
  column_name: '?column?',
911
914
  is_nullable: false,
912
- table_name: '',
913
- table_schema: '',
915
+ table: '',
916
+ schema: '',
914
917
  type: 'bool'
915
918
  };
916
919
  }
917
920
  throw Error('traversec_expr - Not expected:' + c_expr.getText());
918
921
  }
919
922
  function filterColumns(fromColumns, fieldName) {
920
- return fromColumns.filter(col => (fieldName.prefix === '' || col.table_name === fieldName.prefix)
923
+ return fromColumns.filter(col => (fieldName.prefix === '' || col.table === fieldName.prefix)
921
924
  && (fieldName.name === '*' || col.column_name === fieldName.name)).map(col => {
922
- const result = Object.assign(Object.assign(Object.assign({ column_name: col.column_name, is_nullable: col.is_nullable, table_name: col.table_name, table_schema: col.table_schema, type: col.type }, (col.column_key !== undefined) && { column_key: col.column_key }), (col.jsonType !== undefined) && { jsonType: col.jsonType }), (col.original_is_nullable !== undefined && { original_is_nullable: col.original_is_nullable }));
925
+ const result = Object.assign(Object.assign(Object.assign({ column_name: col.column_name, is_nullable: col.is_nullable, table: col.table, schema: col.schema, type: col.type }, (col.column_key !== undefined) && { column_key: col.column_key }), (col.jsonType !== undefined) && { jsonType: col.jsonType }), (col.original_is_nullable !== undefined && { original_is_nullable: col.original_is_nullable }));
923
926
  return result;
924
927
  });
925
928
  }
@@ -940,8 +943,8 @@ function traversec_expr_case(c_expr_case, context, traverseResult) {
940
943
  return {
941
944
  column_name: '?column?',
942
945
  is_nullable: !notNull,
943
- table_name: '',
944
- table_schema: '',
946
+ table: '',
947
+ schema: '',
945
948
  type: (_a = whenResult[0].type) !== null && _a !== void 0 ? _a : 'unknown',
946
949
  jsonType: allJsonTypesMatch(whenResult, elseResult) ? whenResult[0].jsonType : undefined
947
950
  };
@@ -979,8 +982,8 @@ function traversewhen_clause(when_clause, context, traverseResult) {
979
982
  return {
980
983
  column_name: '?column?',
981
984
  is_nullable: !notNull,
982
- table_name: '',
983
- table_schema: '',
985
+ table: '',
986
+ schema: '',
984
987
  type: whenExprResult[0].type,
985
988
  jsonType: whenExprResult[0].jsonType
986
989
  };
@@ -1027,10 +1030,10 @@ function mapJsonBuildArgsToJsonProperty(args, filterExpr) {
1027
1030
  }
1028
1031
  function inferJsonNullability(columns, filterExpr) {
1029
1032
  const tables = columns.filter(col => filterExpr && col.original_is_nullable === false
1030
- && isNotNull_a_expr({ name: col.column_name, prefix: col.table_name }, filterExpr))
1031
- .map(col => col.table_name);
1033
+ && isNotNull_a_expr({ name: col.column_name, prefix: col.table }, filterExpr))
1034
+ .map(col => col.table);
1032
1035
  const fields = columns.map(col => {
1033
- return col.original_is_nullable != null && tables.includes(col.table_name) ? !col.original_is_nullable : !col.is_nullable;
1036
+ return col.original_is_nullable != null && tables.includes(col.table) ? !col.original_is_nullable : !col.is_nullable;
1034
1037
  });
1035
1038
  return fields;
1036
1039
  }
@@ -1061,8 +1064,8 @@ function traverse_json_build_obj_func(func_application, context, traverseResult)
1061
1064
  const result = {
1062
1065
  column_name: columnName,
1063
1066
  is_nullable: false,
1064
- table_name: '',
1065
- table_schema: '',
1067
+ table: '',
1068
+ schema: '',
1066
1069
  type: 'json',
1067
1070
  jsonType: {
1068
1071
  name: 'json',
@@ -1080,8 +1083,8 @@ function traverse_json_agg(func_application, context, traverseResult) {
1080
1083
  const result = {
1081
1084
  column_name: columnName,
1082
1085
  is_nullable: context.filter_expr != null,
1083
- table_name: '',
1084
- table_schema: '',
1086
+ table: '',
1087
+ schema: '',
1085
1088
  type: 'json[]',
1086
1089
  jsonType: createJsonTypeForJsonAgg(argsResult[0], context.filter_expr)
1087
1090
  };
@@ -1117,8 +1120,8 @@ function traversefunc_application(func_application, context, traverseResult) {
1117
1120
  return {
1118
1121
  column_name: functionName,
1119
1122
  is_nullable: false,
1120
- table_name: '',
1121
- table_schema: '',
1123
+ table: '',
1124
+ schema: '',
1122
1125
  type: 'json',
1123
1126
  jsonType
1124
1127
  };
@@ -1129,8 +1132,8 @@ function traversefunc_application(func_application, context, traverseResult) {
1129
1132
  return {
1130
1133
  column_name: functionName,
1131
1134
  is_nullable: false,
1132
- table_name: '',
1133
- table_schema: '',
1135
+ table: '',
1136
+ schema: '',
1134
1137
  type: 'int8'
1135
1138
  };
1136
1139
  }
@@ -1139,8 +1142,8 @@ function traversefunc_application(func_application, context, traverseResult) {
1139
1142
  return {
1140
1143
  column_name: functionName,
1141
1144
  is_nullable: argsResult.some(col => col.is_nullable),
1142
- table_name: '',
1143
- table_schema: '',
1145
+ table: '',
1146
+ schema: '',
1144
1147
  type: 'text'
1145
1148
  };
1146
1149
  }
@@ -1148,8 +1151,8 @@ function traversefunc_application(func_application, context, traverseResult) {
1148
1151
  return {
1149
1152
  column_name: functionName,
1150
1153
  is_nullable: argsResult.some(col => col.is_nullable),
1151
- table_name: '',
1152
- table_schema: '',
1154
+ table: '',
1155
+ schema: '',
1153
1156
  type: 'tsvector'
1154
1157
  };
1155
1158
  }
@@ -1157,8 +1160,8 @@ function traversefunc_application(func_application, context, traverseResult) {
1157
1160
  return {
1158
1161
  column_name: functionName,
1159
1162
  is_nullable: argsResult.some(col => col.is_nullable),
1160
- table_name: '',
1161
- table_schema: '',
1163
+ table: '',
1164
+ schema: '',
1162
1165
  type: 'float4'
1163
1166
  };
1164
1167
  }
@@ -1169,8 +1172,8 @@ function traversefunc_application(func_application, context, traverseResult) {
1169
1172
  return {
1170
1173
  column_name: functionName,
1171
1174
  is_nullable: argsResult.some(col => col.is_nullable),
1172
- table_name: '',
1173
- table_schema: '',
1175
+ table: '',
1176
+ schema: '',
1174
1177
  type: 'tsquery'
1175
1178
  };
1176
1179
  }
@@ -1178,8 +1181,8 @@ function traversefunc_application(func_application, context, traverseResult) {
1178
1181
  return {
1179
1182
  column_name: functionName,
1180
1183
  is_nullable: argsResult.some(col => col.is_nullable),
1181
- table_name: '',
1182
- table_schema: '',
1184
+ table: '',
1185
+ schema: '',
1183
1186
  type: 'date'
1184
1187
  };
1185
1188
  }
@@ -1187,8 +1190,8 @@ function traversefunc_application(func_application, context, traverseResult) {
1187
1190
  return {
1188
1191
  column_name: functionName,
1189
1192
  is_nullable: false,
1190
- table_name: '',
1191
- table_schema: '',
1193
+ table: '',
1194
+ schema: '',
1192
1195
  type: 'unknown'
1193
1196
  };
1194
1197
  }
@@ -1198,8 +1201,8 @@ function traversefunc_application(func_application, context, traverseResult) {
1198
1201
  return {
1199
1202
  column_name: functionName,
1200
1203
  is_nullable: false,
1201
- table_name: '',
1202
- table_schema: '',
1204
+ table: '',
1205
+ schema: '',
1203
1206
  type: 'int4'
1204
1207
  };
1205
1208
  }
@@ -1208,8 +1211,8 @@ function traversefunc_application(func_application, context, traverseResult) {
1208
1211
  return {
1209
1212
  column_name: functionName,
1210
1213
  is_nullable: false,
1211
- table_name: '',
1212
- table_schema: '',
1214
+ table: '',
1215
+ schema: '',
1213
1216
  type: 'float8'
1214
1217
  };
1215
1218
  }
@@ -1224,8 +1227,8 @@ function traversefunc_application(func_application, context, traverseResult) {
1224
1227
  return {
1225
1228
  column_name: functionName,
1226
1229
  is_nullable: false,
1227
- table_name: '',
1228
- table_schema: '',
1230
+ table: '',
1231
+ schema: '',
1229
1232
  type: 'json[]',
1230
1233
  jsonType: {
1231
1234
  name: 'json[]',
@@ -1237,8 +1240,8 @@ function traversefunc_application(func_application, context, traverseResult) {
1237
1240
  return {
1238
1241
  column_name: functionName,
1239
1242
  is_nullable: true,
1240
- table_name: '',
1241
- table_schema: '',
1243
+ table: '',
1244
+ schema: '',
1242
1245
  type: 'json'
1243
1246
  };
1244
1247
  }
@@ -1249,8 +1252,8 @@ function traversefunc_application(func_application, context, traverseResult) {
1249
1252
  return {
1250
1253
  column_name: functionName,
1251
1254
  is_nullable: false,
1252
- table_name: '',
1253
- table_schema: '',
1255
+ table: '',
1256
+ schema: '',
1254
1257
  type: 'json',
1255
1258
  jsonType: {
1256
1259
  name: 'json_map',
@@ -1263,15 +1266,15 @@ function traversefunc_application(func_application, context, traverseResult) {
1263
1266
  column_name: functionName,
1264
1267
  is_nullable: argsResult[0].is_nullable,
1265
1268
  type: argsResult[0].type,
1266
- table_name: '',
1267
- table_schema: ''
1269
+ table: '',
1270
+ schema: ''
1268
1271
  };
1269
1272
  }
1270
1273
  return {
1271
1274
  column_name: functionName,
1272
1275
  is_nullable: true,
1273
- table_name: '',
1274
- table_schema: '',
1276
+ table: '',
1277
+ schema: '',
1275
1278
  type: 'unknown'
1276
1279
  };
1277
1280
  }
@@ -1294,7 +1297,7 @@ function traversefunc_expr_common_subexpr(func_expr_common_subexpr, context, tra
1294
1297
  const paramResult = traverse_a_expr(func_arg_expr, Object.assign(Object.assign({}, context), { propagatesNull: true }), traverseResult);
1295
1298
  return paramResult;
1296
1299
  });
1297
- return Object.assign(Object.assign({}, result[0]), { is_nullable: result.every(col => col.is_nullable), table_name: '', table_schema: '' });
1300
+ return Object.assign(Object.assign({}, result[0]), { is_nullable: result.every(col => col.is_nullable), table: '', schema: '' });
1298
1301
  }
1299
1302
  if (func_expr_common_subexpr.EXTRACT()) {
1300
1303
  const a_expr = func_expr_common_subexpr.extract_list().a_expr();
@@ -1302,8 +1305,8 @@ function traversefunc_expr_common_subexpr(func_expr_common_subexpr, context, tra
1302
1305
  return {
1303
1306
  column_name: 'extract',
1304
1307
  is_nullable: result.is_nullable,
1305
- table_name: '',
1306
- table_schema: '',
1308
+ table: '',
1309
+ schema: '',
1307
1310
  type: 'float8'
1308
1311
  };
1309
1312
  }
@@ -1313,8 +1316,8 @@ function traversefunc_expr_common_subexpr(func_expr_common_subexpr, context, tra
1313
1316
  return {
1314
1317
  column_name: func_expr_common_subexpr.getText(),
1315
1318
  is_nullable: true,
1316
- table_name: '',
1317
- table_schema: '',
1319
+ table: '',
1320
+ schema: '',
1318
1321
  type: 'unknown'
1319
1322
  };
1320
1323
  }
@@ -1326,18 +1329,18 @@ function traverse_array_expr(array_expr, context, traverseResult) {
1326
1329
  const expr_list = array_expr.expr_list();
1327
1330
  if (expr_list) {
1328
1331
  const traverse_expr_list_result = traverse_expr_list(expr_list, context, traverseResult);
1329
- return Object.assign(Object.assign({}, traverse_expr_list_result[0]), { is_nullable: traverse_expr_list_result.some(expr => expr.is_nullable), column_name: '?column?', table_name: '', table_schema: '' });
1332
+ return Object.assign(Object.assign({}, traverse_expr_list_result[0]), { is_nullable: traverse_expr_list_result.some(expr => expr.is_nullable), column_name: '?column?', table: '', schema: '' });
1330
1333
  }
1331
1334
  const array_expr_list = array_expr.array_expr_list();
1332
1335
  if (array_expr_list) {
1333
1336
  const traverse_array_expr_list_result = traverse_array_expr_list(array_expr_list, context, traverseResult);
1334
- return Object.assign(Object.assign({}, traverse_array_expr_list_result[0]), { column_name: '?column?', table_name: '', table_schema: '' });
1337
+ return Object.assign(Object.assign({}, traverse_array_expr_list_result[0]), { column_name: '?column?', table: '', schema: '' });
1335
1338
  }
1336
1339
  return {
1337
1340
  column_name: array_expr.getText(),
1338
1341
  is_nullable: true,
1339
- table_name: '',
1340
- table_schema: '',
1342
+ table: '',
1343
+ schema: '',
1341
1344
  type: 'unknown'
1342
1345
  };
1343
1346
  }
@@ -1355,14 +1358,14 @@ function findColumn(fieldName, fromColumns) {
1355
1358
  return col;
1356
1359
  }
1357
1360
  function findColumnOrNull(fieldName, fromColumns) {
1358
- const col = fromColumns.find(col => (fieldName.prefix === '' || col.table_name.toLowerCase() === fieldName.prefix.toLowerCase()) && col.column_name.toLowerCase() === fieldName.name.toLowerCase());
1361
+ const col = fromColumns.find(col => (fieldName.prefix === '' || col.table.toLowerCase() === fieldName.prefix.toLowerCase()) && col.column_name.toLowerCase() === fieldName.name.toLowerCase());
1359
1362
  return col;
1360
1363
  }
1361
1364
  function fieldNameToString(fieldName) {
1362
1365
  return fieldName.prefix !== '' ? `${fieldName.prefix}.${fieldName.name}` : fieldName.name;
1363
1366
  }
1364
1367
  function checkIsNullable(where_clause, field) {
1365
- const isNotNullResult = !field.is_nullable || isNotNull({ name: field.column_name, prefix: field.table_name }, where_clause);
1368
+ const isNotNullResult = !field.is_nullable || isNotNull({ name: field.column_name, prefix: field.table }, where_clause);
1366
1369
  const col = Object.assign(Object.assign({}, field), { is_nullable: !isNotNullResult });
1367
1370
  return col;
1368
1371
  }
@@ -1386,8 +1389,8 @@ function traverse_from_list(from_list, context, traverseResult) {
1386
1389
  };
1387
1390
  }
1388
1391
  function getFromColumns(tableName, withColumns, dbSchema) {
1389
- const filteredWithColumns = withColumns.filter(col => col.table_name.toLowerCase() === tableName.name.toLowerCase());
1390
- const filteredSchema = filteredWithColumns.length > 0 ? filteredWithColumns : dbSchema.filter(col => col.table_name.toLowerCase() === tableName.name.toLowerCase());
1392
+ const filteredWithColumns = withColumns.filter(col => col.table.toLowerCase() === tableName.name.toLowerCase());
1393
+ const filteredSchema = filteredWithColumns.length > 0 ? filteredWithColumns : dbSchema.filter(col => col.table.toLowerCase() === tableName.name.toLowerCase());
1391
1394
  return filteredSchema;
1392
1395
  }
1393
1396
  function checkLeftJoinIsNullable(leftJoin, subsequentJoins, fromColumns) {
@@ -1427,12 +1430,14 @@ function traverse_table_ref(table_ref, context, traverseResult) {
1427
1430
  const relation_expr = table_ref.relation_expr();
1428
1431
  const aliasClause = table_ref.alias_clause();
1429
1432
  const alias = aliasClause ? aliasClause.colid().getText() : '';
1433
+ let singleRow = false;
1430
1434
  if (relation_expr) {
1431
1435
  const tableName = traverse_relation_expr(relation_expr);
1432
1436
  const fromColumns = getFromColumns(tableName, context.fromColumns.concat(context.withColumns), context.dbSchema);
1433
1437
  const tableNameWithAlias = alias ? alias : tableName.name;
1434
- const fromColumnsResult = fromColumns.map(col => (Object.assign(Object.assign({}, col), { table_name: tableNameWithAlias.toLowerCase() })));
1438
+ const fromColumnsResult = fromColumns.map(col => (Object.assign(Object.assign({}, col), { table: tableNameWithAlias.toLowerCase() })));
1435
1439
  allColumns.push(...fromColumnsResult);
1440
+ singleRow = false;
1436
1441
  if (context.collectNestedInfo) {
1437
1442
  const key = fromColumnsResult.filter(col => col.column_key === 'PRI');
1438
1443
  const renameAs = (aliasClause === null || aliasClause === void 0 ? void 0 : aliasClause.AS()) != null;
@@ -1450,41 +1455,64 @@ function traverse_table_ref(table_ref, context, traverseResult) {
1450
1455
  if (context.collectDynamicQueryInfo && traverseResult.dynamicQueryInfo.from.length == 0) {
1451
1456
  collectDynamicQueryInfoTableRef(table_ref, null, null, fromColumnsResult, [], traverseResult);
1452
1457
  }
1458
+ }
1459
+ else if (table_ref.LATERAL_P()) {
1460
+ const newContext = Object.assign(Object.assign({}, context), { collectDynamicQueryInfo: false });
1461
+ const lateralJoinResult = traverse_select_with_parens_or_func_table(table_ref, newContext, traverseResult);
1462
+ allColumns.push(...lateralJoinResult.columns);
1463
+ singleRow = false;
1464
+ }
1465
+ else if (table_ref.select_with_parens() || table_ref.func_table()) {
1466
+ const select_with_parens_or_func_result = traverse_select_with_parens_or_func_table(table_ref, context, traverseResult);
1467
+ allColumns.push(...select_with_parens_or_func_result.columns);
1468
+ singleRow = select_with_parens_or_func_result.singleRow;
1469
+ }
1470
+ else if (table_ref.OPEN_PAREN()) {
1471
+ const table_ref2 = table_ref.table_ref(0);
1472
+ const joinList = extractJoins(table_ref2);
1473
+ const joinColumns = traverse_joins(joinList, context, traverseResult);
1474
+ const aliasClause = table_ref.alias_clause();
1475
+ if (aliasClause) {
1476
+ const alias = aliasClause.colid().getText();
1477
+ joinColumns.forEach(col => col.table = alias.toLowerCase());
1478
+ }
1479
+ allColumns.push(...joinColumns);
1480
+ singleRow = false;
1481
+ }
1482
+ const table_ref_list = table_ref.table_ref_list();
1483
+ if (table_ref_list.length > 0) {
1453
1484
  const joinList = extractJoins(table_ref);
1454
- joinList.forEach((join, index) => {
1455
- const joinType = join.joinType; //INNER, LEFT
1456
- const joinQual = join.joinQual;
1457
- const numParamsBefore = traverseResult.parameters.length;
1458
- const subsequentJoints = joinList.slice(index + 1);
1459
- let joinColumns = [];
1460
- if (join.tableRef.LATERAL_P()) {
1461
- const newContext = Object.assign(Object.assign({}, context), { parentColumns: allColumns, collectDynamicQueryInfo: false });
1462
- const lateralJoinResult = traverse_select_with_parens_or_func_table(join.tableRef, newContext, traverseResult);
1463
- joinColumns = lateralJoinResult.columns;
1464
- }
1465
- else {
1466
- const joinTableRefResult = traverse_table_ref(join.tableRef, context, traverseResult);
1467
- joinColumns = (joinQual === null || joinQual === void 0 ? void 0 : joinQual.USING()) ? filterUsingColumns(joinTableRefResult.columns, joinQual) : joinTableRefResult.columns;
1468
- }
1469
- const nullableColumns = (joinType === null || joinType === void 0 ? void 0 : joinType.LEFT())
1470
- ? joinColumns.map(col => (Object.assign(Object.assign({}, col), { original_is_nullable: col.is_nullable, is_nullable: checkLeftJoinIsNullable(join, subsequentJoints, joinColumns) ? true : col.is_nullable })))
1471
- : joinColumns;
1472
- allColumns.push(...nullableColumns);
1473
- if (context.collectNestedInfo && joinQual) {
1474
- collectNestedInfo(joinQual, joinColumns, traverseResult);
1475
- }
1476
- if (context.collectDynamicQueryInfo) {
1477
- const parameters = traverseResult.parameters.slice(numParamsBefore).map((_, index) => index + numParamsBefore);
1478
- collectDynamicQueryInfoTableRef(join.tableRef, joinType, joinQual, joinColumns, parameters, traverseResult);
1479
- }
1480
- });
1481
- return {
1482
- columns: allColumns,
1483
- singleRow: false
1484
- };
1485
+ const joinColumns = traverse_joins(joinList, Object.assign(Object.assign({}, context), { parentColumns: allColumns }), traverseResult);
1486
+ allColumns.push(...joinColumns);
1487
+ singleRow = false;
1485
1488
  }
1486
- const select_with_parens_or_func_result = traverse_select_with_parens_or_func_table(table_ref, context, traverseResult);
1487
- return select_with_parens_or_func_result;
1489
+ return {
1490
+ columns: allColumns,
1491
+ singleRow
1492
+ };
1493
+ }
1494
+ function traverse_joins(joinList, context, traverseResult) {
1495
+ const allColumns = [];
1496
+ joinList.forEach((join, index) => {
1497
+ const joinType = join.joinType; //INNER, LEFT
1498
+ const joinQual = join.joinQual;
1499
+ const numParamsBefore = traverseResult.parameters.length;
1500
+ const subsequentJoints = joinList.slice(index + 1);
1501
+ const joinTableRefResult = traverse_table_ref(join.tableRef, Object.assign(Object.assign({}, context), { fromColumns: context.fromColumns.concat(allColumns) }), traverseResult);
1502
+ const joinColumns = (joinQual === null || joinQual === void 0 ? void 0 : joinQual.USING()) ? filterUsingColumns(joinTableRefResult.columns, joinQual) : joinTableRefResult.columns;
1503
+ const nullableColumns = (joinType === null || joinType === void 0 ? void 0 : joinType.LEFT())
1504
+ ? joinColumns.map(col => (Object.assign(Object.assign({}, col), { original_is_nullable: col.is_nullable, is_nullable: checkLeftJoinIsNullable(join, subsequentJoints, joinColumns) ? true : col.is_nullable })))
1505
+ : joinColumns;
1506
+ allColumns.push(...nullableColumns);
1507
+ if (context.collectNestedInfo && joinQual) {
1508
+ collectNestedInfo(joinQual, joinColumns, traverseResult);
1509
+ }
1510
+ if (context.collectDynamicQueryInfo) {
1511
+ const parameters = traverseResult.parameters.slice(numParamsBefore).map((_, index) => index + numParamsBefore);
1512
+ collectDynamicQueryInfoTableRef(join.tableRef, joinType, joinQual, joinColumns, parameters, traverseResult);
1513
+ }
1514
+ });
1515
+ return allColumns;
1488
1516
  }
1489
1517
  function traverse_select_with_parens_or_func_table(tableRef, context, traverseResult) {
1490
1518
  var _a, _b, _c, _d, _e, _f, _g, _h;
@@ -1492,7 +1520,7 @@ function traverse_select_with_parens_or_func_table(tableRef, context, traverseRe
1492
1520
  if (func_table) {
1493
1521
  const funcAlias = ((_c = (_b = (_a = tableRef.func_alias_clause()) === null || _a === void 0 ? void 0 : _a.alias_clause()) === null || _b === void 0 ? void 0 : _b.colid()) === null || _c === void 0 ? void 0 : _c.getText()) || '';
1494
1522
  const result = traverse_func_table(func_table, context, traverseResult);
1495
- const resultWithAlias = result.columns.map(col => (Object.assign(Object.assign({}, col), { table_name: funcAlias || col.table_name })));
1523
+ const resultWithAlias = result.columns.map(col => (Object.assign(Object.assign({}, col), { table: funcAlias || col.table })));
1496
1524
  return {
1497
1525
  columns: resultWithAlias,
1498
1526
  singleRow: result.singleRow
@@ -1503,7 +1531,7 @@ function traverse_select_with_parens_or_func_table(tableRef, context, traverseRe
1503
1531
  const columns = traverse_select_with_parens(select_with_parens, Object.assign(Object.assign({}, context), { collectDynamicQueryInfo: false }), traverseResult);
1504
1532
  const aliasNameList = (_f = (_e = (_d = tableRef.alias_clause()) === null || _d === void 0 ? void 0 : _d.name_list()) === null || _e === void 0 ? void 0 : _e.name_list()) === null || _f === void 0 ? void 0 : _f.map(name => name.getText());
1505
1533
  const alias = (_h = (_g = tableRef.alias_clause()) === null || _g === void 0 ? void 0 : _g.colid().getText()) !== null && _h !== void 0 ? _h : '';
1506
- const withAlias = columns.columns.map((col, i) => (Object.assign(Object.assign({}, col), { column_name: (aliasNameList === null || aliasNameList === void 0 ? void 0 : aliasNameList[i]) || col.column_name, table_name: alias || col.table_name })));
1534
+ const withAlias = columns.columns.map((col, i) => (Object.assign(Object.assign({}, col), { column_name: (aliasNameList === null || aliasNameList === void 0 ? void 0 : aliasNameList[i]) || col.column_name, table: alias || col.table })));
1507
1535
  return {
1508
1536
  columns: withAlias,
1509
1537
  singleRow: false
@@ -1634,38 +1662,79 @@ function parseReturnType(returnType) {
1634
1662
  if (setofMatch) {
1635
1663
  return { kind: 'setof', table: setofMatch[1] };
1636
1664
  }
1665
+ else {
1666
+ return { kind: 'table_name', table: trimmed };
1667
+ }
1637
1668
  throw new Error(`Unsupported return type format: ${returnType}`);
1638
1669
  }
1670
+ function handleFunctionReturn(dbSchema, returnType) {
1671
+ switch (returnType.kind) {
1672
+ case 'table':
1673
+ const columns = handleTableReturnType(returnType);
1674
+ return {
1675
+ columns,
1676
+ singleRow: false
1677
+ };
1678
+ case 'setof':
1679
+ case 'table_name':
1680
+ const tableName = (0, select_columns_1.splitName)(returnType.table);
1681
+ const setOfColumns = dbSchema.filter(col => col.table.toLowerCase() === tableName.name && tableName.prefix === '' || col.schema === tableName.prefix);
1682
+ return {
1683
+ columns: setOfColumns,
1684
+ singleRow: false
1685
+ };
1686
+ }
1687
+ }
1688
+ function handleTableReturnType(returnType) {
1689
+ const columns = returnType.columns.map(col => {
1690
+ const columnInfo = {
1691
+ column_name: col.name,
1692
+ type: col.type,
1693
+ is_nullable: true,
1694
+ table: '',
1695
+ schema: ''
1696
+ };
1697
+ return columnInfo;
1698
+ });
1699
+ return columns;
1700
+ }
1701
+ function parseArgumentList(argString) {
1702
+ return argString
1703
+ .split(',')
1704
+ .map(arg => arg.trim())
1705
+ .filter(arg => arg.length > 0)
1706
+ .map(arg => {
1707
+ const [name, ...typeParts] = arg.split(/\s+/);
1708
+ return {
1709
+ name,
1710
+ type: typeParts.join(' ') // Handles multi-word types like "character varying"
1711
+ };
1712
+ });
1713
+ }
1639
1714
  function traverse_func_expr_windowless(func_expr_windowless, context, traverseResult) {
1640
1715
  const func_application = func_expr_windowless.func_application();
1641
1716
  if (func_application) {
1642
- const func_name = func_application.func_name().getText().toLowerCase();
1643
- const funcSchema = context.userFunctions.find(func => func.function_name.toLowerCase() === func_name);
1717
+ const func_name = (0, select_columns_1.splitName)(func_application.func_name().getText().toLowerCase());
1718
+ const funcSchema = context.userFunctions.find(func => func.function_name.toLowerCase() === func_name.name && (func_name.prefix === '' || func_name.prefix === func.schema));
1644
1719
  if (funcSchema) {
1645
1720
  const definition = funcSchema.definition;
1646
1721
  const returnType = parseReturnType(funcSchema.return_type);
1647
- const functionColumns = returnType.kind === 'table' ? returnType.columns.map(col => {
1648
- const columnInfo = {
1649
- column_name: col.name,
1650
- type: col.type,
1651
- is_nullable: true,
1652
- table_name: '',
1653
- table_schema: ''
1654
- };
1655
- return columnInfo;
1656
- }) : context.dbSchema.filter(col => col.table_name.toLowerCase() === returnType.table);
1722
+ const argList = parseArgumentList(funcSchema.arguments);
1723
+ const functionResult = handleFunctionReturn(context.dbSchema, returnType);
1657
1724
  if (funcSchema.language.toLowerCase() === 'sql') {
1658
1725
  const parser = (0, postgres_1.parseSql)(definition);
1659
1726
  const selectstmt = parser.stmt().selectstmt();
1660
- const { columns, multipleRowsResult } = traverseSelectstmt(selectstmt, context, traverseResult);
1727
+ const params = argList.map(arg => ({ column_name: arg.name, type: arg.type, is_nullable: false, schema: funcSchema.schema, table: '$' }));
1728
+ const newFromColumns = params.concat(context.fromColumns);
1729
+ const { columns, multipleRowsResult } = traverseSelectstmt(selectstmt, Object.assign(Object.assign({}, context), { fromColumns: newFromColumns }), traverseResult);
1661
1730
  return {
1662
- columns: columns.map((c) => (Object.assign(Object.assign({}, c), { table_name: funcSchema.function_name }))),
1731
+ columns: columns.map((c) => (Object.assign(Object.assign({}, c), { table: funcSchema.function_name }))),
1663
1732
  singleRow: !multipleRowsResult
1664
1733
  };
1665
1734
  }
1666
1735
  else {
1667
1736
  return {
1668
- columns: functionColumns,
1737
+ columns: functionResult.columns.map((c) => (Object.assign(Object.assign({}, c), { table: funcSchema.function_name, schema: funcSchema.schema }))),
1669
1738
  singleRow: false
1670
1739
  };
1671
1740
  }
@@ -1762,8 +1831,8 @@ function traverseInsertstmt(insertstmt, dbSchema) {
1762
1831
  parameters: []
1763
1832
  };
1764
1833
  const insert_target = insertstmt.insert_target();
1765
- const tableName = insert_target.getText();
1766
- const insertColumns = dbSchema.filter(col => col.table_name.toLowerCase() === tableName.toLowerCase());
1834
+ const tableName = (0, select_columns_1.splitName)(insert_target.getText());
1835
+ const insertColumns = filterSchemaColumns(dbSchema, tableName);
1767
1836
  const insert_rest = insertstmt.insert_rest();
1768
1837
  const insertColumnsList = insert_rest.insert_column_list()
1769
1838
  .insert_column_item_list()
@@ -1803,8 +1872,8 @@ function traverseInsertstmt(insertstmt, dbSchema) {
1803
1872
  function traverseDeletestmt(deleteStmt, dbSchema, traverseResult) {
1804
1873
  var _a;
1805
1874
  const relation_expr = deleteStmt.relation_expr_opt_alias().relation_expr();
1806
- const tableName = relation_expr.getText();
1807
- const deleteColumns = dbSchema.filter(col => col.table_name.toLowerCase() === tableName.toLowerCase());
1875
+ const tableName = (0, select_columns_1.splitName)(relation_expr.getText());
1876
+ const deleteColumns = filterSchemaColumns(dbSchema, tableName);
1808
1877
  const paramIsListResult = getInParameterList(deleteStmt);
1809
1878
  const returning_clause = deleteStmt.returning_clause();
1810
1879
  const context = {
@@ -1837,13 +1906,17 @@ function traverseDeletestmt(deleteStmt, dbSchema, traverseResult) {
1837
1906
  function addConstraintIfNotNull(checkConstraint) {
1838
1907
  return checkConstraint !== undefined ? { checkConstraint } : undefined;
1839
1908
  }
1909
+ function filterSchemaColumns(dbSchema, tableName) {
1910
+ return dbSchema.filter(col => col.table.toLowerCase() === tableName.name.toLowerCase()
1911
+ && (tableName.prefix === '' || col.schema.toLowerCase() === tableName.prefix.toLowerCase()));
1912
+ }
1840
1913
  function traverseUpdatestmt(updatestmt, traverseContext, traverseResult) {
1841
1914
  var _a;
1842
1915
  const relation_expr_opt_alias = updatestmt.relation_expr_opt_alias();
1843
- const tableName = relation_expr_opt_alias.relation_expr().getText();
1916
+ const tableName = (0, select_columns_1.splitName)(relation_expr_opt_alias.relation_expr().getText());
1844
1917
  const tableAlias = ((_a = relation_expr_opt_alias.colid()) === null || _a === void 0 ? void 0 : _a.getText()) || '';
1845
- const updateColumns = traverseContext.dbSchema.filter(col => col.table_name.toLowerCase() === tableName.toLowerCase())
1846
- .map(col => (Object.assign(Object.assign({}, col), { table_name: tableAlias || col.table_name })));
1918
+ const updateColumns = filterSchemaColumns(traverseContext.dbSchema, tableName)
1919
+ .map(col => (Object.assign(Object.assign({}, col), { table: tableAlias || col.table })));
1847
1920
  const context = Object.assign(Object.assign({}, traverseContext), { fromColumns: updateColumns, collectNestedInfo: false, collectDynamicQueryInfo: false });
1848
1921
  updatestmt.set_clause_list().set_clause_list()
1849
1922
  .forEach(set_clause => traverse_set_clause(set_clause, context, traverseResult));
@@ -1877,7 +1950,7 @@ function traverse_set_clause(set_clause, context, traverseResult) {
1877
1950
  const columnName = (0, select_columns_1.splitName)(set_target.getText());
1878
1951
  const column = findColumn(columnName, context.fromColumns);
1879
1952
  const a_expr = set_clause.a_expr();
1880
- const excludedColumns = context.fromColumns.map((col) => (Object.assign(Object.assign({}, col), { table_name: 'excluded' })));
1953
+ const excludedColumns = context.fromColumns.map((col) => (Object.assign(Object.assign({}, col), { table: 'excluded' })));
1881
1954
  const a_exprResult = traverse_a_expr(a_expr, Object.assign(Object.assign({}, context), { fromColumns: context.fromColumns.concat(excludedColumns) }), traverseResult);
1882
1955
  if (isParameter(a_exprResult.column_name)) {
1883
1956
  traverseResult.parameters.at(-1).isNotNull = !column.is_nullable;
@@ -2416,7 +2489,7 @@ function checkLimit(selectstmt) {
2416
2489
  function traverseCopystmt(copyStmt, dbSchema, traverseResult) {
2417
2490
  var _a, _b;
2418
2491
  const tableName = copyStmt.qualified_name().getText();
2419
- const copyColumns = dbSchema.filter(col => col.table_name.toLowerCase() === tableName.toLowerCase());
2492
+ const copyColumns = dbSchema.filter(col => col.table.toLowerCase() === tableName.toLowerCase());
2420
2493
  const columnlist = (_b = (_a = copyStmt.column_list_()) === null || _a === void 0 ? void 0 : _a.columnlist()) === null || _b === void 0 ? void 0 : _b.columnElem_list().map(columnElem => traverse_columnElem(columnElem, copyColumns, traverseResult));
2421
2494
  return {
2422
2495
  queryType: 'Copy',