Crate sqlite_wasm_rs

Source
Expand description

§SQLite Wasm Rust

Wrap the official sqlite-wasm, and expect to provide a usable C-like API.

And currently the following APIs are implemented and tested:

123456
sqlite3_open_v2sqlite3_execsqlite3_closesqlite3_close_v2sqlite3_changessqlite3_deserialize
sqlite3_serializesqlite3_freesqlite3_create_function_v2sqlite3_result_textsqlite3_result_blobsqlite3_result_int
sqlite3_result_int64sqlite3_result_doublesqlite3_result_nullsqlite3_column_valuesqlite3_column_countsqlite3_column_name
sqlite3_bind_nullsqlite3_bind_blobsqlite3_bind_textsqlite3_value_freesqlite3_value_bytessqlite3_value_text
sqlite3_value_blobsqlite3_value_intsqlite3_value_int64sqlite3_value_doublesqlite3_value_typesqlite3_value_dup
sqlite3_bind_doublesqlite3_bind_intsqlite3_bind_int64sqlite3_create_collation_v2sqlite3_extended_errcodesqlite3_finalize
sqlite3_stepsqlite3_errmsgsqlite3_db_handlesqlite3_resetsqlite3_prepare_v3sqlite3_context_db_handle
sqlite3_user_datasqlite3_aggregate_contextsqlite3_result_error

§Usage

use sqlite_wasm_rs::c as ffi;
use std::ffi::CString;

async fn open_db() -> anyhow::Result<()> {
    // Before using CAPI, you must initialize sqlite.
    //
    // Initializing the database is a one-time operation during
    // the life of the program.
    ffi::init_sqlite().await?;

    let mut db = std::ptr::null_mut();
    let filename = CString::new("mydb").unwrap();
    // Persistent Storage is supported, use opfs vfs.
    // This support is only available when sqlite is loaded from a
    // Worker thread, whether it's loaded in its own dedicated worker
    // or in a worker together with client code.
    //
    // See <https://sqlite.org/wasm/doc/trunk/persistence.md#opfs>
    let vfs = CString::new("opfs").unwrap();
    let ret = unsafe {
        ffi::sqlite3_open_v2(
            filename.as_ptr(),
            &mut db as *mut _,
            ffi::SQLITE_OPEN_READWRITE | ffi::SQLITE_OPEN_CREATE,
            // Using std::ptr::null() is a memory DB
            vfs.as_ptr(),
        )
    };
    assert_eq!(ffi::SQLITE_OK, ret);
}

§About TEST

This project was successfully used in diesel, and diesel’s integration tests and unit tests all run successfully (except for a few tests that required std::fs::* ), see sqlitest.gif.

  • sqlite-wasm: SQLite Wasm conveniently wrapped as an ES Module.
  • sqlite-web-rs: A SQLite WebAssembly backend for Diesel.
  • rusqlite: Ergonomic bindings to SQLite for Rust.

Modules§

  • This module provides some C-Like interfaces from sqlite-wasm.
  • This module is copied from libsqlite-sys for compatibility with various ORM libraries, such as diesel
  • This module provides some “raw” C-Like interfaces for sqlite-wasm. It is called “raw” because the memory needs to be handled by yourself. It is not recommended to use it directly, please use the c module.

Structs§

Enums§

Functions§