mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/gen: use join for loading m2m relationship (#2417)
* entc/gen: use join for m2m relationship * entc/gen: add test for eager-load inverse-m2m
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
"entgo.io/ent/examples/fs/ent/file"
|
||||
)
|
||||
|
||||
@@ -464,3 +465,6 @@ func (s *selector) BoolX(ctx context.Context) bool {
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// queryHook describes an internal hook for the different sqlAll methods.
|
||||
type queryHook func(context.Context, *sqlgraph.QuerySpec)
|
||||
|
||||
@@ -389,7 +389,7 @@ func (fq *FileQuery) prepareQuery(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fq *FileQuery) sqlAll(ctx context.Context) ([]*File, error) {
|
||||
func (fq *FileQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*File, error) {
|
||||
var (
|
||||
nodes = []*File{}
|
||||
_spec = fq.querySpec()
|
||||
@@ -399,18 +399,17 @@ func (fq *FileQuery) sqlAll(ctx context.Context) ([]*File, error) {
|
||||
}
|
||||
)
|
||||
_spec.ScanValues = func(columns []string) ([]interface{}, error) {
|
||||
node := &File{config: fq.config}
|
||||
nodes = append(nodes, node)
|
||||
return node.scanValues(columns)
|
||||
return (*File).scanValues(nil, columns)
|
||||
}
|
||||
_spec.Assign = func(columns []string, values []interface{}) error {
|
||||
if len(nodes) == 0 {
|
||||
return fmt.Errorf("ent: Assign called without calling ScanValues")
|
||||
}
|
||||
node := nodes[len(nodes)-1]
|
||||
node := &File{config: fq.config}
|
||||
nodes = append(nodes, node)
|
||||
node.Edges.loadedTypes = loadedTypes
|
||||
return node.assignValues(columns, values)
|
||||
}
|
||||
for i := range hooks {
|
||||
hooks[i](ctx, _spec)
|
||||
}
|
||||
if err := sqlgraph.QueryNodes(ctx, fq.driver, _spec); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user