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.
CoreBound
func CoreBound(s BoundStatement) (db core.Database, text string, values []any, err error)CoreBound is a bound statement's database, text and values, or why it could not be made, as this module's packages send it: a stream's Where; a program has no use for it.
CoreStatement
func CoreStatement(p Piece) (text string, values []any, err error)CoreStatement is a piece's text and values, or why it could not be made, as this module's packages send it: a table's index's Where; a program has no use for it.
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: an export holds a part, not every row, however many there are. The server reads them a part at a time from one snapshot, which lasts five seconds at most: a walk that takes longer fails Limit, and is read a page at a time with a table's List, or ordered by its key with a table's Each, which reads on past it. Inside a transaction it reads 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.
BoundStatement
type BoundStatement struct {
// contains filtered or unexported fields
}BoundStatement is a statement bound to the database that runs it, from DB.Statement: what a stream's Where keeps a key to, as SQL's in (select …) does. A stream lives in records.db and belongs to no database, so the statement says whose it is. Nothing runs until a query reads it.
Changed
type Changed[T any] struct {
Before T
After T
}Changed is a row that changed: as the table held it, and as the source gave it.
Column
type Column struct {
Name string
Type string
}Column is a column a statement answers: its name as its rows name it, and the type its table declares for it where it is a table's own column, as SQLite keeps it, INTEGER or varchar(80); empty for an expression.
Describe
func Describe(ctx context.Context, db *DB, text string, args ...any) ([]Column, error)Describe is the columns a statement answers, in its order, whether or not it answers a row: the server compiles it and runs nothing. Text alone is described with its ?s left without values; a statement that writes is invalid.
columns, err := sqldb.Describe(ctx, db, "select id, title from notes where author_id = ?")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 writes known before they run as one: all of them or none, in a shared commit. A write is a statement, or a job's add of a queue opened from the database, its Adding, which commits with the rows.
err = db.Batch(ctx, sqldb.SQL(`update notes set body = ? where id = ?`, body, id), indexing.Adding(Index{NoteID: id}))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.Statement
func (db *DB) Statement(text string, args ...any) BoundStatementStatement is a statement bound to this database, for a stream's Where, which keeps a key to the values its one column gives: nothing runs until a query reads it, once as the query starts, on a reader of this file.
teachers := db.Statement("select id from teachers")
n, err := events.Count(ctx, records.Query{Since: 24 * time.Hour, Where: records.Where{"userId": teachers}})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.
Diff
type Diff[T any] struct {
Added []T
Changed []Changed[T]
Gone []T
Same int
Suspect bool
}Diff is what a mirror's call changed; a Suspect one wrote nothing.
Direction
type Direction stringDirection is an order's direction.
Asc
const (
Asc Direction = "asc"
Desc Direction = "desc"
)The directions an order takes.
EditOption
type EditOption interface {
// contains filtered or unexported methods
}EditOption is how an Update or a Delete writes: IfVersion.
IfVersion
func IfVersion(version int64) EditOptionIfVersion writes only if every row the conditions match is at version, all of them, and otherwise nothing, failing with tinystore.ErrConflict and the version a row is at. No row at all is no conflict: the edit changes none.
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.
Mirror
type Mirror[T any] struct {
// contains filtered or unexported fields
}Mirror is a table that mirrors another system: its Replace takes the source's list of a scope and its Upsert a change of some rows, and each answers what was added, changed and gone. A row the list gives as it is held is not written at all.
OpenMirror
func OpenMirror[T any](db *DB, table string, options MirrorOptions) *Mirror[T]OpenMirror is the table name of db mirroring another system, its rows Ts:
works := sqldb.OpenMirror[Work](db, "works", sqldb.MirrorOptions{Key: []string{"course_id", "id"}, Gone: "gone_at"})
diff, err := works.Replace(ctx, map[string]any{"course_id": 1}, fetched)Mirror.Begin
func (m *Mirror[T]) Begin(scope map[string]any, options ...ReplaceOption) *Run[T]Begin begins a Replace of the scope whose list comes in parts: each Add gives a part, and Commit makes the scope what every part listed, in one write, so that only what no part named is gone. Nothing is sent until the first Add or Commit. A run never committed writes nothing: closed, idle for five minutes, its connection lost or the store closed; defer run.Close(ctx) lets go of one not committed.
run := works.Begin(map[string]any{"course_id": 1})
defer run.Close(ctx)
for page := range pages {
if err := run.Add(ctx, page); err != nil { … }
}
diff, err := run.Commit(ctx)Mirror.Replace
func (m *Mirror[T]) Replace(ctx context.Context, scope map[string]any, rows []T, options ...ReplaceOption) (Diff[T], error)Replace makes the scope, the rows whose columns hold its values, nil for the whole table, what rows lists: it adds what the table lacks, writes what changed, and deletes, or marks gone, what the list leaves out. A row outside the scope is invalid, and so is one whose key holds a null. Values compare as the table keeps them: 4 given to a text column is "4", and a key's text compares by its unique index's collation. A list, or an answer, past one message goes in the parts of a run, as Begin makes one, and writes in one commit all the same.
Mirror.Upsert
func (m *Mirror[T]) Upsert(ctx context.Context, rows []T) (Diff[T], error)Upsert writes what rows adds and changes, a delta of the source's; its Gone is always empty.
MirrorOptions
type MirrorOptions struct {
// Key is the columns that name a row at the source: a unique index or the
// table's primary key covers them.
Key []string
// Gone is a column that marks what the source dropped with the store's
// time instead of deleting it; empty deletes it.
Gone string
// GoneUnit is the unit Gone marks the store's time in: Millis when
// empty, or Seconds.
GoneUnit Unit
// ChangeOn is the columns whose change makes a row changed; every column
// the rows carry but the key's when nil.
ChangeOn []string
}MirrorOptions is how a mirror opens: what names a row at the source, what marks it gone, and what makes it changed.
Option
type Option interface {
// contains filtered or unexported methods
}Option is how a database opens.
Backup
func Backup(taken bool) OptionBackup says whether backups take the database. Backup(false) keeps its file and the bytes of its files in local/<name>.db rather than sql/, where no backup looks, open or not, and no node copies it: each node that opens it writes its own. A name lives in one folder: one whose file lies in the other is ErrInvalid, naming both.
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, opts ...EditOption) (Result, error)Delete deletes the rows the conditions match, and says how many it deleted. On a table with a deleted column it marks the live ones instead, with the store's time in the mark's unit, and raises a versioned table's version; IfVersion is Update's.
Query.DeletedOnly
func (q *Query[T]) DeletedOnly() *Query[T]DeletedOnly is the query of the rows the deleted column marks, and of no other.
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: for more than fits in memory. The server reads them from one snapshot of five seconds at most; ordered by columns by their names, a unique one last, as List is, it reads on past that after the last row it sent, and any other walk fails Limit there. Inside a transaction it reads the rows All does.
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.Purge
func (q *Query[T]) Purge(ctx context.Context) (Result, error)Purge deletes for good the rows the conditions match of those the query shows: the marked ones of DeletedOnly, or every one of WithDeleted. A query that shows the live rows alone refuses it, so that nothing live is purged by a query that forgot to ask.
Query.Restore
func (q *Query[T]) Restore(ctx context.Context) (Result, error)Restore clears the mark of the marked rows the conditions match, whichever rows the query shows, and says how many came back; a versioned table's version goes up by one.
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, opts ...EditOption) (Result, error)Update sets columns of the rows the conditions match, and says how many changed. A versioned table's version goes up by one, and with IfVersion the update writes only rows at that version.
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.
Query.WithDeleted
func (q *Query[T]) WithDeleted() *Query[T]WithDeleted is the query of every row, those the deleted column marks among them.
ReplaceOption
type ReplaceOption func(*float64)ReplaceOption guards a Replace.
MinKept
func MinKept(fraction float64) ReplaceOptionMinKept writes nothing, and answers Suspect, when the list would leave fewer than fraction of the scope's live rows live.
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.
Run
type Run[T any] struct {
// contains filtered or unexported fields
}Run is a mirror's Replace whose list comes in parts, from Begin. Its parts wait on the server, outside the table, until Commit. A run is its connection's: every call of it goes on the one it began on, never again on another, since another holds none of its parts.
Run.Add
func (r *Run[T]) Add(ctx context.Context, rows []T) errorAdd gives a part of the list: rows of the first part's shape, inside the scope, each key once in the whole run. A part past one message goes as several. A part the server refuses ends the run.
Run.Close
func (r *Run[T]) Close(ctx context.Context) errorClose lets go of a run not committed, which writes nothing; after Commit, or before anything was sent, it sends nothing.
Run.Commit
func (r *Run[T]) Commit(ctx context.Context) (Diff[T], error)Commit makes the scope what every part listed, in one write, and answers what changed.
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 write of a batch: a piece of SQL, or a queue's Adding.
CoreLent
func CoreLent(write core.Lent) StatementCoreLent is how this module's packages put a write of their own in a batch, as a queue's Adding and a stream's Appending do; a program has no use for it.
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.
OpenTableNew in 0.13.1
func OpenTable[T any](r Runner, name string, opts ...TableOption) *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.
tasks := sqldb.OpenTable[Task](db, "tasks", sqldb.Deleted("deleted_at"), sqldb.Version("version"))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. On a versioned table the row starts at version 0, whatever it gives for the column.
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. On a table with a deleted column the update clears a row's mark, since the row came back, and on a versioned one it raises the version.
Table.UpsertMany
func (t *Table[T]) UpsertMany(ctx context.Context, rows []T, opts ...UpsertOption) (Result, error)UpsertMany upserts rows of one shape as one statement, all of them or none, each as Upsert does one: its Changes is the rows it wrote, without those whose key Where kept. SQLite refuses a list that holds one key twice.
TableOption
type TableOption interface {
// contains filtered or unexported methods
}TableOption is how a table opens: Deleted, Version.
DeletedNew in 0.13.1
func Deleted(column string, unit ...Unit) TableOptionDeleted marks a row deleted rather than deleting it: column, a nullable integer, holds when, by the store's clock, in milliseconds, or in seconds with sqldb.Deleted("deleted_at", sqldb.Seconds). Every query and write of the table passes the marked rows by, unless WithDeleted or DeletedOnly asks for them, and Delete marks rather than deletes.
Version
func Version(column string) TableOptionVersion counts a row's versions in column, an integer, not null, default 0: every update of the table raises it by one, and an edit with IfVersion writes only rows at the version it names.
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.
Unit
type Unit stringUnit is what a mark's number counts since 1970: Seconds, as SQLite's unixepoch() gives them, or Millis.
MillisNew in 0.13.1
const (
Millis Unit = "ms"
Seconds Unit = "s"
)The units a mark keeps the store's time in.
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.