2016-08-12 08:16:06 +02:00
{ { - if . Table . IsJoinTable - } }
2016-08-21 08:28:47 +02:00
{ { - else - } }
2016-08-12 08:16:06 +02:00
{ { - $dot : = . - } }
{ { - range . Table . ToManyRelationships - } }
2016-08-16 09:30:08 +02:00
{ { - if ( and . ForeignColumnUnique ( not . ToJoinTable ) ) - } }
2016-08-12 08:16:06 +02:00
{ { - template "relationship_to_one_eager_helper" ( textsFromOneToOneRelationship $dot.PkgName $dot.Tables $dot.Table . ) - } }
{ { - else - } }
{ { - $rel : = textsFromRelationship $dot.Tables $dot.Table . - } }
{ { - $arg : = printf "maybe%s" $rel.LocalTable.NameGo - } }
2016-08-21 08:28:47 +02:00
{ { - $slice : = printf "%sSlice" $rel.LocalTable.NameGo - } }
2016-08-12 08:16:06 +02:00
// Load { { $rel.Function.Name } } allows an eager lookup of values, cached into the
2016-08-23 06:53:37 +02:00
// loaded structs of the objects.
2016-08-27 06:40:11 +02:00
func (r * { { $rel.LocalTable.NameGo } } R) Load { { $rel.Function.Name } } (e boil.Executor, singular bool, { { $arg } } interface { } ) error {
2016-08-12 08:16:06 +02:00
var slice []* { { $rel.LocalTable.NameGo } }
var object * { { $rel.LocalTable.NameGo } }
count := 1
if singular {
object = { { $arg } } .(* { { $rel.LocalTable.NameGo } } )
} else {
2016-08-24 07:07:51 +02:00
slice = * { { $arg } } .(* { { $slice } } )
2016-08-12 08:16:06 +02:00
count = len(slice)
}
2016-08-16 09:30:08 +02:00
args := make([]interface { } , count)
2016-08-12 08:16:06 +02:00
if singular {
2016-08-16 09:30:08 +02:00
args[0] = object. { { . Column | titleCase } }
2016-08-12 08:16:06 +02:00
} else {
2016-08-16 09:30:08 +02:00
for i, obj := range slice {
args[i] = obj. { { . Column | titleCase } }
}
2016-08-12 08:16:06 +02:00
}
2016-08-16 09:30:08 +02:00
{ { if . ToJoinTable - } }
query := fmt.Sprintf(
2016-08-18 08:07:39 +02:00
`select " { { id 0 } } ".*, " { { id 1 } } "." { { . JoinLocalColumn } } " from " { { . ForeignTable } } " as " { { id 0 } } " inner join " { { . JoinTable } } " as " { { id 1 } } " on " { { id 0 } } "." { { . ForeignColumn } } " = " { { id 1 } } "." { { . JoinForeignColumn } } " where " { { id 1 } } "." { { . JoinLocalColumn } } " in (%s)`,
2016-08-16 09:30:08 +02:00
strmangle.Placeholders(count, 1, 1),
)
{ { else - } }
2016-08-16 07:24:56 +02:00
query := fmt.Sprintf(
`select * from " { { . ForeignTable } } " where " { { . ForeignColumn } } " in (%s)`,
strmangle.Placeholders(count, 1, 1),
)
2016-08-16 09:30:08 +02:00
{ { end - } }
2016-08-16 07:24:56 +02:00
2016-08-17 06:08:55 +02:00
if boil.DebugMode {
fmt.Fprintf(boil.DebugWriter, "%s\n%v\n", query, args)
}
2016-08-12 08:16:06 +02:00
results, err := e.Query(query, args...)
if err != nil {
return errors.Wrap(err, "failed to eager load { { . ForeignTable } } ")
}
2016-08-16 07:24:56 +02:00
defer results.Close()
2016-08-12 08:16:06 +02:00
var resultSlice []* { { $rel.ForeignTable.NameGo } }
2016-08-18 08:07:39 +02:00
{ { if . ToJoinTable - } }
{ { - $foreignTable : = getTable $dot.Tables . ForeignTable - } }
{ { - $joinTable : = getTable $dot.Tables . JoinTable - } }
{ { - $localCol : = $joinTable.GetColumn . JoinLocalColumn } }
var localJoinCols [] { { $localCol.Type } }
for results.Next() {
one := new( { { $rel.ForeignTable.NameGo } } )
var localJoinCol { { $localCol.Type } }
err = results.Scan( { { $foreignTable.Columns | columnNames | stringMap $dot.StringFuncs.titleCase | prefixStringSlice "&one." | join ", " } } , &localJoinCol)
if err = results.Err(); err != nil {
return errors.Wrap(err, "failed to plebian-bind eager loaded slice { { . ForeignTable } } ")
}
resultSlice = append(resultSlice, one)
localJoinCols = append(localJoinCols, localJoinCol)
}
if err = results.Err(); err != nil {
return errors.Wrap(err, "failed to plebian-bind eager loaded slice { { . ForeignTable } } ")
}
{ { else - } }
2016-08-24 08:20:41 +02:00
if err = boil.BindFast(results, &resultSlice, { { $dot.Table.Name | singular | camelCase } } TitleCases); err != nil {
2016-08-12 08:16:06 +02:00
return errors.Wrap(err, "failed to bind eager loaded slice { { . ForeignTable } } ")
}
2016-08-18 08:07:39 +02:00
{ { end } }
2016-08-12 08:16:06 +02:00
if singular {
2016-08-27 06:40:11 +02:00
if object.R == nil {
object.R = & { { $rel.LocalTable.NameGo } } R { }
2016-08-12 08:16:06 +02:00
}
2016-08-27 06:40:11 +02:00
object.R. { { $rel.Function.Name } } = resultSlice
2016-08-12 08:16:06 +02:00
return nil
}
2016-08-18 08:07:39 +02:00
{ { if . ToJoinTable - } }
for i, foreign := range resultSlice {
localJoinCol := localJoinCols[i]
for _, local := range slice {
if local. { { $rel.Function.LocalAssignment } } == localJoinCol {
2016-08-27 06:40:11 +02:00
if local.R == nil {
local.R = & { { $rel.LocalTable.NameGo } } R { }
2016-08-18 08:07:39 +02:00
}
2016-08-27 06:40:11 +02:00
local.R. { { $rel.Function.Name } } = append(local.R. { { $rel.Function.Name } } , foreign)
2016-08-18 08:07:39 +02:00
break
}
}
}
{ { else - } }
2016-08-12 08:16:06 +02:00
for _, foreign := range resultSlice {
for _, local := range slice {
if local. { { $rel.Function.LocalAssignment } } == foreign. { { $rel.Function.ForeignAssignment } } {
2016-08-27 06:40:11 +02:00
if local.R == nil {
local.R = & { { $rel.LocalTable.NameGo } } R { }
2016-08-12 08:16:06 +02:00
}
2016-08-27 06:40:11 +02:00
local.R. { { $rel.Function.Name } } = append(local.R. { { $rel.Function.Name } } , foreign)
2016-08-12 08:16:06 +02:00
break
}
}
}
2016-08-18 08:07:39 +02:00
{ { end } }
2016-08-12 08:16:06 +02:00
return nil
}
{ { end - } } { { / * if ForeignColumnUnique * / } }
{ { - end - } } { { / * range tomany * / } }
{ { - end - } } { { / * if isjointable * / } }