ddl-manager
Version:
store postgres procedures and triggers in files
136 lines • 5.05 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FuncCall = void 0;
const UnknownExpressionElement_1 = require("./UnknownExpressionElement");
const AbstractExpressionElement_1 = require("./AbstractExpressionElement");
const Spaces_1 = require("../Spaces");
const OrderBy_1 = require("../OrderBy");
const OrderByItem_1 = require("../OrderByItem");
class FuncCall extends AbstractExpressionElement_1.AbstractExpressionElement {
constructor(name, args, where, distinct, orderBy) {
super();
this.name = name;
this.args = args;
this.where = where;
this.distinct = distinct ? true : false;
this.orderBy = orderBy;
}
children() {
const children = this.args.slice();
if (this.where) {
children.push(this.where);
}
if (this.orderBy) {
this.orderBy.items.forEach(item => {
children.push(item.expression);
});
}
return children;
}
getOnlyName() {
return this.name.split(".").pop();
}
getFirstArg() {
return this.args[0];
}
getLastArg() {
return this.args[this.args.length - 1];
}
getFuncCalls() {
return [
this,
...super.getFuncCalls()
];
}
replaceTable(replaceTable, toTable) {
const newArgs = this.args.map(arg => arg.replaceTable(replaceTable, toTable));
let orderBy;
if (this.orderBy) {
const orderByItems = this.orderBy.items.map(item => new OrderByItem_1.OrderByItem(Object.assign(Object.assign({}, item), { expression: item.expression.replaceTable(replaceTable, toTable) })));
orderBy = new OrderBy_1.OrderBy(orderByItems);
}
let newWhere = this.where;
if (newWhere) {
newWhere = newWhere.replaceTable(replaceTable, toTable);
}
return this.clone(newArgs, orderBy, newWhere);
}
replaceColumn(replaceColumn, toSql) {
const newArgs = this.args.map(arg => arg.replaceColumn(replaceColumn, toSql));
let orderBy;
if (this.orderBy) {
const orderByItems = this.orderBy.items.map(item => new OrderByItem_1.OrderByItem(Object.assign(Object.assign({}, item), { expression: item.expression.replaceColumn(replaceColumn, toSql) })));
orderBy = new OrderBy_1.OrderBy(orderByItems);
}
let newWhere = this.where;
if (newWhere) {
newWhere = newWhere.replaceColumn(replaceColumn, toSql);
}
return this.clone(newArgs, orderBy, newWhere);
}
replaceFuncCall(replaceFunc, toSql) {
if (replaceFunc.equal(this)) {
return UnknownExpressionElement_1.UnknownExpressionElement.fromSql(toSql);
}
const newArgs = this.args.map(arg => arg.replaceFuncCall(replaceFunc, toSql));
let orderBy;
if (this.orderBy) {
const orderByItems = this.orderBy.items.map(item => new OrderByItem_1.OrderByItem(Object.assign(Object.assign({}, item), { expression: item.expression.replaceFuncCall(replaceFunc, toSql) })));
orderBy = new OrderBy_1.OrderBy(orderByItems);
}
let newWhere = this.where;
if (newWhere) {
newWhere = newWhere.replaceFuncCall(replaceFunc, toSql);
}
return this.clone(newArgs, orderBy, newWhere);
}
clone(newArgs, newOrderBy, newWhere) {
return new FuncCall(this.name, newArgs || this.args.map(arg => arg.clone()), newWhere || (this.where ?
this.where.clone() :
undefined), this.distinct, newOrderBy || this.cloneOrderBy());
}
withoutWhere() {
return new FuncCall(this.name, this.args.map(arg => arg.clone()), undefined, this.distinct, this.cloneOrderBy());
}
template(spaces) {
let sql = "";
sql += `${this.name}(`;
if (this.distinct) {
sql += "distinct ";
}
const isLongArgs = (this.orderBy ||
this.args.join(", ").trim().length > 24);
if (isLongArgs) {
sql += "\n";
for (let i = 0, n = this.args.length; i < n; i++) {
if (i > 0) {
sql += ",\n";
}
const arg = this.args[i];
sql += arg.toSQL(Spaces_1.Spaces.level(1));
}
sql += "\n";
sql += spaces;
}
else {
sql += this.args.join(", ");
}
if (this.orderBy) {
sql += this.orderBy.toSQL(Spaces_1.Spaces.level(1)) + "\n";
}
sql += ")";
if (this.where) {
sql += " filter (where ";
sql += Spaces_1.Spaces.level(1) + this.where.toString();
sql += ")";
}
return sql.split("\n").map(line => spaces + line);
}
cloneOrderBy() {
if (this.orderBy) {
return this.orderBy.clone();
}
}
}
exports.FuncCall = FuncCall;
//# sourceMappingURL=FuncCall.js.map