@query-doctor/core 0.1.3 → 0.1.4

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.
package/dist/index.cjs CHANGED
@@ -600,7 +600,7 @@ var Analyzer = class {
600
600
  nudges
601
601
  };
602
602
  }
603
- deriveIndexes(tables, discovered) {
603
+ deriveIndexes(tables, discovered, referencedTables) {
604
604
  const allIndexes = [];
605
605
  const seenIndexes = /* @__PURE__ */ new Set();
606
606
  function addIndex(index) {
@@ -611,6 +611,7 @@ var Analyzer = class {
611
611
  seenIndexes.add(key);
612
612
  allIndexes.push(index);
613
613
  }
614
+ const matchingTables = this.filterReferences(referencedTables, tables);
614
615
  for (const colReference of discovered) {
615
616
  const partsCount = colReference.parts.length;
616
617
  const columnOnlyReference = partsCount === 1;
@@ -619,12 +620,10 @@ var Analyzer = class {
619
620
  if (columnOnlyReference) {
620
621
  const [column] = colReference.parts;
621
622
  const referencedColumn = this.normalize(column);
622
- const matchingTables = tables.filter((table) => {
623
- return table.columns?.some((column2) => {
624
- return column2.columnName === referencedColumn;
625
- }) ?? false;
626
- });
627
623
  for (const table of matchingTables) {
624
+ if (!this.hasColumn(table, referencedColumn)) {
625
+ continue;
626
+ }
628
627
  const index = {
629
628
  schema: table.schemaName,
630
629
  table: table.tableName,
@@ -640,16 +639,12 @@ var Analyzer = class {
640
639
  }
641
640
  } else if (tableReference) {
642
641
  const [table, column] = colReference.parts;
643
- const referencedSchema = table.schema;
644
642
  const referencedTable = this.normalize(table);
645
643
  const referencedColumn = this.normalize(column);
646
- const matchingTable = tables.find((table2) => {
647
- const hasMatchingColumn = table2.columns?.some((column2) => {
648
- return column2.columnName === referencedColumn;
649
- }) ?? false;
650
- return table2.schemaName === referencedSchema && table2.tableName === referencedTable && hasMatchingColumn;
651
- });
652
- if (matchingTable) {
644
+ for (const matchingTable of matchingTables) {
645
+ if (!this.hasColumn(matchingTable, referencedColumn)) {
646
+ continue;
647
+ }
653
648
  const index = {
654
649
  schema: matchingTable.schemaName,
655
650
  table: referencedTable,
@@ -690,6 +685,23 @@ var Analyzer = class {
690
685
  }
691
686
  return allIndexes;
692
687
  }
688
+ filterReferences(referencedTables, tables) {
689
+ const matchingTables = [];
690
+ for (const referencedTable of referencedTables) {
691
+ const refs = tables.filter(({ tableName, schemaName }) => {
692
+ let schemaMatches = true;
693
+ if (referencedTable.schema) {
694
+ schemaMatches = schemaName === referencedTable.schema;
695
+ }
696
+ return schemaMatches && tableName === referencedTable.table;
697
+ });
698
+ matchingTables.push(...refs);
699
+ }
700
+ return matchingTables;
701
+ }
702
+ hasColumn(table, columnName) {
703
+ return table.columns?.some((column) => column.columnName === columnName) ?? false;
704
+ }
693
705
  colorizeKeywords(query, color) {
694
706
  return query.replace(
695
707
  // eh? This kinda sucks