dialect/sql/sqlgraph: add query edges function (#274)

This commit is contained in:
Ariel Mashraki
2020-01-07 19:50:33 +02:00
committed by GitHub
parent 9cb0eb7467
commit b93958ebf4
2 changed files with 80 additions and 1 deletions

View File

@@ -452,7 +452,7 @@ type QuerySpec struct {
Assign func(...interface{}) error
}
// QueryNodes query the nodes in the graph query and scans them to the given values.
// QueryNodes queries the nodes in the graph query and scans them to the given values.
func QueryNodes(ctx context.Context, drv dialect.Driver, spec *QuerySpec) error {
builder := sql.Dialect(drv.Dialect())
qr := &query{graph: graph{builder: builder}, QuerySpec: spec}
@@ -466,6 +466,48 @@ func CountNodes(ctx context.Context, drv dialect.Driver, spec *QuerySpec) (int,
return qr.count(ctx, drv)
}
// EdgeQuerySpec holds the information for querying
// edges in the graph.
type EdgeQuerySpec struct {
Edge *EdgeSpec
Predicate func(*sql.Selector)
ScanValues func() [2]interface{}
Assign func(out, in interface{}) error
}
// QueryEdges queries the edges in the graph and scans the result with the given dest function.
func QueryEdges(ctx context.Context, drv dialect.Driver, spec *EdgeQuerySpec) error {
if len(spec.Edge.Columns) != 2 {
return fmt.Errorf("sqlgraph: edge query requires 2 columns (out, in)")
}
out, in := spec.Edge.Columns[0], spec.Edge.Columns[1]
if spec.Edge.Inverse {
out, in = in, out
}
selector := sql.Dialect(drv.Dialect()).
Select(out, in).
From(sql.Table(spec.Edge.Table))
if p := spec.Predicate; p != nil {
p(selector)
}
rows := &sql.Rows{}
query, args := selector.Query()
if err := drv.Query(ctx, query, args, rows); err != nil {
return err
}
defer rows.Close()
for rows.Next() {
values := spec.ScanValues()
if err := rows.Scan(values[0], values[1]); err != nil {
return err
}
if err := spec.Assign(values[0], values[1]); err != nil {
return err
}
}
return nil
}
type query struct {
graph
*QuerySpec