2014-01-26 13:10:33 +04:00
|
|
|
package gorm
|
|
|
|
|
2015-02-28 06:48:18 +03:00
|
|
|
import "reflect"
|
2014-01-27 04:26:59 +04:00
|
|
|
|
2016-01-17 10:30:42 +03:00
|
|
|
func beginTransactionCallback(scope *Scope) {
|
2014-01-26 13:10:33 +04:00
|
|
|
scope.Begin()
|
|
|
|
}
|
|
|
|
|
2016-01-17 10:30:42 +03:00
|
|
|
func commitOrRollbackTransactionCallback(scope *Scope) {
|
2014-01-26 13:10:33 +04:00
|
|
|
scope.CommitOrRollback()
|
|
|
|
}
|
2014-01-26 15:34:06 +04:00
|
|
|
|
2016-10-27 05:31:46 +03:00
|
|
|
func saveFieldAsAssociation(scope *Scope, field *Field) (bool, *Relationship) {
|
2017-10-10 21:20:56 +03:00
|
|
|
if scope.changeableField(field) && !field.IsBlank && !field.IsIgnored {
|
|
|
|
if value, ok := field.TagSettings["SAVE_ASSOCIATIONS"]; !ok || (value != "false" && value != "skip") {
|
|
|
|
return true, field.Relationship
|
|
|
|
}
|
|
|
|
return false, field.Relationship
|
|
|
|
}
|
|
|
|
return false, nil
|
2016-10-27 05:31:46 +03:00
|
|
|
}
|
|
|
|
|
2016-01-17 10:30:42 +03:00
|
|
|
func saveBeforeAssociationsCallback(scope *Scope) {
|
2017-10-10 21:20:56 +03:00
|
|
|
for _, field := range scope.Fields() {
|
|
|
|
ok, relationship := saveFieldAsAssociation(scope, field)
|
|
|
|
if relationship != nil && relationship.Kind == "belongs_to" {
|
|
|
|
fieldValue := field.Field.Addr().Interface()
|
|
|
|
if ok && scope.shouldSaveAssociations() {
|
|
|
|
scope.Err(scope.NewDB().Save(fieldValue).Error)
|
|
|
|
}
|
|
|
|
if len(relationship.ForeignFieldNames) != 0 {
|
|
|
|
// set value's foreign key
|
|
|
|
for idx, fieldName := range relationship.ForeignFieldNames {
|
|
|
|
associationForeignName := relationship.AssociationForeignDBNames[idx]
|
|
|
|
if foreignField, ok := scope.New(fieldValue).FieldByName(associationForeignName); ok {
|
|
|
|
scope.Err(scope.SetColumn(fieldName, foreignField.Field.Interface()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-26 15:34:06 +04:00
|
|
|
}
|
|
|
|
|
2016-01-17 10:30:42 +03:00
|
|
|
func saveAfterAssociationsCallback(scope *Scope) {
|
2015-03-29 00:15:12 +03:00
|
|
|
if !scope.shouldSaveAssociations() {
|
|
|
|
return
|
|
|
|
}
|
2014-01-27 04:26:59 +04:00
|
|
|
for _, field := range scope.Fields() {
|
2017-10-04 11:19:16 +03:00
|
|
|
if ok, relationship := saveFieldAsAssociation(scope, field); ok && relationship != nil &&
|
2016-10-27 05:31:46 +03:00
|
|
|
(relationship.Kind == "has_one" || relationship.Kind == "has_many" || relationship.Kind == "many_to_many") {
|
|
|
|
value := field.Field
|
2014-01-27 04:26:59 +04:00
|
|
|
|
2016-10-27 05:31:46 +03:00
|
|
|
switch value.Kind() {
|
|
|
|
case reflect.Slice:
|
|
|
|
for i := 0; i < value.Len(); i++ {
|
|
|
|
newDB := scope.NewDB()
|
|
|
|
elem := value.Index(i).Addr().Interface()
|
|
|
|
newScope := newDB.NewScope(elem)
|
2014-07-30 10:18:15 +04:00
|
|
|
|
2016-10-27 05:31:46 +03:00
|
|
|
if relationship.JoinTableHandler == nil && len(relationship.ForeignFieldNames) != 0 {
|
2015-07-30 09:26:48 +03:00
|
|
|
for idx, fieldName := range relationship.ForeignFieldNames {
|
|
|
|
associationForeignName := relationship.AssociationForeignDBNames[idx]
|
2015-07-30 13:56:05 +03:00
|
|
|
if f, ok := scope.FieldByName(associationForeignName); ok {
|
2015-07-30 13:41:43 +03:00
|
|
|
scope.Err(newScope.SetColumn(fieldName, f.Field.Interface()))
|
2015-07-30 09:26:48 +03:00
|
|
|
}
|
|
|
|
}
|
2015-02-17 15:19:47 +03:00
|
|
|
}
|
2014-01-27 04:26:59 +04:00
|
|
|
|
2015-02-18 07:30:09 +03:00
|
|
|
if relationship.PolymorphicType != "" {
|
2016-10-06 15:33:48 +03:00
|
|
|
scope.Err(newScope.SetColumn(relationship.PolymorphicType, relationship.PolymorphicValue))
|
2014-07-30 07:32:18 +04:00
|
|
|
}
|
2016-10-27 05:31:46 +03:00
|
|
|
|
|
|
|
scope.Err(newDB.Save(elem).Error)
|
|
|
|
|
|
|
|
if joinTableHandler := relationship.JoinTableHandler; joinTableHandler != nil {
|
|
|
|
scope.Err(joinTableHandler.Add(joinTableHandler, newDB, scope.Value, newScope.Value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
elem := value.Addr().Interface()
|
|
|
|
newScope := scope.New(elem)
|
|
|
|
if len(relationship.ForeignFieldNames) != 0 {
|
|
|
|
for idx, fieldName := range relationship.ForeignFieldNames {
|
|
|
|
associationForeignName := relationship.AssociationForeignDBNames[idx]
|
|
|
|
if f, ok := scope.FieldByName(associationForeignName); ok {
|
|
|
|
scope.Err(newScope.SetColumn(fieldName, f.Field.Interface()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if relationship.PolymorphicType != "" {
|
|
|
|
scope.Err(newScope.SetColumn(relationship.PolymorphicType, relationship.PolymorphicValue))
|
2014-01-27 04:26:59 +04:00
|
|
|
}
|
2016-10-27 05:31:46 +03:00
|
|
|
scope.Err(scope.NewDB().Save(elem).Error)
|
2014-01-27 04:26:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-26 15:34:06 +04:00
|
|
|
}
|