2014-01-26 08:46:04 +04:00
|
|
|
package gorm
|
|
|
|
|
2014-01-27 07:06:13 +04:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2016-01-17 13:38:18 +03:00
|
|
|
// Define callbacks for updating
|
2016-01-17 11:28:32 +03:00
|
|
|
func init() {
|
2016-01-17 13:38:18 +03:00
|
|
|
defaultCallback.Update().Register("gorm:assign_updating_attributes", assignUpdatingAttributesCallback)
|
2016-01-17 11:28:32 +03:00
|
|
|
defaultCallback.Update().Register("gorm:begin_transaction", beginTransactionCallback)
|
|
|
|
defaultCallback.Update().Register("gorm:before_update", beforeUpdateCallback)
|
|
|
|
defaultCallback.Update().Register("gorm:save_before_associations", saveBeforeAssociationsCallback)
|
2016-01-17 13:38:18 +03:00
|
|
|
defaultCallback.Update().Register("gorm:update_time_stamp", updateTimeStampForUpdateCallback)
|
2016-01-17 11:28:32 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-01-17 13:38:18 +03:00
|
|
|
// assignUpdatingAttributesCallback assign updating attributes to model
|
|
|
|
func assignUpdatingAttributesCallback(scope *Scope) {
|
2014-08-20 13:05:02 +04:00
|
|
|
if attrs, ok := scope.InstanceGet("gorm:update_interface"); ok {
|
2014-01-27 18:36:08 +04:00
|
|
|
if maps := convertInterfaceToMap(attrs); len(maps) > 0 {
|
2016-02-18 17:24:35 +03:00
|
|
|
if updateMaps, hasUpdate := scope.updatedAttrsWithValues(maps); hasUpdate {
|
|
|
|
scope.InstanceSet("gorm:update_attrs", updateMaps)
|
|
|
|
} else {
|
2014-01-27 18:36:08 +04:00
|
|
|
scope.SkipLeft()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 13:38:18 +03:00
|
|
|
// beforeUpdateCallback will invoke `BeforeSave`, `BeforeUpdate` method before updating
|
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 {
|
2016-01-17 11:14:14 +03:00
|
|
|
if !scope.HasError() {
|
|
|
|
scope.CallMethod("BeforeSave")
|
|
|
|
}
|
|
|
|
if !scope.HasError() {
|
|
|
|
scope.CallMethod("BeforeUpdate")
|
|
|
|
}
|
2014-01-28 04:27:12 +04:00
|
|
|
}
|
2014-01-26 08:46:04 +04:00
|
|
|
}
|
|
|
|
|
2016-01-17 13:38:18 +03:00
|
|
|
// updateTimeStampForUpdateCallback will set `UpdatedAt` when updating
|
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 {
|
2014-08-23 12:00:04 +04:00
|
|
|
scope.SetColumn("UpdatedAt", NowFunc())
|
2014-01-27 07:06:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 13:38:18 +03:00
|
|
|
// updateCallback the callback used to update data to database
|
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 {
|
2016-01-17 13:38:18 +03:00
|
|
|
for column, value := range updateAttrs.(map[string]interface{}) {
|
2016-02-18 17:24:35 +03:00
|
|
|
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(column), 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 {
|
2016-01-17 13:38:18 +03:00
|
|
|
if scope.changeableField(field) {
|
|
|
|
if !field.IsPrimaryKey && field.IsNormal {
|
|
|
|
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Field.Interface())))
|
|
|
|
} else if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" {
|
|
|
|
for _, foreignKey := range relationship.ForeignDBNames {
|
|
|
|
if foreignField := fields[foreignKey]; !scope.changeableField(foreignField) {
|
|
|
|
sqls = append(sqls,
|
|
|
|
fmt.Sprintf("%v = %v", scope.Quote(foreignField.DBName), scope.AddToVars(foreignField.Field.Interface())))
|
|
|
|
}
|
2015-03-12 13:30:59 +03:00
|
|
|
}
|
|
|
|
}
|
2014-01-27 18:36:08 +04:00
|
|
|
}
|
2014-01-26 08:46:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-15 14:36:48 +03:00
|
|
|
var extraOption string
|
|
|
|
if str, ok := scope.Get("gorm:update_option"); ok {
|
|
|
|
extraOption = fmt.Sprint(str)
|
|
|
|
}
|
|
|
|
|
2015-04-10 11:55:53 +03:00
|
|
|
if len(sqls) > 0 {
|
|
|
|
scope.Raw(fmt.Sprintf(
|
2016-02-15 14:36:48 +03:00
|
|
|
"UPDATE %v SET %v%v%v",
|
|
|
|
scope.QuotedTableName(),
|
|
|
|
strings.Join(sqls, ", "),
|
|
|
|
addExtraSpaceIfExist(scope.CombinedConditionSql()),
|
|
|
|
addExtraSpaceIfExist(extraOption),
|
2016-01-17 13:38:18 +03:00
|
|
|
)).Exec()
|
2015-04-10 11:55:53 +03:00
|
|
|
}
|
2014-01-26 08:46:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 13:38:18 +03:00
|
|
|
// afterUpdateCallback will invoke `AfterUpdate`, `AfterSave` method after updating
|
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 {
|
2016-01-17 11:14:14 +03:00
|
|
|
if !scope.HasError() {
|
|
|
|
scope.CallMethod("AfterUpdate")
|
|
|
|
}
|
|
|
|
if !scope.HasError() {
|
|
|
|
scope.CallMethod("AfterSave")
|
|
|
|
}
|
2014-01-28 04:27:12 +04:00
|
|
|
}
|
2014-01-26 08:46:04 +04:00
|
|
|
}
|