use crate::PackageId;
use serde::{Deserialize, Serialize};
use thiserror::Error;
#[cfg(not(feature = "hyperapp"))]
mod sqlite_sync;
#[cfg(not(feature = "hyperapp"))]
pub use sqlite_sync::{open, remove_db};
#[cfg(feature = "hyperapp")]
mod sqlite_async;
#[cfg(feature = "hyperapp")]
pub use sqlite_async::{open, remove_db};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SqliteRequest {
pub package_id: PackageId,
pub db: String,
pub action: SqliteAction,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum SqliteAction {
Open,
RemoveDb,
Write {
statement: String,
tx_id: Option<u64>,
},
Query(String),
BeginTx,
Commit { tx_id: u64 },
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum SqliteResponse {
Ok,
Read,
BeginTx { tx_id: u64 },
Err(SqliteError),
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub enum SqlValue {
Integer(i64),
Real(f64),
Text(String),
Blob(Vec<u8>),
Boolean(bool),
Null,
}
#[derive(Clone, Debug, Serialize, Deserialize, Error)]
pub enum SqliteError {
#[error("db [{0}, {1}] does not exist")]
NoDb(PackageId, String),
#[error("no transaction {0} found")]
NoTx(u64),
#[error("no write capability for requested DB")]
NoWriteCap,
#[error("no read capability for requested DB")]
NoReadCap,
#[error("request to open or remove DB with mismatching package ID")]
MismatchingPackageId,
#[error("failed to generate capability for new DB")]
AddCapFailed,
#[error("write statement started with non-existent write keyword")]
NotAWriteKeyword,
#[error("read query started with non-existent read keyword")]
NotAReadKeyword,
#[error("parameters blob in read/write was misshapen or contained invalid JSON objects")]
InvalidParameters,
#[error("sqlite got a malformed request that failed to deserialize")]
MalformedRequest,
#[error("rusqlite error: {0}")]
RusqliteError(String),
#[error("IO error: {0}")]
IOError(String),
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SqliteCapabilityParams {
pub kind: SqliteCapabilityKind,
pub db_key: (PackageId, String),
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SqliteCapabilityKind {
Read,
Write,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Sqlite {
pub package_id: PackageId,
pub db: String,
pub timeout: u64,
}