entc/gen: allow selecting partial fields on update-one

This commit is contained in:
Ariel Mashraki
2021-03-14 15:55:17 +02:00
committed by Ariel Mashraki
parent 0cd637ceb2
commit 5d70144f44
96 changed files with 1814 additions and 22 deletions

View File

@@ -192,6 +192,7 @@ func (pu *PetUpdate) sqlSave(ctx context.Context) (n int, err error) {
// PetUpdateOne is the builder for updating a single Pet entity.
type PetUpdateOne struct {
config
fields []string
hooks []Hook
mutation *PetMutation
}
@@ -232,6 +233,13 @@ func (puo *PetUpdateOne) ClearOwner() *PetUpdateOne {
return puo
}
// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (puo *PetUpdateOne) Select(field string, fields ...string) *PetUpdateOne {
puo.fields = append([]string{field}, fields...)
return puo
}
// Save executes the query and returns the updated Pet entity.
func (puo *PetUpdateOne) Save(ctx context.Context) (*Pet, error) {
var (
@@ -299,6 +307,18 @@ func (puo *PetUpdateOne) sqlSave(ctx context.Context) (_node *Pet, err error) {
return nil, &ValidationError{Name: "ID", err: fmt.Errorf("missing Pet.ID for update")}
}
_spec.Node.ID.Value = id
if fields := puo.fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, pet.FieldID)
for _, f := range fields {
if !pet.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
if f != pet.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, f)
}
}
}
if ps := puo.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {

View File

@@ -249,6 +249,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) {
// UserUpdateOne is the builder for updating a single User entity.
type UserUpdateOne struct {
config
fields []string
hooks []Hook
mutation *UserMutation
}
@@ -313,6 +314,13 @@ func (uuo *UserUpdateOne) RemovePets(p ...*Pet) *UserUpdateOne {
return uuo.RemovePetIDs(ids...)
}
// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne {
uuo.fields = append([]string{field}, fields...)
return uuo
}
// Save executes the query and returns the updated User entity.
func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) {
var (
@@ -380,6 +388,18 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error)
return nil, &ValidationError{Name: "ID", err: fmt.Errorf("missing User.ID for update")}
}
_spec.Node.ID.Value = id
if fields := uuo.fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, user.FieldID)
for _, f := range fields {
if !user.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
if f != user.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, f)
}
}
}
if ps := uuo.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {