From 9f1a7f53511168c0567b4b4b4f10ab7d21265174 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=9C=BB=E8=9C=93=E7=89=B9=E6=B4=BE=E5=91=98?= Date: Wed, 2 Jan 2019 21:32:08 +0800 Subject: [PATCH] optimize getColumnAsArray (#2196) --- scope.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/scope.go b/scope.go index 806ccb7d..90e16782 100644 --- a/scope.go +++ b/scope.go @@ -1309,6 +1309,7 @@ func (scope *Scope) autoIndex() *Scope { } func (scope *Scope) getColumnAsArray(columns []string, values ...interface{}) (results [][]interface{}) { + resultMap := make(map[string][]interface{}) for _, value := range values { indirectValue := indirect(reflect.ValueOf(value)) @@ -1327,7 +1328,10 @@ func (scope *Scope) getColumnAsArray(columns []string, values ...interface{}) (r } if hasValue { - results = append(results, result) + h := fmt.Sprint(result...) + if _, exist := resultMap[h]; !exist { + resultMap[h] = result + } } } case reflect.Struct: @@ -1342,11 +1346,16 @@ func (scope *Scope) getColumnAsArray(columns []string, values ...interface{}) (r } if hasValue { - results = append(results, result) + h := fmt.Sprint(result...) + if _, exist := resultMap[h]; !exist { + resultMap[h] = result + } } } } - + for _, v := range resultMap { + results = append(results, v) + } return }