@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.cjs CHANGED
@@ -829,6 +829,9 @@ var PostgresQueryBuilder = class _PostgresQueryBuilder {
829
829
  __publicField(this, "isIntrospection", false);
830
830
  __publicField(this, "explainFlags", []);
831
831
  __publicField(this, "_preamble", 0);
832
+ __publicField(this, "parameters", {});
833
+ // substitution for `limit $1` -> `limit 5`
834
+ __publicField(this, "limitSubstitution");
832
835
  }
833
836
  get preamble() {
834
837
  return this._preamble;
@@ -862,6 +865,14 @@ var PostgresQueryBuilder = class _PostgresQueryBuilder {
862
865
  this.explainFlags = flags;
863
866
  return this;
864
867
  }
868
+ parameterize(parameters) {
869
+ Object.assign(this.parameters, parameters);
870
+ return this;
871
+ }
872
+ replaceLimit(limit) {
873
+ this.limitSubstitution = limit;
874
+ return this;
875
+ }
865
876
  build() {
866
877
  let commands = this.generateSetCommands();
867
878
  commands += this.generateExplain().query;
@@ -890,14 +901,28 @@ var PostgresQueryBuilder = class _PostgresQueryBuilder {
890
901
  return commands;
891
902
  }
892
903
  generateExplain() {
893
- let query = "";
904
+ let finalQuery = "";
894
905
  if (this.explainFlags.length > 0) {
895
- query += `explain (${this.explainFlags.join(", ")}) `;
906
+ finalQuery += `explain (${this.explainFlags.join(", ")}) `;
907
+ }
908
+ const query = this.substituteQuery();
909
+ const semicolon = query.endsWith(";") ? "" : ";";
910
+ const preamble = finalQuery.length;
911
+ finalQuery += `${query}${semicolon}`;
912
+ return { query: finalQuery, preamble };
913
+ }
914
+ substituteQuery() {
915
+ let query = this.query;
916
+ if (this.limitSubstitution !== void 0) {
917
+ query = query.replace(
918
+ /limit\s+\$\d+/g,
919
+ `limit ${this.limitSubstitution}`
920
+ );
921
+ }
922
+ for (const [key, value] of Object.entries(this.parameters)) {
923
+ query = query.replaceAll(`\\$${key}`, value.toString());
896
924
  }
897
- const semicolon = this.query.endsWith(";") ? "" : ";";
898
- const preamble = query.length;
899
- query += `${this.query}${semicolon}`;
900
- return { query, preamble };
925
+ return query;
901
926
  }
902
927
  };
903
928