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