Skip to content

Commit

Permalink
Deduplicate IDs in eager loads.
Browse files Browse the repository at this point in the history
Take a schema like youtube where there are users and videos, in the
past eager loading when doing a query like Videos(Load("User")) would
create something that would look like this:

SELECT * FROM "users" WHERE "id" IN ($1,$2,$3,$4,$5,$6,$7);
[1 1 1 1 1 2 2]

Now we de-duplicate the IDs of the eager loaded relationships, so even
though there are 7 videos, they only refer to 2 users so we only look up
those two.

SELECT * FROM "videos";
[]
SELECT * FROM "users" WHERE "id" IN ($1,$2);
[1 2]

- Fix #267
  • Loading branch information
aarondl committed Jun 5, 2018
1 parent 062a79d commit b5d2ec2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
22 changes: 16 additions & 6 deletions templates/07_relationship_to_one_eager.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,36 @@ func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}({{if $.NoContext}}e boil
var slice []*{{$txt.LocalTable.NameGo}}
var object *{{$txt.LocalTable.NameGo}}

count := 1
if singular {
object = {{$arg}}.(*{{$txt.LocalTable.NameGo}})
} else {
slice = *{{$arg}}.(*[]*{{$txt.LocalTable.NameGo}})
count = len(slice)
}

args := make([]interface{}, count)
args := make([]interface{}, 0, 1)
if singular {
if object.R == nil {
object.R = &{{$varNameSingular}}R{}
}
args[0] = object.{{$txt.LocalTable.ColumnNameGo}}
args = append(args, object.{{$txt.LocalTable.ColumnNameGo}})
} else {
for i, obj := range slice {
Outer:
for _, obj := range slice {
if obj.R == nil {
obj.R = &{{$varNameSingular}}R{}
}
args[i] = obj.{{$txt.LocalTable.ColumnNameGo}}

for _, a := range args {
{{if $txt.Function.UsesBytes -}}
if 0 == bytes.Compare(a.([]byte), obj.{{$txt.Function.LocalAssignment}}) {
{{else -}}
if a == obj.{{$txt.Function.LocalAssignment}} {
{{end -}}
continue Outer
}
}

args = append(args, obj.{{$txt.LocalTable.ColumnNameGo}})
}
}

Expand Down
22 changes: 16 additions & 6 deletions templates/08_relationship_one_to_one_eager.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,36 @@ func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}({{if $.NoContext}}e boil
var slice []*{{$txt.LocalTable.NameGo}}
var object *{{$txt.LocalTable.NameGo}}

count := 1
if singular {
object = {{$arg}}.(*{{$txt.LocalTable.NameGo}})
} else {
slice = *{{$arg}}.(*[]*{{$txt.LocalTable.NameGo}})
count = len(slice)
}

args := make([]interface{}, count)
args := make([]interface{}, 0, 1)
if singular {
if object.R == nil {
object.R = &{{$varNameSingular}}R{}
}
args[0] = object.{{$txt.LocalTable.ColumnNameGo}}
args = append(args, object.{{$txt.LocalTable.ColumnNameGo}})
} else {
for i, obj := range slice {
Outer:
for _, obj := range slice {
if obj.R == nil {
obj.R = &{{$varNameSingular}}R{}
}
args[i] = obj.{{$txt.LocalTable.ColumnNameGo}}

for _, a := range args {
{{if $txt.Function.UsesBytes -}}
if 0 == bytes.Compare(a.([]byte), obj.{{$txt.Function.LocalAssignment}}) {
{{else -}}
if a == obj.{{$txt.Function.LocalAssignment}} {
{{end -}}
continue Outer
}
}

args = append(args, obj.{{$txt.LocalTable.ColumnNameGo}})
}
}

Expand Down
22 changes: 16 additions & 6 deletions templates/09_relationship_to_many_eager.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,36 @@ func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}({{if $.NoContext}}e boil
var slice []*{{$txt.LocalTable.NameGo}}
var object *{{$txt.LocalTable.NameGo}}

count := 1
if singular {
object = {{$arg}}.(*{{$txt.LocalTable.NameGo}})
} else {
slice = *{{$arg}}.(*[]*{{$txt.LocalTable.NameGo}})
count = len(slice)
}

args := make([]interface{}, count)
args := make([]interface{}, 0, 1)
if singular {
if object.R == nil {
object.R = &{{$varNameSingular}}R{}
}
args[0] = object.{{.Column | titleCase}}
args = append(args, object.{{.Column | titleCase}})
} else {
for i, obj := range slice {
Outer:
for _, obj := range slice {
if obj.R == nil {
obj.R = &{{$varNameSingular}}R{}
}
args[i] = obj.{{.Column | titleCase}}

for _, a := range args {
{{if $txt.Function.UsesBytes -}}
if 0 == bytes.Compare(a.([]byte), obj.{{$txt.Function.LocalAssignment}}) {
{{else -}}
if a == obj.{{$txt.Function.LocalAssignment}} {
{{end -}}
continue Outer
}
}

args = append(args, obj.{{.Column | titleCase}})
}
}

Expand Down

0 comments on commit b5d2ec2

Please sign in to comment.