Skip to content

Commit

Permalink
fix: where with empty object (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
csbun authored and fengmk2 committed Jan 20, 2017
1 parent 74bce55 commit db0b90e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/operator.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ proto._where = function (where) {
return '';
}

let wheres = [];
let values = [];
const wheres = [];
const values = [];
for (let key in where) {
let value = where[key];
const value = where[key];
if (Array.isArray(value)) {
wheres.push('?? IN (?)');
} else {
Expand All @@ -197,7 +197,11 @@ proto._where = function (where) {
values.push(key);
values.push(value);
}
return this.format(' WHERE ' + wheres.join(' AND '), values);
if (wheres.length > 0) {
return this.format(' WHERE ' + wheres.join(' AND '), values);
} else {
return '';
}
};

proto._selectColumns = function (table, columns) {
Expand Down
2 changes: 2 additions & 0 deletions test/operator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ describe('operator.test.js', function () {
describe('_where(where)', function () {
it('should get where sql', function () {
let op = new Operator();
assert.equal(op._where(), '');
assert.equal(op._where({}), '');
assert.equal(op._where({ id: 1 }), ' WHERE `id` = 1');
assert.equal(op._where({ id: 1, name: 'foo' }), ' WHERE `id` = 1 AND `name` = \'foo\'');
assert.equal(op._where({ id: 1, name2: null }), ' WHERE `id` = 1 AND `name2` = NULL');
Expand Down

0 comments on commit db0b90e

Please sign in to comment.