UNPKG

database-builder

Version:

Library to assist in creating and maintaining SQL commands.

116 lines (115 loc) 5.53 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ColumnsValuesBuilder = void 0; var errors_1 = require("./errors"); var key_utils_1 = require("./key-utils"); var primary_key_type_1 = require("./enums/primary-key-type"); var utils_1 = require("./utils"); var columns_base_builder_1 = require("./columns-base-builder"); var column_type_1 = require("./enums/column-type"); var ColumnsValuesBuilder = /** @class */ (function (_super) { __extends(ColumnsValuesBuilder, _super); function ColumnsValuesBuilder(mapperTable, toSave) { return _super.call(this, mapperTable, toSave) || this; } ColumnsValuesBuilder.prototype.setColumnValue = function (column, values, fieldType, primaryKeyType) { var _this = this; values = values.map(function (value, index) { switch (primaryKeyType) { case primary_key_type_1.PrimaryKeyType.Assigned: if (utils_1.Utils.isNull(value)) { throw new errors_1.DatabaseBuilderError("Primary key to be informed when generation strategy is 'Assigned'!"); } return value; case primary_key_type_1.PrimaryKeyType.Guid: if ((utils_1.Utils.isNull(value) || value.length === 0) && _this.allowGenerateKey()) { // gerar GUID value = utils_1.Utils.GUID(); // set value GUID in model key_utils_1.KeyUtils.setKey(_this.mapperTable, utils_1.Utils.isArray(_this.toSave) ? _this.toSave[index] : _this.toSave, value); } return value; case primary_key_type_1.PrimaryKeyType.AutoIncrement: default: return value; } }); this._columns.push({ name: column, type: fieldType, value: utils_1.Utils.getValueType(values, fieldType), primaryKeyType: primaryKeyType }); return this.getInstance(); }; ColumnsValuesBuilder.prototype.setValue = function (expression, value, primaryKeyType) { return this.setColumnValue(utils_1.Utils.getColumn(expression), utils_1.Utils.isArray(value) ? value : [value], utils_1.Utils.getType(value), primaryKeyType); }; ColumnsValuesBuilder.prototype.set = function (expression, primaryKeyType) { return this.setValue(expression, this.getValueByExpression(expression), primaryKeyType); }; ColumnsValuesBuilder.prototype.compile = function () { var _this = this; var result = { columns: [], keyColumns: [], params: [], }; result.keyColumns = this._columns.filter(function (x) { return !!x.primaryKeyType; }).map(function (x) { return x.name; }); this._columns.forEach(function (column) { if (_this.isAddColumn(column)) { var columnName = _this.columnFormat(column); if (!utils_1.Utils.isNull(columnName)) { result.columns.push(columnName); var values = (utils_1.Utils.isArray(column.value) ? column.value : [column.value]); for (var index = 0; index < values.length; index++) { var item = values[index]; if (!result.params[index]) result.params[index] = []; result.params[index].push(_this.resolveNullValueType(item)); } } } }); return result; }; ColumnsValuesBuilder.prototype.resolveNullValueType = function (value) { return utils_1.Utils.isNull(value) ? null : value; }; ColumnsValuesBuilder.prototype.allowGenerateKey = function () { return false; }; ColumnsValuesBuilder.prototype.isAddColumn = function (column) { // is table reference/list var columnType = utils_1.Utils.parseColumnType(column.type); if (columnType === column_type_1.ColumnType.TABLE_REFERENCE) { return false; } return true; }; ColumnsValuesBuilder.prototype.columnFormat = function (column) { return column.name; }; ColumnsValuesBuilder.prototype.getValueByExpression = function (expression) { return utils_1.Utils.getValue(this.toSave, expression); }; return ColumnsValuesBuilder; }(columns_base_builder_1.ColumnsBaseBuilder)); exports.ColumnsValuesBuilder = ColumnsValuesBuilder;