solidb_client/client/
transaction.rs1use super::SoliDBClient;
2use crate::protocol::{Command, DriverError, IsolationLevel};
3
4impl SoliDBClient {
5 pub async fn begin_transaction(
6 &mut self,
7 database: &str,
8 isolation_level: Option<IsolationLevel>,
9 ) -> Result<String, DriverError> {
10 let response = self
11 .send_command(Command::BeginTransaction {
12 database: database.to_string(),
13 isolation_level: isolation_level.unwrap_or_default(),
14 })
15 .await?;
16
17 let tx_id = Self::extract_tx_id(response)?;
18 self.current_tx = Some(tx_id.clone());
19 Ok(tx_id)
20 }
21
22 pub async fn commit(&mut self) -> Result<(), DriverError> {
23 let tx_id = self
24 .current_tx
25 .take()
26 .ok_or_else(|| DriverError::TransactionError("No active transaction".to_string()))?;
27
28 let response = self
29 .send_command(Command::CommitTransaction { tx_id })
30 .await?;
31 Self::extract_data(response)?;
32 Ok(())
33 }
34
35 pub async fn rollback(&mut self) -> Result<(), DriverError> {
36 let tx_id = self
37 .current_tx
38 .take()
39 .ok_or_else(|| DriverError::TransactionError("No active transaction".to_string()))?;
40
41 let response = self
42 .send_command(Command::RollbackTransaction { tx_id })
43 .await?;
44 Self::extract_data(response)?;
45 Ok(())
46 }
47
48 pub fn in_transaction(&self) -> bool {
49 self.current_tx.is_some()
50 }
51
52 pub fn transaction_id(&self) -> Option<&str> {
53 self.current_tx.as_deref()
54 }
55}