@query-doctor/core 0.2.5 → 0.3.0

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.js CHANGED
@@ -780,6 +780,9 @@ var PostgresQueryBuilder = class _PostgresQueryBuilder {
780
780
  __publicField(this, "isIntrospection", false);
781
781
  __publicField(this, "explainFlags", []);
782
782
  __publicField(this, "_preamble", 0);
783
+ __publicField(this, "parameters", {});
784
+ // substitution for `limit $1` -> `limit 5`
785
+ __publicField(this, "limitSubstitution");
783
786
  }
784
787
  get preamble() {
785
788
  return this._preamble;
@@ -813,6 +816,14 @@ var PostgresQueryBuilder = class _PostgresQueryBuilder {
813
816
  this.explainFlags = flags;
814
817
  return this;
815
818
  }
819
+ parameterize(parameters) {
820
+ Object.assign(this.parameters, parameters);
821
+ return this;
822
+ }
823
+ replaceLimit(limit) {
824
+ this.limitSubstitution = limit;
825
+ return this;
826
+ }
816
827
  build() {
817
828
  let commands = this.generateSetCommands();
818
829
  commands += this.generateExplain().query;
@@ -841,14 +852,28 @@ var PostgresQueryBuilder = class _PostgresQueryBuilder {
841
852
  return commands;
842
853
  }
843
854
  generateExplain() {
844
- let query = "";
855
+ let finalQuery = "";
845
856
  if (this.explainFlags.length > 0) {
846
- query += `explain (${this.explainFlags.join(", ")}) `;
857
+ finalQuery += `explain (${this.explainFlags.join(", ")}) `;
858
+ }
859
+ const query = this.substituteQuery();
860
+ const semicolon = query.endsWith(";") ? "" : ";";
861
+ const preamble = finalQuery.length;
862
+ finalQuery += `${query}${semicolon}`;
863
+ return { query: finalQuery, preamble };
864
+ }
865
+ substituteQuery() {
866
+ let query = this.query;
867
+ if (this.limitSubstitution !== void 0) {
868
+ query = query.replace(
869
+ /limit\s+\$\d+/g,
870
+ `limit ${this.limitSubstitution}`
871
+ );
872
+ }
873
+ for (const [key, value] of Object.entries(this.parameters)) {
874
+ query = query.replaceAll(`\\$${key}`, value.toString());
847
875
  }
848
- const semicolon = this.query.endsWith(";") ? "" : ";";
849
- const preamble = query.length;
850
- query += `${this.query}${semicolon}`;
851
- return { query, preamble };
876
+ return query;
852
877
  }
853
878
  };
854
879