tx.go 14 KB

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