@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.d.ts CHANGED
@@ -6,4 +6,5 @@ export * from "./sql/builder.js";
6
6
  export * from "./sql/pg-identifier.js";
7
7
  export * from "./optimizer/genalgo.js";
8
8
  export * from "./optimizer/statistics.js";
9
+ export * from "./optimizer/pss-rewriter.js";
9
10
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,wBAAwB,CAAC;AACvC,cAAc,wBAAwB,CAAC;AACvC,cAAc,2BAA2B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,wBAAwB,CAAC;AACvC,cAAc,wBAAwB,CAAC;AACvC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,6BAA6B,CAAC"}
package/dist/index.js CHANGED
@@ -269,11 +269,15 @@ var Walker = class _Walker {
269
269
  }
270
270
  }
271
271
  if (is2(node, "RangeVar") && node.RangeVar.relname) {
272
- this.tableMappings.set(node.RangeVar.relname, {
272
+ const columnReference = {
273
273
  text: node.RangeVar.relname,
274
274
  start: node.RangeVar.location,
275
275
  quoted: false
276
- });
276
+ };
277
+ if (node.RangeVar.schemaname) {
278
+ columnReference.schema = node.RangeVar.schemaname;
279
+ }
280
+ this.tableMappings.set(node.RangeVar.relname, columnReference);
277
281
  if (node.RangeVar.alias?.aliasname) {
278
282
  const aliasName = node.RangeVar.alias.aliasname;
279
283
  const existingMapping = this.tableMappings.get(aliasName);
@@ -286,9 +290,12 @@ var Walker = class _Walker {
286
290
  quoted: true,
287
291
  alias: aliasName
288
292
  };
293
+ if (node.RangeVar.schemaname) {
294
+ part.schema = node.RangeVar.schemaname;
295
+ }
289
296
  if (existingMapping) {
290
297
  console.warn(
291
- `Ignoring alias ${aliasName} as it shadows an existing mapping. We currently do not support alias shadowing.`
298
+ `Ignoring alias ${aliasName} as it shadows an existing mapping for ${existingMapping.text}. We currently do not support alias shadowing.`
292
299
  );
293
300
  this.shadowedAliases.push(part);
294
301
  return;
@@ -524,7 +531,10 @@ var Analyzer = class {
524
531
  const referencedTables = [];
525
532
  for (const value of tableMappings.values()) {
526
533
  if (!value.alias) {
527
- referencedTables.push(value.text);
534
+ referencedTables.push({
535
+ schema: value.schema,
536
+ table: value.text
537
+ });
528
538
  }
529
539
  }
530
540
  const { tags, queryWithoutTags } = this.extractSqlcommenter(query);
@@ -580,13 +590,14 @@ var Analyzer = class {
580
590
  }
581
591
  } else if (tableReference) {
582
592
  const [table, column] = colReference.parts;
593
+ const referencedSchema = table.schema;
583
594
  const referencedTable = this.normalize(table);
584
595
  const referencedColumn = this.normalize(column);
585
596
  const matchingTable = tables.find((table2) => {
586
597
  const hasMatchingColumn = table2.columns?.some((column2) => {
587
598
  return column2.columnName === referencedColumn;
588
599
  }) ?? false;
589
- return table2.tableName === referencedTable && hasMatchingColumn;
600
+ return table2.schemaName === referencedSchema && table2.tableName === referencedTable && hasMatchingColumn;
590
601
  });
591
602
  if (matchingTable) {
592
603
  const index = {
@@ -1963,6 +1974,28 @@ __publicField(_Statistics, "defaultStatsMode", Object.freeze({
1963
1974
  relpages: DEFAULT_RELPAGES
1964
1975
  }));
1965
1976
  var Statistics = _Statistics;
1977
+
1978
+ // src/optimizer/pss-rewriter.ts
1979
+ var PssRewriter = class {
1980
+ constructor() {
1981
+ __publicField(this, "problematicKeywords", ["interval", "timestamp", "geometry"]);
1982
+ }
1983
+ rewrite(query) {
1984
+ return this.rewriteKeywordWithParameter(query);
1985
+ }
1986
+ rewriteKeywordWithParameter(query) {
1987
+ return query.replace(/\b(\w+) (\$\d+)\b/gi, (match) => {
1988
+ const [keyword, parameter] = match.split(" ");
1989
+ const isProblematicKeyword = this.problematicKeywords.includes(
1990
+ keyword.toLowerCase()
1991
+ );
1992
+ if (!isProblematicKeyword) {
1993
+ return match;
1994
+ }
1995
+ return `(${parameter}::${keyword.toLowerCase()})`;
1996
+ });
1997
+ }
1998
+ };
1966
1999
  export {
1967
2000
  Analyzer,
1968
2001
  ExportedStats,
@@ -1975,6 +2008,7 @@ export {
1975
2008
  PgIdentifier,
1976
2009
  PostgresQueryBuilder,
1977
2010
  PostgresVersion,
2011
+ PssRewriter,
1978
2012
  SKIP,
1979
2013
  Statistics,
1980
2014
  StatisticsMode,