entc/gen: returns affected rows in delete

Summary: Pull Request resolved: https://github.com/facebookexternal/fbc/pull/1455

Reviewed By: alexsn

Differential Revision: D17396880

fbshipit-source-id: 3a3a9849b816777bd0ef94cf55b2291705a37df6
This commit is contained in:
Ariel Mashraki
2019-09-16 08:45:45 -07:00
committed by Facebook Github Bot
parent 2b1ff377b5
commit 83d0063437
82 changed files with 1566 additions and 712 deletions

View File

@@ -21,32 +21,41 @@ type CardDelete struct {
predicates []predicate.Card
}
// Where adds a new predicate for the builder.
// Where adds a new predicate to the delete builder.
func (cd *CardDelete) Where(ps ...predicate.Card) *CardDelete {
cd.predicates = append(cd.predicates, ps...)
return cd
}
// Exec executes the deletion query.
func (cd *CardDelete) Exec(ctx context.Context) error {
// Exec executes the deletion query and returns how many vertices were deleted.
func (cd *CardDelete) Exec(ctx context.Context) (int, error) {
return cd.sqlExec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (cd *CardDelete) ExecX(ctx context.Context) {
if err := cd.Exec(ctx); err != nil {
func (cd *CardDelete) ExecX(ctx context.Context) int {
n, err := cd.Exec(ctx)
if err != nil {
panic(err)
}
return n
}
func (cd *CardDelete) sqlExec(ctx context.Context) error {
func (cd *CardDelete) sqlExec(ctx context.Context) (int, error) {
var res sql.Result
selector := sql.Select().From(sql.Table(card.Table))
for _, p := range cd.predicates {
p(selector)
}
query, args := sql.Delete(card.Table).FromSelect(selector).Query()
return cd.driver.Exec(ctx, query, args, &res)
if err := cd.driver.Exec(ctx, query, args, &res); err != nil {
return 0, err
}
affected, err := res.RowsAffected()
if err != nil {
return 0, err
}
return int(affected), nil
}
// CardDeleteOne is the builder for deleting a single Card entity.
@@ -56,7 +65,15 @@ type CardDeleteOne struct {
// Exec executes the deletion query.
func (cdo *CardDeleteOne) Exec(ctx context.Context) error {
return cdo.cd.Exec(ctx)
n, err := cdo.cd.Exec(ctx)
switch {
case err != nil:
return err
case n == 0:
return &ErrNotFound{card.Label}
default:
return nil
}
}
// ExecX is like Exec, but panics if an error occurs.

View File

@@ -68,20 +68,6 @@ func (cq *CardQuery) QueryOwner() *UserQuery {
return query
}
// Get returns a Card entity by its id.
func (cq *CardQuery) Get(ctx context.Context, id int) (*Card, error) {
return cq.Where(card.ID(id)).Only(ctx)
}
// GetX is like Get, but panics if an error occurs.
func (cq *CardQuery) GetX(ctx context.Context, id int) *Card {
c, err := cq.Get(ctx, id)
if err != nil {
panic(err)
}
return c
}
// First returns the first Card entity in the query. Returns *ErrNotFound when no card was found.
func (cq *CardQuery) First(ctx context.Context) (*Card, error) {
cs, err := cq.Limit(1).All(ctx)

View File

@@ -132,13 +132,13 @@ func (cu *CardUpdate) sqlSave(ctx context.Context) (n int, err error) {
res sql.Result
builder = sql.Update(card.Table).Where(sql.InInts(card.FieldID, ids...))
)
if cu.expired != nil {
if value := cu.expired; value != nil {
update = true
builder.Set(card.FieldExpired, *cu.expired)
builder.Set(card.FieldExpired, *value)
}
if cu.number != nil {
if value := cu.number; value != nil {
update = true
builder.Set(card.FieldNumber, *cu.number)
builder.Set(card.FieldNumber, *value)
}
if update {
query, args := builder.Query()
@@ -290,15 +290,15 @@ func (cuo *CardUpdateOne) sqlSave(ctx context.Context) (c *Card, err error) {
res sql.Result
builder = sql.Update(card.Table).Where(sql.InInts(card.FieldID, ids...))
)
if cuo.expired != nil {
if value := cuo.expired; value != nil {
update = true
builder.Set(card.FieldExpired, *cuo.expired)
c.Expired = *cuo.expired
builder.Set(card.FieldExpired, *value)
c.Expired = *value
}
if cuo.number != nil {
if value := cuo.number; value != nil {
update = true
builder.Set(card.FieldNumber, *cuo.number)
c.Number = *cuo.number
builder.Set(card.FieldNumber, *value)
c.Number = *value
}
if update {
query, args := builder.Query()

View File

@@ -130,6 +130,20 @@ func (c *CardClient) Query() *CardQuery {
return &CardQuery{config: c.config}
}
// Get returns a Card entity by its id.
func (c *CardClient) Get(ctx context.Context, id int) (*Card, error) {
return c.Query().Where(card.ID(id)).Only(ctx)
}
// GetX is like Get, but panics if an error occurs.
func (c *CardClient) GetX(ctx context.Context, id int) *Card {
ca, err := c.Get(ctx, id)
if err != nil {
panic(err)
}
return ca
}
// QueryOwner queries the owner edge of a Card.
func (c *CardClient) QueryOwner(ca *Card) *UserQuery {
query := &UserQuery{config: c.config}
@@ -193,6 +207,20 @@ func (c *UserClient) Query() *UserQuery {
return &UserQuery{config: c.config}
}
// Get returns a User entity by its id.
func (c *UserClient) Get(ctx context.Context, id int) (*User, error) {
return c.Query().Where(user.ID(id)).Only(ctx)
}
// GetX is like Get, but panics if an error occurs.
func (c *UserClient) GetX(ctx context.Context, id int) *User {
u, err := c.Get(ctx, id)
if err != nil {
panic(err)
}
return u
}
// QueryCard queries the card edge of a User.
func (c *UserClient) QueryCard(u *User) *CardQuery {
query := &CardQuery{config: c.config}

View File

@@ -21,32 +21,41 @@ type UserDelete struct {
predicates []predicate.User
}
// Where adds a new predicate for the builder.
// Where adds a new predicate to the delete builder.
func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete {
ud.predicates = append(ud.predicates, ps...)
return ud
}
// Exec executes the deletion query.
func (ud *UserDelete) Exec(ctx context.Context) error {
// Exec executes the deletion query and returns how many vertices were deleted.
func (ud *UserDelete) Exec(ctx context.Context) (int, error) {
return ud.sqlExec(ctx)
}
// ExecX is like Exec, but panics if an error occurs.
func (ud *UserDelete) ExecX(ctx context.Context) {
if err := ud.Exec(ctx); err != nil {
func (ud *UserDelete) ExecX(ctx context.Context) int {
n, err := ud.Exec(ctx)
if err != nil {
panic(err)
}
return n
}
func (ud *UserDelete) sqlExec(ctx context.Context) error {
func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) {
var res sql.Result
selector := sql.Select().From(sql.Table(user.Table))
for _, p := range ud.predicates {
p(selector)
}
query, args := sql.Delete(user.Table).FromSelect(selector).Query()
return ud.driver.Exec(ctx, query, args, &res)
if err := ud.driver.Exec(ctx, query, args, &res); err != nil {
return 0, err
}
affected, err := res.RowsAffected()
if err != nil {
return 0, err
}
return int(affected), nil
}
// UserDeleteOne is the builder for deleting a single User entity.
@@ -56,7 +65,15 @@ type UserDeleteOne struct {
// Exec executes the deletion query.
func (udo *UserDeleteOne) Exec(ctx context.Context) error {
return udo.ud.Exec(ctx)
n, err := udo.ud.Exec(ctx)
switch {
case err != nil:
return err
case n == 0:
return &ErrNotFound{user.Label}
default:
return nil
}
}
// ExecX is like Exec, but panics if an error occurs.

View File

@@ -68,20 +68,6 @@ func (uq *UserQuery) QueryCard() *CardQuery {
return query
}
// Get returns a User entity by its id.
func (uq *UserQuery) Get(ctx context.Context, id int) (*User, error) {
return uq.Where(user.ID(id)).Only(ctx)
}
// GetX is like Get, but panics if an error occurs.
func (uq *UserQuery) GetX(ctx context.Context, id int) *User {
u, err := uq.Get(ctx, id)
if err != nil {
panic(err)
}
return u
}
// First returns the first User entity in the query. Returns *ErrNotFound when no user was found.
func (uq *UserQuery) First(ctx context.Context) (*User, error) {
us, err := uq.Limit(1).All(ctx)

View File

@@ -22,6 +22,7 @@ import (
type UserUpdate struct {
config
age *int
addage *int
name *string
card map[int]struct{}
clearedCard bool
@@ -40,6 +41,12 @@ func (uu *UserUpdate) SetAge(i int) *UserUpdate {
return uu
}
// AddAge adds i to age.
func (uu *UserUpdate) AddAge(i int) *UserUpdate {
uu.addage = &i
return uu
}
// SetName sets the name field.
func (uu *UserUpdate) SetName(s string) *UserUpdate {
uu.name = &s
@@ -136,13 +143,17 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) {
res sql.Result
builder = sql.Update(user.Table).Where(sql.InInts(user.FieldID, ids...))
)
if uu.age != nil {
if value := uu.age; value != nil {
update = true
builder.Set(user.FieldAge, *uu.age)
builder.Set(user.FieldAge, *value)
}
if uu.name != nil {
if value := uu.addage; value != nil {
update = true
builder.Set(user.FieldName, *uu.name)
builder.Add(user.FieldAge, *value)
}
if value := uu.name; value != nil {
update = true
builder.Set(user.FieldName, *value)
}
if update {
query, args := builder.Query()
@@ -189,6 +200,7 @@ type UserUpdateOne struct {
config
id int
age *int
addage *int
name *string
card map[int]struct{}
clearedCard bool
@@ -200,6 +212,12 @@ func (uuo *UserUpdateOne) SetAge(i int) *UserUpdateOne {
return uuo
}
// AddAge adds i to age.
func (uuo *UserUpdateOne) AddAge(i int) *UserUpdateOne {
uuo.addage = &i
return uuo
}
// SetName sets the name field.
func (uuo *UserUpdateOne) SetName(s string) *UserUpdateOne {
uuo.name = &s
@@ -299,15 +317,20 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (u *User, err error) {
res sql.Result
builder = sql.Update(user.Table).Where(sql.InInts(user.FieldID, ids...))
)
if uuo.age != nil {
if value := uuo.age; value != nil {
update = true
builder.Set(user.FieldAge, *uuo.age)
u.Age = *uuo.age
builder.Set(user.FieldAge, *value)
u.Age = *value
}
if uuo.name != nil {
if value := uuo.addage; value != nil {
update = true
builder.Set(user.FieldName, *uuo.name)
u.Name = *uuo.name
builder.Add(user.FieldAge, *value)
u.Age += *value
}
if value := uuo.name; value != nil {
update = true
builder.Set(user.FieldName, *value)
u.Name = *value
}
if update {
query, args := builder.Query()