typesql-cli 0.18.6 → 0.19.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -233,7 +233,7 @@ function traverse_simple_select_pramary(simple_select_pramary, context, traverse
233
233
  }
234
234
  const where_a_expr = (_a = simple_select_pramary.where_clause()) === null || _a === void 0 ? void 0 : _a.a_expr();
235
235
  //fromColumns has precedence
236
- //context.fromColumns only becase of insert. update
236
+ //context.fromColumns because of exists(... where), join lateral(... where).
237
237
  const newContext = Object.assign(Object.assign({}, context), { fromColumns: [...context.fromColumns, ...fromResult.columns, ...context.parentColumns] });
238
238
  if (where_a_expr) {
239
239
  const numParamsBefore = traverseResult.parameters.length;
@@ -262,7 +262,7 @@ function traverse_simple_select_pramary(simple_select_pramary, context, traverse
262
262
  if (having_expr) {
263
263
  traverse_a_expr(having_expr, newContext, traverseResult);
264
264
  }
265
- const filteredColumns = filterColumns_simple_select_pramary(simple_select_pramary, Object.assign(Object.assign({}, context), { fromColumns: context.fromColumns.concat(fromResult.columns) }), traverseResult);
265
+ const filteredColumns = filterColumns_simple_select_pramary(simple_select_pramary, Object.assign(Object.assign({}, context), { fromColumns: fromResult.columns, parentColumns: context.fromColumns }), traverseResult);
266
266
  return {
267
267
  columns: filteredColumns,
268
268
  singleRow: fromResult.singleRow
@@ -273,7 +273,7 @@ function extractRelations(a_expr) {
273
273
  const relations = columnsRef
274
274
  .map((colRefExpr) => {
275
275
  const colRef = colRefExpr;
276
- const tableName = (0, select_columns_1.splitName)(colRef.getText());
276
+ const tableName = getFieldName(colRef);
277
277
  return tableName;
278
278
  });
279
279
  const uniqueRelations = [...new Set(relations.map(relation => relation.prefix))];
@@ -338,7 +338,7 @@ function traverse_target_list(target_list, context, traverseResult) {
338
338
  }
339
339
  const column = traverse_target_el(target_el, context, traverseResult);
340
340
  if (isParameter(column.column_name)) {
341
- traverseResult.parameters.at(-1).isNotNull = !context.fromColumns[index].is_nullable;
341
+ traverseResult.parameters.at(-1).isNotNull = !context.parentColumns[index].is_nullable;
342
342
  }
343
343
  return [column];
344
344
  });
@@ -351,7 +351,7 @@ function traverse_target_el(target_el, context, traverseResult) {
351
351
  const numParamsBefore = traverseResult.parameters.length;
352
352
  const exprResult = traverse_a_expr(a_expr, context, traverseResult);
353
353
  const colLabel = target_el.colLabel();
354
- const alias = colLabel != null ? colLabel.getText() : '';
354
+ const alias = colLabel != null ? get_colid_text(colLabel) : '';
355
355
  if (alias) {
356
356
  (_a = traverseResult.relations) === null || _a === void 0 ? void 0 : _a.forEach(relation => {
357
357
  if ((relation.name === exprResult.table_name || relation.alias === exprResult.table_name)
@@ -688,10 +688,16 @@ function traverse_expr_typecast(a_expr_typecast, context, traverseResult) {
688
688
  throw Error('traverse_expr_typecast - Not expected:' + a_expr_typecast.getText());
689
689
  }
690
690
  function traverseColumnRef(columnref, fromColumns) {
691
- const fieldName = (0, select_columns_1.splitName)(columnref.getText());
691
+ const fieldName = getFieldName(columnref);
692
692
  const col = findColumn(fieldName, fromColumns);
693
693
  return Object.assign(Object.assign({}, col), { is_nullable: col.is_nullable });
694
694
  }
695
+ function getFieldName(columnref) {
696
+ const colid = get_colid_text(columnref.colid());
697
+ const indirection = columnref.indirection();
698
+ let fieldName = indirection ? { name: get_indiretion_text(indirection), prefix: colid } : { name: colid, prefix: '' };
699
+ return fieldName;
700
+ }
695
701
  function getNameAndTypeIdFromAExprConst(a_expr_const) {
696
702
  var _a, _b;
697
703
  if (a_expr_const.iconst()) {
@@ -918,11 +924,7 @@ function filterColumns(fromColumns, fieldName) {
918
924
  });
919
925
  }
920
926
  function excludeColumns(fromColumns, excludeList) {
921
- return fromColumns.filter(col => {
922
- const found = excludeList.find(excluded => (excluded.prefix === '' || col.table_name === excluded.prefix)
923
- && excluded.name == col.column_name);
924
- return !found;
925
- });
927
+ return fromColumns.filter(col => !excludeList.find(excluded => excluded == col.column_name));
926
928
  }
927
929
  function traversec_expr_case(c_expr_case, context, traverseResult) {
928
930
  var _a;
@@ -1425,12 +1427,14 @@ function traverse_table_ref(table_ref, context, traverseResult) {
1425
1427
  const relation_expr = table_ref.relation_expr();
1426
1428
  const aliasClause = table_ref.alias_clause();
1427
1429
  const alias = aliasClause ? aliasClause.colid().getText() : '';
1430
+ let singleRow = false;
1428
1431
  if (relation_expr) {
1429
1432
  const tableName = traverse_relation_expr(relation_expr);
1430
1433
  const fromColumns = getFromColumns(tableName, context.fromColumns.concat(context.withColumns), context.dbSchema);
1431
1434
  const tableNameWithAlias = alias ? alias : tableName.name;
1432
1435
  const fromColumnsResult = fromColumns.map(col => (Object.assign(Object.assign({}, col), { table_name: tableNameWithAlias.toLowerCase() })));
1433
1436
  allColumns.push(...fromColumnsResult);
1437
+ singleRow = false;
1434
1438
  if (context.collectNestedInfo) {
1435
1439
  const key = fromColumnsResult.filter(col => col.column_key === 'PRI');
1436
1440
  const renameAs = (aliasClause === null || aliasClause === void 0 ? void 0 : aliasClause.AS()) != null;
@@ -1448,41 +1452,64 @@ function traverse_table_ref(table_ref, context, traverseResult) {
1448
1452
  if (context.collectDynamicQueryInfo && traverseResult.dynamicQueryInfo.from.length == 0) {
1449
1453
  collectDynamicQueryInfoTableRef(table_ref, null, null, fromColumnsResult, [], traverseResult);
1450
1454
  }
1455
+ }
1456
+ else if (table_ref.LATERAL_P()) {
1457
+ const newContext = Object.assign(Object.assign({}, context), { collectDynamicQueryInfo: false });
1458
+ const lateralJoinResult = traverse_select_with_parens_or_func_table(table_ref, newContext, traverseResult);
1459
+ allColumns.push(...lateralJoinResult.columns);
1460
+ singleRow = false;
1461
+ }
1462
+ else if (table_ref.select_with_parens() || table_ref.func_table()) {
1463
+ const select_with_parens_or_func_result = traverse_select_with_parens_or_func_table(table_ref, context, traverseResult);
1464
+ allColumns.push(...select_with_parens_or_func_result.columns);
1465
+ singleRow = select_with_parens_or_func_result.singleRow;
1466
+ }
1467
+ else if (table_ref.OPEN_PAREN()) {
1468
+ const table_ref2 = table_ref.table_ref(0);
1469
+ const joinList = extractJoins(table_ref2);
1470
+ const joinColumns = traverse_joins(joinList, context, traverseResult);
1471
+ const aliasClause = table_ref.alias_clause();
1472
+ if (aliasClause) {
1473
+ const alias = aliasClause.colid().getText();
1474
+ joinColumns.forEach(col => col.table_name = alias.toLowerCase());
1475
+ }
1476
+ allColumns.push(...joinColumns);
1477
+ singleRow = false;
1478
+ }
1479
+ const table_ref_list = table_ref.table_ref_list();
1480
+ if (table_ref_list.length > 0) {
1451
1481
  const joinList = extractJoins(table_ref);
1452
- joinList.forEach((join, index) => {
1453
- const joinType = join.joinType; //INNER, LEFT
1454
- const joinQual = join.joinQual;
1455
- const numParamsBefore = traverseResult.parameters.length;
1456
- const subsequentJoints = joinList.slice(index + 1);
1457
- let joinColumns = [];
1458
- if (join.tableRef.LATERAL_P()) {
1459
- const newContext = Object.assign(Object.assign({}, context), { parentColumns: allColumns, collectDynamicQueryInfo: false });
1460
- const lateralJoinResult = traverse_select_with_parens_or_func_table(join.tableRef, newContext, traverseResult);
1461
- joinColumns = lateralJoinResult.columns;
1462
- }
1463
- else {
1464
- const joinTableRefResult = traverse_table_ref(join.tableRef, context, traverseResult);
1465
- joinColumns = (joinQual === null || joinQual === void 0 ? void 0 : joinQual.USING()) ? filterUsingColumns(joinTableRefResult.columns, joinQual) : joinTableRefResult.columns;
1466
- }
1467
- const nullableColumns = (joinType === null || joinType === void 0 ? void 0 : joinType.LEFT())
1468
- ? joinColumns.map(col => (Object.assign(Object.assign({}, col), { original_is_nullable: col.is_nullable, is_nullable: checkLeftJoinIsNullable(join, subsequentJoints, joinColumns) ? true : col.is_nullable })))
1469
- : joinColumns;
1470
- allColumns.push(...nullableColumns);
1471
- if (context.collectNestedInfo && joinQual) {
1472
- collectNestedInfo(joinQual, joinColumns, traverseResult);
1473
- }
1474
- if (context.collectDynamicQueryInfo) {
1475
- const parameters = traverseResult.parameters.slice(numParamsBefore).map((_, index) => index + numParamsBefore);
1476
- collectDynamicQueryInfoTableRef(join.tableRef, joinType, joinQual, joinColumns, parameters, traverseResult);
1477
- }
1478
- });
1479
- return {
1480
- columns: allColumns,
1481
- singleRow: false
1482
- };
1482
+ const joinColumns = traverse_joins(joinList, Object.assign(Object.assign({}, context), { parentColumns: allColumns }), traverseResult);
1483
+ allColumns.push(...joinColumns);
1484
+ singleRow = false;
1483
1485
  }
1484
- const select_with_parens_or_func_result = traverse_select_with_parens_or_func_table(table_ref, context, traverseResult);
1485
- return select_with_parens_or_func_result;
1486
+ return {
1487
+ columns: allColumns,
1488
+ singleRow
1489
+ };
1490
+ }
1491
+ function traverse_joins(joinList, context, traverseResult) {
1492
+ const allColumns = [];
1493
+ joinList.forEach((join, index) => {
1494
+ const joinType = join.joinType; //INNER, LEFT
1495
+ const joinQual = join.joinQual;
1496
+ const numParamsBefore = traverseResult.parameters.length;
1497
+ const subsequentJoints = joinList.slice(index + 1);
1498
+ const joinTableRefResult = traverse_table_ref(join.tableRef, Object.assign(Object.assign({}, context), { fromColumns: context.fromColumns.concat(allColumns) }), traverseResult);
1499
+ const joinColumns = (joinQual === null || joinQual === void 0 ? void 0 : joinQual.USING()) ? filterUsingColumns(joinTableRefResult.columns, joinQual) : joinTableRefResult.columns;
1500
+ const nullableColumns = (joinType === null || joinType === void 0 ? void 0 : joinType.LEFT())
1501
+ ? joinColumns.map(col => (Object.assign(Object.assign({}, col), { original_is_nullable: col.is_nullable, is_nullable: checkLeftJoinIsNullable(join, subsequentJoints, joinColumns) ? true : col.is_nullable })))
1502
+ : joinColumns;
1503
+ allColumns.push(...nullableColumns);
1504
+ if (context.collectNestedInfo && joinQual) {
1505
+ collectNestedInfo(joinQual, joinColumns, traverseResult);
1506
+ }
1507
+ if (context.collectDynamicQueryInfo) {
1508
+ const parameters = traverseResult.parameters.slice(numParamsBefore).map((_, index) => index + numParamsBefore);
1509
+ collectDynamicQueryInfoTableRef(join.tableRef, joinType, joinQual, joinColumns, parameters, traverseResult);
1510
+ }
1511
+ });
1512
+ return allColumns;
1486
1513
  }
1487
1514
  function traverse_select_with_parens_or_func_table(tableRef, context, traverseResult) {
1488
1515
  var _a, _b, _c, _d, _e, _f, _g, _h;
@@ -1556,7 +1583,7 @@ function getJoinColumns(joinQual) {
1556
1583
  const a_expr_or = a_expr_or_list[0];
1557
1584
  const a_expr_and = a_expr_or.a_expr_and_list()[0];
1558
1585
  const columnref = collectContextsOfType(a_expr_and, PostgreSQLParser_1.ColumnrefContext);
1559
- const joinColumns = columnref.map(colRef => (0, select_columns_1.splitName)(colRef.getText()));
1586
+ const joinColumns = columnref.map(colRef => getFieldName(colRef));
1560
1587
  return joinColumns;
1561
1588
  }
1562
1589
  return [];
@@ -1584,7 +1611,7 @@ function collectNestedInfo(joinQual, resultColumns, traverseResult) {
1584
1611
  });
1585
1612
  }
1586
1613
  function filterUsingColumns(fromColumns, joinQual) {
1587
- const excludeList = joinQual.name_list().name_list().map(name => (0, select_columns_1.splitName)(name.getText()));
1614
+ const excludeList = joinQual.name_list().name_list().map(name => get_colid_text(name.colid()));
1588
1615
  const filteredColumns = excludeColumns(fromColumns, excludeList);
1589
1616
  return filteredColumns;
1590
1617
  }
@@ -1677,12 +1704,12 @@ function traverse_relation_expr(relation_expr) {
1677
1704
  return name;
1678
1705
  }
1679
1706
  function traverse_qualified_name(qualified_name) {
1680
- var _a, _b;
1681
1707
  const colid_name = qualified_name.colid() ? get_colid_text(qualified_name.colid()) : '';
1682
- const indirection_el_list = (_a = qualified_name.indirection()) === null || _a === void 0 ? void 0 : _a.indirection_el_list();
1683
- if (indirection_el_list && indirection_el_list.length === 1) {
1708
+ const indirection = qualified_name.indirection();
1709
+ if (indirection) {
1710
+ const indirection_text = get_indiretion_text(indirection);
1684
1711
  return {
1685
- name: ((_b = indirection_el_list[0].attr_name()) === null || _b === void 0 ? void 0 : _b.getText()) || '',
1712
+ name: indirection_text,
1686
1713
  alias: colid_name
1687
1714
  };
1688
1715
  }
@@ -1694,7 +1721,7 @@ function traverse_qualified_name(qualified_name) {
1694
1721
  function get_colid_text(colid) {
1695
1722
  const identifier = colid.identifier();
1696
1723
  if (identifier) {
1697
- return traverse_identifier(identifier);
1724
+ return get_identifier_text(identifier);
1698
1725
  }
1699
1726
  const unreserved_keyword = colid.unreserved_keyword();
1700
1727
  if (unreserved_keyword) {
@@ -1702,8 +1729,24 @@ function get_colid_text(colid) {
1702
1729
  }
1703
1730
  return '';
1704
1731
  }
1705
- function traverse_identifier(identifier) {
1706
- const tableName = identifier.Identifier().getText();
1732
+ function get_indiretion_text(indirection) {
1733
+ var _a;
1734
+ const indirection_el_list = indirection.indirection_el_list();
1735
+ if (indirection_el_list && indirection_el_list.length === 1) {
1736
+ const colLabel = (_a = indirection_el_list[0].attr_name()) === null || _a === void 0 ? void 0 : _a.colLabel();
1737
+ if (colLabel) {
1738
+ return get_colid_text(colLabel);
1739
+ }
1740
+ }
1741
+ return '';
1742
+ }
1743
+ function get_identifier_text(identifier) {
1744
+ const quoted_identifier = identifier.QuotedIdentifier();
1745
+ if (quoted_identifier) {
1746
+ const tableName = quoted_identifier.getText().slice(1, -1);
1747
+ return tableName;
1748
+ }
1749
+ const tableName = identifier.getText();
1707
1750
  return tableName;
1708
1751
  }
1709
1752
  function traverse_unreserved_keyword(unreserved_keyword) {
@@ -2080,7 +2123,7 @@ function isNotNull_c_expr(c_expr, field) {
2080
2123
  if (c_expr instanceof PostgreSQLParser_1.C_expr_exprContext) {
2081
2124
  const columnref = c_expr.columnref();
2082
2125
  if (columnref) {
2083
- const fieldName = (0, select_columns_1.splitName)(columnref.getText());
2126
+ const fieldName = getFieldName(columnref);
2084
2127
  return (fieldName.name === field.name && (fieldName.prefix === '' || field.prefix === fieldName.prefix));
2085
2128
  }
2086
2129
  const aexprconst = c_expr.aexprconst();
@@ -2378,8 +2421,7 @@ function getCheckedUniqueColumn(a_expr_like) {
2378
2421
  if (c_expr instanceof PostgreSQLParser_1.C_expr_exprContext) {
2379
2422
  const columnref = c_expr.columnref();
2380
2423
  if (columnref) {
2381
- const fieldName = (0, select_columns_1.splitName)(columnref.getText());
2382
- // const col = traverseColumnRef(columnref, dbSchema);
2424
+ const fieldName = getFieldName(columnref);
2383
2425
  return fieldName.name;
2384
2426
  }
2385
2427
  }