ddl-manager
Version:
store postgres procedures and triggers in files
70 lines • 2.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SetItemsFactory = void 0;
const ast_1 = require("../../ast");
const TableReference_1 = require("../../database/schema/TableReference");
class SetItemsFactory {
constructor(context) {
this.context = context;
}
jsonColumnName() {
return this.context.cache.jsonColumnName();
}
minus() {
const nextJson = `${this.jsonColumnName()} - old.id::text`;
return this.setSelect(nextJson);
}
plus() {
const cache = this.context.cache;
const nextJson = `cm_merge_json(
${this.jsonColumnName()},
${cache.getSourceRowJson("new")},
TG_OP
)`;
return this.setSelect(nextJson);
}
setSelect(nextJson) {
const cache = this.context.cache;
const fromRef = cache.select.getFromTable();
const sourceAlias = "source_row";
const orderBy = cache.select.getDeterministicOrderBy();
return [
new ast_1.SetItem({
column: this.jsonColumnName(),
value: new ast_1.HardCode({
sql: nextJson
})
}),
new ast_1.SetSelectItem({
columns: cache.select.columns
.map(column => column.name),
select: cache.select.fixArraySearchForDifferentArrayTypes().clone({
from: [new ast_1.From({
source: new ast_1.Select({
columns: [
new ast_1.SelectColumn({
name: "*",
expression: ast_1.Expression.unknown("record.*")
})
],
from: [new ast_1.From({
source: new ast_1.FuncCall("jsonb_each", [ast_1.Expression.unknown(nextJson)]),
as: "json_entry",
joins: [
new ast_1.Join("left join lateral", new ast_1.HardCode({
sql: `jsonb_populate_record(null::${this.context.triggerTable}, json_entry.value) as record`
}), ast_1.Expression.unknown("true"))
]
})]
}),
as: sourceAlias,
joins: cache.select.from[0].joins
})],
orderBy
}).replaceTable(fromRef, new TableReference_1.TableReference(this.context.triggerTable, sourceAlias))
})
];
}
}
exports.SetItemsFactory = SetItemsFactory;
//# sourceMappingURL=SetItemsFactory.js.map