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
#|);
),
)
assert_eq(create.step(), false)
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)
assert_eq(insert.step(), false)
insert.finalize()
let query = conn.prepare(
(
#|SELECT id, name, score FROM users;
),
)
assert_true(query.step())
let id : Int64 = query.column(index=0)
let name : String = query.column(index=1)
let score : Double = query.column(index=2)
assert_eq(id, 1L)
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
#|);
),
)
assert_eq(create.step(), false)
create.finalize()
let insert1 = conn.prepare(
(
#|INSERT INTO files (id, payload) VALUES (?, ?);
),
)
insert1.bind(index=1, 1)
insert1.bind(index=2, b"abc")
assert_eq(insert1.step(), false)
insert1.finalize()
let insert2 = conn.prepare(
(
#|INSERT INTO files (id, payload) VALUES (?, ?);
),
)
insert2.bind(index=1, 2)
insert2.bind(index=2, b"xyz")
assert_eq(insert2.step(), false)
insert2.finalize()
let query = conn.prepare(
(
#|SELECT id, payload FROM files ORDER BY id;
),
)
assert_true(query.step())
let first_id : Int64 = query.column(index=0)
let first_payload : Bytes = query.column(index=1)
assert_eq(first_id, 1L)
assert_eq(first_payload, b"abc")
assert_true(query.step())
let second_id : Int64 = query.column(index=0)
let second_payload : Bytes = query.column(index=1)
assert_eq(second_id, 2L)
assert_eq(second_payload, b"xyz")
assert_eq(query.step(), false)
query.finalize()
conn.close()
}///|
test "error handling" {
let conn = @sqlite3.Connection::open(":memory:")
let error = @test.expect_error(() => conn.prepare("SELECT FROM"))
match error {
@sqlite3.SqliteError(code~, extended~, msg~) => {
assert_eq(code, @sqlite3.ErrorCode::Error)
assert_eq(extended, None)
assert_true(msg.length() > 0)
}
}
conn.close()
}| MoonBit type | Bound as SQLite | Read from SQLite as |
|---|---|---|
| Int | INTEGER | — |
| Int64 | INTEGER | Int64 |
| Double | REAL | Double |
| String | TEXT | String |
| StringView | TEXT | — |
| Bytes | BLOB | Bytes |
| BytesView | BLOB | — |
| Value | Exact SQLite storage class, including NULL | Value |
///|
test "dynamic value" {
let conn = @sqlite3.Connection::open(":memory:")
let stmt = conn.prepare("SELECT ?")
stmt.bind(index=1, @sqlite3.Value::Null)
assert_true(stmt.step())
let value : @sqlite3.Value = stmt.column(index=0)
assert_eq(value, @sqlite3.Value::Null)
stmt.finalize()
conn.close()
}moon check
moon test
moon info
moon fmttrait Columnpub suberror SqliteError {
SqliteError(code~ : ErrorCode, extended~ : ExtendedCode?, msg~ : String)
} derive(Eq, Debug)type Connectionpub(all) enum ExtendedCode {
ErrorMissingCollSeq
ErrorRetry
ErrorSnapshot
IoErrRead
IoErrShortRead
IoErrWrite
IoErrFsync
IoErrDirFsync
IoErrTruncate
IoErrFstat
IoErrUnlock
IoErrReadLock
IoErrDelete
IoErrBlocked
IoErrNoMem
IoErrAccess
IoErrCheckReservedLock
IoErrLock
IoErrClose
IoErrDirClose
IoErrShmOpen
IoErrShmSize
IoErrShmLock
IoErrShmMap
IoErrSeek
IoErrDeleteNoEntry
IoErrMmap
IoErrGetTempPath
IoErrConvertPath
IoErrVNode
IoErrAuth
IoErrBeginAtomic
IoErrCommitAtomic
IoErrRollbackAtomic
IoErrData
IoErrCorruptFs
IoErrInPage
LockedSharedCache
LockedVTab
BusyRecovery
BusySnapshot
BusyTimeout
CantOpenNoTempDir
CantOpenIsDir
CantOpenFullPath
CantOpenConvertPath
CantOpenDirtyWal
CantOpenSymlink
CorruptVTab
CorruptSequence
CorruptIndex
ReadOnlyRecovery
ReadOnlyCantLock
ReadOnlyRollback
ReadOnlyDbMoved
ReadOnlyCantInit
ReadOnlyDirectory
AbortRollback
ConstraintCheck
ConstraintCommitHook
ConstraintForeignKey
ConstraintFunction
ConstraintNotNull
ConstraintPrimaryKey
ConstraintTrigger
ConstraintUnique
ConstraintVTab
ConstraintRowId
ConstraintPinned
ConstraintDatatype
NoticeRecoverWal
NoticeRecoverRollback
NoticeRbu
WarningAutoIndex
AuthUser
Unknown(Int)
} derive(Eq, Debug)type StatementA lightweight, low-level SQLite3 binding for MoonBit, providing a thin interface over the SQLite3 C API.