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

Commit 2e096c06 by ThePoloQ

Update query-generator.js

1 parent 0dff27a8
Showing with 18 additions and 2 deletions
...@@ -527,6 +527,7 @@ module.exports = (function() { ...@@ -527,6 +527,7 @@ module.exports = (function() {
// We'll use a subquery if we have hasMany associations and a limit and a filtered/required association // We'll use a subquery if we have hasMany associations and a limit and a filtered/required association
, subQuery = limit && (options.hasIncludeWhere || options.hasIncludeRequired || options.hasMultiAssociation) , subQuery = limit && (options.hasIncludeWhere || options.hasIncludeRequired || options.hasMultiAssociation)
, subQueryItems = [] , subQueryItems = []
, subQueryAs = []
, subQueryAttributes = null , subQueryAttributes = null
, subJoinQueries = [] , subJoinQueries = []
...@@ -583,6 +584,7 @@ module.exports = (function() { ...@@ -583,6 +584,7 @@ module.exports = (function() {
if (subQuery) { if (subQuery) {
// We need primary keys // We need primary keys
subQueryAttributes = mainAttributes subQueryAttributes = mainAttributes
subQueryAs.push(options.table.replace(/\`/g,''))
mainAttributes = [options.table+'.*'] mainAttributes = [options.table+'.*']
} }
...@@ -607,6 +609,10 @@ module.exports = (function() { ...@@ -607,6 +609,10 @@ module.exports = (function() {
if (tableName !== parentTable) { if (tableName !== parentTable) {
as = parentTable+'.'+include.as as = parentTable+'.'+include.as
} }
if (include.subQuery && subQuery){
subQueryAs.push(as);
}
// includeIgnoreAttributes is used by aggregate functions // includeIgnoreAttributes is used by aggregate functions
if (options.includeIgnoreAttributes !== false) { if (options.includeIgnoreAttributes !== false) {
...@@ -795,10 +801,20 @@ module.exports = (function() { ...@@ -795,10 +801,20 @@ module.exports = (function() {
if (Array.isArray(options.order)) { if (Array.isArray(options.order)) {
options.order.forEach(function (t) { options.order.forEach(function (t) {
var strOrder = this.quote(t, factory)
var asName = strOrder.match(/`(.*)`\./)
if (subQuery && !(t[0] instanceof daoFactory) && !(t[0].model instanceof daoFactory)) { if (subQuery && !(t[0] instanceof daoFactory) && !(t[0].model instanceof daoFactory)) {
subQueryOrder.push(this.quote(t, factory)) if(asName && subQueryAs.indexOf(asName[1]) > -1){
subQueryOrder.push(strOrder)
}
}
if(subQuery && asName && asName[1] != subQueryAs[0] && subQueryAs.indexOf(asName[1]) > -1){
mainQueryOrder.push(strOrder.replace(/`\.`/,'.'))
}else{
mainQueryOrder.push(strOrder)
} }
mainQueryOrder.push(this.quote(t, factory))
}.bind(this)) }.bind(this))
} else { } else {
mainQueryOrder.push(options.order) mainQueryOrder.push(options.order)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!