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