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

Commit 637d6b6f by alexwl

Added support for limit clause on update

1 parent 1bc46df2
...@@ -1399,6 +1399,7 @@ module.exports = (function() { ...@@ -1399,6 +1399,7 @@ module.exports = (function() {
* @param {Object} [options] * @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.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 {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 * @deprecated The syntax is due for change, in order to make `where` more consistent with the rest of the API
* *
* @return {EventEmitter} * @return {EventEmitter}
......
...@@ -7,6 +7,7 @@ AbstractDialect.prototype.supports = { ...@@ -7,6 +7,7 @@ AbstractDialect.prototype.supports = {
'DEFAULT': true, 'DEFAULT': true,
'DEFAULT VALUES': false, 'DEFAULT VALUES': false,
'VALUES ()': false, 'VALUES ()': false,
'LIMIT ON UPDATE':false,
schemas: false schemas: false
} }
......
...@@ -204,6 +204,11 @@ module.exports = (function() { ...@@ -204,6 +204,11 @@ module.exports = (function() {
, values = [] , values = []
query = "UPDATE <%= table %> SET <%= values %> WHERE <%= where %>" 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)) { if (this._dialect.supports['RETURNING'] && (options.returning || options.returning === undefined)) {
query += " RETURNING *" query += " RETURNING *"
} }
......
...@@ -6,7 +6,7 @@ var MariaDialect = function(sequelize) { ...@@ -6,7 +6,7 @@ var MariaDialect = function(sequelize) {
} }
MariaDialect.prototype = _.defaults({ MariaDialect.prototype = _.defaults({
'LIMIT ON UPDATE':true
}, MySQL.prototype) }, MySQL.prototype)
module.exports = MariaDialect module.exports = MariaDialect
...@@ -6,7 +6,8 @@ var MysqlDialect = function(sequelize) { ...@@ -6,7 +6,8 @@ var MysqlDialect = function(sequelize) {
} }
MysqlDialect.prototype.supports = _.defaults({ MysqlDialect.prototype.supports = _.defaults({
'VALUES ()': true 'VALUES ()': true,
'LIMIT ON UPDATE':true
}, Abstract.prototype.supports) }, Abstract.prototype.supports)
module.exports = MysqlDialect module.exports = MysqlDialect
...@@ -806,6 +806,24 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -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() { describe('destroy', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!