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

Commit 204c7e70 by Mick Hansen

quickfix for where to place order by when using subqueries

1 parent 28a35c30
...@@ -767,12 +767,26 @@ module.exports = (function() { ...@@ -767,12 +767,26 @@ module.exports = (function() {
// Add ORDER to sub or main query // Add ORDER to sub or main query
if (options.order) { 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) { if (Array.isArray(options.order)) {
subQueryItems.push(" ORDER BY " + 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 { } 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(', '))
} }
} }
...@@ -793,6 +807,7 @@ module.exports = (function() { ...@@ -793,6 +807,7 @@ module.exports = (function() {
query += subQueryItems.join('') query += subQueryItems.join('')
query += ") AS "+options.table query += ") AS "+options.table
query += mainJoinQueries.join('') query += mainJoinQueries.join('')
query += mainQueryItems.join('')
} else { } else {
query = mainQueryItems.join('') query = mainQueryItems.join('')
} }
......
...@@ -139,6 +139,8 @@ module.exports = (function() { ...@@ -139,6 +139,8 @@ module.exports = (function() {
Sequelize.DAOValidator = DAOValidator Sequelize.DAOValidator = DAOValidator
Sequelize.DAOFactory = Sequelize.Model = DAOFactory
for (var dataType in DataTypes) { for (var dataType in DataTypes) {
Sequelize[dataType] = DataTypes[dataType] Sequelize[dataType] = DataTypes[dataType]
} }
......
...@@ -1029,6 +1029,28 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -1029,6 +1029,28 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}, function() {done()}) }, 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) { it('sorts by through table attribute', function(done) {
var self = this var self = this
async.forEach([ [ 'ASC', 'England', 'Energy' ], [ 'DESC', 'France', 'Media' ] ], function(params, callback) { async.forEach([ [ 'ASC', 'England', 'Energy' ], [ 'DESC', 'France', 'Media' ] ], function(params, callback) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!