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

Commit 43ed6755 by Mick Hansen

Fix bug with limit: 1 and findAndCountAll

1 parent f5fb6122
......@@ -545,7 +545,9 @@ module.exports = (function() {
}
options = paranoidClause.call(this, options)
if (options.limit === undefined) {
options.limit = 1
}
return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({
plain: true,
......@@ -586,7 +588,9 @@ module.exports = (function() {
options.attributes = [
[this.sequelize.fn('COUNT', this.sequelize.col(this.tableName+'.*')), 'count']
]
options.includeIgnoreAttributes = false
options.limit = null
this.find(options, {raw: true, transaction: options.transaction}).proxy(emitter, {events: ['sql', 'error']}).success(function (result) {
emitter.emit('success', parseInt(result.count, 10))
......
......@@ -473,8 +473,8 @@ module.exports = (function() {
, mainQueryItems = []
, mainAttributes = options.attributes
, mainJoinQueries = []
// We'll use a subquery if there's a limit, if we have hasMany associations and if any of them are filtered
, subQuery = limit && options && options.hasIncludeWhere && options.hasIncludeRequired && options.hasMultiAssociation
// We'll use a subquery if we have hasMany associations and a limit or a filtered/required association
, subQuery = (limit || options.hasIncludeWhere || options.hasIncludeRequired) && options.hasMultiAssociation
, subQueryItems = []
, subQueryAttributes = null
, subJoinQueries = []
......@@ -534,7 +534,9 @@ module.exports = (function() {
, includeWhere = {}
, whereOptions = Utils._.clone(options)
if (tableName !== parentTable) as = parentTable+'.'+include.as
if (tableName !== parentTable) {
as = parentTable+'.'+include.as
}
if (include.where) {
for (var key in include.where) {
......@@ -795,12 +797,12 @@ module.exports = (function() {
return "ROLLBACK;"
},
addLimitAndOffset: function(options, query){
addLimitAndOffset: function(options, query) {
query = query || ""
if (options.offset && !options.limit) {
query += " LIMIT " + options.offset + ", " + 10000000000000;
} else if (options.limit && !(options.include && (options.limit === 1))) {
} else if (options.limit) {
if (options.offset) {
query += " LIMIT " + options.offset + ", " + options.limit
} else {
......
......@@ -410,7 +410,6 @@ module.exports = (function() {
addLimitAndOffset: function(options, query){
query = query || ""
if (!(options.include && (options.limit === 1))) {
if (options.limit) {
query += " LIMIT " + options.limit
}
......@@ -418,7 +417,6 @@ module.exports = (function() {
if (options.offset) {
query += " OFFSET " + options.offset
}
}
return query;
},
......
......@@ -153,7 +153,7 @@ module.exports = (function() {
query = query || ""
if (options.offset && !options.limit) {
query += " LIMIT " + options.offset + ", " + 10000000000000;
} else if (options.limit && !(options.include && (options.limit === 1))) {
} else if (options.limit) {
if (options.offset) {
query += " LIMIT " + options.offset + ", " + options.limit
} else {
......
......@@ -599,7 +599,9 @@ Utils.fn.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)
}
......
......@@ -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) {
this.User.findAndCountAll({where: "id != " + this.users[0].id, attributes: ['data']}).success(function(info) {
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!