gorm/callback_shared.go

116 lines
3.7 KiB
Go
Raw Normal View History

2014-01-26 13:10:33 +04:00
package gorm
2014-07-30 07:32:18 +04:00
import (
"fmt"
"reflect"
"strings"
2014-07-30 07:32:18 +04:00
)
2014-01-27 04:26:59 +04:00
2014-01-26 13:10:33 +04:00
func BeginTransaction(scope *Scope) {
scope.Begin()
}
func CommitOrRollbackTransaction(scope *Scope) {
scope.CommitOrRollback()
}
2014-01-26 15:34:06 +04:00
func SaveBeforeAssociations(scope *Scope) {
2014-01-27 04:26:59 +04:00
for _, field := range scope.Fields() {
if !field.IsBlank && !field.IsIgnored {
relationship := field.Relationship
if relationship != nil && relationship.kind == "belongs_to" {
value := reflect.ValueOf(field.Value)
newDB := scope.NewDB()
2014-01-27 04:26:59 +04:00
if value.CanAddr() {
scope.Err(newDB.Save(value.Addr().Interface()).Error)
} else {
// If can't take address, then clone the value and set it back
value = reflect.New(reflect.ValueOf(field.Value).Type()).Elem()
for _, f := range newDB.NewScope(field.Value).Fields() {
value.FieldByName(f.Name).Set(reflect.ValueOf(f.Value))
}
scope.Err(newDB.Save(value.Addr().Interface()).Error)
scope.SetColumn(field.Name, value.Interface())
2014-01-27 04:26:59 +04:00
}
if relationship.foreignKey != "" {
scope.SetColumn(relationship.foreignKey, newDB.NewScope(value.Interface()).PrimaryKeyValue())
}
2014-01-27 04:26:59 +04:00
}
}
}
2014-01-26 15:34:06 +04:00
}
func SaveAfterAssociations(scope *Scope) {
2014-01-27 04:26:59 +04:00
for _, field := range scope.Fields() {
if !field.IsBlank && !field.IsIgnored {
relationship := field.Relationship
if relationship != nil &&
(relationship.kind == "has_one" || relationship.kind == "has_many" || relationship.kind == "many_to_many") {
value := reflect.ValueOf(field.Value)
2014-01-27 04:26:59 +04:00
switch value.Kind() {
case reflect.Slice:
for i := 0; i < value.Len(); i++ {
newDB := scope.NewDB()
elem := value.Index(i).Addr().Interface()
2014-01-27 04:26:59 +04:00
if relationship.joinTable == "" && relationship.foreignKey != "" {
newDB.NewScope(elem).SetColumn(relationship.foreignKey, scope.PrimaryKeyValue())
}
2014-01-27 04:26:59 +04:00
scope.Err(newDB.Save(elem).Error)
if relationship.joinTable != "" {
newScope := scope.New(elem)
joinTable := relationship.joinTable
foreignKey := ToSnake(relationship.foreignKey)
foreignValue := fmt.Sprintf("%v", scope.PrimaryKeyValue())
associationForeignKey := ToSnake(relationship.associationForeignKey)
associationForeignValue := fmt.Sprintf("%v", newScope.PrimaryKeyValue())
newScope.Raw(fmt.Sprintf(
"INSERT INTO %v (%v) SELECT %v %v WHERE NOT EXISTS (SELECT * FROM %v WHERE %v = %v AND %v = %v);",
joinTable,
strings.Join([]string{scope.Quote(foreignKey), scope.Quote(associationForeignKey)}, ","),
strings.Join([]string{newScope.AddToVars(foreignValue), newScope.AddToVars(associationForeignValue)}, ","),
scope.Dialect().SelectFromDummyTable(),
joinTable,
scope.Quote(foreignKey),
newScope.AddToVars(foreignValue),
scope.Quote(associationForeignKey),
newScope.AddToVars(associationForeignValue),
))
if _, err := scope.DB().Exec(newScope.Sql, newScope.SqlVars...); err != nil {
scope.Err(err)
}
}
}
default:
newDB := scope.NewDB()
if value.CanAddr() {
if relationship.foreignKey != "" {
newDB.NewScope(field.Value).SetColumn(relationship.foreignKey, scope.PrimaryKeyValue())
}
scope.Err(newDB.Save(field.Value).Error)
} else {
destValue := reflect.New(reflect.TypeOf(field.Value)).Elem()
2014-01-27 04:26:59 +04:00
for _, f := range newDB.NewScope(field.Value).Fields() {
destValue.FieldByName(f.Name).Set(reflect.ValueOf(f.Value))
}
2014-01-27 04:26:59 +04:00
elem := destValue.Addr().Interface()
if relationship.foreignKey != "" {
newDB.NewScope(elem).SetColumn(relationship.foreignKey, scope.PrimaryKeyValue())
}
scope.Err(newDB.Save(elem).Error)
scope.SetColumn(field.Name, destValue.Interface())
2014-07-30 07:32:18 +04:00
}
2014-01-27 04:26:59 +04:00
}
}
}
}
2014-01-26 15:34:06 +04:00
}