UNPKG

ddl-manager

Version:

store postgres procedures and triggers in files

203 lines 8.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AbstractLastRowTriggerBuilder = void 0; const ast_1 = require("../../../ast"); const AbstractTriggerBuilder_1 = require("../AbstractTriggerBuilder"); class AbstractLastRowTriggerBuilder extends AbstractTriggerBuilder_1.AbstractTriggerBuilder { getIsLastColumnName() { return this.context.cache.getIsLastColumnName(); } whereDistinctRowValues(row) { const selectColumns = this.context.cache.select.columns; const prevValues = selectColumns.map(selectColumn => this.replaceTriggerTableToRow(row, selectColumn.expression)); return this.whereDistinctFrom(prevValues); } whereDistinctFrom(values) { const selectColumns = this.context.cache.select.columns; const compareConditions = selectColumns.map((selectColumn, i) => { // TODO: test hard expressions const cacheColumnRef = new ast_1.ColumnReference(this.context.cache.for, selectColumn.name); return ast_1.UnknownExpressionElement.fromSql(`${cacheColumnRef} is distinct from ${values[i]}`); }); return ast_1.Expression.or(compareConditions); } setItemsByRow(row) { const setItems = this.context.cache.select.columns.map(selectColumn => { const setItem = new ast_1.SetItem({ column: selectColumn.name, value: this.replaceTriggerTableToRow(row, selectColumn.expression) }); return setItem; }); return setItems; } updateNew() { return new ast_1.Update({ table: this.context.cache.for.toString(), set: this.setItemsByRow("new"), where: ast_1.Expression.and([ this.conditions.simpleWhere("new"), this.whereDistinctRowValues("new") ]) }); } updatePrev() { return new ast_1.Update({ table: this.context.cache.for.toString(), set: this.setItemsByRow("prev_row"), where: ast_1.Expression.and([ this.conditions.simpleWhere("old"), this.whereDistinctRowValues("prev_row") ]) }); } updatePrevRowLastColumnTrue() { const triggerTable = this.triggerTableAlias(); return new ast_1.Update({ table: this.fromTable().toString(), set: [new ast_1.SetItem({ column: this.getIsLastColumnName(), value: ast_1.Expression.unknown("true") })], where: ast_1.Expression.and([ `${triggerTable}.id = prev_row.id` ]) }); } updateMaxRowLastColumnFalse(filterBy) { const isLastColumnName = this.getIsLastColumnName(); const triggerTable = this.triggerTableAlias(); return new ast_1.Update({ table: this.fromTable().toString(), set: [new ast_1.SetItem({ column: isLastColumnName, value: ast_1.Expression.unknown("false") })], where: ast_1.Expression.and([ `${triggerTable}.id = ${filterBy}`, `${isLastColumnName} = true` ]) }); } updateThisRowLastColumn(value) { const triggerTable = this.triggerTableAlias(); return new ast_1.Update({ table: this.fromTable().toString(), set: [new ast_1.SetItem({ column: this.getIsLastColumnName(), value: ast_1.Expression.unknown(value) })], where: ast_1.Expression.and([ `${triggerTable}.id = new.id` ]) }); } allPrevRowColumns() { const selectPrevRowColumnsNames = this.context.triggerTableColumns.slice(); if (!selectPrevRowColumnsNames.includes("id")) { selectPrevRowColumnsNames.unshift("id"); } return selectPrevRowColumnsNames.map(name => ast_1.SelectColumn.onlyName(name)); } selectPrevRowByOrder() { return this.context.cache.select.clone({ columns: this.allPrevRowColumns(), where: this.filterTriggerTable("old"), intoRow: "prev_row" }); } filterTriggerTable(byRow, andConditions = []) { const triggerTable = this.triggerTableAlias(); return ast_1.Expression.and([ ...this.context.referenceMeta.columns.map(column => `${triggerTable}.${column} = ${byRow}.${column}`), ...this.context.referenceMeta.filters, ...andConditions ]); } fromTable() { return this.context.cache.select.getFromTable(); } triggerTableAlias() { const fromTable = this.fromTable(); if (fromTable.alias) { return fromTable.alias; } return fromTable.table.toStringWithoutPublic(); } findDataColumns() { const dataColumns = []; this.context.cache.select.columns.forEach(selectColumn => selectColumn.expression.getColumnReferences() .forEach(columnRef => { if (this.context.isColumnRefToTriggerTable(columnRef)) { if (!dataColumns.includes(columnRef.name)) { dataColumns.push(columnRef.name); } } })); return dataColumns; } reselectSetItem() { return new ast_1.SetSelectItem({ columns: [ ...this.getOrderByColumnsRefs().map(columnRef => this.helperColumnName(columnRef.name)), ...this.context.cache.select.columns.map(selectColumn => selectColumn.name) ], select: this.reselect() }); } reselect() { const { select } = this.context.cache; const orderByItems = select.orderBy.items.slice(); if (!select.orderBy.isOnlyId()) { const firstOrder = orderByItems[0]; orderByItems.push(new ast_1.OrderByItem({ expression: ast_1.Expression.unknown(`${this.fromTable().getIdentifier()}.id`), type: firstOrder.type })); } const reselect = select.clone({ columns: [ ...this.getOrderByColumnsRefs().map(columnRef => new ast_1.SelectColumn({ name: this.helperColumnName(columnRef.name), expression: ast_1.Expression.unknown(this.triggerTableAlias() + "." + columnRef.name) })), ...select.columns ], orderBy: new ast_1.OrderBy(orderByItems) }); return reselect; } getOrderByColumnsRefs() { return ast_1.getOrderByColumnsRefs(this.context.cache.select); } helperColumnName(triggerTableColumnName) { return ast_1.helperColumnName(this.context.cache, triggerTableColumnName); } setHelpersByRow(row = "new") { const helpers = this.getOrderByColumnsRefs().map(columnRef => new ast_1.SetItem({ column: this.helperColumnName(columnRef.name), value: ast_1.Expression.unknown(row + "." + columnRef.name) })); return helpers; } whereIsGreat(additionalOr = []) { const orderBy = this.context.cache.select.orderBy; const cacheTable = (this.context.cache.for.alias || this.context.cache.for.table.toStringWithoutPublic()); const cacheRow = (columnName) => `${cacheTable}.${this.helperColumnName(columnName)}`; if (orderBy.isOnlyId()) { if (orderBy.items[0].type === "asc") { return [ `${cacheRow("id")} is null` ]; } return []; } return [orderBy.compareRowsByOrder(cacheRow, "below", "new", [ ...additionalOr, `${cacheRow("id")} is null` ])]; } } exports.AbstractLastRowTriggerBuilder = AbstractLastRowTriggerBuilder; //# sourceMappingURL=AbstractLastRowTriggerBuilder.js.map