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 tx = self
.inner
.as_mut()
.ok_or_else(|| OrmError::Transaction("Transaction already consumed".into()))?;
tx.commit().await
}
pub fn tx_type(&self) -> TxType {
self.tx_type
}
}