mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/gen: move fields selection to top builder (#1093)
This commit is contained in:
@@ -26,6 +26,7 @@ type CarQuery struct {
|
||||
limit *int
|
||||
offset *int
|
||||
order []OrderFunc
|
||||
fields []string
|
||||
predicates []predicate.Car
|
||||
// eager-loading edges.
|
||||
withOwner *UserQuery
|
||||
@@ -314,18 +315,16 @@ func (cq *CarQuery) GroupBy(field string, fields ...string) *CarGroupBy {
|
||||
// Scan(ctx, &v)
|
||||
//
|
||||
func (cq *CarQuery) Select(field string, fields ...string) *CarSelect {
|
||||
selector := &CarSelect{config: cq.config}
|
||||
selector.fields = append([]string{field}, fields...)
|
||||
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
|
||||
cq.fields = append([]string{field}, fields...)
|
||||
return &CarSelect{CarQuery: cq}
|
||||
}
|
||||
|
||||
func (cq *CarQuery) prepareQuery(ctx context.Context) error {
|
||||
for _, f := range cq.fields {
|
||||
if !car.ValidColumn(f) {
|
||||
return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
|
||||
}
|
||||
}
|
||||
if cq.path != nil {
|
||||
prev, err := cq.path(ctx)
|
||||
if err != nil {
|
||||
@@ -725,20 +724,17 @@ func (cgb *CarGroupBy) sqlQuery() *sql.Selector {
|
||||
|
||||
// CarSelect is the builder for select fields of Car entities.
|
||||
type CarSelect struct {
|
||||
config
|
||||
fields []string
|
||||
*CarQuery
|
||||
// intermediate query (i.e. traversal path).
|
||||
sql *sql.Selector
|
||||
path func(context.Context) (*sql.Selector, error)
|
||||
sql *sql.Selector
|
||||
}
|
||||
|
||||
// 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 {
|
||||
if err := cs.prepareQuery(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
cs.sql = query
|
||||
cs.sql = cs.CarQuery.sqlQuery()
|
||||
return cs.sqlScan(ctx, v)
|
||||
}
|
||||
|
||||
@@ -938,11 +934,6 @@ func (cs *CarSelect) BoolX(ctx context.Context) bool {
|
||||
}
|
||||
|
||||
func (cs *CarSelect) sqlScan(ctx context.Context, v interface{}) error {
|
||||
for _, f := range cs.fields {
|
||||
if !car.ValidColumn(f) {
|
||||
return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for selection", f)}
|
||||
}
|
||||
}
|
||||
rows := &sql.Rows{}
|
||||
query, args := cs.sqlQuery().Query()
|
||||
if err := cs.driver.Query(ctx, query, args, rows); err != nil {
|
||||
|
||||
@@ -27,6 +27,7 @@ type GroupQuery struct {
|
||||
limit *int
|
||||
offset *int
|
||||
order []OrderFunc
|
||||
fields []string
|
||||
predicates []predicate.Group
|
||||
// eager-loading edges.
|
||||
withUsers *UserQuery
|
||||
@@ -314,18 +315,16 @@ func (gq *GroupQuery) GroupBy(field string, fields ...string) *GroupGroupBy {
|
||||
// Scan(ctx, &v)
|
||||
//
|
||||
func (gq *GroupQuery) Select(field string, fields ...string) *GroupSelect {
|
||||
selector := &GroupSelect{config: gq.config}
|
||||
selector.fields = append([]string{field}, fields...)
|
||||
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
|
||||
gq.fields = append([]string{field}, fields...)
|
||||
return &GroupSelect{GroupQuery: gq}
|
||||
}
|
||||
|
||||
func (gq *GroupQuery) prepareQuery(ctx context.Context) error {
|
||||
for _, f := range gq.fields {
|
||||
if !group.ValidColumn(f) {
|
||||
return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
|
||||
}
|
||||
}
|
||||
if gq.path != nil {
|
||||
prev, err := gq.path(ctx)
|
||||
if err != nil {
|
||||
@@ -757,20 +756,17 @@ func (ggb *GroupGroupBy) sqlQuery() *sql.Selector {
|
||||
|
||||
// GroupSelect is the builder for select fields of Group entities.
|
||||
type GroupSelect struct {
|
||||
config
|
||||
fields []string
|
||||
*GroupQuery
|
||||
// intermediate query (i.e. traversal path).
|
||||
sql *sql.Selector
|
||||
path func(context.Context) (*sql.Selector, error)
|
||||
sql *sql.Selector
|
||||
}
|
||||
|
||||
// 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 {
|
||||
if err := gs.prepareQuery(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
gs.sql = query
|
||||
gs.sql = gs.GroupQuery.sqlQuery()
|
||||
return gs.sqlScan(ctx, v)
|
||||
}
|
||||
|
||||
@@ -970,11 +966,6 @@ func (gs *GroupSelect) BoolX(ctx context.Context) bool {
|
||||
}
|
||||
|
||||
func (gs *GroupSelect) sqlScan(ctx context.Context, v interface{}) error {
|
||||
for _, f := range gs.fields {
|
||||
if !group.ValidColumn(f) {
|
||||
return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for selection", f)}
|
||||
}
|
||||
}
|
||||
rows := &sql.Rows{}
|
||||
query, args := gs.sqlQuery().Query()
|
||||
if err := gs.driver.Query(ctx, query, args, rows); err != nil {
|
||||
|
||||
@@ -28,6 +28,7 @@ type UserQuery struct {
|
||||
limit *int
|
||||
offset *int
|
||||
order []OrderFunc
|
||||
fields []string
|
||||
predicates []predicate.User
|
||||
// eager-loading edges.
|
||||
withCars *CarQuery
|
||||
@@ -350,18 +351,16 @@ func (uq *UserQuery) GroupBy(field string, fields ...string) *UserGroupBy {
|
||||
// Scan(ctx, &v)
|
||||
//
|
||||
func (uq *UserQuery) Select(field string, fields ...string) *UserSelect {
|
||||
selector := &UserSelect{config: uq.config}
|
||||
selector.fields = append([]string{field}, fields...)
|
||||
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
|
||||
uq.fields = append([]string{field}, fields...)
|
||||
return &UserSelect{UserQuery: uq}
|
||||
}
|
||||
|
||||
func (uq *UserQuery) prepareQuery(ctx context.Context) error {
|
||||
for _, f := range uq.fields {
|
||||
if !user.ValidColumn(f) {
|
||||
return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
|
||||
}
|
||||
}
|
||||
if uq.path != nil {
|
||||
prev, err := uq.path(ctx)
|
||||
if err != nil {
|
||||
@@ -823,20 +822,17 @@ func (ugb *UserGroupBy) sqlQuery() *sql.Selector {
|
||||
|
||||
// UserSelect is the builder for select fields of User entities.
|
||||
type UserSelect struct {
|
||||
config
|
||||
fields []string
|
||||
*UserQuery
|
||||
// intermediate query (i.e. traversal path).
|
||||
sql *sql.Selector
|
||||
path func(context.Context) (*sql.Selector, error)
|
||||
sql *sql.Selector
|
||||
}
|
||||
|
||||
// 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 {
|
||||
if err := us.prepareQuery(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
us.sql = query
|
||||
us.sql = us.UserQuery.sqlQuery()
|
||||
return us.sqlScan(ctx, v)
|
||||
}
|
||||
|
||||
@@ -1036,11 +1032,6 @@ func (us *UserSelect) BoolX(ctx context.Context) bool {
|
||||
}
|
||||
|
||||
func (us *UserSelect) sqlScan(ctx context.Context, v interface{}) error {
|
||||
for _, f := range us.fields {
|
||||
if !user.ValidColumn(f) {
|
||||
return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for selection", f)}
|
||||
}
|
||||
}
|
||||
rows := &sql.Rows{}
|
||||
query, args := us.sqlQuery().Query()
|
||||
if err := us.driver.Query(ctx, query, args, rows); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user