2016-09-18 20:13:11 +02:00
|
|
|
{{- if .Table.IsJoinTable -}}
|
|
|
|
{{- else -}}
|
|
|
|
{{- $dot := . -}}
|
|
|
|
{{- range .Table.FKeys -}}
|
2016-09-19 01:02:08 +02:00
|
|
|
{{- $txt := txtsFromFKey $dot.Tables $dot.Table . -}}
|
2016-09-18 20:13:11 +02:00
|
|
|
{{- $varNameSingular := $dot.Table.Name | singular | camelCase -}}
|
2017-05-09 21:30:04 +02:00
|
|
|
{{- $arg := printf "maybe%s" $txt.LocalTable.NameGo}}
|
2016-09-18 20:13:11 +02:00
|
|
|
// Load{{$txt.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-18 20:13:11 +02:00
|
|
|
func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error {
|
|
|
|
var slice []*{{$txt.LocalTable.NameGo}}
|
|
|
|
var object *{{$txt.LocalTable.NameGo}}
|
2016-08-16 09:30:08 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
count := 1
|
|
|
|
if singular {
|
2016-09-18 20:13:11 +02:00
|
|
|
object = {{$arg}}.(*{{$txt.LocalTable.NameGo}})
|
2016-09-14 10:08:30 +02:00
|
|
|
} else {
|
2017-04-05 04:44:36 +02:00
|
|
|
slice = *{{$arg}}.(*[]*{{$txt.LocalTable.NameGo}})
|
2016-09-14 10:08:30 +02:00
|
|
|
count = len(slice)
|
|
|
|
}
|
2016-08-16 09:30:08 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
args := make([]interface{}, count)
|
|
|
|
if singular {
|
2016-11-09 03:25:15 +01:00
|
|
|
if object.R == nil {
|
|
|
|
object.R = &{{$varNameSingular}}R{}
|
|
|
|
}
|
2016-09-18 20:13:11 +02:00
|
|
|
args[0] = object.{{$txt.LocalTable.ColumnNameGo}}
|
2016-09-14 10:08:30 +02:00
|
|
|
} else {
|
|
|
|
for i, obj := range slice {
|
2016-11-09 03:25:15 +01:00
|
|
|
if obj.R == nil {
|
|
|
|
obj.R = &{{$varNameSingular}}R{}
|
|
|
|
}
|
2016-09-18 20:13:11 +02:00
|
|
|
args[i] = obj.{{$txt.LocalTable.ColumnNameGo}}
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-16 09:30:08 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
query := fmt.Sprintf(
|
2016-09-19 01:02:08 +02:00
|
|
|
"select * from {{.ForeignTable | $dot.SchemaTable}} where {{.ForeignColumn | $dot.Quotes}} in (%s)",
|
2016-09-14 10:08:30 +02:00
|
|
|
strmangle.Placeholders(dialect.IndexPlaceholders, count, 1, 1),
|
|
|
|
)
|
2016-08-16 09:30:08 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
if boil.DebugMode {
|
2017-09-02 17:27:27 +02:00
|
|
|
fmt.Fprintf(boil.DebugWriter, "%s\n%v\n", query, args)
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-08-17 06:08:55 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
results, err := e.Query(query, args...)
|
|
|
|
if err != nil {
|
2018-02-07 15:35:46 +01:00
|
|
|
return errors.Prefix("failed to eager load {{$txt.ForeignTable.NameGo}}", err)
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
defer results.Close()
|
2016-08-16 09:30:08 +02:00
|
|
|
|
2016-09-18 20:13:11 +02:00
|
|
|
var resultSlice []*{{$txt.ForeignTable.NameGo}}
|
2016-09-15 05:59:55 +02:00
|
|
|
if err = queries.Bind(results, &resultSlice); err != nil {
|
2018-02-07 15:35:46 +01:00
|
|
|
return errors.Prefix("failed to bind eager loaded slice {{$txt.ForeignTable.NameGo}}", err)
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
2016-08-16 09:30:08 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
{{if not $dot.NoHooks -}}
|
2016-09-21 06:05:26 +02:00
|
|
|
if len({{$varNameSingular}}AfterSelectHooks) != 0 {
|
2016-09-14 10:08:30 +02:00
|
|
|
for _, obj := range resultSlice {
|
|
|
|
if err := obj.doAfterSelectHooks(e); err != nil {
|
2018-02-07 15:35:46 +01:00
|
|
|
return errors.Err(err)
|
2016-09-14 10:08:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{{- end}}
|
2016-08-29 18:36:07 +02:00
|
|
|
|
2017-01-27 07:06:47 +01:00
|
|
|
if len(resultSlice) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if singular {
|
2016-09-18 20:13:11 +02:00
|
|
|
object.R.{{$txt.Function.Name}} = resultSlice[0]
|
2016-09-14 10:08:30 +02:00
|
|
|
return nil
|
|
|
|
}
|
2016-08-16 09:30:08 +02:00
|
|
|
|
2017-01-27 07:06:47 +01:00
|
|
|
for _, local := range slice {
|
|
|
|
for _, foreign := range resultSlice {
|
2016-09-19 01:02:08 +02:00
|
|
|
{{if $txt.Function.UsesBytes -}}
|
2016-09-18 20:13:11 +02:00
|
|
|
if 0 == bytes.Compare(local.{{$txt.Function.LocalAssignment}}, foreign.{{$txt.Function.ForeignAssignment}}) {
|
2016-09-16 09:22:12 +02:00
|
|
|
{{else -}}
|
2016-09-18 20:13:11 +02:00
|
|
|
if local.{{$txt.Function.LocalAssignment}} == foreign.{{$txt.Function.ForeignAssignment}} {
|
2016-09-16 09:22:12 +02:00
|
|
|
{{end -}}
|
2016-09-18 20:13:11 +02:00
|
|
|
local.R.{{$txt.Function.Name}} = foreign
|
2016-09-14 10:08:30 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-16 09:30:08 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
return nil
|
2016-08-16 09:30:08 +02:00
|
|
|
}
|
2016-09-18 20:13:11 +02:00
|
|
|
{{end -}}{{/* range */}}
|
|
|
|
{{end}}{{/* join table */}}
|