Make clone search conditions works

This commit is contained in:
Jinzhu 2013-11-16 09:12:22 +08:00
parent 2b4a4a06f0
commit 7912631be7
3 changed files with 84 additions and 24 deletions

View File

@ -3,67 +3,91 @@ package gorm
import "strconv" import "strconv"
type search struct { type search struct {
conditions map[string][]interface{} whereClause []map[string]interface{}
orders []string orClause []map[string]interface{}
selectStr string notClause []map[string]interface{}
offsetStr string initAttrs []interface{}
limitStr string assignAttrs []interface{}
specifiedTableName string orders []string
unscope bool selectStr string
offsetStr string
limitStr string
unscope bool
} }
func (s *search) addToCondition(typ string, value interface{}) { func (s *search) clone() *search {
s.conditions[typ] = append(s.conditions[typ], value) return &search{
whereClause: s.whereClause,
orClause: s.orClause,
notClause: s.notClause,
initAttrs: s.initAttrs,
assignAttrs: s.assignAttrs,
orders: s.orders,
selectStr: s.selectStr,
offsetStr: s.offsetStr,
limitStr: s.limitStr,
unscope: s.unscope,
}
} }
func (s *search) where(query string, values ...interface{}) { func (s *search) where(query interface{}, values ...interface{}) *search {
s.addToCondition("where", map[string]interface{}{"query": query, "args": values}) s.whereClause = append(s.whereClause, map[string]interface{}{"query": query, "args": values})
return s
} }
func (s *search) not(query string, values ...interface{}) { func (s *search) not(query interface{}, values ...interface{}) *search {
s.addToCondition("not", map[string]interface{}{"query": query, "args": values}) s.notClause = append(s.notClause, map[string]interface{}{"query": query, "args": values})
return s
} }
func (s *search) or(query string, values ...interface{}) { func (s *search) or(query interface{}, values ...interface{}) *search {
s.addToCondition("or", map[string]interface{}{"query": query, "args": values}) s.orClause = append(s.orClause, map[string]interface{}{"query": query, "args": values})
return s
} }
func (s *search) attrs(attrs ...interface{}) { func (s *search) attrs(attrs ...interface{}) *search {
s.addToCondition("attrs", toSearchableMap(attrs...)) s.initAttrs = append(s.initAttrs, toSearchableMap(attrs...))
return s
} }
func (s *search) assign(attrs ...interface{}) { func (s *search) assign(attrs ...interface{}) *search {
s.addToCondition("assign", toSearchableMap(attrs...)) s.assignAttrs = append(s.assignAttrs, toSearchableMap(attrs...))
return s
} }
func (s *search) order(value string, reorder ...bool) { func (s *search) order(value string, reorder ...bool) *search {
if len(reorder) > 0 && reorder[0] { if len(reorder) > 0 && reorder[0] {
s.orders = []string{value} s.orders = []string{value}
} else { } else {
s.orders = append(s.orders, value) s.orders = append(s.orders, value)
} }
return s
} }
func (s *search) selects(value interface{}) { func (s *search) selects(value interface{}) *search {
if str, err := getInterfaceAsString(value); err == nil { if str, err := getInterfaceAsString(value); err == nil {
s.selectStr = str s.selectStr = str
} }
return s
} }
func (s *search) limit(value interface{}) { func (s *search) limit(value interface{}) *search {
if str, err := getInterfaceAsString(value); err == nil { if str, err := getInterfaceAsString(value); err == nil {
s.limitStr = str s.limitStr = str
} }
return s
} }
func (s *search) offset(value interface{}) { func (s *search) offset(value interface{}) *search {
if str, err := getInterfaceAsString(value); err == nil { if str, err := getInterfaceAsString(value); err == nil {
s.offsetStr = str s.offsetStr = str
} }
return s
} }
func (s *search) unscoped() { func (s *search) unscoped() *search {
s.unscope = true s.unscope = true
return s
} }
func getInterfaceAsString(value interface{}) (str string, err error) { func getInterfaceAsString(value interface{}) (str string, err error) {

30
search_test.go Normal file
View File

@ -0,0 +1,30 @@
package gorm
import (
"reflect"
"testing"
)
func TestCloneSearch(t *testing.T) {
s := new(search)
s.where("name = ?", "jinzhu").order("name").attrs("name", "jinzhu").selects("name, age")
s1 := s.clone()
s1.where("age = ?", 20).order("age").attrs("email", "a@e.org").selects("email")
if reflect.DeepEqual(s.whereClause, s1.whereClause) {
t.Errorf("Where should be copied")
}
if reflect.DeepEqual(s.orders, s1.orders) {
t.Errorf("Order should be copied")
}
if reflect.DeepEqual(s.initAttrs, s1.initAttrs) {
t.Errorf("initAttrs should be copied")
}
if reflect.DeepEqual(s.selectStr, s1.selectStr) {
t.Errorf("selectStr should be copied")
}
}

View File

@ -3,6 +3,7 @@ package gorm
import ( import (
"bytes" "bytes"
"database/sql" "database/sql"
"fmt"
"reflect" "reflect"
"strconv" "strconv"
@ -97,3 +98,8 @@ func isBlank(value reflect.Value) bool {
} }
return false return false
} }
func debug(values ...interface{}) {
fmt.Println("*****************")
fmt.Println(values)
}