elif_orm/transactions/
savepoints.rs1use crate::backends::DatabaseTransaction;
6use crate::error::{ModelError, ModelResult};
7
8pub struct SavepointManager {
10 savepoint_count: u32,
11}
12
13impl SavepointManager {
14 pub fn new() -> Self {
15 Self { savepoint_count: 0 }
16 }
17
18 pub async fn create_savepoint(
20 &mut self,
21 tx: &mut Box<dyn DatabaseTransaction>,
22 ) -> ModelResult<String> {
23 self.savepoint_count += 1;
24 let savepoint_name = format!("sp_{}", self.savepoint_count);
25
26 let sql = format!("SAVEPOINT {}", savepoint_name);
27 tx.execute(&sql, &[])
28 .await
29 .map_err(|e| ModelError::Transaction(format!("Failed to create savepoint: {}", e)))?;
30
31 Ok(savepoint_name)
32 }
33
34 pub async fn release_savepoint(
36 &mut self,
37 tx: &mut Box<dyn DatabaseTransaction>,
38 savepoint_name: &str,
39 ) -> ModelResult<()> {
40 let sql = format!("RELEASE SAVEPOINT {}", savepoint_name);
41 tx.execute(&sql, &[])
42 .await
43 .map_err(|e| ModelError::Transaction(format!("Failed to release savepoint: {}", e)))?;
44
45 Ok(())
46 }
47
48 pub async fn rollback_to_savepoint(
50 &mut self,
51 tx: &mut Box<dyn DatabaseTransaction>,
52 savepoint_name: &str,
53 ) -> ModelResult<()> {
54 let sql = format!("ROLLBACK TO SAVEPOINT {}", savepoint_name);
55 tx.execute(&sql, &[]).await.map_err(|e| {
56 ModelError::Transaction(format!("Failed to rollback to savepoint: {}", e))
57 })?;
58
59 Ok(())
60 }
61
62 pub fn savepoint_count(&self) -> u32 {
64 self.savepoint_count
65 }
66}
67
68impl Default for SavepointManager {
69 fn default() -> Self {
70 Self::new()
71 }
72}