std.sqlz - ZZ
SQLite foundation (canonical module name sqlz), plus PostgreSQL and MySQL wire-protocol drivers, and unified closure transactions for all backends.
import std.sqlzPurpose
Section titled “Purpose”std.sqlz is the only SQLite module. open(path) -> db, exec(db, sql) -> int (rows changed), query(db, sql) -> [T] (rows mapped to structs), close(db). The SQL param is checked by verify_sql_params (Fmt segments become ?N bound params); query’s return unifies with the caller’s annotation (let users: [User] = sqlz.query(...)) so the runtime can map columns positionally into that struct. Handle variables named db (db := sqlz.open(..); db.exec(..)) are method calls.
Postgres: import std.sqlz.postgres as pg — pg.connect, pg.exec, pg.query, pg.close. MySQL: import std.sqlz.mysql as my — same shape. Both wire-protocol drivers; MySQL uses binary prepared statements.
Transactions: sqlz.transaction(db, fn(tx) { ... }) — BEGIN, run closure, COMMIT on .ok(value), ROLLBACK on .err(message).
Quickstart
Section titled “Quickstart”import std.sqlz
struct User { id: int, name: str }
func main() -> Result<int, str> { db := sqlz.open("app.db") sqlz.exec(db, "CREATE TABLE IF NOT EXISTS users (id INTEGER, name TEXT)") sqlz.exec(db, "INSERT INTO users VALUES (1, 'Ada')")
users := sqlz.query(db, "SELECT id, name FROM users") for u in users { println(u.name) }
sqlz.close(db) return .ok(0)}Transactions
Section titled “Transactions”import std.sqlz
func main() -> Result<int, str> { db := sqlz.open("app.db")
result := sqlz.transaction(db, |tx| -> int { sqlz.exec(tx, "UPDATE users SET name = 'Bob' WHERE id = 1") return 42 }) // COMMIT on .ok, ROLLBACK on .err return .ok(0)}Method syntax
Section titled “Method syntax”import std.sqlz
func main() -> Result<int, str> { db := sqlz.open("app.db") _ = db.exec("CREATE TABLE ...") _rows := db.query("SELECT * FROM users") db.close() return .ok(0)}Gotchas
Section titled “Gotchas”sqlzis the only SQLite module — there is nostd.sqlite.- SQL is checked at compile time via
verify_sql_params; Fmt segments become bound params. queryreturn type unifies with the caller’s annotation for positional column mapping — annotate your struct.- No
db.transaction— usesqlz.transactiononly. - Postgres/MySQL are wire-protocol drivers — not embedded; require a running server.
Production patterns
Section titled “Production patterns”import std.sqlz
func main() -> Result<int, str> { db := sqlz.open("app.db")
// Transaction with rollback on error result := sqlz.transaction(db, |tx| -> int { sqlz.exec(tx, "INSERT INTO ledger ...") sqlz.exec(tx, "UPDATE balance ...") return 0 }) match result { .ok(_) => println("committed"), .err(e) => println("rolled back: {e}"), } return .ok(0)}API reference
Section titled “API reference”13 entries. Signatures come from the compiler’s own FuncSig table, so they match the runtime exactly; descriptions come from the module guide.
Full index: API reference · Standard library