Quote order column name, fix #918

This commit is contained in:
Jinzhu 2016-03-23 10:29:52 +08:00
parent 7d5d171168
commit 2530dcbccd
1 changed files with 9 additions and 2 deletions

View File

@ -441,8 +441,10 @@ func (scope *Scope) callMethod(methodName string, reflectValue reflect.Value) {
} }
} }
var columnRegexp = regexp.MustCompile("^[a-zA-Z]+(\\.[a-zA-Z]+)*$") // only match string like `name`, `users.name`
func (scope *Scope) quoteIfPossible(str string) string { func (scope *Scope) quoteIfPossible(str string) string {
if regexp.MustCompile("^[a-zA-Z]+(.[a-zA-Z]+)*$").MatchString(str) { if columnRegexp.MatchString(str) {
return scope.Quote(str) return scope.Quote(str)
} }
return str return str
@ -724,7 +726,12 @@ func (scope *Scope) orderSQL() string {
if len(scope.Search.orders) == 0 || scope.Search.countingQuery { if len(scope.Search.orders) == 0 || scope.Search.countingQuery {
return "" return ""
} }
return " ORDER BY " + strings.Join(scope.Search.orders, ",")
var orders []string
for _, order := range scope.Search.orders {
orders = append(orders, scope.quoteIfPossible(order))
}
return " ORDER BY " + strings.Join(orders, ",")
} }
func (scope *Scope) limitAndOffsetSQL() string { func (scope *Scope) limitAndOffsetSQL() string {