stdlib

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2026 License: MIT Imports: 11 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewConnector

func NewConnector(cfg pgz.Config) driver.Connector

NewConnector builds a Connector from an already-constructed Config. Useful when ParseDSN cannot express the settings you need (for example, a TLS config with a custom RootCAs pool).

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

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

Begin implements driver.Conn by delegating to BeginTx with default options. database/sql calls this for legacy BeginTx-less code paths.

func (*Conn) BeginTx

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

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) Close

func (c *Conn) Close() 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) IsValid

func (c *Conn) IsValid() bool

IsValid is called by sql.DB's connection validator.

func (*Conn) Ping

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

Ping forwards to the underlying client.

func (*Conn) Prepare

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

func (*Conn) PrepareContext

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

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.

func (Driver) Open

func (Driver) Open(dsn string) (driver.Conn, error)

Open parses dsn and connects. Used by sql.DB when no Connector is registered. Prefer NewConnector if you need to customise Config fields that ParseDSN does not expose (TLS callbacks, RuntimeParams, etc.).

func (Driver) OpenConnector

func (Driver) OpenConnector(dsn string) (driver.Connector, error)

OpenConnector returns a database/sql Connector bound to dsn. sql.DB uses this to open new connections on demand.

type Stmt

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

Stmt implements driver.Stmt / StmtQueryContext / StmtExecContext.

func (*Stmt) Close

func (s *Stmt) Close() error

func (*Stmt) Exec

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

func (*Stmt) ExecContext

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

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.

func (*Stmt) NumInput

func (s *Stmt) NumInput() int

func (*Stmt) Query

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

func (*Stmt) QueryContext

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL