pg

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsConflict added in v0.11.3

func AsConflict(err error, msg string) error

AsConflict maps a foreign-key violation to 409 with msg (the "still referenced, cannot delete" reading of 23503). Any other error passes through; use AsNotFound when the "referenced row missing" reading is the intended one.

func AsDuplicate

func AsDuplicate(err error, dup error) error

func AsNotFound

func AsNotFound(err error, msg string) error

func AsValidation added in v0.11.3

func AsValidation(err error, msg string) error

AsValidation maps "the caller's input was rejected by the database" to a 400 E_VALIDATION carrying msg: string overflow, unparseable values and numeric range errors. Every other error (transport, constraint shape the caller must handle itself, programming errors) is returned untouched so the caller keeps its own sentinel semantics.

fid, err := r.createField(ctx, ...)
if err != nil { return pg.AsValidation(err, "field name exceeds the column width") }

Foreign-key violations are deliberately NOT folded in: 23503 means different things on insert (related row absent → 400/404) and delete (still referenced → 400/409), so callers pick with AsNotFound/IsForeignKeyViolation. It returns a *webx.Error (not the internal apierr carrier) so it drops straight into any handler that materializes errors through webx.WriteErr — including products with their own error-code bridge, which only recognizes *webx.Error and would otherwise re-bucket this as E_INTERNAL.

func IsForeignKeyViolation added in v0.11.3

func IsForeignKeyViolation(err error) bool

IsForeignKeyViolation reports whether err is a foreign-key rejection (SQLSTATE 23503) — the referenced row is missing on insert, or the row is still referenced on delete.

func IsInvalidTextRepresentation added in v0.11.3

func IsInvalidTextRepresentation(err error) bool

IsInvalidTextRepresentation reports whether err is a value the database could not parse as the target type (SQLSTATE 22P02) — typically a malformed UUID reaching a ::uuid cast or a path parameter used verbatim in a query.

func IsNumericOutOfRange added in v0.11.3

func IsNumericOutOfRange(err error) bool

IsNumericOutOfRange reports whether err is a numeric overflow (SQLSTATE 22003).

func IsStringTooLong added in v0.11.3

func IsStringTooLong(err error) bool

IsStringTooLong reports whether err is PostgreSQL's varchar/char overflow (SQLSTATE 22001). Service layers validate maxLength themselves; this is the backstop for the columns nobody remembered to bound.

func IsUniqueViolation

func IsUniqueViolation(err error, constraint string) bool

func TenantExec added in v0.9.0

func TenantExec(ctx context.Context, db Beginner, id Identity, sql string, args ...any) (pgconn.CommandTag, error)

TenantExec runs ONE statement inside a transaction that carries the tenant GUCs, committing immediately. Multi-statement units of work belong in WithTenant. Each call costs one BEGIN/COMMIT round trip — that is the price of correct tenant scoping; do not be tempted to set session-level GUCs on a pooled connection instead (cross-tenant leak).

func TenantScan added in v0.9.0

func TenantScan(ctx context.Context, db Beginner, id Identity, sql string, args []any, dest ...any) error

TenantScan runs ONE query inside the same scoped transaction and scans a SINGLE row into dest (pgx QueryRow semantics). Deliberately NOT a rows-returning helper: streaming rows would force the transaction (and the tenant scope) to outlive the call.

func WithService

func WithService(ctx context.Context, db Beginner, fn func(ctx context.Context, tx pgx.Tx) error) error

func WithTenant

func WithTenant(ctx context.Context, db Beginner, id Identity, fn func(ctx context.Context, tx pgx.Tx) error) error

Types

type Beginner

type Beginner interface {
	Begin(ctx context.Context) (pgx.Tx, error)
}

type DB

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

func Connect

func Connect(ctx context.Context, dsn string, opt Options) (*DB, error)

func WrapPool

func WrapPool(pool *pgxpool.Pool) *DB

func (*DB) Acquire

func (d *DB) Acquire(ctx context.Context) (*pgxpool.Conn, error)

func (*DB) Begin

func (d *DB) Begin(ctx context.Context) (pgx.Tx, error)

func (*DB) Close

func (d *DB) Close()

func (*DB) Ping

func (d *DB) Ping(ctx context.Context) error

func (*DB) Pool

func (d *DB) Pool() *pgxpool.Pool

func (*DB) Read

func (d *DB) Read(ctx context.Context, fn func(ctx context.Context, q DBTX) error) error

func (*DB) Reset

func (d *DB) Reset()

func (*DB) Tx

func (d *DB) Tx(ctx context.Context) DBTX

func (*DB) Within

func (d *DB) Within(ctx context.Context, fn func(ctx context.Context) error) error

type DBTX

type DBTX interface {
	Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
}

type Identity

type Identity struct {
	WorkspaceID uuid.UUID
	UserID      uuid.UUID
}

type Options

type Options struct {
	StartupTimeout time.Duration

	ConnectTimeout time.Duration

	ReplicaDSN string

	MaxConns int32
	MinConns int32

	MaxConnLifetime  time.Duration
	MaxConnIdleTime  time.Duration
	HealthCheckEvery time.Duration

	SelfHeal *bool

	ExitOnWedge bool
}

Jump to

Keyboard shortcuts

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