tx.go 10 KB

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