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.
func IsValidJSONB ¶
IsValidJSONB returns true if the provided bytes may be stored in a Postgres JSONB data type. It validates that b is valid utf-8 and valid json. It also verifies that it does not include the \u0000 escape sequence, unsupported by the jsonb data type: https://www.postgresql.org/message-id/E1YHHV8-00032A-Em@gemulon.postgresql.org
Types ¶
type DB ¶
type DB interface { QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) QueryRowContext(context.Context, string, ...interface{}) *sql.Row ExecContext(context.Context, string, ...interface{}) (sql.Result, error) }
DB holds methods common to the DB, Tx, and Stmt types in package sql.