github.com/crowdsecurity/crowdsec@v1.6.1/pkg/database/ent/tx.go (about)

     1  // Code generated by ent, DO NOT EDIT.
     2  
     3  package ent
     4  
     5  import (
     6  	"context"
     7  	"sync"
     8  
     9  	"entgo.io/ent/dialect"
    10  )
    11  
    12  // Tx is a transactional client that is created by calling Client.Tx().
    13  type Tx struct {
    14  	config
    15  	// Alert is the client for interacting with the Alert builders.
    16  	Alert *AlertClient
    17  	// Bouncer is the client for interacting with the Bouncer builders.
    18  	Bouncer *BouncerClient
    19  	// ConfigItem is the client for interacting with the ConfigItem builders.
    20  	ConfigItem *ConfigItemClient
    21  	// Decision is the client for interacting with the Decision builders.
    22  	Decision *DecisionClient
    23  	// Event is the client for interacting with the Event builders.
    24  	Event *EventClient
    25  	// Lock is the client for interacting with the Lock builders.
    26  	Lock *LockClient
    27  	// Machine is the client for interacting with the Machine builders.
    28  	Machine *MachineClient
    29  	// Meta is the client for interacting with the Meta builders.
    30  	Meta *MetaClient
    31  
    32  	// lazily loaded.
    33  	client     *Client
    34  	clientOnce sync.Once
    35  	// ctx lives for the life of the transaction. It is
    36  	// the same context used by the underlying connection.
    37  	ctx context.Context
    38  }
    39  
    40  type (
    41  	// Committer is the interface that wraps the Commit method.
    42  	Committer interface {
    43  		Commit(context.Context, *Tx) error
    44  	}
    45  
    46  	// The CommitFunc type is an adapter to allow the use of ordinary
    47  	// function as a Committer. If f is a function with the appropriate
    48  	// signature, CommitFunc(f) is a Committer that calls f.
    49  	CommitFunc func(context.Context, *Tx) error
    50  
    51  	// CommitHook defines the "commit middleware". A function that gets a Committer
    52  	// and returns a Committer. For example:
    53  	//
    54  	//	hook := func(next ent.Committer) ent.Committer {
    55  	//		return ent.CommitFunc(func(ctx context.Context, tx *ent.Tx) error {
    56  	//			// Do some stuff before.
    57  	//			if err := next.Commit(ctx, tx); err != nil {
    58  	//				return err
    59  	//			}
    60  	//			// Do some stuff after.
    61  	//			return nil
    62  	//		})
    63  	//	}
    64  	//
    65  	CommitHook func(Committer) Committer
    66  )
    67  
    68  // Commit calls f(ctx, m).
    69  func (f CommitFunc) Commit(ctx context.Context, tx *Tx) error {
    70  	return f(ctx, tx)
    71  }
    72  
    73  // Commit commits the transaction.
    74  func (tx *Tx) Commit() error {
    75  	txDriver := tx.config.driver.(*txDriver)
    76  	var fn Committer = CommitFunc(func(context.Context, *Tx) error {
    77  		return txDriver.tx.Commit()
    78  	})
    79  	txDriver.mu.Lock()
    80  	hooks := append([]CommitHook(nil), txDriver.onCommit...)
    81  	txDriver.mu.Unlock()
    82  	for i := len(hooks) - 1; i >= 0; i-- {
    83  		fn = hooks[i](fn)
    84  	}
    85  	return fn.Commit(tx.ctx, tx)
    86  }
    87  
    88  // OnCommit adds a hook to call on commit.
    89  func (tx *Tx) OnCommit(f CommitHook) {
    90  	txDriver := tx.config.driver.(*txDriver)
    91  	txDriver.mu.Lock()
    92  	txDriver.onCommit = append(txDriver.onCommit, f)
    93  	txDriver.mu.Unlock()
    94  }
    95  
    96  type (
    97  	// Rollbacker is the interface that wraps the Rollback method.
    98  	Rollbacker interface {
    99  		Rollback(context.Context, *Tx) error
   100  	}
   101  
   102  	// The RollbackFunc type is an adapter to allow the use of ordinary
   103  	// function as a Rollbacker. If f is a function with the appropriate
   104  	// signature, RollbackFunc(f) is a Rollbacker that calls f.
   105  	RollbackFunc func(context.Context, *Tx) error
   106  
   107  	// RollbackHook defines the "rollback middleware". A function that gets a Rollbacker
   108  	// and returns a Rollbacker. For example:
   109  	//
   110  	//	hook := func(next ent.Rollbacker) ent.Rollbacker {
   111  	//		return ent.RollbackFunc(func(ctx context.Context, tx *ent.Tx) error {
   112  	//			// Do some stuff before.
   113  	//			if err := next.Rollback(ctx, tx); err != nil {
   114  	//				return err
   115  	//			}
   116  	//			// Do some stuff after.
   117  	//			return nil
   118  	//		})
   119  	//	}
   120  	//
   121  	RollbackHook func(Rollbacker) Rollbacker
   122  )
   123  
   124  // Rollback calls f(ctx, m).
   125  func (f RollbackFunc) Rollback(ctx context.Context, tx *Tx) error {
   126  	return f(ctx, tx)
   127  }
   128  
   129  // Rollback rollbacks the transaction.
   130  func (tx *Tx) Rollback() error {
   131  	txDriver := tx.config.driver.(*txDriver)
   132  	var fn Rollbacker = RollbackFunc(func(context.Context, *Tx) error {
   133  		return txDriver.tx.Rollback()
   134  	})
   135  	txDriver.mu.Lock()
   136  	hooks := append([]RollbackHook(nil), txDriver.onRollback...)
   137  	txDriver.mu.Unlock()
   138  	for i := len(hooks) - 1; i >= 0; i-- {
   139  		fn = hooks[i](fn)
   140  	}
   141  	return fn.Rollback(tx.ctx, tx)
   142  }
   143  
   144  // OnRollback adds a hook to call on rollback.
   145  func (tx *Tx) OnRollback(f RollbackHook) {
   146  	txDriver := tx.config.driver.(*txDriver)
   147  	txDriver.mu.Lock()
   148  	txDriver.onRollback = append(txDriver.onRollback, f)
   149  	txDriver.mu.Unlock()
   150  }
   151  
   152  // Client returns a Client that binds to current transaction.
   153  func (tx *Tx) Client() *Client {
   154  	tx.clientOnce.Do(func() {
   155  		tx.client = &Client{config: tx.config}
   156  		tx.client.init()
   157  	})
   158  	return tx.client
   159  }
   160  
   161  func (tx *Tx) init() {
   162  	tx.Alert = NewAlertClient(tx.config)
   163  	tx.Bouncer = NewBouncerClient(tx.config)
   164  	tx.ConfigItem = NewConfigItemClient(tx.config)
   165  	tx.Decision = NewDecisionClient(tx.config)
   166  	tx.Event = NewEventClient(tx.config)
   167  	tx.Lock = NewLockClient(tx.config)
   168  	tx.Machine = NewMachineClient(tx.config)
   169  	tx.Meta = NewMetaClient(tx.config)
   170  }
   171  
   172  // txDriver wraps the given dialect.Tx with a nop dialect.Driver implementation.
   173  // The idea is to support transactions without adding any extra code to the builders.
   174  // When a builder calls to driver.Tx(), it gets the same dialect.Tx instance.
   175  // Commit and Rollback are nop for the internal builders and the user must call one
   176  // of them in order to commit or rollback the transaction.
   177  //
   178  // If a closed transaction is embedded in one of the generated entities, and the entity
   179  // applies a query, for example: Alert.QueryXXX(), the query will be executed
   180  // through the driver which created this transaction.
   181  //
   182  // Note that txDriver is not goroutine safe.
   183  type txDriver struct {
   184  	// the driver we started the transaction from.
   185  	drv dialect.Driver
   186  	// tx is the underlying transaction.
   187  	tx dialect.Tx
   188  	// completion hooks.
   189  	mu         sync.Mutex
   190  	onCommit   []CommitHook
   191  	onRollback []RollbackHook
   192  }
   193  
   194  // newTx creates a new transactional driver.
   195  func newTx(ctx context.Context, drv dialect.Driver) (*txDriver, error) {
   196  	tx, err := drv.Tx(ctx)
   197  	if err != nil {
   198  		return nil, err
   199  	}
   200  	return &txDriver{tx: tx, drv: drv}, nil
   201  }
   202  
   203  // Tx returns the transaction wrapper (txDriver) to avoid Commit or Rollback calls
   204  // from the internal builders. Should be called only by the internal builders.
   205  func (tx *txDriver) Tx(context.Context) (dialect.Tx, error) { return tx, nil }
   206  
   207  // Dialect returns the dialect of the driver we started the transaction from.
   208  func (tx *txDriver) Dialect() string { return tx.drv.Dialect() }
   209  
   210  // Close is a nop close.
   211  func (*txDriver) Close() error { return nil }
   212  
   213  // Commit is a nop commit for the internal builders.
   214  // User must call `Tx.Commit` in order to commit the transaction.
   215  func (*txDriver) Commit() error { return nil }
   216  
   217  // Rollback is a nop rollback for the internal builders.
   218  // User must call `Tx.Rollback` in order to rollback the transaction.
   219  func (*txDriver) Rollback() error { return nil }
   220  
   221  // Exec calls tx.Exec.
   222  func (tx *txDriver) Exec(ctx context.Context, query string, args, v any) error {
   223  	return tx.tx.Exec(ctx, query, args, v)
   224  }
   225  
   226  // Query calls tx.Query.
   227  func (tx *txDriver) Query(ctx context.Context, query string, args, v any) error {
   228  	return tx.tx.Query(ctx, query, args, v)
   229  }
   230  
   231  var _ dialect.Driver = (*txDriver)(nil)