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 CardQuery struct {
// eager-loading edges.
withOwner *UserQuery
withFKs bool
// 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 (cq *CardQuery) Order(o ...Order) *CardQuery {
// QueryOwner chains the current query on the owner edge.
func (cq *CardQuery) QueryOwner() *UserQuery {
query := &UserQuery{config: cq.config}
step := sqlgraph.NewStep(
sqlgraph.From(card.Table, card.FieldID, cq.sqlQuery()),
sqlgraph.To(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, card.OwnerTable, card.OwnerColumn),
)
query.sql = sqlgraph.SetNeighbors(cq.driver.Dialect(), step)
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := cq.prepareQuery(ctx); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(card.Table, card.FieldID, cq.sqlQuery()),
sqlgraph.To(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, card.OwnerTable, card.OwnerColumn),
)
fromU = sqlgraph.SetNeighbors(cq.driver.Dialect(), step)
return fromU, nil
}
return query
}
@@ -167,6 +174,9 @@ func (cq *CardQuery) OnlyXID(ctx context.Context) int {
// All executes the query and returns a list of Cards.
func (cq *CardQuery) All(ctx context.Context) ([]*Card, error) {
if err := cq.prepareQuery(ctx); err != nil {
return nil, err
}
return cq.sqlAll(ctx)
}
@@ -199,6 +209,9 @@ func (cq *CardQuery) IDsX(ctx context.Context) []int {
// Count returns the count of the given query.
func (cq *CardQuery) Count(ctx context.Context) (int, error) {
if err := cq.prepareQuery(ctx); err != nil {
return 0, err
}
return cq.sqlCount(ctx)
}
@@ -213,6 +226,9 @@ func (cq *CardQuery) CountX(ctx context.Context) int {
// Exist returns true if the query has elements in the graph.
func (cq *CardQuery) Exist(ctx context.Context) (bool, error) {
if err := cq.prepareQuery(ctx); err != nil {
return false, err
}
return cq.sqlExist(ctx)
}
@@ -236,7 +252,8 @@ func (cq *CardQuery) Clone() *CardQuery {
unique: append([]string{}, cq.unique...),
predicates: append([]predicate.Card{}, cq.predicates...),
// clone intermediate query.
sql: cq.sql.Clone(),
sql: cq.sql.Clone(),
path: cq.path,
}
}
@@ -269,7 +286,12 @@ func (cq *CardQuery) WithOwner(opts ...func(*UserQuery)) *CardQuery {
func (cq *CardQuery) GroupBy(field string, fields ...string) *CardGroupBy {
group := &CardGroupBy{config: cq.config}
group.fields = append([]string{field}, fields...)
group.sql = cq.sqlQuery()
group.path = func(ctx context.Context) (prev *sql.Selector, err error) {
if err := cq.prepareQuery(ctx); err != nil {
return nil, err
}
return cq.sqlQuery(), nil
}
return group
}
@@ -288,10 +310,27 @@ func (cq *CardQuery) GroupBy(field string, fields ...string) *CardGroupBy {
func (cq *CardQuery) Select(field string, fields ...string) *CardSelect {
selector := &CardSelect{config: cq.config}
selector.fields = append([]string{field}, fields...)
selector.sql = cq.sqlQuery()
selector.path = func(ctx context.Context) (prev *sql.Selector, err error) {
if err := cq.prepareQuery(ctx); err != nil {
return nil, err
}
return cq.sqlQuery(), nil
}
return selector
}
func (cq *CardQuery) prepareQuery(ctx context.Context) error {
if cq.path != nil {
prev, err := cq.path(ctx)
if err != nil {
return err
}
cq.sql = prev
}
// Privacy and query checks go here.
return nil
}
func (cq *CardQuery) sqlAll(ctx context.Context) ([]*Card, error) {
var (
nodes = []*Card{}
@@ -438,8 +477,9 @@ type CardGroupBy 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.
@@ -450,6 +490,11 @@ func (cgb *CardGroupBy) Aggregate(fns ...Aggregate) *CardGroupBy {
// Scan applies the group-by query and scan the result into the given value.
func (cgb *CardGroupBy) Scan(ctx context.Context, v interface{}) error {
query, err := cgb.path(ctx)
if err != nil {
return err
}
cgb.sql = query
return cgb.sqlScan(ctx, v)
}
@@ -568,12 +613,18 @@ func (cgb *CardGroupBy) sqlQuery() *sql.Selector {
type CardSelect 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 (cs *CardSelect) Scan(ctx context.Context, v interface{}) error {
query, err := cs.path(ctx)
if err != nil {
return err
}
cs.sql = query
return cs.sqlScan(ctx, v)
}

View File

@@ -190,14 +190,16 @@ func (c *CardClient) GetX(ctx context.Context, id int) *Card {
// QueryOwner queries the owner edge of a Card.
func (c *CardClient) QueryOwner(ca *Card) *UserQuery {
query := &UserQuery{config: c.config}
id := ca.ID
step := sqlgraph.NewStep(
sqlgraph.From(card.Table, card.FieldID, id),
sqlgraph.To(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, card.OwnerTable, card.OwnerColumn),
)
query.sql = sqlgraph.Neighbors(ca.driver.Dialect(), step)
query.path = func(ctx context.Context) (fromV *sql.Selector, _ error) {
id := ca.ID
step := sqlgraph.NewStep(
sqlgraph.From(card.Table, card.FieldID, id),
sqlgraph.To(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, card.OwnerTable, card.OwnerColumn),
)
fromV = sqlgraph.Neighbors(ca.driver.Dialect(), step)
return fromV, nil
}
return query
}
@@ -288,42 +290,48 @@ func (c *UserClient) GetX(ctx context.Context, id int) *User {
// QueryCards queries the cards edge of a User.
func (c *UserClient) QueryCards(u *User) *CardQuery {
query := &CardQuery{config: c.config}
id := u.ID
step := sqlgraph.NewStep(
sqlgraph.From(user.Table, user.FieldID, id),
sqlgraph.To(card.Table, card.FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, user.CardsTable, user.CardsColumn),
)
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(card.Table, card.FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, user.CardsTable, user.CardsColumn),
)
fromV = sqlgraph.Neighbors(u.driver.Dialect(), step)
return fromV, nil
}
return query
}
// QueryFriends queries the friends edge of a User.
func (c *UserClient) QueryFriends(u *User) *UserQuery {
query := &UserQuery{config: c.config}
id := u.ID
step := sqlgraph.NewStep(
sqlgraph.From(user.Table, user.FieldID, id),
sqlgraph.To(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.M2M, false, user.FriendsTable, user.FriendsPrimaryKey...),
)
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(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.M2M, false, user.FriendsTable, user.FriendsPrimaryKey...),
)
fromV = sqlgraph.Neighbors(u.driver.Dialect(), step)
return fromV, nil
}
return query
}
// QueryBestFriend queries the best_friend edge of a User.
func (c *UserClient) QueryBestFriend(u *User) *UserQuery {
query := &UserQuery{config: c.config}
id := u.ID
step := sqlgraph.NewStep(
sqlgraph.From(user.Table, user.FieldID, id),
sqlgraph.To(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.O2O, false, user.BestFriendTable, user.BestFriendColumn),
)
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(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.O2O, false, user.BestFriendTable, user.BestFriendColumn),
)
fromV = sqlgraph.Neighbors(u.driver.Dialect(), step)
return fromV, nil
}
return query
}

View File

@@ -34,8 +34,9 @@ type UserQuery struct {
withFriends *UserQuery
withBestFriend *UserQuery
withFKs bool
// 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.
@@ -65,36 +66,54 @@ func (uq *UserQuery) Order(o ...Order) *UserQuery {
// QueryCards chains the current query on the cards edge.
func (uq *UserQuery) QueryCards() *CardQuery {
query := &CardQuery{config: uq.config}
step := sqlgraph.NewStep(
sqlgraph.From(user.Table, user.FieldID, uq.sqlQuery()),
sqlgraph.To(card.Table, card.FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, user.CardsTable, user.CardsColumn),
)
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(card.Table, card.FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, user.CardsTable, user.CardsColumn),
)
fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
return fromU, nil
}
return query
}
// QueryFriends chains the current query on the friends edge.
func (uq *UserQuery) QueryFriends() *UserQuery {
query := &UserQuery{config: uq.config}
step := sqlgraph.NewStep(
sqlgraph.From(user.Table, user.FieldID, uq.sqlQuery()),
sqlgraph.To(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.M2M, false, user.FriendsTable, user.FriendsPrimaryKey...),
)
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(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.M2M, false, user.FriendsTable, user.FriendsPrimaryKey...),
)
fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
return fromU, nil
}
return query
}
// QueryBestFriend chains the current query on the best_friend edge.
func (uq *UserQuery) QueryBestFriend() *UserQuery {
query := &UserQuery{config: uq.config}
step := sqlgraph.NewStep(
sqlgraph.From(user.Table, user.FieldID, uq.sqlQuery()),
sqlgraph.To(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.O2O, false, user.BestFriendTable, user.BestFriendColumn),
)
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(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.O2O, false, user.BestFriendTable, user.BestFriendColumn),
)
fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
return fromU, nil
}
return query
}
@@ -194,6 +213,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)
}
@@ -226,6 +248,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)
}
@@ -240,6 +265,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)
}
@@ -263,7 +291,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,
}
}
@@ -318,7 +347,12 @@ func (uq *UserQuery) WithBestFriend(opts ...func(*UserQuery)) *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
}
@@ -337,10 +371,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{}
@@ -580,8 +631,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.
@@ -592,6 +644,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)
}
@@ -710,12 +767,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)
}