sedk-mysql
Version:
Simple SQL builder and validator for MySQL
84 lines • 3.27 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Column = void 0;
const util_1 = require("../util");
const orderBy_1 = require("../orderBy");
const SelectItemInfo_1 = require("../SelectItemInfo");
const UpdateSetItemInfo_1 = require("../UpdateSetItemInfo");
const singletoneConstants_1 = require("../singletoneConstants");
const errors_1 = require("../errors");
class Column {
constructor(data) {
this.data = data;
}
set table(table) {
if (this.mTable === undefined)
this.mTable = table;
else
throw new Error('Table can only be assigned one time');
}
get table() {
if (this.mTable === undefined)
throw new Error('Table was not assigned');
return this.mTable;
}
get name() {
return this.data.name;
}
get fqName() {
return `${this.table.fqName}.\`${(0, util_1.escapeBackTick)(this.data.name)}\``;
}
getDoubleQuotedName() {
return `\`${(0, util_1.escapeBackTick)(this.data.name)}\``;
}
as(alias) {
return new SelectItemInfo_1.SelectItemInfo(this, alias);
}
get ASC() {
return new orderBy_1.OrderByItemInfo(this, orderBy_1.ASC, orderBy_1.NULLS_POSITION_NOT_EXIST);
}
get DESC() {
return new orderBy_1.OrderByItemInfo(this, orderBy_1.DESC, orderBy_1.NULLS_POSITION_NOT_EXIST);
}
get NULLS_FIRST() {
return new orderBy_1.OrderByItemInfo(this, orderBy_1.DIRECTION_NOT_EXIST, orderBy_1.NULLS_FIRST);
}
get NULLS_LAST() {
return new orderBy_1.OrderByItemInfo(this, orderBy_1.DIRECTION_NOT_EXIST, orderBy_1.NULLS_LAST);
}
get ASC_NULLS_FIRST() {
return new orderBy_1.OrderByItemInfo(this, orderBy_1.ASC, orderBy_1.NULLS_FIRST);
}
get DESC_NULLS_FIRST() {
return new orderBy_1.OrderByItemInfo(this, orderBy_1.DESC, orderBy_1.NULLS_FIRST);
}
get ASC_NULLS_LAST() {
return new orderBy_1.OrderByItemInfo(this, orderBy_1.ASC, orderBy_1.NULLS_LAST);
}
get DESC_NULLS_LAST() {
return new orderBy_1.OrderByItemInfo(this, orderBy_1.DESC, orderBy_1.NULLS_LAST);
}
get eqDEFAULT() {
return new UpdateSetItemInfo_1.UpdateSetItemInfo(this, singletoneConstants_1.DEFAULT);
}
getStmt(data, artifacts) {
if (this.mTable === undefined)
throw new Error('Table of this column is undefined');
const schemaName = Array
.from(artifacts.tables)
.some(it => it !== this.table && it.name === this.table.name)
? `\`${(0, util_1.escapeBackTick)(this.table.schema.name)}\`.`
: '';
const tableName = (data.option.addTableName === 'always'
|| (data.option.addTableName === 'when two tables or more'
&& Array.from(artifacts.tables)
.some(it => it !== this.table))) ? `\`${(0, util_1.escapeBackTick)(this.table.name)}\`.` : '';
return `${schemaName}${tableName}\`${(0, util_1.escapeBackTick)(this.data.name)}\``;
}
static throwIfArrayIsEmpty(arr, operator) {
if (arr.length === 0)
throw new errors_1.EmptyArrayError(operator);
}
}
exports.Column = Column;
//# sourceMappingURL=Column.js.map