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

@@ -27,6 +27,7 @@ type GroupQuery struct {
limit *int
offset *int
order []OrderFunc
fields []string
predicates []predicate.Group
// eager-loading edges.
withUsers *UserQuery
@@ -350,18 +351,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 {
@@ -826,20 +825,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)
}
@@ -1039,11 +1035,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 {

View File

@@ -27,6 +27,7 @@ type PetQuery struct {
limit *int
offset *int
order []OrderFunc
fields []string
predicates []predicate.Pet
// eager-loading edges.
withFriends *PetQuery
@@ -350,18 +351,16 @@ func (pq *PetQuery) GroupBy(field string, fields ...string) *PetGroupBy {
// Scan(ctx, &v)
//
func (pq *PetQuery) Select(field string, fields ...string) *PetSelect {
selector := &PetSelect{config: pq.config}
selector.fields = append([]string{field}, fields...)
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
pq.fields = append([]string{field}, fields...)
return &PetSelect{PetQuery: pq}
}
func (pq *PetQuery) prepareQuery(ctx context.Context) error {
for _, f := range pq.fields {
if !pet.ValidColumn(f) {
return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
}
if pq.path != nil {
prev, err := pq.path(ctx)
if err != nil {
@@ -826,20 +825,17 @@ func (pgb *PetGroupBy) sqlQuery() *sql.Selector {
// PetSelect is the builder for select fields of Pet entities.
type PetSelect struct {
config
fields []string
*PetQuery
// 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 (ps *PetSelect) Scan(ctx context.Context, v interface{}) error {
query, err := ps.path(ctx)
if err != nil {
if err := ps.prepareQuery(ctx); err != nil {
return err
}
ps.sql = query
ps.sql = ps.PetQuery.sqlQuery()
return ps.sqlScan(ctx, v)
}
@@ -1039,11 +1035,6 @@ func (ps *PetSelect) BoolX(ctx context.Context) bool {
}
func (ps *PetSelect) sqlScan(ctx context.Context, v interface{}) error {
for _, f := range ps.fields {
if !pet.ValidColumn(f) {
return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for selection", f)}
}
}
rows := &sql.Rows{}
query, args := ps.sqlQuery().Query()
if err := ps.driver.Query(ctx, query, args, rows); err != nil {

View File

@@ -28,6 +28,7 @@ type UserQuery struct {
limit *int
offset *int
order []OrderFunc
fields []string
predicates []predicate.User
// eager-loading edges.
withPets *PetQuery
@@ -420,18 +421,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 {
@@ -988,20 +987,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)
}
@@ -1201,11 +1197,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 {