From e8a9d93eb52cdff15acee789a89e3c569da9b259 Mon Sep 17 00:00:00 2001 From: Yasushi Ichikawa Date: Sat, 4 Feb 2012 11:21:06 +0900 Subject: [PATCH] Add params option to logQuery() Parameters for prepared statements are now part of the logged query data. --- lib/Cake/Model/Datasource/DboSource.php | 5 +- .../Case/Model/Datasource/DboSourceTest.php | 68 ++++++++++++------- lib/Cake/View/Elements/sql_dump.ctp | 15 +++- 3 files changed, 60 insertions(+), 28 deletions(-) diff --git a/lib/Cake/Model/Datasource/DboSource.php b/lib/Cake/Model/Datasource/DboSource.php index 5ac75f82b..abe851d6c 100644 --- a/lib/Cake/Model/Datasource/DboSource.php +++ b/lib/Cake/Model/Datasource/DboSource.php @@ -405,7 +405,7 @@ class DboSource extends DataSource { if ($options['log']) { $this->took = round((microtime(true) - $t) * 1000, 0); $this->numRows = $this->affected = $this->lastAffected(); - $this->logQuery($sql); + $this->logQuery($sql, $params); } return $this->_result; @@ -894,11 +894,12 @@ class DboSource extends DataSource { * @param string $sql SQL statement * @return void */ - public function logQuery($sql) { + public function logQuery($sql, $params = array()) { $this->_queriesCnt++; $this->_queriesTime += $this->took; $this->_queriesLog[] = array( 'query' => $sql, + 'params' => $params, 'affected' => $this->affected, 'numRows' => $this->numRows, 'took' => $this->took diff --git a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php index 630b4601f..7b1ddb3ce 100644 --- a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php @@ -651,14 +651,32 @@ class DboSourceTest extends CakeTestCase { $this->testDb->logQuery('Query 2'); $log = $this->testDb->getLog(); + $expected = array('query' => 'Query 1', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => ''); - $expected = array('query' => 'Query 1', 'affected' => '', 'numRows' => '', 'took' => ''); $this->assertEquals($log['log'][0], $expected); - $expected = array('query' => 'Query 2', 'affected' => '', 'numRows' => '', 'took' => ''); + $expected = array('query' => 'Query 2', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => ''); $this->assertEquals($log['log'][1], $expected); $expected = array('query' => 'Error 1', 'affected' => '', 'numRows' => '', 'took' => ''); } + +/** + * test getting the query log as an array, setting bind params. + * + * @return void + */ + public function testGetLogParams() { + $this->testDb->logQuery('Query 1', array(1,2,'abc')); + $this->testDb->logQuery('Query 2', array('field1' => 1, 'field2' => 'abc')); + + $log = $this->testDb->getLog(); + $expected = array('query' => 'Query 1', 'params' => array(1,2,'abc'), 'affected' => '', 'numRows' => '', 'took' => ''); + $this->assertEquals($log['log'][0], $expected); + $expected = array('query' => 'Query 2', 'params' => array('field1' => 1, 'field2' => 'abc'), 'affected' => '', 'numRows' => '', 'took' => ''); + $this->assertEquals($log['log'][1], $expected); + } + + /** * test that query() returns boolean values from operations like CREATE TABLE * @@ -797,32 +815,32 @@ class DboSourceTest extends CakeTestCase { * @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)); + $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->begin(); + $log = $db->getLog(); + $expected = array('query' => 'BEGIN', 'params' => array(), '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->commit(); + $expected = array('query' => 'COMMIT', 'params' => array(), '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->begin(); + $expected = array('query' => 'BEGIN', 'params' => array(), '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]); + $db->rollback(); + $expected = array('query' => 'ROLLBACK', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => ''); + $log = $db->getLog(); + $this->assertEquals($expected, $log['log'][0]); } } diff --git a/lib/Cake/View/Elements/sql_dump.ctp b/lib/Cake/View/Elements/sql_dump.ctp index b022370e4..9e637251d 100644 --- a/lib/Cake/View/Elements/sql_dump.ctp +++ b/lib/Cake/View/Elements/sql_dump.ctp @@ -49,6 +49,20 @@ if ($noLogs || isset($_forced_from_dbo_)): $i) : $i += array('error' => ''); + if(!empty($i['params']) && is_array($i['params'])) { + $bindParam = $bindType = null; + if(preg_match('/.+ :.+/', $i['query'])) { + $bindType = true; + } + foreach($i['params'] as $bindKey => $bindVal) { + if($bindType === true) { + $bindParam .= h($bindKey) ." => " . h($bindVal) . ", "; + } else { + $bindParam .= h($bindVal) . ", "; + } + } + $i['query'] .= " , params[ " . rtrim($bindParam, ', ') . " ]"; + } echo "" . ($k + 1) . "" . h($i['query']) . "{$i['error']}{$i['affected']}{$i['numRows']}{$i['took']}\n"; endforeach; ?> @@ -58,4 +72,3 @@ if ($noLogs || isset($_forced_from_dbo_)): else: echo '

Encountered unexpected $logs cannot generate SQL log

'; endif; -?>