Skip to content

Commit

Permalink
add lock method to SelectQueryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkdave authored and tom committed Nov 20, 2020
1 parent 6bd2aea commit b59090f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/query-builder/QueryExpressionMap.ts
Expand Up @@ -163,6 +163,12 @@ export class QueryExpressionMap {
*/
withDeleted: boolean = false;

/**
* Locking clause.
*
*/
lockClause?: string;

/**
* Parameters used to be escaped in final query.
*/
Expand Down
22 changes: 15 additions & 7 deletions src/query-builder/SelectQueryBuilder.ts
Expand Up @@ -1019,6 +1019,16 @@ export class SelectQueryBuilder<Entity> extends QueryBuilder<Entity> implements
return this;
}

/**
* Set a lock clause, passed directly into the query.
* e.g. `FOR UPDATE NOWAIT`.
* If set, will override `lockMode`.
*/
lock(clause: string): this {
this.expressionMap.lockClause = clause;
return this;
}

/**
* Gets first raw result returned by execution of generated query builder sql.
*/
Expand Down Expand Up @@ -1055,13 +1065,6 @@ export class SelectQueryBuilder<Entity> extends QueryBuilder<Entity> implements

} 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
Expand Down Expand Up @@ -1690,6 +1693,11 @@ export class SelectQueryBuilder<Entity> extends QueryBuilder<Entity> implements
*/
protected createLockExpression(): string {
const driver = this.connection.driver;

if (this.expressionMap.lockClause) {
return ` ${this.expressionMap.lockClause}`;
}

switch (this.expressionMap.lockMode) {
case "pessimistic_read":
if (driver instanceof MysqlDriver || driver instanceof AuroraDataApiDriver) {
Expand Down

0 comments on commit b59090f

Please sign in to comment.