From 41adce884a3e971cea7e994d8c4e2b22855c554d Mon Sep 17 00:00:00 2001 From: nbm Date: Fri, 30 Sep 2005 14:00:19 +0000 Subject: [PATCH] Add transparent (requested) transaction support for dispatcher. A dispatcher can be configured to always use transactions, or you can start a transaction. Either way, rollback occurs when errorPage or errorRedirectTo is used, and committing happens in standard case and in successRedirectTo. --- lib/dispatcher.inc.php | 50 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/lib/dispatcher.inc.php b/lib/dispatcher.inc.php index 7287571..5ffc5fd 100644 --- a/lib/dispatcher.inc.php +++ b/lib/dispatcher.inc.php @@ -2,6 +2,8 @@ class KTDispatcher { var $event_var = "action"; + var $bAutomaticTransaction = false; + var $bTransactionStarted = false; function dispatch () { $method = 'do_main'; @@ -12,19 +14,50 @@ class KTDispatcher { } } + if ($this->bAutomaticTransaction) { + $this->startTransaction(); + } + $ret = $this->$method(); $this->handleOutput($ret); + + if ($this->bTransactionStarted) { + $this->commitTransaction(); + } + } + + function startTransaction() { + DBUtil::startTransaction(); + $this->bTransactionStarted = true; + } + + function commitTransaction() { + DBUtil::commit(); + $this->bTransactionStarted = false; + } + + function rollbackTransaction() { + DBUtil::rollback(); + $this->bTransactionStarted = false; } function errorRedirectTo($event, $error_message, $sQuery = "") { - /* $method = 'do_main'; - if (method_exists($this, 'do_' . $event)) { - $method = 'do_' . $event; - }*/ + if ($this->bTransactionStarted) { + $this->rollbackTransaction(); + } + $_SESSION['KTErrorMessage'][] = $error_message; - //exit(redirect($_SERVER["PHP_SELF"] . '?action=' . $event)); - exit($this->redirectTo($event, $sQuery)); - //return $this->$method(); + $this->redirectTo($event, $sQuery); + } + + function successRedirectTo($event, $info_message, $sQuery = "") { + if ($this->bTransactionStarted) { + $this->commitTransaction(); + } + if (!empty($info_message)) { + $_SESSION['KTInfoMessage'][] = $info_message; + } + $this->redirectTo($event, $sQuery); } function redirectTo($event, $sQuery = "") { @@ -109,6 +142,9 @@ class KTStandardDispatcher extends KTDispatcher { } function errorPage($errorMessage) { + if ($this->bTransactionStarted) { + $this->rollbackTransaction(); + } $this->handleOutput($errorMessage); exit(0); } -- libgit2 0.21.4