use alopex_core::types::TxnMode;
use alopex_core::{BoxFuture, BoxStream, MaybeSend};
use crate::SqlError;
use crate::executor::{ExecutionResult, Row};
use crate::planner::LogicalPlan;
pub type AsyncResult<T> = core::result::Result<T, SqlError>;
pub type AsyncRowStream<'a> = BoxStream<'a, AsyncResult<Row>>;
pub trait ErasedAsyncSqlTransaction: MaybeSend {
fn mode(&self) -> TxnMode;
fn commit_boxed(self: Box<Self>) -> BoxFuture<'static, AsyncResult<()>>;
fn rollback_boxed(self: Box<Self>) -> BoxFuture<'static, AsyncResult<()>>;
}
pub trait AsyncSqlTransaction: MaybeSend {
fn mode(&self) -> TxnMode;
fn commit<'a>(&'a mut self) -> BoxFuture<'a, AsyncResult<()>>;
fn rollback<'a>(&'a mut self) -> BoxFuture<'a, AsyncResult<()>>;
fn execute_plan<'a>(
&'a mut self,
plan: LogicalPlan,
) -> BoxFuture<'a, AsyncResult<ExecutionResult>>;
fn query_stream<'a>(
&'a mut self,
plan: LogicalPlan,
) -> BoxFuture<'a, AsyncResult<AsyncRowStream<'a>>>;
}
impl<T> ErasedAsyncSqlTransaction for T
where
T: AsyncSqlTransaction + MaybeSend + 'static,
{
fn mode(&self) -> TxnMode {
AsyncSqlTransaction::mode(self)
}
fn commit_boxed(self: Box<Self>) -> BoxFuture<'static, AsyncResult<()>> {
Box::pin(async move {
let mut this = *self;
this.commit().await
})
}
fn rollback_boxed(self: Box<Self>) -> BoxFuture<'static, AsyncResult<()>> {
Box::pin(async move {
let mut this = *self;
this.rollback().await
})
}
}
pub trait AsyncTxnBridge: MaybeSend {
type Transaction<'a>: AsyncSqlTransaction + 'a
where
Self: 'a;
fn begin_read<'a>(&'a self) -> BoxFuture<'a, AsyncResult<Self::Transaction<'a>>>;
fn begin_write<'a>(&'a self) -> BoxFuture<'a, AsyncResult<Self::Transaction<'a>>>;
}