2014-01-26 13:10:33 +04:00
|
|
|
package gorm
|
|
|
|
|
2014-07-30 07:32:18 +04:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2014-07-30 10:18:15 +04:00
|
|
|
"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() {
|
2014-07-30 17:14:10 +04:00
|
|
|
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
|
2014-07-30 17:14:10 +04:00
|
|
|
newDB := scope.NewDB()
|
2014-01-27 04:26:59 +04:00
|
|
|
|
2014-09-02 15:03:01 +04:00
|
|
|
if !value.CanAddr() {
|
2014-07-30 17:14:10 +04:00
|
|
|
// 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()
|
2014-12-04 06:44:00 +03:00
|
|
|
for _, f := range newDB.NewScope(field.Field.Addr().Interface()).Fields() {
|
2014-09-02 15:03:01 +04:00
|
|
|
value.FieldByName(f.Name).Set(reflect.ValueOf(f.Field.Interface()))
|
2014-07-30 17:14:10 +04:00
|
|
|
}
|
2015-02-17 12:40:21 +03:00
|
|
|
scope.SetColumn(field, 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
|
|
|
|
2015-02-16 11:35:26 +03:00
|
|
|
if relationship.ForeignFieldName != "" {
|
|
|
|
scope.SetColumn(relationship.ForeignFieldName, newDB.NewScope(value.Addr().Interface()).PrimaryKeyValue())
|
2014-07-30 17:14:10 +04:00
|
|
|
}
|
2014-11-26 08:23:43 +03:00
|
|
|
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() {
|
2014-07-30 17:14:10 +04:00
|
|
|
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
|
|
|
|
2014-07-30 17:14:10 +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
|
|
|
|
2015-02-16 11:35:26 +03:00
|
|
|
if relationship.JoinTable == "" && relationship.ForeignFieldName != "" {
|
|
|
|
newDB.NewScope(elem).SetColumn(relationship.ForeignFieldName, scope.PrimaryKeyValue())
|
2014-07-30 17:14:10 +04:00
|
|
|
}
|
2014-11-26 08:23:43 +03:00
|
|
|
if relationship.ForeignType != "" {
|
|
|
|
newDB.NewScope(elem).SetColumn(relationship.ForeignType, scope.TableName())
|
|
|
|
}
|
2014-01-27 04:26:59 +04:00
|
|
|
|
2014-07-30 17:14:10 +04:00
|
|
|
scope.Err(newDB.Save(elem).Error)
|
2014-07-30 10:18:15 +04:00
|
|
|
|
2014-07-31 07:08:26 +04:00
|
|
|
if relationship.JoinTable != "" {
|
2014-11-26 08:23:43 +03:00
|
|
|
if relationship.ForeignType != "" {
|
|
|
|
scope.Err(fmt.Errorf("gorm does not support polymorphic many-to-many associations"))
|
|
|
|
}
|
|
|
|
|
2014-07-30 17:14:10 +04:00
|
|
|
newScope := scope.New(elem)
|
2014-07-31 07:08:26 +04:00
|
|
|
joinTable := relationship.JoinTable
|
2015-02-16 11:35:26 +03:00
|
|
|
foreignKey := ToSnake(relationship.ForeignFieldName)
|
2014-07-30 17:14:10 +04:00
|
|
|
foreignValue := fmt.Sprintf("%v", scope.PrimaryKeyValue())
|
2015-02-16 11:35:26 +03:00
|
|
|
associationForeignKey := ToSnake(relationship.AssociationForeignFieldName)
|
2014-07-30 17:14:10 +04:00
|
|
|
associationForeignValue := fmt.Sprintf("%v", newScope.PrimaryKeyValue())
|
2014-07-30 10:18:15 +04:00
|
|
|
|
2014-07-30 17:14:10 +04:00
|
|
|
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)
|
2014-07-30 10:18:15 +04:00
|
|
|
}
|
|
|
|
}
|
2014-07-30 17:14:10 +04:00
|
|
|
default:
|
|
|
|
newDB := scope.NewDB()
|
|
|
|
if value.CanAddr() {
|
2015-02-16 11:35:26 +03:00
|
|
|
if relationship.ForeignFieldName != "" {
|
|
|
|
newDB.NewScope(value.Addr().Interface()).SetColumn(relationship.ForeignFieldName, scope.PrimaryKeyValue())
|
2014-07-30 17:14:10 +04:00
|
|
|
}
|
2014-11-26 08:23:43 +03:00
|
|
|
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)
|
2014-07-30 17:14:10 +04:00
|
|
|
} 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-12-04 06:44:00 +03:00
|
|
|
for _, f := range newDB.NewScope(field.Field.Addr().Interface()).Fields() {
|
2014-09-02 15:03:01 +04:00
|
|
|
destValue.FieldByName(f.Name).Set(f.Field)
|
2014-07-30 17:14:10 +04:00
|
|
|
}
|
2014-01-27 04:26:59 +04:00
|
|
|
|
2014-07-30 17:14:10 +04:00
|
|
|
elem := destValue.Addr().Interface()
|
2015-02-16 11:35:26 +03:00
|
|
|
if relationship.ForeignFieldName != "" {
|
|
|
|
newDB.NewScope(elem).SetColumn(relationship.ForeignFieldName, scope.PrimaryKeyValue())
|
2014-07-30 17:14:10 +04:00
|
|
|
}
|
2014-11-26 08:23:43 +03:00
|
|
|
if relationship.ForeignType != "" {
|
|
|
|
newDB.NewScope(value.Addr().Interface()).SetColumn(relationship.ForeignType, scope.TableName())
|
|
|
|
}
|
2014-07-30 17:14:10 +04:00
|
|
|
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
|
|
|
}
|