pgxutil

package
v0.6.3 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Scanner

type Scanner[T any] struct {
	// contains filtered or unexported fields
}

Scanner pre-computes struct reflection at construction time and caches column-to-field mappings keyed by the actual column layout returned by PostgreSQL. Different PG versions returning different columns from SELECT * each get their own cached mapping. Scanner is safe for concurrent use.

Usage:

var pgStatScanner = pgxutil.NewScanner[PgStatDatabase]()

rows, err := pool.Query(ctx, "SELECT * FROM pg_stat_database")
results, err := pgx.CollectRows(rows, pgStatScanner.Scan)

func NewScanner

func NewScanner[T any]() *Scanner[T]

NewScanner creates a Scanner for struct type T. Call this once at package init or startup. It holds a map from field name (as expected from the database) to the index path of the struct. This "cuts" out the index lookup that would be required at runtime if instead it relied on `reflect.Value.FieldByName`.

Panics if T is not a struct.

func (*Scanner[T]) Scan

func (s *Scanner[T]) Scan(row pgx.CollectableRow) (T, error)

Scan implements pgx.RowToFunc[T]. Row columns with no matching struct field are skipped; struct fields with no matching column get zero values.

func (*Scanner[T]) ScanAddr

func (s *Scanner[T]) ScanAddr(row pgx.CollectableRow) (*T, error)

ScanAddr is like Scan but returns a pointer, matching the pgx RowToAddrOf* convention.

Jump to

Keyboard shortcuts

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