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() {
|
|
|
|
if field.BeforeAssociation && !field.IsBlank && !field.IsIgnored {
|
|
|
|
value := reflect.ValueOf(field.Value)
|
|
|
|
newDB := scope.NewDB()
|
|
|
|
|
|
|
|
if value.CanAddr() {
|
2014-07-29 06:59:13 +04:00
|
|
|
scope.Err(newDB.Save(value.Addr().Interface()).Error)
|
2014-01-27 04:26:59 +04:00
|
|
|
} else {
|
|
|
|
// If can't take address, then clone the value and set it back
|
2014-01-27 06:47:37 +04:00
|
|
|
value = reflect.New(reflect.ValueOf(field.Value).Type()).Elem()
|
2014-01-27 04:26:59 +04:00
|
|
|
for _, f := range newDB.NewScope(field.Value).Fields() {
|
2014-01-27 06:47:37 +04:00
|
|
|
value.FieldByName(f.Name).Set(reflect.ValueOf(f.Value))
|
2014-01-27 04:26:59 +04:00
|
|
|
}
|
2014-07-29 06:59:13 +04:00
|
|
|
scope.Err(newDB.Save(value.Addr().Interface()).Error)
|
2014-01-27 06:47:37 +04:00
|
|
|
scope.SetColumn(field.Name, value.Interface())
|
2014-01-27 04:26:59 +04:00
|
|
|
}
|
|
|
|
|
2014-07-30 07:32:18 +04:00
|
|
|
if field.JoinTable != nil && field.JoinTable.foreignKey != "" {
|
|
|
|
scope.SetColumn(field.JoinTable.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.AfterAssociation && !field.IsBlank && !field.IsIgnored {
|
|
|
|
value := reflect.ValueOf(field.Value)
|
|
|
|
|
|
|
|
switch value.Kind() {
|
|
|
|
case reflect.Slice:
|
|
|
|
for i := 0; i < value.Len(); i++ {
|
|
|
|
newDB := scope.NewDB()
|
|
|
|
elem := value.Index(i).Addr().Interface()
|
|
|
|
|
2014-07-30 10:18:15 +04:00
|
|
|
if field.JoinTable != nil && field.JoinTable.joinTable == "" && field.JoinTable.foreignKey != "" {
|
2014-07-30 07:32:18 +04:00
|
|
|
newDB.NewScope(elem).SetColumn(field.JoinTable.foreignKey, scope.PrimaryKeyValue())
|
2014-01-27 04:26:59 +04:00
|
|
|
}
|
|
|
|
|
2014-07-29 06:59:13 +04:00
|
|
|
scope.Err(newDB.Save(elem).Error)
|
2014-07-30 10:18:15 +04:00
|
|
|
|
|
|
|
if field.JoinTable != nil && field.JoinTable.joinTable != "" {
|
|
|
|
newScope := scope.New(elem)
|
|
|
|
joinTable := field.JoinTable.joinTable
|
|
|
|
foreignKey := ToSnake(field.JoinTable.foreignKey)
|
|
|
|
foreignValue := fmt.Sprintf("%v", scope.PrimaryKeyValue())
|
|
|
|
associationForeignKey := ToSnake(field.JoinTable.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)
|
|
|
|
}
|
|
|
|
}
|
2014-01-27 04:26:59 +04:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
newDB := scope.NewDB()
|
|
|
|
if value.CanAddr() {
|
2014-07-30 07:32:18 +04:00
|
|
|
if field.JoinTable != nil {
|
|
|
|
newDB.NewScope(field.Value).SetColumn(field.JoinTable.foreignKey, scope.PrimaryKeyValue())
|
|
|
|
}
|
2014-07-29 06:59:13 +04:00
|
|
|
scope.Err(newDB.Save(field.Value).Error)
|
2014-01-27 04:26:59 +04:00
|
|
|
} else {
|
|
|
|
destValue := reflect.New(reflect.TypeOf(field.Value)).Elem()
|
|
|
|
|
2014-01-27 06:47:37 +04:00
|
|
|
for _, f := range newDB.NewScope(field.Value).Fields() {
|
2014-01-27 04:26:59 +04:00
|
|
|
destValue.FieldByName(f.Name).Set(reflect.ValueOf(f.Value))
|
|
|
|
}
|
|
|
|
|
|
|
|
elem := destValue.Addr().Interface()
|
2014-07-30 07:32:18 +04:00
|
|
|
if field.JoinTable != nil {
|
|
|
|
newDB.NewScope(elem).SetColumn(field.JoinTable.foreignKey, scope.PrimaryKeyValue())
|
|
|
|
}
|
2014-07-29 06:59:13 +04:00
|
|
|
scope.Err(newDB.Save(elem).Error)
|
2014-01-27 04:26:59 +04:00
|
|
|
scope.SetColumn(field.Name, destValue.Interface())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-26 15:34:06 +04:00
|
|
|
}
|