UNPKG

database-builder

Version:

Library to assist in creating and maintaining SQL commands.

47 lines (46 loc) 1.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ProjectionCase = void 0; var builder_compiled_1 = require("../core/builder-compiled"); var utils_1 = require("../core/utils"); var projection_case_when_1 = require("./projection-case-when"); var ProjectionCase = /** @class */ (function () { function ProjectionCase(expression, _alias) { if (expression === void 0) { expression = void 0; } if (_alias === void 0) { _alias = void 0; } this._alias = _alias; this._caseBuilder = new builder_compiled_1.BuilderCompiled(); this._caseBuilder.builder = "CASE"; if (expression) { this._caseBuilder.builder += " ".concat(utils_1.Utils.getColumn(expression)); } } ProjectionCase.prototype.when = function (value, whenCallback) { var instanceWhen = new projection_case_when_1.ProjectionCaseWhen(value); whenCallback(instanceWhen); this.compileWhen(instanceWhen.compile()); return this; }; // CASE {expression} {when} END ProjectionCase.prototype.compile = function () { var result = new builder_compiled_1.BuilderCompiled(this._caseBuilder.builder, this._caseBuilder.params); if (result.builder.length) { result.builder += " END"; if (this._alias) { result.builder += " AS ".concat(this._alias); } } return result; }; ProjectionCase.prototype.compileWhen = function (compiled) { var _this = this; if (compiled.builder.length) { this._caseBuilder.builder += " ".concat(compiled.builder); compiled.params.forEach(function (value) { _this._caseBuilder.params.push(value); }); } }; return ProjectionCase; }()); exports.ProjectionCase = ProjectionCase;