pkg/exec
The exec package handles query execution and row scanning.
DB Interface
type DB interface {
Exec(ctx context.Context, sql string, arguments ...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
}
Satisfied by pgx.Conn, *pgxpool.Pool, and pgx.Tx.
Scanner Interface
Your scanner struct must implement:
type Scanner[C ColumnAlias] interface {
GetTarget(col string) func() any
}
GetTarget returns a function that provides a pointer to the struct field for scanning. The executor calls this for each column in the result set.
Query Methods
These methods are available on every Table:
Query (multiple rows)
scanners, err := Users.Query(ctx, db, selectQuery)
// Returns []*UsersScanner
QueryRow (single row)
scanner, err := Users.QueryRow(ctx, db, selectQuery)
// Returns *UsersScanner
With Relations
scanners, err := Users.Query(ctx, db, query,
exec.WithRelations(relationLoader1, relationLoader2),
)
Execution Flow
- Build SQL from query
- Execute via
db.Query()ordb.QueryRow() - For each row, create a new Scanner via the factory function
- Call
GetTarget(col)for each column to get scan destinations - Scan row into the scanner
- After all rows, load relations (if any)
- Return populated scanners