db

package
v0.0.0-...-1172882 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMaxConnIdleTime time.Duration = 10 * time.Second
	DefaultMaxConns        int32         = 10
	DefaultMinConns        int32         = 5
	DefaultMaxConnLifetime time.Duration = 5 * time.Minute
)

Variables

This section is empty.

Functions

func AcquireAdvisoryLock

func AcquireAdvisoryLock(ctx context.Context, q Querier, lockKey int) (bool, error)

AcquireAdvisoryLock attempt to acquire an advisory lock on the provided lockKey, returns true if acquired, or false not.

func Migrate

func Migrate(ctx context.Context, databaseURL string, direction migrate.MigrationDirection, count int) (int, error)

func OpenDBConnectionPool

func OpenDBConnectionPool(ctx context.Context, dataSourceName string, poolConfigs ...PoolConfig) (*pgxpool.Pool, error)

func QueryMany

func QueryMany[T any](ctx context.Context, q Querier, query string, args ...any) ([]T, error)

QueryMany executes a query and scans all result rows into []T. For struct types, it uses pgx named-field scanning (db tags). For scalar types (int, bool, string, etc.), it uses direct column scanning. Returns an empty slice (not nil) if no rows are found.

func QueryManyPtrs

func QueryManyPtrs[T any](ctx context.Context, q Querier, query string, args ...any) ([]*T, error)

QueryManyPtrs executes a query and scans all result rows into []*T. For struct types, it uses pgx named-field scanning (db tags). For scalar types (int, bool, string, etc.), it uses direct column scanning. Returns an empty slice (not nil) if no rows are found.

func QueryOne

func QueryOne[T any](ctx context.Context, q Querier, query string, args ...any) (T, error)

QueryOne executes a query and scans the single result row into T. For struct types, it uses pgx named-field scanning (db tags). For scalar types (int, bool, string, etc.), it uses direct column scanning. Returns pgx.ErrNoRows if no row is found.

func ReleaseAdvisoryLock

func ReleaseAdvisoryLock(ctx context.Context, q Querier, lockKey int) error

ReleaseAdvisoryLock releases an advisory lock on the provided lockKey.

func RunInTransaction

func RunInTransaction(ctx context.Context, pool *pgxpool.Pool, fn func(pgx.Tx) error) error

RunInTransaction runs the given atomic function in a pgx transaction. It automatically rolls back on error and commits on success.

func SQLDBFromPool

func SQLDBFromPool(pool *pgxpool.Pool) *sql.DB

SQLDBFromPool returns a *sql.DB backed by the given pgx pool. This is only needed for libraries that require database/sql (e.g. sql-migrate).

Types

type PoolConfig

type PoolConfig struct {
	MaxConns        int32
	MinConns        int32
	MaxConnLifetime time.Duration
	MaxConnIdleTime time.Duration
	QueryExecMode   pgx.QueryExecMode // 0 = pgx default (CacheStatement)
}

PoolConfig holds configurable pgxpool settings. Zero values fall back to Default* constants.

func DefaultPoolConfig

func DefaultPoolConfig() PoolConfig

DefaultPoolConfig returns a PoolConfig populated with the default values.

type Querier

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

Querier is the minimal interface shared by *pgxpool.Pool and pgx.Tx. It allows QueryOne/QueryMany to work with both pool and transaction.

Directories

Path Synopsis
Package dbtest provides test database utilities with TimescaleDB support.
Package dbtest provides test database utilities with TimescaleDB support.

Jump to

Keyboard shortcuts

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