@vulcan-sql/core
Version:
Core package of VulcanSQL
46 lines • 1.75 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Parameterizer = void 0;
const tslib_1 = require("tslib");
const lodash_1 = require("lodash");
class Parameterizer {
constructor(prepare, startedIndex = 1) {
this.startedIndex = 1;
this.parameterIndex = 1;
// We MUST not use pure object here because we care about the order of the keys.
// https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#description
this.idToValueMapping = new Map();
this.valueToIdMapping = new Map();
this.prepare = prepare;
this.startedIndex = startedIndex;
this.parameterIndex = startedIndex;
}
generateIdentifier(value) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
// If there is no value, ignore parameterization.
if ((0, lodash_1.isUndefined)(value))
return '';
if (this.valueToIdMapping.has(value))
return this.valueToIdMapping.get(value);
const id = yield this.prepare({
parameterIndex: this.parameterIndex++,
value,
});
this.idToValueMapping.set(id, value);
this.valueToIdMapping.set(value, id);
return id;
});
}
getBinding() {
return this.idToValueMapping;
}
clone() {
return new Parameterizer(this.prepare, this.parameterIndex + 1);
}
reset() {
this.parameterIndex = this.startedIndex;
}
}
exports.Parameterizer = Parameterizer;
//# sourceMappingURL=parameterizer.js.map