UNPKG

sedk-postgres

Version:

Simple SQL builder and validator

120 lines 4.06 kB
"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.escapeDoubleQuote)(this.data.name)}"`; } getDoubleQuotedName() { return `"${(0, util_1.escapeDoubleQuote)(this.data.name)}"`; } as(alias) { return new SelectItemInfo_1.SelectItemInfo(this, alias); } /** @deprecated */ get asc() { return this.ASC; } get ASC() { return new orderBy_1.OrderByItemInfo(this, orderBy_1.ASC, orderBy_1.NULLS_POSITION_NOT_EXIST); } /** @deprecated */ get desc() { return this.DESC; } get DESC() { return new orderBy_1.OrderByItemInfo(this, orderBy_1.DESC, orderBy_1.NULLS_POSITION_NOT_EXIST); } /** @deprecated */ get nullsFirst() { return this.NULLS_FIRST; } get NULLS_FIRST() { return new orderBy_1.OrderByItemInfo(this, orderBy_1.DIRECTION_NOT_EXIST, orderBy_1.NULLS_FIRST); } /** @deprecated */ get nullsLast() { return this.NULLS_LAST; } get NULLS_LAST() { return new orderBy_1.OrderByItemInfo(this, orderBy_1.DIRECTION_NOT_EXIST, orderBy_1.NULLS_LAST); } /** @deprecated */ get ascNullsFirst() { return this.ASC_NULLS_FIRST; } get ASC_NULLS_FIRST() { return new orderBy_1.OrderByItemInfo(this, orderBy_1.ASC, orderBy_1.NULLS_FIRST); } /** @deprecated */ get descNullsFirst() { return this.DESC_NULLS_FIRST; } get DESC_NULLS_FIRST() { return new orderBy_1.OrderByItemInfo(this, orderBy_1.DESC, orderBy_1.NULLS_FIRST); } /** @deprecated */ get ascNullsLast() { return this.ASC_NULLS_LAST; } get ASC_NULLS_LAST() { return new orderBy_1.OrderByItemInfo(this, orderBy_1.ASC, orderBy_1.NULLS_LAST); } /** @deprecated */ get descNullsLast() { return this.DESC_NULLS_LAST; } get DESC_NULLS_LAST() { return new orderBy_1.OrderByItemInfo(this, orderBy_1.DESC, orderBy_1.NULLS_LAST); } /** @deprecated - since v0.15.0 use eqDEFAULT */ get letDefault() { return this.eqDEFAULT; } 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.escapeDoubleQuote)(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.escapeDoubleQuote)(this.table.name)}".` : ''; return `${schemaName}${tableName}"${(0, util_1.escapeDoubleQuote)(this.data.name)}"`; } static throwIfArrayIsEmpty(arr, operator) { if (arr.length === 0) throw new errors_1.EmptyArrayError(operator); } } exports.Column = Column; //# sourceMappingURL=Column.js.map