github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/db/ent/tx.go (about)

     1  // Code generated by ent, DO NOT EDIT.
     2  
     3  package ent
     4  
     5  import (
     6  	"context"
     7  	stdsql "database/sql"
     8  	"fmt"
     9  	"sync"
    10  
    11  	"entgo.io/ent/dialect"
    12  )
    13  
    14  // Tx is a transactional client that is created by calling Client.Tx().
    15  type Tx struct {
    16  	config
    17  	// AppCoin is the client for interacting with the AppCoin builders.
    18  	AppCoin *AppCoinClient
    19  	// ChainBase is the client for interacting with the ChainBase builders.
    20  	ChainBase *ChainBaseClient
    21  	// CoinBase is the client for interacting with the CoinBase builders.
    22  	CoinBase *CoinBaseClient
    23  	// CoinDescription is the client for interacting with the CoinDescription builders.
    24  	CoinDescription *CoinDescriptionClient
    25  	// CoinExtra is the client for interacting with the CoinExtra builders.
    26  	CoinExtra *CoinExtraClient
    27  	// CoinFiat is the client for interacting with the CoinFiat builders.
    28  	CoinFiat *CoinFiatClient
    29  	// CoinFiatCurrency is the client for interacting with the CoinFiatCurrency builders.
    30  	CoinFiatCurrency *CoinFiatCurrencyClient
    31  	// CoinFiatCurrencyHistory is the client for interacting with the CoinFiatCurrencyHistory builders.
    32  	CoinFiatCurrencyHistory *CoinFiatCurrencyHistoryClient
    33  	// CoinUsedFor is the client for interacting with the CoinUsedFor builders.
    34  	CoinUsedFor *CoinUsedForClient
    35  	// Currency is the client for interacting with the Currency builders.
    36  	Currency *CurrencyClient
    37  	// CurrencyFeed is the client for interacting with the CurrencyFeed builders.
    38  	CurrencyFeed *CurrencyFeedClient
    39  	// CurrencyHistory is the client for interacting with the CurrencyHistory builders.
    40  	CurrencyHistory *CurrencyHistoryClient
    41  	// ExchangeRate is the client for interacting with the ExchangeRate builders.
    42  	ExchangeRate *ExchangeRateClient
    43  	// Fiat is the client for interacting with the Fiat builders.
    44  	Fiat *FiatClient
    45  	// FiatCurrency is the client for interacting with the FiatCurrency builders.
    46  	FiatCurrency *FiatCurrencyClient
    47  	// FiatCurrencyFeed is the client for interacting with the FiatCurrencyFeed builders.
    48  	FiatCurrencyFeed *FiatCurrencyFeedClient
    49  	// FiatCurrencyHistory is the client for interacting with the FiatCurrencyHistory builders.
    50  	FiatCurrencyHistory *FiatCurrencyHistoryClient
    51  	// Setting is the client for interacting with the Setting builders.
    52  	Setting *SettingClient
    53  	// Tran is the client for interacting with the Tran builders.
    54  	Tran *TranClient
    55  
    56  	// lazily loaded.
    57  	client     *Client
    58  	clientOnce sync.Once
    59  
    60  	// completion callbacks.
    61  	mu         sync.Mutex
    62  	onCommit   []CommitHook
    63  	onRollback []RollbackHook
    64  
    65  	// ctx lives for the life of the transaction. It is
    66  	// the same context used by the underlying connection.
    67  	ctx context.Context
    68  }
    69  
    70  type (
    71  	// Committer is the interface that wraps the Commit method.
    72  	Committer interface {
    73  		Commit(context.Context, *Tx) error
    74  	}
    75  
    76  	// The CommitFunc type is an adapter to allow the use of ordinary
    77  	// function as a Committer. If f is a function with the appropriate
    78  	// signature, CommitFunc(f) is a Committer that calls f.
    79  	CommitFunc func(context.Context, *Tx) error
    80  
    81  	// CommitHook defines the "commit middleware". A function that gets a Committer
    82  	// and returns a Committer. For example:
    83  	//
    84  	//	hook := func(next ent.Committer) ent.Committer {
    85  	//		return ent.CommitFunc(func(ctx context.Context, tx *ent.Tx) error {
    86  	//			// Do some stuff before.
    87  	//			if err := next.Commit(ctx, tx); err != nil {
    88  	//				return err
    89  	//			}
    90  	//			// Do some stuff after.
    91  	//			return nil
    92  	//		})
    93  	//	}
    94  	//
    95  	CommitHook func(Committer) Committer
    96  )
    97  
    98  // Commit calls f(ctx, m).
    99  func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error {
   100  	return f(ctx, tx)
   101  }
   102  
   103  // Commit commits the transaction.
   104  func (tx *Tx) Commit() error {
   105  	txDriver := tx.config.driver.(*txDriver)
   106  	var fn Committer = CommitFunc(func(context.Context, *Tx) error {
   107  		return txDriver.tx.Commit()
   108  	})
   109  	tx.mu.Lock()
   110  	hooks := append([]CommitHook(nil), tx.onCommit...)
   111  	tx.mu.Unlock()
   112  	for i := len(hooks) - 1; i >= 0; i-- {
   113  		fn = hooks[i](fn)
   114  	}
   115  	return fn.Commit(tx.ctx, tx)
   116  }
   117  
   118  // OnCommit adds a hook to call on commit.
   119  func (tx *Tx) OnCommit(f CommitHook) {
   120  	tx.mu.Lock()
   121  	defer tx.mu.Unlock()
   122  	tx.onCommit = append(tx.onCommit, f)
   123  }
   124  
   125  type (
   126  	// Rollbacker is the interface that wraps the Rollback method.
   127  	Rollbacker interface {
   128  		Rollback(context.Context, *Tx) error
   129  	}
   130  
   131  	// The RollbackFunc type is an adapter to allow the use of ordinary
   132  	// function as a Rollbacker. If f is a function with the appropriate
   133  	// signature, RollbackFunc(f) is a Rollbacker that calls f.
   134  	RollbackFunc func(context.Context, *Tx) error
   135  
   136  	// RollbackHook defines the "rollback middleware". A function that gets a Rollbacker
   137  	// and returns a Rollbacker. For example:
   138  	//
   139  	//	hook := func(next ent.Rollbacker) ent.Rollbacker {
   140  	//		return ent.RollbackFunc(func(ctx context.Context, tx *ent.Tx) error {
   141  	//			// Do some stuff before.
   142  	//			if err := next.Rollback(ctx, tx); err != nil {
   143  	//				return err
   144  	//			}
   145  	//			// Do some stuff after.
   146  	//			return nil
   147  	//		})
   148  	//	}
   149  	//
   150  	RollbackHook func(Rollbacker) Rollbacker
   151  )
   152  
   153  // Rollback calls f(ctx, m).
   154  func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error {
   155  	return f(ctx, tx)
   156  }
   157  
   158  // Rollback rollbacks the transaction.
   159  func (tx *Tx) Rollback() error {
   160  	txDriver := tx.config.driver.(*txDriver)
   161  	var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error {
   162  		return txDriver.tx.Rollback()
   163  	})
   164  	tx.mu.Lock()
   165  	hooks := append([]RollbackHook(nil), tx.onRollback...)
   166  	tx.mu.Unlock()
   167  	for i := len(hooks) - 1; i >= 0; i-- {
   168  		fn = hooks[i](fn)
   169  	}
   170  	return fn.Rollback(tx.ctx, tx)
   171  }
   172  
   173  // OnRollback adds a hook to call on rollback.
   174  func (tx *Tx) OnRollback(f RollbackHook) {
   175  	tx.mu.Lock()
   176  	defer tx.mu.Unlock()
   177  	tx.onRollback = append(tx.onRollback, f)
   178  }
   179  
   180  // Client returns a Client that binds to current transaction.
   181  func (tx *Tx) Client() *Client {
   182  	tx.clientOnce.Do(func() {
   183  		tx.client = &Client{config: tx.config}
   184  		tx.client.init()
   185  	})
   186  	return tx.client
   187  }
   188  
   189  func (tx *Tx) init() {
   190  	tx.AppCoin = NewAppCoinClient(tx.config)
   191  	tx.ChainBase = NewChainBaseClient(tx.config)
   192  	tx.CoinBase = NewCoinBaseClient(tx.config)
   193  	tx.CoinDescription = NewCoinDescriptionClient(tx.config)
   194  	tx.CoinExtra = NewCoinExtraClient(tx.config)
   195  	tx.CoinFiat = NewCoinFiatClient(tx.config)
   196  	tx.CoinFiatCurrency = NewCoinFiatCurrencyClient(tx.config)
   197  	tx.CoinFiatCurrencyHistory = NewCoinFiatCurrencyHistoryClient(tx.config)
   198  	tx.CoinUsedFor = NewCoinUsedForClient(tx.config)
   199  	tx.Currency = NewCurrencyClient(tx.config)
   200  	tx.CurrencyFeed = NewCurrencyFeedClient(tx.config)
   201  	tx.CurrencyHistory = NewCurrencyHistoryClient(tx.config)
   202  	tx.ExchangeRate = NewExchangeRateClient(tx.config)
   203  	tx.Fiat = NewFiatClient(tx.config)
   204  	tx.FiatCurrency = NewFiatCurrencyClient(tx.config)
   205  	tx.FiatCurrencyFeed = NewFiatCurrencyFeedClient(tx.config)
   206  	tx.FiatCurrencyHistory = NewFiatCurrencyHistoryClient(tx.config)
   207  	tx.Setting = NewSettingClient(tx.config)
   208  	tx.Tran = NewTranClient(tx.config)
   209  }
   210  
   211  // txDriver wraps the given dialect.Tx with a nop dialect.Driver implementation.
   212  // The idea is to support transactions without adding any extra code to the builders.
   213  // When a builder calls to driver.Tx(), it gets the same dialect.Tx instance.
   214  // Commit and Rollback are nop for the internal builders and the user must call one
   215  // of them in order to commit or rollback the transaction.
   216  //
   217  // If a closed transaction is embedded in one of the generated entities, and the entity
   218  // applies a query, for example: AppCoin.QueryXXX(), the query will be executed
   219  // through the driver which created this transaction.
   220  //
   221  // Note that txDriver is not goroutine safe.
   222  type txDriver struct {
   223  	// the driver we started the transaction from.
   224  	drv dialect.Driver
   225  	// tx is the underlying transaction.
   226  	tx dialect.Tx
   227  }
   228  
   229  // newTx creates a new transactional driver.
   230  func newTx(ctx context.Context, drv dialect.Driver) (*txDriver, error) {
   231  	tx, err := drv.Tx(ctx)
   232  	if err != nil {
   233  		return nil, err
   234  	}
   235  	return &txDriver{tx: tx, drv: drv}, nil
   236  }
   237  
   238  // Tx returns the transaction wrapper (txDriver) to avoid Commit or Rollback calls
   239  // from the internal builders. Should be called only by the internal builders.
   240  func (tx *txDriver) Tx(context.Context) (dialect.Tx, error) { return tx, nil }
   241  
   242  // Dialect returns the dialect of the driver we started the transaction from.
   243  func (tx *txDriver) Dialect() string { return tx.drv.Dialect() }
   244  
   245  // Close is a nop close.
   246  func (*txDriver) Close() error { return nil }
   247  
   248  // Commit is a nop commit for the internal builders.
   249  // User must call `Tx.Commit` in order to commit the transaction.
   250  func (*txDriver) Commit() error { return nil }
   251  
   252  // Rollback is a nop rollback for the internal builders.
   253  // User must call `Tx.Rollback` in order to rollback the transaction.
   254  func (*txDriver) Rollback() error { return nil }
   255  
   256  // Exec calls tx.Exec.
   257  func (tx *txDriver) Exec(ctx context.Context, query string, args, v interface{}) error {
   258  	return tx.tx.Exec(ctx, query, args, v)
   259  }
   260  
   261  // Query calls tx.Query.
   262  func (tx *txDriver) Query(ctx context.Context, query string, args, v interface{}) error {
   263  	return tx.tx.Query(ctx, query, args, v)
   264  }
   265  
   266  var _ dialect.Driver = (*txDriver)(nil)
   267  
   268  // ExecContext allows calling the underlying ExecContext method of the transaction if it is supported by it.
   269  // See, database/sql#Tx.ExecContext for more information.
   270  func (tx *txDriver) ExecContext(ctx context.Context, query string, args ...interface{}) (stdsql.Result, error) {
   271  	ex, ok := tx.tx.(interface {
   272  		ExecContext(context.Context, string, ...interface{}) (stdsql.Result, error)
   273  	})
   274  	if !ok {
   275  		return nil, fmt.Errorf("Tx.ExecContext is not supported")
   276  	}
   277  	return ex.ExecContext(ctx, query, args...)
   278  }
   279  
   280  // QueryContext allows calling the underlying QueryContext method of the transaction if it is supported by it.
   281  // See, database/sql#Tx.QueryContext for more information.
   282  func (tx *txDriver) QueryContext(ctx context.Context, query string, args ...interface{}) (*stdsql.Rows, error) {
   283  	q, ok := tx.tx.(interface {
   284  		QueryContext(context.Context, string, ...interface{}) (*stdsql.Rows, error)
   285  	})
   286  	if !ok {
   287  		return nil, fmt.Errorf("Tx.QueryContext is not supported")
   288  	}
   289  	return q.QueryContext(ctx, query, args...)
   290  }