Documentation channel: current development main at compiler checkpoint 1d7e15e. The latest tagged release is v0.0.3; pages identify APIs that are not yet released.

SQLite

The official SQLite package surface uses the system/native SQLite library rather than embedding SQLite into the Strut compiler or runtime. Connections are deterministic resources and close automatically when their last handle is released, or explicitly with close().

The current SQLite API is selected automatically when these functions are used; do not add include <sqlite>;. Operations use the checked SqliteError type, and native compilation requires the SQLite development package.

Open a database

function main() -> int : SqliteError {
    db := sqlite_open("app.db");
    db.close();
    return 0;
}

Execute statements

Parameter arrays are bound through SQLite prepared statements. Prefer parameters over constructing SQL strings manually.

db.exec("CREATE TABLE IF NOT EXISTS users(id INTEGER, name TEXT)");

db.exec(
    "INSERT INTO users VALUES (?, ?)",
    json.parse("[1,\"Nick\"]")
);

Query rows

query() currently returns a JSON array of row objects, giving a useful dynamic mapping while typed row mapping continues to evolve.

rows := db.query(
    "SELECT id, name FROM users WHERE id = ?",
    json.parse("[1]")
);

print(json.stringify(rows));

Transactions

The scoped transaction helper commits when the callback succeeds and rolls back if it throws.

db.transaction(() => {
    db.exec("UPDATE accounts SET balance = balance - 10 WHERE id = 1");
    db.exec("UPDATE accounts SET balance = balance + 10 WHERE id = 2");
});

Blocking policy

SQLite calls are blocking. Put database work inside an async task when it should run away from the caller; Strut does not pretend SQLite itself is natively asynchronous.