Add params option to logQuery()

Parameters for prepared statements are now part of the
logged query data.
This commit is contained in:
Yasushi Ichikawa 2012-02-04 11:21:06 +09:00 committed by mark_story
parent 59ff5146be
commit e8a9d93eb5
3 changed files with 60 additions and 28 deletions

View file

@ -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

View file

@ -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
*
@ -807,21 +825,21 @@ class DboSourceTest extends CakeTestCase {
$db->begin();
$log = $db->getLog();
$expected = array('query' => 'BEGIN', 'affected' => '', 'numRows' => '', 'took' => '');
$expected = array('query' => 'BEGIN', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => '');
$this->assertEquals($expected, $log['log'][0]);
$db->commit();
$expected = array('query' => 'COMMIT', 'affected' => '', 'numRows' => '', 'took' => '');
$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' => '');
$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' => '');
$expected = array('query' => 'ROLLBACK', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => '');
$log = $db->getLog();
$this->assertEquals($expected, $log['log'][0]);
}

View file

@ -49,6 +49,20 @@ if ($noLogs || isset($_forced_from_dbo_)):
<?php
foreach ($logInfo['log'] as $k => $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 "<tr><td>" . ($k + 1) . "</td><td>" . h($i['query']) . "</td><td>{$i['error']}</td><td style = \"text-align: right\">{$i['affected']}</td><td style = \"text-align: right\">{$i['numRows']}</td><td style = \"text-align: right\">{$i['took']}</td></tr>\n";
endforeach;
?>
@ -58,4 +72,3 @@ if ($noLogs || isset($_forced_from_dbo_)):
else:
echo '<p>Encountered unexpected $logs cannot generate SQL log</p>';
endif;
?>