Fix where chain

This commit is contained in:
Jinzhu 2013-10-27 01:02:57 +08:00
parent 97fc5878e9
commit 96128cdec3
3 changed files with 33 additions and 5 deletions

View File

@ -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
}

View File

@ -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
View File

@ -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
}