Skip to content

Commit bf36239

Browse files
committed
Update query-generator.js
1 parent 1d104c1 commit bf36239

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

lib/dialects/abstract/query-generator.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,38 @@ module.exports = (function() {
407407
}
408408
},
409409

410+
getTableNameOrder: function (obj){
411+
if (Utils._.isString(obj)) {
412+
return obj
413+
} else if (Array.isArray(obj)) {
414+
return this.getTableNameOrder(obj[0])
415+
} else if (obj instanceof Utils.fn) {
416+
return this.getTableNameOrder(obj.args)
417+
} else if (obj instanceof Utils.col) {
418+
return obj.col.split('.')[0]
419+
} else if ('raw' in obj){
420+
return obj.raw
421+
} else{
422+
return ''
423+
}
424+
},
425+
426+
getSpecificQuoteOrder: function (obj){
427+
if (Utils._.isString(obj)) {
428+
return this.quoteIdentifier(obj)
429+
} else if (Array.isArray(obj)) {
430+
return this.getSpecificQuoteOrder(obj[0]) + (obj.length > 1 ? ' ' + obj[1] : '')
431+
} else if (obj instanceof Utils.fn) {
432+
return obj.fn + '(' + this.getSpecificQuoteOrder(obj.args) + ')'
433+
} else if (obj instanceof Utils.col) {
434+
return this.quoteIdentifier(obj.col)
435+
} else if ('raw' in obj){
436+
return obj.raw
437+
} else{
438+
return ''
439+
}
440+
},
441+
410442
/*
411443
Create a trigger
412444
*/
@@ -802,16 +834,15 @@ module.exports = (function() {
802834
if (Array.isArray(options.order)) {
803835
options.order.forEach(function (t) {
804836
var strOrder = this.quote(t, factory)
805-
var asName = strOrder.match(/`(.*)`\./)
806-
837+
var tableName = this.getTableNameOrder(t)
807838
if (subQuery && !(t[0] instanceof daoFactory) && !(t[0].model instanceof daoFactory)) {
808-
if(asName && subQueryAs.indexOf(asName[1]) > -1){
839+
if(tableName && subQueryAs.indexOf(tableName) > -1){
809840
subQueryOrder.push(strOrder)
810-
}
841+
}
811842
}
812843

813-
if(subQuery && asName && asName[1] != subQueryAs[0] && subQueryAs.indexOf(asName[1]) > -1){
814-
mainQueryOrder.push(strOrder.replace(/`\.`/,'.'))
844+
if(subQuery && tableName !== subQueryAs[0] && subQueryAs.indexOf(tableName) > -1){
845+
mainQueryOrder.push(this.getSpecificQuoteOrder(t))
815846
}else{
816847
mainQueryOrder.push(strOrder)
817848
}

0 commit comments

Comments
 (0)