mirror of https://github.com/go-gorm/gorm.git
Fix overrite SELECT clause
This commit is contained in:
parent
50826742fd
commit
b5de8aeb42
|
@ -91,6 +91,7 @@ func (db *DB) Select(query interface{}, args ...interface{}) (tx *DB) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
delete(tx.Statement.Clauses, "SELECT")
|
||||||
case string:
|
case string:
|
||||||
fields := strings.FieldsFunc(v, utils.IsChar)
|
fields := strings.FieldsFunc(v, utils.IsChar)
|
||||||
|
|
||||||
|
@ -112,6 +113,8 @@ func (db *DB) Select(query interface{}, args ...interface{}) (tx *DB) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
delete(tx.Statement.Clauses, "SELECT")
|
||||||
} else {
|
} else {
|
||||||
tx.Statement.AddClause(clause.Select{
|
tx.Statement.AddClause(clause.Select{
|
||||||
Distinct: db.Statement.Distinct,
|
Distinct: db.Statement.Distinct,
|
||||||
|
|
|
@ -294,7 +294,7 @@ func (db *DB) Count(count *int64) (tx *DB) {
|
||||||
|
|
||||||
if len(tx.Statement.Selects) == 0 {
|
if len(tx.Statement.Selects) == 0 {
|
||||||
tx.Statement.AddClause(clause.Select{Expression: clause.Expr{SQL: "count(1)"}})
|
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(") {
|
} else if !strings.Contains(strings.ToLower(tx.Statement.Selects[0]), "count(") {
|
||||||
expr := clause.Expr{SQL: "count(1)"}
|
expr := clause.Expr{SQL: "count(1)"}
|
||||||
|
|
||||||
|
|
|
@ -346,6 +346,11 @@ func TestSelect(t *testing.T) {
|
||||||
if !regexp.MustCompile(`SELECT u\.\* FROM .*users.*`).MatchString(r.Statement.SQL.String()) {
|
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())
|
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) {
|
func TestOmit(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue