use std::time::Duration;
use serde::de::DeserializeOwned;
use super::database::{Done, reads};
use super::engine::Base;
use super::rows::{Held, Rows};
use super::run::{self, Wanted};
use super::statement::Sql;
use crate::{Result, Transaction, TxHandle};
pub(crate) const BOUND: Duration = Duration::from_secs(5);
pub struct Tx<'t> {
transaction: &'t Transaction<'t>,
base: &'t Base,
}
impl<'t> Tx<'t> {
pub(crate) fn new(transaction: &'t Transaction<'t>, base: &'t Base) -> Self {
Tx { transaction, base }
}
reads!();
pub fn exec(&self, statement: impl Into<Sql>) -> Result<Done> {
let statement = statement.into();
self.transaction.call(|raw| run::exec(raw, &statement)).map_err(|error| self.base.failed(&statement, error))
}
pub fn with<'h, H: TxHandle>(&'h self, handle: &'h H) -> H::InTx<'h> {
handle.in_tx(self.transaction)
}
#[cfg(feature = "wire")]
pub(crate) fn transaction(&self) -> &'t Transaction<'t> {
self.transaction
}
fn rows(&self, statement: &Sql, wanted: Wanted) -> Result<Rows> {
self.transaction.call(|raw| run::write(raw, statement, wanted, Held::of(&self.base.memory)))
}
}
impl std::fmt::Debug for Tx<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Tx({})", self.base.name)
}
}