Merge 9757e285ad0ec4360ad433700b47a21086327a15 into cb7c41e0b6e3863e7934a50c0aed76b8cfb61bfd

This commit is contained in:
Jake Burkhead 2018-02-10 15:24:35 +00:00 committed by GitHub
commit 8c83c01fde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,6 +6,7 @@ import (
"reflect" "reflect"
"strconv" "strconv"
"strings" "strings"
"sync"
) )
// preloadCallback used to preload associations // preloadCallback used to preload associations
@ -25,7 +26,9 @@ func preloadCallback(scope *Scope) {
var ( var (
preloadedMap = map[string]bool{} preloadedMap = map[string]bool{}
fields = scope.Fields() fields = scope.Fields()
wg = &sync.WaitGroup{}
) )
defer wg.Wait()
for _, preload := range scope.Search.preload { for _, preload := range scope.Search.preload {
var ( var (
@ -54,16 +57,18 @@ func preloadCallback(scope *Scope) {
continue continue
} }
wg.Add(1)
switch field.Relationship.Kind { switch field.Relationship.Kind {
case "has_one": case "has_one":
currentScope.handleHasOnePreload(field, currentPreloadConditions) go currentScope.handleHasOnePreload(field, currentPreloadConditions, wg)
case "has_many": case "has_many":
currentScope.handleHasManyPreload(field, currentPreloadConditions) go currentScope.handleHasManyPreload(field, currentPreloadConditions, wg)
case "belongs_to": case "belongs_to":
currentScope.handleBelongsToPreload(field, currentPreloadConditions) go currentScope.handleBelongsToPreload(field, currentPreloadConditions, wg)
case "many_to_many": case "many_to_many":
currentScope.handleManyToManyPreload(field, currentPreloadConditions) go currentScope.handleManyToManyPreload(field, currentPreloadConditions, wg)
default: default:
wg.Done()
scope.Err(errors.New("unsupported relation")) scope.Err(errors.New("unsupported relation"))
} }
@ -86,6 +91,7 @@ func preloadCallback(scope *Scope) {
} }
} }
} }
} }
func autoPreload(scope *Scope) { func autoPreload(scope *Scope) {
@ -125,7 +131,8 @@ func (scope *Scope) generatePreloadDBWithConditions(conditions []interface{}) (*
} }
// handleHasOnePreload used to preload has one associations // handleHasOnePreload used to preload has one associations
func (scope *Scope) handleHasOnePreload(field *Field, conditions []interface{}) { func (scope *Scope) handleHasOnePreload(field *Field, conditions []interface{}, wg *sync.WaitGroup) {
defer wg.Done()
relation := field.Relationship relation := field.Relationship
// get relations's primary keys // get relations's primary keys
@ -174,7 +181,8 @@ func (scope *Scope) handleHasOnePreload(field *Field, conditions []interface{})
} }
// handleHasManyPreload used to preload has many associations // handleHasManyPreload used to preload has many associations
func (scope *Scope) handleHasManyPreload(field *Field, conditions []interface{}) { func (scope *Scope) handleHasManyPreload(field *Field, conditions []interface{}, wg *sync.WaitGroup) {
defer wg.Done()
relation := field.Relationship relation := field.Relationship
// get relations's primary keys // get relations's primary keys
@ -227,7 +235,8 @@ func (scope *Scope) handleHasManyPreload(field *Field, conditions []interface{})
} }
// handleBelongsToPreload used to preload belongs to associations // handleBelongsToPreload used to preload belongs to associations
func (scope *Scope) handleBelongsToPreload(field *Field, conditions []interface{}) { func (scope *Scope) handleBelongsToPreload(field *Field, conditions []interface{}, wg *sync.WaitGroup) {
defer wg.Done()
relation := field.Relationship relation := field.Relationship
// preload conditions // preload conditions
@ -266,7 +275,8 @@ func (scope *Scope) handleBelongsToPreload(field *Field, conditions []interface{
} }
// handleManyToManyPreload used to preload many to many associations // handleManyToManyPreload used to preload many to many associations
func (scope *Scope) handleManyToManyPreload(field *Field, conditions []interface{}) { func (scope *Scope) handleManyToManyPreload(field *Field, conditions []interface{}, wg *sync.WaitGroup) {
defer wg.Done()
var ( var (
relation = field.Relationship relation = field.Relationship
joinTableHandler = relation.JoinTableHandler joinTableHandler = relation.JoinTableHandler