tx.go 11 KB

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