gorm/callback_update.go

96 lines
3.3 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"
)
2016-01-17 10:30:42 +03:00
func assignUpdateAttributesCallback(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
}
}
}
}
2016-01-17 10:30:42 +03:00
func beforeUpdateCallback(scope *Scope) {
2014-08-30 18:39:28 +04:00
if _, ok := scope.Get("gorm:update_column"); !ok {
2015-02-24 11:28:15 +03:00
scope.CallMethodWithErrorCheck("BeforeSave")
scope.CallMethodWithErrorCheck("BeforeUpdate")
}
2014-01-26 08:46:04 +04:00
}
2016-01-17 10:30:42 +03:00
func updateTimeStampForUpdateCallback(scope *Scope) {
2014-08-30 18:39:28 +04:00
if _, ok := scope.Get("gorm:update_column"); !ok {
scope.SetColumn("UpdatedAt", NowFunc())
2014-01-27 07:06:13 +04:00
}
}
2016-01-17 10:30:42 +03:00
func updateCallback(scope *Scope) {
2014-01-26 08:46:04 +04:00
if !scope.HasError() {
2014-01-27 07:06:13 +04:00
var sqls []string
2014-01-27 18:36:08 +04:00
2015-02-17 17:55:14 +03:00
if updateAttrs, ok := scope.InstanceGet("gorm:update_attrs"); ok {
2014-01-27 18:36:08 +04:00
for key, value := range updateAttrs.(map[string]interface{}) {
if scope.changeableDBColumn(key) {
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(key), scope.AddToVars(value)))
}
2014-01-27 18:36:08 +04:00
}
} else {
2015-03-12 13:30:59 +03:00
fields := scope.Fields()
for _, field := range fields {
if scope.changeableField(field) && !field.IsPrimaryKey && field.IsNormal {
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface())))
2015-03-12 13:30:59 +03:00
} else if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" {
2015-07-30 09:26:48 +03:00
for _, dbName := range relationship.ForeignDBNames {
if relationField := fields[dbName]; !scope.changeableField(relationField) && !relationField.IsBlank {
sql := fmt.Sprintf("%v = %v", scope.Quote(relationField.DBName), scope.AddToVars(relationField.Field.Interface()))
sqls = append(sqls, sql)
2015-03-12 13:30:59 +03:00
}
}
2014-01-27 18:36:08 +04:00
}
2014-01-26 08:46:04 +04:00
}
}
if len(sqls) > 0 {
scope.Raw(fmt.Sprintf(
"UPDATE %v SET %v %v",
scope.QuotedTableName(),
strings.Join(sqls, ", "),
scope.CombinedConditionSql(),
))
scope.Exec()
}
2014-01-26 08:46:04 +04:00
}
}
2016-01-17 10:30:42 +03:00
func afterUpdateCallback(scope *Scope) {
2015-02-17 17:55:14 +03:00
if _, ok := scope.Get("gorm:update_column"); !ok {
2015-02-24 11:28:15 +03:00
scope.CallMethodWithErrorCheck("AfterUpdate")
scope.CallMethodWithErrorCheck("AfterSave")
}
2014-01-26 08:46:04 +04:00
}
func init() {
2016-01-17 10:30:42 +03:00
defaultCallback.Update().Register("gorm:assign_update_attributes", assignUpdateAttributesCallback)
defaultCallback.Update().Register("gorm:begin_transaction", beginTransactionCallback)
defaultCallback.Update().Register("gorm:before_update", beforeUpdateCallback)
defaultCallback.Update().Register("gorm:save_before_associations", saveBeforeAssociationsCallback)
defaultCallback.Update().Register("gorm:update_time_stamp_when_update", updateTimeStampForUpdateCallback)
defaultCallback.Update().Register("gorm:update", updateCallback)
defaultCallback.Update().Register("gorm:save_after_associations", saveAfterAssociationsCallback)
defaultCallback.Update().Register("gorm:after_update", afterUpdateCallback)
defaultCallback.Update().Register("gorm:commit_or_rollback_transaction", commitOrRollbackTransactionCallback)
2014-01-26 08:46:04 +04:00
}