Skip to content
This repository has been archived by the owner on Aug 25, 2019. It is now read-only.

Commit

Permalink
Transactional using closure.
Browse files Browse the repository at this point in the history
  • Loading branch information
dietrichm committed Jul 29, 2012
1 parent 553854d commit 9304c0e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
9 changes: 9 additions & 0 deletions System/Database/Connection.php
Expand Up @@ -95,6 +95,15 @@ public function commit() {
public function rollback() {
$this->connection->rollback();
}

/**
* Transactional run of a code block.
*
* \param $closure Closure or callback
*/
public function transactional($closure) {
$this->connection->transactional($closure);
}

/**
* Create a new query.
Expand Down
32 changes: 31 additions & 1 deletion System/Database/Tests/DatabaseTest.php
Expand Up @@ -226,7 +226,6 @@ public function testTransactionCommit() {
$this->assertEquals(1, $stmt->rowCount());
}

// TODO not working
public function testTransactionRollback() {
$this->installTestTable();

Expand All @@ -240,4 +239,35 @@ public function testTransactionRollback() {
$stmt = $query->select("*")->from("test", "t")->execute();
$this->assertEquals(0, $stmt->rowCount());
}

public function testTransactionalCommit() {
$this->installTestTable();

$this->db->transactional(function() {
$this->db->insert("test", array(
"foo" => "bar"
));
});

$query = $this->db->newQuery();
$stmt = $query->select("*")->from("test", "t")->execute();
$this->assertEquals(1, $stmt->rowCount());
}

public function testTransactionalRollback() {
$this->installTestTable();

try {
$this->db->transactional(function() {
$this->db->insert("test", array(
"foo" => "bar"
));
throw new \Exception("foo");
});
} catch (\Exception $e) {}

$query = $this->db->newQuery();
$stmt = $query->select("*")->from("test", "t")->execute();
$this->assertEquals(0, $stmt->rowCount());
}
}

0 comments on commit 9304c0e

Please sign in to comment.