SQLite for moondb: the vendored amalgamation on native, SQLite's official WebAssembly build on js, one driver for both.
Moved on mooncakes from Lfan-ke/moonsqlite to moonbitstack/moonsqlite.
| Target | Engine | Trait | Open with |
|---|---|---|---|
| native | vendored amalgamation, linked | @moondb.Driver | SqliteDriver::open |
| js | SQLite wasm build, loaded at run time | @moondb.Driver | SqliteDriver::open (async) |
async fn demo() -> Unit raise @moondb.DbError {
let db = @sqlite.SqliteDriver::open(":memory:") // await the engine, then it is a Driver
db.execute("CREATE TABLE t (id INTEGER PRIMARY KEY, n TEXT)", []) |> ignore
db.execute("INSERT INTO t (n) VALUES (?)", [@moondb.Text("a")]) |> ignore
let rows = db.query("SELECT n FROM t", [])
println(rows[0].text(0)) // a
db.close()
}sh scripts/fetch_engine.sh # prints the directory holding sqlite3.wasm
export MOONSQLITE_ENGINE=$(sh scripts/fetch_engine.sh)The package name is moonsqlite — no hyphen, so no alias is forced. Import it as @sqlite for brevity in moon.pkg: import { "moonbitstack/moonsqlite" @sqlite }, then reach it as @sqlite (as below). Value constructors come from @moondb (moon add moonbitstack/moondb).
// native only — this block links the vendored amalgamation.
fn demo() -> Unit raise @moondb.DbError {
let db = @sqlite.SqliteDriver::open(":memory:") // implements @moondb.Driver
db.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)", [])
|> ignore
// Bound params map to SQLite storage classes — never spliced into the SQL text.
db.execute("INSERT INTO users (name, age) VALUES (?, ?)", [
@moondb.Text("alice"), @moondb.Int(30),
]) |> ignore
// Result columns decode back into @moondb.Value by their runtime type.
let rows = db.query("SELECT name, age FROM users WHERE age > ?", [@moondb.Int(18)])
let _ = rows[0].text(0) // "alice"
let _ = rows[0].int(1) // 30
db.close()
}let sess = @moonorm.Session::new(@sqlite.SqliteDriver::open("app.db"))
sess.add(@moonorm.insert("users").set("name", @moondb.Text("bob"))) |> ignore
let rows = sess.fetch(@moonorm.select("users").where_("name", "=", @moondb.Text("bob")))| SQLite | ErrorKind | Python |
|---|---|---|
| SQLITE_CONSTRAINT, SQLITE_MISMATCH | Integrity | IntegrityError |
| SQLITE_ERROR, SQLITE_BUSY, SQLITE_LOCKED, SQLITE_READONLY, SQLITE_IOERR, … | Operational | OperationalError |
| SQLITE_MISUSE, SQLITE_RANGE | Interface | InterfaceError |
| SQLITE_TOOBIG | Data | DataError |
| SQLITE_CORRUPT, SQLITE_AUTH, SQLITE_NOTADB | Database | DatabaseError |
| SQLITE_INTERNAL, SQLITE_NOTFOUND, SQLITE_NOMEM | Internal | InternalError |
type SqliteCursorimpl Cursor for SqliteCursorpub struct SqliteDriver {
handle : Int64
closed : Bool
}impl Driver for SqliteDriverfn SqliteDriver::execute(self : SqliteDriver, sql : String, params : Array[Value]) -> ExecResult raise DbErrorfn SqliteDriver::query(self : SqliteDriver, sql : String, params : Array[Value]) -> Array[Row] raise DbErrorfn SqliteDriver::query_stream(self : SqliteDriver, sql : String, params : Array[Value]) -> &Cursor raise DbErrorpub struct SqliteStmt {
driver : SqliteDriver
st : Int64
text : String
slots : Array[String]
cols : Array[String]
closed : Bool
}type SqliteStmtCursorInstall
Download zipSQLite for moondb: the vendored amalgamation on native, SQLite's official WebAssembly build on js, one driver for both.