不要怂,就是干,撸起袖子干!

Commit 590c643c by Sascha Depold

Merge pull request #189 from vlmonk/sqlite-boolean-bug

Sqlite boolean bug
2 parents a2a07c2e 39dfbe3b
...@@ -3,11 +3,13 @@ var Utils = require("../../utils") ...@@ -3,11 +3,13 @@ var Utils = require("../../utils")
var escape = function(str) { var escape = function(str) {
if (typeof str === 'string') { if (typeof str === 'string') {
return "'" + str.replace(/'/g, "''") + "'" return "'" + str.replace(/'/g, "''") + "'";
} else if(str === null || str === undefined) { } else if (typeof str === 'boolean') {
return 'NULL' return str ? 1 : 0; // SQLite has no type boolean
} else if (str === null || str === undefined) {
return 'NULL';
} else { } else {
return str return str;
} }
}; };
...@@ -46,12 +48,6 @@ module.exports = (function() { ...@@ -46,12 +48,6 @@ module.exports = (function() {
table: Utils.addTicks(tableName), table: Utils.addTicks(tableName),
attributes: Utils._.keys(attrValueHash).map(function(attr){return Utils.addTicks(attr)}).join(","), attributes: Utils._.keys(attrValueHash).map(function(attr){return Utils.addTicks(attr)}).join(","),
values: Utils._.values(attrValueHash).map(function(value){ values: Utils._.values(attrValueHash).map(function(value){
//SQLite has no type boolean :
if (typeof value === "boolean") {
value = value ? 1 : 0;
}
return escape((value instanceof Date) ? Utils.toSqlDate(value) : value) return escape((value instanceof Date) ? Utils.toSqlDate(value) : value)
}).join(",") }).join(",")
} }
......
...@@ -22,6 +22,12 @@ describe('QueryGenerator', function() { ...@@ -22,6 +22,12 @@ describe('QueryGenerator', function() {
}, { }, {
arguments: ['myTable', { name: "bar", value: undefined }], arguments: ['myTable', { name: "bar", value: undefined }],
expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('bar',NULL);" expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('bar',NULL);"
}, {
arguments: ['myTable', { name: "foo", value: true }],
expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('foo',1);"
}, {
arguments: ['myTable', { name: "foo", value: false }],
expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('foo',0);"
} }
], ],
...@@ -38,6 +44,12 @@ describe('QueryGenerator', function() { ...@@ -38,6 +44,12 @@ describe('QueryGenerator', function() {
}, { }, {
arguments: ['myTable', { name: 'bar', value: undefined }, { id: 2 }], arguments: ['myTable', { name: 'bar', value: undefined }, { id: 2 }],
expectation: "UPDATE `myTable` SET `name`='bar',`value`=NULL WHERE `id`=2" expectation: "UPDATE `myTable` SET `name`='bar',`value`=NULL WHERE `id`=2"
}, {
arguments: ['myTable', { flag: true }, { id: 2 }],
expectation: "UPDATE `myTable` SET `flag`=1 WHERE `id`=2"
}, {
arguments: ['myTable', { flag: false }, { id: 2 }],
expectation: "UPDATE `myTable` SET `flag`=0 WHERE `id`=2"
} }
] ]
}; };
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!