MoonBit · database contract
moondb
A trait is a constitution. moondb fixes the calling convention every MoonBit database backend implements — and every ORM builds against. Drivers ratify it; the spec stays small, and stable.
Article I — the contract
The Driver trait
Eight operations, plus one declaration. A backend implements
execute, query, the three
transaction brackets, close, and says which dialect it
speaks; ping, query_stream,
prepare and capabilities
come with sensible defaults. Values cross the boundary as
Value — never spliced into SQL — so binding is
injection-safe by construction.
pub(open) trait Driver { fn execute(Self, String, Array[Value]) -> ExecResult raise DbError fn query(Self, String, Array[Value]) -> Array[Row] raise DbError fn query_stream(Self, String, Array[Value]) -> &Cursor raise DbError = _ // default fn begin(Self) -> Unit raise DbError fn commit(Self) -> Unit raise DbError fn rollback(Self) -> Unit raise DbError fn close(Self) -> Unit fn ping(Self) -> Bool = _ // default fn prepare(Self, String) -> &Stmt raise DbError = _ // default fn dialect(Self) -> Dialect fn capabilities(Self) -> Capabilities = _ // default }
Above the backend sit Stmt (prepare once, bind again),
Txn (a closure that nests through savepoints),
rewrite_named (a :name lexer
that knows each dialect's spelling) and Executor (the
view that runs SQL but cannot close the connection).
Article II — the ratified implementations
Drivers
moondb is the law; these are the ratified amendments. Each is a package that
implements Driver against one backend. Their docs live
as sub-pages of this site.
moonmysql
The MySQL/MariaDB implementation of the contract.
- prepared
- named params
- savepoints
- multi-result
- unsigned
moonpostgres
The PostgreSQL implementation of the contract.
- prepared + cache
- server cursor
- COPY ⇄
- savepoints
- TLS · SCRAM
moonsqlite
The SQLite implementation of the contract — embedded, no server.
- prepared
- named params
- savepoints
- RETURNING