Fix Exec with raw string

This commit is contained in:
Jinzhu 2015-01-20 12:15:24 +08:00
parent f8e4e16e09
commit 5330572c25
3 changed files with 7 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"reflect" "reflect"
"strings"
"time" "time"
) )
@ -287,7 +288,9 @@ func (s *DB) Raw(sql string, values ...interface{}) *DB {
func (s *DB) Exec(sql string, values ...interface{}) *DB { func (s *DB) Exec(sql string, values ...interface{}) *DB {
scope := s.clone().NewScope(nil) scope := s.clone().NewScope(nil)
scope.Raw(scope.buildWhereCondition(map[string]interface{}{"query": sql, "args": values})) generatedSql := scope.buildWhereCondition(map[string]interface{}{"query": sql, "args": values})
generatedSql = strings.TrimSuffix(strings.TrimPrefix(generatedSql, "("), ")")
scope.Raw(generatedSql)
return scope.Exec().db return scope.Exec().db
} }

View File

@ -38,7 +38,7 @@ func (d *mysql) SqlTag(value reflect.Value, size int) string {
} }
case reflect.Struct: case reflect.Struct:
if value.Type() == timeType { if value.Type() == timeType {
return "timestamp" return "timestamp NULL"
} }
default: default:
if _, ok := value.Interface().([]byte); ok { if _, ok := value.Interface().([]byte); ok {

View File

@ -23,7 +23,7 @@ func (scope *Scope) buildWhereCondition(clause map[string]interface{}) (str stri
if regexp.MustCompile("^\\s*\\d+\\s*$").MatchString(value) { if regexp.MustCompile("^\\s*\\d+\\s*$").MatchString(value) {
id, _ := strconv.Atoi(value) id, _ := strconv.Atoi(value)
return scope.primaryCondiation(scope.AddToVars(id)) return scope.primaryCondiation(scope.AddToVars(id))
} else { } else if value != "" {
str = fmt.Sprintf("(%v)", value) str = fmt.Sprintf("(%v)", value)
} }
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
@ -300,7 +300,7 @@ func (s *Scope) joinsSql() string {
func (scope *Scope) prepareQuerySql() { func (scope *Scope) prepareQuerySql() {
if scope.Search.Raw { if scope.Search.Raw {
scope.Raw(strings.TrimLeft(scope.CombinedConditionSql(), "WHERE ")) scope.Raw(strings.TrimRight(strings.TrimLeft(scope.CombinedConditionSql(), "WHERE ("), ")"))
} else { } else {
scope.Raw(fmt.Sprintf("SELECT %v %v FROM %v %v", scope.topSql(), scope.selectSql(), scope.QuotedTableName(), scope.CombinedConditionSql())) scope.Raw(fmt.Sprintf("SELECT %v %v FROM %v %v", scope.topSql(), scope.selectSql(), scope.QuotedTableName(), scope.CombinedConditionSql()))
} }