ddl-manager
Version:
store postgres procedures and triggers in files
141 lines (139 loc) • 5.79 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LastRowByIdTriggerBuilder = void 0;
const ast_1 = require("../../../ast");
const Exists_1 = require("../../../ast/expression/Exists");
const Comment_1 = require("../../../database/schema/Comment");
const DatabaseFunction_1 = require("../../../database/schema/DatabaseFunction");
const DatabaseTrigger_1 = require("../../../database/schema/DatabaseTrigger");
const AbstractLastRowTriggerBuilder_1 = require("./AbstractLastRowTriggerBuilder");
const buildOneLastRowByIdBody_1 = require("../body/buildOneLastRowByIdBody");
const CacheContext_1 = require("../CacheContext");
class LastRowByIdTriggerBuilder extends AbstractLastRowTriggerBuilder_1.AbstractLastRowTriggerBuilder {
createTriggers() {
return [{
trigger: this.createDatabaseTriggerOnDIU(),
procedure: this.createDatabaseFunction(this.createBody())
}, ...this.createHelperTriggers()];
}
createHelperTriggers() {
const orderBy = this.context.cache.select.orderBy.items[0];
if (orderBy.type === "asc") {
return [];
}
const isLastColumnName = this.getIsLastColumnName();
const helperTriggerName = [
"cache",
this.context.cache.name,
"for",
CacheContext_1.shortName(this.context.cache.for.table.name),
"before",
"insert",
CacheContext_1.shortName(this.context.triggerTable.name)
].join("_");
const trigger = new DatabaseTrigger_1.DatabaseTrigger({
name: helperTriggerName,
before: true,
insert: true,
procedure: {
schema: "public",
name: helperTriggerName,
args: []
},
table: this.context.triggerTable,
comment: Comment_1.Comment.fromFs({
objectType: "trigger",
cacheSignature: this.context.cache.getSignature()
})
});
return [{
trigger,
procedure: new DatabaseFunction_1.DatabaseFunction({
schema: "public",
name: helperTriggerName,
body: `
begin
new.${isLastColumnName} = coalesce((
${this.conditions
.hasReferenceWithoutJoins("new")
.toSQL(ast_1.Spaces.level(2))}
), false);
return new;
end;
`.trim(),
comment: Comment_1.Comment.fromFs({
objectType: "function",
cacheSignature: this.context.cache.getSignature()
}),
args: [],
returns: { type: "trigger" }
})
}];
}
createBody() {
const isLastColumnName = this.getIsLastColumnName();
const triggerTable = this.triggerTableAlias();
const clearLastColumnOnInsert = new ast_1.Update({
table: this.fromTable().toString(),
set: [new ast_1.SetItem({
column: isLastColumnName,
value: ast_1.Expression.unknown("false")
})],
where: this.filterTriggerTable("new", [
`${triggerTable}.id < new.id`,
`${triggerTable}.${isLastColumnName} = true`
])
});
const orderBy = this.context.cache.select.orderBy.items[0];
const selectMaxPrevId = new ast_1.SimpleSelect({
columns: [
`${orderBy.type == "desc" ? "max" : "min"}( ${triggerTable}.id )`
],
from: this.context.cache.select.getFromTable(),
where: this.filterTriggerTable("new", [
`${triggerTable}.id <> new.id`
])
});
const existsPrevRow = new Exists_1.Exists({
select: this.context.cache.select.clone({
columns: [],
where: this.filterTriggerTable("new", [
`${triggerTable}.id < new.id`
]),
orderBy: undefined,
limit: undefined
})
});
const body = buildOneLastRowByIdBody_1.buildOneLastRowByIdBody({
orderVector: orderBy.type,
isLastColumn: isLastColumnName,
ifNeedUpdateNewOnChangeReference: ast_1.Expression.or([
`prev_id ${orderBy.type === "desc" ? "<" : ">"} new.id`,
"prev_id is null"
]),
updateNew: this.updateNew(),
updatePrev: this.updatePrev(),
exitFromDeltaUpdateIf: this.conditions.exitFromDeltaUpdateIf(),
selectMaxPrevId,
selectPrevRow: this.selectPrevRowByOrder(),
existsPrevRow,
updateMaxRowLastColumnFalse: this.updateMaxRowLastColumnFalse("prev_id"),
updateThisRowLastColumnTrue: this.updateThisRowLastColumn("true"),
clearLastColumnOnInsert,
updatePrevRowLastColumnTrue: this.updatePrevRowLastColumnTrue(),
hasNewReference: this.conditions
.hasReferenceWithoutJoins("new"),
hasOldReference: this.conditions
.hasReferenceWithoutJoins("old"),
hasOldReferenceAndIsLast: ast_1.Expression.and([
this.conditions.hasReferenceWithoutJoins("old"),
`old.${isLastColumnName}`
]),
noChanges: this.conditions.noChanges(),
noReferenceChanges: this.conditions.noReferenceChanges()
});
return body;
}
}
exports.LastRowByIdTriggerBuilder = LastRowByIdTriggerBuilder;
//# sourceMappingURL=LastRowByIdTriggerBuilder.js.map