tx.go 8.7 KB

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