Skip to content

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.sqlz

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).

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)
}
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)
}
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)
}
  • sqlz is the only SQLite module — there is no std.sqlite.
  • SQL is checked at compile time via verify_sql_params; Fmt segments become bound params.
  • query return type unifies with the caller’s annotation for positional column mapping — annotate your struct.
  • No db.transaction — use sqlz.transaction only.
  • Postgres/MySQL are wire-protocol drivers — not embedded; require a running server.
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)
}

13 entries. Signatures come from the compiler’s own FuncSig table, so they match the runtime exactly; descriptions come from the module guide.

Function Signature Description
std.sqlz.close close(db: db) -> unit Close DB
std.sqlz.exec exec(db: db, sql: str) -> int Execute, rows changed
std.sqlz.mysql.close close(db: db) -> unit Close
std.sqlz.mysql.connect connect(conninfo: str) -> db URL or keyword form
std.sqlz.mysql.exec exec(db: db, sql: str) -> int Rows changed
std.sqlz.mysql.query query<T>(db: db, sql: str) -> [T] Rows mapped to structs
std.sqlz.open open(path: str) -> db Open SQLite DB
std.sqlz.postgres.close close(db: db) -> unit Close
std.sqlz.postgres.connect connect(conninfo: str) -> db URL or keyword form
std.sqlz.postgres.exec exec(db: db, sql: str) -> int Rows changed
std.sqlz.postgres.query query<T>(db: db, sql: str) -> [T] Rows mapped to structs
std.sqlz.query query<T>(db: db, sql: str) -> [T] Query rows mapped to structs
std.sqlz.transaction transaction<T>(db: db, f: func(db) -> T) -> Result<T, str> BEGIN/commit/rollback closure

Full index: API reference · Standard library