Skip to content
Permalink
Browse files

Add params option to logQuery()

Parameters for prepared statements are now part of the
logged query data.
  • Loading branch information...
ichikaway authored and markstory committed Feb 4, 2012
1 parent 59ff514 commit e8a9d93eb52cdff15acee789a89e3c569da9b259
@@ -405,7 +405,7 @@ public function execute($sql, $options = array(), $params = array()) {
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 @@ public function showLog($sorted = false) {
* @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
@@ -651,14 +651,32 @@ public function testGetLog() {
$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 @@ function testLastError() {
* @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]);
$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', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => '');
$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', 'params' => array(), '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]);
}
}
@@ -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'])) {

This comment has been minimized.

Copy link
@ADmad

ADmad Feb 9, 2012

Member

@ichikaway Please follow cake's coding standards. A space is needed after function name. ie. After each "if" and "foreach" in this patch.

$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;
?>

6 comments on commit e8a9d93

@markstory

This comment has been minimized.

Copy link
Member

markstory replied Feb 9, 2012

I can fix that, I merged it in.

@ADmad

This comment has been minimized.

Copy link
Member

ADmad replied Feb 10, 2012

@markstory Thanks. Though this commit has started causing errors in debugkit. DebugKit's, HtmlToobarHelper::table() passes each record of this log to HtmlHelper::tableCells(). Since the value for new 'params' key is an array it goes into this if condition thereby causing undefined index notices.

@ichikaway

This comment has been minimized.

Copy link
Contributor Author

ichikaway replied Feb 10, 2012

@markstory Thank you
@ADmad Sorry, I will follow cake's coding standards next time.

@markstory

This comment has been minimized.

Copy link
Member

markstory replied Feb 10, 2012

Stupid debug kit. I'll fix that too :)

@markstory

This comment has been minimized.

Copy link
Member

markstory replied Feb 11, 2012

Fixed DebugKit in cakephp/debug_kit@0b21dae

@ADmad

This comment has been minimized.

Copy link
Member

ADmad replied Feb 11, 2012

Thanks

Please sign in to comment.
You can’t perform that action at this time.