gorm/search.go

149 lines
3.3 KiB
Go
Raw Normal View History

2013-11-15 18:29:42 +04:00
package gorm
2013-11-16 11:01:31 +04:00
import (
"regexp"
"strconv"
)
2013-11-16 04:22:04 +04:00
2013-11-15 18:29:42 +04:00
type search struct {
2014-01-28 12:56:51 +04:00
db *DB
WhereConditions []map[string]interface{}
OrConditions []map[string]interface{}
NotConditions []map[string]interface{}
InitAttrs []interface{}
AssignAttrs []interface{}
HavingCondition map[string]interface{}
Orders []string
Joins string
Select string
Offset string
Limit string
Group string
TableName string
Unscope bool
Raw bool
2013-11-15 18:29:42 +04:00
}
2013-11-16 05:12:22 +04:00
func (s *search) clone() *search {
return &search{
2014-01-28 12:56:51 +04:00
WhereConditions: s.WhereConditions,
OrConditions: s.OrConditions,
NotConditions: s.NotConditions,
InitAttrs: s.InitAttrs,
AssignAttrs: s.AssignAttrs,
HavingCondition: s.HavingCondition,
Orders: s.Orders,
Select: s.Select,
Offset: s.Offset,
Limit: s.Limit,
Unscope: s.Unscope,
Group: s.Group,
Joins: s.Joins,
TableName: s.TableName,
Raw: s.Raw,
2013-11-16 05:12:22 +04:00
}
2013-11-15 18:29:42 +04:00
}
2013-11-16 05:12:22 +04:00
func (s *search) where(query interface{}, values ...interface{}) *search {
2014-01-28 12:56:51 +04:00
s.WhereConditions = append(s.WhereConditions, map[string]interface{}{"query": query, "args": values})
2013-11-16 05:12:22 +04:00
return s
2013-11-15 18:29:42 +04:00
}
2013-11-16 05:12:22 +04:00
func (s *search) not(query interface{}, values ...interface{}) *search {
2014-01-28 12:56:51 +04:00
s.NotConditions = append(s.NotConditions, map[string]interface{}{"query": query, "args": values})
2013-11-16 05:12:22 +04:00
return s
2013-11-15 18:29:42 +04:00
}
2013-11-16 05:12:22 +04:00
func (s *search) or(query interface{}, values ...interface{}) *search {
2014-01-28 12:56:51 +04:00
s.OrConditions = append(s.OrConditions, map[string]interface{}{"query": query, "args": values})
2013-11-16 05:12:22 +04:00
return s
2013-11-15 18:29:42 +04:00
}
2013-11-16 05:12:22 +04:00
func (s *search) attrs(attrs ...interface{}) *search {
2014-01-28 12:56:51 +04:00
s.InitAttrs = append(s.InitAttrs, toSearchableMap(attrs...))
2013-11-16 05:12:22 +04:00
return s
2013-11-15 18:29:42 +04:00
}
2013-11-16 05:12:22 +04:00
func (s *search) assign(attrs ...interface{}) *search {
2014-01-28 12:56:51 +04:00
s.AssignAttrs = append(s.AssignAttrs, toSearchableMap(attrs...))
2013-11-16 05:12:22 +04:00
return s
2013-11-15 18:29:42 +04:00
}
2013-11-16 05:12:22 +04:00
func (s *search) order(value string, reorder ...bool) *search {
2013-11-15 18:29:42 +04:00
if len(reorder) > 0 && reorder[0] {
2014-01-28 12:56:51 +04:00
s.Orders = []string{value}
2013-11-15 18:29:42 +04:00
} else {
2014-01-28 12:56:51 +04:00
s.Orders = append(s.Orders, value)
2013-11-15 18:29:42 +04:00
}
2013-11-16 05:12:22 +04:00
return s
2013-11-15 18:29:42 +04:00
}
2013-11-16 05:12:22 +04:00
func (s *search) selects(value interface{}) *search {
2014-01-28 12:56:51 +04:00
s.Select = s.getInterfaceAsSql(value)
2013-11-16 05:12:22 +04:00
return s
2013-11-15 18:29:42 +04:00
}
2013-11-16 05:12:22 +04:00
func (s *search) limit(value interface{}) *search {
2014-01-28 12:56:51 +04:00
s.Limit = s.getInterfaceAsSql(value)
2013-11-16 05:12:22 +04:00
return s
2013-11-15 18:29:42 +04:00
}
2013-11-16 05:12:22 +04:00
func (s *search) offset(value interface{}) *search {
2014-01-28 12:56:51 +04:00
s.Offset = s.getInterfaceAsSql(value)
2013-11-16 05:12:22 +04:00
return s
2013-11-15 18:29:42 +04:00
}
2013-11-17 09:22:09 +04:00
func (s *search) group(query string) *search {
2014-01-28 12:56:51 +04:00
s.Group = s.getInterfaceAsSql(query)
2013-11-17 09:22:09 +04:00
return s
}
func (s *search) having(query string, values ...interface{}) *search {
2014-01-28 12:56:51 +04:00
s.HavingCondition = map[string]interface{}{"query": query, "args": values}
2013-11-17 09:22:09 +04:00
return s
}
func (s *search) includes(value interface{}) *search {
return s
}
func (s *search) joins(query string) *search {
2014-01-28 12:56:51 +04:00
s.Joins = query
2013-11-17 09:22:09 +04:00
return s
}
2014-01-28 12:56:51 +04:00
func (s *search) raw(b bool) *search {
s.Raw = b
2014-01-03 14:14:51 +04:00
return s
}
2013-11-16 05:12:22 +04:00
func (s *search) unscoped() *search {
2014-01-28 12:56:51 +04:00
s.Unscope = true
2013-11-16 05:12:22 +04:00
return s
2013-11-15 18:29:42 +04:00
}
2013-11-16 04:22:04 +04:00
2013-11-16 07:36:30 +04:00
func (s *search) table(name string) *search {
2014-01-28 12:56:51 +04:00
s.TableName = name
2013-11-16 07:36:30 +04:00
return s
}
2013-11-16 11:01:31 +04:00
func (s *search) getInterfaceAsSql(value interface{}) (str string) {
2013-11-16 04:22:04 +04:00
switch value := value.(type) {
case string:
str = value
case int:
if value < 0 {
str = ""
} else {
str = strconv.Itoa(value)
}
default:
2013-11-16 11:01:31 +04:00
s.db.err(InvalidSql)
}
if !regexp.MustCompile("^\\s*[\\w\\s,.*()]*\\s*$").MatchString(str) {
s.db.err(InvalidSql)
2013-11-16 04:22:04 +04:00
}
return
}