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 CarQuery 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 *CarQuery) Order(o ...Order) *CarQuery {
// QueryOwner chains the current query on the owner edge.
func (cq *CarQuery) QueryOwner() *UserQuery {
query := &UserQuery{config: cq.config}
step := sqlgraph.NewStep(
sqlgraph.From(car.Table, car.FieldID, cq.sqlQuery()),
sqlgraph.To(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, car.OwnerTable, car.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(car.Table, car.FieldID, cq.sqlQuery()),
sqlgraph.To(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, car.OwnerTable, car.OwnerColumn),
)
fromU = sqlgraph.SetNeighbors(cq.driver.Dialect(), step)
return fromU, nil
}
return query
}
@@ -167,6 +174,9 @@ func (cq *CarQuery) OnlyXID(ctx context.Context) int {
// All executes the query and returns a list of Cars.
func (cq *CarQuery) All(ctx context.Context) ([]*Car, error) {
if err := cq.prepareQuery(ctx); err != nil {
return nil, err
}
return cq.sqlAll(ctx)
}
@@ -199,6 +209,9 @@ func (cq *CarQuery) IDsX(ctx context.Context) []int {
// Count returns the count of the given query.
func (cq *CarQuery) 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 *CarQuery) CountX(ctx context.Context) int {
// Exist returns true if the query has elements in the graph.
func (cq *CarQuery) 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 *CarQuery) Clone() *CarQuery {
unique: append([]string{}, cq.unique...),
predicates: append([]predicate.Car{}, cq.predicates...),
// clone intermediate query.
sql: cq.sql.Clone(),
sql: cq.sql.Clone(),
path: cq.path,
}
}
@@ -269,7 +286,12 @@ func (cq *CarQuery) WithOwner(opts ...func(*UserQuery)) *CarQuery {
func (cq *CarQuery) GroupBy(field string, fields ...string) *CarGroupBy {
group := &CarGroupBy{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 *CarQuery) GroupBy(field string, fields ...string) *CarGroupBy {
func (cq *CarQuery) Select(field string, fields ...string) *CarSelect {
selector := &CarSelect{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 *CarQuery) 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 *CarQuery) sqlAll(ctx context.Context) ([]*Car, error) {
var (
nodes = []*Car{}
@@ -438,8 +477,9 @@ type CarGroupBy 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 *CarGroupBy) Aggregate(fns ...Aggregate) *CarGroupBy {
// Scan applies the group-by query and scan the result into the given value.
func (cgb *CarGroupBy) 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 *CarGroupBy) sqlQuery() *sql.Selector {
type CarSelect 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 *CarSelect) 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

@@ -196,14 +196,16 @@ func (c *CarClient) GetX(ctx context.Context, id int) *Car {
// QueryOwner queries the owner edge of a Car.
func (c *CarClient) QueryOwner(ca *Car) *UserQuery {
query := &UserQuery{config: c.config}
id := ca.ID
step := sqlgraph.NewStep(
sqlgraph.From(car.Table, car.FieldID, id),
sqlgraph.To(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, car.OwnerTable, car.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(car.Table, car.FieldID, id),
sqlgraph.To(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, car.OwnerTable, car.OwnerColumn),
)
fromV = sqlgraph.Neighbors(ca.driver.Dialect(), step)
return fromV, nil
}
return query
}
@@ -293,14 +295,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
}
@@ -390,28 +394,32 @@ func (c *UserClient) GetX(ctx context.Context, id int) *User {
// QueryCars queries the cars edge of a User.
func (c *UserClient) QueryCars(u *User) *CarQuery {
query := &CarQuery{config: c.config}
id := u.ID
step := sqlgraph.NewStep(
sqlgraph.From(user.Table, user.FieldID, id),
sqlgraph.To(car.Table, car.FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, user.CarsTable, user.CarsColumn),
)
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(car.Table, car.FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, user.CarsTable, user.CarsColumn),
)
fromV = sqlgraph.Neighbors(u.driver.Dialect(), step)
return fromV, nil
}
return query
}
// 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

@@ -33,8 +33,9 @@ type UserQuery struct {
// eager-loading edges.
withCars *CarQuery
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.
@@ -64,24 +65,36 @@ func (uq *UserQuery) Order(o ...Order) *UserQuery {
// QueryCars chains the current query on the cars edge.
func (uq *UserQuery) QueryCars() *CarQuery {
query := &CarQuery{config: uq.config}
step := sqlgraph.NewStep(
sqlgraph.From(user.Table, user.FieldID, uq.sqlQuery()),
sqlgraph.To(car.Table, car.FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, user.CarsTable, user.CarsColumn),
)
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(car.Table, car.FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, user.CarsTable, user.CarsColumn),
)
fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
return fromU, nil
}
return query
}
// 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
}
@@ -181,6 +194,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)
}
@@ -213,6 +229,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)
}
@@ -227,6 +246,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)
}
@@ -250,7 +272,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,
}
}
@@ -294,7 +317,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
}
@@ -313,10 +341,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{}
@@ -520,8 +565,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.
@@ -532,6 +578,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)
}
@@ -650,12 +701,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)
}