Yay, Or works

This commit is contained in:
Jinzhu 2013-10-27 12:31:51 +08:00
parent 44c53f6dd5
commit 2a08333bf6
2 changed files with 13 additions and 11 deletions

View File

@ -274,10 +274,15 @@ func TestOffset(t *testing.T) {
} }
} }
func TestWhereOr(t *testing.T) { func TestOrAndNot(t *testing.T) {
// var users []User var users []User
// db.Where("name = ?", "1").Or("name = ?", "3").Find(&users) db.Where("name = ?", "1").Or("name = ?", "3").Find(&users)
// if len(users) != 3 { if len(users) != 3 {
// t.Errorf("Should find three users with name 1 and 3") t.Errorf("Should find three users with name 1 and 3")
// } }
db.Where("name = ?", "3").Not("age = ?", 22).Find(&users)
if len(users) != 3 {
t.Errorf("Should find three users with name 1 and 3")
}
} }

7
sql.go
View File

@ -50,8 +50,6 @@ func (s *Orm) query(out interface{}) {
is_slice = true is_slice = true
dest_type = dest_out.Type().Elem() dest_type = dest_out.Type().Elem()
} }
debug(s.Sql)
debug(s.SqlVars)
rows, err := s.db.Query(s.Sql, s.SqlVars...) rows, err := s.db.Query(s.Sql, s.SqlVars...)
defer rows.Close() defer rows.Close()
@ -196,7 +194,7 @@ func (s *Orm) buildWhereCondition(clause map[string]interface{}) string {
func (s *Orm) whereSql() (sql string) { func (s *Orm) whereSql() (sql string) {
var primary_condiation string var primary_condiation string
var and_conditions, or_conditions, not_conditions []string var and_conditions, or_conditions []string
if !s.model.PrimaryKeyIsEmpty() { if !s.model.PrimaryKeyIsEmpty() {
primary_condiation = fmt.Sprintf("(%v = %v)", s.quote(s.model.PrimaryKeyDb()), s.addToVars(s.model.PrimaryKeyValue())) primary_condiation = fmt.Sprintf("(%v = %v)", s.quote(s.model.PrimaryKeyDb()), s.addToVars(s.model.PrimaryKeyValue()))
@ -215,7 +213,7 @@ func (s *Orm) whereSql() (sql string) {
} }
and_sql := strings.Join(and_conditions, " AND ") and_sql := strings.Join(and_conditions, " AND ")
or_sql := strings.Join(not_conditions, " OR ") or_sql := strings.Join(or_conditions, " OR ")
combined_conditions := and_sql combined_conditions := and_sql
if len(combined_conditions) > 0 { if len(combined_conditions) > 0 {
if len(or_sql) > 0 { if len(or_sql) > 0 {
@ -233,7 +231,6 @@ func (s *Orm) whereSql() (sql string) {
} else if len(combined_conditions) > 0 { } else if len(combined_conditions) > 0 {
sql = "WHERE " + combined_conditions sql = "WHERE " + combined_conditions
} }
debug(sql)
return return
} }