diff --git a/src/query/builder.js b/src/query/builder.js index e75604367..bc35d100d 100644 --- a/src/query/builder.js +++ b/src/query/builder.js @@ -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. diff --git a/test/unit/query/builder.js b/test/unit/query/builder.js index 4c8213b63..7555eb0f7 100644 --- a/test/unit/query/builder.js +++ b/test/unit/query/builder.js @@ -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] + } + }); + }); });