Documentation ¶
Index ¶
Constants ¶
const ( OperatorIntervention = "57000" QueryCanceled = "57014" AdminShutdown = "57P01" CrashShutdown = "57P02" CannotConnectNow = "57P03" DatabaseDropped = "57P04" IdleSessionTimeout = "57P05" )
PostgreSQL disconnect errors - https://www.postgresql.org/docs/current/errcodes-appendix.html
Variables ¶
var ( // Disconnects is the list of PostgreSQL error codes that indicate the connection failed. Disconnects = []string{ OperatorIntervention, QueryCanceled, AdminShutdown, CrashShutdown, CannotConnectNow, DatabaseDropped, IdleSessionTimeout, } )
Functions ¶
func IsDisconnected ¶ added in v1.1.0
IsDisconnected returns true if the error is a PostgreSQL disconnect error (SQLSTATE 57P01).
Types ¶
type Conn ¶
type Conn interface { // Begin starts a transaction. If Conn already represents a transaction, pgx will create a // savepoint instead. Begin(ctx context.Context) (Conn, error) // Commit the transaction. Does nothing if Conn is a *pgxpool.Pool. If the transaction is // a psuedo-transaction, i.e. a savepoint, releases the savepoint. Otherwise commits the // transaction. Commit(ctx context.Context) error // Rollback the transaction. Does nothing if Conn is a *pgxpool.Pool. Rollback(ctx context.Context) error // Close rolls back the transaction if this is a real transaction or rolls back to the // savepoint if this is a pseudo nested transaction. For a *pgxpool.Pool, this call is // ignored. // // Returns ErrTxClosed if the Conn is already closed, but is otherwise safe to call multiple // times. Hence, a defer conn.Close() is safe even if conn.Commit() will be called first in // a non-error condition. // // Any other failure of a real transaction will result in the connection being closed. Close(ctx context.Context) error CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error) SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults Exec(ctx context.Context, sql string, arguments ...interface{}) (commandTag pgconn.CommandTag, err error) Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row }
Conn abstracts the *pgxpool.Pool struct and the pgx.Tx interface into a common interface. This can be useful for building domain models more functionally, i.e the same function could be used for a single database query outside of a transaction, or included in a transaction with other function calls.
It's also useful for testing, as you can pass a transaction into any database-related function, don't commit, and simply Close() at the end of the test to clean up the database.
type DB ¶
DB wraps the *pgxpool.Pool and provides the missing hermes function wrappers.
func ConnectConfig ¶
ConnectConfig creates a pgx database connection pool based on a pool configuration and returns it.
type RowScanner ¶ added in v0.2.0
type RowScanner interface {
Scan(dest ...interface{}) error
}
RowScanner is a shared interface between pgx.Rows and pgx.Row
type Tx ¶
type Tx struct {
pgx.Tx
}
Tx wraps the pgx.Tx interface and provides the missing hermes function wrappers.
func (*Tx) Close ¶
Close rolls back the transaction if this is a real transaction or rolls back to the savepoint if this is a pseudo nested transaction.
Returns ErrTxClosed if the Conn is already closed, but is otherwise safe to call multiple times. Hence, a defer conn.Close() is safe even if conn.Commit() will be called first in a non-error condition.
Any other failure of a real transaction will result in the connection being closed.