From 1c8aa75664c80c4cb12b8d899db202da12dcad50 Mon Sep 17 00:00:00 2001 From: kould Date: Sun, 14 Dec 2025 14:07:06 +0800 Subject: [PATCH 1/3] feat: public schema() on WASM --- README.md | 11 ++++++++++- examples/wasm_hello_world.test.mjs | 11 ++++++++++- src/wasm.rs | 27 +++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2960b3ab..f86c2e6e 100755 --- a/README.md +++ b/README.md @@ -55,6 +55,8 @@ console.log(rows.map((r) => r.values.map((v) => v.Int32 ?? v))); ## Examples ```rust +use kite_sql::db::{DataBaseBuilder, ResultIter}; + let kite_sql = DataBaseBuilder::path("./data").build()?; kite_sql @@ -64,9 +66,16 @@ kite_sql .run("insert into t1 values(0, 0), (1, 1)")? .done()?; -for tuple in kite_sql.run("select * from t1")? { +let mut iter = kite_sql.run("select * from t1")?; + +// Query schema is available on every result iterator. +let column_names: Vec<_> = iter.schema().iter().map(|c| c.name()).collect(); +println!("columns: {column_names:?}"); + +for tuple in &mut iter { println!("{:?}", tuple?); } +iter.done()?; ``` 👉**more examples** diff --git a/examples/wasm_hello_world.test.mjs b/examples/wasm_hello_world.test.mjs index 34ecd2d0..64a2db61 100644 --- a/examples/wasm_hello_world.test.mjs +++ b/examples/wasm_hello_world.test.mjs @@ -12,7 +12,16 @@ async function main() { await db.execute("create table my_struct (c1 int primary key, c2 int)"); await db.execute("insert into my_struct values(0, 0), (1, 1)"); - const rows = db.run("select * from my_struct").rows(); + const iter = db.run("select * from my_struct"); + const schema = iter.schema(); + assert.deepEqual( + schema.map(({ name, datatype, nullable }) => ({ name, datatype, nullable })), + [ + { name: "c1", datatype: "Integer", nullable: false }, + { name: "c2", datatype: "Integer", nullable: true }, + ], + ); + const rows = iter.rows(); assert.equal(rows.length, 2, "should return two rows"); const [first, second] = rows; diff --git a/src/wasm.rs b/src/wasm.rs index c56e040f..5d54ac61 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -13,6 +13,13 @@ struct WasmRow { values: Vec, } +#[derive(Serialize)] +struct WasmSchemaColumn { + name: String, + datatype: String, + nullable: bool, +} + fn to_js_err(err: impl ToString) -> JsValue { js_sys::Error::new(&err.to_string()).into() } @@ -89,6 +96,26 @@ impl WasmResultIter { } } + /// Returns the output schema as an array of `{ name, datatype, nullable }`. + #[wasm_bindgen(js_name = schema)] + pub fn schema(&self) -> Result { + let iter = self + .inner + .as_ref() + .ok_or_else(|| to_js_err("iterator already consumed"))?; + let columns: Vec = iter + .schema() + .iter() + .map(|col| WasmSchemaColumn { + name: col.name().to_string(), + datatype: col.datatype().to_string(), + nullable: col.nullable(), + }) + .collect(); + serde_wasm_bindgen::to_value(&columns) + .map_err(|e| to_js_err(format!("serialize schema: {e}"))) + } + /// Collect all remaining rows into an array and finish the iterator. #[wasm_bindgen(js_name = rows)] pub fn rows(&mut self) -> Result { From 57eeddb5e5d86bff0a24ebad3d220f122bf7a189 Mon Sep 17 00:00:00 2001 From: kould Date: Sun, 14 Dec 2025 14:15:57 +0800 Subject: [PATCH 2/3] chore: 0.1.4 --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 45d97f1b..c035bca6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1320,7 +1320,7 @@ dependencies = [ [[package]] name = "kite_sql" -version = "0.1.3" +version = "0.1.4" dependencies = [ "ahash 0.8.12", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index 6635a96d..b195c947 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ [package] name = "kite_sql" -version = "0.1.3" +version = "0.1.4" edition = "2021" authors = ["Kould ", "Xwg "] description = "SQL as a Function for Rust" From 6aa9c7f0769733a122265128cc35cf55a4d35e85 Mon Sep 17 00:00:00 2001 From: kould Date: Sun, 14 Dec 2025 14:47:25 +0800 Subject: [PATCH 3/3] chore: fix typo --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index f86c2e6e..d62ab8ab 100755 --- a/README.md +++ b/README.md @@ -72,10 +72,9 @@ let mut iter = kite_sql.run("select * from t1")?; let column_names: Vec<_> = iter.schema().iter().map(|c| c.name()).collect(); println!("columns: {column_names:?}"); -for tuple in &mut iter { +for tuple in iter { println!("{:?}", tuple?); } -iter.done()?; ``` 👉**more examples**