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

Commit ea3c1872 by Sascha Depold

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

2 parents 1f2ef885 2cf8d708
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
- [BUG] "order by" is now after "group by". [#585](https://github.com/sequelize/sequelize/pull/585). thanks to mekanics - [BUG] "order by" is now after "group by". [#585](https://github.com/sequelize/sequelize/pull/585). thanks to mekanics
- [BUG] Added decimal support for min/max. [#583](https://github.com/sequelize/sequelize/pull/583). thanks to durango - [BUG] Added decimal support for min/max. [#583](https://github.com/sequelize/sequelize/pull/583). thanks to durango
- [BUG] Null dates don't break SQLite anymore. [#572](https://github.com/sequelize/sequelize/pull/572). thanks to mweibel - [BUG] Null dates don't break SQLite anymore. [#572](https://github.com/sequelize/sequelize/pull/572). thanks to mweibel
- [BUG] Correctly handle booleans in MySQL. [#608](https://github.com/sequelize/sequelize/pull/608). Thanks to terraflubb
- [FEATURE] Schematics. [#564](https://github.com/sequelize/sequelize/pull/564). thanks to durango - [FEATURE] Schematics. [#564](https://github.com/sequelize/sequelize/pull/564). thanks to durango
- [FEATURE] Foreign key constraints. [#595](https://github.com/sequelize/sequelize/pull/595). thanks to optilude - [FEATURE] Foreign key constraints. [#595](https://github.com/sequelize/sequelize/pull/595). thanks to optilude
- [FEATURE] Support for bulk insert (`<DAOFactory>.bulkCreate()`, update (`<DAOFactory>.update()`) and delete (`<DAOFactory>.destroy()`) [#569](https://github.com/sequelize/sequelize/pull/569). thanks to optilude - [FEATURE] Support for bulk insert (`<DAOFactory>.bulkCreate()`, update (`<DAOFactory>.update()`) and delete (`<DAOFactory>.destroy()`) [#569](https://github.com/sequelize/sequelize/pull/569). thanks to optilude
......
...@@ -2,10 +2,20 @@ var Utils = require("../../utils") ...@@ -2,10 +2,20 @@ var Utils = require("../../utils")
, DataTypes = require("../../data-types") , DataTypes = require("../../data-types")
, util = require("util") , util = require("util")
var processAndEscapeValue = function(value) {
var processedValue = value
if (value instanceof Date) {
processedValue = Utils.toSqlDate(value)
} else if (typeof value === 'boolean') {
processedValue = value ? 1 : 0
}
return Utils.escape(processedValue)
}
module.exports = (function() { module.exports = (function() {
var QueryGenerator = { var QueryGenerator = {
addSchema: function(opts) { addSchema: function(opts) {
var tableName = undefined var tableName
var schema = (!!opts && !!opts.options && !!opts.options.schema ? opts.options.schema : undefined) var schema = (!!opts && !!opts.options && !!opts.options.schema ? opts.options.schema : undefined)
var schemaDelimiter = (!!opts && !!opts.options && !!opts.options.schemaDelimiter ? opts.options.schemaDelimiter : undefined) var schemaDelimiter = (!!opts && !!opts.options && !!opts.options.schemaDelimiter ? opts.options.schemaDelimiter : undefined)
...@@ -131,7 +141,7 @@ module.exports = (function() { ...@@ -131,7 +141,7 @@ module.exports = (function() {
var query = "ALTER TABLE `<%= tableName %>` CHANGE <%= attributes %>;" var query = "ALTER TABLE `<%= tableName %>` CHANGE <%= attributes %>;"
var attrString = [] var attrString = []
for (attrName in attributes) { for (var attrName in attributes) {
var definition = attributes[attrName] var definition = attributes[attrName]
attrString.push(Utils._.template('`<%= attrName %>` `<%= attrName %>` <%= definition %>')({ attrString.push(Utils._.template('`<%= attrName %>` `<%= attrName %>` <%= definition %>')({
...@@ -234,9 +244,7 @@ module.exports = (function() { ...@@ -234,9 +244,7 @@ module.exports = (function() {
var table = QueryGenerator.addQuotes(tableName) var table = QueryGenerator.addQuotes(tableName)
var attributes = Object.keys(attrValueHash).map(function(attr){return QueryGenerator.addQuotes(attr)}).join(",") var attributes = Object.keys(attrValueHash).map(function(attr){return QueryGenerator.addQuotes(attr)}).join(",")
var values = Utils._.values(attrValueHash).map(function(value){ var values = Utils._.values(attrValueHash).map(processAndEscapeValue).join(",")
return Utils.escape((value instanceof Date) ? Utils.toSqlDate(value) : value)
}).join(",")
var query = "INSERT INTO " + table + " (" + attributes + ") VALUES (" + values + ");" var query = "INSERT INTO " + table + " (" + attributes + ") VALUES (" + values + ");"
...@@ -248,9 +256,7 @@ module.exports = (function() { ...@@ -248,9 +256,7 @@ module.exports = (function() {
Utils._.forEach(attrValueHashes, function(attrValueHash) { Utils._.forEach(attrValueHashes, function(attrValueHash) {
tuples.push("(" + tuples.push("(" +
Utils._.values(attrValueHash).map(function(value){ Utils._.values(attrValueHash).map(processAndEscapeValue).join(",") +
return Utils.escape((value instanceof Date) ? Utils.toSqlDate(value) : value)
}).join(",") +
")") ")")
}) })
...@@ -269,9 +275,9 @@ module.exports = (function() { ...@@ -269,9 +275,9 @@ module.exports = (function() {
for (var key in attrValueHash) { for (var key in attrValueHash) {
var value = attrValueHash[key] var value = attrValueHash[key]
, _value = (value instanceof Date) ? Utils.toSqlDate(value) : value , _value = processAndEscapeValue(value)
values.push(QueryGenerator.addQuotes(key) + "=" + Utils.escape(_value)) values.push(QueryGenerator.addQuotes(key) + "=" + _value)
} }
var query = "UPDATE " + QueryGenerator.addQuotes(tableName) + var query = "UPDATE " + QueryGenerator.addQuotes(tableName) +
...@@ -285,7 +291,7 @@ module.exports = (function() { ...@@ -285,7 +291,7 @@ module.exports = (function() {
options = options || {} options = options || {}
var table = QueryGenerator.addQuotes(tableName) var table = QueryGenerator.addQuotes(tableName)
var where = QueryGenerator.getWhereConditions(where) where = QueryGenerator.getWhereConditions(where)
var limit = "" var limit = ""
if(Utils._.isUndefined(options.limit)) { if(Utils._.isUndefined(options.limit)) {
...@@ -305,7 +311,7 @@ module.exports = (function() { ...@@ -305,7 +311,7 @@ module.exports = (function() {
options = options || {} options = options || {}
var table = QueryGenerator.addQuotes(tableName) var table = QueryGenerator.addQuotes(tableName)
var where = QueryGenerator.getWhereConditions(where) where = QueryGenerator.getWhereConditions(where)
var query = "DELETE FROM " + table + " WHERE " + where var query = "DELETE FROM " + table + " WHERE " + where
...@@ -319,14 +325,14 @@ module.exports = (function() { ...@@ -319,14 +325,14 @@ module.exports = (function() {
for (var key in attrValueHash) { for (var key in attrValueHash) {
var value = attrValueHash[key] var value = attrValueHash[key]
, _value = (value instanceof Date) ? Utils.toSqlDate(value) : value , _value = processAndEscapeValue(value)
values.push(QueryGenerator.addQuotes(key) + "=" + QueryGenerator.addQuotes(key) + " + " +Utils.escape(_value)) values.push(QueryGenerator.addQuotes(key) + "=" + QueryGenerator.addQuotes(key) + " + " + _value)
} }
var table = QueryGenerator.addQuotes(tableName) var table = QueryGenerator.addQuotes(tableName)
var values = values.join(",") values = values.join(",")
var where = QueryGenerator.getWhereConditions(where) where = QueryGenerator.getWhereConditions(where)
var query = "UPDATE " + table + " SET " + values + " WHERE " + where var query = "UPDATE " + table + " SET " + values + " WHERE " + where
...@@ -425,20 +431,18 @@ module.exports = (function() { ...@@ -425,20 +431,18 @@ module.exports = (function() {
if (Array.isArray(value)) { if (Array.isArray(value)) {
// is value an array? // is value an array?
if (value.length == 0) { value = [null] } if (value.length === 0) { value = [null] }
_value = "(" + value.map(function(subValue) { _value = "(" + value.map(processAndEscapeValue).join(',') + ")"
return Utils.escape(subValue);
}).join(',') + ")"
result.push([_key, _value].join(" IN ")) result.push([_key, _value].join(" IN "))
} else if ((value) && (typeof value == 'object')) { } else if ((value) && (typeof value == 'object') && !(value instanceof Date)) {
// is value an object? // is value an object?
//using as sentinel for join column => value //using as sentinel for join column => value
_value = value.join.split('.').map(function(col){ return QueryGenerator.addQuotes(col) }).join(".") _value = value.join.split('.').map(function(col){ return QueryGenerator.addQuotes(col) }).join(".")
result.push([_key, _value].join("=")) result.push([_key, _value].join("="))
} else { } else {
_value = Utils.escape(value) _value = processAndEscapeValue(value)
result.push((_value == 'NULL') ? _key + " IS NULL" : [_key, _value].join("=")) result.push((_value == 'NULL') ? _key + " IS NULL" : [_key, _value].join("="))
} }
} }
...@@ -475,7 +479,7 @@ module.exports = (function() { ...@@ -475,7 +479,7 @@ module.exports = (function() {
template += " auto_increment" template += " auto_increment"
} }
if ((dataType.defaultValue != undefined) && (dataType.defaultValue != DataTypes.NOW)) { if ((dataType.defaultValue !== undefined) && (dataType.defaultValue != DataTypes.NOW)) {
template += " DEFAULT " + Utils.escape(dataType.defaultValue) template += " DEFAULT " + Utils.escape(dataType.defaultValue)
} }
......
...@@ -197,6 +197,12 @@ describe('QueryGenerator', function() { ...@@ -197,6 +197,12 @@ describe('QueryGenerator', function() {
arguments: ['myTable', {name: 'foo', foo: 1, nullValue: undefined}], arguments: ['myTable', {name: 'foo', foo: 1, nullValue: undefined}],
expectation: "INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1);", expectation: "INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1);",
context: {options: {omitNull: true}} context: {options: {omitNull: true}}
}, {
arguments: ['myTable', {foo: false}],
expectation: "INSERT INTO `myTable` (`foo`) VALUES (0);"
}, {
arguments: ['myTable', {foo: true}],
expectation: "INSERT INTO `myTable` (`foo`) VALUES (1);"
} }
], ],
...@@ -228,6 +234,9 @@ describe('QueryGenerator', function() { ...@@ -228,6 +234,9 @@ describe('QueryGenerator', function() {
arguments: ['myTable', [{name: 'foo', foo: 1, nullValue: undefined}, {name: 'bar', foo: 2, undefinedValue: undefined}]], arguments: ['myTable', [{name: 'foo', foo: 1, nullValue: undefined}, {name: 'bar', foo: 2, undefinedValue: undefined}]],
expectation: "INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL),('bar',2,NULL);", expectation: "INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL),('bar',2,NULL);",
context: {options: {omitNull: true}} // Note: As above context: {options: {omitNull: true}} // Note: As above
}, {
arguments: ['myTable', [{name: "foo", value: true}, {name: 'bar', value: false}]],
expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('foo',1),('bar',0);"
} }
], ],
...@@ -255,6 +264,12 @@ describe('QueryGenerator', function() { ...@@ -255,6 +264,12 @@ describe('QueryGenerator', function() {
arguments: ['myTable', {bar: 2, nullValue: null}, {name: 'foo'}], arguments: ['myTable', {bar: 2, nullValue: null}, {name: 'foo'}],
expectation: "UPDATE `myTable` SET `bar`=2 WHERE `name`='foo'", expectation: "UPDATE `myTable` SET `bar`=2 WHERE `name`='foo'",
context: {options: {omitNull: true}} context: {options: {omitNull: true}}
}, {
arguments: ['myTable', {bar: false}, {name: 'foo'}],
expectation: "UPDATE `myTable` SET `bar`=0 WHERE `name`='foo'"
}, {
arguments: ['myTable', {bar: true}, {name: 'foo'}],
expectation: "UPDATE `myTable` SET `bar`=1 WHERE `name`='foo'"
} }
], ],
...@@ -325,6 +340,27 @@ describe('QueryGenerator', function() { ...@@ -325,6 +340,27 @@ describe('QueryGenerator', function() {
{ {
arguments: [{ id: [] }], arguments: [{ id: [] }],
expectation: "`id` IN (NULL)" expectation: "`id` IN (NULL)"
},
{
arguments: [{ maple: false, bacon: true }],
expectation: "`maple`=0 AND `bacon`=1"
},
{
arguments: [{ beaver: [false, true] }],
expectation: "`beaver` IN (0,1)"
},
{
arguments: [{birthday: new Date(Date.UTC(2011, 6, 1, 10, 1, 55))}],
expectation: "`birthday`='2011-07-01 10:01:55'"
},
{
arguments: [{ birthday: new Date(Date.UTC(2011, 6, 1, 10, 1, 55)),
otherday: new Date(Date.UTC(2013, 6, 2, 10, 1, 22)) }],
expectation: "`birthday`='2011-07-01 10:01:55' AND `otherday`='2013-07-02 10:01:22'"
},
{
arguments: [{ birthday: [new Date(Date.UTC(2011, 6, 1, 10, 1, 55)), new Date(Date.UTC(2013, 6, 2, 10, 1, 22))] }],
expectation: "`birthday` IN ('2011-07-01 10:01:55','2013-07-02 10:01:22')"
} }
] ]
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!