Reference / SQL API for Go
SQL API for Go
Every public type, function and constant of the SQL engine in tinyshed.org/tinystore/sqldb, generated from its source. The SQL guide explains how to use them, and the Bun and Node, Python and Rust pages list the same API.
All
func All[T any](ctx context.Context, r Runner, text string, args ...any) ([]T, error)All is every row a statement gives, read into T: a struct, a map, or one column's value.
Columns
func Columns[T any](ctx context.Context, db *DB, text string, args ...any) (T, error)Columns is what All gives, by its columns: T is a struct whose fields are slices, one a column, by the column's name; a nil pointer where a row holds NULL. For rows by the thousand, a chart's points: they come faster, and nobody turns rows into columns after.
Each
func Each[T any](ctx context.Context, r Runner, text string, args ...any) iter.Seq2[T, error]Each is the rows a statement gives, one at a time as their parts arrive, from one snapshot; inside a transaction, all of them at once.
One
func One[T any](ctx context.Context, r Runner, text string, args ...any) (T, bool, error)One is the one row a statement gives, and whether it gave one; two are Invalid, and a write that gave them rolls back.
ReadRow
func ReadRow[T any](columns []string, cells []any) (T, error)ReadRow is a row of these columns and cells as a query reads it into T: a struct by its fields' columns, a map, or a single column's value. A table's search index reads its hits' rows with it.
Scalar
func Scalar[T any](ctx context.Context, r Runner, text string, args ...any) (T, error)Scalar is the one value of the one row a statement gives, as count(*) does; T's zero for NULL.
Condition
type Condition interface {
// contains filtered or unexported methods
}Condition is a condition a query keeps to: equal values by column, Eq, or a piece of SQL.
DB
type DB struct {
// contains filtered or unexported fields
}DB is an application's database. The verb says what comes back: all the rows, one or none, one value, or what a write changed. A write with returning goes through a read's verb and runs on the writer, its rows given once it is durable.
Open
func Open(ctx context.Context, store *tinystore.Store, name string, opts ...Option) (*DB, error)Open opens the application's database name, sql/<name>.db, once its migrations are applied and checked: a migration that fails, or one changed after it was applied, is told here, at the start. Without migrations the file opens as it is, an empty one when there is none.
DB.Batch
func (db *DB) Batch(ctx context.Context, statements ...Statement) errorBatch runs statements known before they run as one: all of them or none, in a shared commit.
DB.CoreHome
func (db *DB) CoreHome() core.HomeOfCoreHome is how this module's packages reach the database as a handle's home; a program has no use for it.
DB.Describe
func (db *DB) Describe() stringDescribe is the database as an error names it: sql app.
DB.Exec
func (db *DB) Exec(ctx context.Context, text string, args ...any) (Result, error)Exec runs a write once it is durable; writes from many callers share a commit, each failing alone.
DB.Handle
func (db *DB) Handle(ctx context.Context, c *core.Connection) (uint64, error)Handle is the database's handle on a connection, opened there first.
DB.Keep
func (db *DB) Keep(method uint16, body []byte, make func(ctx context.Context, c *core.Connection) ([]byte, error)) *core.OpenerKeep is the opener of a handle kept in the database's file: the one made first for the same open, so that its transaction opens each once, before it holds the writer.
DB.Name
func (db *DB) Name() stringName is the database's name.
DB.Tx
func (db *DB) Tx(ctx context.Context, fn func(tx *Tx) error) errorTx runs fn in a transaction that holds the writer, for writes that depend on what its reads found: an error fn returns rolls it back, and is Tx's. It runs once, five seconds at most, past which the store rolls it back; inside it, call nothing that waits on the world.
DB.Via
func (db *DB) Via(string) core.ViaVia is how a handle kept in the database's file reaches the store outside a transaction.
Direction
type Direction stringDirection is an order's direction.
Asc
const (
Asc Direction = "asc"
Desc Direction = "desc"
)The directions an order takes.
Eq
type Eq map[string]anyEq is equal values by column, written in the order of the columns' names: nil is is null, and a slice in (...).
ListOption
type ListOption interface {
// contains filtered or unexported methods
}ListOption is how a page of rows reads.
After
func After(next string) ListOptionAfter reads the page after the one whose Next it is.
Limit
func Limit(n int) ListOptionLimit is how many rows a page holds: 100 unless given.
Option
type Option interface {
// contains filtered or unexported methods
}Option is how a database opens.
Durability
func Durability(level string) OptionDurability is how far a commit of the database goes before it returns: "full" or "os"; the store's own unless given.
MigrationFiles
func MigrationFiles(byName map[string]string) OptionMigrationFiles are a database's migrations as their text by their names.
Migrations
func Migrations(fsys fs.FS) OptionMigrations are a database's migrations: the .sql files at the root of fsys, named 0001_notes.sql, applied in the order of their names and checked at every open. An embed.FS of migrations/*.sql is fs.Sub(migrations, "migrations").
Order
type Order struct {
Column string
Direction Direction
}Order is an order a request chooses by name: sqldb.Order{"created_at", sqldb.Desc}.
Page
type Page[T any] struct {
Rows []T
Next string
}Page is a page of a query's rows, and the cursor that reads the next: "" after the last.
Piece
type Piece struct {
// contains filtered or unexported fields
}Piece is a piece of SQL and the values of its ?s, which are always values and never SQL: SQL makes one, and a call, a batch and a table's query take it.
And
func And(conditions ...Condition) PieceAnd is conditions joined with and, in parentheses; an empty and finds everything.
Has
func Has(name string, value any) PieceHas is a JSON list in column name holding value.
Ident
func Ident(name string) PieceIdent is a name, quoted, never SQL: a column a request chooses.
JSON
func JSON(value any) PieceJSON is a value kept as its JSON.
List
func List[T any](values []T) PieceList is a list for in (...), as one value SQLite reads with json_each: any length, the same statement.
Or
func Or(conditions ...Condition) PieceOr is conditions joined with or, in parentheses; an empty or finds nothing.
SQL
func SQL(text string, values ...any) PieceSQL is a piece of SQL from text and the values of its ?s; a piece or a query in a ?'s place is written in: sqldb.SQL("exists ?", paid).
Piece.Text
func (p Piece) Text() stringText is the piece's SQL, and Values the values of its ?s.
Piece.Values
func (p Piece) Values() []anyValues are the values of the piece's ?s, as they leave.
Query
type Query[T any] struct {
// contains filtered or unexported fields
}Query is a query on a table: a value, each call giving a new one, so that the first stays as it was. All, One, Count, Each and List read; Update and Delete write.
Select
func Select[R any](q queried, columns string) *Query[R]Select is a query's columns, as SQL text, and the type of the row they make.
Query.All
func (q *Query[T]) All(ctx context.Context) ([]T, error)All is every row the query reads.
Query.Count
func (q *Query[T]) Count(ctx context.Context) (int64, error)Count counts every row the conditions match, without the order and the limit.
Query.Delete
func (q *Query[T]) Delete(ctx context.Context) (Result, error)Delete deletes the rows the conditions match, and says how many it deleted.
Query.Each
func (q *Query[T]) Each(ctx context.Context) iter.Seq2[T, error]Each is the rows one at a time, as their parts arrive.
Query.GroupBy
func (q *Query[T]) GroupBy(columns ...string) *Query[T]GroupBy groups the rows by columns, quoted as names.
Query.Having
func (q *Query[T]) Having(conditions ...Condition) *Query[T]Having is a condition on the groups, as Where takes one.
Query.Include
func (q *Query[T]) Include(name string, query queried) *Query[T]Include gives each row the rows of another query as a list under name, from the same statement: a field of that name holds them. The query names the row it belongs to in its where, by this table's alias, names its columns with Select, and has a limit, which counts for each row. Every value comes back as it is kept.
Query.Join
func (q *Query[T]) Join(table string, on any) *Query[T]Join joins a table, "users as u", on a condition as SQL text or a piece of SQL.
Query.LeftJoin
func (q *Query[T]) LeftJoin(table string, on any) *Query[T]LeftJoin joins a table whose columns may be NULL where no row of it meets the condition.
Query.Limit
func (q *Query[T]) Limit(rows int) *Query[T]Limit is how many rows the query reads at most.
Query.List
func (q *Query[T]) List(ctx context.Context, opts ...ListOption) (Page[T], error)List is a page after the last row of the one before, by the query's order, which ends in a unique column so that no row is read twice or skipped; Next reads the next page, and only of this query.
Query.Offset
func (q *Query[T]) Offset(rows int) *Query[T]Offset is how many rows the query skips.
Query.One
func (q *Query[T]) One(ctx context.Context) (T, bool, error)One is the one row the query reads, and whether it read one; two are Invalid.
Query.OrderBy
func (q *Query[T]) OrderBy(column any, direction Direction) *Query[T]OrderBy orders the rows by a column, quoted as a name, or by a piece of SQL: OrderBy(sqldb.SQL("lower(title)"), sqldb.Asc).
Query.SQL
func (q *Query[T]) SQL() (string, []any, error)SQL is the text and the values the query sends, to read or to log.
Query.Update
func (q *Query[T]) Update(ctx context.Context, set Set) (Result, error)Update sets columns of the rows the conditions match, and says how many changed.
Query.Where
func (q *Query[T]) Where(conditions ...Condition) *Query[T]Where adds conditions with and: equal values, sqldb.Eq{"status": "paid"}, nil as is null and a slice as in (...); or a piece of SQL from sqldb.SQL.
Result
type Result struct {
Changes int64
LastInsertRowid int64
}Result is what a write changed: the rows, and SQLite's rowid of the row it inserted, 0 when none.
Row
type Row = map[string]anyRow is a row as SQLite gave it, by its columns' names.
Runner
type Runner interface {
// contains filtered or unexported methods
}Runner reads and writes a database: the database itself, or a transaction of it.
Set
type Set map[string]anySet is the columns an update sets: values, or pieces of SQL.
Statement
type Statement = PieceStatement is a statement of a batch: a piece of SQL.
Table
type Table[T any] struct {
*Query[T]
// contains filtered or unexported fields
}Table is a table: its queries, and its rows' inserts and upserts. Its rows are T, a struct whose fields are the table's columns.
OpenTable
func OpenTable[T any](r Runner, name string) *Table[T]OpenTable is a table of a database, or of a transaction of it, "orders" or "orders as o", typed where it opens. It costs nothing until its first call.
Table.Insert
func (t *Table[T]) Insert(ctx context.Context, row T) (T, error)Insert inserts a row and gives it back as the file keeps it: the defaults and the generated columns the database filled.
Table.InsertMany
func (t *Table[T]) InsertMany(ctx context.Context, rows []T) ([]T, error)InsertMany inserts rows of one shape as one write, all of them or none, and gives them back as the file keeps them.
Table.Name
func (t *Table[T]) Name() stringName is the table's name.
Table.Upsert
func (t *Table[T]) Upsert(ctx context.Context, row T, opts ...UpsertOption) (Result, error)Upsert inserts a row, or, when the columns Conflict names are taken, sets the columns Update names on the row that holds them, when that row meets Where: its Changes is 0 when Where kept it.
Tx
type Tx struct {
// contains filtered or unexported fields
}Tx is a database's transaction, from db.Tx. Its reads see its writes, a call that fails leaves it as it was before the call, and its calls go one at a time, in order. A bucket, a queue or a channel takes it in with In: bucket.In(tx), and what they write commits with the rows.
Tx.CoreTx
func (tx *Tx) CoreTx() core.TxOfCoreTx is how this module's packages take a handle into the transaction; a program has no use for it.
Tx.Database
func (tx *Tx) Database() core.DatabaseDatabase is the database the transaction is of.
Tx.Exec
func (tx *Tx) Exec(ctx context.Context, text string, args ...any) (Result, error)Exec runs a write in the transaction.
Tx.Nested
func (tx *Tx) Nested(ctx context.Context, method uint16, body []byte) ([]byte, error)Nested is a call of kv, jobs or channels in the transaction: its method, its request, and the method's answer.
Tx.Via
func (tx *Tx) Via() core.ViaVia is how a handle's calls go inside the transaction, on its stream.
UpsertOption
type UpsertOption interface {
// contains filtered or unexported methods
}UpsertOption is what an upsert says: the columns whose taken values make it an update, the columns that update sets, and the condition the row that holds them must meet.
Conflict
func Conflict(columns ...string) UpsertOptionConflict names the columns whose values, when taken, make an upsert an update.
Update
func Update(columns ...string) UpsertOptionUpdate names the columns an upsert's update sets from the row.
Where
func Where(c Condition) UpsertOptionWhere is the condition the row that holds an upsert's key must meet, its owner's, so that an id from a request changes nothing of another's row.