From 9d0056f0a66229a7b56513912e09a58055f0b755 Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Tue, 18 Feb 2025 16:46:56 +0200 Subject: [PATCH] Adds transaction database methods (for the tests) --- app/classes/database.php | 50 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/app/classes/database.php b/app/classes/database.php index 7d06545..bdbcf40 100644 --- a/app/classes/database.php +++ b/app/classes/database.php @@ -170,6 +170,56 @@ class Database { } } + /** + * Begins a database transaction. + * + * @throws Exception If starting the transaction fails + */ + public function beginTransaction() { + if (!$this->pdo) { + throw new Exception('No database connection.'); + } + + try { + return $this->pdo->beginTransaction(); + } catch (PDOException $e) { + throw new Exception('Failed to start transaction: ' . $e->getMessage()); + } + } + + /** + * Commits the current database transaction. + * + * @throws Exception If committing the transaction fails + */ + public function commit() { + if (!$this->pdo) { + throw new Exception('No database connection.'); + } + + try { + return $this->pdo->commit(); + } catch (PDOException $e) { + throw new Exception('Failed to commit transaction: ' . $e->getMessage()); + } + } + + /** + * Rolls back the current database transaction. + * + * @throws Exception If rolling back the transaction fails + */ + public function rollBack() { + if (!$this->pdo) { + throw new Exception('No database connection.'); + } + + try { + return $this->pdo->rollBack(); + } catch (PDOException $e) { + throw new Exception('Failed to rollback transaction: ' . $e->getMessage()); + } + } } ?>