Skip to content

Commit

Permalink
quickfix for where to place order by when using subqueries
Browse files Browse the repository at this point in the history
  • Loading branch information
mickhansen committed Feb 19, 2014
1 parent 28a35c3 commit 204c7e7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
23 changes: 19 additions & 4 deletions lib/dialects/abstract/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -767,12 +767,26 @@ module.exports = (function() {

// Add ORDER to sub or main query
if (options.order) {
options.order = Array.isArray(options.order) ? options.order.map(function (t) { return this.quote(t, factory) }.bind(this)).join(', ') : options.order
var mainQueryOrder = [];
var subQueryOrder = [];

if (subQuery) {
subQueryItems.push(" ORDER BY " + options.order)
if (Array.isArray(options.order)) {
options.order.forEach(function (t) {
if (subQuery && !(t[0] instanceof daoFactory)) {
subQueryOrder.push(this.quote(t, factory))
} else {
mainQueryOrder.push(this.quote(t, factory))
}
}.bind(this))
} else {
mainQueryItems.push(" ORDER BY " + options.order)
mainQueryOrder.push(options.order)
}

if (mainQueryOrder.length) {
mainQueryItems.push(" ORDER BY " + mainQueryOrder.join(', '))
}
if (subQueryOrder.length) {
subQueryItems.push(" ORDER BY " + subQueryOrder.join(', '))
}
}

Expand All @@ -793,6 +807,7 @@ module.exports = (function() {
query += subQueryItems.join('')
query += ") AS "+options.table
query += mainJoinQueries.join('')
query += mainQueryItems.join('')
} else {
query = mainQueryItems.join('')
}
Expand Down
2 changes: 2 additions & 0 deletions lib/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ module.exports = (function() {

Sequelize.DAOValidator = DAOValidator

Sequelize.DAOFactory = Sequelize.Model = DAOFactory

for (var dataType in DataTypes) {
Sequelize[dataType] = DataTypes[dataType]
}
Expand Down
22 changes: 22 additions & 0 deletions test/dao-factory/findAll.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,28 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}, function() {done()})
})

it('sorts by 1st degree association while using limit', function(done) {
var self = this
async.forEach([ [ 'ASC', 'England', 'Energy' ], [ 'DESC', 'Korea', 'Tech' ] ], function(params, callback) {
self.Country.findAll({
include: [ self.Industry ],
order: [
[ self.Industry, 'name', params[0] ]
],
limit: 3
}).done(function(err, countries) {
expect(err).not.to.be.ok
expect(countries).to.exist
expect(countries[0]).to.exist
expect(countries[0].name).to.equal(params[1])
expect(countries[0].industries).to.exist
expect(countries[0].industries[0]).to.exist
expect(countries[0].industries[0].name).to.equal(params[2])
callback()
})
}, function() {done()})
})

it('sorts by through table attribute', function(done) {
var self = this
async.forEach([ [ 'ASC', 'England', 'Energy' ], [ 'DESC', 'France', 'Media' ] ], function(params, callback) {
Expand Down

0 comments on commit 204c7e7

Please sign in to comment.