tx.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "sync"
  6. "entgo.io/ent/dialect"
  7. )
  8. // Tx is a transactional client that is created by calling Client.Tx().
  9. type Tx struct {
  10. config
  11. // Car is the client for interacting with the Car builders.
  12. Car *CarClient
  13. // Group is the client for interacting with the Group builders.
  14. Group *GroupClient
  15. // User is the client for interacting with the User builders.
  16. User *UserClient
  17. // lazily loaded.
  18. client *Client
  19. clientOnce sync.Once
  20. // completion callbacks.
  21. mu sync.Mutex
  22. onCommit []CommitHook
  23. onRollback []RollbackHook
  24. // ctx lives for the life of the transaction. It is
  25. // the same context used by the underlying connection.
  26. ctx context.Context
  27. }
  28. type (
  29. // Committer is the interface that wraps the Commit method.
  30. Committer interface {
  31. Commit(context.Context, *Tx) error
  32. }
  33. // The CommitFunc type is an adapter to allow the use of ordinary
  34. // function as a Committer. If f is a function with the appropriate
  35. // signature, CommitFunc(f) is a Committer that calls f.
  36. CommitFunc func(context.Context, *Tx) error
  37. // CommitHook defines the "commit middleware". A function that gets a Committer
  38. // and returns a Committer. For example:
  39. //
  40. // hook := func(next ent.Committer) ent.Committer {
  41. // return ent.CommitFunc(func(ctx context.Context, tx *ent.Tx) error {
  42. // // Do some stuff before.
  43. // if err := next.Commit(ctx, tx); err != nil {
  44. // return err
  45. // }
  46. // // Do some stuff after.
  47. // return nil
  48. // })
  49. // }
  50. //
  51. CommitHook func(Committer) Committer
  52. )
  53. // Commit calls f(ctx, m).
  54. func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error {
  55. return f(ctx, tx)
  56. }
  57. // Commit commits the transaction.
  58. func (tx *Tx) Commit() error {
  59. txDriver := tx.config.driver.(*txDriver)
  60. var fn Committer = CommitFunc(func(context.Context, *Tx) error {
  61. return txDriver.tx.Commit()
  62. })
  63. tx.mu.Lock()
  64. hooks := append([]CommitHook(nil), tx.onCommit...)
  65. tx.mu.Unlock()
  66. for i := len(hooks) - 1; i >= 0; i-- {
  67. fn = hooks[i](fn)
  68. }
  69. return fn.Commit(tx.ctx, tx)
  70. }
  71. // OnCommit adds a hook to call on commit.
  72. func (tx *Tx) OnCommit(f CommitHook) {
  73. tx.mu.Lock()
  74. defer tx.mu.Unlock()
  75. tx.onCommit = append(tx.onCommit, f)
  76. }
  77. type (
  78. // Rollbacker is the interface that wraps the Rollback method.
  79. Rollbacker interface {
  80. Rollback(context.Context, *Tx) error
  81. }
  82. // The RollbackFunc type is an adapter to allow the use of ordinary
  83. // function as a Rollbacker. If f is a function with the appropriate
  84. // signature, RollbackFunc(f) is a Rollbacker that calls f.
  85. RollbackFunc func(context.Context, *Tx) error
  86. // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker
  87. // and returns a Rollbacker. For example:
  88. //
  89. // hook := func(next ent.Rollbacker) ent.Rollbacker {
  90. // return ent.RollbackFunc(func(ctx context.Context, tx *ent.Tx) error {
  91. // // Do some stuff before.
  92. // if err := next.Rollback(ctx, tx); err != nil {
  93. // return err
  94. // }
  95. // // Do some stuff after.
  96. // return nil
  97. // })
  98. // }
  99. //
  100. RollbackHook func(Rollbacker) Rollbacker
  101. )
  102. // Rollback calls f(ctx, m).
  103. func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error {
  104. return f(ctx, tx)
  105. }
  106. // Rollback rollbacks the transaction.
  107. func (tx *Tx) Rollback() error {
  108. txDriver := tx.config.driver.(*txDriver)
  109. var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error {
  110. return txDriver.tx.Rollback()
  111. })
  112. tx.mu.Lock()
  113. hooks := append([]RollbackHook(nil), tx.onRollback...)
  114. tx.mu.Unlock()
  115. for i := len(hooks) - 1; i >= 0; i-- {
  116. fn = hooks[i](fn)
  117. }
  118. return fn.Rollback(tx.ctx, tx)
  119. }
  120. // OnRollback adds a hook to call on rollback.
  121. func (tx *Tx) OnRollback(f RollbackHook) {
  122. tx.mu.Lock()
  123. defer tx.mu.Unlock()
  124. tx.onRollback = append(tx.onRollback, f)
  125. }
  126. // Client returns a Client that binds to current transaction.
  127. func (tx *Tx) Client() *Client {
  128. tx.clientOnce.Do(func() {
  129. tx.client = &Client{config: tx.config}
  130. tx.client.init()
  131. })
  132. return tx.client
  133. }
  134. func (tx *Tx) init() {
  135. tx.Car = NewCarClient(tx.config)
  136. tx.Group = NewGroupClient(tx.config)
  137. tx.User = NewUserClient(tx.config)
  138. }
  139. // txDriver wraps the given dialect.Tx with a nop dialect.Driver implementation.
  140. // The idea is to support transactions without adding any extra code to the builders.
  141. // When a builder calls to driver.Tx(), it gets the same dialect.Tx instance.
  142. // Commit and Rollback are nop for the internal builders and the user must call one
  143. // of them in order to commit or rollback the transaction.
  144. //
  145. // If a closed transaction is embedded in one of the generated entities, and the entity
  146. // applies a query, for example: Car.QueryXXX(), the query will be executed
  147. // through the driver which created this transaction.
  148. //
  149. // Note that txDriver is not goroutine safe.
  150. type txDriver struct {
  151. // the driver we started the transaction from.
  152. drv dialect.Driver
  153. // tx is the underlying transaction.
  154. tx dialect.Tx
  155. }
  156. // newTx creates a new transactional driver.
  157. func newTx(ctx context.Context, drv dialect.Driver) (*txDriver, error) {
  158. tx, err := drv.Tx(ctx)
  159. if err != nil {
  160. return nil, err
  161. }
  162. return &txDriver{tx: tx, drv: drv}, nil
  163. }
  164. // Tx returns the transaction wrapper (txDriver) to avoid Commit or Rollback calls
  165. // from the internal builders. Should be called only by the internal builders.
  166. func (tx *txDriver) Tx(context.Context) (dialect.Tx, error) { return tx, nil }
  167. // Dialect returns the dialect of the driver we started the transaction from.
  168. func (tx *txDriver) Dialect() string { return tx.drv.Dialect() }
  169. // Close is a nop close.
  170. func (*txDriver) Close() error { return nil }
  171. // Commit is a nop commit for the internal builders.
  172. // User must call `Tx.Commit` in order to commit the transaction.
  173. func (*txDriver) Commit() error { return nil }
  174. // Rollback is a nop rollback for the internal builders.
  175. // User must call `Tx.Rollback` in order to rollback the transaction.
  176. func (*txDriver) Rollback() error { return nil }
  177. // Exec calls tx.Exec.
  178. func (tx *txDriver) Exec(ctx context.Context, query string, args, v interface{}) error {
  179. return tx.tx.Exec(ctx, query, args, v)
  180. }
  181. // Query calls tx.Query.
  182. func (tx *txDriver) Query(ctx context.Context, query string, args, v interface{}) error {
  183. return tx.tx.Query(ctx, query, args, v)
  184. }
  185. var _ dialect.Driver = (*txDriver)(nil)