tx.go 9.3 KB

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