pgx: github.com/jackc/pgx/stdlib Index | Files

package stdlib

import "github.com/jackc/pgx/stdlib"

Package stdlib is the compatibility layer from pgx to database/sql.

A database/sql connection can be established through sql.Open.

db, err := sql.Open("pgx", "postgres://pgx_md5:secret@localhost:5432/pgx_test?sslmode=disable")
if err != nil {
	return err
}

Or from a DSN string.

db, err := sql.Open("pgx", "user=postgres password=secret host=localhost port=5432 database=pgx_test sslmode=disable")
if err != nil {
	return err
}

A DriverConfig can be used to further configure the connection process. This allows configuring TLS configuration, setting a custom dialer, logging, and setting an AfterConnect hook.

driverConfig := stdlib.DriverConfig{
	ConnConfig: ConnConfig: pgx.ConnConfig{
		Logger:   logger,
	},
	AfterConnect: func(c *pgx.Conn) error {
		// Ensure all connections have this temp table available
		_, err := c.Exec("create temporary table foo(...)")
		return err
	},
}

stdlib.RegisterDriverConfig(&driverConfig)

db, err := sql.Open("pgx", driverConfig.ConnectionString("postgres://pgx_md5:secret@127.0.0.1:5432/pgx_test"))
if err != nil {
	return err
}

AcquireConn and ReleaseConn acquire and release a *pgx.Conn from the standard database/sql.DB connection pool. This allows operations that must be performed on a single connection, but should not be run in a transaction or to use pgx specific functionality.

conn, err := stdlib.AcquireConn(db)
if err != nil {
	return err
}
defer stdlib.ReleaseConn(db, conn)

// do stuff with pgx.Conn

It also can be used to enable a fast path for pgx while preserving compatibility with other drivers and database.

conn, err := stdlib.AcquireConn(db)
if err == nil {
	// fast path with pgx
	// ...
	// release conn when done
	stdlib.ReleaseConn(db, conn)
} else {
	// normal path for other drivers and databases
}

Index

Package Files

sql.go

Variables

var ErrNotPgx = errors.New("not pgx *sql.DB")

func AcquireConn Uses

func AcquireConn(db *sql.DB) (*pgx.Conn, error)

func RegisterDriverConfig Uses

func RegisterDriverConfig(c *DriverConfig)

RegisterDriverConfig registers a DriverConfig for use with Open.

func ReleaseConn Uses

func ReleaseConn(db *sql.DB, conn *pgx.Conn) error

func UnregisterDriverConfig Uses

func UnregisterDriverConfig(c *DriverConfig)

UnregisterDriverConfig removes a DriverConfig registration.

type Conn Uses

type Conn struct {
    // contains filtered or unexported fields
}

func (*Conn) Begin Uses

func (c *Conn) Begin() (driver.Tx, error)

func (*Conn) BeginTx Uses

func (c *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error)

func (*Conn) Close Uses

func (c *Conn) Close() error

func (*Conn) Exec Uses

func (c *Conn) Exec(query string, argsV []driver.Value) (driver.Result, error)

func (*Conn) ExecContext Uses

func (c *Conn) ExecContext(ctx context.Context, query string, argsV []driver.NamedValue) (driver.Result, error)

func (*Conn) Ping Uses

func (c *Conn) Ping(ctx context.Context) error

func (*Conn) Prepare Uses

func (c *Conn) Prepare(query string) (driver.Stmt, error)

func (*Conn) PrepareContext Uses

func (c *Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error)

func (*Conn) Query Uses

func (c *Conn) Query(query string, argsV []driver.Value) (driver.Rows, error)

func (*Conn) QueryContext Uses

func (c *Conn) QueryContext(ctx context.Context, query string, argsV []driver.NamedValue) (driver.Rows, error)

type Driver Uses

type Driver struct {
    // contains filtered or unexported fields
}

func (*Driver) Open Uses

func (d *Driver) Open(name string) (driver.Conn, error)

type DriverConfig Uses

type DriverConfig struct {
    pgx.ConnConfig
    AfterConnect func(*pgx.Conn) error // function to call on every new connection
    // contains filtered or unexported fields
}

func (*DriverConfig) ConnectionString Uses

func (c *DriverConfig) ConnectionString(original string) string

ConnectionString encodes the DriverConfig into the original connection string. DriverConfig must be registered before calling ConnectionString.

type Rows Uses

type Rows struct {
    // contains filtered or unexported fields
}

func (*Rows) Close Uses

func (r *Rows) Close() error

func (*Rows) ColumnTypeDatabaseTypeName Uses

func (r *Rows) ColumnTypeDatabaseTypeName(index int) string

ColumnTypeDatabaseTypeName return the database system type name.

func (*Rows) ColumnTypeLength Uses

func (r *Rows) ColumnTypeLength(index int) (int64, bool)

ColumnTypeLength returns the length of the column type if the column is a variable length type. If the column is not a variable length type ok should return false.

func (*Rows) ColumnTypePrecisionScale Uses

func (r *Rows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool)

ColumnTypePrecisionScale should return the precision and scale for decimal types. If not applicable, ok should be false.

func (*Rows) ColumnTypeScanType Uses

func (r *Rows) ColumnTypeScanType(index int) reflect.Type

ColumnTypeScanType returns the value type that can be used to scan types into.

func (*Rows) Columns Uses

func (r *Rows) Columns() []string

func (*Rows) Next Uses

func (r *Rows) Next(dest []driver.Value) error

type Stmt Uses

type Stmt struct {
    // contains filtered or unexported fields
}

func (*Stmt) Close Uses

func (s *Stmt) Close() error

func (*Stmt) Exec Uses

func (s *Stmt) Exec(argsV []driver.Value) (driver.Result, error)

func (*Stmt) ExecContext Uses

func (s *Stmt) ExecContext(ctx context.Context, argsV []driver.NamedValue) (driver.Result, error)

func (*Stmt) NumInput Uses

func (s *Stmt) NumInput() int

func (*Stmt) Query Uses

func (s *Stmt) Query(argsV []driver.Value) (driver.Rows, error)

func (*Stmt) QueryContext Uses

func (s *Stmt) QueryContext(ctx context.Context, argsV []driver.NamedValue) (driver.Rows, error)

Package stdlib imports 12 packages (graph) and is imported by 21 packages. Updated 2017-08-23. Refresh now. Tools for package owners.