store

package
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package store defines the database execution abstraction used by core tools.

DB and Tx are minimal interfaces satisfied by *sql.DB / *sql.Tx via thin adapters in x/providers. Core packages depend on these interfaces so they can be tested with hand-written fakes and remain free of any database driver import. Iter turns a Rows stream into a range-friendly iterator for zero-materialization streaming of large result sets.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoBeginFn = errors.New("store: no BeginFn configured")

ErrNoBeginFn is returned when a FakeDB has no BeginFn configured.

View Source
var ErrNoExecFn = errors.New("store: no ExecFn configured")

ErrNoExecFn is returned when a FakeDB has no ExecFn configured.

View Source
var ErrNoQueryFn = errors.New("store: no QueryFn configured")

ErrNoQueryFn is returned when a FakeDB has no QueryFn configured.

View Source
var ErrScanBeforeNext = errors.New("store: Scan called before Next")

ErrScanBeforeNext is returned when Scan is called before Next.

Functions

func Iter

func Iter(rows Rows) iter.Seq2[Row, error]

Iter returns a sequence yielding decoded rows and a terminal error. It closes the Rows when iteration ends — whether by exhausting the cursor, an early break, or a terminal error — so callers need not close it themselves. Use it with range over func to stream large result sets without materializing them.

Types

type DB

type DB interface {
	QueryContext(ctx context.Context, query string, args ...any) (Rows, error)
	ExecContext(ctx context.Context, query string, args ...any) (Result, error)
}

DB is the minimal database interface core tools depend on. *sql.DB satisfies it via a thin adapter in x/providers; tests use hand-written fakes.

type FakeDB

type FakeDB struct {
	QueryFn func(ctx context.Context, query string, args ...any) (Rows, error)
	ExecFn  func(ctx context.Context, query string, args ...any) (Result, error)
	BeginFn func(ctx context.Context, opts *TxOptions) (Tx, error)

	Queries []LoggedQuery
	Execs   []LoggedExec
	TxBegun int
}

FakeDB is a hand-written DB and TxBeginner for tests. Configure the *Fn fields to control responses; calls are recorded for assertion.

func (*FakeDB) BeginTx

func (d *FakeDB) BeginTx(ctx context.Context, opts *TxOptions) (Tx, error)

func (*FakeDB) ExecContext

func (d *FakeDB) ExecContext(ctx context.Context, query string, args ...any) (Result, error)

func (*FakeDB) QueryContext

func (d *FakeDB) QueryContext(ctx context.Context, query string, args ...any) (Rows, error)

type FakeRows

type FakeRows struct {
	Cols []string
	Rows [][]any
	// contains filtered or unexported fields
}

FakeRows is a hand-written Rows implementation for tests.

func NewFakeRows

func NewFakeRows(cols []string, rows ...[]any) *FakeRows

NewFakeRows builds a FakeRows with the given columns and rows.

func (*FakeRows) Close

func (r *FakeRows) Close() error

func (*FakeRows) Closed

func (r *FakeRows) Closed() bool

Closed reports whether Close was called.

func (*FakeRows) Columns

func (r *FakeRows) Columns() ([]string, error)

func (*FakeRows) Err

func (r *FakeRows) Err() error

func (*FakeRows) Next

func (r *FakeRows) Next() bool

func (*FakeRows) Scan

func (r *FakeRows) Scan(dest ...any) error

func (*FakeRows) SetErr

func (r *FakeRows) SetErr(err error)

SetErr sets the terminal error returned by Err and stops iteration.

type FakeTx

type FakeTx struct {
	Committed   bool
	RolledBack  bool
	Savepoints  []string
	RollbacksTo []string
}

FakeTx is a hand-written Tx for tests.

func (*FakeTx) Commit

func (t *FakeTx) Commit() error

func (*FakeTx) ExecContext

func (t *FakeTx) ExecContext(_ context.Context, _ string, _ ...any) (Result, error)

func (*FakeTx) QueryContext

