123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- package ent
- import (
- "context"
- stdsql "database/sql"
- "fmt"
- "sync"
- "entgo.io/ent/dialect"
- )
- type Tx struct {
- config
-
- Agent *AgentClient
-
- AgentBase *AgentBaseClient
-
- AliyunAvatar *AliyunAvatarClient
-
- AllocAgent *AllocAgentClient
-
- ApiKey *ApiKeyClient
-
- BatchMsg *BatchMsgClient
-
- Category *CategoryClient
-
- ChatRecords *ChatRecordsClient
-
- ChatSession *ChatSessionClient
-
- Contact *ContactClient
-
- CreditBalance *CreditBalanceClient
-
- CreditUsage *CreditUsageClient
-
- Employee *EmployeeClient
-
- EmployeeConfig *EmployeeConfigClient
-
- Label *LabelClient
-
- LabelRelationship *LabelRelationshipClient
-
- LabelTagging *LabelTaggingClient
-
- Message *MessageClient
-
- MessageRecords *MessageRecordsClient
-
- Msg *MsgClient
-
- PayRecharge *PayRechargeClient
-
- Server *ServerClient
-
- SopNode *SopNodeClient
-
- SopStage *SopStageClient
-
- SopTask *SopTaskClient
-
- Token *TokenClient
-
- Tutorial *TutorialClient
-
- UsageDetail *UsageDetailClient
-
- UsageStatisticDay *UsageStatisticDayClient
-
- UsageStatisticHour *UsageStatisticHourClient
-
- UsageStatisticMonth *UsageStatisticMonthClient
-
- UsageTotal *UsageTotalClient
-
- Whatsapp *WhatsappClient
-
- WhatsappChannel *WhatsappChannelClient
-
- WorkExperience *WorkExperienceClient
-
- WpChatroom *WpChatroomClient
-
- WpChatroomMember *WpChatroomMemberClient
-
- Wx *WxClient
-
- WxCard *WxCardClient
-
- WxCardUser *WxCardUserClient
-
- WxCardVisit *WxCardVisitClient
-
- client *Client
- clientOnce sync.Once
-
-
- ctx context.Context
- }
- type (
-
- Committer interface {
- Commit(context.Context, *Tx) error
- }
-
-
-
- CommitFunc func(context.Context, *Tx) error
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- CommitHook func(Committer) Committer
- )
- func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error {
- return f(ctx, tx)
- }
- func (tx *Tx) Commit() error {
- txDriver := tx.config.driver.(*txDriver)
- var fn Committer = CommitFunc(func(context.Context, *Tx) error {
- return txDriver.tx.Commit()
- })
- txDriver.mu.Lock()
- hooks := append([]CommitHook(nil), txDriver.onCommit...)
- txDriver.mu.Unlock()
- for i := len(hooks) - 1; i >= 0; i-- {
- fn = hooks[i](fn)
- }
- return fn.Commit(tx.ctx, tx)
- }
- func (tx *Tx) OnCommit(f CommitHook) {
- txDriver := tx.config.driver.(*txDriver)
- txDriver.mu.Lock()
- txDriver.onCommit = append(txDriver.onCommit, f)
- txDriver.mu.Unlock()
- }
- type (
-
- Rollbacker interface {
- Rollback(context.Context, *Tx) error
- }
-
-
-
- RollbackFunc func(context.Context, *Tx) error
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- RollbackHook func(Rollbacker) Rollbacker
- )
- func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error {
- return f(ctx, tx)
- }
- func (tx *Tx) Rollback() error {
- txDriver := tx.config.driver.(*txDriver)
- var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error {
- return txDriver.tx.Rollback()
- })
- txDriver.mu.Lock()
- hooks := append([]RollbackHook(nil), txDriver.onRollback...)
- txDriver.mu.Unlock()
- for i := len(hooks) - 1; i >= 0; i-- {
- fn = hooks[i](fn)
- }
- return fn.Rollback(tx.ctx, tx)
- }
- func (tx *Tx) OnRollback(f RollbackHook) {
- txDriver := tx.config.driver.(*txDriver)
- txDriver.mu.Lock()
- txDriver.onRollback = append(txDriver.onRollback, f)
- txDriver.mu.Unlock()
- }
- func (tx *Tx) Client() *Client {
- tx.clientOnce.Do(func() {
- tx.client = &Client{config: tx.config}
- tx.client.init()
- })
- return tx.client
- }
- func (tx *Tx) init() {
- tx.Agent = NewAgentClient(tx.config)
- tx.AgentBase = NewAgentBaseClient(tx.config)
- tx.AliyunAvatar = NewAliyunAvatarClient(tx.config)
- tx.AllocAgent = NewAllocAgentClient(tx.config)
- tx.ApiKey = NewApiKeyClient(tx.config)
- tx.BatchMsg = NewBatchMsgClient(tx.config)
- tx.Category = NewCategoryClient(tx.config)
- tx.ChatRecords = NewChatRecordsClient(tx.config)
- tx.ChatSession = NewChatSessionClient(tx.config)
- tx.Contact = NewContactClient(tx.config)
- tx.CreditBalance = NewCreditBalanceClient(tx.config)
- tx.CreditUsage = NewCreditUsageClient(tx.config)
- tx.Employee = NewEmployeeClient(tx.config)
- tx.EmployeeConfig = NewEmployeeConfigClient(tx.config)
- tx.Label = NewLabelClient(tx.config)
- tx.LabelRelationship = NewLabelRelationshipClient(tx.config)
- tx.LabelTagging = NewLabelTaggingClient(tx.config)
- tx.Message = NewMessageClient(tx.config)
- tx.MessageRecords = NewMessageRecordsClient(tx.config)
- tx.Msg = NewMsgClient(tx.config)
- tx.PayRecharge = NewPayRechargeClient(tx.config)
- tx.Server = NewServerClient(tx.config)
- tx.SopNode = NewSopNodeClient(tx.config)
- tx.SopStage = NewSopStageClient(tx.config)
- tx.SopTask = NewSopTaskClient(tx.config)
- tx.Token = NewTokenClient(tx.config)
- tx.Tutorial = NewTutorialClient(tx.config)
- tx.UsageDetail = NewUsageDetailClient(tx.config)
- tx.UsageStatisticDay = NewUsageStatisticDayClient(tx.config)
- tx.UsageStatisticHour = NewUsageStatisticHourClient(tx.config)
- tx.UsageStatisticMonth = NewUsageStatisticMonthClient(tx.config)
- tx.UsageTotal = NewUsageTotalClient(tx.config)
- tx.Whatsapp = NewWhatsappClient(tx.config)
- tx.WhatsappChannel = NewWhatsappChannelClient(tx.config)
- tx.WorkExperience = NewWorkExperienceClient(tx.config)
- tx.WpChatroom = NewWpChatroomClient(tx.config)
- tx.WpChatroomMember = NewWpChatroomMemberClient(tx.config)
- tx.Wx = NewWxClient(tx.config)
- tx.WxCard = NewWxCardClient(tx.config)
- tx.WxCardUser = NewWxCardUserClient(tx.config)
- tx.WxCardVisit = NewWxCardVisitClient(tx.config)
- }
- type txDriver struct {
-
- drv dialect.Driver
-
- tx dialect.Tx
-
- mu sync.Mutex
- onCommit []CommitHook
- onRollback []RollbackHook
- }
- func newTx(ctx context.Context, drv dialect.Driver) (*txDriver, error) {
- tx, err := drv.Tx(ctx)
- if err != nil {
- return nil, err
- }
- return &txDriver{tx: tx, drv: drv}, nil
- }
- func (tx *txDriver) Tx(context.Context) (dialect.Tx, error) { return tx, nil }
- func (tx *txDriver) Dialect() string { return tx.drv.Dialect() }
- func (*txDriver) Close() error { return nil }
- func (*txDriver) Commit() error { return nil }
- func (*txDriver) Rollback() error { return nil }
- func (tx *txDriver) Exec(ctx context.Context, query string, args, v any) error {
- return tx.tx.Exec(ctx, query, args, v)
- }
- func (tx *txDriver) Query(ctx context.Context, query string, args, v any) error {
- return tx.tx.Query(ctx, query, args, v)
- }
- var _ dialect.Driver = (*txDriver)(nil)
- func (tx *txDriver) ExecContext(ctx context.Context, query string, args ...any) (stdsql.Result, error) {
- ex, ok := tx.tx.(interface {
- ExecContext(context.Context, string, ...any) (stdsql.Result, error)
- })
- if !ok {
- return nil, fmt.Errorf("Tx.ExecContext is not supported")
- }
- return ex.ExecContext(ctx, query, args...)
- }
- func (tx *txDriver) QueryContext(ctx context.Context, query string, args ...any) (*stdsql.Rows, error) {
- q, ok := tx.tx.(interface {
- QueryContext(context.Context, string, ...any) (*stdsql.Rows, error)
- })
- if !ok {
- return nil, fmt.Errorf("Tx.QueryContext is not supported")
- }
- return q.QueryContext(ctx, query, args...)
- }
|