Documentation
¶
Overview ¶
Package recordset holds row- and column-oriented in-memory recordsets.
A column stores one typed value per row in a slice or a bitmap, or computes it from other columns. A columnar recordset stores one column per field and materializes rows by index.
Index ¶
- func ColDbType(dbType string) func(o *ColumnOptions)
- func Format(format func(value any) string) func(o *ColumnOptions)
- type Column
- func NewBitmapColumn[T comparable](name string, initialCapacity int, getDefaultVal func() T, ...) Column[T]
- func NewBoolColumn(name string, options ...ColumnOption) Column[bool]
- func NewColumn[T any](name string, defaultValue T, options ...ColumnOption) Column[any]
- func NewComputedColumn(name string, evaluator Evaluator, options ...ColumnOption) Column[any]
- func NewTypedColumn[T any](name string, defaultValue T, options ...ColumnOption) Column[T]
- func UntypedCol[T any](c Column[T]) Column[any]
- type ColumnAccessor
- type ColumnOption
- type ColumnOptions
- type ColumnarRecordset
- func (c *ColumnarRecordset) Columns() (columns []Column[any])
- func (c *ColumnarRecordset) ColumnsCount() int
- func (c *ColumnarRecordset) GetColumnByIndex(i int) Column[any]
- func (c *ColumnarRecordset) GetColumnByName(name string) Column[any]
- func (c *ColumnarRecordset) GetColumnIndex(name string) int
- func (rs *ColumnarRecordset) GetRow(i int) Row
- func (rs *ColumnarRecordset) Name() string
- func (rs *ColumnarRecordset) NewRow() Row
- func (rs *ColumnarRecordset) RowsCount() int
- type ComputedColumn
- type Evaluator
- type Option
- type Options
- type Recordset
- type Row
- type Type
- type UntypedColWrapper
- func (c UntypedColWrapper[T]) Add(value any) error
- func (c UntypedColWrapper[T]) DbType() string
- func (c UntypedColWrapper[T]) DefaultValue() (value any)
- func (c UntypedColWrapper[T]) GetValue(row int) (value any, err error)
- func (c UntypedColWrapper[T]) IsBitmap() bool
- func (c UntypedColWrapper[T]) Name() string
- func (c UntypedColWrapper[T]) SetValue(row int, value any) (err error)
- func (c UntypedColWrapper[T]) TypedColumn() Column[T]
- func (c UntypedColWrapper[T]) ValueType() reflect.Type
- func (c UntypedColWrapper[T]) Values() []any
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ColDbType ¶ added in v0.39.0
func ColDbType(dbType string) func(o *ColumnOptions)
ColDbType returns a ColumnOption that sets the database type string read by DbType.
func Format ¶ added in v0.35.0
func Format(format func(value any) string) func(o *ColumnOptions)
Format returns a ColumnOption that stores format on ColumnOptions.
Types ¶
type Column ¶
type Column[T any] interface { // Name returns the column name. Name() string // DefaultValue returns the column's default value. DefaultValue() T // GetValue returns the value at row. GetValue(row int) (value T, err error) // SetValue stores value at row. SetValue(row int, value T) (err error) // DbType returns the database type string configured for the column. DbType() string // ValueType returns the reflect type of the column's values. ValueType() reflect.Type // IsBitmap reports whether the column stores values in a bitmap. IsBitmap() bool // Add adds value to the column. Add(value T) error // Values returns the column's values in row order. Values() []T }
Column is one typed field of a recordset, with a value for each row.
func NewBitmapColumn ¶ added in v0.32.0
func NewBitmapColumn[T comparable](name string, initialCapacity int, getDefaultVal func() T, options ...ColumnOption) Column[T]
NewBitmapColumn returns a Column[T] that records each distinct value with the row indexes that hold it. initialCapacity is the initial length of that distinct-value slice. getDefaultVal supplies DefaultValue, and options configure the column.
func NewBoolColumn ¶ added in v0.32.0
func NewBoolColumn(name string, options ...ColumnOption) Column[bool]
NewBoolColumn returns a bitmap-backed column of bools named name. The default value is false, and options configure the column.
func NewColumn ¶
func NewColumn[T any](name string, defaultValue T, options ...ColumnOption) Column[any]
NewColumn returns a Column[any] wrapping a slice-backed column of T named name. defaultValue is the column default, and options configure the column.
func NewComputedColumn ¶ added in v0.46.0
func NewComputedColumn(name string, evaluator Evaluator, options ...ColumnOption) Column[any]
NewComputedColumn returns a column that derives its value via the supplied Evaluator. The returned value implements both Column[any] and ComputedColumn.
func NewTypedColumn ¶ added in v0.35.0
func NewTypedColumn[T any](name string, defaultValue T, options ...ColumnOption) Column[T]
NewTypedColumn returns a slice-backed Column[T] named name. defaultValue is returned by DefaultValue, and options configure the column.
type ColumnAccessor ¶
type ColumnAccessor interface {
// Columns returns the columns.
Columns() []Column[any]
// ColumnsCount returns the number of columns.
ColumnsCount() int
// GetColumnByIndex returns the column at i, or nil when i is past the last column.
GetColumnByIndex(i int) Column[any]
// GetColumnByName returns the column named name, or nil when no column has that name.
GetColumnByName(name string) Column[any]
// GetColumnIndex returns the index of the column named name, or -1 when it is absent.
GetColumnIndex(name string) int
}
ColumnAccessor reads the columns of a recordset.
type ColumnOption ¶ added in v0.35.0
type ColumnOption = func(o *ColumnOptions)
ColumnOption applies a setting to ColumnOptions.
type ColumnOptions ¶ added in v0.35.0
type ColumnOptions struct {
// contains filtered or unexported fields
}
ColumnOptions holds the database type and format function selected for a column.
type ColumnarRecordset ¶
type ColumnarRecordset struct {
// contains filtered or unexported fields
}
ColumnarRecordset stores each field as a column and addresses rows by index.
func NewColumnarRecordset ¶
func NewColumnarRecordset(name string, cols ...Column[any]) *ColumnarRecordset
NewColumnarRecordset returns a columnar recordset named name that holds cols and no rows.
func (*ColumnarRecordset) ColumnsCount ¶
func (c *ColumnarRecordset) ColumnsCount() int
ColumnsCount returns the number of columns.
func (*ColumnarRecordset) GetColumnByIndex ¶
GetColumnByIndex returns the column at i, or nil when i is greater than or equal to the column count.
func (*ColumnarRecordset) GetColumnByName ¶
GetColumnByName returns the first column whose name equals name, or nil when none match.
func (*ColumnarRecordset) GetColumnIndex ¶
GetColumnIndex returns the index of the first column named name, or -1 when none match.
func (*ColumnarRecordset) GetRow ¶
func (rs *ColumnarRecordset) GetRow(i int) Row
GetRow returns the row at index i, or nil when i is greater than or equal to the row count.
func (*ColumnarRecordset) Name ¶
func (rs *ColumnarRecordset) Name() string
Name returns the name passed to NewColumnarRecordset.
func (*ColumnarRecordset) NewRow ¶
func (rs *ColumnarRecordset) NewRow() Row
NewRow appends each column's default value, increases the row count by one, and returns the new row.
func (*ColumnarRecordset) RowsCount ¶
func (rs *ColumnarRecordset) RowsCount() int
RowsCount returns the number of rows created by NewRow.
type ComputedColumn ¶ added in v0.46.0
type ComputedColumn interface {
Column[any]
// Evaluator returns the Evaluator that derives this column's value.
Evaluator() Evaluator
}
ComputedColumn is a column whose value is derived from the stored columns of a row via an Evaluator, rather than from per-row stored data.
type Evaluator ¶ added in v0.46.0
type Evaluator interface {
// Eval returns the computed value for stored, a map of column name to non-computed value.
Eval(stored map[string]any) (value any, err error)
}
Evaluator computes a value for a computed column from the stored (non-computed) values of a row. The stored map is keyed by column name.
type Option ¶ added in v0.39.1
type Option func(o *options)
Option configures the recordset options built by NewOptions.
type Options ¶ added in v0.39.1
type Options interface {
// Name returns the recordset name. It is empty when no name was set.
Name() string
}
Options reports the name configured for a recordset.
func NewOptions ¶ added in v0.39.1
NewOptions applies opts and returns the resulting Options.
type Recordset ¶
type Recordset interface {
ColumnAccessor
// Name returns the recordset name.
Name() string // Can be useful for debugging purpose (or for joining?)
// NewRow returns a new row in the recordset.
NewRow() Row
// GetRow returns nil if out of range
GetRow(i int) Row
// RowsCount returns the number of rows.
RowsCount() int
}
Recordset provides interface for working with a set of row/column based data
type Row ¶
type Row interface {
// GetValueByName returns the value of the named column from rs.
GetValueByName(name string, rs Recordset) (any, error)
// SetValueByName stores value in the named column of rs.
SetValueByName(name string, value any, rs Recordset) error
// GetValueByIndex returns the value of column i from rs.
GetValueByIndex(i int, rs Recordset) (any, error)
// SetValueByIndex stores value in column i of rs.
SetValueByIndex(i int, value any, rs Recordset) error
// Data returns the row values from rs in column order.
Data(rs Recordset) (data []any, err error)
}
Row defines an interface for storing recordset row data. We intentionally pass cols information to functions of the interface so we do not need to allocate memory for cols pointer in each row.
type Type ¶ added in v0.33.0
type Type int
Type distinguishes unknown, table, view, and stored-procedure values.
const ( // UnknownType is the zero Type. Its String method returns "Unknown". UnknownType Type = iota // Table is the table value of Type. Its String method returns "Table". Table // View is the view value of Type. Its String method returns "View". View // StoredProcedure is the stored-procedure value of Type. Its String method returns "StoredProcedure". StoredProcedure )
type UntypedColWrapper ¶ added in v0.35.0
type UntypedColWrapper[T any] struct { // contains filtered or unexported fields }
UntypedColWrapper adapts a Column[T] so it can be used as a Column[any].
func (UntypedColWrapper[T]) Add ¶ added in v0.35.0
func (c UntypedColWrapper[T]) Add(value any) error
Add appends value when its dynamic type is T. Otherwise Add returns an error and leaves the wrapped column unchanged.
func (UntypedColWrapper[T]) DbType ¶ added in v0.39.0
func (c UntypedColWrapper[T]) DbType() string
DbType returns the wrapped column's database type string.
func (UntypedColWrapper[T]) DefaultValue ¶ added in v0.35.0
func (c UntypedColWrapper[T]) DefaultValue() (value any)
DefaultValue returns the wrapped column's default value.
func (UntypedColWrapper[T]) GetValue ¶ added in v0.35.0
func (c UntypedColWrapper[T]) GetValue(row int) (value any, err error)
GetValue returns the wrapped column's value at row.
func (UntypedColWrapper[T]) IsBitmap ¶ added in v0.35.0
func (c UntypedColWrapper[T]) IsBitmap() bool
IsBitmap reports whether the wrapped column stores values in a bitmap.
func (UntypedColWrapper[T]) Name ¶ added in v0.35.0
func (c UntypedColWrapper[T]) Name() string
Name returns the wrapped column's name.
func (UntypedColWrapper[T]) SetValue ¶ added in v0.35.0
func (c UntypedColWrapper[T]) SetValue(row int, value any) (err error)
SetValue stores value at row. It panics when value's dynamic type is not T.
func (UntypedColWrapper[T]) TypedColumn ¶ added in v0.35.0
func (c UntypedColWrapper[T]) TypedColumn() Column[T]
TypedColumn returns the wrapped Column[T].
func (UntypedColWrapper[T]) ValueType ¶ added in v0.35.0
func (c UntypedColWrapper[T]) ValueType() reflect.Type
ValueType returns the wrapped column's value type.
func (UntypedColWrapper[T]) Values ¶ added in v0.35.0
func (c UntypedColWrapper[T]) Values() []any
Values returns a new slice of the wrapped column's values.