diff --git a/README.md b/README.md index 723389ec..64476405 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Yet Another ORM library for Go, aims for developer friendly ## TODO -* Complex where query (= / > / < / <> / in) +* Complex where query (in) * Order * Limit * Select diff --git a/orm_test.go b/orm_test.go index 1ecec736..f12d084b 100644 --- a/orm_test.go +++ b/orm_test.go @@ -180,4 +180,12 @@ func TestComplexWhere(t *testing.T) { if len(users) != 2 { 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)) + } } diff --git a/sql.go b/sql.go index 290327f6..d8857314 100644 --- a/sql.go +++ b/sql.go @@ -144,7 +144,20 @@ func (s *Orm) whereSql() (sql string) { str := "( " + clause["query"].(string) + " )" args := clause["args"].([]interface{}) for _, arg := range args { - str = strings.Replace(str, "?", s.addToVars(arg), 1) + + 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) + } } conditions = append(conditions, str) }