forked from mirror/gorm
Replace BeforeAssociation, AfterAssociation with association type
This commit is contained in:
parent
2356182ea7
commit
6c4b635176
|
@ -16,24 +16,27 @@ func CommitOrRollbackTransaction(scope *Scope) {
|
||||||
|
|
||||||
func SaveBeforeAssociations(scope *Scope) {
|
func SaveBeforeAssociations(scope *Scope) {
|
||||||
for _, field := range scope.Fields() {
|
for _, field := range scope.Fields() {
|
||||||
if field.BeforeAssociation && !field.IsBlank && !field.IsIgnored {
|
if !field.IsBlank && !field.IsIgnored {
|
||||||
value := reflect.ValueOf(field.Value)
|
relationship := field.Relationship
|
||||||
newDB := scope.NewDB()
|
if relationship != nil && relationship.kind == "belongs_to" {
|
||||||
|
value := reflect.ValueOf(field.Value)
|
||||||
|
newDB := scope.NewDB()
|
||||||
|
|
||||||
if value.CanAddr() {
|
if value.CanAddr() {
|
||||||
scope.Err(newDB.Save(value.Addr().Interface()).Error)
|
scope.Err(newDB.Save(value.Addr().Interface()).Error)
|
||||||
} else {
|
} else {
|
||||||
// If can't take address, then clone the value and set it back
|
// If can't take address, then clone the value and set it back
|
||||||
value = reflect.New(reflect.ValueOf(field.Value).Type()).Elem()
|
value = reflect.New(reflect.ValueOf(field.Value).Type()).Elem()
|
||||||
for _, f := range newDB.NewScope(field.Value).Fields() {
|
for _, f := range newDB.NewScope(field.Value).Fields() {
|
||||||
value.FieldByName(f.Name).Set(reflect.ValueOf(f.Value))
|
value.FieldByName(f.Name).Set(reflect.ValueOf(f.Value))
|
||||||
|
}
|
||||||
|
scope.Err(newDB.Save(value.Addr().Interface()).Error)
|
||||||
|
scope.SetColumn(field.Name, value.Interface())
|
||||||
}
|
}
|
||||||
scope.Err(newDB.Save(value.Addr().Interface()).Error)
|
|
||||||
scope.SetColumn(field.Name, value.Interface())
|
|
||||||
}
|
|
||||||
|
|
||||||
if field.Relationship != nil && field.Relationship.foreignKey != "" {
|
if relationship.foreignKey != "" {
|
||||||
scope.SetColumn(field.Relationship.foreignKey, newDB.NewScope(value.Interface()).PrimaryKeyValue())
|
scope.SetColumn(relationship.foreignKey, newDB.NewScope(value.Interface()).PrimaryKeyValue())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,66 +44,70 @@ func SaveBeforeAssociations(scope *Scope) {
|
||||||
|
|
||||||
func SaveAfterAssociations(scope *Scope) {
|
func SaveAfterAssociations(scope *Scope) {
|
||||||
for _, field := range scope.Fields() {
|
for _, field := range scope.Fields() {
|
||||||
if field.AfterAssociation && !field.IsBlank && !field.IsIgnored {
|
if !field.IsBlank && !field.IsIgnored {
|
||||||
value := reflect.ValueOf(field.Value)
|
relationship := field.Relationship
|
||||||
|
if relationship != nil &&
|
||||||
|
(relationship.kind == "has_one" || relationship.kind == "has_many" || relationship.kind == "many_to_many") {
|
||||||
|
value := reflect.ValueOf(field.Value)
|
||||||
|
|
||||||
switch value.Kind() {
|
switch value.Kind() {
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
for i := 0; i < value.Len(); i++ {
|
for i := 0; i < value.Len(); i++ {
|
||||||
newDB := scope.NewDB()
|
newDB := scope.NewDB()
|
||||||
elem := value.Index(i).Addr().Interface()
|
elem := value.Index(i).Addr().Interface()
|
||||||
|
|
||||||
if field.Relationship != nil && field.Relationship.joinTable == "" && field.Relationship.foreignKey != "" {
|
if relationship.joinTable == "" && relationship.foreignKey != "" {
|
||||||
newDB.NewScope(elem).SetColumn(field.Relationship.foreignKey, scope.PrimaryKeyValue())
|
newDB.NewScope(elem).SetColumn(relationship.foreignKey, scope.PrimaryKeyValue())
|
||||||
}
|
}
|
||||||
|
|
||||||
scope.Err(newDB.Save(elem).Error)
|
scope.Err(newDB.Save(elem).Error)
|
||||||
|
|
||||||
if field.Relationship != nil && field.Relationship.joinTable != "" {
|
if relationship.joinTable != "" {
|
||||||
newScope := scope.New(elem)
|
newScope := scope.New(elem)
|
||||||
joinTable := field.Relationship.joinTable
|
joinTable := relationship.joinTable
|
||||||
foreignKey := ToSnake(field.Relationship.foreignKey)
|
foreignKey := ToSnake(relationship.foreignKey)
|
||||||
foreignValue := fmt.Sprintf("%v", scope.PrimaryKeyValue())
|
foreignValue := fmt.Sprintf("%v", scope.PrimaryKeyValue())
|
||||||
associationForeignKey := ToSnake(field.Relationship.associationForeignKey)
|
associationForeignKey := ToSnake(relationship.associationForeignKey)
|
||||||
associationForeignValue := fmt.Sprintf("%v", newScope.PrimaryKeyValue())
|
associationForeignValue := fmt.Sprintf("%v", newScope.PrimaryKeyValue())
|
||||||
|
|
||||||
newScope.Raw(fmt.Sprintf(
|
newScope.Raw(fmt.Sprintf(
|
||||||
"INSERT INTO %v (%v) SELECT %v %v WHERE NOT EXISTS (SELECT * FROM %v WHERE %v = %v AND %v = %v);",
|
"INSERT INTO %v (%v) SELECT %v %v WHERE NOT EXISTS (SELECT * FROM %v WHERE %v = %v AND %v = %v);",
|
||||||
joinTable,
|
joinTable,
|
||||||
strings.Join([]string{scope.Quote(foreignKey), scope.Quote(associationForeignKey)}, ","),
|
strings.Join([]string{scope.Quote(foreignKey), scope.Quote(associationForeignKey)}, ","),
|
||||||
strings.Join([]string{newScope.AddToVars(foreignValue), newScope.AddToVars(associationForeignValue)}, ","),
|
strings.Join([]string{newScope.AddToVars(foreignValue), newScope.AddToVars(associationForeignValue)}, ","),
|
||||||
scope.Dialect().SelectFromDummyTable(),
|
scope.Dialect().SelectFromDummyTable(),
|
||||||
joinTable,
|
joinTable,
|
||||||
scope.Quote(foreignKey),
|
scope.Quote(foreignKey),
|
||||||
newScope.AddToVars(foreignValue),
|
newScope.AddToVars(foreignValue),
|
||||||
scope.Quote(associationForeignKey),
|
scope.Quote(associationForeignKey),
|
||||||
newScope.AddToVars(associationForeignValue),
|
newScope.AddToVars(associationForeignValue),
|
||||||
))
|
))
|
||||||
if _, err := scope.DB().Exec(newScope.Sql, newScope.SqlVars...); err != nil {
|
if _, err := scope.DB().Exec(newScope.Sql, newScope.SqlVars...); err != nil {
|
||||||
scope.Err(err)
|
scope.Err(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
default:
|
||||||
default:
|
newDB := scope.NewDB()
|
||||||
newDB := scope.NewDB()
|
if value.CanAddr() {
|
||||||
if value.CanAddr() {
|
if relationship.foreignKey != "" {
|
||||||
if field.Relationship != nil {
|
newDB.NewScope(field.Value).SetColumn(relationship.foreignKey, scope.PrimaryKeyValue())
|
||||||
newDB.NewScope(field.Value).SetColumn(field.Relationship.foreignKey, scope.PrimaryKeyValue())
|
}
|
||||||
}
|
scope.Err(newDB.Save(field.Value).Error)
|
||||||
scope.Err(newDB.Save(field.Value).Error)
|
} else {
|
||||||
} else {
|
destValue := reflect.New(reflect.TypeOf(field.Value)).Elem()
|
||||||
destValue := reflect.New(reflect.TypeOf(field.Value)).Elem()
|
|
||||||
|
|
||||||
for _, f := range newDB.NewScope(field.Value).Fields() {
|
for _, f := range newDB.NewScope(field.Value).Fields() {
|
||||||
destValue.FieldByName(f.Name).Set(reflect.ValueOf(f.Value))
|
destValue.FieldByName(f.Name).Set(reflect.ValueOf(f.Value))
|
||||||
}
|
}
|
||||||
|
|
||||||
elem := destValue.Addr().Interface()
|
elem := destValue.Addr().Interface()
|
||||||
if field.Relationship != nil {
|
if relationship.foreignKey != "" {
|
||||||
newDB.NewScope(elem).SetColumn(field.Relationship.foreignKey, scope.PrimaryKeyValue())
|
newDB.NewScope(elem).SetColumn(relationship.foreignKey, scope.PrimaryKeyValue())
|
||||||
|
}
|
||||||
|
scope.Err(newDB.Save(elem).Error)
|
||||||
|
scope.SetColumn(field.Name, destValue.Interface())
|
||||||
}
|
}
|
||||||
scope.Err(newDB.Save(elem).Error)
|
|
||||||
scope.SetColumn(field.Name, destValue.Interface())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
21
field.go
21
field.go
|
@ -10,20 +10,19 @@ type relationship struct {
|
||||||
joinTable string
|
joinTable string
|
||||||
foreignKey string
|
foreignKey string
|
||||||
associationForeignKey string
|
associationForeignKey string
|
||||||
|
kind string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Field struct {
|
type Field struct {
|
||||||
Name string
|
Name string
|
||||||
DBName string
|
DBName string
|
||||||
Value interface{}
|
Value interface{}
|
||||||
IsBlank bool
|
IsBlank bool
|
||||||
IsIgnored bool
|
IsIgnored bool
|
||||||
Tag reflect.StructTag
|
Tag reflect.StructTag
|
||||||
SqlTag string
|
SqlTag string
|
||||||
BeforeAssociation bool
|
isPrimaryKey bool
|
||||||
AfterAssociation bool
|
Relationship *relationship
|
||||||
isPrimaryKey bool
|
|
||||||
Relationship *relationship
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Field) IsScanner() bool {
|
func (f *Field) IsScanner() bool {
|
||||||
|
|
15
scope.go
15
scope.go
|
@ -284,29 +284,30 @@ func (scope *Scope) fieldFromStruct(fieldStruct reflect.StructField) *Field {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
field.AfterAssociation = true
|
|
||||||
field.Relationship = &relationship{
|
field.Relationship = &relationship{
|
||||||
joinTable: many2many,
|
joinTable: many2many,
|
||||||
foreignKey: foreignKey,
|
foreignKey: foreignKey,
|
||||||
associationForeignKey: associationForeignKey,
|
associationForeignKey: associationForeignKey,
|
||||||
|
kind: "has_many",
|
||||||
|
}
|
||||||
|
|
||||||
|
if many2many != "" {
|
||||||
|
field.Relationship.kind = "many_to_many"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
if !field.IsTime() && !field.IsScanner() {
|
if !field.IsTime() && !field.IsScanner() {
|
||||||
if foreignKey == "" && scope.HasColumn(field.Name+"Id") {
|
if foreignKey == "" && scope.HasColumn(field.Name+"Id") {
|
||||||
field.Relationship = &relationship{foreignKey: field.Name + "Id"}
|
field.Relationship = &relationship{foreignKey: field.Name + "Id", kind: "belongs_to"}
|
||||||
field.BeforeAssociation = true
|
|
||||||
} else if scope.HasColumn(foreignKey) {
|
} else if scope.HasColumn(foreignKey) {
|
||||||
field.Relationship = &relationship{foreignKey: foreignKey}
|
field.Relationship = &relationship{foreignKey: foreignKey, kind: "belongs_to"}
|
||||||
field.BeforeAssociation = true
|
|
||||||
} else {
|
} else {
|
||||||
if foreignKey == "" {
|
if foreignKey == "" {
|
||||||
foreignKey = scopeTyp.Name() + "Id"
|
foreignKey = scopeTyp.Name() + "Id"
|
||||||
}
|
}
|
||||||
if reflect.New(typ).Elem().FieldByName(foreignKey).IsValid() {
|
if reflect.New(typ).Elem().FieldByName(foreignKey).IsValid() {
|
||||||
field.Relationship = &relationship{foreignKey: foreignKey}
|
field.Relationship = &relationship{foreignKey: foreignKey, kind: "has_one"}
|
||||||
}
|
}
|
||||||
field.AfterAssociation = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue