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

Commit f5d7c2f7 by sdepold

Merge branch 'master' of github.com:sdepold/sequelize

2 parents 76639026 590c643c
...@@ -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) { } 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(",")
} }
......
...@@ -16,6 +16,18 @@ describe('QueryGenerator', function() { ...@@ -16,6 +16,18 @@ describe('QueryGenerator', function() {
}, { }, {
arguments: ['myTable', { name: "'bar'" }], arguments: ['myTable', { name: "'bar'" }],
expectation: "INSERT INTO `myTable` (`name`) VALUES ('''bar''');" expectation: "INSERT INTO `myTable` (`name`) VALUES ('''bar''');"
}, {
arguments: ['myTable', { name: "bar", value: null }],
expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('bar',NULL);"
}, {
arguments: ['myTable', { name: "bar", value: undefined }],
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);"
} }
], ],
...@@ -26,6 +38,18 @@ describe('QueryGenerator', function() { ...@@ -26,6 +38,18 @@ describe('QueryGenerator', function() {
}, { }, {
arguments: ['myTable', { name: "'bar'" }, { id: 2 }], arguments: ['myTable', { name: "'bar'" }, { id: 2 }],
expectation: "UPDATE `myTable` SET `name`='''bar''' WHERE `id`=2" expectation: "UPDATE `myTable` SET `name`='''bar''' WHERE `id`=2"
}, {
arguments: ['myTable', { name: 'bar', value: null }, { id: 2 }],
expectation: "UPDATE `myTable` SET `name`='bar',`value`=NULL WHERE `id`=2"
}, {
arguments: ['myTable', { name: 'bar', value: undefined }, { 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!