pg

package
v0.0.0-...-59fa258 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2016 License: AGPL-3.0 Imports: 15 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 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 {
	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.

type Uint32s

type Uint32s []uint32

func (*Uint32s) Scan

func (a *Uint32s) Scan(val interface{}) error

func (Uint32s) Value

func (a Uint32s) Value() (driver.Value, error)

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