package stdlib
import "github.com/jackc/pgx/v4/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 }
Or a pgx.ConnConfig can be used to set configuration not accessible via connection string. In this case the pgx.ConnConfig must first be registered with the driver. This registration returns a connection string which is used with sql.Open.
connConfig, _ := pgx.ParseConfig(os.Getenv("DATABASE_URL")) connConfig.Logger = myLogger connStr := stdlib.RegisterConnConfig(connConfig) db, _ := sql.Open("pgx", connStr)
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 }
Index ¶
- Variables
- func AcquireConn(db *sql.DB) (*pgx.Conn, error)
- func GetDefaultDriver() driver.Driver
- func OpenDB(config pgx.ConnConfig, opts ...OptionOpenDB) *sql.DB
- func RegisterConnConfig(c *pgx.ConnConfig) string
- func ReleaseConn(db *sql.DB, conn *pgx.Conn) error
- func UnregisterConnConfig(connStr string)
- type Conn
- func (c *Conn) Begin() (driver.Tx, error)
- func (c *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error)
- func (c *Conn) Close() error
- func (c *Conn) ExecContext(ctx context.Context, query string, argsV []driver.NamedValue) (driver.Result, error)
- func (c *Conn) Ping(ctx context.Context) error
- func (c *Conn) Prepare(query string) (driver.Stmt, error)
- func (c *Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error)
- func (c *Conn) QueryContext(ctx context.Context, query string, argsV []driver.NamedValue) (driver.Rows, error)
- type Driver
- func (d *Driver) Open(name string) (driver.Conn, error)
- func (d *Driver) OpenConnector(name string) (driver.Connector, error)
- type OptionOpenDB
- type Rows
- func (r *Rows) Close() error
- func (r *Rows) ColumnTypeDatabaseTypeName(index int) string
- func (r *Rows) ColumnTypeLength(index int) (int64, bool)
- func (r *Rows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool)
- func (r *Rows) ColumnTypeScanType(index int) reflect.Type
- func (r *Rows) Columns() []string
- func (r *Rows) Next(dest []driver.Value) error
- type Stmt
- func (s *Stmt) Close() error
- func (s *Stmt) Exec(argsV []driver.Value) (driver.Result, error)
- func (s *Stmt) ExecContext(ctx context.Context, argsV []driver.NamedValue) (driver.Result, error)
- func (s *Stmt) NumInput() int
- func (s *Stmt) Query(argsV []driver.Value) (driver.Rows, error)
- func (s *Stmt) QueryContext(ctx context.Context, argsV []driver.NamedValue) (driver.Rows, error)
Variables ¶
Functions ¶
func AcquireConn ¶
func GetDefaultDriver ¶
GetDefaultDriver returns the driver initialized in the init function and used when the pgx driver is registered.
func OpenDB ¶
func OpenDB(config pgx.ConnConfig, opts ...OptionOpenDB) *sql.DB
func RegisterConnConfig ¶
func RegisterConnConfig(c *pgx.ConnConfig) string
RegisterConnConfig registers a ConnConfig and returns the connection string to use with Open.
func ReleaseConn ¶
func UnregisterConnConfig ¶
func UnregisterConnConfig(connStr string)
UnregisterConnConfig removes the ConnConfig registration for connStr.
Types ¶
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
func (*Conn) Begin ¶
func (*Conn) BeginTx ¶
func (*Conn) Close ¶
func (*Conn) ExecContext ¶
func (c *Conn) ExecContext(ctx context.Context, query string, argsV []driver.NamedValue) (driver.Result, error)
func (*Conn) Ping ¶
func (*Conn) Prepare ¶
func (*Conn) PrepareContext ¶
func (*Conn) QueryContext ¶
func (c *Conn) QueryContext(ctx context.Context, query string, argsV []driver.NamedValue) (driver.Rows, error)
type Driver ¶
type Driver struct {
// contains filtered or unexported fields
}
func (*Driver) Open ¶
func (*Driver) OpenConnector ¶
type OptionOpenDB ¶
type OptionOpenDB func(*connector)
OptionOpenDB options for configuring the driver when opening a new db pool.
func OptionAfterConnect ¶
func OptionAfterConnect(ac func(context.Context, *pgx.Conn) error) OptionOpenDB
OptionAfterConnect provide a callback for after connect.
type Rows ¶
type Rows struct {
// contains filtered or unexported fields
}
func (*Rows) Close ¶
func (*Rows) ColumnTypeDatabaseTypeName ¶
ColumnTypeDatabaseTypeName return the database system type name.
func (*Rows) ColumnTypeLength ¶
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 ¶
ColumnTypePrecisionScale should return the precision and scale for decimal types. If not applicable, ok should be false.
func (*Rows) ColumnTypeScanType ¶
ColumnTypeScanType returns the value type that can be used to scan types into.
func (*Rows) Columns ¶
func (*Rows) Next ¶
type Stmt ¶
type Stmt struct {
// contains filtered or unexported fields
}
func (*Stmt) Close ¶
func (*Stmt) Exec ¶
func (*Stmt) ExecContext ¶
func (*Stmt) NumInput ¶
func (*Stmt) Query ¶
func (*Stmt) QueryContext ¶
Source Files ¶
- Version
- v4.4.0
- Published
- Feb 5, 2020
- Platform
- linux/amd64
- Imports
- 14 packages
- Last checked
- now –
Tools for package owners.