use sqlx::FromRow;
#[derive(Debug, Clone, FromRow)]
pub struct WasmRow {
pub hash: Vec<u8>,
pub code: Vec<u8>,
}
pub async fn insert_wasm(
pool: &sqlx::Pool<sqlx::Sqlite>,
hash: &[u8],
code: &[u8],
) -> sqlx::Result<()> {
sqlx::query!(
"INSERT OR IGNORE INTO Wasm (hash, code) VALUES (?, ?)",
hash,
code
)
.execute(pool)
.await?;
Ok(())
}
pub async fn get_wasm(
pool: &sqlx::Pool<sqlx::Sqlite>,
hash: &[u8],
) -> sqlx::Result<Option<WasmRow>> {
let row = sqlx::query_as!(
WasmRow,
r#"SELECT hash as "hash!", code as "code!" FROM Wasm WHERE hash = ?"#,
hash
)
.fetch_optional(pool)
.await?;
Ok(row)
}
pub async fn get_conductor_tag(pool: &sqlx::Pool<sqlx::Sqlite>) -> sqlx::Result<Option<String>> {
let row = sqlx::query!("SELECT tag FROM Conductor WHERE id = 1")
.fetch_optional(pool)
.await?;
Ok(row.map(|r| r.tag))
}
pub async fn set_conductor_tag(pool: &sqlx::Pool<sqlx::Sqlite>, tag: &str) -> sqlx::Result<()> {
sqlx::query!(
"INSERT INTO Conductor (id, tag) VALUES (1, ?) ON CONFLICT(id) DO UPDATE SET tag = excluded.tag",
tag
)
.execute(pool)
.await?;
Ok(())
}