tx.go 11 KB

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