tx.go 12 KB

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