tx.go 6.0 KB

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