forked from mirror/gorm
Issue-860. Preload performance
This commit is contained in:
parent
aa45da1542
commit
b520f4bd1c
24
preload.go
24
preload.go
|
@ -150,20 +150,24 @@ func (scope *Scope) handleHasManyPreload(field *Field, conditions []interface{})
|
||||||
resultValues := reflect.Indirect(reflect.ValueOf(results))
|
resultValues := reflect.Indirect(reflect.ValueOf(results))
|
||||||
|
|
||||||
if scope.IndirectValue().Kind() == reflect.Slice {
|
if scope.IndirectValue().Kind() == reflect.Slice {
|
||||||
|
preloadMap := make(map[string][]reflect.Value)
|
||||||
for i := 0; i < resultValues.Len(); i++ {
|
for i := 0; i < resultValues.Len(); i++ {
|
||||||
result := resultValues.Index(i)
|
result := resultValues.Index(i)
|
||||||
value := getRealValue(result, relation.ForeignFieldNames)
|
value := getRealValue(result, relation.ForeignFieldNames)
|
||||||
objects := scope.IndirectValue()
|
preloadMap[toString(value)] = append(preloadMap[toString(value)], result)
|
||||||
for j := 0; j < objects.Len(); j++ {
|
}
|
||||||
object := reflect.Indirect(objects.Index(j))
|
|
||||||
if equalAsString(getRealValue(object, relation.AssociationForeignFieldNames), value) {
|
objects := scope.IndirectValue()
|
||||||
if object.Kind() == reflect.Ptr {
|
for j := 0; j < objects.Len(); j++ {
|
||||||
object = object.Elem()
|
object := reflect.Indirect(objects.Index(j))
|
||||||
}
|
objectRealValue := getRealValue(object, relation.AssociationForeignFieldNames)
|
||||||
f := object.FieldByName(field.Name)
|
objectStringValue := toString(objectRealValue)
|
||||||
f.Set(reflect.Append(f, result))
|
if results, ok := preloadMap[objectStringValue]; ok {
|
||||||
break
|
if object.Kind() == reflect.Ptr {
|
||||||
|
object = object.Elem()
|
||||||
}
|
}
|
||||||
|
f := object.FieldByName(field.Name)
|
||||||
|
f.Set(reflect.Append(f, results...))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue