diff --git a/callback_delete.go b/callback_delete.go index aa13af4a..7b35bca4 100644 --- a/callback_delete.go +++ b/callback_delete.go @@ -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() } diff --git a/model.go b/model.go index 1ff876f3..cd7bd6f6 100644 --- a/model.go +++ b/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 } diff --git a/scope.go b/scope.go index a1c81e06..a387248f 100644 --- a/scope.go +++ b/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) + } }