Documentation ¶
Overview ¶
Package hermes wraps the jmoiron/sqlx *sqlx.DB and *sqlx.Tx in a common interface, hermes.Conn.
Use hermes.Conn in functions to optionally support transactions in your database queries. It allows you to create database queries composed of other functions without having to worry about whether or not you're working off a database connection or an existing transaction.
Additionally, testing with the database becomes easier. Simply create a transaction at the beginning of every test with a `defer tx.Close()`, pass the transaction into your functions instead of the database connection, and don't commit the transaction at the end. Every database insert, select, update, and delete will function normally in your test, then rollback and clean out the database automatically at the end.
Index ¶
- Variables
- func DidConnectionFail(err error) bool
- func DisableTimeouts()
- func EnableTimeouts(dur time.Duration, panic bool)
- func ExitOnFailure(db *DB, err error)
- func GenerateSavepointID() string
- func PanicOnFailure(db *DB, err error)
- type Conn
- type DB
- func (db *DB) BaseDB() *sqlx.DB
- func (db *DB) BaseTx() *sqlx.Tx
- func (db *DB) Begin() (Conn, error)
- func (db *DB) BeginCtx(ctx context.Context) (Conn, error)
- func (db *DB) Close() error
- func (db *DB) Commit() error
- func (db *DB) Context() context.Context
- func (db *DB) Exec(query string, args ...interface{}) (sql.Result, error)
- func (db *DB) Get(dest interface{}, query string, args ...interface{}) error
- func (db *DB) MaxIdle(n int)
- func (db *DB) MaxOpen(n int)
- func (db *DB) Name() string
- func (db *DB) Ping() error
- func (db *DB) Prepare(query string) (*sqlx.Stmt, error)
- func (db *DB) Query(query string, args ...interface{}) (*sqlx.Rows, error)
- func (db *DB) Rollback() error
- func (db *DB) RollbackTo(savepointID string) error
- func (db *DB) RolledBack() bool
- func (db *DB) Row(query string, args ...interface{}) (*sqlx.Row, error)
- func (db *DB) Savepoint() (string, error)
- func (db *DB) Select(dest interface{}, query string, args ...interface{}) error
- type FailureFn
- type MockDB
- type MockTx
- type Tx
- func (tx *Tx) BaseDB() *sqlx.DB
- func (tx *Tx) BaseTx() *sqlx.Tx
- func (tx *Tx) Begin() (Conn, error)
- func (tx *Tx) BeginCtx(ctx context.Context) (Conn, error)
- func (tx *Tx) Close() error
- func (tx *Tx) Commit() error
- func (tx *Tx) Context() context.Context
- func (tx *Tx) Exec(query string, args ...interface{}) (sql.Result, error)
- func (tx *Tx) Get(dest interface{}, query string, args ...interface{}) error
- func (tx *Tx) Name() string
- func (tx *Tx) Prepare(query string) (*sqlx.Stmt, error)
- func (tx *Tx) Query(query string, args ...interface{}) (*sqlx.Rows, error)
- func (tx *Tx) Rollback() error
- func (tx *Tx) RollbackTo(savepointID string) error
- func (tx *Tx) RolledBack() bool
- func (tx *Tx) Row(query string, args ...interface{}) (*sqlx.Row, error)
- func (tx *Tx) Savepoint() (string, error)
- func (tx *Tx) Select(dest interface{}, query string, args ...interface{}) error
Constants ¶
This section is empty.
Variables ¶
var ( // TxTimeout configures the transaction timer, which warns you about // long-lived transactions. This can be used in development to ensure // all transactions are closed correctly. // // Enabling transaction timeouts should not be used in production. If // enabled, a timer is created for each transaction, adding measurable // overhead to database processing. TxTimeout struct { // Enabled must be set to true to enable transaction timers. Enabled bool // Duration is the time to wait in milliseconds before reporting // a transaction being left open. Duration time.Duration // Panic set to true causes Hermes to panic if the transaction // remains open past its duration. When false, Hermes simply // writes a message to os.Stderr. Panic bool } // ErrTooManyClients matches the error returned by PostgreSQL when the // number of client connections exceeds that allowed by the server. ErrTooManyClients = errors.New("pq: sorry, too many clients already") // Confirm indicates whether or not to test each database connection // for validity before attempting a query, i.e. issue a Ping first. // Set to the number of retries before failing. Default is to not // confirm the connection, i.e. zero retries. Confirm int )
var ( // ErrBadContext returned when the caller attempts to reset the context. ErrBadContext = errors.New("context mismatch") // ErrTxRolledBack returned by calls to the transaction if it has been // rolled back. ErrTxRolledBack = errors.New("transaction rolled back") // ErrTxCommitted returned if the caller tries to rollback then // commit a transaction in the same function. ErrTxCommitted = errors.New("already committed") )
Functions ¶
func DidConnectionFail ¶ added in v1.1.0
DidConnectionFail checks the error message returned from a database request Used by hermes.PanicDB in several instances. May be used by applications with other connection types, or to test queries not covered by PanicDB, such as scanning row results.
If exit is nil, simply returns the error, skipping the check.
func DisableTimeouts ¶ added in v1.2.1
func DisableTimeouts()
DisableTimeouts disables transaction timeouts. Transaction timers may be disabled at any time. Any existing timers will simply clean themselves up quietly.
func EnableTimeouts ¶ added in v1.2.1
EnableTimeouts enables the transaction timer, which will display an error message or panic if a transaction exceeds the precribed duration. Useful during development for tracking down transactions that weren't properly cleaned up.
Transaction timers may be enabled and disabled at will without requiring a restart.
Do not use in production! The overhead will measurably slow down your application.
func ExitOnFailure ¶ added in v1.1.0
ExitOnFailure forces an `os.Exit(2)` when the connection fails. This can be useful in applications that trap panics, such as in HTTP middleware.
func GenerateSavepointID ¶ added in v1.2.4
func GenerateSavepointID() string
GenerateSavepointID generates a globally unique ID to use with a savepoint. Note that savepoint identifiers are prefixed with "point_" just in case the generated ID starts with a number.
func PanicOnFailure ¶ added in v1.1.0
PanicOnFailure panics when the connection fails or the database server errors. If your application traps panics, see ExitOnFailure.
Types ¶
type Conn ¶
type Conn interface { // DB returns the base database connection. BaseDB() *sqlx.DB // Tx returns the base database transaction, or nil if there is no // transaction. BaseTx() *sqlx.Tx // Context returns the context associated with this transaction, or nil // if a context is not associated. Context() context.Context // Begin a new transaction. Returns a Conn wrapping the transaction // (*sqlx.Tx). Begin() (Conn, error) // Begin a new transaction in context. The Conn will have the context // associated with it and use it for all subsequent commands. BeginCtx(ctx context.Context) (Conn, error) // Exec executes a database statement with no results.. Exec(query string, args ...interface{}) (sql.Result, error) // Query the databsae. Query(query string, args ...interface{}) (*sqlx.Rows, error) // Row queries for a single row. Row(query string, args ...interface{}) (*sqlx.Row, error) // Prepare a database query. Prepare(query string) (*sqlx.Stmt, error) // Get a single record from the database, e.g. "SELECT ... LIMIT 1". Get(dest interface{}, query string, args ...interface{}) error // Select a collection of results. Select(dest interface{}, query string, args ...interface{}) error // Commit the transaction. Commit() error // Rollback the transaction. This will rollback any parent transactions // as well. Rollback() error // Close rolls back a transaction (and all its parent transactions) if // it hasn't been committed. Useful in a defer. Close() error // Is this connection in a rollback state? RolledBack() bool // The data source name for this connection Name() string // Savepoint creates a savepoint in a transaction. On a database // connection does nothing. Savepoint() (string, error) // RollbackTo a savepoint ID. On a database connection does nothing. RollbackTo(savepointID string) error }
Conn masks the *sqlx.DB and *sqlx.Tx.
type DB ¶
type DB struct { // OnFailure, if defined, is called when the database connection returns // a connection failed or other server-related error. May be used to // reset the database pool connections. Optional. OnFailure FailureFn // contains filtered or unexported fields }
DB represents a database connection. Implements the hermes.Conn interface.
func ConnectUnchecked ¶ added in v1.1.0
ConnectUnchecked connects to the database, but does not test the connection before returning.
func (*DB) BeginCtx ¶
BeginCtx begins a new transaction in context. The Conn will have the context associated with it and use it for all subsequent commands.
func (*DB) RollbackTo ¶ added in v1.2.4
RollbackTo does nothng on DB.
type FailureFn ¶ added in v1.1.0
FailureFn defines the template for the check function called when the database action returns a connection-related error. Useful for trapping connection failures and resetting the database connection pool.
type MockDB ¶
type MockDB struct{ *DB }
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx wraps a sqlx.Tx transaction. Tracks context.
func (*Tx) BeginCtx ¶
BeginCtx begins a new transaction in context. The Conn will have the context associated with it and use it for all subsequent commands.
func (*Tx) Commit ¶
Commit the current transaction. Returns ErrTxRolledBack if the transaction was already rolled back, or ErrTxCommitted if it was committed.
func (*Tx) Rollback ¶
Rollback the transaction. Ignored if the transaction is already in a rollback. Returns ErrTxCommitted if the transaction was committed.
func (*Tx) RollbackTo ¶ added in v1.2.4
RollbackTo rolls back to the savepoint.
func (*Tx) RolledBack ¶
RolledBack returns true if the transaction was rolled back.