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

Commit 637d6b6f by alexwl

Added support for limit clause on update

1 parent 1bc46df2
......@@ -1399,6 +1399,7 @@ module.exports = (function() {
* @param {Object} [options]
* @param {Boolean} [options.validate=true] Should each row be subject to validation before it is inserted. The whole insert will fail if one row fails validation
* @param {Boolean} [options.hooks=false] Run before / after bulkUpdate hooks?
* @param {Number} [options.limit] How many rows to update (only for mysql and mariadb)
* @deprecated The syntax is due for change, in order to make `where` more consistent with the rest of the API
*
* @return {EventEmitter}
......
......@@ -7,6 +7,7 @@ AbstractDialect.prototype.supports = {
'DEFAULT': true,
'DEFAULT VALUES': false,
'VALUES ()': false,
'LIMIT ON UPDATE':false,
schemas: false
}
......
......@@ -204,6 +204,11 @@ module.exports = (function() {
, values = []
query = "UPDATE <%= table %> SET <%= values %> WHERE <%= where %>"
if(this._dialect.supports['LIMIT ON UPDATE'] && options.limit) {
query += " LIMIT "+this.escape(options.limit)+" "
}
if (this._dialect.supports['RETURNING'] && (options.returning || options.returning === undefined)) {
query += " RETURNING *"
}
......
......@@ -6,7 +6,7 @@ var MariaDialect = function(sequelize) {
}
MariaDialect.prototype = _.defaults({
'LIMIT ON UPDATE':true
}, MySQL.prototype)
module.exports = MariaDialect
......@@ -6,7 +6,8 @@ var MysqlDialect = function(sequelize) {
}
MysqlDialect.prototype.supports = _.defaults({
'VALUES ()': true
'VALUES ()': true,
'LIMIT ON UPDATE':true
}, Abstract.prototype.supports)
module.exports = MysqlDialect
......@@ -806,6 +806,24 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
})
if(Support.dialectIsMySQL()) {
it('supports limit clause', function (done) {
var self = this
, data = [{ username: 'Peter', secretValue: '42' },
{ username: 'Peter', secretValue: '42' },
{ username: 'Peter', secretValue: '42' }]
this.User.bulkCreate(data).success(function () {
self.User.update({secretValue: '43'}, {username: 'Peter'}, {limit: 1}).done(function (err, affectedRows) {
expect(err).not.to.be.ok
expect(affectedRows).to.equal(1)
done()
})
})
})
}
})
describe('destroy', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!