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:
@@ -31,8 +31,9 @@ type CityQuery struct {
|
||||
predicates []predicate.City
|
||||
// eager-loading edges.
|
||||
withStreets *StreetQuery
|
||||
// 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 *CityQuery) Order(o ...Order) *CityQuery {
|
||||
// QueryStreets chains the current query on the streets edge.
|
||||
func (cq *CityQuery) QueryStreets() *StreetQuery {
|
||||
query := &StreetQuery{config: cq.config}
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(city.Table, city.FieldID, cq.sqlQuery()),
|
||||
sqlgraph.To(street.Table, street.FieldID),
|
||||
sqlgraph.Edge(sqlgraph.O2M, false, city.StreetsTable, city.StreetsColumn),
|
||||
)
|
||||
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(city.Table, city.FieldID, cq.sqlQuery()),
|
||||
sqlgraph.To(street.Table, street.FieldID),
|
||||
sqlgraph.Edge(sqlgraph.O2M, false, city.StreetsTable, city.StreetsColumn),
|
||||
)
|
||||
fromU = sqlgraph.SetNeighbors(cq.driver.Dialect(), step)
|
||||
return fromU, nil
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
@@ -167,6 +174,9 @@ func (cq *CityQuery) OnlyXID(ctx context.Context) int {
|
||||
|
||||
// All executes the query and returns a list of Cities.
|
||||
func (cq *CityQuery) All(ctx context.Context) ([]*City, error) {
|
||||
if err := cq.prepareQuery(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cq.sqlAll(ctx)
|
||||
}
|
||||
|
||||
@@ -199,6 +209,9 @@ func (cq *CityQuery) IDsX(ctx context.Context) []int {
|
||||
|
||||
// Count returns the count of the given query.
|
||||
func (cq *CityQuery) 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 *CityQuery) CountX(ctx context.Context) int {
|
||||
|
||||
// Exist returns true if the query has elements in the graph.
|
||||
func (cq *CityQuery) 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 *CityQuery) Clone() *CityQuery {
|
||||
unique: append([]string{}, cq.unique...),
|
||||
predicates: append([]predicate.City{}, cq.predicates...),
|
||||
// clone intermediate query.
|
||||
sql: cq.sql.Clone(),
|
||||
sql: cq.sql.Clone(),
|
||||
path: cq.path,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -269,7 +286,12 @@ func (cq *CityQuery) WithStreets(opts ...func(*StreetQuery)) *CityQuery {
|
||||
func (cq *CityQuery) GroupBy(field string, fields ...string) *CityGroupBy {
|
||||
group := &CityGroupBy{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 *CityQuery) GroupBy(field string, fields ...string) *CityGroupBy {
|
||||
func (cq *CityQuery) Select(field string, fields ...string) *CitySelect {
|
||||
selector := &CitySelect{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 *CityQuery) 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 *CityQuery) sqlAll(ctx context.Context) ([]*City, error) {
|
||||
var (
|
||||
nodes = []*City{}
|
||||
@@ -431,8 +470,9 @@ type CityGroupBy 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 (cgb *CityGroupBy) Aggregate(fns ...Aggregate) *CityGroupBy {
|
||||
|
||||
// Scan applies the group-by query and scan the result into the given value.
|
||||
func (cgb *CityGroupBy) 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)
|
||||
}
|
||||
|
||||
@@ -561,12 +606,18 @@ func (cgb *CityGroupBy) sqlQuery() *sql.Selector {
|
||||
type CitySelect 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 *CitySelect) 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)
|
||||
}
|
||||
|
||||
|
||||
@@ -190,14 +190,16 @@ func (c *CityClient) GetX(ctx context.Context, id int) *City {
|
||||
// QueryStreets queries the streets edge of a City.
|
||||
func (c *CityClient) QueryStreets(ci *City) *StreetQuery {
|
||||
query := &StreetQuery{config: c.config}
|
||||
id := ci.ID
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(city.Table, city.FieldID, id),
|
||||
sqlgraph.To(street.Table, street.FieldID),
|
||||
sqlgraph.Edge(sqlgraph.O2M, false, city.StreetsTable, city.StreetsColumn),
|
||||
)
|
||||
query.sql = sqlgraph.Neighbors(ci.driver.Dialect(), step)
|
||||
|
||||
query.path = func(ctx context.Context) (fromV *sql.Selector, _ error) {
|
||||
id := ci.ID
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(city.Table, city.FieldID, id),
|
||||
sqlgraph.To(street.Table, street.FieldID),
|
||||
sqlgraph.Edge(sqlgraph.O2M, false, city.StreetsTable, city.StreetsColumn),
|
||||
)
|
||||
fromV = sqlgraph.Neighbors(ci.driver.Dialect(), step)
|
||||
return fromV, nil
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
@@ -287,14 +289,16 @@ func (c *StreetClient) GetX(ctx context.Context, id int) *Street {
|
||||
// QueryCity queries the city edge of a Street.
|
||||
func (c *StreetClient) QueryCity(s *Street) *CityQuery {
|
||||
query := &CityQuery{config: c.config}
|
||||
id := s.ID
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(street.Table, street.FieldID, id),
|
||||
sqlgraph.To(city.Table, city.FieldID),
|
||||
sqlgraph.Edge(sqlgraph.M2O, true, street.CityTable, street.CityColumn),
|
||||
)
|
||||
query.sql = sqlgraph.Neighbors(s.driver.Dialect(), step)
|
||||
|
||||
query.path = func(ctx context.Context) (fromV *sql.Selector, _ error) {
|
||||
id := s.ID
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(street.Table, street.FieldID, id),
|
||||
sqlgraph.To(city.Table, city.FieldID),
|
||||
sqlgraph.Edge(sqlgraph.M2O, true, street.CityTable, street.CityColumn),
|
||||
)
|
||||
fromV = sqlgraph.Neighbors(s.driver.Dialect(), step)
|
||||
return fromV, nil
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
|
||||
@@ -31,8 +31,9 @@ type StreetQuery struct {
|
||||
// eager-loading edges.
|
||||
withCity *CityQuery
|
||||
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 (sq *StreetQuery) Order(o ...Order) *StreetQuery {
|
||||
// QueryCity chains the current query on the city edge.
|
||||
func (sq *StreetQuery) QueryCity() *CityQuery {
|
||||
query := &CityQuery{config: sq.config}
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(street.Table, street.FieldID, sq.sqlQuery()),
|
||||
sqlgraph.To(city.Table, city.FieldID),
|
||||
sqlgraph.Edge(sqlgraph.M2O, true, street.CityTable, street.CityColumn),
|
||||
)
|
||||
query.sql = sqlgraph.SetNeighbors(sq.driver.Dialect(), step)
|
||||
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
||||
if err := sq.prepareQuery(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
step := sqlgraph.NewStep(
|
||||
sqlgraph.From(street.Table, street.FieldID, sq.sqlQuery()),
|
||||
sqlgraph.To(city.Table, city.FieldID),
|
||||
sqlgraph.Edge(sqlgraph.M2O, true, street.CityTable, street.CityColumn),
|
||||
)
|
||||
fromU = sqlgraph.SetNeighbors(sq.driver.Dialect(), step)
|
||||
return fromU, nil
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
@@ -167,6 +174,9 @@ func (sq *StreetQuery) OnlyXID(ctx context.Context) int {
|
||||
|
||||
// All executes the query and returns a list of Streets.
|
||||
func (sq *StreetQuery) All(ctx context.Context) ([]*Street, error) {
|
||||
if err := sq.prepareQuery(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return sq.sqlAll(ctx)
|
||||
}
|
||||
|
||||
@@ -199,6 +209,9 @@ func (sq *StreetQuery) IDsX(ctx context.Context) []int {
|
||||
|
||||
// Count returns the count of the given query.
|
||||
func (sq *StreetQuery) Count(ctx context.Context) (int, error) {
|
||||
if err := sq.prepareQuery(ctx); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return sq.sqlCount(ctx)
|
||||
}
|
||||
|
||||
@@ -213,6 +226,9 @@ func (sq *StreetQuery) CountX(ctx context.Context) int {
|
||||
|
||||
// Exist returns true if the query has elements in the graph.
|
||||
func (sq *StreetQuery) Exist(ctx context.Context) (bool, error) {
|
||||
if err := sq.prepareQuery(ctx); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return sq.sqlExist(ctx)
|
||||
}
|
||||
|
||||
@@ -236,7 +252,8 @@ func (sq *StreetQuery) Clone() *StreetQuery {
|
||||
unique: append([]string{}, sq.unique...),
|
||||
predicates: append([]predicate.Street{}, sq.predicates...),
|
||||
// clone intermediate query.
|
||||
sql: sq.sql.Clone(),
|
||||
sql: sq.sql.Clone(),
|
||||
path: sq.path,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -269,7 +286,12 @@ func (sq *StreetQuery) WithCity(opts ...func(*CityQuery)) *StreetQuery {
|
||||
func (sq *StreetQuery) GroupBy(field string, fields ...string) *StreetGroupBy {
|
||||
group := &StreetGroupBy{config: sq.config}
|
||||
group.fields = append([]string{field}, fields...)
|
||||
group.sql = sq.sqlQuery()
|
||||
group.path = func(ctx context.Context) (prev *sql.Selector, err error) {
|
||||
if err := sq.prepareQuery(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return sq.sqlQuery(), nil
|
||||
}
|
||||
return group
|
||||
}
|
||||
|
||||
@@ -288,10 +310,27 @@ func (sq *StreetQuery) GroupBy(field string, fields ...string) *StreetGroupBy {
|
||||
func (sq *StreetQuery) Select(field string, fields ...string) *StreetSelect {
|
||||
selector := &StreetSelect{config: sq.config}
|
||||
selector.fields = append([]string{field}, fields...)
|
||||
selector.sql = sq.sqlQuery()
|
||||
selector.path = func(ctx context.Context) (prev *sql.Selector, err error) {
|
||||
if err := sq.prepareQuery(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return sq.sqlQuery(), nil
|
||||
}
|
||||
return selector
|
||||
}
|
||||
|
||||
func (sq *StreetQuery) prepareQuery(ctx context.Context) error {
|
||||
if sq.path != nil {
|
||||
prev, err := sq.path(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sq.sql = prev
|
||||
}
|
||||
// Privacy and query checks go here.
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sq *StreetQuery) sqlAll(ctx context.Context) ([]*Street, error) {
|
||||
var (
|
||||
nodes = []*Street{}
|
||||
@@ -438,8 +477,9 @@ type StreetGroupBy 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 (sgb *StreetGroupBy) Aggregate(fns ...Aggregate) *StreetGroupBy {
|
||||
|
||||
// Scan applies the group-by query and scan the result into the given value.
|
||||
func (sgb *StreetGroupBy) Scan(ctx context.Context, v interface{}) error {
|
||||
query, err := sgb.path(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sgb.sql = query
|
||||
return sgb.sqlScan(ctx, v)
|
||||
}
|
||||
|
||||
@@ -568,12 +613,18 @@ func (sgb *StreetGroupBy) sqlQuery() *sql.Selector {
|
||||
type StreetSelect 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 (ss *StreetSelect) Scan(ctx context.Context, v interface{}) error {
|
||||
query, err := ss.path(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ss.sql = query
|
||||
return ss.sqlScan(ctx, v)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user