From 9a41bf34a13ab259e06be958bb8d1135e1578741 Mon Sep 17 00:00:00 2001 From: Sergey Tsalkov Date: Tue, 18 Sep 2012 20:54:39 -0700 Subject: [PATCH] a few nested transaction cleanups functionality for committing/rolling back all active transactions --- db.class.php | 14 +++++++++----- simpletest/TransactionTest.php | 14 ++++++++++---- simpletest/TransactionTest_55.php | 21 +++++++++++++++++++++ 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/db.class.php b/db.class.php index 8afdaf4..929dd5b 100644 --- a/db.class.php +++ b/db.class.php @@ -215,14 +215,16 @@ class MeekroDB { if (!$this->nested_transactions || $this->nested_transactions_count == 0) { $this->queryNull('START TRANSACTION'); + $this->nested_transactions_count = 1; } else { $this->queryNull("SAVEPOINT LEVEL{$this->nested_transactions_count}"); + $this->nested_transactions_count++; } - if ($this->nested_transactions) return ++$this->nested_transactions_count; + return $this->nested_transactions_count; } - public function commit() { + public function commit($all=false) { if ($this->nested_transactions && $this->serverVersion() < '5.5') { return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion()); } @@ -230,7 +232,8 @@ class MeekroDB { if ($this->nested_transactions && $this->nested_transactions_count > 0) $this->nested_transactions_count--; - if (!$this->nested_transactions || $this->nested_transactions_count == 0) { + if (!$this->nested_transactions || $all || $this->nested_transactions_count == 0) { + $this->nested_transactions_count = 0; $this->queryNull('COMMIT'); } else { $this->queryNull("RELEASE SAVEPOINT LEVEL{$this->nested_transactions_count}"); @@ -239,7 +242,7 @@ class MeekroDB { return $this->nested_transactions_count; } - public function rollback() { + public function rollback($all=false) { if ($this->nested_transactions && $this->serverVersion() < '5.5') { return $this->nonSQLError("Nested transactions are only available on MySQL 5.5 and greater. You are using MySQL " . $this->serverVersion()); } @@ -247,7 +250,8 @@ class MeekroDB { if ($this->nested_transactions && $this->nested_transactions_count > 0) $this->nested_transactions_count--; - if (!$this->nested_transactions || $this->nested_transactions_count == 0) { + if (!$this->nested_transactions || $all || $this->nested_transactions_count == 0) { + $this->nested_transactions_count = 0; $this->queryNull('ROLLBACK'); } else { $this->queryNull("ROLLBACK TO SAVEPOINT LEVEL{$this->nested_transactions_count}"); diff --git a/simpletest/TransactionTest.php b/simpletest/TransactionTest.php index 363a4d4..bdb6657 100644 --- a/simpletest/TransactionTest.php +++ b/simpletest/TransactionTest.php @@ -5,16 +5,22 @@ class TransactionTest extends SimpleTest { DB::query("UPDATE accounts SET age=%i WHERE username=%s", 600, 'Abe'); - DB::startTransaction(); + $depth = DB::startTransaction(); + $this->assert($depth === 1); + DB::query("UPDATE accounts SET age=%i WHERE username=%s", 700, 'Abe'); - DB::startTransaction(); + $depth = DB::startTransaction(); + $this->assert($depth === 1); + DB::query("UPDATE accounts SET age=%i WHERE username=%s", 800, 'Abe'); - DB::rollback(); + $depth = DB::rollback(); + $this->assert($depth === 0); $age = DB::queryFirstField("SELECT age FROM accounts WHERE username=%s", 'Abe'); $this->assert($age == 700); - DB::rollback(); + $depth = DB::rollback(); + $this->assert($depth === 0); $age = DB::queryFirstField("SELECT age FROM accounts WHERE username=%s", 'Abe'); $this->assert($age == 700); diff --git a/simpletest/TransactionTest_55.php b/simpletest/TransactionTest_55.php index cc53d64..d868399 100644 --- a/simpletest/TransactionTest_55.php +++ b/simpletest/TransactionTest_55.php @@ -59,5 +59,26 @@ class TransactionTest_55 extends SimpleTest { DB::$nested_transactions = false; } + function test_3_transaction_rollback_all() { + DB::$nested_transactions = true; + + DB::query("UPDATE accounts SET age=%i WHERE username=%s", 200, 'Abe'); + + $depth = DB::startTransaction(); + $this->assert($depth === 1); + DB::query("UPDATE accounts SET age=%i WHERE username=%s", 300, 'Abe'); + $depth = DB::startTransaction(); + $this->assert($depth === 2); + + DB::query("UPDATE accounts SET age=%i WHERE username=%s", 400, 'Abe'); + $depth = DB::rollback(true); + $this->assert($depth === 0); + + $age = DB::queryFirstField("SELECT age FROM accounts WHERE username=%s", 'Abe'); + $this->assert($age == 200); + + DB::$nested_transactions = false; + } + } ?>