Documentation ¶
Overview ¶
Package pg provides small utilities for the lib/pq database driver.
It also registers the sql.Driver "hapg", which can resolve uris from the high-availability postgres package.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrBadRequest = errors.New("bad request")
var ErrUserInputNotFound = errors.New("pg: user input not found")
ErrUserInputNotFound indicates that a query returned no results. It is equivalent to sql.ErrNoRows, except that ErrUserInputNotFound also indicates the query was based on user-provided parameters, and the lack of results should be communicated back to the user.
In contrast, we use sql.ErrNoRows to represent an internal error; this indicates a bug in our code and only a generic "internal error" message should be communicated back to the user.
Functions ¶
func ForQueryRows ¶
ForQueryRows encapsulates a lot of boilerplate when making db queries. Call it like this:
err = ForQueryRows(ctx, db, query, queryArg1, queryArg2, ..., func(scanVar1 type1, scanVar2 type2, ...) { ...process a row from the result... })
This is equivalent to:
rows, err = db.Query(ctx, query, queryArg1, queryArg2, ...) if err != nil { return err } defer rows.Close() for rows.Next() { var ( scanVar1 type1 scanVar2 type2 ) err = rows.Scan(&scanVar1, &scanVar2, ...) if err != nil { return err } ...process a row from the result... } if err = rows.Err(); err != nil { return err }
The callback is invoked once for each row in the result. The number and types of parameters to the callback must match the values to be scanned with rows.Scan. The space for the callback's arguments is not reused between calls. The callback may return a single error-type value. If any invocation yields a non-nil result, ForQueryRows will abort and return it.
func IsUniqueViolation ¶
IsUniqueViolation returns true if the given error is a Postgres unique constraint violation error.
Types ¶
type DB ¶
type DB interface { Query(context.Context, string, ...interface{}) (*chainsql.Rows, error) QueryRow(context.Context, string, ...interface{}) *chainsql.Row Exec(context.Context, string, ...interface{}) (chainsql.Result, error) }
DB holds methods common to the DB, Tx, and Stmt types in package sql.