From cb2fad2b1c6a173e6f20ef48a29b3bbe491d6f5e Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Fri, 4 Oct 2013 00:09:45 +0200 Subject: PHP < 5.4 can not use $this in anonymous functions. Should have tested that one earlier (damn). --- ratatoeskr/sys/db.php | 57 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 20 deletions(-) (limited to 'ratatoeskr/sys/db.php') diff --git a/ratatoeskr/sys/db.php b/ratatoeskr/sys/db.php index 0b4bb4a..36d5348 100644 --- a/ratatoeskr/sys/db.php +++ b/ratatoeskr/sys/db.php @@ -87,34 +87,51 @@ function qdb() } /* - * Function: transaction + * Class: Transaction * - * Executes function $f and wraps it in a transaction. - * If $f has thrown an exception, the transactrion will be rolled back and the excetion will be re-thrown. - * Otherwise the transaction will be committed. - * - * Parameters: - * $f - A function / callback. + * Makes using transactions easier. */ -function transaction($f) +class Transaction { - global $db_con; + public $startedhere; - if($db_con->inTransaction()) - call_user_func($f); - else + /* + * Constructor: __construct + * + * Start a new transaction. + */ + public function __construct() { - try - { + global $db_con; + $this->startedhere = !($db_con->inTransaction()); + if($this->startedhere) $db_con->beginTransaction(); - call_user_func($f); + } + + /* + * Function: commit + * + * Commit the transaction. + */ + public function commit() + { + global $db_con; + + if($this->startedhere) $db_con->commit(); - } - catch(Exception $e) - { + } + + /* + * Function: rollback + * + * Toll the transaction back. + */ + public function rollback() + { + global $db_con; + + if($this->startedhere) $db_con->rollBack(); - throw $e; - } } } -- cgit v1.2.3-54-g00ecf