use super::backend::{QueryResult, TransactionOps, TxType};
use crate::error::{OrmError, Result};
pub struct Transaction {
inner: Option<Box<dyn TransactionOps>>,
tx_type: TxType,
}
impl Transaction {
pub(crate) fn new(inner: Box<dyn TransactionOps>, tx_type: TxType) -> Self {
Self {
inner: Some(inner),
tx_type,
}
}
pub async fn query(&mut self, typeql: &str) -> Result<QueryResult> {
let tx = self
.inner
.as_mut()
.ok_or_else(|| OrmError::Transaction("Transaction already consumed".into()))?;
tx.query(typeql).await
}
pub async fn commit(&mut self) -> Result<()> {
let mut tx = self
.inner
.take()
.ok_or_else(|| OrmError::Transaction("Transaction already consumed".into()))?;
tx.commit().await
}
pub async fn rollback(&mut self) -> Result<()> {
let mut tx = self
.inner
.take()
.ok_or_else(|| OrmError::Transaction("Transaction already consumed".into()))?;
tx.rollback().await
}
pub async fn close(&mut self) -> Result<()> {
let Some(mut tx) = self.inner.take() else {
return Ok(());
};
tx.close().await
}
pub fn tx_type(&self) -> TxType {
self.tx_type
}
}