car_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/car"
  7. "code.osinet.fr/fgm/go__ent_demo/ent/predicate"
  8. "entgo.io/ent/dialect/sql"
  9. "entgo.io/ent/dialect/sql/sqlgraph"
  10. "entgo.io/ent/schema/field"
  11. )
  12. // CarDelete is the builder for deleting a Car entity.
  13. type CarDelete struct {
  14. config
  15. hooks []Hook
  16. mutation *CarMutation
  17. }
  18. // Where appends a list predicates to the CarDelete builder.
  19. func (cd *CarDelete) Where(ps ...predicate.Car) *CarDelete {
  20. cd.mutation.Where(ps...)
  21. return cd
  22. }
  23. // Exec executes the deletion query and returns how many vertices were deleted.
  24. func (cd *CarDelete) Exec(ctx context.Context) (int, error) {
  25. var (
  26. err error
  27. affected int
  28. )
  29. if len(cd.hooks) == 0 {
  30. affected, err = cd.sqlExec(ctx)
  31. } else {
  32. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  33. mutation, ok := m.(*CarMutation)
  34. if !ok {
  35. return nil, fmt.Errorf("unexpected mutation type %T", m)
  36. }
  37. cd.mutation = mutation
  38. affected, err = cd.sqlExec(ctx)
  39. mutation.done = true
  40. return affected, err
  41. })
  42. for i := len(cd.hooks) - 1; i >= 0; i-- {
  43. if cd.hooks[i] == nil {
  44. return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
  45. }
  46. mut = cd.hooks[i](mut)
  47. }
  48. if _, err := mut.Mutate(ctx, cd.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 (cd *CarDelete) ExecX(ctx context.Context) int {
  56. n, err := cd.Exec(ctx)
  57. if err != nil {
  58. panic(err)
  59. }
  60. return n
  61. }
  62. func (cd *CarDelete) sqlExec(ctx context.Context) (int, error) {
  63. _spec := &sqlgraph.DeleteSpec{
  64. Node: &sqlgraph.NodeSpec{
  65. Table: car.Table,
  66. ID: &sqlgraph.FieldSpec{
  67. Type: field.TypeInt,
  68. Column: car.FieldID,
  69. },
  70. },
  71. }
  72. if ps := cd.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, cd.driver, _spec)
  80. }
  81. // CarDeleteOne is the builder for deleting a single Car entity.
  82. type CarDeleteOne struct {
  83. cd *CarDelete
  84. }
  85. // Exec executes the deletion query.
  86. func (cdo *CarDeleteOne) Exec(ctx context.Context) error {
  87. n, err := cdo.cd.Exec(ctx)
  88. switch {
  89. case err != nil:
  90. return err
  91. case n == 0:
  92. return &NotFoundError{car.Label}
  93. default:
  94. return nil
  95. }
  96. }
  97. // ExecX is like Exec, but panics if an error occurs.
  98. func (cdo *CarDeleteOne) ExecX(ctx context.Context) {
  99. cdo.cd.ExecX(ctx)
  100. }