mirror of
https://github.com/knex/knex.git
synced 2025-12-27 15:08:47 +00:00
OrWhere({..}) Treat as 'AND', not 'OR'. Fixes #1118
This commit is contained in:
parent
31ae460d9b
commit
942877c0fe
@ -225,8 +225,17 @@ assign(Builder.prototype, {
|
||||
return this;
|
||||
},
|
||||
// Adds an `or where` clause to the query.
|
||||
orWhere: function() {
|
||||
return this._bool('or').where.apply(this, arguments);
|
||||
orWhere: function orWhere() {
|
||||
this._bool('or');
|
||||
var obj = arguments[0];
|
||||
if(_.isObject(obj) && !_.isFunction(obj) && !(obj instanceof Raw)) {
|
||||
return this.whereWrapped(function() {
|
||||
for(let key in obj) {
|
||||
this.andWhere(key, obj[key]);
|
||||
}
|
||||
});
|
||||
}
|
||||
return this.where.apply(this, arguments);
|
||||
},
|
||||
|
||||
// Adds an `not where` clause to the query.
|
||||
|
||||
@ -3169,4 +3169,23 @@ describe("QueryBuilder", function() {
|
||||
});
|
||||
});
|
||||
|
||||
it("#1118 orWhere({..}) generates or (and - and - and)", function() {
|
||||
testsql(qb().select('*').from('users').where('id', '=', 1).orWhere({
|
||||
email: 'foo',
|
||||
id: 2
|
||||
}), {
|
||||
mysql: {
|
||||
sql: 'select * from `users` where `id` = ? or (`email` = ? and `id` = ?)',
|
||||
bindings: [1, 'foo', 2]
|
||||
},
|
||||
mssql: {
|
||||
sql: 'select * from [users] where [id] = ? or ([email] = ? and [id] = ?)',
|
||||
bindings: [1, 'foo', 2]
|
||||
},
|
||||
default: {
|
||||
sql: 'select * from "users" where "id" = ? or ("email" = ? and "id" = ?)',
|
||||
bindings: [1, 'foo', 2]
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user