mirror of https://github.com/go-gorm/gorm.git
Update search.go
This commit is contained in:
parent
85109e104e
commit
2b4a4a06f0
40
search.go
40
search.go
|
@ -1,5 +1,7 @@
|
||||||
package gorm
|
package gorm
|
||||||
|
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
type search struct {
|
type search struct {
|
||||||
conditions map[string][]interface{}
|
conditions map[string][]interface{}
|
||||||
orders []string
|
orders []string
|
||||||
|
@ -10,31 +12,31 @@ type search struct {
|
||||||
unscope bool
|
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)
|
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})
|
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})
|
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})
|
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...))
|
s.addToCondition("attrs", toSearchableMap(attrs...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s search) assign(attrs ...interface{}) {
|
func (s *search) assign(attrs ...interface{}) {
|
||||||
s.addToCondition("assign", toSearchableMap(attrs...))
|
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] {
|
if len(reorder) > 0 && reorder[0] {
|
||||||
s.orders = []string{value}
|
s.orders = []string{value}
|
||||||
} else {
|
} 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 {
|
if str, err := getInterfaceAsString(value); err == nil {
|
||||||
s.selectStr = str
|
s.selectStr = str
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s search) limit(value interface{}) {
|
func (s *search) limit(value interface{}) {
|
||||||
if str, err := getInterfaceAsString(value); err == nil {
|
if str, err := getInterfaceAsString(value); err == nil {
|
||||||
s.limitStr = str
|
s.limitStr = str
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s search) offset(value interface{}) {
|
func (s *search) offset(value interface{}) {
|
||||||
if str, err := getInterfaceAsString(value); err == nil {
|
if str, err := getInterfaceAsString(value); err == nil {
|
||||||
s.offsetStr = str
|
s.offsetStr = str
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s search) unscoped() {
|
func (s *search) unscoped() {
|
||||||
s.unscope = true
|
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
|
||||||
|
}
|
||||||
|
|
19
utils.go
19
utils.go
|
@ -3,8 +3,7 @@ package gorm
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -50,22 +49,6 @@ func toSearchableMap(attrs ...interface{}) (result interface{}) {
|
||||||
return
|
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 {
|
func setFieldValue(field reflect.Value, value interface{}) bool {
|
||||||
if field.IsValid() && field.CanAddr() {
|
if field.IsValid() && field.CanAddr() {
|
||||||
switch field.Kind() {
|
switch field.Kind() {
|
||||||
|
|
Loading…
Reference in New Issue