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

Commit 10b93eab by Jan Aagaard Meier

Blob support in upsert. Closes #2993

1 parent f27f9a27
...@@ -1675,7 +1675,7 @@ module.exports = (function() { ...@@ -1675,7 +1675,7 @@ module.exports = (function() {
}).filter(function (item) { }).filter(function (item) {
return item && item.length; return item && item.length;
}); });
return value.length ? '('+value.join(binding)+')' : undefined; return value.length ? '('+value.join(binding)+')' : undefined;
} }
} }
...@@ -1704,8 +1704,8 @@ module.exports = (function() { ...@@ -1704,8 +1704,8 @@ module.exports = (function() {
if (value.$not || value.$notIn) comparator = 'NOT IN'; if (value.$not || value.$notIn) comparator = 'NOT IN';
value = '('+(value.$in || value.$not || value.$notIn || value).map(function (item) { value = '('+(value.$in || value.$not || value.$notIn || value).map(function (item) {
return self.escape(item); return self.escape(item);
}).join(', ')+')'; }).join(', ')+')';
} else if (value && value.$any) { } else if (value && value.$any) {
comparator = '= ANY'; comparator = '= ANY';
if (value.$any.$values) { if (value.$any.$values) {
value = '(VALUES '+value.$any.$values.map(function (value) { value = '(VALUES '+value.$any.$values.map(function (value) {
...@@ -1720,7 +1720,7 @@ module.exports = (function() { ...@@ -1720,7 +1720,7 @@ module.exports = (function() {
value = (value.$between || value.$notBetween).map(function (item) { value = (value.$between || value.$notBetween).map(function (item) {
return self.escape(item); return self.escape(item);
}).join(' AND '); }).join(' AND ');
} else { } else {
if (_.isPlainObject(value)) { if (_.isPlainObject(value)) {
_.forOwn(value, function (item, key) { _.forOwn(value, function (item, key) {
...@@ -1779,7 +1779,7 @@ module.exports = (function() { ...@@ -1779,7 +1779,7 @@ module.exports = (function() {
model: factory, model: factory,
prefix: prepend && tableName prefix: prepend && tableName
}); });
} }
result = this.handleSequelizeMethod(smth, tableName, factory, options, prepend); result = this.handleSequelizeMethod(smth, tableName, factory, options, prepend);
} else if (Utils._.isPlainObject(smth)) { } else if (Utils._.isPlainObject(smth)) {
return self.whereItemsQuery(smth, { return self.whereItemsQuery(smth, {
......
...@@ -2183,7 +2183,7 @@ module.exports = (function() { ...@@ -2183,7 +2183,7 @@ module.exports = (function() {
}; };
var optClone = Model.prototype.__optClone = function(options) { var optClone = Model.prototype.__optClone = function(options) {
return Utils._.cloneDeep(options, function(elem) { return Utils.cloneDeep(options, function(elem) {
// The InstanceFactories used for include are pass by ref, so don't clone them. // The InstanceFactories used for include are pass by ref, so don't clone them.
if (elem && if (elem &&
( (
...@@ -2195,8 +2195,6 @@ module.exports = (function() { ...@@ -2195,8 +2195,6 @@ module.exports = (function() {
) { ) {
return elem; return elem;
} }
// Unfortunately, lodash.cloneDeep doesn't preserve Buffer.isBuffer, which we have to rely on for binary data
if (Buffer.isBuffer(elem)) { return elem; }
// Otherwise return undefined, meaning, 'handle this lodash' // Otherwise return undefined, meaning, 'handle this lodash'
return undefined; return undefined;
......
...@@ -533,7 +533,7 @@ module.exports = (function() { ...@@ -533,7 +533,7 @@ module.exports = (function() {
if (model._timestampAttributes.createdAt) { if (model._timestampAttributes.createdAt) {
// If we are updating an existing row, we shouldn't set createdAt // If we are updating an existing row, we shouldn't set createdAt
updateValues = Utils._.cloneDeep(values); updateValues = Utils.cloneDeep(values);
delete updateValues[model._timestampAttributes.createdAt]; delete updateValues[model._timestampAttributes.createdAt];
} else { } else {
......
...@@ -157,6 +157,14 @@ var Utils = module.exports = { ...@@ -157,6 +157,14 @@ var Utils = module.exports = {
self.scopeObj.where = lodash.uniq(self.scopeObj.where); self.scopeObj.where = lodash.uniq(self.scopeObj.where);
} }
}, },
cloneDeep: function(obj, fn) {
return lodash.cloneDeep(obj, function (elem) {
// Unfortunately, lodash.cloneDeep doesn't preserve Buffer.isBuffer, which we have to rely on for binary data
if (Buffer.isBuffer(elem)) { return elem; }
return fn ? fn(elem) : undefined;
});
},
// smartWhere can accept an array of {where} objects, or a single {where} object. // smartWhere can accept an array of {where} objects, or a single {where} object.
// The smartWhere function breaks down the collection of where objects into a more // The smartWhere function breaks down the collection of where objects into a more
// centralized object for each column so we can avoid duplicates // centralized object for each column so we can avoid duplicates
......
...@@ -27,7 +27,8 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -27,7 +27,8 @@ describe(Support.getTestDialectTeaser('Model'), function() {
bar: { bar: {
unique: 'foobar', unique: 'foobar',
type: DataTypes.INTEGER type: DataTypes.INTEGER
} },
blob: DataTypes.BLOB
}); });
return this.sequelize.sync({ force: true }); return this.sequelize.sync({ force: true });
...@@ -153,6 +154,33 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -153,6 +154,33 @@ describe(Support.getTestDialectTeaser('Model'), function() {
return expect(User.upsert({ email: 'notanemail' })).to.eventually.be.rejectedWith(this.sequelize.ValidationError); return expect(User.upsert({ email: 'notanemail' })).to.eventually.be.rejectedWith(this.sequelize.ValidationError);
}); });
it('works with BLOBs', function () {
return this.User.upsert({ id: 42, username: 'john', blob: new Buffer('kaj') }).bind(this).then(function(created) {
if (dialect === 'sqlite') {
expect(created).not.to.be.defined;
} else {
expect(created).to.be.ok;
}
return this.sequelize.Promise.delay(1000).bind(this).then(function() {
return this.User.upsert({ id: 42, username: 'doe', blob: new Buffer('andrea') });
});
}).then(function(created) {
if (dialect === 'sqlite') {
expect(created).not.to.be.defined;
} else {
expect(created).not.to.be.ok;
}
return this.User.find(42);
}).then(function(user) {
expect(user.createdAt).to.be.defined;
expect(user.username).to.equal('doe');
expect(user.blob.toString()).to.equal('andrea');
expect(user.updatedAt).to.be.afterTime(user.createdAt);
});
});
}); });
} }
}); });
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!