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 {
|
2015-02-17 15:19:47 +03:00
|
|
|
if relationship := field.Relationship; relationship != nil && relationship.Kind == "belongs_to" {
|
2014-09-02 15:03:01 +04:00
|
|
|
value := field.Field
|
2015-02-17 15:19:47 +03:00
|
|
|
scope.Err(scope.NewDB().Save(value.Addr().Interface()).Error)
|
2015-02-16 11:35:26 +03:00
|
|
|
if relationship.ForeignFieldName != "" {
|
2015-02-17 15:19:47 +03:00
|
|
|
scope.SetColumn(relationship.ForeignFieldName, scope.New(value.Addr().Interface()).PrimaryKeyValue())
|
2014-11-26 08:23:43 +03:00
|
|
|
}
|
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 {
|
2015-02-17 15:19:47 +03:00
|
|
|
if relationship := field.Relationship; 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()
|
2015-02-17 15:19:47 +03:00
|
|
|
newScope := newDB.NewScope(elem)
|
2014-01-27 04:26:59 +04:00
|
|
|
|
2015-02-16 11:35:26 +03:00
|
|
|
if relationship.JoinTable == "" && relationship.ForeignFieldName != "" {
|
2015-02-17 15:19:47 +03:00
|
|
|
newScope.SetColumn(relationship.ForeignFieldName, scope.PrimaryKeyValue())
|
2014-07-30 17:14:10 +04:00
|
|
|
}
|
2015-02-17 15:19:47 +03:00
|
|
|
|
2014-11-26 08:23:43 +03:00
|
|
|
if relationship.ForeignType != "" {
|
2015-02-17 15:19:47 +03:00
|
|
|
newScope.SetColumn(relationship.ForeignType, scope.TableName())
|
2014-11-26 08:23:43 +03:00
|
|
|
}
|
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
|
|
|
|
2015-02-17 15:19:47 +03:00
|
|
|
if joinTable := relationship.JoinTable; joinTable != "" {
|
|
|
|
quotedForeignDBName := scope.Quote(relationship.ForeignDBName)
|
|
|
|
foreignValue := scope.PrimaryKeyValue()
|
|
|
|
quoteAssociationForeignDBName := scope.Quote(relationship.AssociationForeignDBName)
|
|
|
|
associationForeignValue := 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,
|
2015-02-17 15:19:47 +03:00
|
|
|
strings.Join([]string{quotedForeignDBName, quoteAssociationForeignDBName}, ","),
|
2014-07-30 17:14:10 +04:00
|
|
|
strings.Join([]string{newScope.AddToVars(foreignValue), newScope.AddToVars(associationForeignValue)}, ","),
|
|
|
|
scope.Dialect().SelectFromDummyTable(),
|
|
|
|
joinTable,
|
2015-02-17 15:19:47 +03:00
|
|
|
quotedForeignDBName,
|
2014-07-30 17:14:10 +04:00
|
|
|
newScope.AddToVars(foreignValue),
|
2015-02-17 15:19:47 +03:00
|
|
|
quoteAssociationForeignDBName,
|
2014-07-30 17:14:10 +04:00
|
|
|
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:
|
2015-02-17 15:19:47 +03:00
|
|
|
elem := value.Addr().Interface()
|
|
|
|
newScope := scope.New(elem)
|
|
|
|
if relationship.ForeignFieldName != "" {
|
|
|
|
newScope.SetColumn(relationship.ForeignFieldName, scope.PrimaryKeyValue())
|
|
|
|
}
|
2014-01-27 04:26:59 +04:00
|
|
|
|
2015-02-17 15:19:47 +03:00
|
|
|
if relationship.ForeignType != "" {
|
|
|
|
newScope.SetColumn(relationship.ForeignType, scope.TableName())
|
2014-07-30 07:32:18 +04:00
|
|
|
}
|
2015-02-17 15:19:47 +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
|
|
|
}
|