UNPKG

ddl-manager

Version:

store postgres procedures and triggers in files

90 lines 3.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildCommutativeBody = void 0; const ast_1 = require("../../../ast"); const doIf_1 = require("./util/doIf"); const exitIf_1 = require("./util/exitIf"); function buildCommutativeBody(needInsertCase, hasMutableColumns, noChanges, oldCase, newCase, deltaCase) { const body = new ast_1.Body({ statements: [ ...buildInsertOrDeleteCase("DELETE", oldCase, "old"), ...(hasMutableColumns ? [ new ast_1.If({ if: new ast_1.HardCode({ sql: "TG_OP = 'UPDATE'" }), then: buildUpdateCaseBody(noChanges, deltaCase) }) ] : []), ...(needInsertCase ? buildInsertOrDeleteCase("INSERT", newCase, "new") : []) ] }); return body; } exports.buildCommutativeBody = buildCommutativeBody; function buildInsertOrDeleteCase(caseName, simpleCase, returnRow) { return [ new ast_1.BlankLine(), new ast_1.If({ if: new ast_1.HardCode({ sql: `TG_OP = '${caseName}'` }), then: [ new ast_1.BlankLine(), ...doIf_1.doIf(simpleCase.hasReferenceWithoutJoins, [ ...doIf_1.doIf(simpleCase.needUpdate, [simpleCase.update]) ]), new ast_1.BlankLine(), new ast_1.HardCode({ sql: `return ${returnRow};` }) ] }), new ast_1.BlankLine() ]; } function buildUpdateCaseBody(noChanges, deltaCase) { const oldUpdate = deltaCase.old.update; const newUpdate = deltaCase.new.update; const oldCaseCondition = deltaCase.old.needUpdate; const newCaseCondition = deltaCase.new.needUpdate; return [ new ast_1.If({ if: noChanges, then: [ new ast_1.HardCode({ sql: "return new;" }) ] }), new ast_1.BlankLine(), ...buildDeltaUpdate(deltaCase), new ast_1.BlankLine(), ...doIf_1.doIf(oldCaseCondition, oldUpdate ? [oldUpdate] : []), new ast_1.BlankLine(), ...doIf_1.doIf(newCaseCondition, newUpdate ? [newUpdate] : []), new ast_1.BlankLine(), new ast_1.HardCode({ sql: "return new;" }) ]; } function buildDeltaUpdate(deltaCase) { if (!deltaCase.update.set.length) { return [new ast_1.BlankLine()]; } if (deltaCase.needUpdate && !deltaCase.needUpdate.isEmpty()) { return [new ast_1.If({ if: deltaCase.needUpdate, then: [ ...exitIf_1.exitIf({ if: deltaCase.exitIf, blanksAfter: [new ast_1.BlankLine()] }), deltaCase.update, new ast_1.BlankLine(), new ast_1.HardCode({ sql: "return new;" }) ] })]; } else { return [ deltaCase.update ]; } } //# sourceMappingURL=buildCommutativeBody.js.map