2013-11-15 18:29:42 +04:00
|
|
|
package gorm
|
|
|
|
|
2016-12-01 11:16:20 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
)
|
2013-11-16 04:22:04 +04:00
|
|
|
|
2013-11-15 18:29:42 +04:00
|
|
|
type search struct {
|
2015-07-22 06:00:20 +03:00
|
|
|
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{}
|
2015-07-22 06:00:20 +03:00
|
|
|
initAttrs []interface{}
|
|
|
|
assignAttrs []interface{}
|
|
|
|
selects map[string]interface{}
|
|
|
|
omits []string
|
2016-06-28 06:15:42 +03:00
|
|
|
orders []interface{}
|
2015-07-22 06:00:20 +03:00
|
|
|
preload []searchPreload
|
2016-06-21 06:13:33 +03:00
|
|
|
offset interface{}
|
|
|
|
limit interface{}
|
2015-07-22 06:00:20 +03:00
|
|
|
group string
|
|
|
|
tableName string
|
|
|
|
raw bool
|
|
|
|
Unscoped bool
|
2016-12-01 11:16:20 +03:00
|
|
|
ignoreOrderQuery 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
|
|
|
}
|
|
|
|
|
2016-06-28 06:15:42 +03:00
|
|
|
func (s *search) Order(value interface{}, reorder ...bool) *search {
|
2013-11-15 18:29:42 +04:00
|
|
|
if len(reorder) > 0 && reorder[0] {
|
2016-06-28 06:15:42 +03:00
|
|
|
s.orders = []interface{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if value != nil {
|
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
|
|
|
}
|
|
|
|
|
2016-12-01 11:16:20 +03:00
|
|
|
var distinctSQLRegexp = regexp.MustCompile(`(?i)distinct[^a-z]+[a-z]+`)
|
|
|
|
|
2015-03-12 10:50:38 +03:00
|
|
|
func (s *search) Select(query interface{}, args ...interface{}) *search {
|
2016-12-01 11:16:20 +03:00
|
|
|
if distinctSQLRegexp.MatchString(fmt.Sprint(query)) {
|
|
|
|
s.ignoreOrderQuery = true
|
|
|
|
}
|
|
|
|
|
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-06-21 06:13:33 +03:00
|
|
|
func (s *search) Limit(limit interface{}) *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
|
|
|
}
|
|
|
|
|
2016-06-21 06:13:33 +03:00
|
|
|
func (s *search) Offset(offset interface{}) *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 {
|
2015-07-22 06:00:20 +03:00
|
|
|
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) {
|
2014-10-22 19:33:13 +04:00
|
|
|
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-10-22 19:33:13 +04:00
|
|
|
|
2014-11-11 08:36:12 +03:00
|
|
|
if str == "-1" {
|
|
|
|
return ""
|
2014-10-22 19:33:13 +04:00
|
|
|
}
|
2013-11-16 04:22:04 +04:00
|
|
|
return
|
|
|
|
}
|