// 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 (@generated) by entc, DO NOT EDIT. package entv2 import ( "context" "fmt" "github.com/facebookincubator/ent/dialect/sql" "github.com/facebookincubator/ent/entc/integration/migrate/entv2/pet" "github.com/facebookincubator/ent/entc/integration/migrate/entv2/predicate" ) // PetUpdate is the builder for updating Pet entities. type PetUpdate struct { config 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) { return pu.sqlSave(ctx) } // 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) { selector := sql.Select(pet.FieldID).From(sql.Table(pet.Table)) for _, p := range pu.predicates { p(selector) } rows := &sql.Rows{} query, args := selector.Query() if err = pu.driver.Query(ctx, query, args, rows); err != nil { return 0, err } defer rows.Close() var ids []int for rows.Next() { var id int if err := rows.Scan(&id); err != nil { return 0, fmt.Errorf("entv2: failed reading id: %v", err) } ids = append(ids, id) } if len(ids) == 0 { return 0, nil } tx, err := pu.driver.Tx(ctx) if err != nil { return 0, err } if err = tx.Commit(); err != nil { return 0, err } return len(ids), nil } // PetUpdateOne is the builder for updating a single Pet entity. type PetUpdateOne struct { config id int } // Save executes the query and returns the updated entity. func (puo *PetUpdateOne) Save(ctx context.Context) (*Pet, error) { return puo.sqlSave(ctx) } // 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) { selector := sql.Select(pet.Columns...).From(sql.Table(pet.Table)) pet.ID(puo.id)(selector) rows := &sql.Rows{} query, args := selector.Query() if err = puo.driver.Query(ctx, query, args, rows); err != nil { return nil, err } defer rows.Close() var ids []int for rows.Next() { var id int pe = &Pet{config: puo.config} if err := pe.FromRows(rows); err != nil { return nil, fmt.Errorf("entv2: failed scanning row into Pet: %v", err) } id = pe.ID ids = append(ids, id) } switch n := len(ids); { case n == 0: return nil, &ErrNotFound{fmt.Sprintf("Pet with id: %v", puo.id)} case n > 1: return nil, fmt.Errorf("entv2: more than one Pet with the same id: %v", puo.id) } tx, err := puo.driver.Tx(ctx) if err != nil { return nil, err } if err = tx.Commit(); err != nil { return nil, err } return pe, nil }