record

package
v0.48.3 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package record holds the record.Record type, which is the core type for holding query results.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal(a, b Record) bool

Equal returns true if each element of a and b are equal values. Note that time.Time values compare both time and location components.

func SetKindIfUnknown

func SetKindIfUnknown(meta Meta, i int, k kind.Kind)

SetKindIfUnknown sets meta[i].kind to k, iff the kind is currently kind.Unknown or kind.Null. This function can be used to set the kind after-the-fact, which is useful for some databases that don't always return sufficient type info upfront.

func Valid

func Valid(rec Record) (i int, err error)

Valid checks that each element of the record vals is of an acceptable type. On the first unacceptable element, the index of that element and an error are returned. On success (-1, nil) is returned.

These acceptable types are:

nil, int64, float64, decimal.Decimal, bool, string, []byte, time.Time

Types

type ColumnTypeData

type ColumnTypeData struct {
	ScanType reflect.Type `json:"scan_type"`

	Name string `json:"name"`

	DatabaseTypeName string `json:"database_type_name"`
	Length           int64  `json:"length"`
	Precision        int64  `json:"precision"`
	Scale            int64  `json:"scale"`

	Kind kind.Kind `json:"kind"`

	HasNullable       bool `json:"has_nullable"`
	HasLength         bool `json:"has_length"`
	HasPrecisionScale bool `json:"has_precision_scale"`

	Nullable bool `json:"nullable"`
}

ColumnTypeData contains the same data as sql.ColumnType as well SQ's derived data kind. This type exists with exported fields instead of methods (as on sql.ColumnType) due to the need to work with the fields for testing, and also because for some drivers it's useful to twiddle with the scan type.

This is all a bit ugly.

func NewColumnTypeData

func NewColumnTypeData(col *sql.ColumnType, knd kind.Kind) *ColumnTypeData

NewColumnTypeData returns a new instance with field values taken from col, supplemented with the kind param.

type FieldMeta

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

FieldMeta is a bit of a strange entity, and in an ideal world, it wouldn't exist. It's here because:

- The DB driver impls that sq utilizes (postgres, sqlite, etc) often have individual quirks (not reporting nullability etc) that necessitate modifying sql.ColumnType so that there's consistent behavior across the drivers.

- We wanted to retain (and supplement) the method set of sql.ColumnType (basically, use the same "interface", even though sql.ColumnType is a struct, not interface) so that devs don't need to learn a whole new thing.

- For that reason, stdlib sql.ColumnType needs to be supplemented with kind.Kind, and there needs to be a mechanism for modifying sql.ColumnType's fields.

- But sql.ColumnType is sealed (its fields cannot be changed from outside its package).

- Hence this construct where we have FieldMeta (which abstractly is an adapter around sql.ColumnType) and also a data holder struct (ColumnDataType), which permits the mutation of the column type fields.

Likely there's a better design available than this one, but it suffices.

func NewFieldMeta

func NewFieldMeta(data *ColumnTypeData, mungedName string) *FieldMeta

NewFieldMeta returns a new instance backed by the data arg. If mungedName is empty, ColumnTypeData.Name is used.

func (*FieldMeta) DatabaseTypeName

func (fm *FieldMeta) DatabaseTypeName() string

DatabaseTypeName is documented by sql.ColumnType.DatabaseTypeName.

func (*FieldMeta) DecimalSize

func (fm *FieldMeta) DecimalSize() (precision, scale int64, ok bool)

DecimalSize is documented by sql.ColumnType.DecimalSize.

func (*FieldMeta) Kind

func (fm *FieldMeta) Kind() kind.Kind

Kind returns the data kind for the column.

func (*FieldMeta) Length

func (fm *FieldMeta) Length() (length int64, ok bool)

Length is documented by sql.ColumnType.Written.

func (*FieldMeta) MungedName added in v0.41.0

func (fm *FieldMeta) MungedName() string

