fix: support implicit table alias

This commit is contained in:
yaofeng-wang 2022-12-24 17:36:13 +08:00
parent 1935eb0adb
commit 0fb5abcfd3
No known key found for this signature in database
2 changed files with 6 additions and 1 deletions

View File

@ -55,7 +55,7 @@ func (db *DB) Clauses(conds ...clause.Expression) (tx *DB) {
return return
} }
var tableRegexp = regexp.MustCompile(`(?i).+? AS (\w+)\s*(?:$|,)`) var tableRegexp = regexp.MustCompile(`(?i).+?(?: AS )?(\w+)\s*(?:$|,)`)
// Table specify the table you would like to run db operations // Table specify the table you would like to run db operations
// //

View File

@ -32,6 +32,11 @@ func TestTable(t *testing.T) {
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) t.Errorf("Table with escape character, got %v", r.Statement.SQL.String())
} }
r = dryDB.Table("user u").Select("name").Find(&User{}).Statement
if !regexp.MustCompile("SELECT .name. FROM user u WHERE .u.\\..deleted_at. IS NULL").MatchString(r.Statement.SQL.String()) {
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String())
}
r = dryDB.Table("`people`").Table("`user`").Find(&User{}).Statement r = dryDB.Table("`people`").Table("`user`").Find(&User{}).Statement
if !regexp.MustCompile("SELECT \\* FROM `user`").MatchString(r.Statement.SQL.String()) { if !regexp.MustCompile("SELECT \\* FROM `user`").MatchString(r.Statement.SQL.String()) {
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) t.Errorf("Table with escape character, got %v", r.Statement.SQL.String())