gorm/callback_shared.go

129 lines
4.3 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
2014-07-31 07:08:26 +04:00
if relationship != nil && relationship.Kind == "belongs_to" {
2014-09-02 15:03:01 +04:00
value := field.Field
newDB := scope.NewDB()
2014-01-27 04:26:59 +04:00
2014-09-02 15:03:01 +04:00
if !value.CanAddr() {
// If can't take address, then clone the value and set it back
2014-09-02 15:03:01 +04:00
value = reflect.New(value.Type()).Elem()
for _, f := range newDB.NewScope(field.Field.Interface()).Fields() {
value.FieldByName(f.Name).Set(reflect.ValueOf(f.Field.Interface()))
}
scope.SetColumn(field.Name, value.Interface())
2014-01-27 04:26:59 +04:00
}
2014-09-02 15:03:01 +04:00
scope.Err(newDB.Save(value.Addr().Interface()).Error)
2014-01-27 04:26:59 +04:00
2014-07-31 07:08:26 +04:00
if relationship.ForeignKey != "" {
scope.SetColumn(relationship.ForeignKey, newDB.NewScope(value.Interface()).PrimaryKeyValue())
}
if relationship.ForeignType != "" {
scope.Err(fmt.Errorf("gorm does not support polymorphic belongs_to associations"))
return
}
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 &&
2014-07-31 07:08:26 +04:00
(relationship.Kind == "has_one" || relationship.Kind == "has_many" || relationship.Kind == "many_to_many") {
2014-09-02 15:03:01 +04:00
value := field.Field
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
2014-07-31 07:08:26 +04:00
if relationship.JoinTable == "" && relationship.ForeignKey != "" {
newDB.NewScope(elem).SetColumn(relationship.ForeignKey, scope.PrimaryKeyValue())
}
if relationship.ForeignType != "" {
newDB.NewScope(elem).SetColumn(relationship.ForeignType, scope.TableName())
}
2014-01-27 04:26:59 +04:00
scope.Err(newDB.Save(elem).Error)
2014-07-31 07:08:26 +04:00
if relationship.JoinTable != "" {
if relationship.ForeignType != "" {
scope.Err(fmt.Errorf("gorm does not support polymorphic many-to-many associations"))
}
newScope := scope.New(elem)
2014-07-31 07:08:26 +04:00
joinTable := relationship.JoinTable
foreignKey := ToSnake(relationship.ForeignKey)
foreignValue := fmt.Sprintf("%v", scope.PrimaryKeyValue())
2014-07-31 07:08:26 +04:00
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),
))
2014-07-30 18:50:27 +04:00
scope.Err(scope.NewDB().Exec(newScope.Sql, newScope.SqlVars...).Error)
}
}
default:
newDB := scope.NewDB()
if value.CanAddr() {
2014-07-31 07:08:26 +04:00
if relationship.ForeignKey != "" {
2014-09-02 15:03:01 +04:00
newDB.NewScope(value.Addr().Interface()).SetColumn(relationship.ForeignKey, scope.PrimaryKeyValue())
}
if relationship.ForeignType != "" {
newDB.NewScope(value.Addr().Interface()).SetColumn(relationship.ForeignType, scope.TableName())
}
2014-09-02 15:03:01 +04:00
scope.Err(newDB.Save(value.Addr().Interface()).Error)
} else {
2014-09-02 15:03:01 +04:00
destValue := reflect.New(field.Field.Type()).Elem()
2014-01-27 04:26:59 +04:00
2014-09-02 15:03:01 +04:00
for _, f := range newDB.NewScope(field.Field.Interface()).Fields() {
destValue.FieldByName(f.Name).Set(f.Field)
}
2014-01-27 04:26:59 +04:00
elem := destValue.Addr().Interface()
2014-07-31 07:08:26 +04:00
if relationship.ForeignKey != "" {
newDB.NewScope(elem).SetColumn(relationship.ForeignKey, scope.PrimaryKeyValue())
}
if relationship.ForeignType != "" {
newDB.NewScope(value.Addr().Interface()).SetColumn(relationship.ForeignType, scope.TableName())
}
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
}