mirror of https://github.com/go-gorm/gorm.git
Fix where chain
This commit is contained in:
parent
97fc5878e9
commit
96128cdec3
2
main.go
2
main.go
|
@ -24,7 +24,7 @@ func (s *DB) buildORM() *Orm {
|
|||
|
||||
func (s *DB) Where(querystring interface{}, args ...interface{}) (orm *Orm) {
|
||||
orm = s.buildORM()
|
||||
orm.Where(querystring, args)
|
||||
orm.Where(querystring, args...)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
32
orm_test.go
32
orm_test.go
|
@ -125,6 +125,36 @@ func TestComplexWhere(t *testing.T) {
|
|||
var users []User
|
||||
db.Where("age > ?", 20).Find(&users)
|
||||
if len(users) != 2 {
|
||||
t.Errorf("Should only have 2 users age great than 20, but have %v", len(users))
|
||||
t.Errorf("Should only found 2 users that age > 20, but have %v", len(users))
|
||||
}
|
||||
|
||||
users = []User{}
|
||||
db.Where("age >= ?", 20).Find(&users)
|
||||
if len(users) != 3 {
|
||||
t.Errorf("Should only found 2 users that age >= 20, but have %v", len(users))
|
||||
}
|
||||
|
||||
users = []User{}
|
||||
db.Where("age = ?", 20).Find(&users)
|
||||
if len(users) != 1 {
|
||||
t.Errorf("Should only found 1 users age == 20, but have %v", len(users))
|
||||
}
|
||||
|
||||
users = []User{}
|
||||
db.Where("age <> ?", 20).Find(&users)
|
||||
if len(users) < 3 {
|
||||
t.Errorf("Should have more than 3 users age != 20, but have %v", len(users))
|
||||
}
|
||||
|
||||
users = []User{}
|
||||
db.Where("name = ? and age >= ?", "3", 20).Find(&users)
|
||||
if len(users) != 2 {
|
||||
t.Errorf("Should only found 2 users that age >= 20 with name 3, but have %v", len(users))
|
||||
}
|
||||
|
||||
users = []User{}
|
||||
db.Where("name = ?", "3").Where("age >= ?", 20).Find(&users)
|
||||
if len(users) != 2 {
|
||||
t.Errorf("Should only found 2 users that age >= 20 with name 3, but have %v", len(users))
|
||||
}
|
||||
}
|
||||
|
|
4
sql.go
4
sql.go
|
@ -145,8 +145,7 @@ func (s *Orm) whereSql() (sql string) {
|
|||
str := "( " + clause["query"].(string) + " )"
|
||||
args := clause["args"].([]interface{})
|
||||
for _, arg := range args {
|
||||
s.SqlVars = append(s.SqlVars, arg.([]interface{})...)
|
||||
str = strings.Replace(str, "?", fmt.Sprintf("$%d", len(s.SqlVars)), 1)
|
||||
str = strings.Replace(str, "?", s.addToVars(arg), 1)
|
||||
}
|
||||
conditions = append(conditions, str)
|
||||
}
|
||||
|
@ -155,7 +154,6 @@ func (s *Orm) whereSql() (sql string) {
|
|||
if len(conditions) > 0 {
|
||||
sql = "WHERE " + strings.Join(conditions, " AND ")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue