mirror of
https://github.com/ent/ent.git
synced 2026-05-22 09:31:45 +03:00
dialect/sql/sqlgraph: add tests for nodes count
This commit is contained in:
@@ -443,13 +443,20 @@ type QuerySpec struct {
|
||||
Assign func(...interface{}) error
|
||||
}
|
||||
|
||||
// QueryNodes query the nodes in the graph and scan them to the given values.
|
||||
// QueryNodes query 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 := Dialect(drv.Dialect())
|
||||
qr := &query{graph: graph{builder: builder}, QuerySpec: spec}
|
||||
return qr.nodes(ctx, drv)
|
||||
}
|
||||
|
||||
// CountNodes counts the nodes in the given graph query.
|
||||
func CountNodes(ctx context.Context, drv dialect.Driver, spec *QuerySpec) (int, error) {
|
||||
builder := Dialect(drv.Dialect())
|
||||
qr := &query{graph: graph{builder: builder}, QuerySpec: spec}
|
||||
return qr.count(ctx, drv)
|
||||
}
|
||||
|
||||
type query struct {
|
||||
graph
|
||||
*QuerySpec
|
||||
@@ -476,7 +483,12 @@ func (q *query) nodes(ctx context.Context, drv dialect.Driver) error {
|
||||
|
||||
func (q *query) count(ctx context.Context, drv dialect.Driver) (int, error) {
|
||||
rows := &Rows{}
|
||||
query, args := q.selector().Count(q.Node.ID.Column).Query()
|
||||
selector := q.selector().Count(q.Node.ID.Column)
|
||||
if q.Unique {
|
||||
selector.distinct = false
|
||||
selector.Count(Distinct(q.Node.ID.Column))
|
||||
}
|
||||
query, args := selector.Query()
|
||||
if err := drv.Query(ctx, query, args, rows); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user