A lightweight, low-level SQLite3 binding for MoonBit, providing a thin interface over the SQLite3 C API.
moon add moonbit-community/sqlite3///|
test "quick start" {
let conn = @sqlite3.Connection::open(":memory:")
let create = conn.prepare(
(
#|CREATE TABLE users (
#| id INTEGER PRIMARY KEY,
#| name TEXT NOT NULL,
#| score REAL NOT NULL
#|);
),
)
create.step_once()
create.finalize()
let insert = conn.prepare(
(
#|INSERT INTO users (id, name, score) VALUES (?, ?, ?);
),
)
insert.bind(index=1, 1)
insert.bind(index=2, "alice")
insert.bind(index=3, 98.5)
insert.step_once()
insert.finalize()
let query = conn.prepare(
(
#|SELECT id, name, score FROM users;
),
)
assert_true(query.step())
let id : Int = query.column(index=0)
let name : String = query.column(index=1)
let score : Double = query.column(index=2)
assert_eq(id, 1)
assert_eq(name, "alice")
assert_eq(score.to_int(), 98)
assert_eq(query.step(), false)
query.finalize()
conn.close()
}///|
test "blob round trip" {
let conn = @sqlite3.Connection::open(":memory:")
let create = conn.prepare(
(
#|CREATE TABLE files (
#| id INTEGER PRIMARY KEY,
#| payload BLOB NOT NULL
#|);
),
)
create.step_once()
create.finalize()
let insert1 = conn.prepare(
(
#|INSERT INTO files (id, payload) VALUES (?, ?);
),
)
insert1.bind(index=1, 1)
insert1.bind(index=2, b"abc")
insert1.step_once()
insert1.finalize()
let insert2 = conn.prepare(
(
#|INSERT INTO files (id, payload) VALUES (?, ?);
),
)
insert2.bind(index=1, 2)
insert2.bind(index=2, b"xyz")
insert2.step_once()
insert2.finalize()
let query = conn.prepare(
(
#|SELECT id, payload FROM files ORDER BY id;
),
)
assert_true(query.step())
let first_id : Int = query.column(index=0)
let first_payload : Bytes = query.column(index=1)
assert_eq(first_id, 1)
assert_eq(first_payload, b"abc")
assert_true(query.step())
let second_id : Int = query.column(index=0)
let second_payload : Bytes = query.column(index=1)
assert_eq(second_id, 2)
assert_eq(second_payload, b"xyz")
assert_eq(query.step(), false)
query.finalize()
conn.close()
}///|
test "error handling" {
let conn = @sqlite3.Connection::open(":memory:")
@test.assert_raise(() => conn.prepare("SELECT FROM"))
assert_true(conn.get_errmsg().length() > 0)
conn.close()
}| MoonBit type | Bound as SQLite | Read from SQLite as |
|---|---|---|
| Int | INTEGER | Int |
| Int64 | INTEGER | Int64 |
| Double | REAL | Double |
| String | TEXT | String |
| Bytes | BLOB | Bytes |
moon check
moon test
moon info
moon fmttrait Bindfn bind(stmt : Statement, param_index : Int, value : Int, loc~ : SourceLoc) -> Unit raise SqliteErrorfn bind(stmt : Statement, param_index : Int, value : Int64, loc~ : SourceLoc) -> Unit raise SqliteErrorfn bind(stmt : Statement, param_index : Int, value : Double, loc~ : SourceLoc) -> Unit raise SqliteErrorfn bind(stmt : Statement, param_index : Int, value : String, loc~ : SourceLoc) -> Unit raise SqliteErrortrait Columntype Connection#callsite(autofill(loc))
fn Connection::close(self : Connection, loc~ : SourceLoc) -> Unit raise SqliteError#callsite(autofill(loc))
fn Connection::open(filename : String, loc~ : SourceLoc) -> Connection raise SqliteError#callsite(autofill(loc))
fn Connection::prepare(self : Connection, stmt : String, loc~ : SourceLoc) -> Statement raise SqliteErrortype Statement#callsite(autofill(loc))
fn[T : Bind] Statement::bind(self : Statement, index~ : Int, val : T, loc~ : SourceLoc) -> Unit raise SqliteError#callsite(autofill(loc))
fn Statement::finalize(self : Statement, loc~ : SourceLoc) -> Unit raise SqliteError#callsite(autofill(loc))
fn Statement::step(self : Statement, loc~ : SourceLoc) -> Bool raise SqliteError#callsite(autofill(loc))
fn Statement::step_once(self : Statement, loc~ : SourceLoc) -> Unit raise SqliteErrorA lightweight, low-level SQLite3 binding for MoonBit, providing a thin interface over the SQLite3 C API.