make update column works with new plugin system

This commit is contained in:
Jinzhu 2014-01-28 08:27:12 +08:00
parent 506d14a2f2
commit 048b8b6abe
3 changed files with 27 additions and 9 deletions

View File

@ -10,8 +10,12 @@ func AssignUpdateAttributes(scope *Scope) {
if attrs, ok := scope.Get("gorm:update_interface"); ok { if attrs, ok := scope.Get("gorm:update_interface"); ok {
if maps := convertInterfaceToMap(attrs); len(maps) > 0 { if maps := convertInterfaceToMap(attrs); len(maps) > 0 {
protected, ok := scope.Get("gorm:ignore_protected_attrs") protected, ok := scope.Get("gorm:ignore_protected_attrs")
_, updateColumn := scope.Get("gorm:update_column")
updateAttrs, hasUpdate := scope.updatedAttrsWithValues(maps, ok && protected.(bool)) updateAttrs, hasUpdate := scope.updatedAttrsWithValues(maps, ok && protected.(bool))
if len(updateAttrs) > 0 {
if updateColumn {
scope.Set("gorm:update_attrs", maps)
} else if len(updateAttrs) > 0 {
scope.Set("gorm:update_attrs", updateAttrs) scope.Set("gorm:update_attrs", updateAttrs)
} else if !hasUpdate { } else if !hasUpdate {
scope.SkipLeft() scope.SkipLeft()
@ -22,12 +26,16 @@ func AssignUpdateAttributes(scope *Scope) {
} }
func BeforeUpdate(scope *Scope) { func BeforeUpdate(scope *Scope) {
scope.CallMethod("BeforeSave") _, ok := scope.Get("gorm:update_column")
scope.CallMethod("BeforeUpdate") if !ok {
scope.CallMethod("BeforeSave")
scope.CallMethod("BeforeUpdate")
}
} }
func UpdateTimeStampWhenUpdate(scope *Scope) { func UpdateTimeStampWhenUpdate(scope *Scope) {
if !scope.HasError() { _, ok := scope.Get("gorm:update_column")
if !ok {
scope.SetColumn("UpdatedAt", time.Now()) scope.SetColumn("UpdatedAt", time.Now())
} }
} }
@ -62,8 +70,11 @@ func Update(scope *Scope) {
} }
func AfterUpdate(scope *Scope) { func AfterUpdate(scope *Scope) {
scope.CallMethod("AfterUpdate") _, ok := scope.Get("gorm:update_column")
scope.CallMethod("AfterSave") if !ok {
scope.CallMethod("AfterUpdate")
scope.CallMethod("AfterSave")
}
} }
func init() { func init() {

View File

@ -172,11 +172,14 @@ func (s *DB) Updates(values interface{}, ignoreProtectedAttrs ...bool) *DB {
} }
func (s *DB) UpdateColumn(attrs ...interface{}) *DB { func (s *DB) UpdateColumn(attrs ...interface{}) *DB {
return s.UpdateColumns(toSearchableMap(attrs...), true) return s.UpdateColumns(toSearchableMap(attrs...))
} }
func (s *DB) UpdateColumns(values interface{}, ignore_protected_attrs ...bool) *DB { func (s *DB) UpdateColumns(values interface{}) *DB {
return s.clone().do(s.Value).begin().updateColumns(values).commit_or_rollback().db return s.clone().NewScope(s.Value).
Set("gorm:update_interface", values).
Set("gorm:update_column", true).
callCallbacks(s.parent.callback.updates).db
} }
func (s *DB) Save(value interface{}) *DB { func (s *DB) Save(value interface{}) *DB {

View File

@ -65,6 +65,10 @@ func (scope *Scope) Err(err error) error {
return err return err
} }
func (scope *Scope) Log(v ...interface{}) {
scope.db.log(v...)
}
func (scope *Scope) HasError() bool { func (scope *Scope) HasError() bool {
return scope.db.hasError() return scope.db.hasError()
} }