UNPKG

ddl-manager

Version:

store postgres procedures and triggers in files

94 lines 4.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CommutativeTriggerBuilder = void 0; const AbstractTriggerBuilder_1 = require("../AbstractTriggerBuilder"); const buildCommutativeBody_1 = require("../body/buildCommutativeBody"); const ast_1 = require("../../../ast"); const SetItemsFactory_1 = require("../../processor/SetItemsFactory"); class CommutativeTriggerBuilder extends AbstractTriggerBuilder_1.AbstractTriggerBuilder { constructor() { super(...arguments); this.setItems = new SetItemsFactory_1.SetItemsFactory(this.context); } createTriggers() { return [{ trigger: this.createDatabaseTriggerOnDIU(), procedure: this.createDatabaseFunction(this.createBody()) }]; } createBody() { const deltaUpdate = new ast_1.Update({ table: this.context.cache.for.toString(), set: this.setItems.plus(), where: this.conditions.simpleWhere("new") }); const body = buildCommutativeBody_1.buildCommutativeBody(this.needListenInsert(), this.conditions.hasMutableColumns(), this.conditions.noChanges(), { hasReferenceWithoutJoins: this.conditions.hasReferenceWithoutJoins("old"), needUpdate: this.conditions.filtersWithJoins("old"), update: new ast_1.Update({ table: this.context.cache.for.toString(), set: this.setItems.minus(), where: this.conditions.simpleWhere("old") }) }, { hasReferenceWithoutJoins: this.conditions.hasReferenceWithoutJoins("new"), needUpdate: this.conditions.filtersWithJoins("new"), update: new ast_1.Update({ table: this.context.cache.for.toString(), set: this.setItems.plus(), where: this.conditions.simpleWhere("new") }) }, { needUpdate: this.conditions.noReferenceChanges(), update: deltaUpdate, exitIf: this.conditions.exitFromDeltaUpdateIf(), old: { needUpdate: this.conditions.needUpdateConditionOnUpdate("old"), update: this.needUpdateInDelta() ? new ast_1.Update({ table: this.context.cache.for.toString(), set: this.setItems.minus(), where: this.conditions.simpleWhereOnUpdate("old") }) : undefined }, new: { needUpdate: this.conditions.needUpdateConditionOnUpdate("new"), update: this.needUpdateInDelta() ? new ast_1.Update({ table: this.context.cache.for.toString(), set: this.setItems.plus(), where: this.conditions.simpleWhereOnUpdate("new") }) : undefined } }); return body; } needListenInsert() { if (this.context.withoutInsertCase()) { return false; } const isReferenceByTriggerTableId = this.context.referenceMeta .expressions.some(condition => { if (!condition.isBinary("=")) { return false; } const [left, right] = condition.splitBy("="); return (this.isTriggerTableId(left) || this.isTriggerTableId(right)); }); return !isReferenceByTriggerTableId; } isTriggerTableId(condition) { const columnRefs = condition.getColumnReferences(); const isTriggerTableIdColumn = (columnRefs.length === 1 && columnRefs[0].name === "id" && this.context.isColumnRefToTriggerTable(columnRefs[0])); return isTriggerTableIdColumn; } needUpdateInDelta() { const hasCondition = !!this.conditions.needUpdateConditionOnUpdate("new"); const hasUnknownReferenceExpressions = this.context.referenceMeta.unknownExpressions.length; return (hasCondition || hasUnknownReferenceExpressions); } } exports.CommutativeTriggerBuilder = CommutativeTriggerBuilder; //# sourceMappingURL=CommutativeTriggerBuilder.js.map