Package 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 normal pgx connection pool can be established and the database/sql
connection can be created through stdlib.OpenFromConnPool(). This allows
more control over the connection process (such as TLS), more control
over the connection pool, setting an AfterConnect hook, and using both
database/sql and pgx interfaces as needed.
connConfig := pgx.ConnConfig{
Host: "localhost",
User: "pgx_md5",
Password: "secret",
Database: "pgx_test",
}
config := pgx.ConnPoolConfig{ConnConfig: connConfig}
pool, err := pgx.NewConnPool(config)
if err != nil {
return err
}
db, err := stdlib.OpenFromConnPool(pool)
if err != nil {
t.Fatalf("Unable to create connection pool: %v", err)
}
If the database/sql connection is established through
stdlib.OpenFromConnPool then access to a pgx *ConnPool can be regained
through db.Driver(). This allows writing a fast path for pgx while
preserving compatibility with other drivers and database
if driver, ok := db.Driver().(*stdlib.Driver); ok && driver.Pool != nil {
// fast path with pgx
} else {
// normal path for other drivers and databases
}
- func OpenFromConnPool(pool *pgx.ConnPool) (*sql.DB, error)
- type Conn
- func (c *Conn) Begin() (driver.Tx, error)
- func (c *Conn) Close() error
- func (c *Conn) Exec(query string, argsV []driver.Value) (driver.Result, error)
- func (c *Conn) Prepare(query string) (driver.Stmt, error)
- func (c *Conn) Query(query string, argsV []driver.Value) (driver.Rows, error)
- type Driver
- func (d *Driver) Open(name string) (driver.Conn, error)
- type Rows
- func (r *Rows) Close() error
- 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) NumInput() int
- func (s *Stmt) Query(argsV []driver.Value) (driver.Rows, error)
- type Tx
- func (t *Tx) Commit() error
- func (t *Tx) Rollback() error
Package files
sql.go
In the call graph viewer below, each node
is a function belonging to this package
and its children are the functions it
calls—perhaps dynamically.
The root nodes are the entry points of the
package: functions that may be called from
outside the package.
There may be non-exported or anonymous
functions among them if they are called
dynamically from another package.
Click a node to visit that function's source code.
From there you can visit its callers by
clicking its declaring func
token.
Functions may be omitted if they were
determined to be unreachable in the
particular programs or tests that were
analyzed.
func OpenFromConnPool(pool *pgx.ConnPool) (*sql.DB, error)
OpenFromConnPool takes the existing *pgx.ConnPool pool and returns a *sql.DB
with pool as the backend. This enables full control over the connection
process and configuration while maintaining compatibility with the
database/sql interface. In addition, by calling Driver() on the returned
*sql.DB and typecasting to *stdlib.Driver a reference to the pgx.ConnPool can
be reaquired later. This allows fast paths targeting pgx to be used while
still maintaining compatibility with other databases and drivers.
pool connection size must be at least 2.
type Conn struct {
}
func (*Conn) Begin
¶
func (c *Conn) Begin() (driver.Tx, error)
func (*Conn) Close
¶
func (c *Conn) Close() error
func (*Conn) Exec
¶
func (c *Conn) Exec(query string, argsV []driver.Value) (driver.Result, error)
func (c *Conn) Prepare(query string) (driver.Stmt, error)
func (*Conn) Query
¶
func (c *Conn) Query(query string, argsV []driver.Value) (driver.Rows, error)
type Driver struct {
Pool *pgx.ConnPool
}
func (*Driver) Open
¶
func (d *Driver) Open(name string) (driver.Conn, error)
TODO - rename to avoid alloc
type Rows struct {
}
func (*Rows) Close
¶
func (r *Rows) Close() error
func (r *Rows) Columns() []string
func (*Rows) Next
¶
func (r *Rows) Next(dest []driver.Value) error
type Stmt struct {
}
func (*Stmt) Close
¶
func (s *Stmt) Close() error
func (*Stmt) Exec
¶
func (s *Stmt) Exec(argsV []driver.Value) (driver.Result, error)
func (s *Stmt) NumInput() int
func (*Stmt) Query
¶
func (s *Stmt) Query(argsV []driver.Value) (driver.Rows, error)
type Tx struct {
}
func (t *Tx) Commit() error
func (t *Tx) Rollback() error