Do not always override select on pluck

This commit is contained in:
matematik7 2017-08-14 20:46:39 +02:00 committed by Jinzhu
parent 4c93473b2d
commit 841ea1bde5
1 changed files with 17 additions and 1 deletions

View File

@ -938,14 +938,30 @@ func (scope *Scope) initialize() *Scope {
return scope
}
func (scope *Scope) isQueryForColumn(query interface{}, column string) bool {
queryStr := fmt.Sprint(query)
if queryStr == column {
return true
}
if strings.HasSuffix(strings.ToLower(queryStr), "as "+column) {
return true
}
return false
}
func (scope *Scope) pluck(column string, value interface{}) *Scope {
dest := reflect.Indirect(reflect.ValueOf(value))
scope.Search.Select(column)
if dest.Kind() != reflect.Slice {
scope.Err(fmt.Errorf("results should be a slice, not %s", dest.Kind()))
return scope
}
if query, ok := scope.Search.selects["query"]; !ok || !scope.isQueryForColumn(query, column) {
scope.Search.Select(column)
}
rows, err := scope.rows()
if scope.Err(err) == nil {
defer rows.Close()