@query-doctor/core 0.1.0 → 0.1.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.
package/dist/index.cjs CHANGED
@@ -44,6 +44,7 @@ __export(index_exports, {
44
44
  PgIdentifier: () => PgIdentifier,
45
45
  PostgresQueryBuilder: () => PostgresQueryBuilder,
46
46
  PostgresVersion: () => PostgresVersion,
47
+ PssRewriter: () => PssRewriter,
47
48
  SKIP: () => SKIP,
48
49
  Statistics: () => Statistics,
49
50
  StatisticsMode: () => StatisticsMode,
@@ -318,11 +319,15 @@ var Walker = class _Walker {
318
319
  }
319
320
  }
320
321
  if (is2(node, "RangeVar") && node.RangeVar.relname) {
321
- this.tableMappings.set(node.RangeVar.relname, {
322
+ const columnReference = {
322
323
  text: node.RangeVar.relname,
323
324
  start: node.RangeVar.location,
324
325
  quoted: false
325
- });
326
+ };
327
+ if (node.RangeVar.schemaname) {
328
+ columnReference.schema = node.RangeVar.schemaname;
329
+ }
330
+ this.tableMappings.set(node.RangeVar.relname, columnReference);
326
331
  if (node.RangeVar.alias?.aliasname) {
327
332
  const aliasName = node.RangeVar.alias.aliasname;
328
333
  const existingMapping = this.tableMappings.get(aliasName);
@@ -335,9 +340,12 @@ var Walker = class _Walker {
335
340
  quoted: true,
336
341
  alias: aliasName
337
342
  };
343
+ if (node.RangeVar.schemaname) {
344
+ part.schema = node.RangeVar.schemaname;
345
+ }
338
346
  if (existingMapping) {
339
347
  console.warn(
340
- `Ignoring alias ${aliasName} as it shadows an existing mapping. We currently do not support alias shadowing.`
348
+ `Ignoring alias ${aliasName} as it shadows an existing mapping for ${existingMapping.text}. We currently do not support alias shadowing.`
341
349
  );
342
350
  this.shadowedAliases.push(part);
343
351
  return;
@@ -573,7 +581,10 @@ var Analyzer = class {
573
581
  const referencedTables = [];
574
582
  for (const value of tableMappings.values()) {
575
583
  if (!value.alias) {
576
- referencedTables.push(value.text);
584
+ referencedTables.push({
585
+ schema: value.schema,
586
+ table: value.text
587
+ });
577
588
  }
578
589
  }
579
590
  const { tags, queryWithoutTags } = this.extractSqlcommenter(query);
@@ -629,13 +640,14 @@ var Analyzer = class {
629
640
  }
630
641
  } else if (tableReference) {
631
642
  const [table, column] = colReference.parts;
643
+ const referencedSchema = table.schema;
632
644
  const referencedTable = this.normalize(table);
633
645
  const referencedColumn = this.normalize(column);
634
646
  const matchingTable = tables.find((table2) => {
635
647
  const hasMatchingColumn = table2.columns?.some((column2) => {
636
648
  return column2.columnName === referencedColumn;
637
649
  }) ?? false;
638
- return table2.tableName === referencedTable && hasMatchingColumn;
650
+ return table2.schemaName === referencedSchema && table2.tableName === referencedTable && hasMatchingColumn;
639
651
  });
640
652
  if (matchingTable) {
641
653
  const index = {
@@ -2012,6 +2024,28 @@ __publicField(_Statistics, "defaultStatsMode", Object.freeze({
2012
2024
  relpages: DEFAULT_RELPAGES
2013
2025
  }));
2014
2026
  var Statistics = _Statistics;
2027
+
2028
+ // src/optimizer/pss-rewriter.ts
2029
+ var PssRewriter = class {
2030
+ constructor() {
2031
+ __publicField(this, "problematicKeywords", ["interval", "timestamp", "geometry"]);
2032
+ }
2033
+ rewrite(query) {
2034
+ return this.rewriteKeywordWithParameter(query);
2035
+ }
2036
+ rewriteKeywordWithParameter(query) {
2037
+ return query.replace(/\b(\w+) (\$\d+)\b/gi, (match) => {
2038
+ const [keyword, parameter] = match.split(" ");
2039
+ const isProblematicKeyword = this.problematicKeywords.includes(
2040
+ keyword.toLowerCase()
2041
+ );
2042
+ if (!isProblematicKeyword) {
2043
+ return match;
2044
+ }
2045
+ return `(${parameter}::${keyword.toLowerCase()})`;
2046
+ });
2047
+ }
2048
+ };
2015
2049
  // Annotate the CommonJS export names for ESM import in node:
2016
2050
  0 && (module.exports = {
2017
2051
  Analyzer,
@@ -2025,6 +2059,7 @@ var Statistics = _Statistics;
2025
2059
  PgIdentifier,
2026
2060
  PostgresQueryBuilder,
2027
2061
  PostgresVersion,
2062
+ PssRewriter,
2028
2063
  SKIP,
2029
2064
  Statistics,
2030
2065
  StatisticsMode,