Permalink
Browse files

Merge pull request #33 from XadillaX/hotfix/#32

Fix #32
  • Loading branch information...
XadillaX committed Jan 5, 2016
2 parents 8e0995e + 50376fa commit eda60efc386fd05d71b028001d0e88f2e6e4049c
Showing with 26 additions and 13 deletions.
  1. +11 −13 lib/query.js
  2. +15 −0 test/issues.js
@@ -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));

@@ -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)) {
@@ -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.