Skip to content

Commit

Permalink
OrWhere({..}) Treat as 'AND', not 'OR'. Fixes #1118
Browse files Browse the repository at this point in the history
  • Loading branch information
wubzz committed Feb 18, 2016
1 parent 31ae460 commit 942877c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/query/builder.js
Expand Up @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions test/unit/query/builder.js
Expand Up @@ -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]
}
});
});
});

0 comments on commit 942877c

Please sign in to comment.