preload_m2m improve
This commit is contained in:
parent
f35dee5531
commit
281c5d10f6
@ -62,14 +62,16 @@ func (structField *StructField) clone() *StructField {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Relationship struct {
|
type Relationship struct {
|
||||||
Kind string
|
Kind string
|
||||||
PolymorphicType string
|
PolymorphicType string
|
||||||
PolymorphicDBName string
|
PolymorphicDBName string
|
||||||
ForeignFieldNames []string
|
ForeignFieldNames []string
|
||||||
ForeignDBNames []string
|
ForeignStructFieldNames []string
|
||||||
AssociationForeignFieldNames []string
|
ForeignDBNames []string
|
||||||
AssociationForeignDBNames []string
|
AssociationForeignFieldNames []string
|
||||||
JoinTableHandler JoinTableHandlerInterface
|
AssociationForeignStructFieldNames []string
|
||||||
|
AssociationForeignDBNames []string
|
||||||
|
JoinTableHandler JoinTableHandlerInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) GetModelStruct() *ModelStruct {
|
func (scope *Scope) GetModelStruct() *ModelStruct {
|
||||||
@ -224,6 +226,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||||||
for _, foreignKey := range foreignKeys {
|
for _, foreignKey := range foreignKeys {
|
||||||
if field, ok := scope.FieldByName(foreignKey); ok {
|
if field, ok := scope.FieldByName(foreignKey); ok {
|
||||||
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, field.DBName)
|
relationship.ForeignFieldNames = append(relationship.ForeignFieldNames, field.DBName)
|
||||||
|
relationship.ForeignStructFieldNames = append(relationship.ForeignFieldNames, field.Name)
|
||||||
joinTableDBName := ToDBName(scopeType.Name()) + "_" + field.DBName
|
joinTableDBName := ToDBName(scopeType.Name()) + "_" + field.DBName
|
||||||
relationship.ForeignDBNames = append(relationship.ForeignDBNames, joinTableDBName)
|
relationship.ForeignDBNames = append(relationship.ForeignDBNames, joinTableDBName)
|
||||||
}
|
}
|
||||||
@ -242,6 +245,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||||||
for _, name := range associationForeignKeys {
|
for _, name := range associationForeignKeys {
|
||||||
if field, ok := toScope.FieldByName(name); ok {
|
if field, ok := toScope.FieldByName(name); ok {
|
||||||
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, field.DBName)
|
relationship.AssociationForeignFieldNames = append(relationship.AssociationForeignFieldNames, field.DBName)
|
||||||
|
relationship.AssociationForeignStructFieldNames = append(relationship.AssociationForeignFieldNames, field.Name)
|
||||||
joinTableDBName := ToDBName(elemType.Name()) + "_" + field.DBName
|
joinTableDBName := ToDBName(elemType.Name()) + "_" + field.DBName
|
||||||
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, joinTableDBName)
|
relationship.AssociationForeignDBNames = append(relationship.AssociationForeignDBNames, joinTableDBName)
|
||||||
}
|
}
|
||||||
|
17
preload.go
17
preload.go
@ -16,15 +16,6 @@ func getRealValue(value reflect.Value, columns []string) (results []interface{})
|
|||||||
result, _ = r.Value()
|
result, _ = r.Value()
|
||||||
}
|
}
|
||||||
results = append(results, result)
|
results = append(results, result)
|
||||||
} else {
|
|
||||||
column = upFL(column)
|
|
||||||
if reflect.Indirect(value).FieldByName(column).IsValid() {
|
|
||||||
result := reflect.Indirect(value).FieldByName(column).Interface()
|
|
||||||
if r, ok := result.(driver.Valuer); ok {
|
|
||||||
result, _ = r.Value()
|
|
||||||
}
|
|
||||||
results = append(results, result)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -283,11 +274,11 @@ func (scope *Scope) handleHasManyToManyPreload(field *Field, conditions []interf
|
|||||||
var checked []string
|
var checked []string
|
||||||
|
|
||||||
object := reflect.Indirect(objects.Index(j))
|
object := reflect.Indirect(objects.Index(j))
|
||||||
source := getRealValue(object, relation.AssociationForeignFieldNames)
|
source := getRealValue(object, relation.AssociationForeignStructFieldNames)
|
||||||
|
|
||||||
for i := 0; i < results.Len(); i++ {
|
for i := 0; i < results.Len(); i++ {
|
||||||
result := results.Index(i)
|
result := results.Index(i)
|
||||||
value := getRealValue(result, relation.ForeignFieldNames)
|
value := getRealValue(result, relation.ForeignStructFieldNames)
|
||||||
|
|
||||||
if strInSlice(toString(value), linkHash[toString(source)]) && !strInSlice(toString(value), checked) {
|
if strInSlice(toString(value), linkHash[toString(source)]) && !strInSlice(toString(value), checked) {
|
||||||
f := object.FieldByName(field.Name)
|
f := object.FieldByName(field.Name)
|
||||||
@ -300,11 +291,11 @@ func (scope *Scope) handleHasManyToManyPreload(field *Field, conditions []interf
|
|||||||
} else {
|
} else {
|
||||||
object := scope.IndirectValue()
|
object := scope.IndirectValue()
|
||||||
var checked []string
|
var checked []string
|
||||||
source := getRealValue(object, relation.AssociationForeignFieldNames)
|
source := getRealValue(object, relation.AssociationForeignStructFieldNames)
|
||||||
|
|
||||||
for i := 0; i < results.Len(); i++ {
|
for i := 0; i < results.Len(); i++ {
|
||||||
result := results.Index(i)
|
result := results.Index(i)
|
||||||
value := getRealValue(result, relation.ForeignFieldNames)
|
value := getRealValue(result, relation.ForeignStructFieldNames)
|
||||||
|
|
||||||
if strInSlice(toString(value), linkHash[toString(source)]) && !strInSlice(toString(value), checked) {
|
if strInSlice(toString(value), linkHash[toString(source)]) && !strInSlice(toString(value), checked) {
|
||||||
f := object.FieldByName(field.Name)
|
f := object.FieldByName(field.Name)
|
||||||
|
@ -5,8 +5,6 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"unicode"
|
|
||||||
"unicode/utf8"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func fileWithLineNum() string {
|
func fileWithLineNum() string {
|
||||||
@ -86,11 +84,3 @@ func strInSlice(a string, list []string) bool {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func upFL(s string) string {
|
|
||||||
if s == "" {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
r, n := utf8.DecodeRuneInString(s)
|
|
||||||
return string(unicode.ToUpper(r)) + s[n:]
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user