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

Commit 43ed6755 by Mick Hansen

Fix bug with limit: 1 and findAndCountAll

1 parent f5fb6122
...@@ -545,7 +545,9 @@ module.exports = (function() { ...@@ -545,7 +545,9 @@ module.exports = (function() {
} }
options = paranoidClause.call(this, options) options = paranoidClause.call(this, options)
options.limit = 1 if (options.limit === undefined) {
options.limit = 1
}
return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({ return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({
plain: true, plain: true,
...@@ -586,7 +588,9 @@ module.exports = (function() { ...@@ -586,7 +588,9 @@ module.exports = (function() {
options.attributes = [ options.attributes = [
[this.sequelize.fn('COUNT', this.sequelize.col(this.tableName+'.*')), 'count'] [this.sequelize.fn('COUNT', this.sequelize.col(this.tableName+'.*')), 'count']
] ]
options.includeIgnoreAttributes = false options.includeIgnoreAttributes = false
options.limit = null
this.find(options, {raw: true, transaction: options.transaction}).proxy(emitter, {events: ['sql', 'error']}).success(function (result) { this.find(options, {raw: true, transaction: options.transaction}).proxy(emitter, {events: ['sql', 'error']}).success(function (result) {
emitter.emit('success', parseInt(result.count, 10)) emitter.emit('success', parseInt(result.count, 10))
...@@ -610,18 +614,18 @@ module.exports = (function() { ...@@ -610,18 +614,18 @@ module.exports = (function() {
} }
self.count(countOptions) self.count(countOptions)
.proxy(emitter, {events: ['sql', 'error']}) .proxy(emitter, {events: ['sql', 'error']})
.success(function(count) { .success(function(count) {
if (count === 0) { if (count === 0) {
return emit.okay(count) // no records, no need for another query return emit.okay(count) // no records, no need for another query
} }
self.findAll(findOptions, queryOptions) self.findAll(findOptions, queryOptions)
.proxy(emitter, {events: ['sql', 'error']}) .proxy(emitter, {events: ['sql', 'error']})
.success(function(results) { .success(function(results) {
emit.okay(count, results) emit.okay(count, results)
}) })
}) })
}).run() }).run()
} }
......
...@@ -473,8 +473,8 @@ module.exports = (function() { ...@@ -473,8 +473,8 @@ module.exports = (function() {
, mainQueryItems = [] , mainQueryItems = []
, mainAttributes = options.attributes , mainAttributes = options.attributes
, mainJoinQueries = [] , mainJoinQueries = []
// We'll use a subquery if there's a limit, if we have hasMany associations and if any of them are filtered // We'll use a subquery if we have hasMany associations and a limit or a filtered/required association
, subQuery = limit && options && options.hasIncludeWhere && options.hasIncludeRequired && options.hasMultiAssociation , subQuery = (limit || options.hasIncludeWhere || options.hasIncludeRequired) && options.hasMultiAssociation
, subQueryItems = [] , subQueryItems = []
, subQueryAttributes = null , subQueryAttributes = null
, subJoinQueries = [] , subJoinQueries = []
...@@ -534,7 +534,9 @@ module.exports = (function() { ...@@ -534,7 +534,9 @@ module.exports = (function() {
, includeWhere = {} , includeWhere = {}
, whereOptions = Utils._.clone(options) , whereOptions = Utils._.clone(options)
if (tableName !== parentTable) as = parentTable+'.'+include.as if (tableName !== parentTable) {
as = parentTable+'.'+include.as
}
if (include.where) { if (include.where) {
for (var key in include.where) { for (var key in include.where) {
...@@ -795,12 +797,12 @@ module.exports = (function() { ...@@ -795,12 +797,12 @@ module.exports = (function() {
return "ROLLBACK;" return "ROLLBACK;"
}, },
addLimitAndOffset: function(options, query){ addLimitAndOffset: function(options, query) {
query = query || "" query = query || ""
if (options.offset && !options.limit) { if (options.offset && !options.limit) {
query += " LIMIT " + options.offset + ", " + 10000000000000; query += " LIMIT " + options.offset + ", " + 10000000000000;
} else if (options.limit && !(options.include && (options.limit === 1))) { } else if (options.limit) {
if (options.offset) { if (options.offset) {
query += " LIMIT " + options.offset + ", " + options.limit query += " LIMIT " + options.offset + ", " + options.limit
} else { } else {
......
...@@ -410,14 +410,12 @@ module.exports = (function() { ...@@ -410,14 +410,12 @@ module.exports = (function() {
addLimitAndOffset: function(options, query){ addLimitAndOffset: function(options, query){
query = query || "" query = query || ""
if (!(options.include && (options.limit === 1))) { if (options.limit) {
if (options.limit) { query += " LIMIT " + options.limit
query += " LIMIT " + options.limit }
}
if (options.offset) { if (options.offset) {
query += " OFFSET " + options.offset query += " OFFSET " + options.offset
}
} }
return query; return query;
......
...@@ -153,7 +153,7 @@ module.exports = (function() { ...@@ -153,7 +153,7 @@ module.exports = (function() {
query = query || "" query = query || ""
if (options.offset && !options.limit) { if (options.offset && !options.limit) {
query += " LIMIT " + options.offset + ", " + 10000000000000; query += " LIMIT " + options.offset + ", " + 10000000000000;
} else if (options.limit && !(options.include && (options.limit === 1))) { } else if (options.limit) {
if (options.offset) { if (options.offset) {
query += " LIMIT " + options.offset + ", " + options.limit query += " LIMIT " + options.offset + ", " + options.limit
} else { } else {
......
...@@ -599,10 +599,12 @@ Utils.fn.prototype.toString = function(queryGenerator) { ...@@ -599,10 +599,12 @@ Utils.fn.prototype.toString = function(queryGenerator) {
} }
Utils.col.prototype.toString = function (queryGenerator) { Utils.col.prototype.toString = function (queryGenerator) {
if (this.col.indexOf('*') !== -1) return '*' if (this.col.indexOf('*') === 0) {
return '*'
}
return queryGenerator.quote(this.col) return queryGenerator.quote(this.col)
} }
Utils.CustomEventEmitter = require(__dirname + "/emitters/custom-event-emitter") Utils.CustomEventEmitter = require(__dirname + "/emitters/custom-event-emitter")
Utils.QueryChainer = require(__dirname + "/query-chainer") Utils.QueryChainer = require(__dirname + "/query-chainer")
Utils.Lingo = require("lingo") Utils.Lingo = require("lingo")
\ No newline at end of file
...@@ -957,6 +957,57 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -957,6 +957,57 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
it("handles offset with includes", function (done) {
var Election = this.sequelize.define('Election', {
name: Sequelize.STRING
})
var Citizen = this.sequelize.define('Citizen', {
name: Sequelize.STRING
})
// Associations
Election.belongsTo(Citizen)
Election.hasMany(Citizen, { as: 'Voters', through: 'ElectionsVotes' })
Citizen.hasMany(Election)
Citizen.hasMany(Election, { as: 'Votes', through: 'ElectionsVotes' })
this.sequelize.sync().done(function (err) {
expect(err).not.be.ok
// Add some data
Citizen.create({ name: 'Alice' }).done(function (err, alice) {
expect(err).not.be.ok
Citizen.create({ name: 'Bob' }).done(function (err, bob) {
expect(err).not.be.ok
Election.create({ name: 'Some election' }).done(function (err, election) {
expect(err).not.be.ok
election.setCitizen(alice).done(function (err) {
expect(err).not.be.ok
election.setVoters([alice, bob]).done(function (err) {
expect(err).not.be.ok
var criteria = {
offset: 5,
limit: 1,
include: [
Citizen, // Election creator
{ model: Citizen, as: 'Voters' } // Election voters
]
}
Election.findAndCountAll(criteria).done(function (err, elections) {
expect(err).not.be.ok
expect(elections.count).to.equal(2)
expect(elections.rows.length).to.equal(0)
done()
})
})
})
})
})
})
})
})
it("handles attributes", function(done) { it("handles attributes", function(done) {
this.User.findAndCountAll({where: "id != " + this.users[0].id, attributes: ['data']}).success(function(info) { this.User.findAndCountAll({where: "id != " + this.users[0].id, attributes: ['data']}).success(function(info) {
expect(info.count).to.equal(2) expect(info.count).to.equal(2)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!