Skip to content

Commit

Permalink
Logging transaction commands in DboSource, fixes #2457
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 21, 2012
1 parent df5d9ac commit 37314a2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/Cake/Model/Datasource/DboSource.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2004,6 +2004,9 @@ public function truncate($table) {
*/ */
public function begin() { public function begin() {
if ($this->_transactionStarted || $this->_connection->beginTransaction()) { if ($this->_transactionStarted || $this->_connection->beginTransaction()) {
if ($this->fullDebug && empty($this->_transactionNesting)) {
$this->logQuery('BEGIN');
}
$this->_transactionStarted = true; $this->_transactionStarted = true;
$this->_transactionNesting++; $this->_transactionNesting++;
return true; return true;
Expand All @@ -2024,6 +2027,9 @@ public function commit() {
if ($this->_transactionNesting <= 0) { if ($this->_transactionNesting <= 0) {
$this->_transactionStarted = false; $this->_transactionStarted = false;
$this->_transactionNesting = 0; $this->_transactionNesting = 0;
if ($this->fullDebug) {
$this->logQuery('COMMIT');
}
return $this->_connection->commit(); return $this->_connection->commit();
} }
return true; return true;
Expand All @@ -2040,6 +2046,9 @@ public function commit() {
*/ */
public function rollback() { public function rollback() {
if ($this->_transactionStarted && $this->_connection->rollBack()) { if ($this->_transactionStarted && $this->_connection->rollBack()) {
if ($this->fullDebug) {
$this->logQuery('ROLLBACK');
}
$this->_transactionStarted = false; $this->_transactionStarted = false;
$this->_transactionNesting = 0; $this->_transactionNesting = 0;
return true; return true;
Expand Down
42 changes: 42 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
App::uses('DboSource', 'Model/Datasource'); App::uses('DboSource', 'Model/Datasource');
require_once dirname(dirname(__FILE__)) . DS . 'models.php'; require_once dirname(dirname(__FILE__)) . DS . 'models.php';


class MockPDO extends PDO {

public function __construct() {
}
}

class MockDataSource extends DataSource { class MockDataSource extends DataSource {
} }


Expand Down Expand Up @@ -645,6 +651,7 @@ public function testGetLog() {
$this->testDb->logQuery('Query 2'); $this->testDb->logQuery('Query 2');


$log = $this->testDb->getLog(); $log = $this->testDb->getLog();

$expected = array('query' => 'Query 1', 'affected' => '', 'numRows' => '', 'took' => ''); $expected = array('query' => 'Query 1', 'affected' => '', 'numRows' => '', 'took' => '');
$this->assertEquals($log['log'][0], $expected); $this->assertEquals($log['log'][0], $expected);
$expected = array('query' => 'Query 2', 'affected' => '', 'numRows' => '', 'took' => ''); $expected = array('query' => 'Query 2', 'affected' => '', 'numRows' => '', 'took' => '');
Expand Down Expand Up @@ -783,4 +790,39 @@ function testLastError() {
$expected = 'something: bad'; $expected = 'something: bad';
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }

/**
* Tests that transaction commands are logged
*
* @return void
**/
public function testTransactionLogging() {
$conn = $this->getMock('MockPDO');
$db = new DboTestSource;
$db->setConnection($conn);
$conn->expects($this->exactly(2))->method('beginTransaction')
->will($this->returnValue(true));
$conn->expects($this->once())->method('commit')->will($this->returnValue(true));
$conn->expects($this->once())->method('rollback')->will($this->returnValue(true));

$db->begin();
$log = $db->getLog();
$expected = array('query' => 'BEGIN', 'affected' => '', 'numRows' => '', 'took' => '');
$this->assertEquals($expected, $log['log'][0]);

$db->commit();
$expected = array('query' => 'COMMIT', 'affected' => '', 'numRows' => '', 'took' => '');
$log = $db->getLog();
$this->assertEquals($expected, $log['log'][0]);

$db->begin();
$expected = array('query' => 'BEGIN', 'affected' => '', 'numRows' => '', 'took' => '');
$log = $db->getLog();
$this->assertEquals($expected, $log['log'][0]);

$db->rollback();
$expected = array('query' => 'ROLLBACK', 'affected' => '', 'numRows' => '', 'took' => '');
$log = $db->getLog();
$this->assertEquals($expected, $log['log'][0]);
}
} }

0 comments on commit 37314a2

Please sign in to comment.