moondb

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.

waxing crescent

moonmysql

The MySQL/MariaDB implementation of the contract.

  • prepared
  • named params
  • savepoints
  • multi-result
  • unsigned
  • long data
read the statute →
waxing gibbous

moonpostgres

The PostgreSQL implementation of the contract.

  • prepared + cache
  • server cursor
  • COPY ⇄
  • savepoints
  • TLS · SCRAM
  • binary results
read the statute →
waning crescent

moonchdb

The ClickHouse implementation — analytical, over HTTP.

  • RowBinary
  • parameters
  • arrays
  • nullable
  • no transactions
read the statute →
waxing gibbous

moonsqls

The SQL Server implementation — TDS, spoken rather than wrapped.

  • sp_prepare
  • bulk copy
  • TDS 8.0
  • savepoints
  • OUTPUT
read the statute →
waxing crescent

moonduckdb

DuckDB, embedded — the engine is loaded, not compiled.

  • columnar
  • prepare
  • transactions
  • no savepoints
read the README →
waning gibbous

moonckdb

CockroachDB — PostgreSQL's wire, its own capabilities.

  • server_of
  • same twelve cases
  • no LISTEN
read the README →
first quarter

moontidb

TiDB — MySQL's wire, without the stored procedures.

  • server_of
  • same twelve cases
  • no multi-result sets
read the README →
waxing gibbous

moonsqls

SQL Server — TDS 7.4 in pure MoonBit, TLS through moontls.

  • prelogin
  • login7
  • sp_executesql
  • token stream
read the README →
full moon

moonsqlite

The SQLite implementation of the contract — embedded, no server.

  • prepared
  • named params
  • savepoints
  • RETURNING
read the statute →

Article III — the bridge and the audit

Two shapes, one contract

A backend that answers inside a C call implements the synchronous Driver; the code above it may be written against the asynchronous one. @blocking.Blocking is the seam between them — a delegation, with the driver's own prepared handle, its live cursor and its real dialect carried across intact.

And a contract nobody checks is a suggestion. @conformance turns it into a case matrix — connect, bind and read back, prepare, transact, nest, classify a failure — that every backend answers the same way, as a report rather than a stack trace.

moondb · the interface layer Apache-2.0 targets: wasm-gc · js · native trait Driver · value Value · error DbError
Implement a driver Read the full API