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

Commit 10b93eab by Jan Aagaard Meier

Blob support in upsert. Closes #2993

1 parent f27f9a27
......@@ -1675,7 +1675,7 @@ module.exports = (function() {
}).filter(function (item) {
return item && item.length;
});
return value.length ? '('+value.join(binding)+')' : undefined;
}
}
......@@ -1704,8 +1704,8 @@ module.exports = (function() {
if (value.$not || value.$notIn) comparator = 'NOT IN';
value = '('+(value.$in || value.$not || value.$notIn || value).map(function (item) {
return self.escape(item);
}).join(', ')+')';
} else if (value && value.$any) {
}).join(', ')+')';
} else if (value && value.$any) {
comparator = '= ANY';
if (value.$any.$values) {
value = '(VALUES '+value.$any.$values.map(function (value) {
......@@ -1720,7 +1720,7 @@ module.exports = (function() {
value = (value.$between || value.$notBetween).map(function (item) {
return self.escape(item);
}).join(' AND ');
}).join(' AND ');
} else {
if (_.isPlainObject(value)) {
_.forOwn(value, function (item, key) {
......@@ -1779,7 +1779,7 @@ module.exports = (function() {
model: factory,
prefix: prepend && tableName
});
}
}
result = this.handleSequelizeMethod(smth, tableName, factory, options, prepend);
} else if (Utils._.isPlainObject(smth)) {
return self.whereItemsQuery(smth, {
......
......@@ -2183,7 +2183,7 @@ module.exports = (function() {
};
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.
if (elem &&
(
......@@ -2195,8 +2195,6 @@ module.exports = (function() {
) {
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'
return undefined;
......
......@@ -533,7 +533,7 @@ module.exports = (function() {
if (model._timestampAttributes.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];
} else {
......
......@@ -157,6 +157,14 @@ var Utils = module.exports = {
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.
// The smartWhere function breaks down the collection of where objects into a more
// centralized object for each column so we can avoid duplicates
......
......@@ -27,7 +27,8 @@ describe(Support.getTestDialectTeaser('Model'), function() {
bar: {
unique: 'foobar',
type: DataTypes.INTEGER
}
},
blob: DataTypes.BLOB
});
return this.sequelize.sync({ force: true });
......@@ -153,6 +154,33 @@ describe(Support.getTestDialectTeaser('Model'), function() {
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!