2016-09-15 05:45:09 +02:00
|
|
|
package queries
|
2016-09-03 08:53:37 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"reflect"
|
2016-09-29 04:57:38 +02:00
|
|
|
"strings"
|
2016-09-03 08:53:37 +02:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2016-09-15 05:45:09 +02:00
|
|
|
"github.com/vattle/sqlboiler/boil"
|
2016-09-03 08:53:37 +02:00
|
|
|
"github.com/vattle/sqlboiler/strmangle"
|
|
|
|
)
|
|
|
|
|
|
|
|
type loadRelationshipState struct {
|
2016-09-15 05:45:09 +02:00
|
|
|
exec boil.Executor
|
2016-09-03 08:53:37 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-09-29 04:57:38 +02:00
|
|
|
// eagerLoad loads all of the model's relationships
|
|
|
|
//
|
|
|
|
// toLoad should look like:
|
|
|
|
// []string{"Relationship", "Relationship.NestedRelationship"} ... etc
|
2016-09-29 08:57:15 +02:00
|
|
|
// obj should be one of:
|
|
|
|
// *[]*struct or *struct
|
|
|
|
// bkind should reflect what kind of thing it is above
|
2016-09-29 04:57:38 +02:00
|
|
|
func eagerLoad(exec boil.Executor, toLoad []string, obj interface{}, bkind bindKind) error {
|
|
|
|
state := loadRelationshipState{
|
|
|
|
exec: exec,
|
|
|
|
loaded: map[string]struct{}{},
|
|
|
|
}
|
|
|
|
for _, toLoad := range toLoad {
|
|
|
|
state.toLoad = strings.Split(toLoad, ".")
|
|
|
|
if err := state.loadRelationships(0, obj, bkind); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-03 08:53:37 +02:00
|
|
|
// 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.
|
2016-11-09 03:27:26 +01:00
|
|
|
// - bkind is passed in to identify whether or not this was a single object
|
2016-09-03 08:53:37 +02:00
|
|
|
// 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.
|
2016-09-29 08:57:15 +02:00
|
|
|
//
|
|
|
|
// We start with a normal select before eager loading anything: select * from a;
|
|
|
|
// Then we start eager loading things, it can be represented by a DAG
|
|
|
|
// a1, a2 select id, a_id from b where id in (a1, a2)
|
|
|
|
// / | \
|
|
|
|
// b1 b2 b3 select id, b_id from c where id in (b2, b3, b4)
|
|
|
|
// / | \ \
|
|
|
|
// c1 c2 c3 c4
|
|
|
|
//
|
|
|
|
// That's to say that we descend the graph of relationships, and at each level
|
|
|
|
// we gather all the things up we want to load into, load them, and then move
|
|
|
|
// to the next level of the graph.
|
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()
|
2016-09-03 19:33:28 +02:00
|
|
|
if bkind == kindPtrSliceStruct {
|
2016-09-03 08:53:37 +02:00
|
|
|
typ = typ.Elem().Elem()
|
|
|
|
}
|
|
|
|
|
2016-09-29 04:57:38 +02:00
|
|
|
loadingFrom := reflect.ValueOf(obj)
|
|
|
|
if loadingFrom.IsNil() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-05 13:33:18 +02:00
|
|
|
if !l.hasLoaded(depth) {
|
2016-09-29 08:57:15 +02:00
|
|
|
if err := l.callLoadFunction(depth, loadingFrom, typ, bkind); err != nil {
|
|
|
|
return err
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-29 04:57:38 +02:00
|
|
|
// Check if we can stop
|
|
|
|
if depth+1 >= len(l.toLoad) {
|
2016-09-03 08:53:37 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-29 08:57:15 +02:00
|
|
|
// *[]*struct -> []*struct
|
|
|
|
// *struct -> struct
|
|
|
|
loadingFrom = reflect.Indirect(loadingFrom)
|
|
|
|
|
2016-09-03 08:53:37 +02:00
|
|
|
// If it's singular we can just immediately call without looping
|
2016-09-03 19:33:28 +02:00
|
|
|
if bkind == kindStruct {
|
2016-09-29 04:57:38 +02:00
|
|
|
return l.loadRelationshipsRecurse(depth, loadingFrom)
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
|
|
|
|
2016-09-29 08:57:15 +02:00
|
|
|
// If we were an empty slice to begin with, bail, probably a useless check
|
|
|
|
if loadingFrom.Len() == 0 {
|
2016-09-03 08:53:37 +02:00
|
|
|
return nil
|
|
|
|
}
|
2016-09-29 08:57:15 +02:00
|
|
|
|
|
|
|
// Collect eagerly loaded things to send into next eager load call
|
|
|
|
slice, nextBKind, err := collectLoaded(l.toLoad[depth], loadingFrom)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we could collect nothing we're done
|
|
|
|
if slice.Len() == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr := reflect.New(slice.Type())
|
|
|
|
ptr.Elem().Set(slice)
|
|
|
|
|
|
|
|
return l.loadRelationships(depth+1, ptr.Interface(), nextBKind)
|
|
|
|
}
|
|
|
|
|
|
|
|
// callLoadFunction finds the loader struct, finds the method that we need
|
|
|
|
// to call and calls it.
|
|
|
|
func (l loadRelationshipState) callLoadFunction(depth int, loadingFrom reflect.Value, typ reflect.Type, bkind bindKind) error {
|
|
|
|
current := l.toLoad[depth]
|
|
|
|
ln, found := typ.FieldByName(loaderStructName)
|
|
|
|
// 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
|
|
|
|
loadMethod, found := ln.Type.MethodByName(loadMethodPrefix + current)
|
|
|
|
if !found {
|
|
|
|
return errors.Errorf("could not find %s%s method for eager loading", loadMethodPrefix, current)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hack to allow nil executors
|
|
|
|
execArg := reflect.ValueOf(l.exec)
|
|
|
|
if !execArg.IsValid() {
|
|
|
|
execArg = reflect.ValueOf((*sql.DB)(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a loader instance from anything we have, *struct, or *[]*struct
|
|
|
|
val := reflect.Indirect(loadingFrom)
|
|
|
|
if bkind == kindPtrSliceStruct {
|
2016-11-16 05:34:36 +01:00
|
|
|
if val.Len() == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2016-11-16 06:28:13 +01:00
|
|
|
val = val.Index(0)
|
|
|
|
if val.IsNil() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
val = reflect.Indirect(val)
|
2016-09-29 08:57:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
methodArgs := []reflect.Value{
|
|
|
|
val.FieldByName(loaderStructName),
|
|
|
|
execArg,
|
|
|
|
reflect.ValueOf(bkind == kindStruct),
|
|
|
|
loadingFrom,
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := loadMethod.Func.Call(methodArgs)
|
|
|
|
if intf := ret[0].Interface(); intf != nil {
|
|
|
|
return errors.Wrapf(intf.(error), "failed to eager load %s", current)
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
|
|
|
|
2016-09-29 08:57:15 +02:00
|
|
|
l.setLoaded(depth)
|
2016-09-03 08:53:37 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadRelationshipsRecurse is a helper function for taking a reflect.Value and
|
2016-09-29 08:57:15 +02:00
|
|
|
// Basically calls loadRelationships with: obj.R.EagerLoadedObj
|
|
|
|
// Called with an obj of *struct
|
2016-09-05 13:33:18 +02:00
|
|
|
func (l loadRelationshipState) loadRelationshipsRecurse(depth int, obj reflect.Value) error {
|
2016-09-29 08:57:15 +02:00
|
|
|
key := l.toLoad[depth]
|
|
|
|
r, err := findRelationshipStruct(obj)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to append loaded %s", key)
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
2016-09-29 04:57:38 +02:00
|
|
|
|
2016-09-29 08:57:15 +02:00
|
|
|
loadedObject := reflect.Indirect(r).FieldByName(key)
|
2016-11-16 06:28:13 +01:00
|
|
|
if loadedObject.IsNil() {
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-29 04:57:38 +02:00
|
|
|
|
2016-09-03 19:33:28 +02:00
|
|
|
bkind := kindStruct
|
2016-09-29 04:57:38 +02:00
|
|
|
if reflect.Indirect(loadedObject).Kind() != reflect.Struct {
|
2016-09-03 19:33:28 +02:00
|
|
|
bkind = kindPtrSliceStruct
|
2016-09-29 04:57:38 +02:00
|
|
|
loadedObject = loadedObject.Addr()
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|
2016-09-29 08:57:15 +02:00
|
|
|
return l.loadRelationships(depth+1, loadedObject.Interface(), bkind)
|
|
|
|
}
|
|
|
|
|
|
|
|
// collectLoaded traverses the next level of the graph and picks up all
|
|
|
|
// the values that we need for the next eager load query.
|
|
|
|
//
|
|
|
|
// For example when loadingFrom is [parent1, parent2]
|
|
|
|
//
|
|
|
|
// parent1 -> child1
|
|
|
|
// \-> child2
|
|
|
|
// parent2 -> child3
|
|
|
|
//
|
|
|
|
// This should return [child1, child2, child3]
|
|
|
|
func collectLoaded(key string, loadingFrom reflect.Value) (reflect.Value, bindKind, error) {
|
|
|
|
// Pull the first one so we can get the types out of it in order to
|
|
|
|
// create the proper type of slice.
|
|
|
|
current := reflect.Indirect(loadingFrom.Index(0))
|
|
|
|
lnFrom := loadingFrom.Len()
|
|
|
|
|
|
|
|
r, err := findRelationshipStruct(current)
|
|
|
|
if err != nil {
|
|
|
|
return reflect.Value{}, 0, errors.Wrapf(err, "failed to collect loaded %s", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
loadedObject := reflect.Indirect(r).FieldByName(key)
|
|
|
|
loadedType := loadedObject.Type() // Should be *obj or []*obj
|
|
|
|
|
2016-09-29 17:17:13 +02:00
|
|
|
bkind := kindPtrSliceStruct
|
|
|
|
if loadedType.Elem().Kind() == reflect.Struct {
|
|
|
|
bkind = kindStruct
|
|
|
|
loadedType = reflect.SliceOf(loadedType)
|
2016-09-29 08:57:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
collection := reflect.MakeSlice(loadedType, 0, 0)
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for {
|
|
|
|
switch bkind {
|
|
|
|
case kindStruct:
|
|
|
|
collection = reflect.Append(collection, loadedObject)
|
|
|
|
case kindPtrSliceStruct:
|
|
|
|
collection = reflect.AppendSlice(collection, loadedObject)
|
|
|
|
}
|
|
|
|
|
|
|
|
i++
|
|
|
|
if i >= lnFrom {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
current = reflect.Indirect(loadingFrom.Index(i))
|
|
|
|
r, err = findRelationshipStruct(current)
|
|
|
|
if err != nil {
|
|
|
|
return reflect.Value{}, 0, errors.Wrapf(err, "failed to collect loaded %s", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
loadedObject = reflect.Indirect(r).FieldByName(key)
|
|
|
|
}
|
|
|
|
|
2016-09-29 17:17:13 +02:00
|
|
|
return collection, kindPtrSliceStruct, nil
|
2016-09-29 08:57:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func findRelationshipStruct(obj reflect.Value) (reflect.Value, error) {
|
|
|
|
relationshipStruct := obj.FieldByName(relationshipStructName)
|
|
|
|
if !relationshipStruct.IsValid() {
|
|
|
|
return reflect.Value{}, errors.New("relationship struct was invalid")
|
|
|
|
} else if relationshipStruct.IsNil() {
|
|
|
|
return reflect.Value{}, errors.New("relationship struct was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
return relationshipStruct, nil
|
2016-09-03 08:53:37 +02:00
|
|
|
}
|