@sqb/builder
Version:
Extensible multi-dialect SQL query builder written with TypeScript
54 lines (53 loc) • 1.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ParamExpression = void 0;
const enums_js_1 = require("../enums.js");
const serializable_js_1 = require("../serializable.js");
class ParamExpression extends serializable_js_1.Serializable {
constructor(arg) {
super();
if (typeof arg === 'object') {
this._name = arg.name;
this._dataType = arg.dataType;
this._isArray = arg.isArray;
}
else
this._name = arg;
}
get _type() {
return enums_js_1.SerializationType.EXTERNAL_PARAMETER;
}
/**
* Performs serialization
*/
_serialize(ctx) {
const o = {
name: this._name,
dataType: this._dataType,
isArray: this._isArray,
};
return ctx.serialize(this._type, o, () => this.__defaultSerialize(ctx, o));
}
__defaultSerialize(ctx, o) {
let prmValue = (ctx.params && ctx.params[o.name]) ?? null;
if (prmValue != null && o.isArray && !Array.isArray(prmValue))
prmValue = [prmValue];
ctx.preparedParams = ctx.preparedParams || {};
if (Array.isArray(ctx.preparedParams))
ctx.preparedParams.push(prmValue);
else
ctx.preparedParams[o.name] = prmValue;
const paramOps = {
dataType: this._dataType,
isArray: this._isArray,
};
ctx.paramOptions =
ctx.paramOptions || (Array.isArray(ctx.preparedParams) ? [] : {});
if (Array.isArray(ctx.paramOptions))
ctx.paramOptions.push(paramOps);
else
ctx.paramOptions[o.name] = paramOps;
return ':' + o.name;
}
}
exports.ParamExpression = ParamExpression;