ddl-manager
Version:
store postgres procedures and triggers in files
84 lines • 2.78 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SimpleSelect = void 0;
const TableID_1 = require("../database/schema/TableID");
const AbstractAstElement_1 = require("./AbstractAstElement");
const Expression_1 = require("./expression/Expression");
class SimpleSelect extends AbstractAstElement_1.AbstractAstElement {
constructor(row) {
super();
Object.assign(this, row);
this.into = row.into || [];
}
template(spaces) {
const originalSpaces = spaces;
if (!this.into.length) {
spaces = spaces.plusOneLevel();
}
const template = [
spaces + "select",
...this.columnsTemplate(spaces),
...this.intoTemplate(spaces),
spaces + `from ${this.fromClause()}`,
spaces + "where",
(this.where instanceof Expression_1.Expression ?
this.where.toSQL(spaces.plusOneLevel()) :
spaces.plusOneLevel() +
`${this.getFromIdentifier()}.id = ${this.where}`)
];
if (!this.into.length) {
template.unshift(originalSpaces + "(");
template.push(originalSpaces + ")");
}
else {
template[template.length - 1] += ";";
}
return template;
}
columnsTemplate(spaces) {
const selectColumnsTemplate = [];
this.columns.forEach((columnName, i) => {
const comma = i === this.columns.length - 1 ? "" : ",";
const selectColumn = /^\w+$/.test(columnName) ?
`${this.getFromIdentifier()}.${columnName}` :
columnName;
selectColumnsTemplate.push(spaces.plusOneLevel() +
selectColumn +
comma);
});
return selectColumnsTemplate;
}
intoTemplate(spaces) {
if (!this.into.length) {
return [];
}
const intoTemplate = [
spaces + "into"
];
this.into.forEach((varName, i) => {
const comma = i === this.columns.length - 1 ? "" : ",";
intoTemplate.push(spaces.plusOneLevel() +
varName +
comma);
});
return intoTemplate;
}
getFromIdentifier() {
if (this.from instanceof TableID_1.TableID) {
return this.from.toStringWithoutPublic();
}
else {
return this.from.getIdentifier();
}
}
fromClause() {
if (this.from instanceof TableID_1.TableID) {
return this.from.toStringWithoutPublic();
}
else {
return this.from.toString();
}
}
}
exports.SimpleSelect = SimpleSelect;
//# sourceMappingURL=SimpleSelect.js.map