sqlboiler/boil/queries/eager_load.go

148 lines
4.1 KiB
Go
Raw Normal View History

2016-09-03 08:53:37 +02:00
package boil
import (
"database/sql"
"reflect"
"github.com/pkg/errors"
"github.com/vattle/sqlboiler/strmangle"
)
type loadRelationshipState struct {
exec Executor
loaded map[string]struct{}
toLoad []string
}
func (l loadRelationshipState) hasLoaded(depth int) bool {
_, ok := l.loaded[l.buildKey(depth)]
return ok
}
func (l loadRelationshipState) setLoaded(depth int) {
l.loaded[l.buildKey(depth)] = struct{}{}
}
func (l loadRelationshipState) buildKey(depth int) string {
buf := strmangle.GetBuffer()
for i, piece := range l.toLoad[:depth+1] {
if i != 0 {
buf.WriteByte('.')
}
buf.WriteString(piece)
}
str := buf.String()
strmangle.PutBuffer(buf)
return str
}
// loadRelationships dynamically calls the template generated eager load
// functions of the form:
//
// func (t *TableR) LoadRelationshipName(exec Executor, singular bool, obj interface{})
//
// The arguments to this function are:
// - t is not considered here, and is always passed nil. The function exists on a loaded
// struct to avoid a circular dependency with boil, and the receiver is ignored.
// - exec is used to perform additional queries that might be required for loading the relationships.
// - singular is passed in to identify whether or not this was a single object
// or a slice that must be loaded into.
// - obj is the object or slice of objects, always of the type *obj or *[]*obj as per bind.
//
// It takes list of nested relationships to load.
2016-09-05 13:33:18 +02:00
func (l loadRelationshipState) loadRelationships(depth int, obj interface{}, bkind bindKind) error {
2016-09-03 08:53:37 +02:00
typ := reflect.TypeOf(obj).Elem()
if bkind == kindPtrSliceStruct {
2016-09-03 08:53:37 +02:00
typ = typ.Elem().Elem()
}
2016-09-05 13:33:18 +02:00
if !l.hasLoaded(depth) {
current := l.toLoad[depth]
ln, found := typ.FieldByName(loaderStructName)
2016-09-03 08:53:37 +02:00
// It's possible a Loaders struct doesn't exist on the struct.
if !found {
return errors.Errorf("attempted to load %s but no L struct was found", current)
}
// Attempt to find the LoadRelationshipName function
2016-09-05 13:33:18 +02:00
loadMethod, found := ln.Type.MethodByName(loadMethodPrefix + current)
2016-09-03 08:53:37 +02:00
if !found {
return errors.Errorf("could not find %s%s method for eager loading", loadMethodPrefix, current)
}
// Hack to allow nil executors
2016-09-05 13:33:18 +02:00
execArg := reflect.ValueOf(l.exec)
2016-09-03 08:53:37 +02:00
if !execArg.IsValid() {
execArg = reflect.ValueOf((*sql.DB)(nil))
}
val := reflect.ValueOf(obj).Elem()
if bkind == kindPtrSliceStruct {
2016-09-03 08:53:37 +02:00
val = val.Index(0).Elem()
}
methodArgs := []reflect.Value{
val.FieldByName(loaderStructName),
execArg,
reflect.ValueOf(bkind == kindStruct),
2016-09-03 08:53:37 +02:00
reflect.ValueOf(obj),
}
resp := loadMethod.Func.Call(methodArgs)
if intf := resp[0].Interface(); intf != nil {
return errors.Wrapf(intf.(error), "failed to eager load %s", current)
}
2016-09-05 13:33:18 +02:00
l.setLoaded(depth)
2016-09-03 08:53:37 +02:00
}
// Pull one off the queue, continue if there's still some to go
depth++
2016-09-05 13:33:18 +02:00
if depth >= len(l.toLoad) {
2016-09-03 08:53:37 +02:00
return nil
}
loadedObject := reflect.ValueOf(obj)
// If we eagerly loaded nothing
if loadedObject.IsNil() {
return nil
}
loadedObject = reflect.Indirect(loadedObject)
// If it's singular we can just immediately call without looping
if bkind == kindStruct {
2016-09-05 13:33:18 +02:00
return l.loadRelationshipsRecurse(depth, loadedObject)
2016-09-03 08:53:37 +02:00
}
// Loop over all eager loaded objects
ln := loadedObject.Len()
if ln == 0 {
return nil
}
for i := 0; i < ln; i++ {
iter := loadedObject.Index(i).Elem()
2016-09-05 13:33:18 +02:00
if err := l.loadRelationshipsRecurse(depth, iter); err != nil {
2016-09-03 08:53:37 +02:00
return err
}
}
return nil
}
// loadRelationshipsRecurse is a helper function for taking a reflect.Value and
// Basically calls loadRelationships with: obj.R.EagerLoadedObj, and whether it's a string or slice
2016-09-05 13:33:18 +02:00
func (l loadRelationshipState) loadRelationshipsRecurse(depth int, obj reflect.Value) error {
2016-09-03 08:53:37 +02:00
r := obj.FieldByName(relationshipStructName)
if !r.IsValid() || r.IsNil() {
2016-09-05 13:33:18 +02:00
return errors.Errorf("could not traverse into loaded %s relationship to load more things", l.toLoad[depth])
2016-09-03 08:53:37 +02:00
}
2016-09-05 13:33:18 +02:00
newObj := reflect.Indirect(r).FieldByName(l.toLoad[depth])
bkind := kindStruct
if reflect.Indirect(newObj).Kind() != reflect.Struct {
bkind = kindPtrSliceStruct
2016-09-03 08:53:37 +02:00
newObj = newObj.Addr()
}
2016-09-05 13:33:18 +02:00
return l.loadRelationships(depth, newObj.Interface(), bkind)
2016-09-03 08:53:37 +02:00
}