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
|
|
|
|
WhereConditions []map[string]interface{}
|
|
|
|
OrConditions []map[string]interface{}
|
|
|
|
NotConditions []map[string]interface{}
|
|
|
|
InitAttrs []interface{}
|
|
|
|
AssignAttrs []interface{}
|
|
|
|
HavingCondition map[string]interface{}
|
|
|
|
Orders []string
|
|
|
|
Joins string
|
2014-11-17 15:12:32 +03:00
|
|
|
Selects []map[string]interface{}
|
2015-02-01 18:19:29 +03:00
|
|
|
Preload map[string][]interface{}
|
2014-01-28 12:56:51 +04:00
|
|
|
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{
|
2015-02-01 18:19:29 +03:00
|
|
|
Preload: s.Preload,
|
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,
|
2014-11-17 15:12:32 +03:00
|
|
|
Selects: s.Selects,
|
2014-01-28 12:56:51 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-11-17 15:12:32 +03:00
|
|
|
func (s *search) selects(query interface{}, args ...interface{}) *search {
|
|
|
|
s.Selects = append(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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-02-01 18:19:29 +03:00
|
|
|
func (s *search) joins(query string) *search {
|
|
|
|
s.Joins = query
|
2013-11-17 09:22:09 +04:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2015-02-01 18:19:29 +03:00
|
|
|
func (s *search) preload(column string, values ...interface{}) *search {
|
|
|
|
if s.Preload == nil {
|
|
|
|
s.Preload = map[string][]interface{}{}
|
|
|
|
}
|
|
|
|
s.Preload[column] = values
|
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) {
|
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:
|
2013-11-16 11:01:31 +04:00
|
|
|
s.db.err(InvalidSql)
|
|
|
|
}
|
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
|
|
|
|
}
|