sedk-mysql
Version:
Simple SQL builder and validator for MySQL
122 lines • 5.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OrderByStep = void 0;
const SelectStep_1 = require("./SelectStep");
const database_1 = require("../../database");
const orderBy_1 = require("../../orderBy");
const util_1 = require("../../util");
const models_1 = require("../../models");
const BaseStep_1 = require("../BaseStep");
const LimitStep_1 = require("./LimitStep");
const LimitWithOffsetStep_1 = require("./LimitWithOffsetStep");
const OffsetStep_1 = require("./OffsetStep");
class OrderByStep extends BaseStep_1.BaseStep {
constructor(prevStep, orderByArgsElement) {
super(prevStep);
this.orderByArgsElement = orderByArgsElement;
this.orderByItemInfos = [];
if (orderByArgsElement.length === 0) {
throw new Error('Order by should have at lease one item');
}
const store = { orderByItem: undefined, direction: undefined, nullsPos: undefined };
const pushWhenOrderByItemDefined = () => {
if (store.orderByItem !== undefined) {
this.orderByItemInfos.push(new orderBy_1.OrderByItemInfo(store.orderByItem, store.direction, store.nullsPos));
store.orderByItem = undefined;
store.direction = undefined;
store.nullsPos = undefined;
}
};
orderByArgsElement.forEach(it => {
var _a;
if (it instanceof orderBy_1.OrderByDirection) {
if (store.orderByItem === undefined)
throw new Error(`${it} expects to have column or alias before it`);
if (store.direction !== undefined)
throw new Error(`${it} shouldn't come after "ASC" or "DESC" without column or alias in between`);
store.direction = it;
}
else if (it instanceof orderBy_1.OrderByNullsPosition) {
if (store.orderByItem === undefined)
throw new Error(`${it} expects to have column or alias before it`);
if (store.nullsPos !== undefined)
throw new Error(`${it} shouldn't come directly after "NULLS FIRST" or "NULLS LAST" without column or alias in between`);
store.nullsPos = it;
pushWhenOrderByItemDefined();
}
else if (it instanceof orderBy_1.OrderByItemInfo) {
pushWhenOrderByItemDefined();
this.orderByItemInfos.push(it);
}
else if (it instanceof database_1.Column) {
pushWhenOrderByItemDefined();
store.orderByItem = it;
}
else if (it instanceof models_1.Expression) {
pushWhenOrderByItemDefined();
store.orderByItem = it;
}
else { //it is a string
pushWhenOrderByItemDefined();
/** look for the alias */
let prevStep = this.prevStep;
while (!(prevStep instanceof SelectStep_1.SelectStep)) {
prevStep = (_a = prevStep === null || prevStep === void 0 ? void 0 : prevStep.prevStep) !== null && _a !== void 0 ? _a : null;
if (prevStep === null) {
throw new Error(`Can't find select step to look for aliases`);
}
}
const aliases = prevStep.getAliases();
if (aliases.find(alias => alias === it)) {
store.orderByItem = `\`${(0, util_1.escapeBackTick)(it)}\``;
}
else {
throw new Error(`Alias ${it} is not exist, if this is a column, then it should be entered as Column class`);
}
}
});
pushWhenOrderByItemDefined();
}
limit(offsetOrLimit, limit = undefined) {
if (limit === undefined) {
return new LimitStep_1.LimitStep(this, offsetOrLimit);
}
return new LimitWithOffsetStep_1.LimitWithOffsetStep(this, offsetOrLimit, limit);
}
limit$(offsetOrLimit, limit = undefined) {
if (limit === undefined) {
return new LimitStep_1.LimitStep(this, offsetOrLimit, true);
}
return new LimitWithOffsetStep_1.LimitWithOffsetStep(this, offsetOrLimit, limit, true);
}
offset(value) {
return new OffsetStep_1.OffsetStep(this, value);
}
offset$(value) {
return new OffsetStep_1.OffsetStep(this, value, true);
}
getStepArtifacts() {
const columns = this.orderByArgsElement
.map(it => {
if (it instanceof database_1.Column) {
return it;
}
else if (it instanceof models_1.Expression) {
return it.getColumns();
}
else {
return [];
}
})
.flat(1);
return { tables: new Set(), columns: new Set(columns) };
}
getStepStatement(artifacts = { tables: new Set(), columns: new Set() }) {
const orderByPartsString = this.orderByItemInfos.map(it => {
return it.getStmt(this.data, artifacts, this.binderStore);
});
return `ORDER BY ${orderByPartsString.join(', ')}`;
}
}
exports.OrderByStep = OrderByStep;
//# sourceMappingURL=OrderByStep.js.map