vibesql_executor/
transaction.rs1use vibesql_ast::{
4 BeginStmt, CommitStmt, ReleaseSavepointStmt, RollbackStmt, RollbackToSavepointStmt,
5 SavepointStmt,
6};
7use vibesql_storage::Database;
8
9use crate::errors::ExecutorError;
10
11pub struct BeginTransactionExecutor;
13
14impl BeginTransactionExecutor {
15 pub fn execute(_stmt: &BeginStmt, db: &mut Database) -> Result<String, ExecutorError> {
17 db.begin_transaction().map_err(|e| {
18 ExecutorError::StorageError(format!("Failed to begin transaction: {}", e))
19 })?;
20
21 Ok("Transaction started".to_string())
22 }
23}
24
25pub struct CommitExecutor;
27
28impl CommitExecutor {
29 pub fn execute(_stmt: &CommitStmt, db: &mut Database) -> Result<String, ExecutorError> {
31 db.commit_transaction().map_err(|e| {
32 ExecutorError::StorageError(format!("Failed to commit transaction: {}", e))
33 })?;
34
35 Ok("Transaction committed".to_string())
36 }
37}
38
39pub struct RollbackExecutor;
41
42impl RollbackExecutor {
43 pub fn execute(_stmt: &RollbackStmt, db: &mut Database) -> Result<String, ExecutorError> {
45 db.rollback_transaction().map_err(|e| {
46 ExecutorError::StorageError(format!("Failed to rollback transaction: {}", e))
47 })?;
48
49 Ok("Transaction rolled back".to_string())
50 }
51}
52
53pub struct SavepointExecutor;
55
56impl SavepointExecutor {
57 pub fn execute(stmt: &SavepointStmt, db: &mut Database) -> Result<String, ExecutorError> {
59 db.create_savepoint(stmt.name.clone()).map_err(|e| {
60 ExecutorError::StorageError(format!("Failed to create savepoint: {}", e))
61 })?;
62
63 Ok(format!("Savepoint '{}' created", stmt.name))
64 }
65}
66
67pub struct RollbackToSavepointExecutor;
69
70impl RollbackToSavepointExecutor {
71 pub fn execute(
73 stmt: &RollbackToSavepointStmt,
74 db: &mut Database,
75 ) -> Result<String, ExecutorError> {
76 db.rollback_to_savepoint(stmt.name.clone()).map_err(|e| {
77 ExecutorError::StorageError(format!("Failed to rollback to savepoint: {}", e))
78 })?;
79
80 Ok(format!("Rolled back to savepoint '{}'", stmt.name))
81 }
82}
83
84pub struct ReleaseSavepointExecutor;
86
87impl ReleaseSavepointExecutor {
88 pub fn execute(
90 stmt: &ReleaseSavepointStmt,
91 db: &mut Database,
92 ) -> Result<String, ExecutorError> {
93 db.release_savepoint(stmt.name.clone()).map_err(|e| {
94 ExecutorError::StorageError(format!("Failed to release savepoint: {}", e))
95 })?;
96
97 Ok(format!("Savepoint '{}' released", stmt.name))
98 }
99}