The standard database-access interface for MoonBit — the driver↔query-layer contract, transliterated from Go's database/sql/driver and Python's DB-API 2.0. ORMs (moonorm) build on it; drivers (moon-sqlite / moon-postgres / moon-mysql) implement it.
flowchart TD
subgraph layers["query layers — build on moondb"]
ORM["moonorm<br/><small>SQLAlchemy-style ORM / query builder</small>"]
APP["your app / repository code"]
end
IFACE(["<b>@moondb</b><br/><small>Value · Row · ExecResult · DbError · Driver</small>"])
subgraph drivers["drivers — implement moondb"]
SQLITE["moon-sqlite<br/><small>C-FFI, native</small>"]
PG["moon-postgres<br/><small>pure-MoonBit wire</small>"]
MYSQL["moon-mysql<br/><small>pure-MoonBit wire</small>"]
MOCK["MockDriver<br/><small>in-memory, ships here</small>"]
end
APP --> ORM --> IFACE
IFACE --> SQLITE
IFACE --> PG
IFACE --> MYSQL
IFACE --> MOCKmoon add Lfan-ke/moondb| Type | Role | Transliterated from |
|---|---|---|
| Value | one dialect-neutral cell — the unit crossing the boundary both ways | Go driver.Value, DB-API type objects |
| Row | one result row: aligned column names + Values, with typed accessors | sql.Rows / DB-API row tuple |
| ExecResult | outcome of a non-query: rows affected + last insert id | Go sql.Result, DB-API rowcount/lastrowid |
| DbError | the one error every operation raises | DB-API exception hierarchy (flattened) |
| Driver | the trait a backend implements / a query layer targets | Go driver.Conn+Execer+Queryer |
| MockDriver | dependency-free in-memory reference driver, for tests | — |
test "round-trip through the interface" {
// A query layer is written against the `Driver` trait, not a concrete backend.
let db = @moondb.MockDriver::new()
// Bind values as parameters — never string-interpolated into the SQL.
db.execute("INSERT INTO hero (id, name) VALUES (?, ?)", [Int(1), Text("Nova")])
|> ignore
db.begin()
db.execute("INSERT INTO hero (id, name) VALUES (?, ?)", [Int(2), Text("Iris")])
|> ignore
db.rollback() // Iris is undone; Nova remains.
let rows = db.query("SELECT id, name FROM hero", [])
assert_eq(rows.length(), 1)
assert_eq(rows[0].int_by("c0"), 1)
assert_eq(rows[0].text_by("c1"), "Nova")
}pub impl @moondb.Driver for MyConn with execute(self, sql, params) {
// ... bind `params` positionally, run `sql`, then:
{ rows_affected: n, last_insert_id: id }
}
// query / begin / commit / rollback / close likewise.moon test --target allpub(all) suberror DbError {
ConnectError(String)
QueryError(String)
TypeError(String)
Closed
}impl Driver for MockDriverpub(all) enum Value {
Null
Bool(Bool)
Int(Int)
Int64(Int64)
Double(Double)
Text(String)
Blob(Bytes)
} derive(Eq)The standard database-access interface for MoonBit — the driver↔query-layer contract, transliterated from Go's database/sql/driver and Python's DB-API 2.0. ORMs (moonorm) build on it; drivers (moon-sqlite / moon-postgres / moon-mysql) implement it.