use std::future::Future;
use std::pin::Pin;
use serde::{Deserialize, Serialize};
use crate::error::OrmError;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum QueryResult {
Ok,
Documents(Vec<serde_json::Value>),
Rows(Vec<serde_json::Value>),
}
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TxType {
Read,
Write,
Schema,
}
pub trait TransactionOps: Send {
fn query(&mut self, typeql: &str) -> BoxFuture<'_, Result<QueryResult, OrmError>>;
fn commit(&mut self) -> BoxFuture<'_, Result<(), OrmError>>;
fn rollback(&mut self) -> BoxFuture<'_, Result<(), OrmError>>;
fn close(&mut self) -> BoxFuture<'_, Result<(), OrmError>>;
}
pub trait DriverBackend: Send + Sync {
fn open_transaction(
&self,
database: &str,
tx_type: TxType,
) -> BoxFuture<'_, Result<Box<dyn TransactionOps>, OrmError>>;
fn is_open(&self) -> bool;
fn database_exists(&self, database: &str) -> BoxFuture<'_, Result<bool, OrmError>> {
let database = database.to_string();
Box::pin(async move {
Err(OrmError::Connection(format!(
"Database existence checks are not supported by this backend for database '{database}'"
)))
})
}
fn create_database(&self, database: &str) -> BoxFuture<'_, Result<(), OrmError>> {
let database = database.to_string();
Box::pin(async move {
Err(OrmError::Connection(format!(
"Database creation is not supported by this backend for database '{database}'"
)))
})
}
fn delete_database(&self, database: &str) -> BoxFuture<'_, Result<(), OrmError>> {
let database = database.to_string();
Box::pin(async move {
Err(OrmError::Connection(format!(
"Database deletion is not supported by this backend for database '{database}'"
)))
})
}
fn schema_text(&self, database: &str) -> BoxFuture<'_, Result<String, OrmError>> {
let database = database.to_string();
Box::pin(async move {
Err(OrmError::Connection(format!(
"Schema export is not supported by this backend for database '{database}'"
)))
})
}
}