UNPKG

typeorm

Version:

Data-Mapper ORM for TypeScript, ES7, ES6, ES5. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, WebSQL, MongoDB databases.

151 lines (149 loc) 6.88 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var QueryBuilder_1 = require("./QueryBuilder"); var SqlServerDriver_1 = require("../driver/sqlserver/SqlServerDriver"); var PostgresDriver_1 = require("../driver/postgres/PostgresDriver"); /** * Allows to build complex sql queries in a fashion way and execute those queries. */ var DeleteQueryBuilder = /** @class */ (function (_super) { __extends(DeleteQueryBuilder, _super); // ------------------------------------------------------------------------- // Constructor // ------------------------------------------------------------------------- function DeleteQueryBuilder(connectionOrQueryBuilder, queryRunner) { var _this = _super.call(this, connectionOrQueryBuilder, queryRunner) || this; _this.expressionMap.aliasNamePrefixingEnabled = false; return _this; } // ------------------------------------------------------------------------- // Public Implemented Methods // ------------------------------------------------------------------------- /** * Gets generated sql query without parameters being replaced. */ DeleteQueryBuilder.prototype.getQuery = function () { var sql = this.createDeleteExpression(); return sql.trim(); }; // ------------------------------------------------------------------------- // Public Methods // ------------------------------------------------------------------------- /** * Specifies FROM which entity's table select/update/delete will be executed. * Also sets a main string alias of the selection data. */ DeleteQueryBuilder.prototype.from = function (entityTarget, aliasName) { var mainAlias = this.createFromAlias(entityTarget, aliasName); this.expressionMap.setMainAlias(mainAlias); return this; }; /** * Sets WHERE condition in the query builder. * If you had previously WHERE expression defined, * calling this function will override previously set WHERE conditions. * Additionally you can add parameters used in where expression. */ DeleteQueryBuilder.prototype.where = function (where, parameters) { this.expressionMap.wheres = []; // don't move this block below since computeWhereParameter can add where expressions var condition = this.computeWhereParameter(where); if (condition) this.expressionMap.wheres = [{ type: "simple", condition: condition }]; if (parameters) this.setParameters(parameters); return this; }; /** * Adds new AND WHERE condition in the query builder. * Additionally you can add parameters used in where expression. */ DeleteQueryBuilder.prototype.andWhere = function (where, parameters) { this.expressionMap.wheres.push({ type: "and", condition: this.computeWhereParameter(where) }); if (parameters) this.setParameters(parameters); return this; }; /** * Adds new OR WHERE condition in the query builder. * Additionally you can add parameters used in where expression. */ DeleteQueryBuilder.prototype.orWhere = function (where, parameters) { this.expressionMap.wheres.push({ type: "or", condition: this.computeWhereParameter(where) }); if (parameters) this.setParameters(parameters); return this; }; /** * Adds new AND WHERE with conditions for the given ids. */ DeleteQueryBuilder.prototype.whereInIds = function (ids) { ids = ids instanceof Array ? ids : [ids]; var _a = this.createWhereIdsExpression(ids), whereExpression = _a[0], parameters = _a[1]; this.where(whereExpression, parameters); return this; }; /** * Adds new AND WHERE with conditions for the given ids. */ DeleteQueryBuilder.prototype.andWhereInIds = function (ids) { var _a = this.createWhereIdsExpression(ids), whereExpression = _a[0], parameters = _a[1]; this.andWhere(whereExpression, parameters); return this; }; /** * Adds new OR WHERE with conditions for the given ids. */ DeleteQueryBuilder.prototype.orWhereInIds = function (ids) { var _a = this.createWhereIdsExpression(ids), whereExpression = _a[0], parameters = _a[1]; this.orWhere(whereExpression, parameters); return this; }; /** * Optional returning/output clause. */ DeleteQueryBuilder.prototype.returning = function (returning) { if (this.connection.driver instanceof SqlServerDriver_1.SqlServerDriver || this.connection.driver instanceof PostgresDriver_1.PostgresDriver) { this.expressionMap.returning = returning; return this; } else throw new Error("OUTPUT or RETURNING clause only supported by MS SQLServer or PostgreSQL"); }; /** * Optional returning/output clause. */ DeleteQueryBuilder.prototype.output = function (output) { return this.returning(output); }; // ------------------------------------------------------------------------- // Protected Methods // ------------------------------------------------------------------------- /** * Creates DELETE express used to perform query. */ DeleteQueryBuilder.prototype.createDeleteExpression = function () { var whereExpression = this.createWhereExpression(); if (this.expressionMap.returning !== "" && this.connection.driver instanceof PostgresDriver_1.PostgresDriver) { return "DELETE FROM " + this.getTableName(this.getMainTableName()) + whereExpression + " RETURNING " + this.expressionMap.returning; } else if (this.expressionMap.returning !== "" && this.connection.driver instanceof SqlServerDriver_1.SqlServerDriver) { return "DELETE FROM " + this.getTableName(this.getMainTableName()) + " OUTPUT " + this.expressionMap.returning + whereExpression; } else { return "DELETE FROM " + this.getTableName(this.getMainTableName()) + whereExpression; // todo: how do we replace aliases in where to nothing? } }; return DeleteQueryBuilder; }(QueryBuilder_1.QueryBuilder)); exports.DeleteQueryBuilder = DeleteQueryBuilder; //# sourceMappingURL=DeleteQueryBuilder.js.map