From 2b4a4a06f0ff83ee1810ddf2e1c4bc8f2cf7955f Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sat, 16 Nov 2013 08:22:04 +0800 Subject: [PATCH] Update search.go --- search.go | 40 +++++++++++++++++++++++++++++----------- utils.go | 19 +------------------ 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/search.go b/search.go index db32bd11..84de4a40 100644 --- a/search.go +++ b/search.go @@ -1,5 +1,7 @@ package gorm +import "strconv" + type search struct { conditions map[string][]interface{} orders []string @@ -10,31 +12,31 @@ type search struct { unscope bool } -func (s search) addToCondition(typ string, value interface{}) { +func (s *search) addToCondition(typ string, value interface{}) { s.conditions[typ] = append(s.conditions[typ], value) } -func (s search) where(query string, values ...interface{}) { +func (s *search) where(query string, values ...interface{}) { s.addToCondition("where", map[string]interface{}{"query": query, "args": values}) } -func (s search) not(query string, values ...interface{}) { +func (s *search) not(query string, values ...interface{}) { s.addToCondition("not", map[string]interface{}{"query": query, "args": values}) } -func (s search) or(query string, values ...interface{}) { +func (s *search) or(query string, values ...interface{}) { s.addToCondition("or", map[string]interface{}{"query": query, "args": values}) } -func (s search) attrs(attrs ...interface{}) { +func (s *search) attrs(attrs ...interface{}) { s.addToCondition("attrs", toSearchableMap(attrs...)) } -func (s search) assign(attrs ...interface{}) { +func (s *search) assign(attrs ...interface{}) { s.addToCondition("assign", toSearchableMap(attrs...)) } -func (s search) order(value string, reorder ...bool) { +func (s *search) order(value string, reorder ...bool) { if len(reorder) > 0 && reorder[0] { s.orders = []string{value} } else { @@ -42,24 +44,40 @@ func (s search) order(value string, reorder ...bool) { } } -func (s search) selects(value interface{}) { +func (s *search) selects(value interface{}) { if str, err := getInterfaceAsString(value); err == nil { s.selectStr = str } } -func (s search) limit(value interface{}) { +func (s *search) limit(value interface{}) { if str, err := getInterfaceAsString(value); err == nil { s.limitStr = str } } -func (s search) offset(value interface{}) { +func (s *search) offset(value interface{}) { if str, err := getInterfaceAsString(value); err == nil { s.offsetStr = str } } -func (s search) unscoped() { +func (s *search) unscoped() { s.unscope = true } + +func getInterfaceAsString(value interface{}) (str string, err error) { + switch value := value.(type) { + case string: + str = value + case int: + if value < 0 { + str = "" + } else { + str = strconv.Itoa(value) + } + default: + err = InvalidSql + } + return +} diff --git a/utils.go b/utils.go index ea39a11a..d4897c13 100644 --- a/utils.go +++ b/utils.go @@ -3,8 +3,7 @@ package gorm import ( "bytes" "database/sql" - "errors" - "fmt" + "reflect" "strconv" "strings" @@ -50,22 +49,6 @@ func toSearchableMap(attrs ...interface{}) (result interface{}) { return } -func getInterfaceAsString(value interface{}) (str string, err error) { - switch value := value.(type) { - case string: - str = value - case int: - if value < 0 { - str = "" - } else { - str = strconv.Itoa(value) - } - default: - err = errors.New(fmt.Sprintf("Can't understand %v", value)) - } - return -} - func setFieldValue(field reflect.Value, value interface{}) bool { if field.IsValid() && field.CanAddr() { switch field.Kind() {