tx.go 13 KB

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