yay, where in works

This commit is contained in:
Jinzhu 2013-10-27 02:31:38 +08:00
parent d18203ee62
commit 4587a8a686
3 changed files with 23 additions and 2 deletions

View File

@ -3,7 +3,7 @@
Yet Another ORM library for Go, aims for developer friendly Yet Another ORM library for Go, aims for developer friendly
## TODO ## TODO
* Complex where query (= / > / < / <> / in) * Complex where query (in)
* Order * Order
* Limit * Limit
* Select * Select

View File

@ -180,4 +180,12 @@ func TestComplexWhere(t *testing.T) {
if len(users) != 2 { if len(users) != 2 {
t.Errorf("Should only found 2 users's birthday <= t2, but have %v", len(users)) t.Errorf("Should only found 2 users's birthday <= t2, but have %v", len(users))
} }
users = []User{}
a := db.Where("name in (?)", []string{"1", "3"}).Find(&users)
a.db.Query("SELECT * FROM users WHERE ( name in ($1) )", "'1', '2'")
if len(users) != 3 {
t.Errorf("Should only found 3 users's name in (1, 3), but have %v", len(users))
}
} }

13
sql.go
View File

@ -144,8 +144,21 @@ func (s *Orm) whereSql() (sql string) {
str := "( " + clause["query"].(string) + " )" str := "( " + clause["query"].(string) + " )"
args := clause["args"].([]interface{}) args := clause["args"].([]interface{})
for _, arg := range args { for _, arg := range args {
switch arg.(type) {
case []string, []int, []int64, []int32:
var temp_marks []string
for _ = range arg.([]string) {
temp_marks = append(temp_marks, "?")
}
str = strings.Replace(str, "?", strings.Join(temp_marks, ","), 1)
for _, a := range arg.([]string) {
str = strings.Replace(str, "?", s.addToVars(a), 1)
}
default:
str = strings.Replace(str, "?", s.addToVars(arg), 1) str = strings.Replace(str, "?", s.addToVars(arg), 1)
} }
}
conditions = append(conditions, str) conditions = append(conditions, str)
} }
} }