gorm/scope_condition.go

248 lines
6.7 KiB
Go
Raw Normal View History

2014-01-26 10:55:41 +04:00
package gorm
import (
"database/sql"
"database/sql/driver"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
)
func (scope *Scope) primaryCondiation(value interface{}) string {
2014-01-28 12:22:41 +04:00
return fmt.Sprintf("(%v = %v)", scope.Quote(scope.PrimaryKey()), value)
2014-01-26 10:55:41 +04:00
}
func (scope *Scope) buildWhereCondition(clause map[string]interface{}) (str string) {
switch value := clause["query"].(type) {
case string:
// if string is number
if regexp.MustCompile("^\\s*\\d+\\s*$").MatchString(value) {
id, _ := strconv.Atoi(value)
return scope.primaryCondiation(scope.AddToVars(id))
} else {
str = value
}
case int, int64, int32:
return scope.primaryCondiation(scope.AddToVars(value))
case sql.NullInt64:
return scope.primaryCondiation(scope.AddToVars(value.Int64))
case []int64, []int, []int32, []string:
2014-01-28 12:22:41 +04:00
str = fmt.Sprintf("(%v in (?))", scope.Quote(scope.PrimaryKey()))
2014-01-26 10:55:41 +04:00
clause["args"] = []interface{}{value}
case map[string]interface{}:
var sqls []string
for key, value := range value {
2014-01-28 12:22:41 +04:00
sqls = append(sqls, fmt.Sprintf("(%v = %v)", scope.Quote(key), scope.AddToVars(value)))
2014-01-26 10:55:41 +04:00
}
return strings.Join(sqls, " AND ")
case interface{}:
var sqls []string
2014-01-28 05:25:30 +04:00
for _, field := range scope.New(value).Fields() {
if !field.IsBlank {
2014-01-28 12:22:41 +04:00
sqls = append(sqls, fmt.Sprintf("(%v = %v)", scope.Quote(field.DBName), scope.AddToVars(field.Value)))
2014-01-28 05:25:30 +04:00
}
2014-01-26 10:55:41 +04:00
}
return strings.Join(sqls, " AND ")
}
args := clause["args"].([]interface{})
for _, arg := range args {
switch reflect.TypeOf(arg).Kind() {
case reflect.Slice: // For where("id in (?)", []int64{1,2})
values := reflect.ValueOf(arg)
2014-01-28 13:09:43 +04:00
var tempMarks []string
2014-01-26 10:55:41 +04:00
for i := 0; i < values.Len(); i++ {
2014-01-28 13:09:43 +04:00
tempMarks = append(tempMarks, scope.AddToVars(values.Index(i).Interface()))
2014-01-26 10:55:41 +04:00
}
2014-01-28 13:09:43 +04:00
str = strings.Replace(str, "?", strings.Join(tempMarks, ","), 1)
2014-01-26 10:55:41 +04:00
default:
if valuer, ok := interface{}(arg).(driver.Valuer); ok {
arg, _ = valuer.Value()
}
str = strings.Replace(str, "?", scope.AddToVars(arg), 1)
}
}
return
}
func (scope *Scope) buildNotCondition(clause map[string]interface{}) (str string) {
2014-01-28 13:09:43 +04:00
var notEqualSql string
2014-01-26 10:55:41 +04:00
switch value := clause["query"].(type) {
case string:
if regexp.MustCompile("^\\s*\\d+\\s*$").MatchString(value) {
id, _ := strconv.Atoi(value)
2014-01-28 12:22:41 +04:00
return fmt.Sprintf("(%v <> %v)", scope.Quote(scope.PrimaryKey()), id)
2014-01-26 10:55:41 +04:00
} else if regexp.MustCompile("(?i) (=|<>|>|<|LIKE|IS) ").MatchString(value) {
str = fmt.Sprintf(" NOT (%v) ", value)
2014-01-28 13:09:43 +04:00
notEqualSql = fmt.Sprintf("NOT (%v)", value)
2014-01-26 10:55:41 +04:00
} else {
2014-01-28 12:22:41 +04:00
str = fmt.Sprintf("(%v NOT IN (?))", scope.Quote(value))
2014-01-28 13:09:43 +04:00
notEqualSql = fmt.Sprintf("(%v <> ?)", scope.Quote(value))
2014-01-26 10:55:41 +04:00
}
case int, int64, int32:
2014-01-28 12:22:41 +04:00
return fmt.Sprintf("(%v <> %v)", scope.Quote(scope.PrimaryKey()), value)
2014-01-26 10:55:41 +04:00
case []int64, []int, []int32, []string:
if reflect.ValueOf(value).Len() > 0 {
2014-01-28 12:22:41 +04:00
str = fmt.Sprintf("(%v not in (?))", scope.Quote(scope.PrimaryKey()))
2014-01-26 10:55:41 +04:00
clause["args"] = []interface{}{value}
} else {
return ""
}
case map[string]interface{}:
var sqls []string
for key, value := range value {
2014-01-28 12:22:41 +04:00
sqls = append(sqls, fmt.Sprintf("(%v <> %v)", scope.Quote(key), scope.AddToVars(value)))
2014-01-26 10:55:41 +04:00
}
return strings.Join(sqls, " AND ")
case interface{}:
var sqls []string
2014-01-28 05:25:30 +04:00
for _, field := range scope.New(value).Fields() {
if !field.IsBlank {
2014-01-28 12:22:41 +04:00
sqls = append(sqls, fmt.Sprintf("(%v <> %v)", scope.Quote(field.DBName), scope.AddToVars(field.Value)))
2014-01-28 05:25:30 +04:00
}
2014-01-26 10:55:41 +04:00
}
return strings.Join(sqls, " AND ")
}
args := clause["args"].([]interface{})
for _, arg := range args {
switch reflect.TypeOf(arg).Kind() {
case reflect.Slice: // For where("id in (?)", []int64{1,2})
values := reflect.ValueOf(arg)
2014-01-28 13:09:43 +04:00
var tempMarks []string
2014-01-26 10:55:41 +04:00
for i := 0; i < values.Len(); i++ {
2014-01-28 13:09:43 +04:00
tempMarks = append(tempMarks, scope.AddToVars(values.Index(i).Interface()))
2014-01-26 10:55:41 +04:00
}
2014-01-28 13:09:43 +04:00
str = strings.Replace(str, "?", strings.Join(tempMarks, ","), 1)
2014-01-26 10:55:41 +04:00
default:
if scanner, ok := interface{}(arg).(driver.Valuer); ok {
arg, _ = scanner.Value()
}
2014-01-28 13:09:43 +04:00
str = strings.Replace(notEqualSql, "?", scope.AddToVars(arg), 1)
2014-01-26 10:55:41 +04:00
}
}
return
}
func (scope *Scope) where(where ...interface{}) {
if len(where) > 0 {
scope.Search = scope.Search.clone().where(where[0], where[1:]...)
}
}
func (scope *Scope) whereSql() (sql string) {
2014-01-28 13:09:43 +04:00
var primaryCondiations, andConditions, orConditions []string
2014-01-26 10:55:41 +04:00
2014-01-28 12:56:51 +04:00
if !scope.Search.Unscope && scope.HasColumn("DeletedAt") {
2014-01-28 13:09:43 +04:00
primaryCondiations = append(primaryCondiations, "(deleted_at IS NULL OR deleted_at <= '0001-01-02')")
2014-01-26 10:55:41 +04:00
}
if !scope.PrimaryKeyZero() {
2014-01-28 13:09:43 +04:00
primaryCondiations = append(primaryCondiations, scope.primaryCondiation(scope.AddToVars(scope.PrimaryKeyValue())))
2014-01-26 10:55:41 +04:00
}
2014-01-28 12:56:51 +04:00
for _, clause := range scope.Search.WhereConditions {
2014-01-28 13:09:43 +04:00
andConditions = append(andConditions, scope.buildWhereCondition(clause))
2014-01-26 10:55:41 +04:00
}
2014-01-28 12:56:51 +04:00
for _, clause := range scope.Search.OrConditions {
2014-01-28 13:09:43 +04:00
orConditions = append(orConditions, scope.buildWhereCondition(clause))
2014-01-26 10:55:41 +04:00
}
2014-01-28 12:56:51 +04:00
for _, clause := range scope.Search.NotConditions {
2014-01-28 13:09:43 +04:00
andConditions = append(andConditions, scope.buildNotCondition(clause))
2014-01-26 10:55:41 +04:00
}
2014-01-28 13:09:43 +04:00
orSql := strings.Join(orConditions, " OR ")
combinedSql := strings.Join(andConditions, " AND ")
if len(combinedSql) > 0 {
if len(orSql) > 0 {
combinedSql = combinedSql + " OR " + orSql
2014-01-26 10:55:41 +04:00
}
} else {
2014-01-28 13:09:43 +04:00
combinedSql = orSql
2014-01-26 10:55:41 +04:00
}
2014-01-28 13:09:43 +04:00
if len(primaryCondiations) > 0 {
sql = "WHERE " + strings.Join(primaryCondiations, " AND ")
if len(combinedSql) > 0 {
sql = sql + " AND (" + combinedSql + ")"
2014-01-26 10:55:41 +04:00
}
2014-01-28 13:09:43 +04:00
} else if len(combinedSql) > 0 {
sql = "WHERE " + combinedSql
2014-01-26 10:55:41 +04:00
}
return
}
func (s *Scope) selectSql() string {
2014-01-28 12:56:51 +04:00
if len(s.Search.Select) == 0 {
2014-01-26 10:55:41 +04:00
return "*"
} else {
2014-01-28 12:56:51 +04:00
return s.Search.Select
2014-01-26 10:55:41 +04:00
}
}
func (s *Scope) orderSql() string {
2014-01-28 12:56:51 +04:00
if len(s.Search.Orders) == 0 {
2014-01-26 10:55:41 +04:00
return ""
} else {
2014-01-28 12:56:51 +04:00
return " ORDER BY " + strings.Join(s.Search.Orders, ",")
2014-01-26 10:55:41 +04:00
}
}
func (s *Scope) limitSql() string {
2014-01-28 12:56:51 +04:00
if len(s.Search.Limit) == 0 {
2014-01-26 10:55:41 +04:00
return ""
} else {
2014-01-28 12:56:51 +04:00
return " LIMIT " + s.Search.Limit
2014-01-26 10:55:41 +04:00
}
}
func (s *Scope) offsetSql() string {
2014-01-28 12:56:51 +04:00
if len(s.Search.Offset) == 0 {
2014-01-26 10:55:41 +04:00
return ""
} else {
2014-01-28 12:56:51 +04:00
return " OFFSET " + s.Search.Offset
2014-01-26 10:55:41 +04:00
}
}
func (s *Scope) groupSql() string {
2014-01-28 12:56:51 +04:00
if len(s.Search.Group) == 0 {
2014-01-26 10:55:41 +04:00
return ""
} else {
2014-01-28 12:56:51 +04:00
return " GROUP BY " + s.Search.Group
2014-01-26 10:55:41 +04:00
}
}
func (s *Scope) havingSql() string {
2014-01-28 12:56:51 +04:00
if s.Search.HavingCondition == nil {
2014-01-26 10:55:41 +04:00
return ""
} else {
2014-01-28 12:56:51 +04:00
return " HAVING " + s.buildWhereCondition(s.Search.HavingCondition)
2014-01-26 10:55:41 +04:00
}
}
func (s *Scope) joinsSql() string {
2014-01-28 12:56:51 +04:00
return s.Search.Joins + " "
2014-01-26 10:55:41 +04:00
}
2014-01-28 07:37:32 +04:00
func (scope *Scope) prepareQuerySql() {
2014-01-28 12:56:51 +04:00
if scope.Search.Raw {
2014-01-28 07:37:32 +04:00
scope.Raw(strings.TrimLeft(scope.CombinedConditionSql(), "WHERE "))
} else {
scope.Raw(fmt.Sprintf("SELECT %v FROM %v %v", scope.selectSql(), scope.TableName(), scope.CombinedConditionSql()))
}
return
}
2014-01-28 08:28:44 +04:00
func (scope *Scope) inlineCondition(values ...interface{}) *Scope {
2014-01-28 07:37:32 +04:00
if len(values) > 0 {
scope.Search = scope.Search.clone().where(values[0], values[1:]...)
}
return scope
}