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

Commit 33bdb8f5 by sdepold

merge

2 parents c8066605 ac3acf58
...@@ -18,7 +18,7 @@ module.exports = (function() { ...@@ -18,7 +18,7 @@ module.exports = (function() {
// clear the old associations // clear the old associations
oldAssociations.forEach(function(associatedObject) { oldAssociations.forEach(function(associatedObject) {
associatedObject[self.__factory.identifier] = null associatedObject[self.__factory.identifier] = self.options.omitNull ? '' : null
associatedObject.save() associatedObject.save()
}) })
......
...@@ -53,7 +53,7 @@ module.exports = (function() { ...@@ -53,7 +53,7 @@ module.exports = (function() {
var customEventEmitter = new Utils.CustomEventEmitter(function() { var customEventEmitter = new Utils.CustomEventEmitter(function() {
obj[self.accessors.get]().success(function(oldObj) { obj[self.accessors.get]().success(function(oldObj) {
if(oldObj) { if(oldObj) {
oldObj[self.identifier] = null oldObj[self.identifier] = self.options.omitNull ? '' : null;
oldObj.save() oldObj.save()
} }
...@@ -70,4 +70,4 @@ module.exports = (function() { ...@@ -70,4 +70,4 @@ module.exports = (function() {
} }
return HasOne return HasOne
})() })()
\ No newline at end of file
...@@ -129,6 +129,16 @@ module.exports = (function() { ...@@ -129,6 +129,16 @@ module.exports = (function() {
insertQuery: function(tableName, attrValueHash) { insertQuery: function(tableName, attrValueHash) {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>);" 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 = { var replacements = {
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(","),
...@@ -142,6 +152,17 @@ module.exports = (function() { ...@@ -142,6 +152,17 @@ module.exports = (function() {
updateQuery: function(tableName, values, where) { updateQuery: function(tableName, values, where) {
var query = "UPDATE <%= table %> SET <%= values %> WHERE <%= 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 = { var replacements = {
table: Utils.addTicks(tableName), table: Utils.addTicks(tableName),
values: Utils._.map(values, function(value, key){ values: Utils._.map(values, function(value, key){
......
...@@ -53,6 +53,8 @@ function pgDataTypeMapping(tableName, attr, dataType) { ...@@ -53,6 +53,8 @@ function pgDataTypeMapping(tableName, attr, dataType) {
module.exports = (function() { module.exports = (function() {
var QueryGenerator = { var QueryGenerator = {
options: {},
createTableQuery: function(tableName, attributes, options) { createTableQuery: function(tableName, attributes, options) {
options = Utils._.extend({ options = Utils._.extend({
}, options || {}) }, options || {})
...@@ -191,7 +193,18 @@ module.exports = (function() { ...@@ -191,7 +193,18 @@ module.exports = (function() {
insertQuery: function(tableName, attrValueHash) { insertQuery: function(tableName, attrValueHash) {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>) RETURNING *;" 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 = [] var returning = []
Utils._.forEach(attrValueHash, function(value, key, hash) { Utils._.forEach(attrValueHash, function(value, key, hash) {
if (tables[tableName] && tables[tableName][key]) { if (tables[tableName] && tables[tableName][key]) {
switch (tables[tableName][key]) { switch (tables[tableName][key]) {
...@@ -216,6 +229,17 @@ module.exports = (function() { ...@@ -216,6 +229,17 @@ module.exports = (function() {
updateQuery: function(tableName, values, where) { updateQuery: function(tableName, values, where) {
var query = "UPDATE <%= table %> SET <%= values %> WHERE <%= 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 = { var replacements = {
table: addQuotes(tableName), table: addQuotes(tableName),
values: Utils._.map(values, function(value, key){ values: Utils._.map(values, function(value, key){
......
...@@ -15,6 +15,8 @@ var escape = function(str) { ...@@ -15,6 +15,8 @@ var escape = function(str) {
module.exports = (function() { module.exports = (function() {
var QueryGenerator = { var QueryGenerator = {
options: {},
createTableQuery: function(tableName, attributes, options) { createTableQuery: function(tableName, attributes, options) {
options = options || {} options = options || {}
...@@ -44,6 +46,16 @@ module.exports = (function() { ...@@ -44,6 +46,16 @@ module.exports = (function() {
insertQuery: function(tableName, attrValueHash) { insertQuery: function(tableName, attrValueHash) {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>);"; 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 = { var replacements = {
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(","),
...@@ -57,6 +69,16 @@ module.exports = (function() { ...@@ -57,6 +69,16 @@ module.exports = (function() {
updateQuery: function(tableName, values, where) { updateQuery: function(tableName, values, where) {
var query = "UPDATE <%= table %> SET <%= values %> WHERE <%= 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 = { var replacements = {
table: Utils.addTicks(tableName), table: Utils.addTicks(tableName),
values: Utils._.map(values, function(value, key){ values: Utils._.map(values, function(value, key){
......
...@@ -5,6 +5,7 @@ module.exports = (function() { ...@@ -5,6 +5,7 @@ module.exports = (function() {
var QueryInterface = function(sequelize) { var QueryInterface = function(sequelize) {
this.sequelize = sequelize this.sequelize = sequelize
this.QueryGenerator = require('./dialects/' + this.sequelize.options.dialect + '/query-generator') this.QueryGenerator = require('./dialects/' + this.sequelize.options.dialect + '/query-generator')
this.QueryGenerator.options = this.sequelize.options;
} }
Utils.addEventEmitter(QueryInterface) Utils.addEventEmitter(QueryInterface)
......
...@@ -19,7 +19,8 @@ module.exports = (function() { ...@@ -19,7 +19,8 @@ module.exports = (function() {
define: {}, define: {},
query: {}, query: {},
sync: {}, sync: {},
logging: console.log logging: console.log,
omitNull: false
}, options || {}) }, options || {})
if(this.options.logging === true) { if(this.options.logging === true) {
...@@ -49,7 +50,7 @@ module.exports = (function() { ...@@ -49,7 +50,7 @@ module.exports = (function() {
Sequelize.Utils._.map(DataTypes, function(sql, accessor) { Sequelize[accessor] = sql}) Sequelize.Utils._.map(DataTypes, function(sql, accessor) { Sequelize[accessor] = sql})
Sequelize.prototype.getQueryInterface = function() { Sequelize.prototype.getQueryInterface = function() {
this.queryInterface = this.queryInterface || new QueryInterface(this) this.queryInterface = this.queryInterface || new QueryInterface(this)
return this.queryInterface return this.queryInterface
} }
...@@ -67,6 +68,7 @@ module.exports = (function() { ...@@ -67,6 +68,7 @@ module.exports = (function() {
if(this.options.define) if(this.options.define)
options = Sequelize.Utils.merge(options, this.options.define) options = Sequelize.Utils.merge(options, this.options.define)
options.omitNull = this.options.omitNull
var factory = new DAOFactory(daoName, attributes, options) var factory = new DAOFactory(daoName, attributes, options)
......
...@@ -97,6 +97,37 @@ var Utils = module.exports = { ...@@ -97,6 +97,37 @@ var Utils = module.exports = {
toDefaultValue: function(value) { toDefaultValue: function(value) {
return (value == DataTypes.NOW) ? new Date() : 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
} }
} }
......
...@@ -575,7 +575,7 @@ describe('DAO', function() { ...@@ -575,7 +575,7 @@ describe('DAO', function() {
it('returns a response that can be stringified', function() { it('returns a response that can be stringified', function() {
var self = this var self = this
var User = sequelize.define('User', { var User = sequelize.define('User', {
username: Sequelize.STRING, age: Sequelize.INTEGER, isAdmin: Sequelize.BOOLEAN username: Sequelize.STRING, age: Sequelize.INTEGER, isAdmin: Sequelize.BOOLEAN
}, { timestamps: false, logging: false }) }, { timestamps: false, logging: false })
......
...@@ -89,6 +89,21 @@ describe('QueryGenerator', function() { ...@@ -89,6 +89,21 @@ describe('QueryGenerator', function() {
}, { }, {
arguments: ['myTable', {name: 'foo', foo: 1}], arguments: ['myTable', {name: 'foo', foo: 1}],
expectation: "INSERT INTO `myTable` (`name`,`foo`) VALUES ('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() { ...@@ -105,6 +120,17 @@ describe('QueryGenerator', function() {
}, { }, {
arguments: ['myTable', {name: "foo';DROP TABLE myTable;"}, {name: 'foo'}], arguments: ['myTable', {name: "foo';DROP TABLE myTable;"}, {name: 'foo'}],
expectation: "UPDATE `myTable` SET `name`='foo\\';DROP TABLE myTable;' WHERE `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() { ...@@ -177,7 +203,10 @@ describe('QueryGenerator', function() {
tests.forEach(function(test) { tests.forEach(function(test) {
var title = test.title || 'correctly returns ' + test.expectation + ' for ' + util.inspect(test.arguments) var title = test.title || 'correctly returns ' + test.expectation + ' for ' + util.inspect(test.arguments)
it(title, function() { 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) expect(conditions).toEqual(test.expectation)
}) })
}) })
......
...@@ -85,6 +85,21 @@ describe('QueryGenerator', function() { ...@@ -85,6 +85,21 @@ describe('QueryGenerator', function() {
}, { }, {
arguments: ['myTable', {name: 'foo', foo: 1}], arguments: ['myTable', {name: 'foo', foo: 1}],
expectation: "INSERT INTO \"myTable\" (\"name\",\"foo\") VALUES ('foo',1) RETURNING *;" 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() { ...@@ -101,7 +116,22 @@ describe('QueryGenerator', function() {
}, { }, {
arguments: ['myTable', {name: "foo';DROP TABLE myTable;"}, {name: 'foo'}], arguments: ['myTable', {name: "foo';DROP TABLE myTable;"}, {name: 'foo'}],
expectation: "UPDATE \"myTable\" SET \"name\"='foo\\';DROP TABLE myTable;' WHERE \"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: [ deleteQuery: [
...@@ -174,7 +204,9 @@ describe('QueryGenerator', function() { ...@@ -174,7 +204,9 @@ describe('QueryGenerator', function() {
tests.forEach(function(test) { tests.forEach(function(test) {
var title = test.title || 'correctly returns ' + test.expectation + ' for ' + util.inspect(test.arguments) var title = test.title || 'correctly returns ' + test.expectation + ' for ' + util.inspect(test.arguments)
it(title, function() { 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) expect(conditions).toEqual(test.expectation)
}) })
}) })
......
...@@ -28,6 +28,21 @@ describe('QueryGenerator', function() { ...@@ -28,6 +28,21 @@ describe('QueryGenerator', function() {
}, { }, {
arguments: ['myTable', { name: "foo", value: false }], arguments: ['myTable', { name: "foo", value: false }],
expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('foo',0);" 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() { ...@@ -50,6 +65,17 @@ describe('QueryGenerator', function() {
}, { }, {
arguments: ['myTable', { flag: false }, { id: 2 }], arguments: ['myTable', { flag: false }, { id: 2 }],
expectation: "UPDATE `myTable` SET `flag`=0 WHERE `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() { ...@@ -59,7 +85,10 @@ describe('QueryGenerator', function() {
tests.forEach(function(test) { tests.forEach(function(test) {
var title = test.title || 'correctly returns ' + test.expectation + ' for ' + util.inspect(test.arguments) var title = test.title || 'correctly returns ' + test.expectation + ' for ' + util.inspect(test.arguments)
it(title, function() { 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) 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!