use crate::error::Result;
use crate::traits::Pool;
use std::future::Future;
use std::pin::Pin;
use std::result::Result as StdResult;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum IsolationLevel {
ReadUncommitted,
ReadCommitted,
#[default]
RepeatableRead,
Serializable,
}
pub trait Transaction: Pool {
fn commit(&self) -> impl Future<Output = Result<()>> + Send;
fn rollback(&self) -> impl Future<Output = Result<()>> + Send;
}
pub trait Transactional: Pool {
type Tx: Transaction + Send + Sync;
fn begin(&self) -> impl Future<Output = Result<Self::Tx>> + Send;
fn begin_with(&self, level: IsolationLevel) -> impl Future<Output = Result<Self::Tx>> + Send;
fn in_transaction<R, E, F>(&self, f: F) -> impl Future<Output = StdResult<R, E>> + Send
where
R: Send,
E: From<crate::Error> + Send,
F: for<'a> FnOnce(
&'a Self::Tx,
)
-> Pin<Box<dyn Future<Output = StdResult<R, E>> + Send + 'a>>
+ Send;
fn in_transaction_with<R, E, F>(
&self,
level: IsolationLevel,
f: F,
) -> impl Future<Output = StdResult<R, E>> + Send
where
R: Send,
E: From<crate::Error> + Send,
F: for<'a> FnOnce(
&'a Self::Tx,
)
-> Pin<Box<dyn Future<Output = StdResult<R, E>> + Send + 'a>>
+ Send;
fn with_connection<R, E, F>(&self, f: F) -> impl Future<Output = StdResult<R, E>> + Send
where
R: Send,
E: From<crate::Error> + Send,
F: FnOnce(&Self) -> Pin<Box<dyn Future<Output = StdResult<R, E>> + Send + '_>> + Send;
}