Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[package]
name = "kite_sql"
version = "0.1.3"
version = "0.1.4"
edition = "2021"
authors = ["Kould <kould2333@gmail.com>", "Xwg <loloxwg@gmail.com>"]
description = "SQL as a Function for Rust"
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -64,7 +66,13 @@ 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 iter {
println!("{:?}", tuple?);
}
```
Expand Down
11 changes: 10 additions & 1 deletion examples/wasm_hello_world.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 27 additions & 0 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ struct WasmRow {
values: Vec<DataValue>,
}

#[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()
}
Expand Down Expand Up @@ -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<JsValue, JsValue> {
let iter = self
.inner
.as_ref()
.ok_or_else(|| to_js_err("iterator already consumed"))?;
let columns: Vec<WasmSchemaColumn> = 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<JsValue, JsValue> {
Expand Down
Loading