2016-08-11 23:16:06 -07:00
{ { - if . Table . IsJoinTable - } }
2016-08-21 16:28:47 +10:00
{ { - else - } }
2016-09-14 18:08:30 +10:00
{ { - $dot : = . - } }
{ { - range . Table . ToManyRelationships - } }
2016-09-17 23:50:31 -07:00
{ { - $varNameSingular : = $dot.Table.Name | singular | camelCase - } }
2016-09-18 11:18:43 -07:00
{ { - $txt : = txtsFromToMany $dot.Tables $dot.Table . - } }
2016-09-17 23:50:31 -07:00
{ { - $arg : = printf "maybe%s" $txt.LocalTable.NameGo - } }
{ { - $schemaForeignTable : = . ForeignTable | $dot.SchemaTable } }
2016-08-30 02:36:07 +10:00
// Load { { $txt.Function.Name } } allows an eager lookup of values, cached into the
2016-08-22 21:53:37 -07:00
// loaded structs of the objects.
2016-09-02 11:22:56 +10:00
func ( { { $varNameSingular } } L) Load { { $txt.Function.Name } } (e boil.Executor, singular bool, { { $arg } } interface { } ) error {
2016-09-14 18:08:30 +10:00
var slice []* { { $txt.LocalTable.NameGo } }
var object * { { $txt.LocalTable.NameGo } }
2016-08-11 23:16:06 -07:00
2016-09-14 18:08:30 +10:00
count := 1
if singular {
object = { { $arg } } .(* { { $txt.LocalTable.NameGo } } )
} else {
2017-04-04 19:44:36 -07:00
slice = * { { $arg } } .(*[]* { { $txt.LocalTable.NameGo } } )
2016-09-14 18:08:30 +10:00
count = len(slice)
}
2016-08-11 23:16:06 -07:00
2016-09-14 18:08:30 +10:00
args := make([]interface { } , count)
if singular {
2016-11-08 18:25:15 -08:00
if object.R == nil {
object.R = & { { $varNameSingular } } R { }
}
2016-09-14 18:08:30 +10:00
args[0] = object. { { . Column | titleCase } }
} else {
for i, obj := range slice {
2016-11-08 18:25:15 -08:00
if obj.R == nil {
obj.R = & { { $varNameSingular } } R { }
}
2016-09-14 18:08:30 +10:00
args[i] = obj. { { . Column | titleCase } }
}
}
2016-08-11 23:16:06 -07:00
2016-09-14 18:08:30 +10:00
{ { if . ToJoinTable - } }
{ { - $schemaJoinTable : = . JoinTable | $dot.SchemaTable - } }
query := fmt.Sprintf(
"select { { id 0 | $dot.Quotes } } .*, { { id 1 | $dot.Quotes } } . { { . JoinLocalColumn | $dot.Quotes } } from { { $schemaForeignTable } } as { { id 0 | $dot.Quotes } } inner join { { $schemaJoinTable } } as { { id 1 | $dot.Quotes } } on { { id 0 | $dot.Quotes } } . { { . ForeignColumn | $dot.Quotes } } = { { id 1 | $dot.Quotes } } . { { . JoinForeignColumn | $dot.Quotes } } where { { id 1 | $dot.Quotes } } . { { . JoinLocalColumn | $dot.Quotes } } in (%s)",
strmangle.Placeholders(dialect.IndexPlaceholders, count, 1, 1),
)
{ { else - } }
query := fmt.Sprintf(
"select * from { { $schemaForeignTable } } where { { . ForeignColumn | $dot.Quotes } } in (%s)",
strmangle.Placeholders(dialect.IndexPlaceholders, count, 1, 1),
)
{ { end - } }
2016-08-15 22:24:56 -07:00
2016-09-14 18:08:30 +10:00
if boil.DebugMode {
2017-07-06 15:15:00 -04:00
qStr, err := interpolateParams(query, args...)
if err != nil {
return err
}
fmt.Fprintln(boil.DebugWriter, qStr)
2016-09-14 18:08:30 +10:00
}
2016-08-16 21:08:55 -07:00
2016-09-14 18:08:30 +10:00
results, err := e.Query(query, args...)
if err != nil {
return errors.Wrap(err, "failed to eager load { { . ForeignTable } } ")
}
defer results.Close()
2016-08-11 23:16:06 -07:00
2016-09-14 18:08:30 +10:00
var resultSlice []* { { $txt.ForeignTable.NameGo } }
{ { 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( { { $txt.ForeignTable.NameGo } } )
var localJoinCol { { $localCol.Type } }
2016-08-17 23:07:39 -07:00
2016-09-14 18:08:30 +10:00
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 } } ")
}
2016-08-17 23:07:39 -07:00
2016-09-14 18:08:30 +10:00
resultSlice = append(resultSlice, one)
localJoinCols = append(localJoinCols, localJoinCol)
}
2016-08-17 23:07:39 -07:00
2016-09-14 18:08:30 +10:00
if err = results.Err(); err != nil {
return errors.Wrap(err, "failed to plebian-bind eager loaded slice { { . ForeignTable } } ")
}
{ { else - } }
2016-09-14 20:59:55 -07:00
if err = queries.Bind(results, &resultSlice); err != nil {
2016-09-14 18:08:30 +10:00
return errors.Wrap(err, "failed to bind eager loaded slice { { . ForeignTable } } ")
}
{ { end } }
2016-08-11 23:16:06 -07:00
2016-09-14 18:08:30 +10:00
{ { if not $dot.NoHooks - } }
if len( { { . ForeignTable | singular | camelCase } } AfterSelectHooks) != 0 {
for _, obj := range resultSlice {
if err := obj.doAfterSelectHooks(e); err != nil {
return err
}
}
}
2016-08-30 02:36:07 +10:00
2016-09-14 18:08:30 +10:00
{ { - end } }
if singular {
object.R. { { $txt.Function.Name } } = resultSlice
return nil
}
2016-08-11 23:16:06 -07:00
2016-09-14 18:08:30 +10:00
{ { if . ToJoinTable - } }
for i, foreign := range resultSlice {
localJoinCol := localJoinCols[i]
for _, local := range slice {
2016-09-17 00:02:03 -07:00
{ { if $txt.Function.UsesBytes - } }
2016-09-16 00:22:12 -07:00
if 0 == bytes.Compare(local. { { $txt.Function.LocalAssignment } } , localJoinCol) {
{ { else - } }
2016-09-14 18:08:30 +10:00
if local. { { $txt.Function.LocalAssignment } } == localJoinCol {
2016-09-16 00:22:12 -07:00
{ { end - } }
2016-09-14 18:08:30 +10:00
local.R. { { $txt.Function.Name } } = append(local.R. { { $txt.Function.Name } } , foreign)
break
}
}
}
{ { else - } }
for _, foreign := range resultSlice {
for _, local := range slice {
2016-09-17 00:02:03 -07:00
{ { if $txt.Function.UsesBytes - } }
2016-09-16 00:22:12 -07:00
if 0 == bytes.Compare(local. { { $txt.Function.LocalAssignment } } , foreign. { { $txt.Function.ForeignAssignment } } ) {
{ { else - } }
2016-09-14 18:08:30 +10:00
if local. { { $txt.Function.LocalAssignment } } == foreign. { { $txt.Function.ForeignAssignment } } {
2016-09-16 00:22:12 -07:00
{ { end - } }
2016-09-14 18:08:30 +10:00
local.R. { { $txt.Function.Name } } = append(local.R. { { $txt.Function.Name } } , foreign)
break
}
}
}
{ { end } }
2016-08-11 23:16:06 -07:00
2016-09-14 18:08:30 +10:00
return nil
2016-08-11 23:16:06 -07:00
}
2016-09-17 23:50:31 -07:00
{ { end - } } { { / * range tomany * / } }
2016-09-09 22:31:51 +10:00
{ { - end - } } { { / * if IsJoinTable * / } }