2016-08-16 09:30:08 +02:00
{ { - define "relationship_to_one_eager_helper" - } }
2016-09-09 14:31:51 +02:00
{ { - $tmplData : = . Dot - } } { { / * . Dot holds the root templateData struct , passed in through preserveDot * / } }
{ { - $varNameSingular : = $tmplData.Table.Name | singular | camelCase - } }
2016-08-29 18:36:07 +02:00
{ { - with . Rel - } }
2016-08-16 09:30:08 +02:00
{ { - $arg : = printf "maybe%s" . LocalTable . NameGo - } }
2016-08-21 08:28:47 +02:00
{ { - $slice : = printf "%sSlice" . LocalTable . NameGo - } }
2016-08-16 09:30:08 +02:00
// Load { { . 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-09-02 03:22:56 +02:00
func ( { { $varNameSingular } } L) Load { { . Function . Name } } (e boil.Executor, singular bool, { { $arg } } interface { } ) error {
2016-08-16 09:30:08 +02:00
var slice []* { { . LocalTable . NameGo } }
var object * { { . LocalTable . NameGo } }
count := 1
if singular {
object = { { $arg } } .(* { { . LocalTable . NameGo } } )
} else {
2016-08-24 07:07:51 +02:00
slice = * { { $arg } } .(* { { $slice } } )
2016-08-16 09:30:08 +02:00
count = len(slice)
}
args := make([]interface { } , count)
if singular {
args[0] = object. { { . LocalTable . ColumnNameGo } }
} else {
for i, obj := range slice {
args[i] = obj. { { . LocalTable . ColumnNameGo } }
}
}
query := fmt.Sprintf(
2016-09-09 21:15:50 +02:00
`select * from { { schemaTable $tmplData.Dialect.LQ $tmplData.Dialect.RQ $tmplData.DriverName $tmplData.Schema . ForeignKey . ForeignTable } } where " { { . ForeignKey . ForeignColumn } } " in (%s)`,
strmangle.Placeholders(dialect.IndexPlaceholders, count, 1, 1),
2016-08-16 09:30:08 +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-16 09:30:08 +02:00
results, err := e.Query(query, args...)
if err != nil {
return errors.Wrap(err, "failed to eager load { { . ForeignTable . NameGo } } ")
}
defer results.Close()
var resultSlice []* { { . ForeignTable . NameGo } }
2016-09-02 09:09:42 +02:00
if err = boil.Bind(results, &resultSlice); err != nil {
2016-08-29 18:36:07 +02:00
return errors.Wrap(err, "failed to bind eager loaded slice { { . ForeignTable . NameGo } } ")
2016-08-16 09:30:08 +02:00
}
2016-09-09 14:31:51 +02:00
{ { if not $tmplData.NoHooks - } }
2016-08-29 18:36:07 +02:00
if len( { { . ForeignTable . Name | singular | camelCase } } AfterSelectHooks) != 0 {
for _, obj := range resultSlice {
if err := obj.doAfterSelectHooks(e); err != nil {
return err
}
}
}
{ { - end } }
2016-08-16 09:30:08 +02:00
if singular && len(resultSlice) != 0 {
2016-08-27 06:40:11 +02:00
if object.R == nil {
2016-09-01 05:33:05 +02:00
object.R = & { { $varNameSingular } } R { }
2016-08-16 09:30:08 +02:00
}
2016-08-27 06:40:11 +02:00
object.R. { { . Function . Name } } = resultSlice[0]
2016-08-16 09:30:08 +02:00
return nil
}
for _, foreign := range resultSlice {
for _, local := range slice {
if local. { { . Function . LocalAssignment } } == foreign. { { . Function . ForeignAssignment } } {
2016-08-27 06:40:11 +02:00
if local.R == nil {
2016-09-01 05:33:05 +02:00
local.R = & { { $varNameSingular } } R { }
2016-08-16 09:30:08 +02:00
}
2016-08-27 06:40:11 +02:00
local.R. { { . Function . Name } } = foreign
2016-08-16 09:30:08 +02:00
break
}
}
}
return nil
}
2016-09-08 23:23:10 +02:00
{ { - end - } } { { - / * end with * / - } }
{ { end - } } { { - / * end define * / - } }
2016-09-09 14:31:51 +02:00
{ { - / * Begin execution of template for one - to - one eager load * / - } }
2016-08-16 09:30:08 +02:00
{ { - if . Table . IsJoinTable - } }
{ { - else - } }
{ { - $dot : = . - } }
{ { - range . Table . FKeys - } }
2016-09-09 14:31:51 +02:00
{ { - $txt : = textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . - } }
{ { - template "relationship_to_one_eager_helper" ( preserveDot $dot $txt ) - } }
{ { - end - } }
2016-08-29 18:36:07 +02:00
{ { end } }