Skip to content

Commit

Permalink
ForUpdate array parameter (#4882)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierCavadenti committed Dec 7, 2021
1 parent e4f4446 commit de1122a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/query/querybuilder.js
Expand Up @@ -1233,7 +1233,11 @@ class Builder extends EventEmitter {
// Set a lock for update constraint.
forUpdate(...tables) {
this._single.lock = lockMode.forUpdate;
this._single.lockTables = tables;
if (tables.length === 1 && Array.isArray(tables[0])) {
this._single.lockTables = tables[0];
} else {
this._single.lockTables = tables;
}
return this;
}

Expand Down
28 changes: 28 additions & 0 deletions test/unit/query/builder.js
Expand Up @@ -6717,6 +6717,34 @@ describe('QueryBuilder', () => {
);
});

it('lock only some tables for update (with array #4878)', () => {
testsql(
qb()
.select('*')
.from('foo')
.where('bar', '=', 'baz')
.forUpdate(['lo', 'rem']),
{
mysql: {
sql: 'select * from `foo` where `bar` = ? for update',
bindings: ['baz'],
},
pg: {
sql: 'select * from "foo" where "bar" = ? for update of "lo", "rem"',
bindings: ['baz'],
},
mssql: {
sql: 'select * from [foo] with (UPDLOCK) where [bar] = ?',
bindings: ['baz'],
},
oracledb: {
sql: 'select * from "foo" where "bar" = ? for update',
bindings: ['baz'],
},
}
);
});

it('lock for update with skip locked #1937', () => {
testsql(qb().select('*').from('foo').first().forUpdate().skipLocked(), {
mysql: {
Expand Down

0 comments on commit de1122a

Please sign in to comment.