aboutsummaryrefslogtreecommitdiff
path: root/ratatoeskr/sys/db.php
diff options
context:
space:
mode:
authorKevin Chabowski <kevin@kch42.de>2013-10-04 00:09:45 +0200
committerKevin Chabowski <kevin@kch42.de>2013-10-04 00:09:45 +0200
commitcb2fad2b1c6a173e6f20ef48a29b3bbe491d6f5e (patch)
treec550b1b79817e9dfbbb186c33ad960eada286336 /ratatoeskr/sys/db.php
parent458b2eaaad507180ab3f95a765534041874ac914 (diff)
downloadratatoeskr-cms-cb2fad2b1c6a173e6f20ef48a29b3bbe491d6f5e.tar.gz
ratatoeskr-cms-cb2fad2b1c6a173e6f20ef48a29b3bbe491d6f5e.tar.bz2
ratatoeskr-cms-cb2fad2b1c6a173e6f20ef48a29b3bbe491d6f5e.zip
PHP < 5.4 can not use $this in anonymous functions.
Should have tested that one earlier (damn).
Diffstat (limited to 'ratatoeskr/sys/db.php')
-rw-r--r--ratatoeskr/sys/db.php57
1 files changed, 37 insertions, 20 deletions
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;
- }
}
}