Fix overrite SELECT clause

This commit is contained in:
Jinzhu 2020-08-18 18:58:53 +08:00
parent 50826742fd
commit b5de8aeb42
3 changed files with 9 additions and 1 deletions

View File

@ -91,6 +91,7 @@ func (db *DB) Select(query interface{}, args ...interface{}) (tx *DB) {
return
}
}
delete(tx.Statement.Clauses, "SELECT")
case string:
fields := strings.FieldsFunc(v, utils.IsChar)
@ -112,6 +113,8 @@ func (db *DB) Select(query interface{}, args ...interface{}) (tx *DB) {
return
}
}
delete(tx.Statement.Clauses, "SELECT")
} else {
tx.Statement.AddClause(clause.Select{
Distinct: db.Statement.Distinct,

View File

@ -294,7 +294,7 @@ func (db *DB) Count(count *int64) (tx *DB) {
if len(tx.Statement.Selects) == 0 {
tx.Statement.AddClause(clause.Select{Expression: clause.Expr{SQL: "count(1)"}})
defer tx.Statement.AddClause(clause.Select{})
defer delete(tx.Statement.Clauses, "SELECT")
} else if !strings.Contains(strings.ToLower(tx.Statement.Selects[0]), "count(") {
expr := clause.Expr{SQL: "count(1)"}

View File

@ -346,6 +346,11 @@ func TestSelect(t *testing.T) {
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())
}
r = dryDB.Select("count(*)").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 TestOmit(t *testing.T) {