use super::Client;
use crate::{
bson::Document,
client::session::ClusterTime,
error::Result,
options::{SessionOptions, TransactionOptions},
runtime,
ClientSession as AsyncClientSession,
};
pub struct ClientSession {
pub(crate) async_client_session: AsyncClientSession,
}
impl From<AsyncClientSession> for ClientSession {
fn from(async_client_session: AsyncClientSession) -> Self {
Self {
async_client_session,
}
}
}
impl ClientSession {
pub fn client(&self) -> Client {
self.async_client_session.client().into()
}
pub fn id(&self) -> &Document {
self.async_client_session.id()
}
pub fn cluster_time(&self) -> Option<&ClusterTime> {
self.async_client_session.cluster_time()
}
pub fn options(&self) -> Option<&SessionOptions> {
self.async_client_session.options()
}
pub fn advance_cluster_time(&mut self, to: &ClusterTime) {
self.async_client_session.advance_cluster_time(to)
}
pub fn start_transaction(
&mut self,
options: impl Into<Option<TransactionOptions>>,
) -> Result<()> {
runtime::block_on(self.async_client_session.start_transaction(options))
}
pub fn commit_transaction(&mut self) -> Result<()> {
runtime::block_on(self.async_client_session.commit_transaction())
}
pub fn abort_transaction(&mut self) -> Result<()> {
runtime::block_on(self.async_client_session.abort_transaction())
}
}