gorm/search.go

150 lines
3.5 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{}
2016-02-16 17:48:26 +03:00
joinConditions []map[string]interface{}
initAttrs []interface{}
assignAttrs []interface{}
selects map[string]interface{}
omits []string
orders []string
preload []searchPreload
offset int64
limit int64
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
}
func (s *search) Limit(limit int64) *search {
2016-01-18 15:32:52 +03:00
s.limit = limit
2013-11-16 05:12:22 +04:00
return s
2013-11-15 18:29:42 +04:00
}
func (s *search) Offset(offset int64) *search {
2016-01-18 15:32:52 +03:00
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 {
2016-03-07 09:54:20 +03:00
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
}
2016-02-16 17:48:26 +03:00
func (s *search) Joins(query string, values ...interface{}) *search {
s.joinConditions = append(s.joinConditions, map[string]interface{}{"query": query, "args": values})
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
}
2016-03-07 09:54:20 +03: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:
2016-03-07 09:54:20 +03:00
s.db.AddError(ErrInvalidSQL)
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
}