mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/gen: lazy compute storage-driver queries (#408)
This commit is contained in:
@@ -190,14 +190,16 @@ func (c *PetClient) GetX(ctx context.Context, id int) *Pet {
|
||||
// QueryOwner queries the owner edge of a Pet.
|
||||
func (c *PetClient) QueryOwner(pe *Pet) *UserQuery {
|
||||
query := &UserQuery{config: c.config}
|
||||
id := pe.ID
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(pet.Table, pet.FieldID, id),
|
||||
sqlgraph.To(user.Table, user.FieldID),
|
||||
sqlgraph.Edge(sqlgraph.M2O, true, pet.OwnerTable, pet.OwnerColumn),
|
||||
)
|
||||
query.sql = sqlgraph.Neighbors(pe.driver.Dialect(), step)
|
||||
|
||||
query.path = func(ctx context.Context) (fromV *sql.Selector, _ error) {
|
||||
id := pe.ID
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(pet.Table, pet.FieldID, id),
|
||||
sqlgraph.To(user.Table, user.FieldID),
|
||||
sqlgraph.Edge(sqlgraph.M2O, true, pet.OwnerTable, pet.OwnerColumn),
|
||||
)
|
||||
fromV = sqlgraph.Neighbors(pe.driver.Dialect(), step)
|
||||
return fromV, nil
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
@@ -287,14 +289,16 @@ func (c *UserClient) GetX(ctx context.Context, id int) *User {
|
||||
// QueryPets queries the pets edge of a User.
|
||||
func (c *UserClient) QueryPets(u *User) *PetQuery {
|
||||
query := &PetQuery{config: c.config}
|
||||
id := u.ID
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(user.Table, user.FieldID, id),
|
||||
sqlgraph.To(pet.Table, pet.FieldID),
|
||||
sqlgraph.Edge(sqlgraph.O2M, false, user.PetsTable, user.PetsColumn),
|
||||
)
|
||||
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(pet.Table, pet.FieldID),
|
||||
sqlgraph.Edge(sqlgraph.O2M, false, user.PetsTable, user.PetsColumn),
|
||||
)
|
||||
fromV = sqlgraph.Neighbors(u.driver.Dialect(), step)
|
||||
return fromV, nil
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
|
||||
@@ -31,8 +31,9 @@ type PetQuery 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 (pq *PetQuery) Order(o ...Order) *PetQuery {
|
||||
// QueryOwner chains the current query on the owner edge.
|
||||
func (pq *PetQuery) QueryOwner() *UserQuery {
|
||||
query := &UserQuery{config: pq.config}
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(pet.Table, pet.FieldID, pq.sqlQuery()),
|
||||
sqlgraph.To(user.Table, user.FieldID),
|
||||
sqlgraph.Edge(sqlgraph.M2O, true, pet.OwnerTable, pet.OwnerColumn),
|
||||
)
|
||||
query.sql = sqlgraph.SetNeighbors(pq.driver.Dialect(), step)
|
||||
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
||||
if err := pq.prepareQuery(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(pet.Table, pet.FieldID, pq.sqlQuery()),
|
||||
sqlgraph.To(user.Table, user.FieldID),
|
||||
sqlgraph.Edge(sqlgraph.M2O, true, pet.OwnerTable, pet.OwnerColumn),
|
||||
)
|
||||
fromU = sqlgraph.SetNeighbors(pq.driver.Dialect(), step)
|
||||
return fromU, nil
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
@@ -167,6 +174,9 @@ func (pq *PetQuery) OnlyXID(ctx context.Context) int {
|
||||
|
||||
// All executes the query and returns a list of Pets.
|
||||
func (pq *PetQuery) All(ctx context.Context) ([]*Pet, error) {
|
||||
if err := pq.prepareQuery(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pq.sqlAll(ctx)
|
||||
}
|
||||
|
||||
@@ -199,6 +209,9 @@ func (pq *PetQuery) IDsX(ctx context.Context) []int {
|
||||
|
||||
// Count returns the count of the given query.
|
||||
func (pq *PetQuery) Count(ctx context.Context) (int, error) {
|
||||
if err := pq.prepareQuery(ctx); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return pq.sqlCount(ctx)
|
||||
}
|
||||
|
||||
@@ -213,6 +226,9 @@ func (pq *PetQuery) CountX(ctx context.Context) int {
|
||||
|
||||
// Exist returns true if the query has elements in the graph.
|
||||
func (pq *PetQuery) Exist(ctx context.Context) (bool, error) {
|
||||
if err := pq.prepareQuery(ctx); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return pq.sqlExist(ctx)
|
||||
}
|
||||
|
||||
@@ -236,7 +252,8 @@ func (pq *PetQuery) Clone() *PetQuery {
|
||||
unique: append([]string{}, pq.unique...),
|
||||
predicates: append([]predicate.Pet{}, pq.predicates...),
|
||||
// clone intermediate query.
|
||||
sql: pq.sql.Clone(),
|
||||
sql: pq.sql.Clone(),
|
||||
path: pq.path,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -269,7 +286,12 @@ func (pq *PetQuery) WithOwner(opts ...func(*UserQuery)) *PetQuery {
|
||||
func (pq *PetQuery) GroupBy(field string, fields ...string) *PetGroupBy {
|
||||
group := &PetGroupBy{config: pq.config}
|
||||
group.fields = append([]string{field}, fields...)
|
||||
group.sql = pq.sqlQuery()
|
||||
group.path = func(ctx context.Context) (prev *sql.Selector, err error) {
|
||||
if err := pq.prepareQuery(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pq.sqlQuery(), nil
|
||||
}
|
||||
return group
|
||||
}
|
||||
|
||||
@@ -288,10 +310,27 @@ func (pq *PetQuery) GroupBy(field string, fields ...string) *PetGroupBy {
|
||||
func (pq *PetQuery) Select(field string, fields ...string) *PetSelect {
|
||||
selector := &PetSelect{config: pq.config}
|
||||
selector.fields = append([]string{field}, fields...)
|
||||
selector.sql = pq.sqlQuery()
|
||||
selector.path = func(ctx context.Context) (prev *sql.Selector, err error) {
|
||||
if err := pq.prepareQuery(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pq.sqlQuery(), nil
|
||||
}
|
||||
return selector
|
||||
}
|
||||
|
||||
func (pq *PetQuery) prepareQuery(ctx context.Context) error {
|
||||
if pq.path != nil {
|
||||
prev, err := pq.path(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pq.sql = prev
|
||||
}
|
||||
// Privacy and query checks go here.
|
||||
return nil
|
||||
}
|
||||
|
||||
func (pq *PetQuery) sqlAll(ctx context.Context) ([]*Pet, error) {
|
||||
var (
|
||||
nodes = []*Pet{}
|
||||
@@ -438,8 +477,9 @@ type PetGroupBy 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 (pgb *PetGroupBy) Aggregate(fns ...Aggregate) *PetGroupBy {
|
||||
|
||||
// Scan applies the group-by query and scan the result into the given value.
|
||||
func (pgb *PetGroupBy) Scan(ctx context.Context, v interface{}) error {
|
||||
query, err := pgb.path(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pgb.sql = query
|
||||
return pgb.sqlScan(ctx, v)
|
||||
}
|
||||
|
||||
@@ -568,12 +613,18 @@ func (pgb *PetGroupBy) sqlQuery() *sql.Selector {
|
||||
type PetSelect 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 (ps *PetSelect) Scan(ctx context.Context, v interface{}) error {
|
||||
query, err := ps.path(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ps.sql = query
|
||||
return ps.sqlScan(ctx, v)
|
||||
}
|
||||
|
||||
|
||||
@@ -31,8 +31,9 @@ type UserQuery struct {
|
||||
predicates []predicate.User
|
||||
// eager-loading edges.
|
||||
withPets *PetQuery
|
||||
// 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 {
|
||||
// QueryPets chains the current query on the pets edge.
|
||||
func (uq *UserQuery) QueryPets() *PetQuery {
|
||||
query := &PetQuery{config: uq.config}
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(user.Table, user.FieldID, uq.sqlQuery()),
|
||||
sqlgraph.To(pet.Table, pet.FieldID),
|
||||
sqlgraph.Edge(sqlgraph.O2M, false, user.PetsTable, user.PetsColumn),
|
||||
)
|
||||
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(pet.Table, pet.FieldID),
|
||||
sqlgraph.Edge(sqlgraph.O2M, false, user.PetsTable, user.PetsColumn),
|
||||
)
|
||||
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) WithPets(opts ...func(*PetQuery)) *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{}
|
||||
@@ -431,8 +470,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.
|
||||
@@ -443,6 +483,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)
|
||||
}
|
||||
|
||||
@@ -561,12 +606,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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user