Documentation
¶
Overview ¶
Package dbkit names database engines, their error classes and shared types, and lends capped shares of one handle.
Index ¶
- Variables
- func EscapeLike(term string) string
- func InTx(ctx context.Context, db *sql.DB, body func(*sql.Tx) error) error
- func SQLitePath(address string) (string, error)
- type Checkpoint
- type Engine
- type Function
- type FunctionList
- type Kind
- type Observer
- type QueryBudget
- type QueryCount
- type Row
- type Rows
- type Share
- func (s *Share) Begin(ctx context.Context) (*Tx, error)
- func (s *Share) Engine() Engine
- func (s *Share) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (s *Share) Query(ctx context.Context, query string, args ...any) (*Rows, error)
- func (s *Share) QueryRow(ctx context.Context, query string, args ...any) *Row
- type ShareOptions
- type Snapshot
- type Snapshotter
- type Statement
- type Time
- type Tx
- func (t *Tx) Commit() error
- func (t *Tx) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (t *Tx) Query(ctx context.Context, query string, args ...any) (*Rows, error)
- func (t *Tx) QueryRow(ctx context.Context, query string, args ...any) *Row
- func (t *Tx) Rollback() error
Constants ¶
This section is empty.
Variables ¶
var ( // ErrBusy is the class of an error from a lock that was not free within the engine's wait. ErrBusy = errors.New("dbkit: the database is busy") // ErrUnique is the class of an error from a unique or primary key constraint. ErrUnique = errors.New("dbkit: a unique constraint failed") // ErrForeignKey is the class of an error from a foreign key constraint. ErrForeignKey = errors.New("dbkit: a foreign key constraint failed") // ErrNotNull is the class of an error from a not null constraint. ErrNotNull = errors.New("dbkit: a not null constraint failed") // ErrCheck is the class of an error from a check constraint. ErrCheck = errors.New("dbkit: a check constraint failed") )
var ( ErrShareFull = errors.New("dbkit: no free slot in the share before the statement deadline") // ErrRefused marks a statement the observer refused. ErrRefused = errors.New("dbkit: the observer refused the statement") // ErrPlaceholder marks a SQLite statement with a question mark parameter, or a $N after a named parameter. ErrPlaceholder = errors.New("dbkit: write every parameter as $N") )
var ErrAddress = errors.New("dbkit: refused database address")
ErrAddress marks every database address EngineOf and SQLitePath refuse.
Functions ¶
func EscapeLike ¶ added in v0.2.0
EscapeLike returns term with every LIKE pattern character escaped by a backslash.
func InTx ¶ added in v0.3.0
InTx runs body in a write transaction on db, rolling back when body does not return nil and committing otherwise.
func SQLitePath ¶
SQLitePath returns the file path of a SQLite address as written.
Types ¶
type Checkpoint ¶
type Checkpoint struct {
// Busy reports that a reader or a writer stopped the checkpoint before it finished.
Busy bool
// LogFrames is the count of frames in the write-ahead log, or -1 when there is none.
LogFrames int
// CheckpointedFrames is the count of log frames written back into the database file, or -1 when there is none.
CheckpointedFrames int
// Took is how long the checkpoint ran.
Took time.Duration
}
Checkpoint reports one write-ahead log checkpoint.
type Engine ¶
type Engine int
Engine names the database engine an address selects.
type Function ¶
type Function struct {
// Name is the name SQL calls the function by, its ASCII letters matched in any case and every other letter exactly.
Name string
// Args is the count of arguments the function takes, or -1 for any count.
Args int
// Deterministic reports that the same arguments always give the same result.
Deterministic bool
// Call computes the result from the arguments, which stay valid only until it returns.
Call func(args []driver.Value) (driver.Value, error)
}
Function is one Go function an engine registers for SQL to call.
type FunctionList ¶
type FunctionList struct {
// contains filtered or unexported fields
}
FunctionList is a checked list of functions with no name repeated, its ASCII letters in any case.
func NewFunctionList ¶
func NewFunctionList(fns ...Function) (*FunctionList, error)
NewFunctionList returns a list of fns, or an error naming the first function it refuses.
func (*FunctionList) All ¶
func (l *FunctionList) All() []Function
All returns a copy of the functions in the list, or nil for a nil list.
type Observer ¶
Observer sees every statement and transaction start of a share before the driver, and refuses one by an error.
type QueryBudget ¶
type QueryBudget struct {
// contains filtered or unexported fields
}
QueryBudget hands each request a fresh statement counter with the same limit.
func NewQueryBudget ¶
func NewQueryBudget(limit int) (*QueryBudget, error)
NewQueryBudget returns a budget of limit statements per request.
type QueryCount ¶
type QueryCount struct {
// Total is the number of statements of every share.
Total int
// ByID is the number of statements of each share, keyed by its ID.
ByID map[string]int
// PastBudget reports whether Total is above the limit of the budget.
PastBudget bool
}
QueryCount is a copy of the statement counter of one request.
type Row ¶
type Row struct {
// contains filtered or unexported fields
}
Row is the result of QueryRow, holding its slot until Scan.
type Rows ¶
Rows is a *sql.Rows that gives its slot back exactly once, read within the statement timeout.
func (*Rows) Err ¶
Err returns the error met while reading, marked as a deadline error when the statement deadline stopped it.
func (*Rows) Next ¶
Next prepares the next row for Scan and gives the slot back once the last result set ends.
func (*Rows) NextResultSet ¶
NextResultSet moves to the next result set and gives the slot back when none follows.
type Share ¶
type Share struct {
// contains filtered or unexported fields
}
Share is a capped, timed and counted view of one database handle, lent to one part of an application.
func NewShare ¶
NewShare lends a capped share of an open handle. It opens nothing and sends nothing.
func (*Share) Begin ¶
Begin starts a transaction that ends at the transaction timeout at the latest.
type ShareOptions ¶
type ShareOptions struct {
ID string
Slots int
StatementTimeout time.Duration
TransactionTimeout time.Duration
Observer Observer
}
ShareOptions configures one share. Every number is required.
type Snapshot ¶
type Snapshot struct {
// Path is the file the copy was written to.
Path string
// Checkpoint is the write-ahead log checkpoint run after the copy.
Checkpoint Checkpoint
}
Snapshot describes one finished copy of a database.
type Snapshotter ¶
type Snapshotter interface {
// Snapshot writes a consistent copy of the database to target and reports the checkpoint run after it.
Snapshot(ctx context.Context, target string) (Snapshot, error)
}
Snapshotter writes a consistent copy of a database to a file.
type Statement ¶
type Statement struct {
// ID is the ID of the share.
ID string
// Kind is the call that sent the statement.
Kind Kind
// Query is the text as the caller wrote it, before any rewrite, and empty for Begin.
Query string
// Writes reports whether the text of the statement shows a write, always true for Begin.
Writes bool
// InTransaction reports whether the statement runs inside a transaction of the share.
InTransaction bool
// Count is the number of statements in the request so far, this one included and Begin never counted, 0 with none.
Count int
// PastBudget reports whether Count is above the budget of the request.
PastBudget bool
}
Statement is what the observer sees before a statement or a transaction start reaches the driver.
type Time ¶
Time is a time.Time stored as INTEGER UTC microseconds since the Unix epoch.
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx is a transaction of a share that holds one slot until Commit, Rollback or the end of its context.
func (*Tx) Commit ¶
Commit commits the transaction and gives its slot back, or rolls it back once its context has ended.
func (*Tx) Query ¶
Query runs a query that returns rows inside the transaction, read within the statement timeout.