entc/gen: lazy compute storage-driver queries (#408)

This commit is contained in:
Ariel Mashraki
2020-03-29 14:36:44 +03:00
committed by GitHub
parent a2ea5bfbee
commit de5006a4d8
91 changed files with 5681 additions and 1858 deletions

View File

@@ -31,8 +31,9 @@ type GroupQuery struct {
predicates []predicate.Group
// eager-loading edges.
withUsers *UserQuery
// intermediate query.
sql *sql.Selector
// intermediate query (i.e. traversal path).
sql *sql.Selector
path func(context.Context) (*sql.Selector, error)
}
// Where adds a new predicate for the builder.
@@ -62,12 +63,18 @@ func (gq *GroupQuery) Order(o ...Order) *GroupQuery {
// QueryUsers chains the current query on the users edge.
func (gq *GroupQuery) QueryUsers() *UserQuery {
query := &UserQuery{config: gq.config}
step := sqlgraph.NewStep(
sqlgraph.From(group.Table, group.FieldID, gq.sqlQuery()),
sqlgraph.To(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.M2M, false, group.UsersTable, group.UsersPrimaryKey...),
)
query.sql = sqlgraph.SetNeighbors(gq.driver.Dialect(), step)
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := gq.prepareQuery(ctx); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(group.Table, group.FieldID, gq.sqlQuery()),
sqlgraph.To(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.M2M, false, group.UsersTable, group.UsersPrimaryKey...),
)
fromU = sqlgraph.SetNeighbors(gq.driver.Dialect(), step)
return fromU, nil
}
return query
}
@@ -167,6 +174,9 @@ func (gq *GroupQuery) OnlyXID(ctx context.Context) int {
// All executes the query and returns a list of Groups.
func (gq *GroupQuery) All(ctx context.Context) ([]*Group, error) {
if err := gq.prepareQuery(ctx); err != nil {
return nil, err
}
return gq.sqlAll(ctx)
}
@@ -199,6 +209,9 @@ func (gq *GroupQuery) IDsX(ctx context.Context) []int {
// Count returns the count of the given query.
func (gq *GroupQuery) Count(ctx context.Context) (int, error) {
if err := gq.prepareQuery(ctx); err != nil {
return 0, err
}
return gq.sqlCount(ctx)
}
@@ -213,6 +226,9 @@ func (gq *GroupQuery) CountX(ctx context.Context) int {
// Exist returns true if the query has elements in the graph.
func (gq *GroupQuery) Exist(ctx context.Context) (bool, error) {
if err := gq.prepareQuery(ctx); err != nil {
return false, err
}
return gq.sqlExist(ctx)
}
@@ -236,7 +252,8 @@ func (gq *GroupQuery) Clone() *GroupQuery {
unique: append([]string{}, gq.unique...),
predicates: append([]predicate.Group{}, gq.predicates...),
// clone intermediate query.
sql: gq.sql.Clone(),
sql: gq.sql.Clone(),
path: gq.path,
}
}
@@ -269,7 +286,12 @@ func (gq *GroupQuery) WithUsers(opts ...func(*UserQuery)) *GroupQuery {
func (gq *GroupQuery) GroupBy(field string, fields ...string) *GroupGroupBy {
group := &GroupGroupBy{config: gq.config}
group.fields = append([]string{field}, fields...)
group.sql = gq.sqlQuery()
group.path = func(ctx context.Context) (prev *sql.Selector, err error) {
if err := gq.prepareQuery(ctx); err != nil {
return nil, err
}
return gq.sqlQuery(), nil
}
return group
}
@@ -288,10 +310,27 @@ func (gq *GroupQuery) GroupBy(field string, fields ...string) *GroupGroupBy {
func (gq *GroupQuery) Select(field string, fields ...string) *GroupSelect {
selector := &GroupSelect{config: gq.config}
selector.fields = append([]string{field}, fields...)
selector.sql = gq.sqlQuery()
selector.path = func(ctx context.Context) (prev *sql.Selector, err error) {
if err := gq.prepareQuery(ctx); err != nil {
return nil, err
}
return gq.sqlQuery(), nil
}
return selector
}
func (gq *GroupQuery) prepareQuery(ctx context.Context) error {
if gq.path != nil {
prev, err := gq.path(ctx)
if err != nil {
return err
}
gq.sql = prev
}
// Privacy and query checks go here.
return nil
}
func (gq *GroupQuery) sqlAll(ctx context.Context) ([]*Group, error) {
var (
nodes = []*Group{}
@@ -466,8 +505,9 @@ type GroupGroupBy struct {
config
fields []string
fns []Aggregate
// intermediate query.
sql *sql.Selector
// intermediate query (i.e. traversal path).
sql *sql.Selector
path func(context.Context) (*sql.Selector, error)
}
// Aggregate adds the given aggregation functions to the group-by query.
@@ -478,6 +518,11 @@ func (ggb *GroupGroupBy) Aggregate(fns ...Aggregate) *GroupGroupBy {
// Scan applies the group-by query and scan the result into the given value.
func (ggb *GroupGroupBy) Scan(ctx context.Context, v interface{}) error {
query, err := ggb.path(ctx)
if err != nil {
return err
}
ggb.sql = query
return ggb.sqlScan(ctx, v)
}
@@ -596,12 +641,18 @@ func (ggb *GroupGroupBy) sqlQuery() *sql.Selector {
type GroupSelect struct {
config
fields []string
// intermediate queries.
sql *sql.Selector
// intermediate query (i.e. traversal path).
sql *sql.Selector
path func(context.Context) (*sql.Selector, error)
}
// Scan applies the selector query and scan the result into the given value.
func (gs *GroupSelect) Scan(ctx context.Context, v interface{}) error {
query, err := gs.path(ctx)
if err != nil {
return err
}
gs.sql = query
return gs.sqlScan(ctx, v)
}