MungedName returns the (possibly-munged) column name. This value is what should be used for outputting the col name. This exists largely to handle the case of duplicate col names in a result set, e.g. when doing a JOIN on tables with identically-named columns. But typically this value is the same as that returned by FieldMeta.Name.

func (*FieldMeta) Name

func (fm *FieldMeta) Name() string

Name is documented by sql.ColumnType.Name.

func (*FieldMeta) Nullable

func (fm *FieldMeta) Nullable() (nullable, ok bool)

Nullable is documented by sql.ColumnType.Nullable.

func (*FieldMeta) ScanType

func (fm *FieldMeta) ScanType() reflect.Type

ScanType is documented by sql.ColumnType.ScanType.

func (*FieldMeta) String

func (fm *FieldMeta) String() string

String returns a log-debug friendly representation.

type Meta

type Meta []*FieldMeta

Meta is a slice of *FieldMeta, encapsulating the metadata for a record.

func (Meta) Equalish added in v0.48.0

func (rm Meta) Equalish(other Meta) bool

Equalish returns true if rm and other are equal in the most relevant aspects. Specifically, they're equal if their field kinds are equal, and have the same munged names.

func (Meta) Kinds

func (rm Meta) Kinds() []kind.Kind

Kinds returns the data kinds for the record.

func (Meta) LogValue added in v0.41.0

func (rm Meta) LogValue() slog.Value

LogValue implements slog.LogValuer.

func (Meta) MungedNames added in v0.41.0

func (rm Meta) MungedNames() []string

MungedNames returns the munged column names, which may be the same as those returned from Meta.Names.

func (Meta) Names

func (rm Meta) Names() []string

Names returns the column names. These are the col names from the database. See also: MungedNames.

func (Meta) NewScanRow

func (rm Meta) NewScanRow() []any

NewScanRow returns a new []any that can be scanned into by sql.Rows.Scan.

func (Meta) ScanTypes

func (rm Meta) ScanTypes() []reflect.Type

ScanTypes returns the scan types for the record.

type Pair added in v0.48.0

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

Pair is a pair of records, typically used to represent matching records from two different sources, which are being compared. Either of the pair may be nil. The value return by Pair.Equal is calculated once at the time of construction; mutating the records after construction may make that value inaccurate.

func NewIdenticalPairs added in v0.48.0

func NewIdenticalPairs(row int, recs ...Record) []Pair

NewIdenticalPairs returns a slice of record.Pair where Pair.Rec1 and Pair.Rec2 are the same record. Pair.Equal returns true for each pair.

func NewPair added in v0.48.0

func NewPair(row int, rec1, rec2 Record) Pair

NewPair returns a new Pair of records. The value returned by Pair.Equal is calculated once in this constructor. Mutating the records after construction may make that value inaccurate.

func (Pair) Equal added in v0.48.0

func (p Pair) Equal() bool

Equal returns true if the records were equal at the time of the Pair's construction. Mutating the records after construction may make this value inaccurate. Two nil records are considered equal.

See: record.Equal.

func (Pair) Rec1 added in v0.48.0

func (p Pair) Rec1() Record

func (Pair) Rec2 added in v0.48.0

func (p Pair) Rec2() Record

func (Pair) Row added in v0.48.0

func (p Pair) Row() int

type Record

type Record []any

Record is a []any row of field values returned from a query.

In the codebase, we distinguish between a "Record" and a "ScanRow", although both are []any and are closely related.

An instance of ScanRow is passed to the sql rows.Scan method, and its elements may include implementations of the sql.Scanner interface such as sql.NullString, sql.NullInt64 or even driver-specific types.

A Record is typically built from a ScanRow, unwrapping and munging elements such that the Record only contains standard types:

nil, int64, float64, bool, string, []byte, time.Time

It is an error for a Record to contain elements of any other type.

func Clone added in v0.37.0

func Clone(rec Record) Record

Clone returns a deep copy of rec.

func CloneSlice added in v0.37.0

func CloneSlice(recs []Record) []Record

CloneSlice returns a deep copy of recs.

Jump to

Keyboard shortcuts

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