Skip to content

Commit

Permalink
Compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
marcua committed Nov 22, 2023
1 parent f1e9f40 commit 1231f24
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 15 deletions.
56 changes: 56 additions & 0 deletions src/hosted_db_runner/Cargo.lock

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

1 change: 1 addition & 0 deletions src/hosted_db_runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ edition = "2021"

[dependencies]
rusqlite = { version = "0.26.3", features = ["bundled", "wasm32-wasi-vfs"] }
serde = { version = "1.0", features = ["derive"] }
33 changes: 33 additions & 0 deletions src/hosted_db_runner/build_wasi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash

# Also: apt install llvm-dev libclang-dev clang

export WASI_VERSION=20
export WASI_VERSION_FULL=20.0
export WASI_SDK_PATH=`pwd`/wasi-sdk-${WASI_VERSION_FULL}
export WASI_LIB=`pwd`/wasi-sdk-${WASI_VERSION_FULL}/lib

export WASI_SYSROOT="${WASI_SDK_PATH}/share/wasi-sysroot"
export CC="${WASI_SDK_PATH}/bin/clang --sysroot=${WASI_SYSROOT}"
export AR="${WASI_SDK_PATH}/bin/llvm-ar"
export CC_wasm32_wasi="${CC}"
export CARGO_TARGET_WASM32_WASI_LINKER="${WASI_SDK_PATH}/bin/clang"
export LIBCLANG_PATH=${WASI_LIB}
export RUSTFLAGS="-Clink-arg=-L${WASI_CLANG_LIB} -Clink-arg=-lclang_rt.builtins-wasm32"

export LIBSQLITE3_FLAGS="\
-DSQLITE_OS_OTHER \
-USQLITE_TEMP_STORE \
-DSQLITE_TEMP_STORE=3 \
-USQLITE_THREADSAFE \
-DSQLITE_THREADSAFE=0 \
-DSQLITE_OMIT_LOCALTIME \
-DSQLITE_OMIT_LOAD_EXTENSION \
-DLONGDOUBLE_TYPE=double"

export RUST_BACKTRACE=1

wget -nc "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_VERSION}/wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz"
tar xvf wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz

cargo build --target "wasm32-wasi" --verbose
73 changes: 58 additions & 15 deletions src/hosted_db_runner/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,62 @@
use rusqlite;
use rusqlite::types::ValueRef;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use std::string;
use std::vec::Vec;
use rusqlite;
use rusqlite::types::ValueRef;

use sqlx::{
migrate,
postgres::PgPoolOptions,
sqlite::{SqliteConnectOptions, SqlitePoolOptions},
Pool, Postgres, Sqlite,
};



#[derive(Serialize, Debug, Deserialize)]
pub struct QueryResult {
pub fields: Vec<String>,
pub rows: Vec<Vec<Option<String>>>,
}

pub fn run_sqlite_query(path: &PathBuf, query: &str) -> Result<QueryResult, std::io::Error> {
#[derive(Debug, Deserialize, Serialize)]
pub struct AybError {
pub message: String,
}

impl From<std::io::Error> for AybError {
fn from(cause: std::io::Error) -> Self {
AybError {
message: format!("IO error: {:?}", cause),
}
}
}

impl From<rusqlite::Error> for AybError {
fn from(cause: rusqlite::Error) -> Self {
AybError {
message: format!("{:?}", cause),
}
}
}

impl From<rusqlite::types::FromSqlError> for AybError {
fn from(cause: rusqlite::types::FromSqlError) -> Self {
AybError {
message: format!("{:?}", cause),
}
}
}

impl From<std::str::Utf8Error> for AybError {
fn from(cause: std::str::Utf8Error) -> Self {
AybError {
message: format!("{:?}", cause),
}
}
}

impl From<string::FromUtf8Error> for AybError {
fn from(cause: string::FromUtf8Error) -> Self {
AybError {
message: format!("{:?}", cause),
}
}
}

pub fn run_sqlite_query(path: &PathBuf, query: &str) -> Result<QueryResult, AybError> {
let conn = rusqlite::Connection::open(path)?;
let mut prepared = conn.prepare(query)?;
let num_columns = prepared.column_count();
Expand Down Expand Up @@ -52,7 +89,13 @@ pub fn run_sqlite_query(path: &PathBuf, query: &str) -> Result<QueryResult, std:
})
}


fn main() -> std::io::Result<()> {
println!("{:#?}", run_sqlite_query(&PathBuf::from("testing.sqlite"), "CREATE TABLE TABLE moo (hello varchar(20));")?);
fn main() -> Result<(), AybError> {
println!(
"{:#?}",
run_sqlite_query(
&PathBuf::from("testing.sqlite"),
"CREATE TABLE TABLE moo (hello varchar(20));"
)?
);
Ok(())
}

0 comments on commit 1231f24

Please sign in to comment.