sqlboiler/templates/07_relationship_to_many_eager.tpl

140 lines
4.9 KiB
Smarty
Raw Normal View History

{{- /* Begin execution of template for many-to-one or many-to-many eager load */ -}}
{{- if .Table.IsJoinTable -}}
{{- else -}}
{{- $dot := . -}}
{{- range .Table.ToManyRelationships -}}
{{- if (and .ForeignColumnUnique (not .ToJoinTable)) -}}
{{- /* Begin execution of template for many-to-one eager load */ -}}
{{- $txt := textsFromOneToOneRelationship $dot.PkgName $dot.Tables $dot.Table . -}}
{{- template "relationship_to_one_eager_helper" (preserveDot $dot $txt) -}}
{{- else -}}
{{- /* Begin execution of template for many-to-many eager load */ -}}
{{- $varNameSingular := $dot.Table.Name | singular | camelCase -}}
{{- $txt := textsFromRelationship $dot.Tables $dot.Table . -}}
{{- $arg := printf "maybe%s" $txt.LocalTable.NameGo -}}
{{- $slice := printf "%sSlice" $txt.LocalTable.NameGo -}}
// 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.
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}}
count := 1
if singular {
object = {{$arg}}.(*{{$txt.LocalTable.NameGo}})
} else {
2016-08-24 07:07:51 +02:00
slice = *{{$arg}}.(*{{$slice}})
count = len(slice)
}
2016-08-16 09:30:08 +02:00
args := make([]interface{}, count)
if singular {
2016-08-16 09:30:08 +02:00
args[0] = object.{{.Column | titleCase}}
} else {
2016-08-16 09:30:08 +02:00
for i, obj := range slice {
args[i] = obj.{{.Column | titleCase}}
}
}
2016-08-16 09:30:08 +02:00
{{if .ToJoinTable -}}
query := fmt.Sprintf(
`select "{{id 0}}".*, "{{id 1}}"."{{.JoinLocalColumn}}" from {{schemaTable $dot.Dialect.LQ $dot.Dialect.RQ $dot.DriverName $dot.Schema .ForeignTable}} as "{{id 0}}" inner join {{schemaTable $dot.Dialect.LQ $dot.Dialect.RQ $dot.DriverName $dot.Schema .JoinTable}} as "{{id 1}}" on "{{id 0}}"."{{.ForeignColumn}}" = "{{id 1}}"."{{.JoinForeignColumn}}" where "{{id 1}}"."{{.JoinLocalColumn}}" in (%s)`,
strmangle.Placeholders(dialect.IndexPlaceholders, count, 1, 1),
2016-08-16 09:30:08 +02:00
)
{{else -}}
2016-08-16 07:24:56 +02:00
query := fmt.Sprintf(
`select * from {{schemaTable $dot.Dialect.LQ $dot.Dialect.RQ $dot.DriverName $dot.Schema .ForeignTable}} where "{{.ForeignColumn}}" in (%s)`,
strmangle.Placeholders(dialect.IndexPlaceholders, count, 1, 1),
2016-08-16 07:24:56 +02:00
)
2016-08-16 09:30:08 +02:00
{{end -}}
2016-08-16 07:24:56 +02:00
if boil.DebugMode {
fmt.Fprintf(boil.DebugWriter, "%s\n%v\n", query, args)
}
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()
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}}
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-09-02 09:09:42 +02:00
if err = boil.Bind(results, &resultSlice); err != nil {
return errors.Wrap(err, "failed to bind eager loaded slice {{.ForeignTable}}")
}
{{end}}
{{if not $dot.NoHooks -}}
if len({{.ForeignTable | singular | camelCase}}AfterSelectHooks) != 0 {
for _, obj := range resultSlice {
if err := obj.doAfterSelectHooks(e); err != nil {
return err
}
}
}
{{- end}}
if singular {
2016-08-27 06:40:11 +02:00
if object.R == nil {
2016-09-01 05:33:05 +02:00
object.R = &{{$varNameSingular}}R{}
}
object.R.{{$txt.Function.Name}} = resultSlice
return nil
}
{{if .ToJoinTable -}}
for i, foreign := range resultSlice {
localJoinCol := localJoinCols[i]
for _, local := range slice {
if local.{{$txt.Function.LocalAssignment}} == localJoinCol {
2016-08-27 06:40:11 +02:00
if local.R == nil {
2016-09-01 05:33:05 +02:00
local.R = &{{$varNameSingular}}R{}
}
local.R.{{$txt.Function.Name}} = append(local.R.{{$txt.Function.Name}}, foreign)
break
}
}
}
{{else -}}
for _, foreign := range resultSlice {
for _, local := range slice {
if local.{{$txt.Function.LocalAssignment}} == foreign.{{$txt.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{}
}
local.R.{{$txt.Function.Name}} = append(local.R.{{$txt.Function.Name}}, foreign)
break
}
}
}
{{end}}
return nil
}
{{end -}}{{/* if ForeignColumnUnique */}}
{{- end -}}{{/* range tomany */}}
{{- end -}}{{/* if IsJoinTable */}}