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

Commit cc99c74a by Nuno Sousa

Change bulkUpdate to return the affected rows

1 parent a64fe80d
......@@ -398,6 +398,12 @@ module.exports = {
UUIDV4: 'UUIDV4',
/**
* A key / value column. Only available in postgres.
* @property HSTORE
*/
HSTORE: 'HSTORE',
/**
* A virtual value that is not stored in the DB. This could for example be useful if you want to provide a default value in your model
* that is returned to the user but not stored in the DB.
*
......@@ -453,22 +459,5 @@ module.exports = {
* An array of `type`, e.g. `DataTypes.ARRAY(DataTypes.DECIMAL)`. Only available in postgres.
* @property ARRAY
*/
ARRAY: function(type) { return type + '[]'; },
/**
* A key / value column. Only available in postgres.
* @property HSTORE
*/
get HSTORE() {
var result = function() {
return {
type: 'HSTORE'
};
};
result.type = 'HSTORE';
result.toString = result.valueOf = function() { return 'HSTORE'; };
return result;
}
ARRAY: function(type) { return type + '[]'; }
};
......@@ -812,7 +812,7 @@ module.exports = (function() {
if (value && value._isSequelizeMethod) {
return value.toString(this);
} else {
if (field && field.type && field.type.toString() === DataTypes.HSTORE.type && Utils._.isObject(value)) {
if (Utils._.isObject(value) && field && (field === DataTypes.HSTORE || field.type === DataTypes.HSTORE)) {
value = hstore.stringify(value);
}
......
......@@ -7,6 +7,16 @@ var Utils = require('../../utils')
, QueryTypes = require('../../query-types')
, Promise = require('../../promise');
// Parses hstore fields if the model has any hstore fields.
// This cannot be done in the 'pg' lib because hstore is a UDT.
var parseHstoreFields = function(model, row) {
Utils._.keys(row).forEach(function(key) {
if (model._isHstoreAttribute(key)) {
row[key] = hstore.parse(row[key]);
}
});
};
module.exports = (function() {
var Query = function(client, sequelize, callee, options) {
this.client = client;
......@@ -125,15 +135,9 @@ module.exports = (function() {
});
}
// Parse hstore fields if the model has any hstore fields.
// This cannot be done in the 'pg' lib because hstore is a UDT.
if (!!self.callee && !!self.callee._hasHstoreAttributes) {
rows.forEach(function(row) {
Utils._.keys(row).forEach(function(key) {
if (self.callee._isHstoreAttribute(key)) {
row[key] = hstore.parse(row[key]);
}
});
parseHstoreFields(self.callee, row);
});
}
......@@ -141,20 +145,30 @@ module.exports = (function() {
}
} else if (self.send('isShowOrDescribeQuery')) {
return results;
} else if ([QueryTypes.BULKUPDATE, QueryTypes.BULKDELETE].indexOf(self.options.type) !== -1) {
} else if (QueryTypes.BULKUPDATE === self.options.type) {
if (!!self.callee && !!self.callee._hasHstoreAttributes) {
rows.forEach(function(row) {
parseHstoreFields(self.callee, row);
});
}
return self.send('handleSelectQuery', rows);
} else if (QueryTypes.BULKDELETE === self.options.type) {
return result.rowCount;
} else if (self.send('isInsertQuery') || self.send('isUpdateQuery')) {
if (self.callee !== null) { // may happen for bulk inserts or bulk updates
if (!!self.callee && self.callee.dataValues) {
if (!!self.callee.Model && !!self.callee.Model._hasHstoreAttributes) {
parseHstoreFields(self.callee.Model, rows[0]);
}
for (var key in rows[0]) {
if (rows[0].hasOwnProperty(key)) {
var record = rows[0][key];
if (!!self.callee.Model && !!self.callee.Model.rawAttributes && !!self.callee.Model.rawAttributes[key] && !!self.callee.Model.rawAttributes[key].type && self.callee.Model.rawAttributes[key].type.toString() === DataTypes.HSTORE.toString()) {
record = hstore.parse(record);
}
var attr = Utils._.find(self.callee.Model.rawAttributes, function (attribute) {
return attribute.fieldName === key || attribute.field === key;
});
self.callee.dataValues[attr && attr.fieldName || key] = record;
}
}
......
......@@ -262,11 +262,12 @@ module.exports = (function() {
Utils._.each(this.rawAttributes, function(definition, name) {
var type = definition.originalType || definition.type || definition;
if (type === DataTypes.BOOLEAN) {
self._booleanAttributes.push(name);
} else if (type === DataTypes.DATE) {
self._dateAttributes.push(name);
} else if (type === DataTypes.HSTORE.type) {
} else if (type === DataTypes.HSTORE) {
self._hstoreAttributes.push(name);
} else if (type === DataTypes.VIRTUAL) {
self._virtualAttributes.push(name);
......
......@@ -448,9 +448,11 @@ module.exports = (function() {
};
QueryInterface.prototype.bulkUpdate = function(tableName, values, identifier, options, attributes) {
var sql = this.QueryGenerator.updateQuery(tableName, values, identifier, options, attributes);
var sql = this.QueryGenerator.updateQuery(tableName, values, identifier, options, attributes)
, table = Utils._.isObject(tableName) ? tableName : { tableName: tableName }
, daoTable = Utils._.find(this.sequelize.daoFactoryManager.daos, { tableName: table.tableName });
return this.sequelize.query(sql, null, options);
return this.sequelize.query(sql, daoTable, options);
};
QueryInterface.prototype.delete = function(dao, tableName, identifier, options) {
......
......@@ -783,6 +783,31 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
if (dialect === "postgres") {
it('returns the affected rows', function(_done) {
var self = this
, data = [{ username: 'Peter', secretValue: '42' },
{ username: 'Paul', secretValue: '42' },
{ username: 'Bob', secretValue: '43' }]
, done = _.after(2, _done)
this.User.bulkCreate(data).success(function() {
self.User.update({username: 'Bill'}, {secretValue: '42'}).spread(function(affectedRows) {
expect(affectedRows).to.have.length(2)
done()
})
self.User.update({username: 'Bill'}, {secretValue: '44'}).spread(function(affectedRows) {
expect(affectedRows).to.have.length(0)
done()
})
})
})
}
if (dialect !== "postgres") {
it('returns the number of affected rows', function(_done) {
var self = this
, data = [{ username: 'Peter', secretValue: '42' },
......@@ -804,6 +829,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
})
}
if(Support.dialectIsMySQL()) {
it('supports limit clause', function (done) {
......
......@@ -260,7 +260,7 @@ if (dialect.match(/^postgres/)) {
.create({ username: 'user', email: ['foo@bar.com'], settings: { created: { test: '"value"' }}})
.success(function(newUser) {
// Check to see if the default value for an hstore field works
expect(newUser.document).to.deep.equal({default: 'value'})
expect(newUser.document).to.deep.equal({ default: 'value' })
expect(newUser.settings).to.deep.equal({ created: { test: '"value"' }})
// Check to see if updating an hstore field works
......@@ -295,6 +295,22 @@ if (dialect.match(/^postgres/)) {
.error(console.log)
})
it("should update hstore correctly and return affected rows", function(done) {
var self = this
this.User
.create({ username: 'user', email: ['foo@bar.com'], settings: { created: { test: '"value"' }}})
.success(function(oldUser) {
// Update the user and check that the returned object's fields have been parsed by the hstore library
self.User.update({settings: {should: 'update', to: 'this', first: 'place'}}, oldUser.identifiers).spread(function(newUsers) {
expect(newUsers).to.have.length(1);
expect(newUsers[0].settings).to.deep.equal({should: 'update', to: 'this', first: 'place'})
done()
})
})
.error(console.log)
})
it("should read hstore correctly", function(done) {
var self = this
var data = { username: 'user', email: ['foo@bar.com'], settings: { created: { test: '"value"' }}}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!