Support many_to_many eager loading properly
- Reorder the where statement in to_many to match eager loading.
This commit is contained in:
parent
3ae0b8a0d6
commit
92c4e7f8f3
3 changed files with 41 additions and 2 deletions
|
@ -157,4 +157,5 @@ var templateFunctions = template.FuncMap{
|
||||||
"sqlColDefinitions": bdb.SQLColDefinitions,
|
"sqlColDefinitions": bdb.SQLColDefinitions,
|
||||||
"columnNames": bdb.ColumnNames,
|
"columnNames": bdb.ColumnNames,
|
||||||
"columnDBTypes": bdb.ColumnDBTypes,
|
"columnDBTypes": bdb.ColumnDBTypes,
|
||||||
|
"getTable": bdb.GetTable,
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Na
|
||||||
|
|
||||||
{{if .ToJoinTable -}}
|
{{if .ToJoinTable -}}
|
||||||
queryMods = append(queryMods,
|
queryMods = append(queryMods,
|
||||||
qm.InnerJoin(`"{{.JoinTable}}" as "{{id 1}}" on "{{id 1}}"."{{.JoinForeignColumn}}" = "{{id 0}}"."{{.ForeignColumn}}"`),
|
qm.InnerJoin(`"{{.JoinTable}}" as "{{id 1}}" on "{{id 0}}"."{{.ForeignColumn}}" = "{{id 1}}"."{{.JoinForeignColumn}}"`),
|
||||||
qm.Where(`"{{id 1}}"."{{.JoinLocalColumn}}"=$1`, {{.Column | titleCase | printf "%s.%s" $rel.Function.Receiver }}),
|
qm.Where(`"{{id 1}}"."{{.JoinLocalColumn}}"=$1`, {{.Column | titleCase | printf "%s.%s" $rel.Function.Receiver }}),
|
||||||
)
|
)
|
||||||
{{else -}}
|
{{else -}}
|
||||||
|
|
|
@ -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 0}}"."{{.ForeignColumn}}" = "{{id 1}}"."{{.JoinForeignColumn}}" where "{{id 1}}"."{{.JoinLocalColumn}}" in (%s)`,
|
`select "{{id 0}}".*, "{{id 1}}"."{{.JoinLocalColumn}}" 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 -}}
|
||||||
|
@ -54,9 +54,32 @@ func (r *{{$rel.LocalTable.NameGo}}Relationships) Load{{$rel.Function.Name}}(e b
|
||||||
defer results.Close()
|
defer results.Close()
|
||||||
|
|
||||||
var resultSlice []*{{$rel.ForeignTable.NameGo}}
|
var resultSlice []*{{$rel.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}})
|
||||||
|
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 -}}
|
||||||
if err = boil.Bind(results, &resultSlice); err != nil {
|
if err = boil.Bind(results, &resultSlice); 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}}
|
||||||
|
|
||||||
if singular {
|
if singular {
|
||||||
if object.Relationships == nil {
|
if object.Relationships == nil {
|
||||||
|
@ -66,6 +89,20 @@ func (r *{{$rel.LocalTable.NameGo}}Relationships) Load{{$rel.Function.Name}}(e b
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{{if .ToJoinTable -}}
|
||||||
|
for i, foreign := range resultSlice {
|
||||||
|
localJoinCol := localJoinCols[i]
|
||||||
|
for _, local := range slice {
|
||||||
|
if local.{{$rel.Function.LocalAssignment}} == localJoinCol {
|
||||||
|
if local.Relationships == nil {
|
||||||
|
local.Relationships = &{{$rel.LocalTable.NameGo}}Relationships{}
|
||||||
|
}
|
||||||
|
local.Relationships.{{$rel.Function.Name}} = append(local.Relationships.{{$rel.Function.Name}}, foreign)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{{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.{{$rel.Function.LocalAssignment}} == foreign.{{$rel.Function.ForeignAssignment}} {
|
||||||
|
@ -77,6 +114,7 @@ func (r *{{$rel.LocalTable.NameGo}}Relationships) Load{{$rel.Function.Name}}(e b
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
{{end}}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue