pg

package
v0.0.0-...-4e9b90d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2018 License: AGPL-3.0 Imports: 16 Imported by: 0

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

View Source
var ErrBadRequest = errors.New("bad request")
View Source
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

func ForQueryRows(ctx context.Context, db DB, query string, args ...interface{}) error

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

func IsUniqueViolation(err error) bool

IsUniqueViolation returns true if the given error is a Postgres unique constraint violation error.

func IsValidJSONB

func IsValidJSONB(b []byte) bool

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

func NewDriver

func NewDriver() driver.Driver

func NewListener

func NewListener(ctx context.Context, dbURL, channel string) (*pq.Listener, error)

NewListener creates a new pq.Listener and begins listening.

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.

Directories

Path Synopsis
Package pgtest provides support functions for tests that need to use Postgres.
Package pgtest provides support functions for tests that need to use Postgres.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL