gorm/search.go

153 lines
3.6 KiB
Go
Raw Normal View History

2013-11-15 18:29:42 +04:00
package gorm
2014-11-11 08:36:12 +03:00
import "fmt"
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
2015-03-12 08:52:29 +03:00
whereConditions []map[string]interface{}
orConditions []map[string]interface{}
notConditions []map[string]interface{}
havingCondition map[string]interface{}
initAttrs []interface{}
assignAttrs []interface{}
selects map[string]interface{}
2015-03-12 10:50:38 +03:00
omits []string
2015-03-12 08:52:29 +03:00
orders []string
joins string
preload map[string][]interface{}
offset string
limit string
group string
tableName string
raw bool
2015-03-12 10:50:38 +03:00
Unscoped bool
2013-11-15 18:29:42 +04:00
}
2013-11-16 05:12:22 +04:00
func (s *search) clone() *search {
return &search{
2015-03-12 08:52:29 +03:00
preload: s.preload,
whereConditions: s.whereConditions,
orConditions: s.orConditions,
notConditions: s.notConditions,
havingCondition: s.havingCondition,
initAttrs: s.initAttrs,
assignAttrs: s.assignAttrs,
selects: s.selects,
2015-03-12 10:50:38 +03:00
omits: s.omits,
2015-03-12 08:52:29 +03:00
orders: s.orders,
joins: s.joins,
offset: s.offset,
limit: s.limit,
group: s.group,
tableName: s.tableName,
raw: s.raw,
2015-03-12 10:50:38 +03:00
Unscoped: s.Unscoped,
2013-11-16 05:12:22 +04:00
}
2013-11-15 18:29:42 +04:00
}
2015-03-12 08:52:29 +03:00
func (s *search) Where(query interface{}, values ...interface{}) *search {
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
}
2015-03-12 08:52:29 +03:00
func (s *search) Not(query interface{}, values ...interface{}) *search {
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
}
2015-03-12 08:52:29 +03:00
func (s *search) Or(query interface{}, values ...interface{}) *search {
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
}
2015-03-12 08:52:29 +03:00
func (s *search) Attrs(attrs ...interface{}) *search {
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
}
2015-03-12 08:52:29 +03:00
func (s *search) Assign(attrs ...interface{}) *search {
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
}
2015-03-12 08:52:29 +03:00
func (s *search) Order(value string, reorder ...bool) *search {
2013-11-15 18:29:42 +04:00
if len(reorder) > 0 && reorder[0] {
2015-03-12 08:52:29 +03:00
s.orders = []string{value}
2013-11-15 18:29:42 +04:00
} else {
2015-03-12 08:52:29 +03: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
}
2015-03-12 10:50:38 +03:00
func (s *search) Select(query interface{}, args ...interface{}) *search {
2015-03-12 08:52:29 +03:00
s.selects = map[string]interface{}{"query": query, "args": args}
2013-11-16 05:12:22 +04:00
return s
2013-11-15 18:29:42 +04:00
}
2015-03-12 10:50:38 +03:00
func (s *search) Omit(columns ...string) *search {
s.omits = columns
return s
}
2015-03-12 08:52:29 +03:00
func (s *search) Limit(value interface{}) *search {
s.limit = s.getInterfaceAsSql(value)
2013-11-16 05:12:22 +04:00
return s
2013-11-15 18:29:42 +04:00
}
2015-03-12 08:52:29 +03:00
func (s *search) Offset(value interface{}) *search {
s.offset = s.getInterfaceAsSql(value)
2013-11-16 05:12:22 +04:00
return s
2013-11-15 18:29:42 +04:00
}
2015-03-12 08:52:29 +03:00
func (s *search) Group(query string) *search {
s.group = s.getInterfaceAsSql(query)
2013-11-17 09:22:09 +04:00
return s
}
2015-03-12 08:52:29 +03:00
func (s *search) Having(query string, values ...interface{}) *search {
s.havingCondition = map[string]interface{}{"query": query, "args": values}
2013-11-17 09:22:09 +04:00
return s
}
2015-03-12 08:52:29 +03:00
func (s *search) Joins(query string) *search {
s.joins = query
2013-11-17 09:22:09 +04:00
return s
}
2015-03-12 08:52:29 +03:00
func (s *search) Preload(column string, values ...interface{}) *search {
if s.preload == nil {
s.preload = map[string][]interface{}{}
2015-02-01 18:19:29 +03:00
}
2015-03-12 08:52:29 +03:00
s.preload[column] = values
2013-11-17 09:22:09 +04:00
return s
}
2015-03-12 08:52:29 +03:00
func (s *search) Raw(b bool) *search {
s.raw = b
2014-01-03 14:14:51 +04:00
return s
}
2015-03-12 10:01:59 +03:00
func (s *search) unscoped() *search {
s.Unscoped = 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
2015-03-12 08:52:29 +03:00
func (s *search) Table(name string) *search {
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) {
switch value.(type) {
2014-11-11 08:36:12 +03:00
case string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
str = fmt.Sprintf("%v", value)
2013-11-16 04:22:04 +04:00
default:
2013-11-16 11:01:31 +04:00
s.db.err(InvalidSql)
}
2014-11-11 08:36:12 +03:00
if str == "-1" {
return ""
}
2013-11-16 04:22:04 +04:00
return
}