scan

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 5 Imported by: 2

Documentation

Overview

Package scan maps database result sets to Go structs using cached field metadata from the schema package. It is driver-agnostic: callers provide implementations of the Row and Rows interfaces (compatible with the driver package), and the scanner handles column-to-field resolution, pointer construction, and slice population.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FieldPtr

func FieldPtr(v reflect.Value, field *schema.Field) any

FieldPtr returns a scan destination for the struct field identified by the schema Field, suitable for passing to database/sql or pgx Scan. It navigates nested structs using the field's GoIndex chain.

time.Time and *time.Time fields are wrapped in sql.Scanner adapters: TEXT-affinity drivers (sqlite, turso) return timestamps as RFC3339 strings, which the raw pointers cannot accept. Drivers that already produce time.Time (postgres, clickhouse) pass through the adapter unchanged.

v must be the reflect.Value of the struct (not a pointer to it).

func IsNilable

func IsNilable(t reflect.Type) bool

IsNilable returns true if the given type can hold a nil value. This includes pointers, interfaces, slices, maps, channels, and functions.

func ScanRow

func ScanRow(row Row, dest any, table *schema.Table) error

ScanRow scans a single row into a struct using the provided table metadata. dest must be a pointer to a struct. The function builds a slice of field pointers from the struct based on the Table's Fields ordering and calls row.Scan with those pointers.

func ScanRows

func ScanRows(rows Rows, dest any, table *schema.Table) error

ScanRows scans all rows from a result set into a slice of structs using the provided table metadata. dest must be a pointer to a slice of structs (e.g., *[]User). The function uses column names from the result set to resolve which table fields correspond to which scan positions, allowing queries that return a subset of columns to work correctly.

After initial column resolution the hot loop performs zero allocations beyond the new struct values appended to the slice.

Types

type ColumnMap

type ColumnMap struct {
	// contains filtered or unexported fields
}

ColumnMap maps database column names to schema fields for efficient scanning. It is built once from a Table's field list and reused across queries.

func NewColumnMap

func NewColumnMap(table *schema.Table) *ColumnMap

NewColumnMap creates a ColumnMap from a Table's fields. It reuses the pre-built FieldsByColumn map from the table instead of rebuilding one on every call.

func (*ColumnMap) Resolve

func (cm *ColumnMap) Resolve(columns []string) []*schema.Field

Resolve returns the fields that correspond to the given column names, in the order the columns appear. If a column has no matching field in the map, the corresponding entry in the returned slice is nil.

type Row

type Row interface {
	Scan(dest ...any) error
}

Row interface compatible with driver.Row. It represents a single result row from a query.

type Rows

type Rows interface {
	Next() bool
	Scan(dest ...any) error
	Columns() ([]string, error)
	Close() error
	Err() error
}

Rows interface compatible with driver.Rows. It represents a multi-row result set from a query.

Jump to

Keyboard shortcuts

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