github.com/rjgonzale/pop/v5@v5.1.3-dev/tx.go (about)

     1  package pop
     2  
     3  import (
     4  	"context"
     5  	"math/rand"
     6  	"time"
     7  
     8  	"github.com/jmoiron/sqlx"
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  func init() {
    13  	rand.Seed(time.Now().UnixNano())
    14  }
    15  
    16  // Tx stores a transaction with an ID to keep track.
    17  type Tx struct {
    18  	ID int
    19  	*sqlx.Tx
    20  }
    21  
    22  func newTX(ctx context.Context, db *dB) (*Tx, error) {
    23  	t := &Tx{
    24  		ID: rand.Int(),
    25  	}
    26  	tx, err := db.BeginTxx(ctx, nil)
    27  	t.Tx = tx
    28  	return t, errors.Wrap(err, "could not create new transaction")
    29  }
    30  
    31  // TransactionContext simply returns the current transaction,
    32  // this is defined so it implements the `Store` interface.
    33  func (tx *Tx) TransactionContext(ctx context.Context) (*Tx, error) {
    34  	return tx, nil
    35  }
    36  
    37  // Transaction simply returns the current transaction,
    38  // this is defined so it implements the `Store` interface.
    39  func (tx *Tx) Transaction() (*Tx, error) {
    40  	return tx, nil
    41  }
    42  
    43  // Close does nothing. This is defined so it implements the `Store` interface.
    44  func (tx *Tx) Close() error {
    45  	return nil
    46  }