OrWhere({..}) Treat as 'AND', not 'OR'. Fixes #1118

This commit is contained in:
wubzz 2016-01-28 22:25:05 +01:00
parent 31ae460d9b
commit 942877c0fe
2 changed files with 30 additions and 2 deletions

View File

@ -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.

View File

@ -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]
}
});
});
});