func (t *FakeTx) QueryContext(_ context.Context, _ string, _ ...any) (Rows, error)

QueryContext and ExecContext on FakeTx defer to package-level helpers; tests usually configure a shared FakeDB for tx behavior. For simplicity they return zero values unless overridden via fields.

func (*FakeTx) Rollback

func (t *FakeTx) Rollback() error

func (*FakeTx) RollbackTo

func (t *FakeTx) RollbackTo(_ context.Context, sp Savepoint) error

func (*FakeTx) Savepoint

func (t *FakeTx) Savepoint(_ context.Context, name string) (Savepoint, error)

type IsolationLevel

type IsolationLevel uint8

IsolationLevel names a transaction isolation level.

const (
	// LevelReadUncommitted allows dirty reads.
	LevelReadUncommitted IsolationLevel = iota
	// LevelReadCommitted is the default for most databases.
	LevelReadCommitted
	// LevelRepeatableRead prevents non-repeatable reads.
	LevelRepeatableRead
	// LevelSerializable is the strictest isolation.
	LevelSerializable
)

type LoggedExec

type LoggedExec struct {
	Query string
	Args  []any
}

LoggedExec records an ExecContext call.

type LoggedQuery

type LoggedQuery struct {
	Query string
	Args  []any
}

LoggedQuery records a QueryContext call.

type Prepared

type Prepared interface {
	QueryContext(ctx context.Context, args ...any) (Rows, error)
	ExecContext(ctx context.Context, args ...any) (Result, error)
	Close() error
}

Prepared is a reusable statement.

type PreparedDB

type PreparedDB struct {
	DB
	// contains filtered or unexported fields
}

PreparedDB uses an optional bounded prepared-statement cache while preserving the DB interface. A non-Preparer DB is passed through unchanged.

func WithPreparedCache

func WithPreparedCache(db DB, max int) *PreparedDB

WithPreparedCache wraps db. max <= 0 disables preparing.

func (*PreparedDB) Close

func (d *PreparedDB) Close() error

Close clears all prepared statements. It does not close the wrapped DB.

func (*PreparedDB) ExecContext

func (d *PreparedDB) ExecContext(ctx context.Context, query string, args ...any) (Result, error)

ExecContext implements DB.

func (*PreparedDB) QueryContext

func (d *PreparedDB) QueryContext(ctx context.Context, query string, args ...any) (Rows, error)

QueryContext implements DB.

type Preparer

type Preparer interface {
	PrepareContext(ctx context.Context, query string) (Prepared, error)
}

Preparer is an optional DB capability. Existing DB fakes need not implement it.

type Result

type Result struct {
	LastInsertID int64
	RowsAffected int64
}

Result summarizes a write execution.

type Row

type Row = map[string]any

Row is a single decoded result row keyed by column name.

type Rows

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

Rows is a forward-only cursor over query results. Callers must Close it, typically with defer. It mirrors the subset of *sql.Rows used by core tools.

type Savepoint

type Savepoint struct{ Name string }

Savepoint names a savepoint within a transaction.

type Tx

type Tx interface {
	QueryContext(ctx context.Context, query string, args ...any) (Rows, error)
	ExecContext(ctx context.Context, query string, args ...any) (Result, error)
	Commit() error
	Rollback() error
	Savepoint(ctx context.Context, name string) (Savepoint, error)
	RollbackTo(ctx context.Context, sp Savepoint) error
}

Tx is a transaction boundary. ACID is an inseparable part of the relational model; tools may execute multiple writes within a Tx. Every Tx must end in Commit or Rollback (invariant I10).

type TxBeginner

type TxBeginner interface {
	BeginTx(ctx context.Context, opts *TxOptions) (Tx, error)
}

TxBeginner begins a transaction. *sql.DB satisfies it via an adapter.

type TxOptions

type TxOptions struct {
	Isolation IsolationLevel
	ReadOnly  bool
}

TxOptions configures a transaction.

Jump to

Keyboard shortcuts

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