mirror of https://github.com/go-gorm/gorm.git
update scope.go
This commit is contained in:
parent
79cacf7f09
commit
10fcb0c88e
|
@ -15,10 +15,16 @@ func Delete(scope *Scope) {
|
|||
}
|
||||
|
||||
if !scope.Search.unscope && scope.HasColumn("DeletedAt") {
|
||||
scope.Raw(fmt.Sprintf("UPDATE %v SET deleted_at=%v %v", scope.TableName(), scope.AddToVars(time.Now()), scope.CombinedConditionSql()))
|
||||
scope.Raw(
|
||||
fmt.Sprintf("UPDATE %v SET deleted_at=%v %v",
|
||||
scope.TableName(),
|
||||
scope.AddToVars(time.Now()),
|
||||
scope.CombinedConditionSql(),
|
||||
))
|
||||
} else {
|
||||
scope.Raw(fmt.Sprintf("DELETE FROM %v %v", scope.TableName(), scope.CombinedConditionSql()))
|
||||
}
|
||||
|
||||
scope.Exec()
|
||||
}
|
||||
|
||||
|
|
2
model.go
2
model.go
|
@ -1,7 +1,6 @@
|
|||
package gorm
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"go/ast"
|
||||
"reflect"
|
||||
"regexp"
|
||||
|
@ -210,7 +209,6 @@ func (m *Model) typeName() string {
|
|||
|
||||
func (m *Model) tableName() (str string) {
|
||||
if m.data == nil {
|
||||
m.do.err(errors.New("Model haven't been set"))
|
||||
return
|
||||
}
|
||||
|
||||
|
|
57
scope.go
57
scope.go
|
@ -4,8 +4,10 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"github.com/jinzhu/gorm/dialect"
|
||||
"strings"
|
||||
|
||||
"reflect"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type Scope struct {
|
||||
|
@ -89,21 +91,56 @@ func (scope *Scope) CallMethod(name string) {
|
|||
}
|
||||
}
|
||||
|
||||
func (scope *Scope) AddToVars(value interface{}) string {
|
||||
scope.SqlVars = append(scope.SqlVars, value)
|
||||
return scope.Dialect().BinVar(len(scope.SqlVars))
|
||||
}
|
||||
|
||||
func (scope *Scope) TableName() string {
|
||||
if len(scope.Search.tableName) > 0 {
|
||||
return scope.Search.tableName
|
||||
} else {
|
||||
data := reflect.Indirect(reflect.ValueOf(scope.Value))
|
||||
|
||||
if data.Kind() == reflect.Slice {
|
||||
data = reflect.New(data.Type().Elem()).Elem()
|
||||
}
|
||||
|
||||
if fm := data.MethodByName("TableName"); fm.IsValid() {
|
||||
if v := fm.Call([]reflect.Value{}); len(v) > 0 {
|
||||
if result, ok := v[0].Interface().(string); ok {
|
||||
return result
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
str := toSnake(data.Type().Name())
|
||||
|
||||
if !scope.db.parent.singularTable {
|
||||
pluralMap := map[string]string{"ch": "ches", "ss": "sses", "sh": "shes", "day": "days", "y": "ies", "x": "xes", "s?": "s"}
|
||||
for key, value := range pluralMap {
|
||||
reg := regexp.MustCompile(key + "$")
|
||||
if reg.MatchString(str) {
|
||||
return reg.ReplaceAllString(str, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
}
|
||||
|
||||
func (scope *Scope) CombinedConditionSql() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (scope *Scope) AddToVars(value interface{}) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (scope *Scope) TableName() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (scope *Scope) Raw(sql string, values ...interface{}) {
|
||||
fmt.Println(sql, values)
|
||||
func (scope *Scope) Raw(sql string) {
|
||||
scope.Sql = strings.Replace(sql, "$$", "?", -1)
|
||||
}
|
||||
|
||||
func (scope *Scope) Exec() {
|
||||
if !scope.HasError() {
|
||||
_, err := scope.DB().Exec(scope.Sql, scope.SqlVars...)
|
||||
scope.Err(err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue