gorm/search.go

150 lines
3.4 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 {
db *DB
whereConditions []map[string]interface{}
orConditions []map[string]interface{}
notConditions []map[string]interface{}
havingConditions []map[string]interface{}
initAttrs []interface{}
assignAttrs []interface{}
selects map[string]interface{}
omits []string
orders []string
joins string
preload []searchPreload
2016-01-18 15:32:52 +03:00
offset int
limit int
group string
tableName string
raw bool
Unscoped bool
2015-08-05 10:58:11 +03:00
countingQuery bool
2013-11-15 18:29:42 +04:00
}
2015-04-21 10:00:36 +03:00
type searchPreload struct {
schema string
conditions []interface{}
}
2013-11-16 05:12:22 +04:00
func (s *search) clone() *search {
2015-04-16 12:36:55 +03:00
clone := *s
return &clone
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-08-01 06:31:36 +03:00
if value != "" {
s.orders = []string{value}
} else {
s.orders = []string{}
}
} else if value != "" {
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
}
2016-01-18 15:32:52 +03:00
func (s *search) Limit(limit int) *search {
s.limit = limit
2013-11-16 05:12:22 +04:00
return s
2013-11-15 18:29:42 +04:00
}
2016-01-18 15:32:52 +03:00
func (s *search) Offset(offset int) *search {
s.offset = offset
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.havingConditions = append(s.havingConditions, 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-04-21 10:00:36 +03:00
func (s *search) Preload(schema string, values ...interface{}) *search {
var preloads []searchPreload
for _, preload := range s.preload {
if preload.schema != schema {
preloads = append(preloads, preload)
}
2015-02-01 18:19:29 +03:00
}
2015-04-21 10:00:36 +03:00
preloads = append(preloads, searchPreload{schema, values})
s.preload = preloads
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:
2015-08-13 11:42:13 +03:00
s.db.AddError(InvalidSql)
2013-11-16 11:01:31 +04:00
}
2014-11-11 08:36:12 +03:00
if str == "-1" {
return ""
}
2013-11-16 04:22:04 +04:00
return
}