From 96128cdec3e07bfdf24ade74c6b8950927325203 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 27 Oct 2013 01:02:57 +0800 Subject: [PATCH] Fix where chain --- main.go | 2 +- orm_test.go | 32 +++++++++++++++++++++++++++++++- sql.go | 4 +--- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index cc4c1372..eaad72e3 100644 --- a/main.go +++ b/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 } diff --git a/orm_test.go b/orm_test.go index e32abf35..caddf5b6 100644 --- a/orm_test.go +++ b/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)) } } diff --git a/sql.go b/sql.go index e06b3070..2625c742 100644 --- a/sql.go +++ b/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 }