From 46269198a4e50bbffb5682321fe5865836dd17b9 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 11 Feb 2018 13:41:46 +0800 Subject: [PATCH] Refactor PR #1569 --- query_test.go | 23 ++++++++++++++++++----- scope.go | 2 +- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/query_test.go b/query_test.go index df8893fd..135805a7 100644 --- a/query_test.go +++ b/query_test.go @@ -2,6 +2,7 @@ package gorm_test import ( "fmt" + "os" "reflect" "github.com/jinzhu/gorm" @@ -676,25 +677,37 @@ func TestSelectWithArrayInput(t *testing.T) { } func TestPluckWithSelect(t *testing.T) { - DB.Save(&User{Name: "matematik7", Age: 25}) + var ( + user = User{Name: "matematik7_pluck_with_select", Age: 25} + combinedName = fmt.Sprintf("%v%v", user.Name, user.Age) + combineUserAgeSQL = fmt.Sprintf("concat(%v, %v)", DB.Dialect().Quote("name"), DB.Dialect().Quote("age")) + ) + if dialect := os.Getenv("GORM_DIALECT"); dialect == "sqlite" { + combineUserAgeSQL = fmt.Sprintf("(%v || %v)", DB.Dialect().Quote("name"), DB.Dialect().Quote("age")) + } + + DB.Save(&user) + + selectStr := combineUserAgeSQL + " as user_age" var userAges []string - err := DB.Model(&User{}).Where("age = ?", 25).Select("name || ' - ' || age as user_age").Pluck("user_age", &userAges).Error + err := DB.Model(&User{}).Where("age = ?", 25).Select(selectStr).Pluck("user_age", &userAges).Error if err != nil { t.Error(err) } - if len(userAges) != 1 || userAges[0] != "matematik7 - 25" { + if len(userAges) != 1 || userAges[0] != combinedName { t.Errorf("Should correctly pluck with select, got: %s", userAges) } + selectStr = combineUserAgeSQL + fmt.Sprintf(" as %v", DB.Dialect().Quote("user_age")) userAges = userAges[:0] - err = DB.Model(&User{}).Where("age = ?", 25).Select("name || ' - ' || age as \"user_age\"").Pluck("user_age", &userAges).Error + err = DB.Model(&User{}).Where("age = ?", 25).Select(selectStr).Pluck("user_age", &userAges).Error if err != nil { t.Error(err) } - if len(userAges) != 1 || userAges[0] != "matematik7 - 25" { + if len(userAges) != 1 || userAges[0] != combinedName { t.Errorf("Should correctly pluck with select, got: %s", userAges) } } diff --git a/scope.go b/scope.go index 65ac62d9..29508d8d 100644 --- a/scope.go +++ b/scope.go @@ -948,7 +948,7 @@ func (scope *Scope) isQueryForColumn(query interface{}, column string) bool { return true } - if strings.HasSuffix(queryStr, "as \""+column+"\"") { + if strings.HasSuffix(queryStr, "as "+scope.Quote(column)) { return true }