mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-25 02:21:37 +08:00
Merge pull request #1332 from etehtsea/wasi-example
Improve wasm32-wasi support
This commit is contained in:
commit
c2fbd167de
28
examples/persons/README.md
Normal file
28
examples/persons/README.md
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
# Persons example
|
||||||
|
|
||||||
|
## Run
|
||||||
|
|
||||||
|
```
|
||||||
|
$ cargo run --example persons
|
||||||
|
```
|
||||||
|
|
||||||
|
## Run (wasm32-wasi)
|
||||||
|
|
||||||
|
### Requisites
|
||||||
|
|
||||||
|
- [wasi-sdk](https://github.com/WebAssembly/wasi-sdk)
|
||||||
|
- [wasmtime](https://wasmtime.dev/)
|
||||||
|
|
||||||
|
```
|
||||||
|
# Set to wasi-sdk directory
|
||||||
|
$ export WASI_SDK_PATH=`<wasi-sdk-path>`
|
||||||
|
$ export CC_wasm32_wasi="${WASI_SDK_PATH}/bin/clang --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot"
|
||||||
|
# Build
|
||||||
|
$ cargo build --example persons --target wasm32-wasi --release --features bundled
|
||||||
|
# Run
|
||||||
|
$ wasmtime target/wasm32-wasi/release/examples/persons.wasm
|
||||||
|
Found persons:
|
||||||
|
ID: 1, Name: Steven
|
||||||
|
ID: 2, Name: John
|
||||||
|
ID: 3, Name: Alex
|
||||||
|
```
|
42
examples/persons/main.rs
Normal file
42
examples/persons/main.rs
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
extern crate rusqlite;
|
||||||
|
use rusqlite::{Connection, Result};
|
||||||
|
|
||||||
|
struct Person {
|
||||||
|
id: i32,
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
let conn = Connection::open_in_memory()?;
|
||||||
|
|
||||||
|
conn.execute(
|
||||||
|
"CREATE TABLE IF NOT EXISTS persons (
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
name TEXT NOT NULL
|
||||||
|
)",
|
||||||
|
(), // empty list of parameters.
|
||||||
|
)?;
|
||||||
|
|
||||||
|
conn.execute(
|
||||||
|
"INSERT INTO persons (name) VALUES (?1), (?2), (?3)",
|
||||||
|
["Steven", "John", "Alex"].map(|n| n.to_string()),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let mut stmt = conn.prepare("SELECT id, name FROM persons")?;
|
||||||
|
let rows = stmt.query_map([], |row| {
|
||||||
|
Ok(Person {
|
||||||
|
id: row.get(0)?,
|
||||||
|
name: row.get(1)?,
|
||||||
|
})
|
||||||
|
})?;
|
||||||
|
|
||||||
|
println!("Found persons:");
|
||||||
|
|
||||||
|
for person in rows {
|
||||||
|
match person {
|
||||||
|
Ok(p) => println!("ID: {}, Name: {}", p.id, p.name),
|
||||||
|
Err(e) => eprintln!("Error: {:?}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
@ -246,11 +246,16 @@ mod build_bundled {
|
|||||||
if !win_target() {
|
if !win_target() {
|
||||||
cfg.flag("-DHAVE_LOCALTIME_R");
|
cfg.flag("-DHAVE_LOCALTIME_R");
|
||||||
}
|
}
|
||||||
// Target wasm32-wasi can't compile the default VFS
|
|
||||||
if env::var("TARGET").map_or(false, |v| v == "wasm32-wasi") {
|
if env::var("TARGET").map_or(false, |v| v == "wasm32-wasi") {
|
||||||
cfg.flag("-DSQLITE_OS_OTHER")
|
cfg.flag("-USQLITE_THREADSAFE")
|
||||||
|
.flag("-DSQLITE_THREADSAFE=0")
|
||||||
// https://github.com/rust-lang/rust/issues/74393
|
// https://github.com/rust-lang/rust/issues/74393
|
||||||
.flag("-DLONGDOUBLE_TYPE=double");
|
.flag("-DLONGDOUBLE_TYPE=double")
|
||||||
|
.flag("-D_WASI_EMULATED_MMAN")
|
||||||
|
.flag("-D_WASI_EMULATED_GETPID")
|
||||||
|
.flag("-D_WASI_EMULATED_SIGNAL")
|
||||||
|
.flag("-D_WASI_EMULATED_PROCESS_CLOCKS");
|
||||||
|
|
||||||
if cfg!(feature = "wasm32-wasi-vfs") {
|
if cfg!(feature = "wasm32-wasi-vfs") {
|
||||||
cfg.file("sqlite3/wasm32-wasi-vfs.c");
|
cfg.file("sqlite3/wasm32-wasi-vfs.c");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user