tx.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // Code generated by ent, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. stdsql "database/sql"
  6. "fmt"
  7. "sync"
  8. "entgo.io/ent/dialect"
  9. )
  10. // Tx is a transactional client that is created by calling Client.Tx().
  11. type Tx struct {
  12. config
  13. // Contact is the client for interacting with the Contact builders.
  14. Contact *ContactClient
  15. // Label is the client for interacting with the Label builders.
  16. Label *LabelClient
  17. // LabelRelationship is the client for interacting with the LabelRelationship builders.
  18. LabelRelationship *LabelRelationshipClient
  19. // Message is the client for interacting with the Message builders.
  20. Message *MessageClient
  21. // MessageRecords is the client for interacting with the MessageRecords builders.
  22. MessageRecords *MessageRecordsClient
  23. // Server is the client for interacting with the Server builders.
  24. Server *ServerClient
  25. // SopNode is the client for interacting with the SopNode builders.
  26. SopNode *SopNodeClient
  27. // SopStage is the client for interacting with the SopStage builders.
  28. SopStage *SopStageClient
  29. // SopTask is the client for interacting with the SopTask builders.
  30. SopTask *SopTaskClient
  31. // Wx is the client for interacting with the Wx builders.
  32. Wx *WxClient
  33. // lazily loaded.
  34. client *Client
  35. clientOnce sync.Once
  36. // ctx lives for the life of the transaction. It is
  37. // the same context used by the underlying connection.
  38. ctx context.Context
  39. }
  40. type (
  41. // Committer is the interface that wraps the Commit method.
  42. Committer interface {
  43. Commit(context.Context, *Tx) error
  44. }
  45. // The CommitFunc type is an adapter to allow the use of ordinary
  46. // function as a Committer. If f is a function with the appropriate
  47. // signature, CommitFunc(f) is a Committer that calls f.
  48. CommitFunc func(context.Context, *Tx) error
  49. // CommitHook defines the "commit middleware". A function that gets a Committer
  50. // and returns a Committer. For example:
  51. //
  52. // hook := func(next ent.Committer) ent.Committer {
  53. // return ent.CommitFunc(func(ctx context.Context, tx *ent.Tx) error {
  54. // // Do some stuff before.
  55. // if err := next.Commit(ctx, tx); err != nil {
  56. // return err
  57. // }
  58. // // Do some stuff after.
  59. // return nil
  60. // })
  61. // }
  62. //
  63. CommitHook func(Committer) Committer
  64. )
  65. // Commit calls f(ctx, m).
  66. func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error {
  67. return f(ctx, tx)
  68. }
  69. // Commit commits the transaction.
  70. func (tx *Tx) Commit() error {
  71. txDriver := tx.config.driver.(*txDriver)
  72. var fn Committer = CommitFunc(func(context.Context, *Tx) error {
  73. return txDriver.tx.Commit()
  74. })
  75. txDriver.mu.Lock()
  76. hooks := append([]CommitHook(nil), txDriver.onCommit...)
  77. txDriver.mu.Unlock()
  78. for i := len(hooks) - 1; i >= 0; i-- {
  79. fn = hooks[i](fn)
  80. }
  81. return fn.Commit(tx.ctx, tx)
  82. }
  83. // OnCommit adds a hook to call on commit.
  84. func (tx *Tx) OnCommit(f CommitHook) {
  85. txDriver := tx.config.driver.(*txDriver)
  86. txDriver.mu.Lock()
  87. txDriver.onCommit = append(txDriver.onCommit, f)
  88. txDriver.mu.Unlock()
  89. }
  90. type (
  91. // Rollbacker is the interface that wraps the Rollback method.
  92. Rollbacker interface {
  93. Rollback(context.Context, *Tx) error
  94. }
  95. // The RollbackFunc type is an adapter to allow the use of ordinary
  96. // function as a Rollbacker. If f is a function with the appropriate
  97. // signature, RollbackFunc(f) is a Rollbacker that calls f.
  98. RollbackFunc func(context.Context, *Tx) error
  99. // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker
  100. // and returns a Rollbacker. For example:
  101. //
  102. // hook := func(next ent.Rollbacker) ent.Rollbacker {
  103. // return ent.RollbackFunc(func(ctx context.Context, tx *ent.Tx) error {
  104. // // Do some stuff before.
  105. // if err := next.Rollback(ctx, tx); err != nil {
  106. // return err
  107. // }
  108. // // Do some stuff after.
  109. // return nil
  110. // })
  111. // }
  112. //
  113. RollbackHook func(Rollbacker) Rollbacker
  114. )
  115. // Rollback calls f(ctx, m).
  116. func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error {
  117. return f(ctx, tx)
  118. }
  119. // Rollback rollbacks the transaction.
  120. func (tx *Tx) Rollback() error {
  121. txDriver := tx.config.driver.(*txDriver)
  122. var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error {
  123. return txDriver.tx.Rollback()
  124. })
  125. txDriver.mu.Lock()
  126. hooks := append([]RollbackHook(nil), txDriver.onRollback...)
  127. txDriver.mu.Unlock()
  128. for i := len(hooks) - 1; i >= 0; i-- {
  129. fn = hooks[i](fn)
  130. }
  131. return fn.Rollback(tx.ctx, tx)
  132. }
  133. // OnRollback adds a hook to call on rollback.
  134. func (tx *Tx) OnRollback(f RollbackHook) {
  135. txDriver := tx.config.driver.(*txDriver)
  136. txDriver.mu.Lock()
  137. txDriver.onRollback = append(txDriver.onRollback, f)
  138. txDriver.mu.Unlock()
  139. }
  140. // Client returns a Client that binds to current transaction.
  141. func (tx *Tx) Client() *Client {
  142. tx.clientOnce.Do(func() {
  143. tx.client = &Client{config: tx.config}
  144. tx.client.init()
  145. })
  146. return tx.client
  147. }
  148. func (tx *Tx) init() {
  149. tx.Contact = NewContactClient(tx.config)
  150. tx.Label = NewLabelClient(tx.config)
  151. tx.LabelRelationship = NewLabelRelationshipClient(tx.config)
  152. tx.Message = NewMessageClient(tx.config)
  153. tx.MessageRecords = NewMessageRecordsClient(tx.config)
  154. tx.Server = NewServerClient(tx.config)
  155. tx.SopNode = NewSopNodeClient(tx.config)
  156. tx.SopStage = NewSopStageClient(tx.config)
  157. tx.SopTask = NewSopTaskClient(tx.config)
  158. tx.Wx = NewWxClient(tx.config)
  159. }
  160. // txDriver wraps the given dialect.Tx with a nop dialect.Driver implementation.
  161. // The idea is to support transactions without adding any extra code to the builders.
  162. // When a builder calls to driver.Tx(), it gets the same dialect.Tx instance.
  163. // Commit and Rollback are nop for the internal builders and the user must call one
  164. // of them in order to commit or rollback the transaction.
  165. //
  166. // If a closed transaction is embedded in one of the generated entities, and the entity
  167. // applies a query, for example: Contact.QueryXXX(), the query will be executed
  168. // through the driver which created this transaction.
  169. //
  170. // Note that txDriver is not goroutine safe.
  171. type txDriver struct {
  172. // the driver we started the transaction from.
  173. drv dialect.Driver
  174. // tx is the underlying transaction.
  175. tx dialect.Tx
  176. // completion hooks.
  177. mu sync.Mutex
  178. onCommit []CommitHook
  179. onRollback []RollbackHook
  180. }
  181. // newTx creates a new transactional driver.
  182. func newTx(ctx context.Context, drv dialect.Driver) (*txDriver, error) {
  183. tx, err := drv.Tx(ctx)
  184. if err != nil {
  185. return nil, err
  186. }
  187. return &txDriver{tx: tx, drv: drv}, nil
  188. }
  189. // Tx returns the transaction wrapper (txDriver) to avoid Commit or Rollback calls
  190. // from the internal builders. Should be called only by the internal builders.
  191. func (tx *txDriver) Tx(context.Context) (dialect.Tx, error) { return tx, nil }
  192. // Dialect returns the dialect of the driver we started the transaction from.
  193. func (tx *txDriver) Dialect() string { return tx.drv.Dialect() }
  194. // Close is a nop close.
  195. func (*txDriver) Close() error { return nil }
  196. // Commit is a nop commit for the internal builders.
  197. // User must call `Tx.Commit` in order to commit the transaction.
  198. func (*txDriver) Commit() error { return nil }
  199. // Rollback is a nop rollback for the internal builders.
  200. // User must call `Tx.Rollback` in order to rollback the transaction.
  201. func (*txDriver) Rollback() error { return nil }
  202. // Exec calls tx.Exec.
  203. func (tx *txDriver) Exec(ctx context.Context, query string, args, v any) error {
  204. return tx.tx.Exec(ctx, query, args, v)
  205. }
  206. // Query calls tx.Query.
  207. func (tx *txDriver) Query(ctx context.Context, query string, args, v any) error {
  208. return tx.tx.Query(ctx, query, args, v)
  209. }
  210. var _ dialect.Driver = (*txDriver)(nil)
  211. // ExecContext allows calling the underlying ExecContext method of the transaction if it is supported by it.
  212. // See, database/sql#Tx.ExecContext for more information.
  213. func (tx *txDriver) ExecContext(ctx context.Context, query string, args ...any) (stdsql.Result, error) {
  214. ex, ok := tx.tx.(interface {
  215. ExecContext(context.Context, string, ...any) (stdsql.Result, error)
  216. })
  217. if !ok {
  218. return nil, fmt.Errorf("Tx.ExecContext is not supported")
  219. }
  220. return ex.ExecContext(ctx, query, args...)
  221. }
  222. // QueryContext allows calling the underlying QueryContext method of the transaction if it is supported by it.
  223. // See, database/sql#Tx.QueryContext for more information.
  224. func (tx *txDriver) QueryContext(ctx context.Context, query string, args ...any) (*stdsql.Rows, error) {
  225. q, ok := tx.tx.(interface {
  226. QueryContext(context.Context, string, ...any) (*stdsql.Rows, error)
  227. })
  228. if !ok {
  229. return nil, fmt.Errorf("Tx.QueryContext is not supported")
  230. }
  231. return q.QueryContext(ctx, query, args...)
  232. }