car_delete.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "fmt"
  6. "code.osinet.fr/fgm/entdemo/ent/car"
  7. "code.osinet.fr/fgm/entdemo/ent/predicate"
  8. "github.com/facebookincubator/ent/dialect/sql"
  9. "github.com/facebookincubator/ent/dialect/sql/sqlgraph"
  10. "github.com/facebookincubator/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. predicates []predicate.Car
  18. }
  19. // Where adds a new predicate to the delete builder.
  20. func (cd *CarDelete) Where(ps ...predicate.Car) *CarDelete {
  21. cd.predicates = append(cd.predicates, ps...)
  22. return cd
  23. }
  24. // Exec executes the deletion query and returns how many vertices were deleted.
  25. func (cd *CarDelete) Exec(ctx context.Context) (int, error) {
  26. var (
  27. err error
  28. affected int
  29. )
  30. if len(cd.hooks) == 0 {
  31. affected, err = cd.sqlExec(ctx)
  32. } else {
  33. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  34. mutation, ok := m.(*CarMutation)
  35. if !ok {
  36. return nil, fmt.Errorf("unexpected mutation type %T", m)
  37. }
  38. cd.mutation = mutation
  39. affected, err = cd.sqlExec(ctx)
  40. return affected, err
  41. })
  42. for i := len(cd.hooks) - 1; i >= 0; i-- {
  43. mut = cd.hooks[i](mut)
  44. }
  45. if _, err := mut.Mutate(ctx, cd.mutation); err != nil {
  46. return 0, err
  47. }
  48. }
  49. return affected, err
  50. }
  51. // ExecX is like Exec, but panics if an error occurs.
  52. func (cd *CarDelete) ExecX(ctx context.Context) int {
  53. n, err := cd.Exec(ctx)
  54. if err != nil {
  55. panic(err)
  56. }
  57. return n
  58. }
  59. func (cd *CarDelete) sqlExec(ctx context.Context) (int, error) {
  60. _spec := &sqlgraph.DeleteSpec{
  61. Node: &sqlgraph.NodeSpec{
  62. Table: car.Table,
  63. ID: &sqlgraph.FieldSpec{
  64. Type: field.TypeInt,
  65. Column: car.FieldID,
  66. },
  67. },
  68. }
  69. if ps := cd.predicates; len(ps) > 0 {
  70. _spec.Predicate = func(selector *sql.Selector) {
  71. for i := range ps {
  72. ps[i](selector)
  73. }
  74. }
  75. }
  76. return sqlgraph.DeleteNodes(ctx, cd.driver, _spec)
  77. }
  78. // CarDeleteOne is the builder for deleting a single Car entity.
  79. type CarDeleteOne struct {
  80. cd *CarDelete
  81. }
  82. // Exec executes the deletion query.
  83. func (cdo *CarDeleteOne) Exec(ctx context.Context) error {
  84. n, err := cdo.cd.Exec(ctx)
  85. switch {
  86. case err != nil:
  87. return err
  88. case n == 0:
  89. return &NotFoundError{car.Label}
  90. default:
  91. return nil
  92. }
  93. }
  94. // ExecX is like Exec, but panics if an error occurs.
  95. func (cdo *CarDeleteOne) ExecX(ctx context.Context) {
  96. cdo.cd.ExecX(ctx)
  97. }