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

@@ -190,14 +190,16 @@ func (c *GroupClient) GetX(ctx context.Context, id int) *Group {
// QueryUsers queries the users edge of a Group.
func (c *GroupClient) QueryUsers(gr *Group) *UserQuery {
query := &UserQuery{config: c.config}
id := gr.ID
step := sqlgraph.NewStep(
sqlgraph.From(group.Table, group.FieldID, id),
sqlgraph.To(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.M2M, false, group.UsersTable, group.UsersPrimaryKey...),
)
query.sql = sqlgraph.Neighbors(gr.driver.Dialect(), step)
query.path = func(ctx context.Context) (fromV *sql.Selector, _ error) {
id := gr.ID
step := sqlgraph.NewStep(
sqlgraph.From(group.Table, group.FieldID, id),
sqlgraph.To(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.M2M, false, group.UsersTable, group.UsersPrimaryKey...),
)
fromV = sqlgraph.Neighbors(gr.driver.Dialect(), step)
return fromV, nil
}
return query
}
@@ -287,14 +289,16 @@ func (c *UserClient) GetX(ctx context.Context, id int) *User {
// QueryGroups queries the groups edge of a User.
func (c *UserClient) QueryGroups(u *User) *GroupQuery {
query := &GroupQuery{config: c.config}
id := u.ID
step := sqlgraph.NewStep(
sqlgraph.From(user.Table, user.FieldID, id),
sqlgraph.To(group.Table, group.FieldID),
sqlgraph.Edge(sqlgraph.M2M, true, user.GroupsTable, user.GroupsPrimaryKey...),
)
query.sql = sqlgraph.Neighbors(u.driver.Dialect(), step)
query.path = func(ctx context.Context) (fromV *sql.Selector, _ error) {
id := u.ID
step := sqlgraph.NewStep(
sqlgraph.From(user.Table, user.FieldID, id),
sqlgraph.To(group.Table, group.FieldID),
sqlgraph.Edge(sqlgraph.M2M, true, user.GroupsTable, user.GroupsPrimaryKey...),
)
fromV = sqlgraph.Neighbors(u.driver.Dialect(), step)
return fromV, nil
}
return query
}

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)
}

View File

@@ -31,8 +31,9 @@ type UserQuery struct {
predicates []predicate.User
// eager-loading edges.
withGroups *GroupQuery
// 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 (uq *UserQuery) Order(o ...Order) *UserQuery {
// QueryGroups chains the current query on the groups edge.
func (uq *UserQuery) QueryGroups() *GroupQuery {
query := &GroupQuery{config: uq.config}
step := sqlgraph.NewStep(
sqlgraph.From(user.Table, user.FieldID, uq.sqlQuery()),
sqlgraph.To(group.Table, group.FieldID),
sqlgraph.Edge(sqlgraph.M2M, true, user.GroupsTable, user.GroupsPrimaryKey...),
)
query.sql = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := uq.prepareQuery(ctx); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(user.Table, user.FieldID, uq.sqlQuery()),
sqlgraph.To(group.Table, group.FieldID),
sqlgraph.Edge(sqlgraph.M2M, true, user.GroupsTable, user.GroupsPrimaryKey...),
)
fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
return fromU, nil
}
return query
}
@@ -167,6 +174,9 @@ func (uq *UserQuery) OnlyXID(ctx context.Context) int {
// All executes the query and returns a list of Users.
func (uq *UserQuery) All(ctx context.Context) ([]*User, error) {
if err := uq.prepareQuery(ctx); err != nil {
return nil, err
}
return uq.sqlAll(ctx)
}
@@ -199,6 +209,9 @@ func (uq *UserQuery) IDsX(ctx context.Context) []int {
// Count returns the count of the given query.
func (uq *UserQuery) Count(ctx context.Context) (int, error) {
if err := uq.prepareQuery(ctx); err != nil {
return 0, err
}
return uq.sqlCount(ctx)
}
@@ -213,6 +226,9 @@ func (uq *UserQuery) CountX(ctx context.Context) int {
// Exist returns true if the query has elements in the graph.
func (uq *UserQuery) Exist(ctx context.Context) (bool, error) {
if err := uq.prepareQuery(ctx); err != nil {
return false, err
}
return uq.sqlExist(ctx)
}
@@ -236,7 +252,8 @@ func (uq *UserQuery) Clone() *UserQuery {
unique: append([]string{}, uq.unique...),
predicates: append([]predicate.User{}, uq.predicates...),
// clone intermediate query.
sql: uq.sql.Clone(),
sql: uq.sql.Clone(),
path: uq.path,
}
}
@@ -269,7 +286,12 @@ func (uq *UserQuery) WithGroups(opts ...func(*GroupQuery)) *UserQuery {
func (uq *UserQuery) GroupBy(field string, fields ...string) *UserGroupBy {
group := &UserGroupBy{config: uq.config}
group.fields = append([]string{field}, fields...)
group.sql = uq.sqlQuery()
group.path = func(ctx context.Context) (prev *sql.Selector, err error) {
if err := uq.prepareQuery(ctx); err != nil {
return nil, err
}
return uq.sqlQuery(), nil
}
return group
}
@@ -288,10 +310,27 @@ func (uq *UserQuery) GroupBy(field string, fields ...string) *UserGroupBy {
func (uq *UserQuery) Select(field string, fields ...string) *UserSelect {
selector := &UserSelect{config: uq.config}
selector.fields = append([]string{field}, fields...)
selector.sql = uq.sqlQuery()
selector.path = func(ctx context.Context) (prev *sql.Selector, err error) {
if err := uq.prepareQuery(ctx); err != nil {
return nil, err
}
return uq.sqlQuery(), nil
}
return selector
}
func (uq *UserQuery) prepareQuery(ctx context.Context) error {
if uq.path != nil {
prev, err := uq.path(ctx)
if err != nil {
return err
}
uq.sql = prev
}
// Privacy and query checks go here.
return nil
}
func (uq *UserQuery) sqlAll(ctx context.Context) ([]*User, error) {
var (
nodes = []*User{}
@@ -466,8 +505,9 @@ type UserGroupBy 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 (ugb *UserGroupBy) Aggregate(fns ...Aggregate) *UserGroupBy {
// Scan applies the group-by query and scan the result into the given value.
func (ugb *UserGroupBy) Scan(ctx context.Context, v interface{}) error {
query, err := ugb.path(ctx)
if err != nil {
return err
}
ugb.sql = query
return ugb.sqlScan(ctx, v)
}
@@ -596,12 +641,18 @@ func (ugb *UserGroupBy) sqlQuery() *sql.Selector {
type UserSelect 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 (us *UserSelect) Scan(ctx context.Context, v interface{}) error {
query, err := us.path(ctx)
if err != nil {
return err
}
us.sql = query
return us.sqlScan(ctx, v)
}