table

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2023 License: Zlib Imports: 6 Imported by: 0

README

table

Buffer database/sql query results into an in-memory table.

t, err := table.NewBuffer(ctx, db, "select ID, Name from Account;")
if err != nil {
	return err
}
for _, row := range t.Rows {
	id := row.Get("ID").(int64)
	name := row.Get("Name").(string)
	// ...
}

Documentation

Overview

package table creates table buffers for results from database/sql.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BufferToStruct added in v0.0.7

func BufferToStruct[T any](buf *Buffer) ([]T, error)

Copy Buffer into a slice of structs of type T. Names can be provided in `sql:"Name"` field tags. If a field should be ignored, use the `sql:"-"` tag. Pointer to structs or points to fields are not supported.

TODO: add option to set value converter.

func NewScaler

func NewScaler(ctx context.Context, q Queryer, sql string, params ...any) (any, error)

NewScaler returns the first field in the first row.

func QueryStruct added in v0.0.7

func QueryStruct[T any](ctx context.Context, q Queryer, text string, params ...any) ([]T, error)

Query into a struct slice.

Types

type Buffer

type Buffer struct {
	Columns []string
	Rows    []Row
	// contains filtered or unexported fields
}

Buffer is a result within memory.

func NewBuffer

func NewBuffer(ctx context.Context, q Queryer, sql string, params ...any) (table *Buffer, err error)

NewBuffer returns a new single table buffer.

func (*Buffer) AddRow added in v0.0.7

func (b *Buffer) AddRow(row []any)

Add a new row to an existing Buffer.

func (*Buffer) Get added in v0.0.5

func (t *Buffer) Get(rowIndex int, columnName string) any

Get the field from the row index and named column.

type IndexError

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

Error returned when attempting to access a row or column which does not exist.

func (*IndexError) Error

func (tie *IndexError) Error() string

type Queryer added in v0.0.5

type Queryer interface {
	QueryContext(ctx context.Context, sql string, params ...any) (*sql.Rows, error)
}

type Row

type Row struct {
	Field []any
	// contains filtered or unexported fields
}

Row hold field level data.

func NewRow added in v0.0.5

func NewRow(ctx context.Context, q Queryer, sql string, params ...any) (Row, error)

NewRow returns the first row.

func (Row) Get added in v0.0.5

func (r Row) Get(columnName string) any

Get the field from the named column.

func (Row) MarshalJSON

func (r Row) MarshalJSON() ([]byte, error)

func (*Row) UnmarshalJSON

func (r *Row) UnmarshalJSON(bb []byte) error

type Set

type Set []*Buffer

Set stores a list of Buffers.

func FillSet

func FillSet(ctx context.Context, rows *sql.Rows) (Set, error)

FillSet will take a sql query result and fill the buffer with the entire result set.

func NewSet

func NewSet(ctx context.Context, q Queryer, sql string, params ...any) (Set, error)

NewSet returns a set of table buffers from the given query.

Jump to

Keyboard shortcuts

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