...

Source file src/github.com/jackc/pgx/v4/pgxpool/tx.go

Documentation: github.com/jackc/pgx/v4/pgxpool

     1  package pgxpool
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/jackc/pgconn"
     7  	"github.com/jackc/pgx/v4"
     8  )
     9  
    10  // Tx represents a database transaction acquired from a Pool.
    11  type Tx struct {
    12  	t pgx.Tx
    13  	c *Conn
    14  }
    15  
    16  // Begin starts a pseudo nested transaction implemented with a savepoint.
    17  func (tx *Tx) Begin(ctx context.Context) (pgx.Tx, error) {
    18  	return tx.t.Begin(ctx)
    19  }
    20  
    21  func (tx *Tx) BeginFunc(ctx context.Context, f func(pgx.Tx) error) error {
    22  	return tx.t.BeginFunc(ctx, f)
    23  }
    24  
    25  // Commit commits the transaction and returns the associated connection back to the Pool. Commit will return ErrTxClosed
    26  // if the Tx is already closed, but is otherwise safe to call multiple times. If the commit fails with a rollback status
    27  // (e.g. the transaction was already in a broken state) then ErrTxCommitRollback will be returned.
    28  func (tx *Tx) Commit(ctx context.Context) error {
    29  	err := tx.t.Commit(ctx)
    30  	if tx.c != nil {
    31  		tx.c.Release()
    32  		tx.c = nil
    33  	}
    34  	return err
    35  }
    36  
    37  // Rollback rolls back the transaction and returns the associated connection back to the Pool. Rollback will return ErrTxClosed
    38  // if the Tx is already closed, but is otherwise safe to call multiple times. Hence, defer tx.Rollback() is safe even if
    39  // tx.Commit() will be called first in a non-error condition.
    40  func (tx *Tx) Rollback(ctx context.Context) error {
    41  	err := tx.t.Rollback(ctx)
    42  	if tx.c != nil {
    43  		tx.c.Release()
    44  		tx.c = nil
    45  	}
    46  	return err
    47  }
    48  
    49  func (tx *Tx) CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error) {
    50  	return tx.t.CopyFrom(ctx, tableName, columnNames, rowSrc)
    51  }
    52  
    53  func (tx *Tx) SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults {
    54  	return tx.t.SendBatch(ctx, b)
    55  }
    56  
    57  func (tx *Tx) LargeObjects() pgx.LargeObjects {
    58  	return tx.t.LargeObjects()
    59  }
    60  
    61  // Prepare creates a prepared statement with name and sql. If the name is empty,
    62  // an anonymous prepared statement will be used. sql can contain placeholders
    63  // for bound parameters. These placeholders are referenced positionally as $1, $2, etc.
    64  //
    65  // Prepare is idempotent; i.e. it is safe to call Prepare multiple times with the same
    66  // name and sql arguments. This allows a code path to Prepare and Query/Exec without
    67  // needing to first check whether the statement has already been prepared.
    68  func (tx *Tx) Prepare(ctx context.Context, name, sql string) (*pgconn.StatementDescription, error) {
    69  	return tx.t.Prepare(ctx, name, sql)
    70  }
    71  
    72  func (tx *Tx) Exec(ctx context.Context, sql string, arguments ...interface{}) (pgconn.CommandTag, error) {
    73  	return tx.t.Exec(ctx, sql, arguments...)
    74  }
    75  
    76  func (tx *Tx) Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error) {
    77  	return tx.t.Query(ctx, sql, args...)
    78  }
    79  
    80  func (tx *Tx) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row {
    81  	return tx.t.QueryRow(ctx, sql, args...)
    82  }
    83  
    84  func (tx *Tx) QueryFunc(ctx context.Context, sql string, args []interface{}, scans []interface{}, f func(pgx.QueryFuncRow) error) (pgconn.CommandTag, error) {
    85  	return tx.t.QueryFunc(ctx, sql, args, scans, f)
    86  }
    87  
    88  func (tx *Tx) Conn() *pgx.Conn {
    89  	return tx.t.Conn()
    90  }
    91  

View as plain text