Files
dbx/scan.go
Aleksey Shakhmatov 2c9af28548
Some checks failed
CI / test (push) Failing after 13s
Add production features: slog adapter, scan helpers, slow query logging, pool stats, tracer passthrough, test tx isolation
- slog.go: SlogLogger adapts *slog.Logger to dbx.Logger interface
- scan.go: Collect[T] and CollectOne[T] generic helpers using pgx.RowToStructByName
- cluster.go: slow query logging via Config.SlowQueryThreshold (Warn level in queryEnd)
- stats.go: PoolStats with Cluster.Stats() aggregating pool stats across all nodes
- config.go/node.go: NodeConfig.Tracer passthrough for pgx.QueryTracer (OpenTelemetry)
- options.go: WithSlowQueryThreshold and WithTracer functional options
- dbxtest/tx.go: RunInTx runs callback in always-rolled-back transaction for test isolation

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-23 00:19:26 +03:00

29 lines
840 B
Go

package dbx
import (
"context"
"github.com/jackc/pgx/v5"
)
// Collect executes a read query and collects all rows into a slice of T
// using pgx.RowToStructByName. T must be a struct with db tags matching column names.
func Collect[T any](ctx context.Context, c *Cluster, sql string, args ...any) ([]T, error) {
rows, err := c.ReadQuery(ctx, sql, args...)
if err != nil {
return nil, err
}
return pgx.CollectRows(rows, pgx.RowToStructByName[T])
}
// CollectOne executes a read query and collects exactly one row into T.
// Returns pgx.ErrNoRows if no rows are returned.
func CollectOne[T any](ctx context.Context, c *Cluster, sql string, args ...any) (T, error) {
rows, err := c.ReadQuery(ctx, sql, args...)
if err != nil {
var zero T
return zero, err
}
return pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[T])
}