Add hooks to eager load funcs

* Add PreserveDots text helper
* Fix bug using wrong XTitleCases name
This commit is contained in:
Patrick O'brien 2016-08-30 02:36:07 +10:00
parent ccdfc93fee
commit 629ec8c8d0
4 changed files with 61 additions and 24 deletions

View file

@ -143,6 +143,7 @@ var templateFunctions = template.FuncMap{
"textsFromForeignKey": textsFromForeignKey, "textsFromForeignKey": textsFromForeignKey,
"textsFromOneToOneRelationship": textsFromOneToOneRelationship, "textsFromOneToOneRelationship": textsFromOneToOneRelationship,
"textsFromRelationship": textsFromRelationship, "textsFromRelationship": textsFromRelationship,
"preserveDot": preserveDot,
// dbdrivers ops // dbdrivers ops
"filterColumnsByDefault": bdb.FilterColumnsByDefault, "filterColumnsByDefault": bdb.FilterColumnsByDefault,

View file

@ -1,4 +1,6 @@
{{- define "relationship_to_one_eager_helper" -}} {{- define "relationship_to_one_eager_helper" -}}
{{- $noHooks := .Dot.NoHooks -}}
{{- with .Rel -}}
{{- $arg := printf "maybe%s" .LocalTable.NameGo -}} {{- $arg := printf "maybe%s" .LocalTable.NameGo -}}
{{- $slice := printf "%sSlice" .LocalTable.NameGo -}} {{- $slice := printf "%sSlice" .LocalTable.NameGo -}}
// Load{{.Function.Name}} allows an eager lookup of values, cached into the // 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() defer results.Close()
var resultSlice []*{{.ForeignTable.NameGo}} var resultSlice []*{{.ForeignTable.NameGo}}
if err = boil.BindFast(results, &resultSlice, {{.ForeignKey.Table | singular | camelCase}}TitleCases); err != nil { if err = boil.BindFast(results, &resultSlice, {{.ForeignTable.Name | singular | camelCase}}TitleCases); err != nil {
return errors.Wrap(err, "failed to bind eager loaded slice {{.ForeignTable}}") 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 singular && len(resultSlice) != 0 {
if object.R == nil { if object.R == nil {
object.R = &{{.LocalTable.NameGo}}R{} object.R = &{{.LocalTable.NameGo}}R{}
@ -66,12 +78,13 @@ func (r *{{.LocalTable.NameGo}}R) Load{{.Function.Name}}(e boil.Executor, singul
return nil return nil
} }
{{- end -}}
{{end -}} {{end -}}
{{- if .Table.IsJoinTable -}} {{- if .Table.IsJoinTable -}}
{{- else -}} {{- else -}}
{{- $dot := . -}} {{- $dot := . -}}
{{- range .Table.FKeys -}} {{- range .Table.FKeys -}}
{{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}} {{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
{{- template "relationship_to_one_eager_helper" $rel -}} {{- template "relationship_to_one_eager_helper" (preserveDot $dot $rel) -}}
{{- end -}}
{{- end -}} {{- end -}}
{{end}}

View file

@ -3,20 +3,21 @@
{{- $dot := . -}} {{- $dot := . -}}
{{- range .Table.ToManyRelationships -}} {{- range .Table.ToManyRelationships -}}
{{- if (and .ForeignColumnUnique (not .ToJoinTable)) -}} {{- 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 -}} {{- else -}}
{{- $rel := textsFromRelationship $dot.Tables $dot.Table . -}} {{- $txt := textsFromRelationship $dot.Tables $dot.Table . -}}
{{- $arg := printf "maybe%s" $rel.LocalTable.NameGo -}} {{- $arg := printf "maybe%s" $txt.LocalTable.NameGo -}}
{{- $slice := printf "%sSlice" $rel.LocalTable.NameGo -}} {{- $slice := printf "%sSlice" $txt.LocalTable.NameGo -}}
// Load{{$rel.Function.Name}} allows an eager lookup of values, cached into the // Load{{$txt.Function.Name}} allows an eager lookup of values, cached into the
// loaded structs of the objects. // loaded structs of the objects.
func (r *{{$rel.LocalTable.NameGo}}R) Load{{$rel.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error { func (r *{{$txt.LocalTable.NameGo}}R) Load{{$txt.Function.Name}}(e boil.Executor, singular bool, {{$arg}} interface{}) error {
var slice []*{{$rel.LocalTable.NameGo}} var slice []*{{$txt.LocalTable.NameGo}}
var object *{{$rel.LocalTable.NameGo}} var object *{{$txt.LocalTable.NameGo}}
count := 1 count := 1
if singular { if singular {
object = {{$arg}}.(*{{$rel.LocalTable.NameGo}}) object = {{$arg}}.(*{{$txt.LocalTable.NameGo}})
} else { } else {
slice = *{{$arg}}.(*{{$slice}}) slice = *{{$arg}}.(*{{$slice}})
count = len(slice) count = len(slice)
@ -53,14 +54,14 @@ func (r *{{$rel.LocalTable.NameGo}}R) Load{{$rel.Function.Name}}(e boil.Executor
} }
defer results.Close() defer results.Close()
var resultSlice []*{{$rel.ForeignTable.NameGo}} var resultSlice []*{{$txt.ForeignTable.NameGo}}
{{if .ToJoinTable -}} {{if .ToJoinTable -}}
{{- $foreignTable := getTable $dot.Tables .ForeignTable -}} {{- $foreignTable := getTable $dot.Tables .ForeignTable -}}
{{- $joinTable := getTable $dot.Tables .JoinTable -}} {{- $joinTable := getTable $dot.Tables .JoinTable -}}
{{- $localCol := $joinTable.GetColumn .JoinLocalColumn}} {{- $localCol := $joinTable.GetColumn .JoinLocalColumn}}
var localJoinCols []{{$localCol.Type}} var localJoinCols []{{$localCol.Type}}
for results.Next() { for results.Next() {
one := new({{$rel.ForeignTable.NameGo}}) one := new({{$txt.ForeignTable.NameGo}})
var localJoinCol {{$localCol.Type}} var localJoinCol {{$localCol.Type}}
err = results.Scan({{$foreignTable.Columns | columnNames | stringMap $dot.StringFuncs.titleCase | prefixStringSlice "&one." | join ", "}}, &localJoinCol) 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}}") return errors.Wrap(err, "failed to plebian-bind eager loaded slice {{.ForeignTable}}")
} }
{{else -}} {{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}}") return errors.Wrap(err, "failed to bind eager loaded slice {{.ForeignTable}}")
} }
{{end}} {{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 singular {
if object.R == nil { 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 return nil
} }
@ -93,11 +104,11 @@ func (r *{{$rel.LocalTable.NameGo}}R) Load{{$rel.Function.Name}}(e boil.Executor
for i, foreign := range resultSlice { for i, foreign := range resultSlice {
localJoinCol := localJoinCols[i] localJoinCol := localJoinCols[i]
for _, local := range slice { for _, local := range slice {
if local.{{$rel.Function.LocalAssignment}} == localJoinCol { if local.{{$txt.Function.LocalAssignment}} == localJoinCol {
if local.R == nil { 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 break
} }
} }
@ -105,11 +116,11 @@ func (r *{{$rel.LocalTable.NameGo}}R) Load{{$rel.Function.Name}}(e boil.Executor
{{else -}} {{else -}}
for _, foreign := range resultSlice { for _, foreign := range resultSlice {
for _, local := range slice { 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 { 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 break
} }
} }

View file

@ -183,3 +183,15 @@ func mkFunctionName(fkeyTableSingular, foreignTablePluralGo, fkeyColumn string,
return strmangle.TitleCase(colName) + foreignTablePluralGo 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,
}
}