UNPKG

sedk-postgres

Version:

Simple SQL builder and validator

72 lines 3.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ReturningStep = void 0; const RootStep_1 = require("./RootStep"); const SelectStep_1 = require("./select-path/SelectStep"); const AggregateFunction_1 = require("../AggregateFunction"); const binder_1 = require("../binder"); const database_1 = require("../database"); const ItemInfo_1 = require("../ItemInfo"); const models_1 = require("../models"); const SelectItemInfo_1 = require("../SelectItemInfo"); const singletoneConstants_1 = require("../singletoneConstants"); const TableAsterisk_1 = require("../TableAsterisk"); const ReturningItemInfo_1 = require("../ReturningItemInfo"); const BaseStep_1 = require("./BaseStep"); class ReturningStep extends BaseStep_1.BaseStep { constructor(prevStep, returningItems) { super(prevStep); if (returningItems.length === 0) { throw new Error('RETURNING step items cannot be empty'); } // find first step and check if it is Select let step = prevStep; while (step !== null && !(step.prevStep instanceof RootStep_1.RootStep)) { step = step.prevStep; } if (step instanceof SelectStep_1.SelectStep) { throw new Error('Returning step can not be used in SELECT statement, It can be only use if the path start with INSERT, DELETE, or UPDATE'); } const returningItemInfo = returningItems.map(it => { if (it instanceof ReturningItemInfo_1.ReturningItemInfo) { return it; } else if (it instanceof models_1.Expression || it instanceof database_1.Column || it instanceof singletoneConstants_1.Asterisk || it instanceof TableAsterisk_1.TableAsterisk) { return new ReturningItemInfo_1.ReturningItemInfo(it, undefined); } else if (it instanceof binder_1.Binder) { if (it.no === undefined) { this.binderStore.add(it); } return new ReturningItemInfo_1.ReturningItemInfo(it, undefined); } else if (it instanceof SelectItemInfo_1.SelectItemInfo) { if (it.selectItem instanceof AggregateFunction_1.AggregateFunction) { throw new Error(`Aggregate function ${it.selectItem.funcName} cannot be used in RETURNING clause`); } else { return new ReturningItemInfo_1.ReturningItemInfo(it.selectItem, it.alias); } } else if (it instanceof ItemInfo_1.ItemInfo) { // not possible as long as ItemInfo is an abstract class throw new Error('ItemInfo is an abstract class'); } else { //it from here is a PrimitiveType return new ReturningItemInfo_1.ReturningItemInfo(models_1.Expression.getSimpleExp(it), undefined); } }); this.throwIfColumnsNotInDb(returningItemInfo); this.returningItemInfo = returningItemInfo; } getStepStatement(artifacts = { tables: new Set(), columns: new Set() }) { const returningPartsString = this.returningItemInfo.map(it => { return it.getStmt(this.data, artifacts, this.binderStore); }); return `RETURNING ${returningPartsString.join(', ')}`; } getStepArtifacts() { return { tables: new Set(), columns: new Set(this.returningItemInfo.map(it => it.getColumns()).flat(1)) }; } } exports.ReturningStep = ReturningStep; //# sourceMappingURL=ReturningStep.js.map