mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
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:
committed by
Facebook Github Bot
parent
2b1ff377b5
commit
83d0063437
@@ -124,6 +124,20 @@ func (c *NodeClient) Query() *NodeQuery {
|
||||
return &NodeQuery{config: c.config}
|
||||
}
|
||||
|
||||
// Get returns a Node entity by its id.
|
||||
func (c *NodeClient) Get(ctx context.Context, id int) (*Node, error) {
|
||||
return c.Query().Where(node.ID(id)).Only(ctx)
|
||||
}
|
||||
|
||||
// GetX is like Get, but panics if an error occurs.
|
||||
func (c *NodeClient) GetX(ctx context.Context, id int) *Node {
|
||||
n, err := c.Get(ctx, id)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// QueryParent queries the parent edge of a Node.
|
||||
func (c *NodeClient) QueryParent(n *Node) *NodeQuery {
|
||||
query := &NodeQuery{config: c.config}
|
||||
|
||||
@@ -21,32 +21,41 @@ type NodeDelete struct {
|
||||
predicates []predicate.Node
|
||||
}
|
||||
|
||||
// Where adds a new predicate for the builder.
|
||||
// Where adds a new predicate to the delete builder.
|
||||
func (nd *NodeDelete) Where(ps ...predicate.Node) *NodeDelete {
|
||||
nd.predicates = append(nd.predicates, ps...)
|
||||
return nd
|
||||
}
|
||||
|
||||
// Exec executes the deletion query.
|
||||
func (nd *NodeDelete) Exec(ctx context.Context) error {
|
||||
// Exec executes the deletion query and returns how many vertices were deleted.
|
||||
func (nd *NodeDelete) Exec(ctx context.Context) (int, error) {
|
||||
return nd.sqlExec(ctx)
|
||||
}
|
||||
|
||||
// ExecX is like Exec, but panics if an error occurs.
|
||||
func (nd *NodeDelete) ExecX(ctx context.Context) {
|
||||
if err := nd.Exec(ctx); err != nil {
|
||||
func (nd *NodeDelete) ExecX(ctx context.Context) int {
|
||||
n, err := nd.Exec(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (nd *NodeDelete) sqlExec(ctx context.Context) error {
|
||||
func (nd *NodeDelete) sqlExec(ctx context.Context) (int, error) {
|
||||
var res sql.Result
|
||||
selector := sql.Select().From(sql.Table(node.Table))
|
||||
for _, p := range nd.predicates {
|
||||
p(selector)
|
||||
}
|
||||
query, args := sql.Delete(node.Table).FromSelect(selector).Query()
|
||||
return nd.driver.Exec(ctx, query, args, &res)
|
||||
if err := nd.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
|
||||
}
|
||||
|
||||
// NodeDeleteOne is the builder for deleting a single Node entity.
|
||||
@@ -56,7 +65,15 @@ type NodeDeleteOne struct {
|
||||
|
||||
// Exec executes the deletion query.
|
||||
func (ndo *NodeDeleteOne) Exec(ctx context.Context) error {
|
||||
return ndo.nd.Exec(ctx)
|
||||
n, err := ndo.nd.Exec(ctx)
|
||||
switch {
|
||||
case err != nil:
|
||||
return err
|
||||
case n == 0:
|
||||
return &ErrNotFound{node.Label}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// ExecX is like Exec, but panics if an error occurs.
|
||||
|
||||
@@ -80,20 +80,6 @@ func (nq *NodeQuery) QueryChildren() *NodeQuery {
|
||||
return query
|
||||
}
|
||||
|
||||
// Get returns a Node entity by its id.
|
||||
func (nq *NodeQuery) Get(ctx context.Context, id int) (*Node, error) {
|
||||
return nq.Where(node.ID(id)).Only(ctx)
|
||||
}
|
||||
|
||||
// GetX is like Get, but panics if an error occurs.
|
||||
func (nq *NodeQuery) GetX(ctx context.Context, id int) *Node {
|
||||
n, err := nq.Get(ctx, id)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// First returns the first Node entity in the query. Returns *ErrNotFound when no node was found.
|
||||
func (nq *NodeQuery) First(ctx context.Context) (*Node, error) {
|
||||
ns, err := nq.Limit(1).All(ctx)
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
type NodeUpdate struct {
|
||||
config
|
||||
value *int
|
||||
addvalue *int
|
||||
parent map[int]struct{}
|
||||
children map[int]struct{}
|
||||
clearedParent bool
|
||||
@@ -40,6 +41,12 @@ func (nu *NodeUpdate) SetValue(i int) *NodeUpdate {
|
||||
return nu
|
||||
}
|
||||
|
||||
// AddValue adds i to value.
|
||||
func (nu *NodeUpdate) AddValue(i int) *NodeUpdate {
|
||||
nu.addvalue = &i
|
||||
return nu
|
||||
}
|
||||
|
||||
// SetParentID sets the parent edge to Node by id.
|
||||
func (nu *NodeUpdate) SetParentID(id int) *NodeUpdate {
|
||||
if nu.parent == nil {
|
||||
@@ -170,9 +177,13 @@ func (nu *NodeUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
||||
res sql.Result
|
||||
builder = sql.Update(node.Table).Where(sql.InInts(node.FieldID, ids...))
|
||||
)
|
||||
if nu.value != nil {
|
||||
if value := nu.value; value != nil {
|
||||
update = true
|
||||
builder.Set(node.FieldValue, *nu.value)
|
||||
builder.Set(node.FieldValue, *value)
|
||||
}
|
||||
if value := nu.addvalue; value != nil {
|
||||
update = true
|
||||
builder.Add(node.FieldValue, *value)
|
||||
}
|
||||
if update {
|
||||
query, args := builder.Query()
|
||||
@@ -247,6 +258,7 @@ type NodeUpdateOne struct {
|
||||
config
|
||||
id int
|
||||
value *int
|
||||
addvalue *int
|
||||
parent map[int]struct{}
|
||||
children map[int]struct{}
|
||||
clearedParent bool
|
||||
@@ -259,6 +271,12 @@ func (nuo *NodeUpdateOne) SetValue(i int) *NodeUpdateOne {
|
||||
return nuo
|
||||
}
|
||||
|
||||
// AddValue adds i to value.
|
||||
func (nuo *NodeUpdateOne) AddValue(i int) *NodeUpdateOne {
|
||||
nuo.addvalue = &i
|
||||
return nuo
|
||||
}
|
||||
|
||||
// SetParentID sets the parent edge to Node by id.
|
||||
func (nuo *NodeUpdateOne) SetParentID(id int) *NodeUpdateOne {
|
||||
if nuo.parent == nil {
|
||||
@@ -392,10 +410,15 @@ func (nuo *NodeUpdateOne) sqlSave(ctx context.Context) (n *Node, err error) {
|
||||
res sql.Result
|
||||
builder = sql.Update(node.Table).Where(sql.InInts(node.FieldID, ids...))
|
||||
)
|
||||
if nuo.value != nil {
|
||||
if value := nuo.value; value != nil {
|
||||
update = true
|
||||
builder.Set(node.FieldValue, *nuo.value)
|
||||
n.Value = *nuo.value
|
||||
builder.Set(node.FieldValue, *value)
|
||||
n.Value = *value
|
||||
}
|
||||
if value := nuo.addvalue; value != nil {
|
||||
update = true
|
||||
builder.Add(node.FieldValue, *value)
|
||||
n.Value += *value
|
||||
}
|
||||
if update {
|
||||
query, args := builder.Query()
|
||||
|
||||
Reference in New Issue
Block a user