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

@@ -222,6 +222,7 @@ func (cu *CityUpdate) sqlSave(ctx context.Context) (n int, err error) {
// CityUpdateOne is the builder for updating a single City entity.
type CityUpdateOne struct {
config
fields []string
hooks []Hook
mutation *CityMutation
}
@@ -273,6 +274,13 @@ func (cuo *CityUpdateOne) RemoveStreets(s ...*Street) *CityUpdateOne {
return cuo.RemoveStreetIDs(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 (cuo *CityUpdateOne) Select(field string, fields ...string) *CityUpdateOne {
cuo.fields = append([]string{field}, fields...)
return cuo
}
// Save executes the query and returns the updated City entity.
func (cuo *CityUpdateOne) Save(ctx context.Context) (*City, error) {
var (
@@ -340,6 +348,18 @@ func (cuo *CityUpdateOne) sqlSave(ctx context.Context) (_node *City, err error)
return nil, &ValidationError{Name: "ID", err: fmt.Errorf("missing City.ID for update")}
}
_spec.Node.ID.Value = id
if fields := cuo.fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, city.FieldID)
for _, f := range fields {
if !city.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
if f != city.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, f)
}
}
}
if ps := cuo.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {

View File

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