UNPKG

sedk-mysql

Version:
99 lines 4.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IntoColumnsStep = exports.IntoTableStep = exports.IntoStep = void 0; const TableAsterisk_1 = require("../../TableAsterisk"); const util_1 = require("../../util"); const BaseStep_1 = require("../BaseStep"); const errors_1 = require("../../errors"); const binder_1 = require("../../binder"); const SelectStep_1 = require("../select-path/SelectStep"); const singletoneConstants_1 = require("../../singletoneConstants"); const ValuesStep_1 = require("./ValuesStep"); class IntoStep extends BaseStep_1.BaseStep { values(...values) { return new ValuesStep_1.ValuesStep(this, (0, util_1.getMinOneArray)(values)); } values$(...values) { return new ValuesStep_1.ValuesStep(this, (0, util_1.getMinOneArray)(values.map(it => new binder_1.Binder(it)))); } select(...items) { // TODO: consider adding DISTINCT and ALL to items without effecting matching number of values // TODO: consider adding ASTERISK and Table Asterisk to items without throwing error if matching column number if (items.length === 0) { throw new Error('Invalid empty SELECT step'); } this.throwWhenInvalidExpressionsNumber(items); return new SelectStep_1.SelectStep(this, items); } } exports.IntoStep = IntoStep; class IntoTableStep extends IntoStep { constructor(prevStep, table) { super(prevStep); this.table = table; this.throwIfTableNotInDb(table); return new Proxy(this, { apply: (target, thisArg, args) => target.selfCall(...args) }); } getStepStatement(artifacts = { tables: new Set(), columns: new Set() }) { return `INTO ${this.table.getStmt(this.data, artifacts)}`; } getStepArtifacts() { return { tables: new Set([this.table]), columns: new Set() }; } selfCall(...columns) { return new IntoColumnsStep(this, columns); } throwWhenInvalidExpressionsNumber(items) { if (items.find(it => it instanceof singletoneConstants_1.Asterisk)) { /** Validation can not be done when asterisk is used because it is unknown which table(s) is used */ return; } const expCount = countExpressions(items); const tableColumnCount = this.table.getColumns().length; if (expCount !== tableColumnCount) { throw new errors_1.InsertColumnsAndExpressionsNotEqualError(tableColumnCount, expCount); } } } exports.IntoTableStep = IntoTableStep; class IntoColumnsStep extends IntoStep { constructor(prevStep, columns) { super(prevStep); this.columns = columns; this.prefixSeparator = ''; } getStepStatement(artifacts = { tables: new Set(), columns: new Set() }) { return `(${this.columns.map(it => it.getDoubleQuotedName()).join(', ')})`; } getStepArtifacts() { return { tables: new Set(), columns: new Set(this.columns) }; } throwWhenInvalidExpressionsNumber(items) { if (items.find(it => it instanceof singletoneConstants_1.Asterisk)) { /** Validation can not be done when asterisk is used because it is unknown which table(s) is used */ return; } const expCount = countExpressions(items); const columnsCount = this.getStepArtifacts().columns.size; if (columnsCount === 0) { throw new Error('IntoColumnsStep must have at least one column'); } else if (expCount !== columnsCount) { throw new errors_1.InsertColumnsAndExpressionsNotEqualError(columnsCount, expCount); } } } exports.IntoColumnsStep = IntoColumnsStep; function countExpressions(items) { let expCount = 0; items.forEach(it => { if (it instanceof TableAsterisk_1.TableAsterisk) { expCount += it.table.getColumns().length; } else { expCount += 1; } }); return expCount; } //# sourceMappingURL=IntoStep.js.map