Documentation
¶
Overview ¶
Package stdlib registers pgz as a database/sql driver named "pgz". Import it for side effects:
import (
"database/sql"
_ "github.com/arturoeanton/pgz/pgz/stdlib"
)
db, err := sql.Open("pgz", "postgres://user:pass@host/db?sslmode=disable")
rows, err := db.Query("SELECT id, name FROM users WHERE active = $1", true)
_, err = db.Exec("INSERT INTO users (name) VALUES ($1)", "alice")
Both read and write paths are supported. Transactions go through BeginTx / Commit / Rollback. Positional parameters ($1..$N) only — named parameters return an error.
The adapter shares the same prepared-statement cache and the same wire connection as the native API, so sql.DB using this driver pays the standard database/sql interface-boxing cost on top of that, but no second pool, no duplicated handshake, no duplicated stmt cache.
The native API (pgz.ScanStruct, pgz.StreamNDJSON, pgz.Exec, pgz.CopyFromBinary) remains the fastest path when you own the code end-to-end. Use this adapter when you need sqlc / goose / sqlx / golang-migrate compatibility or when an existing codebase already builds on database/sql.
Index ¶
- func NewConnector(cfg pgz.Config) driver.Connector
- 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, args []driver.NamedValue) (driver.Result, error)
- func (c *Conn) IsValid() bool
- 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, args []driver.NamedValue) (driver.Rows, error)
- type Driver
- type Stmt
- func (s *Stmt) Close() error
- func (s *Stmt) Exec(args []driver.Value) (driver.Result, error)
- func (s *Stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error)
- func (s *Stmt) NumInput() int
- func (s *Stmt) Query(args []driver.Value) (driver.Rows, error)
- func (s *Stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn implements driver.Conn / ConnPrepareContext / ConnBeginTx / ExecerContext / QueryerContext / Pinger / Validator. The inTx flag short-circuits BeginTx when a transaction is already open — the database/sql layer would catch it too, but surfacing it here keeps the error message specific.
func (*Conn) Begin ¶
Begin implements driver.Conn by delegating to BeginTx with default options. database/sql calls this for legacy BeginTx-less code paths.
func (*Conn) BeginTx ¶
BeginTx opens a transaction. The isolation level and read-only flag are translated to their SQL equivalents and issued as part of the opening statement. Nesting is rejected; database/sql normally prevents it but we surface a specific error.
func (*Conn) ExecContext ¶
func (c *Conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error)
ExecContext runs a DML / DDL statement. On PG protocol-level errors (ErrorResponse) we return a *pgz.PGError untouched so callers can errors.As it; on wire-level errors we mark the conn bad and return driver.ErrBadConn so database/sql evicts it from the pool.
func (*Conn) PrepareContext ¶
func (*Conn) QueryContext ¶
func (c *Conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error)
QueryContext is the fast path for database/sql.Query without a separate Prepare step. args are converted via namedToAny. Uses RawQueryAny so INSERT/UPDATE/DELETE … RETURNING work through db.QueryRow exactly like SELECT.
type Driver ¶
type Driver struct{}
Driver is the registered database/sql driver. Users typically do not reference this type directly — sql.Open("pgz", ...) is all that is required.
type Stmt ¶
type Stmt struct {
// contains filtered or unexported fields
}
Stmt implements driver.Stmt / StmtQueryContext / StmtExecContext.
func (*Stmt) ExecContext ¶
ExecContext reuses the shared native stmt cache by SQL key. The second call with the same query skips Parse + Describe and goes straight to Bind + Execute + Sync.