Skip to content

Commit

Permalink
Fix Select with specific symbol, close go-gorm#3158
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jul 17, 2020
1 parent b8692c7 commit 58e3241
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 9 additions & 4 deletions tests/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 58e3241

Please sign in to comment.