mirror of
https://github.com/ent/ent.git
synced 2026-05-22 09:31:45 +03:00
entc/gen: move mutation hooks logic to a shared generic function (#3180)
This commit is contained in:
@@ -63,49 +63,7 @@ func (cc *CarCreate) Mutation() *CarMutation {
|
||||
|
||||
// Save creates the Car in the database.
|
||||
func (cc *CarCreate) Save(ctx context.Context) (*Car, error) {
|
||||
var (
|
||||
err error
|
||||
node *Car
|
||||
)
|
||||
if len(cc.hooks) == 0 {
|
||||
if err = cc.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
node, err = cc.sqlSave(ctx)
|
||||
} else {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
mutation, ok := m.(*CarMutation)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
if err = cc.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cc.mutation = mutation
|
||||
if node, err = cc.sqlSave(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mutation.id = &node.ID
|
||||
mutation.done = true
|
||||
return node, err
|
||||
})
|
||||
for i := len(cc.hooks) - 1; i >= 0; i-- {
|
||||
if cc.hooks[i] == nil {
|
||||
return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
||||
}
|
||||
mut = cc.hooks[i](mut)
|
||||
}
|
||||
v, err := mut.Mutate(ctx, cc.mutation)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nv, ok := v.(*Car)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected node type %T returned from CarMutation", v)
|
||||
}
|
||||
node = nv
|
||||
}
|
||||
return node, err
|
||||
return withHooks[*Car, CarMutation](ctx, cc.sqlSave, cc.mutation, cc.hooks)
|
||||
}
|
||||
|
||||
// SaveX calls Save and panics if Save returns an error.
|
||||
@@ -142,6 +100,9 @@ func (cc *CarCreate) check() error {
|
||||
}
|
||||
|
||||
func (cc *CarCreate) sqlSave(ctx context.Context) (*Car, error) {
|
||||
if err := cc.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_node, _spec := cc.createSpec()
|
||||
if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil {
|
||||
if sqlgraph.IsConstraintError(err) {
|
||||
@@ -151,6 +112,8 @@ func (cc *CarCreate) sqlSave(ctx context.Context) (*Car, error) {
|
||||
}
|
||||
id := _spec.ID.Value.(int64)
|
||||
_node.ID = int(id)
|
||||
cc.mutation.id = &_node.ID
|
||||
cc.mutation.done = true
|
||||
return _node, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@ package ent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
@@ -32,40 +31,7 @@ func (cd *CarDelete) Where(ps ...predicate.Car) *CarDelete {
|
||||
|
||||
// Exec executes the deletion query and returns how many vertices were deleted.
|
||||
func (cd *CarDelete) Exec(ctx context.Context) (int, error) {
|
||||
var (
|
||||
err error
|
||||
affected int
|
||||
)
|
||||
if len(cd.hooks) == 0 {
|
||||
affected, err = cd.sqlExec(ctx)
|
||||
} else {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
mutation, ok := m.(*CarMutation)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
cd.mutation = mutation
|
||||
affected, err = cd.sqlExec(ctx)
|
||||
mutation.done = true
|
||||
return affected, err
|
||||
})
|
||||
for i := len(cd.hooks) - 1; i >= 0; i-- {
|
||||
if cd.hooks[i] == nil {
|
||||
return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
||||
}
|
||||
mut = cd.hooks[i](mut)
|
||||
}
|
||||
n, err := mut.Mutate(ctx, cd.mutation)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
nv, ok := n.(int)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n)
|
||||
}
|
||||
affected = nv
|
||||
}
|
||||
return affected, err
|
||||
return withHooks[int, CarMutation](ctx, cd.sqlExec, cd.mutation, cd.hooks)
|
||||
}
|
||||
|
||||
// ExecX is like Exec, but panics if an error occurs.
|
||||
@@ -98,6 +64,7 @@ func (cd *CarDelete) sqlExec(ctx context.Context) (int, error) {
|
||||
if err != nil && sqlgraph.IsConstraintError(err) {
|
||||
err = &ConstraintError{msg: err.Error(), wrap: err}
|
||||
}
|
||||
cd.mutation.done = true
|
||||
return affected, err
|
||||
}
|
||||
|
||||
|
||||
@@ -77,34 +77,7 @@ func (cu *CarUpdate) ClearOwner() *CarUpdate {
|
||||
|
||||
// Save executes the query and returns the number of nodes affected by the update operation.
|
||||
func (cu *CarUpdate) Save(ctx context.Context) (int, error) {
|
||||
var (
|
||||
err error
|
||||
affected int
|
||||
)
|
||||
if len(cu.hooks) == 0 {
|
||||
affected, err = cu.sqlSave(ctx)
|
||||
} else {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
mutation, ok := m.(*CarMutation)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
cu.mutation = mutation
|
||||
affected, err = cu.sqlSave(ctx)
|
||||
mutation.done = true
|
||||
return affected, err
|
||||
})
|
||||
for i := len(cu.hooks) - 1; i >= 0; i-- {
|
||||
if cu.hooks[i] == nil {
|
||||
return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
||||
}
|
||||
mut = cu.hooks[i](mut)
|
||||
}
|
||||
if _, err := mut.Mutate(ctx, cu.mutation); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
return affected, err
|
||||
return withHooks[int, CarMutation](ctx, cu.sqlSave, cu.mutation, cu.hooks)
|
||||
}
|
||||
|
||||
// SaveX is like Save, but panics if an error occurs.
|
||||
@@ -196,6 +169,7 @@ func (cu *CarUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
cu.mutation.done = true
|
||||
return n, nil
|
||||
}
|
||||
|
||||
@@ -258,40 +232,7 @@ func (cuo *CarUpdateOne) Select(field string, fields ...string) *CarUpdateOne {
|
||||
|
||||
// Save executes the query and returns the updated Car entity.
|
||||
func (cuo *CarUpdateOne) Save(ctx context.Context) (*Car, error) {
|
||||
var (
|
||||
err error
|
||||
node *Car
|
||||
)
|
||||
if len(cuo.hooks) == 0 {
|
||||
node, err = cuo.sqlSave(ctx)
|
||||
} else {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
mutation, ok := m.(*CarMutation)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
cuo.mutation = mutation
|
||||
node, err = cuo.sqlSave(ctx)
|
||||
mutation.done = true
|
||||
return node, err
|
||||
})
|
||||
for i := len(cuo.hooks) - 1; i >= 0; i-- {
|
||||
if cuo.hooks[i] == nil {
|
||||
return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
||||
}
|
||||
mut = cuo.hooks[i](mut)
|
||||
}
|
||||
v, err := mut.Mutate(ctx, cuo.mutation)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nv, ok := v.(*Car)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected node type %T returned from CarMutation", v)
|
||||
}
|
||||
node = nv
|
||||
}
|
||||
return node, err
|
||||
return withHooks[*Car, CarMutation](ctx, cuo.sqlSave, cuo.mutation, cuo.hooks)
|
||||
}
|
||||
|
||||
// SaveX is like Save, but panics if an error occurs.
|
||||
@@ -403,5 +344,6 @@ func (cuo *CarUpdateOne) sqlSave(ctx context.Context) (_node *Car, err error) {
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
cuo.mutation.done = true
|
||||
return _node, nil
|
||||
}
|
||||
|
||||
@@ -477,6 +477,40 @@ func (s *selector) BoolX(ctx context.Context) bool {
|
||||
return v
|
||||
}
|
||||
|
||||
// withHooks invokes the builder operation with the given hooks, if any.
|
||||
func withHooks[V Value, M any, PM interface {
|
||||
*M
|
||||
Mutation
|
||||
}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) {
|
||||
if len(hooks) == 0 {
|
||||
return exec(ctx)
|
||||
}
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
mutationT, ok := m.(PM)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
// Set the mutation to the builder.
|
||||
*mutation = *mutationT
|
||||
return exec(ctx)
|
||||
})
|
||||
for i := len(hooks) - 1; i >= 0; i-- {
|
||||
if hooks[i] == nil {
|
||||
return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
||||
}
|
||||
mut = hooks[i](mut)
|
||||
}
|
||||
v, err := mut.Mutate(ctx, mutation)
|
||||
if err != nil {
|
||||
return value, err
|
||||
}
|
||||
nv, ok := v.(V)
|
||||
if !ok {
|
||||
return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation)
|
||||
}
|
||||
return nv, nil
|
||||
}
|
||||
|
||||
// newQueryContext returns a new context with the given QueryContext attached in case it does not exist.
|
||||
func newQueryContext(ctx context.Context, typ, op string) context.Context {
|
||||
if ent.QueryFromContext(ctx) == nil {
|
||||
|
||||
@@ -52,49 +52,7 @@ func (gc *GroupCreate) Mutation() *GroupMutation {
|
||||
|
||||
// Save creates the Group in the database.
|
||||
func (gc *GroupCreate) Save(ctx context.Context) (*Group, error) {
|
||||
var (
|
||||
err error
|
||||
node *Group
|
||||
)
|
||||
if len(gc.hooks) == 0 {
|
||||
if err = gc.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
node, err = gc.sqlSave(ctx)
|
||||
} else {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
mutation, ok := m.(*GroupMutation)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
if err = gc.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gc.mutation = mutation
|
||||
if node, err = gc.sqlSave(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mutation.id = &node.ID
|
||||
mutation.done = true
|
||||
return node, err
|
||||
})
|
||||
for i := len(gc.hooks) - 1; i >= 0; i-- {
|
||||
if gc.hooks[i] == nil {
|
||||
return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
||||
}
|
||||
mut = gc.hooks[i](mut)
|
||||
}
|
||||
v, err := mut.Mutate(ctx, gc.mutation)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nv, ok := v.(*Group)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v)
|
||||
}
|
||||
node = nv
|
||||
}
|
||||
return node, err
|
||||
return withHooks[*Group, GroupMutation](ctx, gc.sqlSave, gc.mutation, gc.hooks)
|
||||
}
|
||||
|
||||
// SaveX calls Save and panics if Save returns an error.
|
||||
@@ -133,6 +91,9 @@ func (gc *GroupCreate) check() error {
|
||||
}
|
||||
|
||||
func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) {
|
||||
if err := gc.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_node, _spec := gc.createSpec()
|
||||
if err := sqlgraph.CreateNode(ctx, gc.driver, _spec); err != nil {
|
||||
if sqlgraph.IsConstraintError(err) {
|
||||
@@ -142,6 +103,8 @@ func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) {
|
||||
}
|
||||
id := _spec.ID.Value.(int64)
|
||||
_node.ID = int(id)
|
||||
gc.mutation.id = &_node.ID
|
||||
gc.mutation.done = true
|
||||
return _node, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@ package ent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
@@ -32,40 +31,7 @@ func (gd *GroupDelete) Where(ps ...predicate.Group) *GroupDelete {
|
||||
|
||||
// Exec executes the deletion query and returns how many vertices were deleted.
|
||||
func (gd *GroupDelete) Exec(ctx context.Context) (int, error) {
|
||||
var (
|
||||
err error
|
||||
affected int
|
||||
)
|
||||
if len(gd.hooks) == 0 {
|
||||
affected, err = gd.sqlExec(ctx)
|
||||
} else {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
mutation, ok := m.(*GroupMutation)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
gd.mutation = mutation
|
||||
affected, err = gd.sqlExec(ctx)
|
||||
mutation.done = true
|
||||
return affected, err
|
||||
})
|
||||
for i := len(gd.hooks) - 1; i >= 0; i-- {
|
||||
if gd.hooks[i] == nil {
|
||||
return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
||||
}
|
||||
mut = gd.hooks[i](mut)
|
||||
}
|
||||
n, err := mut.Mutate(ctx, gd.mutation)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
nv, ok := n.(int)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n)
|
||||
}
|
||||
affected = nv
|
||||
}
|
||||
return affected, err
|
||||
return withHooks[int, GroupMutation](ctx, gd.sqlExec, gd.mutation, gd.hooks)
|
||||
}
|
||||
|
||||
// ExecX is like Exec, but panics if an error occurs.
|
||||
@@ -98,6 +64,7 @@ func (gd *GroupDelete) sqlExec(ctx context.Context) (int, error) {
|
||||
if err != nil && sqlgraph.IsConstraintError(err) {
|
||||
err = &ConstraintError{msg: err.Error(), wrap: err}
|
||||
}
|
||||
gd.mutation.done = true
|
||||
return affected, err
|
||||
}
|
||||
|
||||
|
||||
@@ -81,40 +81,7 @@ func (gu *GroupUpdate) RemoveUsers(u ...*User) *GroupUpdate {
|
||||
|
||||
// Save executes the query and returns the number of nodes affected by the update operation.
|
||||
func (gu *GroupUpdate) Save(ctx context.Context) (int, error) {
|
||||
var (
|
||||
err error
|
||||
affected int
|
||||
)
|
||||
if len(gu.hooks) == 0 {
|
||||
if err = gu.check(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
affected, err = gu.sqlSave(ctx)
|
||||
} else {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
mutation, ok := m.(*GroupMutation)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
if err = gu.check(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
gu.mutation = mutation
|
||||
affected, err = gu.sqlSave(ctx)
|
||||
mutation.done = true
|
||||
return affected, err
|
||||
})
|
||||
for i := len(gu.hooks) - 1; i >= 0; i-- {
|
||||
if gu.hooks[i] == nil {
|
||||
return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
||||
}
|
||||
mut = gu.hooks[i](mut)
|
||||
}
|
||||
if _, err := mut.Mutate(ctx, gu.mutation); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
return affected, err
|
||||
return withHooks[int, GroupMutation](ctx, gu.sqlSave, gu.mutation, gu.hooks)
|
||||
}
|
||||
|
||||
// SaveX is like Save, but panics if an error occurs.
|
||||
@@ -150,6 +117,9 @@ func (gu *GroupUpdate) check() error {
|
||||
}
|
||||
|
||||
func (gu *GroupUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
||||
if err := gu.check(); err != nil {
|
||||
return n, err
|
||||
}
|
||||
_spec := &sqlgraph.UpdateSpec{
|
||||
Node: &sqlgraph.NodeSpec{
|
||||
Table: group.Table,
|
||||
@@ -232,6 +202,7 @@ func (gu *GroupUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
gu.mutation.done = true
|
||||
return n, nil
|
||||
}
|
||||
|
||||
@@ -299,46 +270,7 @@ func (guo *GroupUpdateOne) Select(field string, fields ...string) *GroupUpdateOn
|
||||
|
||||
// Save executes the query and returns the updated Group entity.
|
||||
func (guo *GroupUpdateOne) Save(ctx context.Context) (*Group, error) {
|
||||
var (
|
||||
err error
|
||||
node *Group
|
||||
)
|
||||
if len(guo.hooks) == 0 {
|
||||
if err = guo.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
node, err = guo.sqlSave(ctx)
|
||||
} else {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
mutation, ok := m.(*GroupMutation)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
if err = guo.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
guo.mutation = mutation
|
||||
node, err = guo.sqlSave(ctx)
|
||||
mutation.done = true
|
||||
return node, err
|
||||
})
|
||||
for i := len(guo.hooks) - 1; i >= 0; i-- {
|
||||
if guo.hooks[i] == nil {
|
||||
return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
||||
}
|
||||
mut = guo.hooks[i](mut)
|
||||
}
|
||||
v, err := mut.Mutate(ctx, guo.mutation)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nv, ok := v.(*Group)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected node type %T returned from GroupMutation", v)
|
||||
}
|
||||
node = nv
|
||||
}
|
||||
return node, err
|
||||
return withHooks[*Group, GroupMutation](ctx, guo.sqlSave, guo.mutation, guo.hooks)
|
||||
}
|
||||
|
||||
// SaveX is like Save, but panics if an error occurs.
|
||||
@@ -374,6 +306,9 @@ func (guo *GroupUpdateOne) check() error {
|
||||
}
|
||||
|
||||
func (guo *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error) {
|
||||
if err := guo.check(); err != nil {
|
||||
return _node, err
|
||||
}
|
||||
_spec := &sqlgraph.UpdateSpec{
|
||||
Node: &sqlgraph.NodeSpec{
|
||||
Table: group.Table,
|
||||
@@ -476,5 +411,6 @@ func (guo *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
guo.mutation.done = true
|
||||
return _node, nil
|
||||
}
|
||||
|
||||
@@ -82,50 +82,8 @@ func (uc *UserCreate) Mutation() *UserMutation {
|
||||
|
||||
// Save creates the User in the database.
|
||||
func (uc *UserCreate) Save(ctx context.Context) (*User, error) {
|
||||
var (
|
||||
err error
|
||||
node *User
|
||||
)
|
||||
uc.defaults()
|
||||
if len(uc.hooks) == 0 {
|
||||
if err = uc.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
node, err = uc.sqlSave(ctx)
|
||||
} else {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
mutation, ok := m.(*UserMutation)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
if err = uc.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
uc.mutation = mutation
|
||||
if node, err = uc.sqlSave(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mutation.id = &node.ID
|
||||
mutation.done = true
|
||||
return node, err
|
||||
})
|
||||
for i := len(uc.hooks) - 1; i >= 0; i-- {
|
||||
if uc.hooks[i] == nil {
|
||||
return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
||||
}
|
||||
mut = uc.hooks[i](mut)
|
||||
}
|
||||
v, err := mut.Mutate(ctx, uc.mutation)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nv, ok := v.(*User)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v)
|
||||
}
|
||||
node = nv
|
||||
}
|
||||
return node, err
|
||||
return withHooks[*User, UserMutation](ctx, uc.sqlSave, uc.mutation, uc.hooks)
|
||||
}
|
||||
|
||||
// SaveX calls Save and panics if Save returns an error.
|
||||
@@ -175,6 +133,9 @@ func (uc *UserCreate) check() error {
|
||||
}
|
||||
|
||||
func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) {
|
||||
if err := uc.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_node, _spec := uc.createSpec()
|
||||
if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil {
|
||||
if sqlgraph.IsConstraintError(err) {
|
||||
@@ -184,6 +145,8 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) {
|
||||
}
|
||||
id := _spec.ID.Value.(int64)
|
||||
_node.ID = int(id)
|
||||
uc.mutation.id = &_node.ID
|
||||
uc.mutation.done = true
|
||||
return _node, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@ package ent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
@@ -32,40 +31,7 @@ func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete {
|
||||
|
||||
// Exec executes the deletion query and returns how many vertices were deleted.
|
||||
func (ud *UserDelete) Exec(ctx context.Context) (int, error) {
|
||||
var (
|
||||
err error
|
||||
affected int
|
||||
)
|
||||
if len(ud.hooks) == 0 {
|
||||
affected, err = ud.sqlExec(ctx)
|
||||
} else {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
mutation, ok := m.(*UserMutation)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
ud.mutation = mutation
|
||||
affected, err = ud.sqlExec(ctx)
|
||||
mutation.done = true
|
||||
return affected, err
|
||||
})
|
||||
for i := len(ud.hooks) - 1; i >= 0; i-- {
|
||||
if ud.hooks[i] == nil {
|
||||
return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
||||
}
|
||||
mut = ud.hooks[i](mut)
|
||||
}
|
||||
n, err := mut.Mutate(ctx, ud.mutation)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
nv, ok := n.(int)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n)
|
||||
}
|
||||
affected = nv
|
||||
}
|
||||
return affected, err
|
||||
return withHooks[int, UserMutation](ctx, ud.sqlExec, ud.mutation, ud.hooks)
|
||||
}
|
||||
|
||||
// ExecX is like Exec, but panics if an error occurs.
|
||||
@@ -98,6 +64,7 @@ func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) {
|
||||
if err != nil && sqlgraph.IsConstraintError(err) {
|
||||
err = &ConstraintError{msg: err.Error(), wrap: err}
|
||||
}
|
||||
ud.mutation.done = true
|
||||
return affected, err
|
||||
}
|
||||
|
||||
|
||||
@@ -139,40 +139,7 @@ func (uu *UserUpdate) RemoveGroups(g ...*Group) *UserUpdate {
|
||||
|
||||
// Save executes the query and returns the number of nodes affected by the update operation.
|
||||
func (uu *UserUpdate) Save(ctx context.Context) (int, error) {
|
||||
var (
|
||||
err error
|
||||
affected int
|
||||
)
|
||||
if len(uu.hooks) == 0 {
|
||||
if err = uu.check(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
affected, err = uu.sqlSave(ctx)
|
||||
} else {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
mutation, ok := m.(*UserMutation)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
if err = uu.check(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
uu.mutation = mutation
|
||||
affected, err = uu.sqlSave(ctx)
|
||||
mutation.done = true
|
||||
return affected, err
|
||||
})
|
||||
for i := len(uu.hooks) - 1; i >= 0; i-- {
|
||||
if uu.hooks[i] == nil {
|
||||
return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
||||
}
|
||||
mut = uu.hooks[i](mut)
|
||||
}
|
||||
if _, err := mut.Mutate(ctx, uu.mutation); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
return affected, err
|
||||
return withHooks[int, UserMutation](ctx, uu.sqlSave, uu.mutation, uu.hooks)
|
||||
}
|
||||
|
||||
// SaveX is like Save, but panics if an error occurs.
|
||||
@@ -208,6 +175,9 @@ func (uu *UserUpdate) check() error {
|
||||
}
|
||||
|
||||
func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
||||
if err := uu.check(); err != nil {
|
||||
return n, err
|
||||
}
|
||||
_spec := &sqlgraph.UpdateSpec{
|
||||
Node: &sqlgraph.NodeSpec{
|
||||
Table: user.Table,
|
||||
@@ -350,6 +320,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
uu.mutation.done = true
|
||||
return n, nil
|
||||
}
|
||||
|
||||
@@ -474,46 +445,7 @@ func (uuo *UserUpdateOne) Select(field string, fields ...string) *UserUpdateOne
|
||||
|
||||
// Save executes the query and returns the updated User entity.
|
||||
func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) {
|
||||
var (
|
||||
err error
|
||||
node *User
|
||||
)
|
||||
if len(uuo.hooks) == 0 {
|
||||
if err = uuo.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
node, err = uuo.sqlSave(ctx)
|
||||
} else {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
mutation, ok := m.(*UserMutation)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
if err = uuo.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
uuo.mutation = mutation
|
||||
node, err = uuo.sqlSave(ctx)
|
||||
mutation.done = true
|
||||
return node, err
|
||||
})
|
||||
for i := len(uuo.hooks) - 1; i >= 0; i-- {
|
||||
if uuo.hooks[i] == nil {
|
||||
return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
||||
}
|
||||
mut = uuo.hooks[i](mut)
|
||||
}
|
||||
v, err := mut.Mutate(ctx, uuo.mutation)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nv, ok := v.(*User)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected node type %T returned from UserMutation", v)
|
||||
}
|
||||
node = nv
|
||||
}
|
||||
return node, err
|
||||
return withHooks[*User, UserMutation](ctx, uuo.sqlSave, uuo.mutation, uuo.hooks)
|
||||
}
|
||||
|
||||
// SaveX is like Save, but panics if an error occurs.
|
||||
@@ -549,6 +481,9 @@ func (uuo *UserUpdateOne) check() error {
|
||||
}
|
||||
|
||||
func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) {
|
||||
if err := uuo.check(); err != nil {
|
||||
return _node, err
|
||||
}
|
||||
_spec := &sqlgraph.UpdateSpec{
|
||||
Node: &sqlgraph.NodeSpec{
|
||||
Table: user.Table,
|
||||
@@ -711,5 +646,6 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
uuo.mutation.done = true
|
||||
return _node, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user