Skip to content

Commit

Permalink
Merge pull request #33 from XadillaX/hotfix/#32
Browse files Browse the repository at this point in the history
Fix #32
  • Loading branch information
XadillaX committed Jan 5, 2016
2 parents 8e0995e + 50376fa commit eda60ef
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
24 changes: 11 additions & 13 deletions lib/query.js
Expand Up @@ -153,8 +153,18 @@ Query.prototype._makeConditionField = function(condition, field, type) {
value;
};
var cdtMapper = function(symbol, value) {
value = field.type.restore(value);
// add null support
if(value === null) {
if(symbol === "=") {
conditionStrings.push("`" + field.column + "` IS NULL");
return;
} else if(symbol === "!=") {
conditionStrings.push("`" + field.column + "` IS NOT NULL");
return;
}
}

value = field.type.restore(value);
var cdt = util.format("`%s` %s %s", field.column, symbol,
(field.type.needQuotes ? "\"" + escaper.escape(value) + "\"" : value));

Expand Down Expand Up @@ -230,18 +240,6 @@ Query.prototype._makeConditionField = function(condition, field, type) {
continue;
}

// null, not null...
if(value === null) {
if(symbol === "=") {
conditionStrings.push("`" + field.column + "` IS NULL");
continue;
}
if(symbol === "!=") {
conditionStrings.push("`" + field.column + "` IS NOT NULL");
continue;
}
}

// array supported:
// eg. { field: { $neq: [ "123", "456" ] } }
if(!util.isArray(value)) {
Expand Down
15 changes: 15 additions & 0 deletions test/issues.js
Expand Up @@ -81,4 +81,19 @@ describe("issues", function () {
});
});
});

describe("generate", function() {
it("should fix #32, 逻辑运算 AND 或者 OR 的时候有 NULL 时发生的 Bug", function(done) {
var sql = Model.where({ key1: { $neq: [ 0, null ] } }).makeSQL("find");
sql.should.be.eql("SELECT `id`, `key2`, `key3`, `key4`, `index` FROM `test` WHERE ((`id` != 0 AND `id` IS NOT NULL))");

sql = Model.where({ key1: null }).makeSQL("find");
sql.should.be.eql("SELECT `id`, `key2`, `key3`, `key4`, `index` FROM `test` WHERE (`id` IS NULL)");

sql = Model.where({ key1: { $neq: null } }).makeSQL("find");
sql.should.be.eql("SELECT `id`, `key2`, `key3`, `key4`, `index` FROM `test` WHERE ((`id` IS NOT NULL))");

done();
});
});
});

0 comments on commit eda60ef

Please sign in to comment.