Add hooks to eager load funcs
* Add PreserveDots text helper * Fix bug using wrong XTitleCases name
This commit is contained in:
parent
ccdfc93fee
commit
629ec8c8d0
4 changed files with 61 additions and 24 deletions
|
@ -143,6 +143,7 @@ var templateFunctions = template.FuncMap{
|
|||
"textsFromForeignKey": textsFromForeignKey,
|
||||
"textsFromOneToOneRelationship": textsFromOneToOneRelationship,
|
||||
"textsFromRelationship": textsFromRelationship,
|
||||
"preserveDot": preserveDot,
|
||||
|
||||
// dbdrivers ops
|
||||
"filterColumnsByDefault": bdb.FilterColumnsByDefault,
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
{{- define "relationship_to_one_eager_helper" -}}
|
||||
{{- $noHooks := .Dot.NoHooks -}}
|
||||
{{- with .Rel -}}
|
||||
{{- $arg := printf "maybe%s" .LocalTable.NameGo -}}
|
||||
{{- $slice := printf "%sSlice" .LocalTable.NameGo -}}
|
||||
// Load{{.Function.Name}} allows an eager lookup of values, cached into the
|
||||
|
@ -40,10 +42,20 @@ func (r *{{.LocalTable.NameGo}}R) Load{{.Function.Name}}(e boil.Executor, singul
|
|||
defer results.Close()
|
||||
|
||||
var resultSlice []*{{.ForeignTable.NameGo}}
|
||||
if err = boil.BindFast(results, &resultSlice, {{.ForeignKey.Table | singular | camelCase}}TitleCases); err != nil {
|
||||
return errors.Wrap(err, "failed to bind eager loaded slice {{.ForeignTable}}")
|
||||
if err = boil.BindFast(results, &resultSlice, {{.ForeignTable.Name | singular | camelCase}}TitleCases); err != nil {
|
||||
return errors.Wrap(err, "failed to bind eager loaded slice {{.ForeignTable.NameGo}}")
|
||||
}
|
||||
|
||||
{{if not $noHooks -}}
|
||||
if len({{.ForeignTable.Name | singular | camelCase}}AfterSelectHooks) != 0 {
|
||||
for _, obj := range resultSlice {
|
||||
if err := obj.doAfterSelectHooks(e); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
{{- end}}
|
||||
|
||||
if singular && len(resultSlice) != 0 {
|
||||
if object.R == nil {
|
||||
object.R = &{{.LocalTable.NameGo}}R{}
|
||||
|
@ -66,12 +78,13 @@ func (r *{{.LocalTable.NameGo}}R) Load{{.Function.Name}}(e boil.Executor, singul
|
|||
|
||||
return nil
|
||||
}
|
||||
{{- end -}}
|
||||
{{end -}}
|
||||
{{- if .Table.IsJoinTable -}}
|
||||
{{- else -}}
|
||||
{{- $dot := . -}}
|
||||
{{- range .Table.FKeys -}}
|
||||
{{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
|
||||
{{- template "relationship_to_one_eager_helper" $rel -}}
|
||||
{{- end -}}
|
||||
{{- template "relationship_to_one_eager_helper" (preserveDot $dot $rel) -}}
|
||||
{{- end -}}
|
||||
{{end}}
|
||||
|
|
|
@ -3,20 +3,21 @@
|
|||
{{- $dot := . -}}
|
||||
{{- range .Table.ToManyRelationships -}}
|
||||
{{- if (and .ForeignColumnUnique (not .ToJoinTable)) -}}
|
||||
{{- template "relationship_to_one_eager_helper" (textsFromOneToOneRelationship $dot.PkgName $dot.Tables $dot.Table .) -}}
|
||||
{{- $txt := textsFromOneToOneRelationship $dot.PkgName $dot.Tables $dot.Table . -}}
|
||||
{{- template "relationship_to_one_eager_helper" (preserveDot $dot $txt) -}}
|
||||
{{- else -}}
|
||||
{{- $rel := textsFromRelationship $dot.Tables $dot.Table . -}}
|
||||
{{- $arg := printf "maybe%s" $rel.LocalTable.NameGo -}}
|
||||
{{- $slice := printf "%sSlice" $rel.LocalTable.NameGo -}}
|
||||
// Load{{$rel.Function.Name}} allows an eager lookup of values, cached into the
|
||||
{{- $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
|
||||
// loaded structs of the objects.
|
||||
func (r *{{$rel.LocalTable.NameGo}}R) Load{{$rel.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error {
|
||||
var slice []*{{$rel.LocalTable.NameGo}}
|
||||
var object *{{$rel.LocalTable.NameGo}}
|
||||
func (r *{{$txt.LocalTable.NameGo}}R) 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}}.(*{{$rel.LocalTable.NameGo}})
|
||||
object = {{$arg}}.(*{{$txt.LocalTable.NameGo}})
|
||||
} else {
|
||||
slice = *{{$arg}}.(*{{$slice}})
|
||||
count = len(slice)
|
||||
|
@ -53,14 +54,14 @@ func (r *{{$rel.LocalTable.NameGo}}R) Load{{$rel.Function.Name}}(e boil.Executor
|
|||
}
|
||||
defer results.Close()
|
||||
|
||||
var resultSlice []*{{$rel.ForeignTable.NameGo}}
|
||||
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({{$rel.ForeignTable.NameGo}})
|
||||
one := new({{$txt.ForeignTable.NameGo}})
|
||||
var localJoinCol {{$localCol.Type}}
|
||||
|
||||
err = results.Scan({{$foreignTable.Columns | columnNames | stringMap $dot.StringFuncs.titleCase | prefixStringSlice "&one." | join ", "}}, &localJoinCol)
|
||||
|
@ -76,16 +77,26 @@ func (r *{{$rel.LocalTable.NameGo}}R) Load{{$rel.Function.Name}}(e boil.Executor
|
|||
return errors.Wrap(err, "failed to plebian-bind eager loaded slice {{.ForeignTable}}")
|
||||
}
|
||||
{{else -}}
|
||||
if err = boil.BindFast(results, &resultSlice, {{$dot.Table.Name | singular | camelCase}}TitleCases); err != nil {
|
||||
if err = boil.BindFast(results, &resultSlice, {{.ForeignTable | singular | camelCase}}TitleCases); 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 {
|
||||
if object.R == nil {
|
||||
object.R = &{{$rel.LocalTable.NameGo}}R{}
|
||||
object.R = &{{$txt.LocalTable.NameGo}}R{}
|
||||
}
|
||||
object.R.{{$rel.Function.Name}} = resultSlice
|
||||
object.R.{{$txt.Function.Name}} = resultSlice
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -93,11 +104,11 @@ func (r *{{$rel.LocalTable.NameGo}}R) Load{{$rel.Function.Name}}(e boil.Executor
|
|||
for i, foreign := range resultSlice {
|
||||
localJoinCol := localJoinCols[i]
|
||||
for _, local := range slice {
|
||||
if local.{{$rel.Function.LocalAssignment}} == localJoinCol {
|
||||
if local.{{$txt.Function.LocalAssignment}} == localJoinCol {
|
||||
if local.R == nil {
|
||||
local.R = &{{$rel.LocalTable.NameGo}}R{}
|
||||
local.R = &{{$txt.LocalTable.NameGo}}R{}
|
||||
}
|
||||
local.R.{{$rel.Function.Name}} = append(local.R.{{$rel.Function.Name}}, foreign)
|
||||
local.R.{{$txt.Function.Name}} = append(local.R.{{$txt.Function.Name}}, foreign)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -105,11 +116,11 @@ func (r *{{$rel.LocalTable.NameGo}}R) Load{{$rel.Function.Name}}(e boil.Executor
|
|||
{{else -}}
|
||||
for _, foreign := range resultSlice {
|
||||
for _, local := range slice {
|
||||
if local.{{$rel.Function.LocalAssignment}} == foreign.{{$rel.Function.ForeignAssignment}} {
|
||||
if local.{{$txt.Function.LocalAssignment}} == foreign.{{$txt.Function.ForeignAssignment}} {
|
||||
if local.R == nil {
|
||||
local.R = &{{$rel.LocalTable.NameGo}}R{}
|
||||
local.R = &{{$txt.LocalTable.NameGo}}R{}
|
||||
}
|
||||
local.R.{{$rel.Function.Name}} = append(local.R.{{$rel.Function.Name}}, foreign)
|
||||
local.R.{{$txt.Function.Name}} = append(local.R.{{$txt.Function.Name}}, foreign)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
|
|
@ -183,3 +183,15 @@ func mkFunctionName(fkeyTableSingular, foreignTablePluralGo, fkeyColumn string,
|
|||
|
||||
return strmangle.TitleCase(colName) + foreignTablePluralGo
|
||||
}
|
||||
|
||||
type PreserveDot struct {
|
||||
Dot templateData
|
||||
Rel RelationshipToOneTexts
|
||||
}
|
||||
|
||||
func preserveDot(data templateData, obj RelationshipToOneTexts) PreserveDot {
|
||||
return PreserveDot{
|
||||
Dot: data,
|
||||
Rel: obj,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue