entc/gen: move fields selection to top builder (#1093)

This commit is contained in:
Ariel Mashraki
2020-12-28 12:28:07 +02:00
committed by GitHub
parent 0f7ab71e24
commit c4a43bc2be
85 changed files with 909 additions and 1626 deletions

View File

@@ -25,6 +25,7 @@ type UserQuery struct {
limit *int
offset *int
order []OrderFunc
fields []string
predicates []predicate.User
// intermediate query (i.e. traversal path).
sql *sql.Selector
@@ -276,18 +277,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 {
@@ -656,20 +655,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)
}
@@ -869,11 +865,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 {