vibesql_executor/
transaction.rs

1//! Transaction control statement execution (BEGIN, COMMIT, ROLLBACK)
2
3use vibesql_ast::{
4    BeginStmt, CommitStmt, ReleaseSavepointStmt, RollbackStmt, RollbackToSavepointStmt,
5    SavepointStmt,
6};
7use vibesql_storage::Database;
8
9use crate::errors::ExecutorError;
10
11/// Executor for BEGIN TRANSACTION statements
12pub struct BeginTransactionExecutor;
13
14impl BeginTransactionExecutor {
15    /// Execute a BEGIN TRANSACTION statement
16    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
25/// Executor for COMMIT statements
26pub struct CommitExecutor;
27
28impl CommitExecutor {
29    /// Execute a COMMIT statement
30    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
39/// Executor for ROLLBACK statements
40pub struct RollbackExecutor;
41
42impl RollbackExecutor {
43    /// Execute a ROLLBACK statement
44    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
53/// Executor for SAVEPOINT statements
54pub struct SavepointExecutor;
55
56impl SavepointExecutor {
57    /// Execute a SAVEPOINT statement
58    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
67/// Executor for ROLLBACK TO SAVEPOINT statements
68pub struct RollbackToSavepointExecutor;
69
70impl RollbackToSavepointExecutor {
71    /// Execute a ROLLBACK TO SAVEPOINT statement
72    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
84/// Executor for RELEASE SAVEPOINT statements
85pub struct ReleaseSavepointExecutor;
86
87impl ReleaseSavepointExecutor {
88    /// Execute a RELEASE SAVEPOINT statement
89    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}