From 39a4402472ca82fb946d42d9e6958d8ae71e1fed Mon Sep 17 00:00:00 2001 From: matematik7 Date: Mon, 14 Aug 2017 20:46:39 +0200 Subject: [PATCH 1/2] Do not always override select on pluck --- scope.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/scope.go b/scope.go index fda7f653..f63c31f3 100644 --- a/scope.go +++ b/scope.go @@ -924,14 +924,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() From c26a35564f51876422089df02f77d397a1723c44 Mon Sep 17 00:00:00 2001 From: matematik7 Date: Mon, 4 Sep 2017 18:12:20 +0200 Subject: [PATCH 2/2] Fix for quoted column names and add test --- query_test.go | 24 ++++++++++++++++++++++++ scope.go | 8 ++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/query_test.go b/query_test.go index def84e04..f95c89f1 100644 --- a/query_test.go +++ b/query_test.go @@ -665,3 +665,27 @@ func TestSelectWithArrayInput(t *testing.T) { t.Errorf("Should have selected both age and name") } } + +func TestPluckWithSelect(t *testing.T) { + DB.Save(&User{Name: "matematik7", Age: 25}) + + var userAges []string + err := DB.Model(&User{}).Where("age = ?", 25).Select("name || ' - ' || age as user_age").Pluck("user_age", &userAges).Error + if err != nil { + t.Error(err) + } + + if len(userAges) != 1 || userAges[0] != "matematik7 - 25" { + t.Errorf("Should correctly pluck with select, got: %s", userAges) + } + + userAges = userAges[:0] + err = DB.Model(&User{}).Where("age = ?", 25).Select("name || ' - ' || age as \"user_age\"").Pluck("user_age", &userAges).Error + if err != nil { + t.Error(err) + } + + if len(userAges) != 1 || userAges[0] != "matematik7 - 25" { + t.Errorf("Should correctly pluck with select, got: %s", userAges) + } +} diff --git a/scope.go b/scope.go index f63c31f3..5bbf936c 100644 --- a/scope.go +++ b/scope.go @@ -925,12 +925,16 @@ func (scope *Scope) initialize() *Scope { } func (scope *Scope) isQueryForColumn(query interface{}, column string) bool { - queryStr := fmt.Sprint(query) + queryStr := strings.ToLower(fmt.Sprint(query)) if queryStr == column { return true } - if strings.HasSuffix(strings.ToLower(queryStr), "as "+column) { + if strings.HasSuffix(queryStr, "as "+column) { + return true + } + + if strings.HasSuffix(queryStr, "as \""+column+"\"") { return true }