tx.go 8.6 KB

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