Skip to content

Commit

Permalink
Merge pull request #1337 from nicowolf91/improve-loads
Browse files Browse the repository at this point in the history
Improve loads by using maps for deduplication
  • Loading branch information
stephenafamo authored Jan 2, 2024
2 parents 682d8c9 + c7f1c20 commit 74a8ec7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 49 deletions.
30 changes: 13 additions & 17 deletions templates/main/07_relationship_to_one_eager.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,29 @@ func ({{$ltable.DownSingular}}L) Load{{$rel.Foreign}}({{if $.NoContext}}e boil.E
}
}

args := make([]interface{}, 0, 1)
args := make(map[interface{}]struct{})
if singular {
if object.R == nil {
object.R = &{{$ltable.DownSingular}}R{}
}
{{if $usesPrimitives -}}
args = append(args, object.{{$col}})
args[object.{{$col}}] = struct{}{}
{{else -}}
if !queries.IsNil(object.{{$col}}) {
args = append(args, object.{{$col}})
args[object.{{$col}}] = struct{}{}
}
{{end}}
} else {
Outer:
for _, obj := range slice {
if obj.R == nil {
obj.R = &{{$ltable.DownSingular}}R{}
}

for _, a := range args {
{{if $usesPrimitives -}}
if a == obj.{{$col}} {
{{else -}}
if queries.Equal(a, obj.{{$col}}) {
{{end -}}
continue Outer
}
}

{{if $usesPrimitives -}}
args = append(args, obj.{{$col}})
args[obj.{{$col}}] = struct{}{}
{{else -}}
if !queries.IsNil(obj.{{$col}}) {
args = append(args, obj.{{$col}})
args[obj.{{$col}}] = struct{}{}
}
{{end}}
}
Expand All @@ -80,9 +69,16 @@ func ({{$ltable.DownSingular}}L) Load{{$rel.Foreign}}({{if $.NoContext}}e boil.E
return nil
}

argsSlice := make([]interface{}, len(args))
i := 0
for arg := range args {
argsSlice[i] = arg
i++
}

query := NewQuery(
qm.From(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}`),
qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, args...),
qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, argsSlice...),
{{if and $.AddSoftDeletes $canSoftDelete -}}
qmhelper.WhereIsNull(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{or $.AutoColumns.Deleted "deleted_at"}}`),
{{- end}}
Expand Down
26 changes: 11 additions & 15 deletions templates/main/08_relationship_one_to_one_eager.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,36 @@ func ({{$ltable.DownSingular}}L) Load{{$relAlias.Local}}({{if $.NoContext}}e boi
}
}

args := make([]interface{}, 0, 1)
args := make(map[interface{}]struct{})
if singular {
if object.R == nil {
object.R = &{{$ltable.DownSingular}}R{}
}
args = append(args, object.{{$col}})
args[object.{{$col}}] = struct{}{}
} else {
Outer:
for _, obj := range slice {
if obj.R == nil {
obj.R = &{{$ltable.DownSingular}}R{}
}

for _, a := range args {
{{if $usesPrimitives -}}
if a == obj.{{$col}} {
{{else -}}
if queries.Equal(a, obj.{{$col}}) {
{{end -}}
continue Outer
}
}

args = append(args, obj.{{$col}})
args[obj.{{$col}}] = struct{}{}
}
}

if len(args) == 0 {
return nil
}

argsSlice := make([]interface{}, len(args))
i := 0
for arg := range args {
argsSlice[i] = arg
i++
}

query := NewQuery(
qm.From(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}`),
qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, args...),
qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, argsSlice...),
{{if and $.AddSoftDeletes $canSoftDelete -}}
qmhelper.WhereIsNull(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{or $.AutoColumns.Deleted "deleted_at"}}`),
{{- end}}
Expand Down
29 changes: 12 additions & 17 deletions templates/main/09_relationship_to_many_eager.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,53 +38,48 @@ func ({{$ltable.DownSingular}}L) Load{{$relAlias.Local}}({{if $.NoContext}}e boi
}
}

args := make([]interface{}, 0, 1)
args := make(map[interface{}]struct{})
if singular {
if object.R == nil {
object.R = &{{$ltable.DownSingular}}R{}
}
args = append(args, object.{{$col}})
args[object.{{$col}}] = struct{}{}
} else {
Outer:
for _, obj := range slice {
if obj.R == nil {
obj.R = &{{$ltable.DownSingular}}R{}
}

for _, a := range args {
{{if $usesPrimitives -}}
if a == obj.{{$col}} {
{{else -}}
if queries.Equal(a, obj.{{$col}}) {
{{end -}}
continue Outer
}
}

args = append(args, obj.{{$col}})
args[obj.{{$col}}] = struct{}{}
}
}

if len(args) == 0 {
return nil
}

argsSlice := make([]interface{}, len(args))
i := 0
for arg := range args {
argsSlice[i] = arg
i++
}

{{if .ToJoinTable -}}
{{- $schemaJoinTable := .JoinTable | $.SchemaTable -}}
{{- $foreignTable := getTable $.Tables .ForeignTable -}}
query := NewQuery(
qm.Select("{{$foreignTable.Columns | columnNames | $.QuoteMap | prefixStringSlice (print $schemaForeignTable ".") | join ", "}}, {{id 0 | $.Quotes}}.{{.JoinLocalColumn | $.Quotes}}"),
qm.From("{{$schemaForeignTable}}"),
qm.InnerJoin("{{$schemaJoinTable}} as {{id 0 | $.Quotes}} on {{$schemaForeignTable}}.{{.ForeignColumn | $.Quotes}} = {{id 0 | $.Quotes}}.{{.JoinForeignColumn | $.Quotes}}"),
qm.WhereIn("{{id 0 | $.Quotes}}.{{.JoinLocalColumn | $.Quotes}} in ?", args...),
qm.WhereIn("{{id 0 | $.Quotes}}.{{.JoinLocalColumn | $.Quotes}} in ?", argsSlice...),
{{if and $.AddSoftDeletes $canSoftDelete -}}
qmhelper.WhereIsNull("{{$schemaForeignTable}}.{{or $.AutoColumns.Deleted "deleted_at" | $.Quotes}}"),
{{- end}}
)
{{else -}}
query := NewQuery(
qm.From(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}`),
qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, args...),
qm.WhereIn(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{.ForeignColumn}} in ?`, argsSlice...),
{{if and $.AddSoftDeletes $canSoftDelete -}}
qmhelper.WhereIsNull(`{{if $.Dialect.UseSchema}}{{$.Schema}}.{{end}}{{.ForeignTable}}.{{or $.AutoColumns.Deleted "deleted_at"}}`),
{{- end}}
Expand Down

0 comments on commit 74a8ec7

Please sign in to comment.