Looking like to_many eager load is done

- Fix to_many join table query
- Fix testing output
- Add debug logging
This commit is contained in:
Aaron L 2016-08-16 21:08:55 -07:00
parent 756281f9fb
commit fb198cc92c
3 changed files with 21 additions and 11 deletions

View file

@ -33,7 +33,7 @@ func (r *{{$rel.LocalTable.NameGo}}Relationships) Load{{$rel.Function.Name}}(e b
{{if .ToJoinTable -}} {{if .ToJoinTable -}}
query := fmt.Sprintf( query := fmt.Sprintf(
`select "{{id 0}}".* from "{{.ForeignTable}}" as {{id 0}} inner join "{{.JoinTable}}" as "{{id 1}}" on "{{id 1}}"."{{.JoinForeignColumn}}" = "{{id 0}}"."{{.ForeignColumn}}" where "{{.ForeignColumn}}" in (%s)`, `select "{{id 0}}".* from "{{.ForeignTable}}" as "{{id 0}}" inner join "{{.JoinTable}}" as "{{id 1}}" on "{{id 0}}"."{{.ForeignColumn}}" = "{{id 1}}"."{{.JoinForeignColumn}}" where "{{id 1}}"."{{.JoinLocalColumn}}" in (%s)`,
strmangle.Placeholders(count, 1, 1), strmangle.Placeholders(count, 1, 1),
) )
{{else -}} {{else -}}
@ -43,6 +43,10 @@ func (r *{{$rel.LocalTable.NameGo}}Relationships) Load{{$rel.Function.Name}}(e b
) )
{{end -}} {{end -}}
if boil.DebugMode {
fmt.Fprintf(boil.DebugWriter, "%s\n%v\n", query, args)
}
results, err := e.Query(query, args...) results, err := e.Query(query, args...)
if err != nil { if err != nil {
return errors.Wrap(err, "failed to eager load {{.ForeignTable}}") return errors.Wrap(err, "failed to eager load {{.ForeignTable}}")
@ -55,9 +59,10 @@ func (r *{{$rel.LocalTable.NameGo}}Relationships) Load{{$rel.Function.Name}}(e b
} }
if singular { if singular {
object.Relationships = &{{$rel.LocalTable.NameGo}}Relationships{ if object.Relationships == nil {
{{$rel.Function.Name}}: resultSlice, object.Relationships = &{{$rel.LocalTable.NameGo}}Relationships{}
} }
object.Relationships.{{$rel.Function.Name}} = resultSlice
return nil return nil
} }

View file

@ -29,6 +29,10 @@ func (r *{{.LocalTable.NameGo}}Relationships) Load{{.Function.Name}}(e boil.Exec
strmangle.Placeholders(count, 1, 1), strmangle.Placeholders(count, 1, 1),
) )
if boil.DebugMode {
fmt.Fprintf(boil.DebugWriter, "%s\n%v\n", query, args)
}
results, err := e.Query(query, args...) results, err := e.Query(query, args...)
if err != nil { if err != nil {
return errors.Wrap(err, "failed to eager load {{.ForeignTable.NameGo}}") return errors.Wrap(err, "failed to eager load {{.ForeignTable.NameGo}}")
@ -41,9 +45,10 @@ func (r *{{.LocalTable.NameGo}}Relationships) Load{{.Function.Name}}(e boil.Exec
} }
if singular && len(resultSlice) != 0 { if singular && len(resultSlice) != 0 {
object.Relationships = &{{.LocalTable.NameGo}}Relationships{ if object.Relationships == nil {
{{.Function.Name}}: resultSlice[0], object.Relationships = &{{.LocalTable.NameGo}}Relationships{}
} }
object.Relationships.{{.Function.Name}} = resultSlice[0]
return nil return nil
} }

View file

@ -75,18 +75,18 @@ func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
} }
if err = a.Relationships.Load{{$rel.Function.Name}}(tx, false, {{$rel.LocalTable.NameGo}}Slice{&a}); err != nil { if err = a.Relationships.Load{{$rel.Function.Name}}(tx, false, {{$rel.LocalTable.NameGo}}Slice{&a}); err != nil {
t.Error(err) t.Fatal(err)
} }
if len(a.Relationships.{{$rel.Function.Name}}) != 2 { if got := len(a.Relationships.{{$rel.Function.Name}}); got != 2 {
t.Error("expected to be able to eager load 2 relationships") t.Error("number of eager loaded records wrong, got:", got)
} }
a.Relationships.{{$rel.Function.Name}} = nil a.Relationships.{{$rel.Function.Name}} = nil
if err = a.Relationships.Load{{$rel.Function.Name}}(tx, true, &a); err != nil { if err = a.Relationships.Load{{$rel.Function.Name}}(tx, true, &a); err != nil {
t.Error(err) t.Fatal(err)
} }
if len(a.Relationships.{{$rel.Function.Name}}) != 2 { if got := len(a.Relationships.{{$rel.Function.Name}}); got != 2 {
t.Error("expected to be able to eager load 2 relationships") t.Error("number of eager loaded records wrong, got:", got)
} }
if t.Failed() { if t.Failed() {