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

Commit 33bdb8f5 by sdepold

merge

2 parents c8066605 ac3acf58
......@@ -18,7 +18,7 @@ module.exports = (function() {
// clear the old associations
oldAssociations.forEach(function(associatedObject) {
associatedObject[self.__factory.identifier] = null
associatedObject[self.__factory.identifier] = self.options.omitNull ? '' : null
associatedObject.save()
})
......
......@@ -53,7 +53,7 @@ module.exports = (function() {
var customEventEmitter = new Utils.CustomEventEmitter(function() {
obj[self.accessors.get]().success(function(oldObj) {
if(oldObj) {
oldObj[self.identifier] = null
oldObj[self.identifier] = self.options.omitNull ? '' : null;
oldObj.save()
}
......
......@@ -129,6 +129,16 @@ module.exports = (function() {
insertQuery: function(tableName, attrValueHash) {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>);"
if(this.options.omitNull) {
_attrValueHash = {}
Utils._.each(attrValueHash, function(val, key) {
if(val !== null && val !== undefined) {
_attrValueHash[key] = val;
}
})
attrValueHash = _attrValueHash
}
var replacements = {
table: Utils.addTicks(tableName),
attributes: Utils._.keys(attrValueHash).map(function(attr){return Utils.addTicks(attr)}).join(","),
......@@ -142,6 +152,17 @@ module.exports = (function() {
updateQuery: function(tableName, values, where) {
var query = "UPDATE <%= table %> SET <%= values %> WHERE <%= where %>"
if(this.options.omitNull) {
_attrValueHash = {}
Utils._.each(values, function(val, key) {
if(val !== null && val !== undefined) {
_attrValueHash[key] = val;
}
})
values = _attrValueHash
}
var replacements = {
table: Utils.addTicks(tableName),
values: Utils._.map(values, function(value, key){
......
......@@ -53,6 +53,8 @@ function pgDataTypeMapping(tableName, attr, dataType) {
module.exports = (function() {
var QueryGenerator = {
options: {},
createTableQuery: function(tableName, attributes, options) {
options = Utils._.extend({
}, options || {})
......@@ -191,7 +193,18 @@ module.exports = (function() {
insertQuery: function(tableName, attrValueHash) {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>) RETURNING *;"
if(this.options.omitNull) {
_attrValueHash = {}
Utils._.each(attrValueHash, function(val, key) {
if(val !== null && val !== undefined) {
_attrValueHash[key] = val;
}
})
attrValueHash = _attrValueHash
}
var returning = []
Utils._.forEach(attrValueHash, function(value, key, hash) {
if (tables[tableName] && tables[tableName][key]) {
switch (tables[tableName][key]) {
......@@ -216,6 +229,17 @@ module.exports = (function() {
updateQuery: function(tableName, values, where) {
var query = "UPDATE <%= table %> SET <%= values %> WHERE <%= where %>"
if(this.options.omitNull) {
_attrValueHash = {}
Utils._.each(values, function(val, key) {
if(val !== null && val !== undefined) {
_attrValueHash[key] = val;
}
})
values = _attrValueHash
}
var replacements = {
table: addQuotes(tableName),
values: Utils._.map(values, function(value, key){
......
......@@ -15,6 +15,8 @@ var escape = function(str) {
module.exports = (function() {
var QueryGenerator = {
options: {},
createTableQuery: function(tableName, attributes, options) {
options = options || {}
......@@ -44,6 +46,16 @@ module.exports = (function() {
insertQuery: function(tableName, attrValueHash) {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>);";
if(this.options.omitNull) {
_attrValueHash = {}
Utils._.each(attrValueHash, function(val, key) {
if(val !== null && val !== undefined) {
_attrValueHash[key] = val;
}
})
attrValueHash = _attrValueHash
}
var replacements = {
table: Utils.addTicks(tableName),
attributes: Utils._.keys(attrValueHash).map(function(attr){return Utils.addTicks(attr)}).join(","),
......@@ -57,6 +69,16 @@ module.exports = (function() {
updateQuery: function(tableName, values, where) {
var query = "UPDATE <%= table %> SET <%= values %> WHERE <%= where %>"
if(this.options.omitNull) {
_attrValueHash = {}
Utils._.each(values, function(val, key) {
if(val !== null && val !== undefined) {
_attrValueHash[key] = val;
}
})
values = _attrValueHash
}
var replacements = {
table: Utils.addTicks(tableName),
values: Utils._.map(values, function(value, key){
......
......@@ -5,6 +5,7 @@ module.exports = (function() {
var QueryInterface = function(sequelize) {
this.sequelize = sequelize
this.QueryGenerator = require('./dialects/' + this.sequelize.options.dialect + '/query-generator')
this.QueryGenerator.options = this.sequelize.options;
}
Utils.addEventEmitter(QueryInterface)
......
......@@ -19,7 +19,8 @@ module.exports = (function() {
define: {},
query: {},
sync: {},
logging: console.log
logging: console.log,
omitNull: false
}, options || {})
if(this.options.logging === true) {
......@@ -49,7 +50,7 @@ module.exports = (function() {
Sequelize.Utils._.map(DataTypes, function(sql, accessor) { Sequelize[accessor] = sql})
Sequelize.prototype.getQueryInterface = function() {
this.queryInterface = this.queryInterface || new QueryInterface(this)
this.queryInterface = this.queryInterface || new QueryInterface(this)
return this.queryInterface
}
......@@ -67,6 +68,7 @@ module.exports = (function() {
if(this.options.define)
options = Sequelize.Utils.merge(options, this.options.define)
options.omitNull = this.options.omitNull
var factory = new DAOFactory(daoName, attributes, options)
......
......@@ -97,6 +97,37 @@ var Utils = module.exports = {
toDefaultValue: function(value) {
return (value == DataTypes.NOW) ? new Date() : value
},
setAttributes: function(hash, identifier, instance, prefix) {
prefix = prefix || ''
if (this.isHash(identifier)) {
this._.each(identifier, function(elem, key) {
hash[prefix + key] = Utils._.isString(instance) ? instance : Utils._.isObject(instance) ? instance[elem.key || elem] : null
})
} else {
hash[prefix + identifier] = Utils._.isString(instance) ? instance : Utils._.isObject(instance) ? instance.id : null
}
return hash
},
removeNullValuesFromHash: function(hash, omitNull) {
var result = hash
if(omitNull) {
var _hash = {}
Utils._.each(hash, function(val, key) {
if(val !== null && val !== undefined) {
_hash[key] = val;
}
})
result = _hash
}
return result
}
}
......
......@@ -89,6 +89,21 @@ describe('QueryGenerator', function() {
}, {
arguments: ['myTable', {name: 'foo', foo: 1}],
expectation: "INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1);"
}, {
arguments: ['myTable', {name: 'foo', foo: 1, nullValue: null}],
expectation: "INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL);"
}, {
arguments: ['myTable', {name: 'foo', foo: 1, nullValue: null}],
expectation: "INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL);",
context: {options: {omitNull: false}}
}, {
arguments: ['myTable', {name: 'foo', foo: 1, nullValue: null}],
expectation: "INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1);",
context: {options: {omitNull: true}}
}, {
arguments: ['myTable', {name: 'foo', foo: 1, nullValue: undefined}],
expectation: "INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1);",
context: {options: {omitNull: true}}
}
],
......@@ -105,6 +120,17 @@ describe('QueryGenerator', function() {
}, {
arguments: ['myTable', {name: "foo';DROP TABLE myTable;"}, {name: 'foo'}],
expectation: "UPDATE `myTable` SET `name`='foo\\';DROP TABLE myTable;' WHERE `name`='foo'"
}, {
arguments: ['myTable', {bar: 2, nullValue: null}, {name: 'foo'}],
expectation: "UPDATE `myTable` SET `bar`=2,`nullValue`=NULL WHERE `name`='foo'"
}, {
arguments: ['myTable', {bar: 2, nullValue: null}, {name: 'foo'}],
expectation: "UPDATE `myTable` SET `bar`=2,`nullValue`=NULL WHERE `name`='foo'",
context: {options: {omitNull: false}}
}, {
arguments: ['myTable', {bar: 2, nullValue: null}, {name: 'foo'}],
expectation: "UPDATE `myTable` SET `bar`=2 WHERE `name`='foo'",
context: {options: {omitNull: true}}
}
],
......@@ -177,7 +203,10 @@ describe('QueryGenerator', function() {
tests.forEach(function(test) {
var title = test.title || 'correctly returns ' + test.expectation + ' for ' + util.inspect(test.arguments)
it(title, function() {
var conditions = QueryGenerator[suiteTitle].apply(null, test.arguments)
// Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly
var context = test.context || {options: {}};
var conditions = QueryGenerator[suiteTitle].apply(context, test.arguments)
expect(conditions).toEqual(test.expectation)
})
})
......
......@@ -85,6 +85,21 @@ describe('QueryGenerator', function() {
}, {
arguments: ['myTable', {name: 'foo', foo: 1}],
expectation: "INSERT INTO \"myTable\" (\"name\",\"foo\") VALUES ('foo',1) RETURNING *;"
}, {
arguments: ['myTable', {name: 'foo', nullValue: null}],
expectation: "INSERT INTO \"myTable\" (\"name\",\"nullValue\") VALUES ('foo',NULL) RETURNING *;"
}, {
arguments: ['myTable', {name: 'foo', nullValue: null}],
expectation: "INSERT INTO \"myTable\" (\"name\",\"nullValue\") VALUES ('foo',NULL) RETURNING *;",
context: {options: {omitNull: false}}
}, {
arguments: ['myTable', {name: 'foo', nullValue: null}],
expectation: "INSERT INTO \"myTable\" (\"name\") VALUES ('foo') RETURNING *;",
context: {options: {omitNull: true}}
}, {
arguments: ['myTable', {name: 'foo', nullValue: undefined}],
expectation: "INSERT INTO \"myTable\" (\"name\") VALUES ('foo') RETURNING *;",
context: {options: {omitNull: true}}
}
],
......@@ -101,7 +116,22 @@ describe('QueryGenerator', function() {
}, {
arguments: ['myTable', {name: "foo';DROP TABLE myTable;"}, {name: 'foo'}],
expectation: "UPDATE \"myTable\" SET \"name\"='foo\\';DROP TABLE myTable;' WHERE \"name\"='foo'"
}
}, {
arguments: ['myTable', {bar: 2, nullValue: null}, {name: 'foo'}],
expectation: "UPDATE \"myTable\" SET \"bar\"=2,\"nullValue\"=NULL WHERE \"name\"='foo'"
}, {
arguments: ['myTable', {bar: 2, nullValue: null}, {name: 'foo'}],
expectation: "UPDATE \"myTable\" SET \"bar\"=2,\"nullValue\"=NULL WHERE \"name\"='foo'",
context: {options: {omitNull: false}}
}, {
arguments: ['myTable', {bar: 2, nullValue: null}, {name: 'foo'}],
expectation: "UPDATE \"myTable\" SET \"bar\"=2 WHERE \"name\"='foo'",
context: {options: {omitNull: true}}
}, {
arguments: ['myTable', {bar: 2, nullValue: undefined}, {name: 'foo'}],
expectation: "UPDATE \"myTable\" SET \"bar\"=2 WHERE \"name\"='foo'",
context: {options: {omitNull: true}}
},
],
deleteQuery: [
......@@ -174,7 +204,9 @@ describe('QueryGenerator', function() {
tests.forEach(function(test) {
var title = test.title || 'correctly returns ' + test.expectation + ' for ' + util.inspect(test.arguments)
it(title, function() {
var conditions = QueryGenerator[suiteTitle].apply(null, test.arguments)
// Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly
var context = test.context || {options: {}};
var conditions = QueryGenerator[suiteTitle].apply(context, test.arguments)
expect(conditions).toEqual(test.expectation)
})
})
......
......@@ -28,6 +28,21 @@ describe('QueryGenerator', function() {
}, {
arguments: ['myTable', { name: "foo", value: false }],
expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('foo',0);"
}, {
arguments: ['myTable', {name: 'foo', foo: 1, nullValue: null}],
expectation: "INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL);"
}, {
arguments: ['myTable', {name: 'foo', foo: 1, nullValue: null}],
expectation: "INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL);",
context: {options: {omitNull: false}}
}, {
arguments: ['myTable', {name: 'foo', foo: 1, nullValue: null}],
expectation: "INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1);",
context: {options: {omitNull: true}}
}, {
arguments: ['myTable', {name: 'foo', foo: 1, nullValue: undefined}],
expectation: "INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1);",
context: {options: {omitNull: true}}
}
],
......@@ -50,6 +65,17 @@ describe('QueryGenerator', function() {
}, {
arguments: ['myTable', { flag: false }, { id: 2 }],
expectation: "UPDATE `myTable` SET `flag`=0 WHERE `id`=2"
}, {
arguments: ['myTable', {bar: 2, nullValue: null}, {name: 'foo'}],
expectation: "UPDATE `myTable` SET `bar`=2,`nullValue`=NULL WHERE `name`='foo'"
}, {
arguments: ['myTable', {bar: 2, nullValue: null}, {name: 'foo'}],
expectation: "UPDATE `myTable` SET `bar`=2,`nullValue`=NULL WHERE `name`='foo'",
context: {options: {omitNull: false}}
}, {
arguments: ['myTable', {bar: 2, nullValue: null}, {name: 'foo'}],
expectation: "UPDATE `myTable` SET `bar`=2 WHERE `name`='foo'",
context: {options: {omitNull: true}}
}
]
};
......@@ -59,7 +85,10 @@ describe('QueryGenerator', function() {
tests.forEach(function(test) {
var title = test.title || 'correctly returns ' + test.expectation + ' for ' + util.inspect(test.arguments)
it(title, function() {
var conditions = QueryGenerator[suiteTitle].apply(null, test.arguments)
// Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly
var context = test.context || {options: {}};
var conditions = QueryGenerator[suiteTitle].apply(context, test.arguments)
expect(conditions).toEqual(test.expectation)
})
})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!