Logging transaction commands in DboSource, fixes #2457

This commit is contained in:
Jose Lorenzo Rodriguez 2012-01-21 15:48:17 -04:30
parent df5d9ac3d1
commit 37314a2188
2 changed files with 51 additions and 0 deletions

View file

@ -2004,6 +2004,9 @@ class DboSource extends DataSource {
*/ */
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;
@ -2024,6 +2027,9 @@ class DboSource extends DataSource {
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;
@ -2040,6 +2046,9 @@ class DboSource extends DataSource {
*/ */
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;

View file

@ -23,6 +23,12 @@ App::uses('DataSource', 'Model/Datasource');
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 {
} }
@ -645,6 +651,7 @@ class DboSourceTest extends CakeTestCase {
$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' => '');
@ -783,4 +790,39 @@ class DboSourceTest extends CakeTestCase {
$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]);
}
} }