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

Commit 51e77239 by Jerome C

Support of ON DUPLICATE KEY for 'bulkCreate' method (only supported by mysql & mariadb)

1 parent e742e00b
......@@ -179,9 +179,10 @@ module.exports = (function() {
},
bulkInsertQuery: function(tableName, attrValueHashes, options) {
var query = 'INSERT<%= ignoreDuplicates %> INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;'
var query = 'INSERT<%= ignoreDuplicates %> INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %><%= onDuplicateKeyUpdate %>;'
, tuples = []
, allAttributes = [];
, allAttributes = []
, onDuplicateKeyUpdate = '';
Utils._.forEach(attrValueHashes, function(attrValueHash, i) {
Utils._.forOwn(attrValueHash, function(value, key, hash) {
......@@ -197,13 +198,21 @@ module.exports = (function() {
')');
}.bind(this));
if (options && options.updateOnDuplicate instanceof Array && options.updateOnDuplicate.length) {
onDuplicateKeyUpdate += ' ON DUPLICATE KEY UPDATE ' + options.updateOnDuplicate.map(function(attr) {
var key = this.quoteIdentifier(attr);
return key + '=VALUES(' + key + ')';
}.bind(this)).join(',');
}
var replacements = {
ignoreDuplicates: options && options.ignoreDuplicates ? ' IGNORE' : '',
table: this.quoteTable(tableName),
attributes: allAttributes.map(function(attr) {
return this.quoteIdentifier(attr);
}.bind(this)).join(','),
tuples: tuples
tuples: tuples,
onDuplicateKeyUpdate: onDuplicateKeyUpdate
};
return Utils._.template(query)(replacements);
......
......@@ -1232,6 +1232,7 @@ module.exports = (function() {
* @param {Boolean} [options.hooks=true] Run before / after bulk create hooks?
* @param {Boolean} [options.individualHooks=false] Run before / after create hooks for each individual Instance? BulkCreate hooks will still be run if options.hooks is true.
* @param {Boolean} [options.ignoreDuplicates=false] Ignore duplicate values for primary keys? (not supported by postgres)
* @param {Boolean} [options.updateOnDuplicate] Fields to update if row key already exists (on duplicate key update)? (only supported by mysql & mariadb)
*
* @return {Promise<Array<Instance>>}
*/
......@@ -1257,9 +1258,13 @@ module.exports = (function() {
options = Utils._.extend(options, fieldsOrOptions);
}
if (this.sequelize.options.dialect === 'postgres' && options.ignoreDuplicates) {
var dialect = this.sequelize.options.dialect;
if (options.ignoreDuplicates && dialect === 'postgres') {
return Promise.reject(new Error('Postgres does not support the \'ignoreDuplicates\' option.'));
}
if (options.updateOnDuplicate && ['mysql', 'mariadb'].indexOf(dialect) === -1) {
return Promise.reject(new Error(dialect + ' does not support the \'updateOnDuplicate\' option.'));
}
var self = this
, createdAtAttr = this._timestampAttributes.createdAt
......
......@@ -424,6 +424,9 @@ if (Support.dialectIsMySQL()) {
}, {
arguments: ['myTable', [{name: 'foo'}, {name: 'bar'}], {ignoreDuplicates: true}],
expectation: "INSERT IGNORE INTO `myTable` (`name`) VALUES ('foo'),('bar');"
}, {
arguments: ['myTable', [{name: 'foo'}, {name: 'bar'}], {updateOnDuplicate: ['name']}],
expectation: "INSERT INTO `myTable` (`name`) VALUES ('foo'),('bar') ON DUPLICATE KEY UPDATE `name`=VALUES(`name`);"
}
],
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!