Fix Select with specific symbol, close #3158

This commit is contained in:
Jinzhu 2020-07-17 11:06:20 +08:00
parent b8692c7671
commit 58e3241544
2 changed files with 10 additions and 5 deletions

View File

@ -310,19 +310,24 @@ func TestSelect(t *testing.T) {
dryDB := DB.Session(&gorm.Session{DryRun: true}) dryDB := DB.Session(&gorm.Session{DryRun: true})
r := dryDB.Select("name", "age").Find(&User{}) r := dryDB.Select("name", "age").Find(&User{})
if !regexp.MustCompile("SELECT .*name.*,.*age.* FROM .*users.*").MatchString(r.Statement.SQL.String()) { 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{}) r = dryDB.Select([]string{"name", "age"}).Find(&User{})
if !regexp.MustCompile("SELECT .*name.*,.*age.* FROM .*users.*").MatchString(r.Statement.SQL.String()) { 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{}) r = dryDB.Table("users").Select("COALESCE(age,?)", 42).Find(&User{})
if !regexp.MustCompile("SELECT COALESCE\\(age,.*\\) FROM .*users.*").MatchString(r.Statement.SQL.String()) { 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()) t.Fatalf("Build Select with func, but got %v", r.Statement.SQL.String())
} }
// SELECT COALESCE(age,'42') FROM users; // 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) { func TestPluckWithSelect(t *testing.T) {

View File

@ -30,7 +30,7 @@ func FileWithLineNum() string {
} }
func IsChar(c rune) bool { 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 { func CheckTruth(val interface{}) bool {