sedk-mysql
Version:
Simple SQL builder and validator for MySQL
126 lines • 5.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SelectStep = void 0;
const errors_1 = require("../../errors");
const AggregateFunction_1 = require("../../AggregateFunction");
const binder_1 = require("../../binder");
const database_1 = require("../../database");
const ItemInfo_1 = require("../../ItemInfo");
const models_1 = require("../../models");
const SelectItemInfo_1 = require("../../SelectItemInfo");
const singletoneConstants_1 = require("../../singletoneConstants");
const BaseStep_1 = require("../BaseStep");
const SelectFromStep_1 = require("./SelectFromStep");
const TableAsterisk_1 = require("../../TableAsterisk");
class SelectStep extends BaseStep_1.BaseStep {
constructor(prevStep, items) {
super(prevStep);
if (items[0] instanceof singletoneConstants_1.Distinct) {
if (items.length <= 1)
throw new Error('Select step must have at least one parameter after DISTINCT');
items.shift(); //remove first item the DISTINCT item
// TODO: use this as type guard
SelectStep.throwIfMoreThanOneDistinctOrAll(items);
this.items = items;
this.distinct = singletoneConstants_1.DISTINCT;
}
if (items[0] instanceof singletoneConstants_1.All) {
items.shift(); //remove first item the ALL item
SelectStep.throwIfMoreThanOneDistinctOrAll(items);
this.items = items;
this.distinct = singletoneConstants_1.ALL;
}
SelectStep.throwIfMoreThanOneDistinctOrAll(items);
const columns = items.map(it => {
if (it instanceof database_1.Column || it instanceof ItemInfo_1.ItemInfo) {
return it;
}
else if (it instanceof models_1.Expression || it instanceof AggregateFunction_1.AggregateFunction || it instanceof models_1.Condition) {
return it.getColumns();
}
return [];
}).flat(1);
this.throwIfColumnsNotInDb(columns);
this.items = items;
}
getStepStatement(artifacts = { tables: new Set(), columns: new Set() }) {
const selectItemInfos = this.items.map(it => {
if (it instanceof SelectItemInfo_1.SelectItemInfo || it instanceof ItemInfo_1.ItemInfo) {
return it;
}
else if (it instanceof models_1.Expression
|| it instanceof database_1.Column
|| it instanceof AggregateFunction_1.AggregateFunction
|| it instanceof singletoneConstants_1.Asterisk
|| it instanceof TableAsterisk_1.TableAsterisk) {
return new SelectItemInfo_1.SelectItemInfo(it, undefined);
}
else if (it instanceof binder_1.Binder) {
if (it.no === undefined) {
this.binderStore.add(it);
}
return new SelectItemInfo_1.SelectItemInfo(it, undefined);
}
else {
return new SelectItemInfo_1.SelectItemInfo(models_1.Expression.getSimpleExp(it), undefined);
}
});
this.throwIfColumnsNotInDb(selectItemInfos);
let result = `SELECT`;
if (this.distinct) {
result += ` ${this.distinct}`;
}
if (selectItemInfos.length > 0) {
const selectPartsString = selectItemInfos.map(it => it.getStmt(this.data, artifacts, this.binderStore));
result += ` ${selectPartsString.join(', ')}`;
}
return result;
}
getStepArtifacts() {
return { tables: new Set(), columns: new Set(this.getColumns()) };
}
getAliases() {
return this.items
.map(it => {
if (this.isSelectItemInfo(it) && it.alias !== undefined) {
return it.alias;
}
return [];
})
.flat(1);
}
from(...tables) {
return new SelectFromStep_1.SelectFromStep(this, tables);
}
getColumns() {
return this.items.map(it => {
if (it instanceof ItemInfo_1.ItemInfo) {
return it.getColumns();
}
else if (it instanceof models_1.Expression) {
return it.getColumns();
}
else if (it instanceof database_1.Column) {
return it;
}
else if (it instanceof AggregateFunction_1.AggregateFunction) {
return it.getColumns();
}
else {
return [];
}
}).flat();
}
isSelectItemInfo(item) {
return item instanceof SelectItemInfo_1.SelectItemInfo || item instanceof ItemInfo_1.ItemInfo;
}
static throwIfMoreThanOneDistinctOrAll(items) {
items.forEach(it => {
if (it instanceof singletoneConstants_1.Distinct || it instanceof singletoneConstants_1.All)
throw new errors_1.MoreThanOneDistinctOrAllError('You can not have more than one DISTINCT or ALL');
});
return true;
}
}
exports.SelectStep = SelectStep;
//# sourceMappingURL=SelectStep.js.map