Skip to content

Commit 063c5f8

Browse files
author
Kyle Farris
committed
Fixed #28
1 parent b567d7d commit 063c5f8

4 files changed

Lines changed: 22 additions & 9 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
TESTS = test/mysql/*.js test/mssql/*.js test/*.js
2-
#TESTS = test/mysql/03-tests-update_batch.js
2+
#TESTS = test/mssql/01-tests-where.js
33
#TESTS = test/05-multiple-drivers.js
44
test:
55
mocha --timeout 5000 --reporter spec $(TESTS)

drivers/query_builder.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ class GenericQueryBuilder {
404404

405405
escape = (typeof escape === 'boolean' ? escape : true);
406406

407-
if (typeof key === 'string' && typeof value === 'object' && Array.isArray(value) && value.length > 0) {
407+
if (typeof key === 'string' && Array.isArray(value) && value.length > 0) {
408408
return this._where_in(key, value, false, 'AND ');
409409
}
410410
return this._where(key, value, 'AND ', escape);
@@ -422,7 +422,7 @@ class GenericQueryBuilder {
422422
_where(key, value=null, type='AND ', escape=true) {
423423
escape = (typeof escape === 'boolean' ? escape : true);
424424

425-
// Must be an object or a string
425+
// If key is not an object....
426426
if (Object.prototype.toString.call(key) !== Object.prototype.toString.call({})) {
427427
// If it's not an object, it must be a string
428428
if (typeof key !== 'string') {
@@ -467,15 +467,18 @@ class GenericQueryBuilder {
467467
key = key_array;
468468
}
469469

470-
if (Object.keys(key).length == 0) {
470+
// Fail if its an empty object
471+
if (Object.keys(key).length === 0) {
471472
throw new Error("where(): You haven't provided any key value pairs to limit the resultset by.");
472473
}
473474

475+
// If an object is supplied...
474476
for (let k in key) {
475477
let v = key[k];
476478

477-
if (typeof v === 'object' && Array.isArray(v) && v.length > 0) {
478-
return this._where_in(k, v, false, type, escape);
479+
if (Array.isArray(v) && v.length > 0) {
480+
this._where_in(k, v, false, type, escape);
481+
continue;
479482
}
480483

481484
const prefix = (this.where_array.length == 0 ? '' : type);
@@ -1300,9 +1303,9 @@ class GenericQueryBuilder {
13001303
this.from(table);
13011304
}
13021305

1303-
if (Object.prototype.toString.call(where) === Object.prototype.toString.call({})) {
1304-
if (Object.keys(where).length == 0) {
1305-
throw new Error("where(): The object you provided to limit the deletion of rows is empty.");
1306+
if (Object.prototype.toString.call(where) === Object.prototype.toString.call({}) && where !== null) {
1307+
if (Object.keys(where).length === 0) {
1308+
throw new Error("where(): The object you provided to limit the deletion of rows is empty. Provide NULL if you need to an empty value.");
13061309
}
13071310
else {
13081311
this.where(where);

test/mssql/01-tests-where.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ describe('MSSQL: where()', () => {
146146
qb.where('galaxy_id >', "(SELECT MIN(id) first_galaxy FROM galaxies WHERE id IN('Milky Way','Andromeda'))");
147147
qb.where_array.should.eql([`[galaxy_id] > '(SELECT MIN(id) first_galaxy FROM galaxies WHERE id IN(''Milky Way'',''Andromeda''))'`]);
148148
});
149+
it('should allow for arrays and non-arrays as values within a where object without dropping anything', () => {
150+
qb.reset_query();
151+
qb.where({planet:'Earth', star_system:'Solar', moons: [1,3,5]});
152+
qb.where_array.should.eql(["[planet] = 'Earth'", "AND [star_system] = 'Solar'", "AND [moons] IN (1, 3, 5)"]);
153+
});
149154
});
150155

151156
describe('MSSQL: or_where()', () => {

test/mysql/01-tests-where.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ describe('MySQL: where()', () => {
146146
qb.where('galaxy_id >', "(SELECT MIN(id) first_galaxy FROM galaxies WHERE id IN('Milky Way','Andromeda'))");
147147
qb.where_array.should.eql(["`galaxy_id` > '(SELECT MIN(id) first_galaxy FROM galaxies WHERE id IN(\\'Milky Way\\',\\'Andromeda\\'))'"]);
148148
});
149+
it('should allow for arrays and non-arrays as values within a where object without dropping anything', () => {
150+
qb.reset_query();
151+
qb.where({planet:'Earth', star_system:'Solar', moons: [1,3,5]});
152+
qb.where_array.should.eql(["`planet` = 'Earth'", "AND `star_system` = 'Solar'", "AND `moons` IN (1, 3, 5)"]);
153+
});
149154
});
150155

151156
describe('MySQL: or_where()', () => {

0 commit comments

Comments
 (0)