-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
/
DeleteQueryBuilder.ts
281 lines (236 loc) · 11 KB
/
DeleteQueryBuilder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import {CockroachDriver} from "../driver/cockroachdb/CockroachDriver";
import {OracleDriver} from "../driver/oracle/OracleDriver";
import {QueryBuilder} from "./QueryBuilder";
import {ObjectLiteral} from "../common/ObjectLiteral";
import {EntityTarget} from "../common/EntityTarget";
import {Connection} from "../connection/Connection";
import {QueryRunner} from "../query-runner/QueryRunner";
import {SqlServerDriver} from "../driver/sqlserver/SqlServerDriver";
import {PostgresDriver} from "../driver/postgres/PostgresDriver";
import {WhereExpression} from "./WhereExpression";
import {Brackets} from "./Brackets";
import {DeleteResult} from "./result/DeleteResult";
import {ReturningStatementNotSupportedError} from "../error/ReturningStatementNotSupportedError";
import {SqljsDriver} from "../driver/sqljs/SqljsDriver";
import {MysqlDriver} from "../driver/mysql/MysqlDriver";
import {BroadcasterResult} from "../subscriber/BroadcasterResult";
import {EntitySchema} from "../entity-schema/EntitySchema";
import {AuroraDataApiDriver} from "../driver/aurora-data-api/AuroraDataApiDriver";
import {BetterSqlite3Driver} from "../driver/better-sqlite3/BetterSqlite3Driver";
/**
* Allows to build complex sql queries in a fashion way and execute those queries.
*/
export class DeleteQueryBuilder<Entity> extends QueryBuilder<Entity> implements WhereExpression {
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
constructor(connectionOrQueryBuilder: Connection|QueryBuilder<any>, queryRunner?: QueryRunner) {
super(connectionOrQueryBuilder as any, queryRunner);
this.expressionMap.aliasNamePrefixingEnabled = false;
}
// -------------------------------------------------------------------------
// Public Implemented Methods
// -------------------------------------------------------------------------
/**
* Gets generated sql query without parameters being replaced.
*/
getQuery(): string {
let sql = this.createComment();
sql += this.createDeleteExpression();
return sql.trim();
}
/**
* Executes sql generated by query builder and returns raw database results.
*/
async execute(): Promise<DeleteResult> {
const [sql, parameters] = this.getQueryAndParameters();
const queryRunner = this.obtainQueryRunner();
let transactionStartedByUs: boolean = false;
try {
// start transaction if it was enabled
if (this.expressionMap.useTransaction === true && queryRunner.isTransactionActive === false) {
await queryRunner.startTransaction();
transactionStartedByUs = true;
}
// call before deletion methods in listeners and subscribers
if (this.expressionMap.callListeners === true && this.expressionMap.mainAlias!.hasMetadata) {
const broadcastResult = new BroadcasterResult();
queryRunner.broadcaster.broadcastBeforeRemoveEvent(broadcastResult, this.expressionMap.mainAlias!.metadata);
if (broadcastResult.promises.length > 0) await Promise.all(broadcastResult.promises);
}
// execute query
const deleteResult = new DeleteResult();
const result = await queryRunner.query(sql, parameters);
const driver = queryRunner.connection.driver;
if (driver instanceof MysqlDriver) {
deleteResult.raw = result;
deleteResult.affected = result.affectedRows;
} else if (driver instanceof AuroraDataApiDriver) {
deleteResult.raw = result;
deleteResult.affected = result.numberOfRecordsUpdated;
} else if (driver instanceof SqlServerDriver || driver instanceof PostgresDriver || driver instanceof CockroachDriver) {
deleteResult.raw = result[0] ? result[0] : null;
// don't return 0 because it could confuse. null means that we did not receive this value
deleteResult.affected = typeof result[1] === "number" ? result[1] : null;
} else if (driver instanceof OracleDriver) {
deleteResult.affected = result;
} else if (driver instanceof BetterSqlite3Driver) { // only works for better-sqlite3
deleteResult.raw = result;
deleteResult.affected = result.changes;
} else {
deleteResult.raw = result;
}
// call after deletion methods in listeners and subscribers
if (this.expressionMap.callListeners === true && this.expressionMap.mainAlias!.hasMetadata) {
const broadcastResult = new BroadcasterResult();
queryRunner.broadcaster.broadcastAfterRemoveEvent(broadcastResult, this.expressionMap.mainAlias!.metadata);
if (broadcastResult.promises.length > 0) await Promise.all(broadcastResult.promises);
}
// close transaction if we started it
if (transactionStartedByUs)
await queryRunner.commitTransaction();
return deleteResult;
} catch (error) {
// rollback transaction if we started it
if (transactionStartedByUs) {
try {
await queryRunner.rollbackTransaction();
} catch (rollbackError) { }
}
throw error;
} finally {
if (queryRunner !== this.queryRunner) { // means we created our own query runner
await queryRunner.release();
}
if (this.connection.driver instanceof SqljsDriver && !queryRunner.isTransactionActive) {
await this.connection.driver.autoSave();
}
}
}
// -------------------------------------------------------------------------
// Public Methods
// -------------------------------------------------------------------------
/**
* Specifies FROM which entity's table select/update/delete will be executed.
* Also sets a main string alias of the selection data.
*/
from<T>(entityTarget: EntityTarget<T>, aliasName?: string): DeleteQueryBuilder<T> {
entityTarget = entityTarget instanceof EntitySchema ? entityTarget.options.name : entityTarget;
const mainAlias = this.createFromAlias(entityTarget, aliasName);
this.expressionMap.setMainAlias(mainAlias);
return (this as any) as DeleteQueryBuilder<T>;
}
/**
* 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.
*/
where(where: Brackets|string|((qb: this) => string)|ObjectLiteral|ObjectLiteral[], parameters?: ObjectLiteral): this {
this.expressionMap.wheres = []; // don't move this block below since computeWhereParameter can add where expressions
const 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.
*/
andWhere(where: Brackets|string|((qb: this) => string)|ObjectLiteral|ObjectLiteral[], parameters?: ObjectLiteral): this {
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.
*/
orWhere(where: Brackets|string|((qb: this) => string)|ObjectLiteral|ObjectLiteral[], parameters?: ObjectLiteral): this {
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.
*/
whereInIds(ids: any|any[]): this {
return this.where(this.getWhereInIdsCondition(ids));
}
/**
* Adds new AND WHERE with conditions for the given ids.
*/
andWhereInIds(ids: any|any[]): this {
return this.andWhere(this.getWhereInIdsCondition(ids));
}
/**
* Adds new OR WHERE with conditions for the given ids.
*/
orWhereInIds(ids: any|any[]): this {
return this.orWhere(this.getWhereInIdsCondition(ids));
}
/**
* Optional returning/output clause.
* This will return given column values.
*/
output(columns: string[]): this;
/**
* Optional returning/output clause.
* Returning is a SQL string containing returning statement.
*/
output(output: string): this;
/**
* Optional returning/output clause.
*/
output(output: string|string[]): this;
/**
* Optional returning/output clause.
*/
output(output: string|string[]): this {
return this.returning(output);
}
/**
* Optional returning/output clause.
* This will return given column values.
*/
returning(columns: string[]): this;
/**
* Optional returning/output clause.
* Returning is a SQL string containing returning statement.
*/
returning(returning: string): this;
/**
* Optional returning/output clause.
*/
returning(returning: string|string[]): this;
/**
* Optional returning/output clause.
*/
returning(returning: string|string[]): this {
// not all databases support returning/output cause
if (!this.connection.driver.isReturningSqlSupported())
throw new ReturningStatementNotSupportedError();
this.expressionMap.returning = returning;
return this;
}
// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------
/**
* Creates DELETE express used to perform query.
*/
protected createDeleteExpression() {
const tableName = this.getTableName(this.getMainTableName());
const whereExpression = this.createWhereExpression();
const returningExpression = this.createReturningExpression();
if (returningExpression && (this.connection.driver instanceof PostgresDriver || this.connection.driver instanceof CockroachDriver)) {
return `DELETE FROM ${tableName}${whereExpression} RETURNING ${returningExpression}`;
} else if (returningExpression !== "" && this.connection.driver instanceof SqlServerDriver) {
return `DELETE FROM ${tableName} OUTPUT ${returningExpression}${whereExpression}`;
} else {
return `DELETE FROM ${tableName}${whereExpression}`;
}
}
}