From 58e32415449ac9e5184de006d00c83072b500a5c Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 17 Jul 2020 11:06:20 +0800 Subject: [PATCH] Fix Select with specific symbol, close #3158 --- tests/query_test.go | 13 +++++++++---- utils/utils.go | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/tests/query_test.go b/tests/query_test.go index 62005e3a..22807377 100644 --- a/tests/query_test.go +++ b/tests/query_test.go @@ -310,19 +310,24 @@ func TestSelect(t *testing.T) { dryDB := DB.Session(&gorm.Session{DryRun: true}) r := dryDB.Select("name", "age").Find(&User{}) if !regexp.MustCompile("SELECT .*name.*,.*age.* FROM .*users.*").MatchString(r.Statement.SQL.String()) { - t.Fatalf("Build NOT condition, but got %v", r.Statement.SQL.String()) + t.Fatalf("Build Select with strings, but got %v", r.Statement.SQL.String()) } r = dryDB.Select([]string{"name", "age"}).Find(&User{}) if !regexp.MustCompile("SELECT .*name.*,.*age.* FROM .*users.*").MatchString(r.Statement.SQL.String()) { - t.Fatalf("Build NOT condition, but got %v", r.Statement.SQL.String()) + t.Fatalf("Build Select with slice, but got %v", r.Statement.SQL.String()) } r = dryDB.Table("users").Select("COALESCE(age,?)", 42).Find(&User{}) - if !regexp.MustCompile("SELECT COALESCE\\(age,.*\\) FROM .*users.*").MatchString(r.Statement.SQL.String()) { - t.Fatalf("Build NOT condition, but got %v", r.Statement.SQL.String()) + if !regexp.MustCompile(`SELECT COALESCE\(age,.*\) FROM .*users.*`).MatchString(r.Statement.SQL.String()) { + t.Fatalf("Build Select with func, but got %v", r.Statement.SQL.String()) } // SELECT COALESCE(age,'42') FROM users; + + r = dryDB.Select("u.*").Table("users as u").First(&User{}, user.ID) + if !regexp.MustCompile(`SELECT u\.\* FROM .*users.*`).MatchString(r.Statement.SQL.String()) { + t.Fatalf("Build Select with u.*, but got %v", r.Statement.SQL.String()) + } } func TestPluckWithSelect(t *testing.T) { diff --git a/utils/utils.go b/utils/utils.go index 3d7e395b..e93f3055 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -30,7 +30,7 @@ func FileWithLineNum() string { } func IsChar(c rune) bool { - return !unicode.IsLetter(c) && !unicode.IsNumber(c) + return !unicode.IsLetter(c) && !unicode.IsNumber(c) && c != '.' && c != '*' } func CheckTruth(val interface{}) bool {