gorm/callback_update.go

88 lines
2.5 KiB
Go
Raw Normal View History

2014-01-26 08:46:04 +04:00
package gorm
2014-01-27 07:06:13 +04:00
import (
"fmt"
"strings"
)
2014-01-27 18:36:08 +04:00
func AssignUpdateAttributes(scope *Scope) {
if attrs, ok := scope.InstanceGet("gorm:update_interface"); ok {
2014-01-27 18:36:08 +04:00
if maps := convertInterfaceToMap(attrs); len(maps) > 0 {
protected, ok := scope.Get("gorm:ignore_protected_attrs")
_, updateColumn := scope.Get("gorm:update_column")
2014-01-27 18:36:08 +04:00
updateAttrs, hasUpdate := scope.updatedAttrsWithValues(maps, ok && protected.(bool))
if updateColumn {
scope.InstanceSet("gorm:update_attrs", maps)
} else if len(updateAttrs) > 0 {
scope.InstanceSet("gorm:update_attrs", updateAttrs)
2014-01-27 18:36:08 +04:00
} else if !hasUpdate {
scope.SkipLeft()
return
}
}
}
}
2014-01-26 08:46:04 +04:00
func BeforeUpdate(scope *Scope) {
_, ok := scope.Get("gorm:update_column")
if !ok {
scope.CallMethod("BeforeSave")
scope.CallMethod("BeforeUpdate")
}
2014-01-26 08:46:04 +04:00
}
2014-01-27 07:06:13 +04:00
func UpdateTimeStampWhenUpdate(scope *Scope) {
_, ok := scope.Get("gorm:update_column")
if !ok {
scope.SetColumn("UpdatedAt", NowFunc())
2014-01-27 07:06:13 +04:00
}
}
2014-01-26 08:46:04 +04:00
func Update(scope *Scope) {
if !scope.HasError() {
2014-01-27 07:06:13 +04:00
var sqls []string
2014-01-27 18:36:08 +04:00
updateAttrs, ok := scope.InstanceGet("gorm:update_attrs")
2014-01-27 18:36:08 +04:00
if ok {
for key, value := range updateAttrs.(map[string]interface{}) {
2014-01-28 12:22:41 +04:00
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(key), scope.AddToVars(value)))
2014-01-27 18:36:08 +04:00
}
} else {
for _, field := range scope.Fields() {
2014-08-28 14:53:27 +04:00
if !field.IsPrimaryKey && len(field.SqlTag) > 0 && !field.IsIgnored {
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-27 18:36:08 +04:00
}
2014-01-26 08:46:04 +04:00
}
}
2014-01-27 07:06:13 +04:00
scope.Raw(fmt.Sprintf(
"UPDATE %v SET %v %v",
2014-06-03 13:15:05 +04:00
scope.QuotedTableName(),
2014-01-27 07:06:13 +04:00
strings.Join(sqls, ", "),
scope.CombinedConditionSql(),
))
scope.Exec()
2014-01-26 08:46:04 +04:00
}
}
func AfterUpdate(scope *Scope) {
_, ok := scope.Get("gorm:update_column")
if !ok {
scope.CallMethod("AfterUpdate")
scope.CallMethod("AfterSave")
}
2014-01-26 08:46:04 +04:00
}
func init() {
2014-01-29 06:28:20 +04:00
DefaultCallback.Update().Register("gorm:assign_update_attributes", AssignUpdateAttributes)
DefaultCallback.Update().Register("gorm:begin_transaction", BeginTransaction)
DefaultCallback.Update().Register("gorm:before_update", BeforeUpdate)
DefaultCallback.Update().Register("gorm:save_before_associations", SaveBeforeAssociations)
DefaultCallback.Update().Register("gorm:update_time_stamp_when_update", UpdateTimeStampWhenUpdate)
DefaultCallback.Update().Register("gorm:update", Update)
DefaultCallback.Update().Register("gorm:save_after_associations", SaveAfterAssociations)
DefaultCallback.Update().Register("gorm:after_update", AfterUpdate)
DefaultCallback.Update().Register("gorm:commit_or_rollback_transaction", CommitOrRollbackTransaction)
2014-01-26 08:46:04 +04:00
}