Search and commands

↑ ↓ to select · Enter to open · Esc to close

Type to search packages.

stdlib

v3.6.2+incompatible
github.com/cdn77/pgx/stdlib

Documentation

pkg.go.dev ↗

Table of contents

↓ to select · Enter to jump· Shift+Enter for source

45 matches

Top ↑

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: 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:[email protected]:5432/pgx_test"))
if err != nil {
	return err
}

pgx uses standard PostgreSQL positional parameters in queries. e.g. $1, $2. It does not support named parameters.

db.QueryRow("select * from users where id=$1", userID)

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 without running in a transaction, and it supports operations that 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
}

Variables

ErrNotPgx

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

Functions

AcquireConn

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

OpenDB

func OpenDB(config pgx.ConnConfig, opts ...OptionOpenDB) *sql.DB

OpenDBFromPool

func OpenDBFromPool(pool *pgx.ConnPool, opts ...OptionOpenDBFromPool) *sql.DB

OpenDBFromPool create a sql.DB connection from a pgx.ConnPool

RegisterDriverConfig

func RegisterDriverConfig(c *DriverConfig)

RegisterDriverConfig registers a DriverConfig for use with Open.

ReleaseConn

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

UnregisterDriverConfig

func UnregisterDriverConfig(c *DriverConfig)

UnregisterDriverConfig removes a DriverConfig registration.

Types

Conn

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

Conn.Begin

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

Conn.BeginTx

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

Conn.Close

func (c *Conn) Close() error

Conn.Exec

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

Conn.ExecContext

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

Conn.Ping

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

Conn.Prepare

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

Conn.PrepareContext

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

Conn.Query

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

Conn.QueryContext

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

Driver

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

GetDefaultDriver

func GetDefaultDriver() *Driver

GetDefaultDriver return the driver initialize in the init function and used when register pgx driver

Driver.Open

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

DriverConfig

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

DriverConfig.ConnectionString

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

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

OptionOpenDB

type OptionOpenDB func(*connector)

OptionOpenDB options for configuring the driver when opening a new db pool.

OptionAfterConnect

func OptionAfterConnect(ac func(*pgx.Conn) error) OptionOpenDB

OptionAfterConnect provide a callback for after connect.

OptionOpenDBFromPool

type OptionOpenDBFromPool func(*poolConnector)

OptionOpenDB options for configuring the driver when opening a new db pool.

OptionPreferSimpleProtocol

func OptionPreferSimpleProtocol(preferSimpleProtocol bool) OptionOpenDBFromPool

OptionAfterConnect provide a callback for after connect.

Rows

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

Rows.Close

func (r *Rows) Close() error

Rows.ColumnTypeDatabaseTypeName

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

ColumnTypeDatabaseTypeName return the database system type name.

Rows.ColumnTypeLength

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.

Rows.ColumnTypePrecisionScale

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.

Rows.ColumnTypeScanType

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

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

Rows.Columns

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

Rows.Next

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

Stmt

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

Stmt.Close

func (s *Stmt) Close() error

Stmt.Exec

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

Stmt.ExecContext

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

Stmt.NumInput

func (s *Stmt) NumInput() int

Stmt.Query

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

Stmt.QueryContext

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

Dependency graph