ddl-manager
Version:
store postgres procedures and triggers in files
56 lines • 2.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fixArraySearchForDifferentArrayTypesInCondition = exports.fixArraySearchForDifferentArrayTypes = void 0;
const ast_1 = require("../../../ast");
// form_table.x && cache_row.y
// =>
// form_table.x && cast(cache_row.y as ...)
function fixArraySearchForDifferentArrayTypes(fromTable, where) {
const conditions = where.splitBy("and").map(condition => fixArraySearchForDifferentArrayTypesInCondition(fromTable, condition));
return ast_1.Expression.and(conditions);
}
exports.fixArraySearchForDifferentArrayTypes = fixArraySearchForDifferentArrayTypes;
function fixArraySearchForDifferentArrayTypesInCondition(fromTable, condition) {
if (!fromTable || !condition.isBinary("&&")) {
return condition;
}
const [leftOperand, rightOperand] = condition.getOperands();
const fromTableColumn = detectFromTableColumn(fromTable, leftOperand, rightOperand);
const otherOperand = detectOther(fromTableColumn, leftOperand, rightOperand);
if (!fromTableColumn || !otherOperand) {
return condition;
}
const table = fromTableColumn.tableReference.table;
const columnName = fromTableColumn.name;
return new ast_1.Expression([
fromTableColumn,
new ast_1.Operator("&&"),
// array[]::bigint[] && some_bigint_ids
ast_1.UnknownExpressionElement.fromSql(`cm_build_array_for((null::${table.schema}.${table.name}).${columnName}, ${otherOperand})`, getColumnReferencesMap(otherOperand))
]);
}
exports.fixArraySearchForDifferentArrayTypesInCondition = fixArraySearchForDifferentArrayTypesInCondition;
function detectFromTableColumn(fromTable, left, right) {
if (left instanceof ast_1.ColumnReference && left.isFrom(fromTable)) {
return left;
}
if (right instanceof ast_1.ColumnReference && right.isFrom(fromTable)) {
return right;
}
}
function detectOther(other, left, right) {
if (left === other) {
return right;
}
if (right === other) {
return left;
}
}
function getColumnReferencesMap(operand) {
const map = {};
for (const columnRef of operand.getColumnReferences()) {
map[columnRef.toString()] = columnRef;
}
return map;
}
//# sourceMappingURL=fixArraySearchForDifferentArrayTypes.js.map