Documentation
¶
Overview ¶
Package dbx implements database-related utility functions.
Index ¶
- Variables
- func CollectRows[T any](rows *sql.Rows, expected int, ignore func(error) bool) ([]T, error)
- func QueryCollect[T any](ctx context.Context, db *sql.DB, query string, args ...any) ([]T, error)
- func QueryCollectCountIgnore[T any](ctx context.Context, db *sql.DB, count int, ignore func(error) bool, ...) ([]T, error)
- func QueryCollectIgnore[T any](ctx context.Context, db *sql.DB, ignore func(error) bool, query string, ...) ([]T, error)
- func QueryScan[T any](ctx context.Context, collect func(T), ignore func(error) bool, db *sql.DB, ...) error
- func QueryScanRow[T any](ctx context.Context, db *sql.DB, query string, args ...any) (T, error)
- func ScanRows[T any](rows *sql.Rows, collect func(T) error, ignore func(error) bool) error
Constants ¶
This section is empty.
Variables ¶
var ErrMultipleRows = errors.New("expected one result")
Functions ¶
func CollectRows ¶
CollectRows reads the results of a SQL query.
expected is the expected number of rows. ignore determines which sql.Rows.Scan errors should be ignored.
If T is a struct, the columns of each row are assigned to visible fields of T.
T must not be a pointer.
func QueryCollect ¶
func QueryCollect[T any]( ctx context.Context, db *sql.DB, query string, args ...any, ) ([]T, error)
QueryCollect runs a SQL query.
The columns of each row are assigned to visible fields of T.
func QueryCollectCountIgnore ¶
func QueryCollectCountIgnore[T any]( ctx context.Context, db *sql.DB, count int, ignore func(error) bool, query string, args ...any, ) ([]T, error)
QueryCollectCountIgnore runs a SQL query.
count is the expected number of rows. ignore determines which sql.Rows.Scan errors should be ignored.
The columns of each row are assigned to visible fields of T.
func QueryCollectIgnore ¶
func QueryCollectIgnore[T any]( ctx context.Context, db *sql.DB, ignore func(error) bool, query string, args ...any, ) ([]T, error)
QueryCollectIgnore runs a SQL query.
ignore determines which sql.Rows.Scan errors should be ignored.
The columns of each row are assigned to visible fields of T.
func QueryScan ¶
func QueryScan[T any]( ctx context.Context, collect func(T), ignore func(error) bool, db *sql.DB, query string, args ...any, ) error
QueryScan is like ScanRows but also runs the query.
func QueryScanRow ¶ added in v0.22.0
QueryScanRow is like QueryScan but expects one result.
It fails with ErrMultipleRows if the query returns more than one result.
func ScanRows ¶
ScanRows calls a function for every result of a SQL query.
collect is called for each row. ignore determines which sql.Rows.Scan errors should be ignored.
If T is a struct or a struct pointer, the columns of each row are assigned to visible fields of T.
If T is a struct pointer, collect receives the same pointer every time it's called. It must not store the pointer to the struct or its fields.
Types ¶
This section is empty.