user_delete.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "fmt"
  6. "code.osinet.fr/fgm/go__ent_demo/ent/predicate"
  7. "code.osinet.fr/fgm/go__ent_demo/ent/user"
  8. "entgo.io/ent/dialect/sql"
  9. "entgo.io/ent/dialect/sql/sqlgraph"
  10. "entgo.io/ent/schema/field"
  11. )
  12. // UserDelete is the builder for deleting a User entity.
  13. type UserDelete struct {
  14. config
  15. hooks []Hook
  16. mutation *UserMutation
  17. }
  18. // Where appends a list predicates to the UserDelete builder.
  19. func (ud *UserDelete) Where(ps ...predicate.User) *UserDelete {
  20. ud.mutation.Where(ps...)
  21. return ud
  22. }
  23. // Exec executes the deletion query and returns how many vertices were deleted.
  24. func (ud *UserDelete) Exec(ctx context.Context) (int, error) {
  25. var (
  26. err error
  27. affected int
  28. )
  29. if len(ud.hooks) == 0 {
  30. affected, err = ud.sqlExec(ctx)
  31. } else {
  32. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  33. mutation, ok := m.(*UserMutation)
  34. if !ok {
  35. return nil, fmt.Errorf("unexpected mutation type %T", m)
  36. }
  37. ud.mutation = mutation
  38. affected, err = ud.sqlExec(ctx)
  39. mutation.done = true
  40. return affected, err
  41. })
  42. for i := len(ud.hooks) - 1; i >= 0; i-- {
  43. if ud.hooks[i] == nil {
  44. return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
  45. }
  46. mut = ud.hooks[i](mut)
  47. }
  48. if _, err := mut.Mutate(ctx, ud.mutation); err != nil {
  49. return 0, err
  50. }
  51. }
  52. return affected, err
  53. }
  54. // ExecX is like Exec, but panics if an error occurs.
  55. func (ud *UserDelete) ExecX(ctx context.Context) int {
  56. n, err := ud.Exec(ctx)
  57. if err != nil {
  58. panic(err)
  59. }
  60. return n
  61. }
  62. func (ud *UserDelete) sqlExec(ctx context.Context) (int, error) {
  63. _spec := &sqlgraph.DeleteSpec{
  64. Node: &sqlgraph.NodeSpec{
  65. Table: user.Table,
  66. ID: &sqlgraph.FieldSpec{
  67. Type: field.TypeInt,
  68. Column: user.FieldID,
  69. },
  70. },
  71. }
  72. if ps := ud.mutation.predicates; len(ps) > 0 {
  73. _spec.Predicate = func(selector *sql.Selector) {
  74. for i := range ps {
  75. ps[i](selector)
  76. }
  77. }
  78. }
  79. return sqlgraph.DeleteNodes(ctx, ud.driver, _spec)
  80. }
  81. // UserDeleteOne is the builder for deleting a single User entity.
  82. type UserDeleteOne struct {
  83. ud *UserDelete
  84. }
  85. // Exec executes the deletion query.
  86. func (udo *UserDeleteOne) Exec(ctx context.Context) error {
  87. n, err := udo.ud.Exec(ctx)
  88. switch {
  89. case err != nil:
  90. return err
  91. case n == 0:
  92. return &NotFoundError{user.Label}
  93. default:
  94. return nil
  95. }
  96. }
  97. // ExecX is like Exec, but panics if an error occurs.
  98. func (udo *UserDeleteOne) ExecX(ctx context.Context) {
  99. udo.ud.ExecX(ctx)
  100. }