Skip to main content

Module sql

Module sql 

Source
Expand description

The application’s own SQLite databases: its tables, joins, reports and transactions, in sql/<name>.db. SQL stays SQL; TinyStore does the work around it: the file and its connections, migrations checked at every open, writes from many callers sharing a commit, and values that come back as they went in.

let db = store.database("app").migrations("migrations").open()?;
db.exec(sql!("insert into notes (id, author_id, title) values (?, ?, ?)", "0192f2a4", user_id, "Buy milk"))?;
let mine: Vec<Note> = db.all(sql!("select id, title, done from notes where author_id = ?", user_id))?;

docs/guides/sql/README.md is the guide: the TypeScript, Python and Go spellings, and why each call is as it is.

Structs§

Database
An application’s database: its reads, its writes, its transactions. Clones share the database, which closes with its store.
DatabaseBuilder
How a database opens; Store::database makes one.
Done
What a write changed: the rows, and SQLite’s rowid of the row it inserted, 0 when it inserted none. The rowid is a table’s key only for an integer primary key.
Page
A page of a query’s rows, and the cursor that reads the next; none after the last.
Query
A query on a table: a value, each call returning a new one, so that the first stays as it was. all, one, scalar, count and list read; update and delete write.
Sql
A statement’s text and the values of its ?s, which are always values and never SQL, so a value cannot inject any. sql! makes one:
Table
A table: its queries, and its rows’ inserts and upserts. Derefs to the query of all its rows.
Tx
A database’s transaction, from Database::tx: its reads see its writes, and a call that fails leaves it as it was before the call.
Upsert
What an upsert does when the row’s key is taken: Upsert::on("id").update(["title"]).only_if(sql::eq("author_id", &user_id)).
Write
A write of a batch: a statement, a job’s add of a queue opened from the database, queue.adding(&job), or an event’s append of a stream opened from it, audit.appending(name, &fields). A statement becomes one by into:

Enums§

Direction
The way an order goes.
Migrations
A database’s migrations: a directory of .sql files named 0001_notes.sql, 0002_tags.sql, applied in the order of their numbers, or the files themselves by name, for a program that carries them in its binary:

Functions§

and
Conditions joined with and, in parentheses, for a group inside an or; an empty and finds everything.
eq
A column equal to a value, its name quoted; None is is null.
has
A JSON list in column holding value.
ident
A name, quoted, never SQL: a column a request chooses.
is_in
A column holding one of values: in (…), written as one value SQLite reads with json_each, so a list of any length is the same statement and an empty one finds nothing.
json
A value kept as its JSON.
list
A list for in (…), as one JSON value SQLite reads with json_each.
or
Conditions joined with or, in parentheses; an empty or finds nothing.
value
A value as a piece of SQL, for a column an update sets.