// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. // This source code is licensed under the Apache 2.0 license found // in the LICENSE file in the root directory of this source tree. // Code generated by entc, DO NOT EDIT. package entv2 import ( "context" "fmt" "github.com/facebookincubator/ent/dialect/sql" "github.com/facebookincubator/ent/dialect/sql/sqlgraph" "github.com/facebookincubator/ent/entc/integration/migrate/entv2/pet" "github.com/facebookincubator/ent/entc/integration/migrate/entv2/predicate" "github.com/facebookincubator/ent/schema/field" ) // PetUpdate is the builder for updating Pet entities. type PetUpdate struct { config hooks []Hook mutation *PetMutation predicates []predicate.Pet } // Where adds a new predicate for the builder. func (pu *PetUpdate) Where(ps ...predicate.Pet) *PetUpdate { pu.predicates = append(pu.predicates, ps...) return pu } // Save executes the query and returns the number of rows/vertices matched by this operation. func (pu *PetUpdate) Save(ctx context.Context) (int, error) { var ( err error affected int ) if len(pu.hooks) == 0 { affected, err = pu.sqlSave(ctx) } else { var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*PetMutation) if !ok { return nil, fmt.Errorf("unexpected mutation type %T", m) } pu.mutation = mutation affected, err = pu.sqlSave(ctx) return affected, err }) for i := len(pu.hooks) - 1; i >= 0; i-- { mut = pu.hooks[i](mut) } if _, err := mut.Mutate(ctx, pu.mutation); err != nil { return 0, err } } return affected, err } // SaveX is like Save, but panics if an error occurs. func (pu *PetUpdate) SaveX(ctx context.Context) int { affected, err := pu.Save(ctx) if err != nil { panic(err) } return affected } // Exec executes the query. func (pu *PetUpdate) Exec(ctx context.Context) error { _, err := pu.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (pu *PetUpdate) ExecX(ctx context.Context) { if err := pu.Exec(ctx); err != nil { panic(err) } } func (pu *PetUpdate) sqlSave(ctx context.Context) (n int, err error) { _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: pet.Table, Columns: pet.Columns, ID: &sqlgraph.FieldSpec{ Type: field.TypeInt, Column: pet.FieldID, }, }, } if ps := pu.predicates; len(ps) > 0 { _spec.Predicate = func(selector *sql.Selector) { for i := range ps { ps[i](selector) } } } if n, err = sqlgraph.UpdateNodes(ctx, pu.driver, _spec); err != nil { if _, ok := err.(*sqlgraph.NotFoundError); ok { err = &NotFoundError{pet.Label} } else if cerr, ok := isSQLConstraintError(err); ok { err = cerr } return 0, err } return n, nil } // PetUpdateOne is the builder for updating a single Pet entity. type PetUpdateOne struct { config hooks []Hook mutation *PetMutation } // Save executes the query and returns the updated entity. func (puo *PetUpdateOne) Save(ctx context.Context) (*Pet, error) { var ( err error node *Pet ) if len(puo.hooks) == 0 { node, err = puo.sqlSave(ctx) } else { var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*PetMutation) if !ok { return nil, fmt.Errorf("unexpected mutation type %T", m) } puo.mutation = mutation node, err = puo.sqlSave(ctx) return node, err }) for i := len(puo.hooks) - 1; i >= 0; i-- { mut = puo.hooks[i](mut) } if _, err := mut.Mutate(ctx, puo.mutation); err != nil { return nil, err } } return node, err } // SaveX is like Save, but panics if an error occurs. func (puo *PetUpdateOne) SaveX(ctx context.Context) *Pet { pe, err := puo.Save(ctx) if err != nil { panic(err) } return pe } // Exec executes the query on the entity. func (puo *PetUpdateOne) Exec(ctx context.Context) error { _, err := puo.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (puo *PetUpdateOne) ExecX(ctx context.Context) { if err := puo.Exec(ctx); err != nil { panic(err) } } func (puo *PetUpdateOne) sqlSave(ctx context.Context) (pe *Pet, err error) { _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: pet.Table, Columns: pet.Columns, ID: &sqlgraph.FieldSpec{ Type: field.TypeInt, Column: pet.FieldID, }, }, } id, ok := puo.mutation.ID() if !ok { return nil, fmt.Errorf("missing Pet.ID for update") } _spec.Node.ID.Value = id pe = &Pet{config: puo.config} _spec.Assign = pe.assignValues _spec.ScanValues = pe.scanValues() if err = sqlgraph.UpdateNode(ctx, puo.driver, _spec); err != nil { if _, ok := err.(*sqlgraph.NotFoundError); ok { err = &NotFoundError{pet.Label} } else if cerr, ok := isSQLConstraintError(err); ok { err = cerr } return nil, err } return pe, nil }