bindings/wasm: Add Database.prepare() and Statement.all()

This adds `Database.prepare()` and `Statement.all()` APIs to the Wasm
bindings so that you can actually get something useful out of the
library.
This commit is contained in:
Pekka Enberg
2024-11-13 12:15:18 +02:00
parent 1f228db99f
commit b7ca2c9823
4 changed files with 63 additions and 19 deletions

28
Cargo.lock generated
View File

@@ -1,6 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
version = 4
[[package]]
name = "addr2line"
@@ -1016,9 +1016,9 @@ checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
[[package]]
name = "js-sys"
version = "0.3.69"
version = "0.3.72"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d"
checksum = "6a88f1bda2bd75b0452a14784937d796722fdebfe50df998aeb3f0b7603019a9"
dependencies = [
"wasm-bindgen",
]
@@ -1110,6 +1110,7 @@ name = "limbo-wasm"
version = "0.0.4"
dependencies = [
"console_error_panic_hook",
"js-sys",
"limbo_core",
"wasm-bindgen",
]
@@ -2314,19 +2315,20 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasm-bindgen"
version = "0.2.92"
version = "0.2.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8"
checksum = "128d1e363af62632b8eb57219c8fd7877144af57558fb2ef0368d0087bddeb2e"
dependencies = [
"cfg-if",
"once_cell",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.92"
version = "0.2.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da"
checksum = "cb6dd4d3ca0ddffd1dd1c9c04f94b868c37ff5fac97c30b97cff2d74fce3a358"
dependencies = [
"bumpalo",
"log",
@@ -2339,9 +2341,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.92"
version = "0.2.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726"
checksum = "e79384be7f8f5a9dd5d7167216f022090cf1f9ec128e6e6a482a2cb5c5422c56"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
@@ -2349,9 +2351,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.92"
version = "0.2.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68"
dependencies = [
"proc-macro2",
"quote",
@@ -2362,9 +2364,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.92"
version = "0.2.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96"
checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d"
[[package]]
name = "web-sys"

View File

@@ -12,5 +12,6 @@ path = "lib.rs"
[dependencies]
console_error_panic_hook = "0.1.7"
js-sys = "0.3.72"
limbo_core = { path = "../../core", default-features = false }
wasm-bindgen = "0.2"

View File

@@ -2,4 +2,8 @@ import { Database } from 'limbo-wasm';
const db = new Database('hello.db');
console.log(db.exec("SELECT 'hello, world' AS message"));
const stmt = db.prepare('SELECT * FROM users');
const users = stmt.all();
console.log(users);

View File

@@ -6,7 +6,8 @@ use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub struct Database {
_inner: Rc<limbo_core::Database>,
db: Rc<limbo_core::Database>,
conn: Rc<limbo_core::Connection>,
}
#[allow(clippy::arc_with_non_send_sync)]
@@ -18,12 +19,48 @@ impl Database {
let file = io.open_file(path, limbo_core::OpenFlags::None).unwrap();
let page_io = Rc::new(DatabaseStorage::new(file));
let wal = Rc::new(RefCell::new(Wal {}));
let inner = limbo_core::Database::open(io, page_io, wal).unwrap();
Database { _inner: inner }
let db = limbo_core::Database::open(io, page_io, wal).unwrap();
let conn = db.connect();
Database { db, conn }
}
#[wasm_bindgen]
pub fn exec(&self, _sql: &str) {}
#[wasm_bindgen]
pub fn prepare(&self, _sql: &str) -> Statement {
let stmt = self.conn.prepare(_sql).unwrap();
Statement {
inner: RefCell::new(stmt),
}
}
}
#[wasm_bindgen]
pub struct Statement {
inner: RefCell<limbo_core::Statement>,
}
#[wasm_bindgen]
impl Statement {
pub fn all(&self) -> js_sys::Array {
let array = js_sys::Array::new();
loop {
match self.inner.borrow_mut().step() {
Ok(limbo_core::RowResult::Row(row)) => {
let row_array = js_sys::Array::new();
for value in row.values {
row_array.push(&JsValue::from_str(&value.to_string()));
}
array.push(&row_array);
}
Ok(limbo_core::RowResult::IO) => todo!(),
Ok(limbo_core::RowResult::Done) => break,
Err(e) => panic!("Error: {:?}", e),
}
}
array
}
}
pub struct File {
@@ -202,7 +239,7 @@ impl limbo_core::Wal for Wal {
}
fn should_checkpoint(&self) -> bool {
todo!()
false
}
fn append_frame(
@@ -224,7 +261,7 @@ impl limbo_core::Wal for Wal {
}
fn sync(&mut self) -> Result<limbo_core::CheckpointStatus> {
todo!()
Ok(limbo_core::CheckpointStatus::Done)
}
}