Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: where with empty object #15

Merged
merged 2 commits into from
Jan 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这一行没有被测试代码覆盖到?

image

}
};

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(), '');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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