ts-sql-query
Version:
Type-safe SQL query builder like QueryDSL or JOOQ in Java or Linq in .Net for TypeScript with MariaDB, MySql, Oracle, PostgreSql, Sqlite and SqlServer support.
453 lines (452 loc) • 17.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeleteQueryBuilder = void 0;
const SqlBuilder_1 = require("../sqlBuilders/SqlBuilder");
const ITableOrView_1 = require("../utils/ITableOrView");
const values_1 = require("../expressions/values");
const chained_error_1 = require("chained-error");
const attachSource_1 = require("../utils/attachSource");
const symbols_1 = require("../utils/symbols");
const values_2 = require("../expressions/values");
const values_3 = require("../expressions/values");
const ComposeSliptQueryBuilder_1 = require("./ComposeSliptQueryBuilder");
class DeleteQueryBuilder extends ComposeSliptQueryBuilder_1.ComposeSplitQueryBuilder {
constructor(sqlBuilder, table, allowNoWhere) {
super(sqlBuilder);
this.__withs = [];
// cache
this.__query = '';
this.__params = [];
this.__table = table;
(0, ITableOrView_1.__getTableOrViewPrivate)(table).__addWiths(this.__sqlBuilder, this.__withs);
this.__allowNoWhere = allowNoWhere;
}
executeDelete(min, max) {
this.query();
const source = new Error('Query executed at');
(0, ComposeSliptQueryBuilder_1.__setQueryMetadata)(source, this.__params, this.__customization);
try {
let result = this.__sqlBuilder._queryRunner.executeDelete(this.__query, this.__params).catch((e) => {
throw (0, attachSource_1.attachSource)(new chained_error_1.default(e), source);
});
if (min !== undefined) {
result = result.then((count) => {
if (count < min) {
throw (0, attachSource_1.attachSource)(new Error("The delete operation didn't delete the minimum of " + min + " row(s)"), source);
}
if (max !== undefined && count > max) {
throw (0, attachSource_1.attachSource)(new Error("The delete operation deleted more that the maximum of " + max + " row(s)"), source);
}
return count;
});
}
return result;
}
catch (e) {
throw new chained_error_1.default(e);
}
}
executeDeleteNoneOrOne() {
this.query();
const source = new Error('Query executed at');
(0, ComposeSliptQueryBuilder_1.__setQueryMetadata)(source, this.__params, this.__customization);
try {
this.__sqlBuilder._resetUnique();
let result;
if (this.__oneColumn) {
result = this.__sqlBuilder._queryRunner.executeDeleteReturningOneColumnOneRow(this.__query, this.__params).then((value) => {
const valueSource = this.__columns['result'];
if (!(0, values_1.isValueSource)(valueSource)) {
throw new Error('The result column must be a ValueSource');
}
if (value === undefined) {
return null;
}
return this.__transformValueFromDB(valueSource, value);
}).catch((e) => {
throw (0, attachSource_1.attachSource)(new chained_error_1.default(e), source);
});
}
else {
result = this.__sqlBuilder._queryRunner.executeDeleteReturningOneRow(this.__query, this.__params).then((row) => {
if (row) {
return this.__transformRow(row);
}
else {
return null;
}
}).catch((e) => {
throw (0, attachSource_1.attachSource)(new chained_error_1.default(e), source);
});
}
return this.__applyCompositions(result, source);
}
catch (e) {
throw new chained_error_1.default(e);
}
}
executeDeleteOne() {
this.query();
const source = new Error('Query executed at');
(0, ComposeSliptQueryBuilder_1.__setQueryMetadata)(source, this.__params, this.__customization);
try {
this.__sqlBuilder._resetUnique();
let result;
if (this.__oneColumn) {
result = this.__sqlBuilder._queryRunner.executeDeleteReturningOneColumnOneRow(this.__query, this.__params).then((value) => {
const valueSource = this.__columns['result'];
if (!(0, values_1.isValueSource)(valueSource)) {
throw new Error('The result column must be a ValueSource');
}
if (value === undefined) {
throw new Error('No result returned by the database');
}
return this.__transformValueFromDB(valueSource, value);
}).catch((e) => {
throw (0, attachSource_1.attachSource)(new chained_error_1.default(e), source);
});
}
else {
result = this.__sqlBuilder._queryRunner.executeDeleteReturningOneRow(this.__query, this.__params).then((row) => {
if (row) {
return this.__transformRow(row);
}
else {
throw new Error('No result returned by the database');
}
}).catch((e) => {
throw (0, attachSource_1.attachSource)(new chained_error_1.default(e), source);
});
}
return this.__applyCompositions(result, source);
}
catch (e) {
throw new chained_error_1.default(e);
}
}
executeDeleteMany(min, max) {
const source = new Error('Query executed at');
this.query();
(0, ComposeSliptQueryBuilder_1.__setQueryMetadata)(source, this.__params, this.__customization);
try {
this.__sqlBuilder._resetUnique();
let result;
if (this.__oneColumn) {
result = this.__sqlBuilder._queryRunner.executeDeleteReturningOneColumnManyRows(this.__query, this.__params).then((values) => {
const valueSource = this.__columns['result'];
if (!(0, values_1.isValueSource)(valueSource)) {
throw new Error('The result column must be a ValueSource');
}
return values.map((value) => {
if (value === undefined) {
value = null;
}
return this.__transformValueFromDB(valueSource, value);
});
}).catch((e) => {
throw (0, attachSource_1.attachSource)(new chained_error_1.default(e), source);
});
}
else {
result = this.__sqlBuilder._queryRunner.executeDeleteReturningManyRows(this.__query, this.__params).then((rows) => {
return rows.map((row, index) => {
return this.__transformRow(row, index);
});
}).catch((e) => {
throw (0, attachSource_1.attachSource)(new chained_error_1.default(e), source);
});
}
if (min !== undefined) {
result = result.then((rows) => {
const count = rows.length;
if (count < min) {
throw (0, attachSource_1.attachSource)(new Error("The delete operation didn't delete the minimum of " + min + " row(s)"), source);
}
if (max !== undefined && count > max) {
throw (0, attachSource_1.attachSource)(new Error("The delete operation deleted more that the maximum of " + max + " row(s)"), source);
}
return rows;
});
}
return this.__applyCompositions(result, source);
}
catch (e) {
throw new chained_error_1.default(e);
}
}
query() {
this.__finishJoin();
if (this.__query) {
return this.__query;
}
try {
this.__query = this.__sqlBuilder._buildDelete(this, this.__params);
}
catch (e) {
throw new chained_error_1.default(e);
}
return this.__query;
}
params() {
this.__finishJoin();
if (!this.__query) {
this.query();
}
return this.__params;
}
__toSql(_sqlBuilder, params) {
return this.__sqlBuilder._buildDelete(this, params);
}
__toSqlForCondition(sqlBuilder, params) {
return this.__toSql(sqlBuilder, params);
}
dynamicWhere() {
this.__finishJoin();
this.__query = '';
return this;
}
where(condition) {
this.__finishJoin();
this.__query = '';
if (this.__where) {
throw new Error('Illegal state');
}
this.__where = (0, values_2.asAlwaysIfValueSource)(condition);
(0, values_3.__getValueSourcePrivate)(condition).__addWiths(this.__sqlBuilder, this.__withs);
return this;
}
and(condition) {
this.__query = '';
(0, values_3.__getValueSourcePrivate)(condition).__addWiths(this.__sqlBuilder, this.__withs);
if (this.__lastJoin) {
if (this.__lastJoin.__on) {
this.__lastJoin.__on = this.__lastJoin.__on.and((0, values_2.asAlwaysIfValueSource)(condition));
}
else {
this.__lastJoin.__on = (0, values_2.asAlwaysIfValueSource)(condition);
}
return this;
}
if (this.__where) {
this.__where = this.__where.and((0, values_2.asAlwaysIfValueSource)(condition));
}
else {
this.__where = (0, values_2.asAlwaysIfValueSource)(condition);
}
return this;
}
or(condition) {
this.__query = '';
(0, values_3.__getValueSourcePrivate)(condition).__addWiths(this.__sqlBuilder, this.__withs);
if (this.__lastJoin) {
if (this.__lastJoin.__on) {
this.__lastJoin.__on = this.__lastJoin.__on.and((0, values_2.asAlwaysIfValueSource)(condition));
}
else {
this.__lastJoin.__on = (0, values_2.asAlwaysIfValueSource)(condition);
}
return this;
}
if (this.__where) {
this.__where = this.__where.or((0, values_2.asAlwaysIfValueSource)(condition));
}
else {
this.__where = (0, values_2.asAlwaysIfValueSource)(condition);
}
return this;
}
using(table) {
this.__finishJoin();
this.__query = '';
if (!this.__using) {
this.__using = [];
}
this.__using.push(table);
(0, ITableOrView_1.__getTableOrViewPrivate)(table).__addWiths(this.__sqlBuilder, this.__withs);
return this;
}
join(table) {
this.__finishJoin();
this.__query = '';
if (this.__lastJoin) {
throw new Error('Illegal state');
}
this.__lastJoin = {
__joinType: 'join',
__tableOrView: table
};
(0, ITableOrView_1.__getTableOrViewPrivate)(table).__addWiths(this.__sqlBuilder, this.__withs);
return this;
}
innerJoin(table) {
this.__finishJoin();
this.__query = '';
if (this.__lastJoin) {
throw new Error('Illegal state');
}
this.__lastJoin = {
__joinType: 'innerJoin',
__tableOrView: table
};
(0, ITableOrView_1.__getTableOrViewPrivate)(table).__addWiths(this.__sqlBuilder, this.__withs);
return this;
}
leftJoin(source) {
this.__finishJoin();
this.__query = '';
if (this.__lastJoin) {
throw new Error('Illegal state');
}
this.__lastJoin = {
__joinType: 'leftJoin',
__tableOrView: source
};
(0, ITableOrView_1.__getTableOrViewPrivate)(source).__addWiths(this.__sqlBuilder, this.__withs);
return this;
}
leftOuterJoin(source) {
this.__finishJoin();
this.__query = '';
if (this.__lastJoin) {
throw new Error('Illegal state');
}
this.__lastJoin = {
__joinType: 'leftOuterJoin',
__tableOrView: source
};
(0, ITableOrView_1.__getTableOrViewPrivate)(source).__addWiths(this.__sqlBuilder, this.__withs);
return this;
}
dynamicOn() {
this.__query = '';
return this;
}
on(condition) {
this.__query = '';
if (!this.__lastJoin) {
throw new Error('Illegal state');
}
this.__lastJoin.__on = (0, values_2.asAlwaysIfValueSource)(condition);
if (!this.__joins) {
this.__joins = [];
}
(0, values_3.__getValueSourcePrivate)(condition).__addWiths(this.__sqlBuilder, this.__withs);
return this;
}
__finishJoin() {
if (this.__lastJoin) {
if (!this.__joins) {
this.__joins = [];
}
this.__joins.push(this.__lastJoin);
this.__lastJoin = undefined;
}
}
customizeQuery(customization) {
this.__finishJoin();
this.__customization = customization;
(0, ITableOrView_1.__addWiths)(customization.beforeQuery, this.__sqlBuilder, this.__withs);
(0, ITableOrView_1.__addWiths)(customization.afterDeleteKeyword, this.__sqlBuilder, this.__withs);
(0, ITableOrView_1.__addWiths)(customization.afterQuery, this.__sqlBuilder, this.__withs);
return this;
}
returning(columns) {
this.__finishJoin();
this.__query = '';
this.__columns = columns;
this.__registerTableOrViewWithOfColumns(columns, this.__withs);
return this;
}
projectingOptionalValuesAsNullable() {
this.__projectOptionalValuesAsNullable = true;
return this;
}
returningOneColumn(column) {
this.__finishJoin();
this.__query = '';
this.__oneColumn = true;
this.__columns = { 'result': column };
(0, values_3.__getValueSourcePrivate)(column).__addWiths(this.__sqlBuilder, this.__withs);
return this;
}
__addWiths(sqlBuilder, withs) {
const withViews = this.__withs;
for (let i = 0, length = withViews.length; i < length; i++) {
const withView = withViews[i];
(0, ITableOrView_1.__getTableOrViewPrivate)(withView).__addWiths(sqlBuilder, withs);
}
}
__registerTableOrView(_sqlBuilder, _requiredTablesOrViews) {
// do nothing because it is not possible to add external dependency
}
__registerRequiredColumn(_sqlBuilder, _requiredColumns, _onlyForTablesOrViews) {
// do nothing because it is not possible to add external dependency
}
__getOldValues(_sqlBuilder) {
// old values fake table is not possible to be used here
return undefined;
}
__getValuesForInsert(_sqlBuilder) {
// values for insert fake table is not possible to be used here
return undefined;
}
__isAllowed(sqlBuilder) {
let result = (0, ITableOrView_1.__getTableOrViewPrivate)(this.__table).__isAllowed(sqlBuilder);
if (!result) {
return false;
}
const using = this.__using;
if (using) {
for (let i = 0, length = using.length; i < length; i++) {
result = (0, ITableOrView_1.__getTableOrViewPrivate)(using[i]).__isAllowed(sqlBuilder);
if (!result) {
return false;
}
}
}
const joins = this.__joins;
if (joins) {
for (let i = 0, length = joins.length; i < length; i++) {
const join = joins[i];
result = (0, ITableOrView_1.__getTableOrViewPrivate)(join.__tableOrView).__isAllowed(sqlBuilder);
if (!result) {
return false;
}
if (join.__on) {
result = (0, values_3.__getValueSourcePrivate)(join.__on).__isAllowed(sqlBuilder);
if (!result) {
return false;
}
}
}
}
if (this.__where) {
result = (0, values_3.__getValueSourcePrivate)(this.__where).__isAllowed(sqlBuilder);
if (!result) {
return false;
}
}
if (this.__columns) {
result = (0, SqlBuilder_1.isAllowedQueryColumns)(this.__columns, sqlBuilder);
if (!result) {
return false;
}
}
if (this.__customization) {
result = (0, ITableOrView_1.__isAllowed)(this.__customization.beforeQuery, sqlBuilder);
if (!result) {
return false;
}
result = (0, ITableOrView_1.__isAllowed)(this.__customization.afterDeleteKeyword, sqlBuilder);
if (!result) {
return false;
}
result = (0, ITableOrView_1.__isAllowed)(this.__customization.afterQuery, sqlBuilder);
if (!result) {
return false;
}
}
return true;
}
}
exports.DeleteQueryBuilder = DeleteQueryBuilder;