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

Commit 3b9faacf by Michael Schonfeld

add support for mysql insert ignore + tests

1 parent 07c67c43
...@@ -778,7 +778,8 @@ module.exports = (function() { ...@@ -778,7 +778,8 @@ module.exports = (function() {
options = Utils._.extend({ options = Utils._.extend({
validate: false, validate: false,
hooks: false hooks: false,
ignore: false
}, options || {}) }, options || {})
if (fieldsOrOptions instanceof Array) { if (fieldsOrOptions instanceof Array) {
......
...@@ -176,8 +176,8 @@ module.exports = (function() { ...@@ -176,8 +176,8 @@ module.exports = (function() {
return Utils._.template(query)({ tableName: tableName, attributes: attrString.join(', ') }) return Utils._.template(query)({ tableName: tableName, attributes: attrString.join(', ') })
}, },
bulkInsertQuery: function(tableName, attrValueHashes) { bulkInsertQuery: function(tableName, attrValueHashes, options) {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;" var query = "INSERT<%= ignore %> INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
, tuples = [] , tuples = []
, allAttributes = [] , allAttributes = []
...@@ -196,6 +196,7 @@ module.exports = (function() { ...@@ -196,6 +196,7 @@ module.exports = (function() {
}.bind(this)) }.bind(this))
var replacements = { var replacements = {
ignore: options && options.ignore ? ' IGNORE' : '',
table: this.quoteIdentifier(tableName), table: this.quoteIdentifier(tableName),
attributes: allAttributes.map(function(attr){ attributes: allAttributes.map(function(attr){
return this.quoteIdentifier(attr) return this.quoteIdentifier(attr)
......
...@@ -480,7 +480,7 @@ module.exports = (function() { ...@@ -480,7 +480,7 @@ module.exports = (function() {
} }
QueryInterface.prototype.bulkInsert = function(tableName, records, options) { QueryInterface.prototype.bulkInsert = function(tableName, records, options) {
var sql = this.QueryGenerator.bulkInsertQuery(tableName, records) var sql = this.QueryGenerator.bulkInsertQuery(tableName, records, options)
return queryAndEmit.call(this, [sql, null, options], 'bulkInsert') return queryAndEmit.call(this, [sql, null, options], 'bulkInsert')
} }
......
...@@ -24,7 +24,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -24,7 +24,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
data: DataTypes.STRING, data: DataTypes.STRING,
intVal: DataTypes.INTEGER, intVal: DataTypes.INTEGER,
theDate: DataTypes.DATE, theDate: DataTypes.DATE,
aBool: DataTypes.BOOLEAN aBool: DataTypes.BOOLEAN,
uniqueName: { type: DataTypes.STRING, unique: true }
}) })
this.User.sync({ force: true }).success(function() { this.User.sync({ force: true }).success(function() {
...@@ -990,6 +991,28 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -990,6 +991,28 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
it("should support the insert ignore option", function(done) {
var self = this
, data = [{ uniqueName: 'Peter', secretValue: '42' },
{ uniqueName: 'Paul', secretValue: '23' }]
this.User.bulkCreate(data, { fields: ['uniqueName', 'secretValue'] }).success(function() {
data.push({ uniqueName: 'Michael', secretValue: '26' });
self.User.bulkCreate(data, { fields: ['uniqueName', 'secretValue'], ignore: true }).success(function() {
self.User.findAll({order: 'id'}).success(function(users) {
expect(users.length).to.equal(3)
expect(users[0].uniqueName).to.equal("Peter")
expect(users[0].secretValue).to.equal("42");
expect(users[1].uniqueName).to.equal("Paul")
expect(users[1].secretValue).to.equal("23");
expect(users[2].uniqueName).to.equal("Michael")
expect(users[2].secretValue).to.equal("26");
done()
});
});
})
})
describe('enums', function() { describe('enums', function() {
it('correctly restores enum values', function(done) { it('correctly restores enum values', function(done) {
var self = this var self = this
......
...@@ -389,6 +389,9 @@ if (Support.dialectIsMySQL()) { ...@@ -389,6 +389,9 @@ if (Support.dialectIsMySQL()) {
}, { }, {
arguments: ['myTable', [{name: "foo", value: true}, {name: 'bar', value: false}]], arguments: ['myTable', [{name: "foo", value: true}, {name: 'bar', value: false}]],
expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('foo',true),('bar',false);" expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('foo',true),('bar',false);"
}, {
arguments: ['myTable', [{name: 'foo'}, {name: 'bar'}], {ignore: true}],
expectation: "INSERT IGNORE INTO `myTable` (`name`) VALUES ('foo'),('bar');"
} }
], ],
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!