ddl-manager
Version:
store postgres procedures and triggers in files
108 lines • 4.17 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OrderByItem = void 0;
const AbstractAstElement_1 = require("./AbstractAstElement");
const expression_1 = require("./expression");
class OrderByItem extends AbstractAstElement_1.AbstractAstElement {
constructor(params) {
super();
this.expression = params.expression;
this.type = params.type || "asc";
// https://postgrespro.ru/docs/postgrespro/10/queries-order
// По умолчанию значения NULL считаются больше любых других,
this.nulls = (params.nulls || (
// то есть подразумевается NULLS FIRST для порядка DESC и
this.type === "desc" ?
"first" :
// NULLS LAST в противном случае.
"last"));
}
clone() {
return new OrderByItem({
expression: this.expression.clone(),
type: this.type,
nulls: this.nulls
});
}
replaceTable(replaceTable, toTable) {
return new OrderByItem({
expression: this.expression
.replaceTable(replaceTable, toTable),
type: this.type,
nulls: this.nulls
});
}
equal(item) {
return (this.expression.equal(item.expression) &&
this.type == item.type &&
this.nulls == item.nulls);
}
getColumnReferences() {
return this.expression.getColumnReferences();
}
template(spaces) {
return [
this.expression.toSQL(spaces) +
" " + this.type +
" nulls " + this.nulls
];
}
getFirstColumnRef() {
return this.getColumnReferences()[0];
}
isIdSort() {
return (this.expression.isColumnReference() &&
this.getFirstColumnRef().name === "id");
}
compareRowsByOrder(leftRow, vector, rightRow, orPreConditions) {
const operator = (this.type === "asc" && vector === "above" ||
this.type === "desc" && vector === "below") ? "<" : ">";
if (typeof leftRow === "string") {
const rowName = leftRow;
leftRow = (columnName) => `${rowName}.${columnName}`;
}
if (typeof rightRow === "string") {
const rowName = rightRow;
rightRow = (columnName) => `${rowName}.${columnName}`;
}
return expression_1.Expression.or([
...orPreConditions,
expression_1.Expression.and([
`${this.printRowValue(leftRow)} is not distinct from ${this.printRowValue(rightRow)}`,
`${leftRow("id")} ${operator} ${rightRow("id")}`
]),
this.compareRowsNulls(leftRow, vector, rightRow),
`${this.printRowValue(leftRow)} ${operator} ${this.printRowValue(rightRow)}`
]);
}
compareRowsNulls(leftRow, vector, rightRow) {
let conditions = [];
// https://postgrespro.ru/docs/postgrespro/10/queries-order
// По умолчанию значения NULL считаются больше любых других,
const leftShouldBeNull = (vector === "above" && this.nulls === "first"
||
vector === "below" && this.nulls === "last");
if (leftShouldBeNull) {
conditions = [
`${this.printRowValue(leftRow)} is null`,
`${this.printRowValue(rightRow)} is not null`
];
}
else {
conditions = [
`${this.printRowValue(leftRow)} is not null`,
`${this.printRowValue(rightRow)} is null`
];
}
return expression_1.Expression.and(conditions);
}
printRowValue(row) {
let expression = this.expression;
for (const columnRef of this.getColumnReferences()) {
expression = expression.replaceColumn(columnRef, expression_1.Expression.unknown(row(columnRef.name)));
}
return expression.toString();
}
}
exports.OrderByItem = OrderByItem;
//# sourceMappingURL=OrderByItem.js.map