2013-11-15 18:29:42 +04:00
|
|
|
package gorm
|
|
|
|
|
2013-11-16 11:01:31 +04:00
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
)
|
2013-11-16 04:22:04 +04:00
|
|
|
|
2013-11-15 18:29:42 +04:00
|
|
|
type search struct {
|
2013-11-17 09:22:09 +04:00
|
|
|
db *DB
|
|
|
|
whereClause []map[string]interface{}
|
|
|
|
orClause []map[string]interface{}
|
|
|
|
notClause []map[string]interface{}
|
|
|
|
initAttrs []interface{}
|
|
|
|
assignAttrs []interface{}
|
|
|
|
havingClause map[string]interface{}
|
|
|
|
orders []string
|
|
|
|
joinsStr string
|
|
|
|
selectStr string
|
|
|
|
offsetStr string
|
|
|
|
limitStr string
|
|
|
|
groupStr string
|
|
|
|
tableName string
|
|
|
|
unscope bool
|
2013-11-15 18:29:42 +04:00
|
|
|
}
|
|
|
|
|
2013-11-16 05:12:22 +04:00
|
|
|
func (s *search) clone() *search {
|
|
|
|
return &search{
|
2013-11-17 09:22:09 +04:00
|
|
|
whereClause: s.whereClause,
|
|
|
|
orClause: s.orClause,
|
|
|
|
notClause: s.notClause,
|
|
|
|
initAttrs: s.initAttrs,
|
|
|
|
assignAttrs: s.assignAttrs,
|
|
|
|
havingClause: s.havingClause,
|
|
|
|
orders: s.orders,
|
|
|
|
selectStr: s.selectStr,
|
|
|
|
offsetStr: s.offsetStr,
|
|
|
|
limitStr: s.limitStr,
|
|
|
|
unscope: s.unscope,
|
|
|
|
groupStr: s.groupStr,
|
|
|
|
joinsStr: s.joinsStr,
|
|
|
|
tableName: s.tableName,
|
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 {
|
|
|
|
s.whereClause = append(s.whereClause, map[string]interface{}{"query": query, "args": values})
|
|
|
|
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 {
|
|
|
|
s.notClause = append(s.notClause, map[string]interface{}{"query": query, "args": values})
|
|
|
|
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 {
|
|
|
|
s.orClause = append(s.orClause, map[string]interface{}{"query": query, "args": values})
|
|
|
|
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 {
|
|
|
|
s.initAttrs = append(s.initAttrs, toSearchableMap(attrs...))
|
|
|
|
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 {
|
|
|
|
s.assignAttrs = append(s.assignAttrs, toSearchableMap(attrs...))
|
|
|
|
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] {
|
|
|
|
s.orders = []string{value}
|
|
|
|
} else {
|
|
|
|
s.orders = append(s.orders, 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) selects(value interface{}) *search {
|
2013-11-16 11:01:31 +04:00
|
|
|
s.selectStr = 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) limit(value interface{}) *search {
|
2013-11-16 11:01:31 +04:00
|
|
|
s.limitStr = 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 {
|
2013-11-16 11:01:31 +04:00
|
|
|
s.offsetStr = 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 {
|
|
|
|
s.groupStr = s.getInterfaceAsSql(query)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *search) having(query string, values ...interface{}) *search {
|
|
|
|
s.havingClause = map[string]interface{}{"query": query, "args": values}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *search) includes(value interface{}) *search {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *search) joins(query string) *search {
|
|
|
|
s.joinsStr = query
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2013-11-16 05:12:22 +04:00
|
|
|
func (s *search) unscoped() *search {
|
2013-11-15 18:29:42 +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 {
|
|
|
|
s.tableName = name
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2013-11-16 11:01:31 +04:00
|
|
|
func (s *search) getInterfaceAsSql(value interface{}) (str string) {
|
2013-11-16 04:22:04 +04:00
|
|
|
switch value := value.(type) {
|
|
|
|
case string:
|
|
|
|
str = value
|
|
|
|
case int:
|
|
|
|
if value < 0 {
|
|
|
|
str = ""
|
|
|
|
} else {
|
|
|
|
str = strconv.Itoa(value)
|
|
|
|
}
|
|
|
|
default:
|
2013-11-16 11:01:31 +04:00
|
|
|
s.db.err(InvalidSql)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !regexp.MustCompile("^\\s*[\\w\\s,.*()]*\\s*$").MatchString(str) {
|
|
|
|
s.db.err(InvalidSql)
|
2013-11-16 04:22:04 +04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|