tx.go 14 KB

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