tx.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. // APIKey is the client for interacting with the APIKey builders.
  14. APIKey *APIKeyClient
  15. // Account is the client for interacting with the Account builders.
  16. Account *AccountClient
  17. // AccountGroup is the client for interacting with the AccountGroup builders.
  18. AccountGroup *AccountGroupClient
  19. // Announcement is the client for interacting with the Announcement builders.
  20. Announcement *AnnouncementClient
  21. // AnnouncementRead is the client for interacting with the AnnouncementRead builders.
  22. AnnouncementRead *AnnouncementReadClient
  23. // AuthIdentity is the client for interacting with the AuthIdentity builders.
  24. AuthIdentity *AuthIdentityClient
  25. // AuthIdentityChannel is the client for interacting with the AuthIdentityChannel builders.
  26. AuthIdentityChannel *AuthIdentityChannelClient
  27. // BatchImageEvent is the client for interacting with the BatchImageEvent builders.
  28. BatchImageEvent *BatchImageEventClient
  29. // BatchImageItem is the client for interacting with the BatchImageItem builders.
  30. BatchImageItem *BatchImageItemClient
  31. // BatchImageJob is the client for interacting with the BatchImageJob builders.
  32. BatchImageJob *BatchImageJobClient
  33. // ChannelMonitor is the client for interacting with the ChannelMonitor builders.
  34. ChannelMonitor *ChannelMonitorClient
  35. // ChannelMonitorDailyRollup is the client for interacting with the ChannelMonitorDailyRollup builders.
  36. ChannelMonitorDailyRollup *ChannelMonitorDailyRollupClient
  37. // ChannelMonitorHistory is the client for interacting with the ChannelMonitorHistory builders.
  38. ChannelMonitorHistory *ChannelMonitorHistoryClient
  39. // ChannelMonitorRequestTemplate is the client for interacting with the ChannelMonitorRequestTemplate builders.
  40. ChannelMonitorRequestTemplate *ChannelMonitorRequestTemplateClient
  41. // CompositeModelRoute is the client for interacting with the CompositeModelRoute builders.
  42. CompositeModelRoute *CompositeModelRouteClient
  43. // ErrorPassthroughRule is the client for interacting with the ErrorPassthroughRule builders.
  44. ErrorPassthroughRule *ErrorPassthroughRuleClient
  45. // Group is the client for interacting with the Group builders.
  46. Group *GroupClient
  47. // IdempotencyRecord is the client for interacting with the IdempotencyRecord builders.
  48. IdempotencyRecord *IdempotencyRecordClient
  49. // IdentityAdoptionDecision is the client for interacting with the IdentityAdoptionDecision builders.
  50. IdentityAdoptionDecision *IdentityAdoptionDecisionClient
  51. // PaymentAuditLog is the client for interacting with the PaymentAuditLog builders.
  52. PaymentAuditLog *PaymentAuditLogClient
  53. // PaymentOrder is the client for interacting with the PaymentOrder builders.
  54. PaymentOrder *PaymentOrderClient
  55. // PaymentProviderInstance is the client for interacting with the PaymentProviderInstance builders.
  56. PaymentProviderInstance *PaymentProviderInstanceClient
  57. // PendingAuthSession is the client for interacting with the PendingAuthSession builders.
  58. PendingAuthSession *PendingAuthSessionClient
  59. // PromoCode is the client for interacting with the PromoCode builders.
  60. PromoCode *PromoCodeClient
  61. // PromoCodeUsage is the client for interacting with the PromoCodeUsage builders.
  62. PromoCodeUsage *PromoCodeUsageClient
  63. // Proxy is the client for interacting with the Proxy builders.
  64. Proxy *ProxyClient
  65. // RedeemCode is the client for interacting with the RedeemCode builders.
  66. RedeemCode *RedeemCodeClient
  67. // SecurityPolicyKeyword is the client for interacting with the SecurityPolicyKeyword builders.
  68. SecurityPolicyKeyword *SecurityPolicyKeywordClient
  69. // SecuritySecret is the client for interacting with the SecuritySecret builders.
  70. SecuritySecret *SecuritySecretClient
  71. // Setting is the client for interacting with the Setting builders.
  72. Setting *SettingClient
  73. // SubscriptionPlan is the client for interacting with the SubscriptionPlan builders.
  74. SubscriptionPlan *SubscriptionPlanClient
  75. // TLSFingerprintProfile is the client for interacting with the TLSFingerprintProfile builders.
  76. TLSFingerprintProfile *TLSFingerprintProfileClient
  77. // UsageCleanupTask is the client for interacting with the UsageCleanupTask builders.
  78. UsageCleanupTask *UsageCleanupTaskClient
  79. // UsageLog is the client for interacting with the UsageLog builders.
  80. UsageLog *UsageLogClient
  81. // User is the client for interacting with the User builders.
  82. User *UserClient
  83. // UserAllowedGroup is the client for interacting with the UserAllowedGroup builders.
  84. UserAllowedGroup *UserAllowedGroupClient
  85. // UserAttributeDefinition is the client for interacting with the UserAttributeDefinition builders.
  86. UserAttributeDefinition *UserAttributeDefinitionClient
  87. // UserAttributeValue is the client for interacting with the UserAttributeValue builders.
  88. UserAttributeValue *UserAttributeValueClient
  89. // UserPlatformQuota is the client for interacting with the UserPlatformQuota builders.
  90. UserPlatformQuota *UserPlatformQuotaClient
  91. // UserSubscription is the client for interacting with the UserSubscription builders.
  92. UserSubscription *UserSubscriptionClient
  93. // lazily loaded.
  94. client *Client
  95. clientOnce sync.Once
  96. // ctx lives for the life of the transaction. It is
  97. // the same context used by the underlying connection.
  98. ctx context.Context
  99. }
  100. type (
  101. // Committer is the interface that wraps the Commit method.
  102. Committer interface {
  103. Commit(context.Context, *Tx) error
  104. }
  105. // The CommitFunc type is an adapter to allow the use of ordinary
  106. // function as a Committer. If f is a function with the appropriate
  107. // signature, CommitFunc(f) is a Committer that calls f.
  108. CommitFunc func(context.Context, *Tx) error
  109. // CommitHook defines the "commit middleware". A function that gets a Committer
  110. // and returns a Committer. For example:
  111. //
  112. // hook := func(next ent.Committer) ent.Committer {
  113. // return ent.CommitFunc(func(ctx context.Context, tx *ent.Tx) error {
  114. // // Do some stuff before.
  115. // if err := next.Commit(ctx, tx); err != nil {
  116. // return err
  117. // }
  118. // // Do some stuff after.
  119. // return nil
  120. // })
  121. // }
  122. //
  123. CommitHook func(Committer) Committer
  124. )
  125. // Commit calls f(ctx, m).
  126. func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error {
  127. return f(ctx, tx)
  128. }
  129. // Commit commits the transaction.
  130. func (tx *Tx) Commit() error {
  131. txDriver := tx.config.driver.(*txDriver)
  132. var fn Committer = CommitFunc(func(context.Context, *Tx) error {
  133. return txDriver.tx.Commit()
  134. })
  135. txDriver.mu.Lock()
  136. hooks := append([]CommitHook(nil), txDriver.onCommit...)
  137. txDriver.mu.Unlock()
  138. for i := len(hooks) - 1; i >= 0; i-- {
  139. fn = hooks[i](fn)
  140. }
  141. return fn.Commit(tx.ctx, tx)
  142. }
  143. // OnCommit adds a hook to call on commit.
  144. func (tx *Tx) OnCommit(f CommitHook) {
  145. txDriver := tx.config.driver.(*txDriver)
  146. txDriver.mu.Lock()
  147. txDriver.onCommit = append(txDriver.onCommit, f)
  148. txDriver.mu.Unlock()
  149. }
  150. type (
  151. // Rollbacker is the interface that wraps the Rollback method.
  152. Rollbacker interface {
  153. Rollback(context.Context, *Tx) error
  154. }
  155. // The RollbackFunc type is an adapter to allow the use of ordinary
  156. // function as a Rollbacker. If f is a function with the appropriate
  157. // signature, RollbackFunc(f) is a Rollbacker that calls f.
  158. RollbackFunc func(context.Context, *Tx) error
  159. // RollbackHook defines the "rollback middleware". A function that gets a Rollbacker
  160. // and returns a Rollbacker. For example:
  161. //
  162. // hook := func(next ent.Rollbacker) ent.Rollbacker {
  163. // return ent.RollbackFunc(func(ctx context.Context, tx *ent.Tx) error {
  164. // // Do some stuff before.
  165. // if err := next.Rollback(ctx, tx); err != nil {
  166. // return err
  167. // }
  168. // // Do some stuff after.
  169. // return nil
  170. // })
  171. // }
  172. //
  173. RollbackHook func(Rollbacker) Rollbacker
  174. )
  175. // Rollback calls f(ctx, m).
  176. func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error {
  177. return f(ctx, tx)
  178. }
  179. // Rollback rollbacks the transaction.
  180. func (tx *Tx) Rollback() error {
  181. txDriver := tx.config.driver.(*txDriver)
  182. var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error {
  183. return txDriver.tx.Rollback()
  184. })
  185. txDriver.mu.Lock()
  186. hooks := append([]RollbackHook(nil), txDriver.onRollback...)
  187. txDriver.mu.Unlock()
  188. for i := len(hooks) - 1; i >= 0; i-- {
  189. fn = hooks[i](fn)
  190. }
  191. return fn.Rollback(tx.ctx, tx)
  192. }
  193. // OnRollback adds a hook to call on rollback.
  194. func (tx *Tx) OnRollback(f RollbackHook) {
  195. txDriver := tx.config.driver.(*txDriver)
  196. txDriver.mu.Lock()
  197. txDriver.onRollback = append(txDriver.onRollback, f)
  198. txDriver.mu.Unlock()
  199. }
  200. // Client returns a Client that binds to current transaction.
  201. func (tx *Tx) Client() *Client {
  202. tx.clientOnce.Do(func() {
  203. tx.client = &Client{config: tx.config}
  204. tx.client.init()
  205. })
  206. return tx.client
  207. }
  208. func (tx *Tx) init() {
  209. tx.APIKey = NewAPIKeyClient(tx.config)
  210. tx.Account = NewAccountClient(tx.config)
  211. tx.AccountGroup = NewAccountGroupClient(tx.config)
  212. tx.Announcement = NewAnnouncementClient(tx.config)
  213. tx.AnnouncementRead = NewAnnouncementReadClient(tx.config)
  214. tx.AuthIdentity = NewAuthIdentityClient(tx.config)
  215. tx.AuthIdentityChannel = NewAuthIdentityChannelClient(tx.config)
  216. tx.BatchImageEvent = NewBatchImageEventClient(tx.config)
  217. tx.BatchImageItem = NewBatchImageItemClient(tx.config)
  218. tx.BatchImageJob = NewBatchImageJobClient(tx.config)
  219. tx.ChannelMonitor = NewChannelMonitorClient(tx.config)
  220. tx.ChannelMonitorDailyRollup = NewChannelMonitorDailyRollupClient(tx.config)
  221. tx.ChannelMonitorHistory = NewChannelMonitorHistoryClient(tx.config)
  222. tx.ChannelMonitorRequestTemplate = NewChannelMonitorRequestTemplateClient(tx.config)
  223. tx.CompositeModelRoute = NewCompositeModelRouteClient(tx.config)
  224. tx.ErrorPassthroughRule = NewErrorPassthroughRuleClient(tx.config)
  225. tx.Group = NewGroupClient(tx.config)
  226. tx.IdempotencyRecord = NewIdempotencyRecordClient(tx.config)
  227. tx.IdentityAdoptionDecision = NewIdentityAdoptionDecisionClient(tx.config)
  228. tx.PaymentAuditLog = NewPaymentAuditLogClient(tx.config)
  229. tx.PaymentOrder = NewPaymentOrderClient(tx.config)
  230. tx.PaymentProviderInstance = NewPaymentProviderInstanceClient(tx.config)
  231. tx.PendingAuthSession = NewPendingAuthSessionClient(tx.config)
  232. tx.PromoCode = NewPromoCodeClient(tx.config)
  233. tx.PromoCodeUsage = NewPromoCodeUsageClient(tx.config)
  234. tx.Proxy = NewProxyClient(tx.config)
  235. tx.RedeemCode = NewRedeemCodeClient(tx.config)
  236. tx.SecurityPolicyKeyword = NewSecurityPolicyKeywordClient(tx.config)
  237. tx.SecuritySecret = NewSecuritySecretClient(tx.config)
  238. tx.Setting = NewSettingClient(tx.config)
  239. tx.SubscriptionPlan = NewSubscriptionPlanClient(tx.config)
  240. tx.TLSFingerprintProfile = NewTLSFingerprintProfileClient(tx.config)
  241. tx.UsageCleanupTask = NewUsageCleanupTaskClient(tx.config)
  242. tx.UsageLog = NewUsageLogClient(tx.config)
  243. tx.User = NewUserClient(tx.config)
  244. tx.UserAllowedGroup = NewUserAllowedGroupClient(tx.config)
  245. tx.UserAttributeDefinition = NewUserAttributeDefinitionClient(tx.config)
  246. tx.UserAttributeValue = NewUserAttributeValueClient(tx.config)
  247. tx.UserPlatformQuota = NewUserPlatformQuotaClient(tx.config)
  248. tx.UserSubscription = NewUserSubscriptionClient(tx.config)
  249. }
  250. // txDriver wraps the given dialect.Tx with a nop dialect.Driver implementation.
  251. // The idea is to support transactions without adding any extra code to the builders.
  252. // When a builder calls to driver.Tx(), it gets the same dialect.Tx instance.
  253. // Commit and Rollback are nop for the internal builders and the user must call one
  254. // of them in order to commit or rollback the transaction.
  255. //
  256. // If a closed transaction is embedded in one of the generated entities, and the entity
  257. // applies a query, for example: APIKey.QueryXXX(), the query will be executed
  258. // through the driver which created this transaction.
  259. //
  260. // Note that txDriver is not goroutine safe.
  261. type txDriver struct {
  262. // the driver we started the transaction from.
  263. drv dialect.Driver
  264. // tx is the underlying transaction.
  265. tx dialect.Tx
  266. // completion hooks.
  267. mu sync.Mutex
  268. onCommit []CommitHook
  269. onRollback []RollbackHook
  270. }
  271. // newTx creates a new transactional driver.
  272. func newTx(ctx context.Context, drv dialect.Driver) (*txDriver, error) {
  273. tx, err := drv.Tx(ctx)
  274. if err != nil {
  275. return nil, err
  276. }
  277. return &txDriver{tx: tx, drv: drv}, nil
  278. }
  279. // Tx returns the transaction wrapper (txDriver) to avoid Commit or Rollback calls
  280. // from the internal builders. Should be called only by the internal builders.
  281. func (tx *txDriver) Tx(context.Context) (dialect.Tx, error) { return tx, nil }
  282. // Dialect returns the dialect of the driver we started the transaction from.
  283. func (tx *txDriver) Dialect() string { return tx.drv.Dialect() }
  284. // Close is a nop close.
  285. func (*txDriver) Close() error { return nil }
  286. // Commit is a nop commit for the internal builders.
  287. // User must call `Tx.Commit` in order to commit the transaction.
  288. func (*txDriver) Commit() error { return nil }
  289. // Rollback is a nop rollback for the internal builders.
  290. // User must call `Tx.Rollback` in order to rollback the transaction.
  291. func (*txDriver) Rollback() error { return nil }
  292. // Exec calls tx.Exec.
  293. func (tx *txDriver) Exec(ctx context.Context, query string, args, v any) error {
  294. return tx.tx.Exec(ctx, query, args, v)
  295. }
  296. // Query calls tx.Query.
  297. func (tx *txDriver) Query(ctx context.Context, query string, args, v any) error {
  298. return tx.tx.Query(ctx, query, args, v)
  299. }
  300. var _ dialect.Driver = (*txDriver)(nil)
  301. // ExecContext allows calling the underlying ExecContext method of the transaction if it is supported by it.
  302. // See, database/sql#Tx.ExecContext for more information.
  303. func (tx *txDriver) ExecContext(ctx context.Context, query string, args ...any) (stdsql.Result, error) {
  304. ex, ok := tx.tx.(interface {
  305. ExecContext(context.Context, string, ...any) (stdsql.Result, error)
  306. })
  307. if !ok {
  308. return nil, fmt.Errorf("Tx.ExecContext is not supported")
  309. }
  310. return ex.ExecContext(ctx, query, args...)
  311. }
  312. // QueryContext allows calling the underlying QueryContext method of the transaction if it is supported by it.
  313. // See, database/sql#Tx.QueryContext for more information.
  314. func (tx *txDriver) QueryContext(ctx context.Context, query string, args ...any) (*stdsql.Rows, error) {
  315. q, ok := tx.tx.(interface {
  316. QueryContext(context.Context, string, ...any) (*stdsql.Rows, error)
  317. })
  318. if !ok {
  319. return nil, fmt.Errorf("Tx.QueryContext is not supported")
  320. }
  321. return q.QueryContext(ctx, query, args...)
  322. }