tx.go 8.5 KB

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