mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Add params option to logQuery()
Parameters for prepared statements are now part of the logged query data.
This commit is contained in:
parent
59ff5146be
commit
e8a9d93eb5
3 changed files with 60 additions and 28 deletions
|
@ -405,7 +405,7 @@ class DboSource extends DataSource {
|
||||||
if ($options['log']) {
|
if ($options['log']) {
|
||||||
$this->took = round((microtime(true) - $t) * 1000, 0);
|
$this->took = round((microtime(true) - $t) * 1000, 0);
|
||||||
$this->numRows = $this->affected = $this->lastAffected();
|
$this->numRows = $this->affected = $this->lastAffected();
|
||||||
$this->logQuery($sql);
|
$this->logQuery($sql, $params);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->_result;
|
return $this->_result;
|
||||||
|
@ -894,11 +894,12 @@ class DboSource extends DataSource {
|
||||||
* @param string $sql SQL statement
|
* @param string $sql SQL statement
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function logQuery($sql) {
|
public function logQuery($sql, $params = array()) {
|
||||||
$this->_queriesCnt++;
|
$this->_queriesCnt++;
|
||||||
$this->_queriesTime += $this->took;
|
$this->_queriesTime += $this->took;
|
||||||
$this->_queriesLog[] = array(
|
$this->_queriesLog[] = array(
|
||||||
'query' => $sql,
|
'query' => $sql,
|
||||||
|
'params' => $params,
|
||||||
'affected' => $this->affected,
|
'affected' => $this->affected,
|
||||||
'numRows' => $this->numRows,
|
'numRows' => $this->numRows,
|
||||||
'took' => $this->took
|
'took' => $this->took
|
||||||
|
|
|
@ -651,14 +651,32 @@ 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', 'params' => array(), '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', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => '');
|
||||||
$this->assertEquals($log['log'][1], $expected);
|
$this->assertEquals($log['log'][1], $expected);
|
||||||
$expected = array('query' => 'Error 1', 'affected' => '', 'numRows' => '', 'took' => '');
|
$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
|
* test that query() returns boolean values from operations like CREATE TABLE
|
||||||
*
|
*
|
||||||
|
@ -797,32 +815,32 @@ class DboSourceTest extends CakeTestCase {
|
||||||
* @return void
|
* @return void
|
||||||
**/
|
**/
|
||||||
public function testTransactionLogging() {
|
public function testTransactionLogging() {
|
||||||
$conn = $this->getMock('MockPDO');
|
$conn = $this->getMock('MockPDO');
|
||||||
$db = new DboTestSource;
|
$db = new DboTestSource;
|
||||||
$db->setConnection($conn);
|
$db->setConnection($conn);
|
||||||
$conn->expects($this->exactly(2))->method('beginTransaction')
|
$conn->expects($this->exactly(2))->method('beginTransaction')
|
||||||
->will($this->returnValue(true));
|
->will($this->returnValue(true));
|
||||||
$conn->expects($this->once())->method('commit')->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->expects($this->once())->method('rollback')->will($this->returnValue(true));
|
||||||
|
|
||||||
$db->begin();
|
$db->begin();
|
||||||
$log = $db->getLog();
|
$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]);
|
$this->assertEquals($expected, $log['log'][0]);
|
||||||
|
|
||||||
$db->commit();
|
$db->commit();
|
||||||
$expected = array('query' => 'COMMIT', 'affected' => '', 'numRows' => '', 'took' => '');
|
$expected = array('query' => 'COMMIT', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => '');
|
||||||
$log = $db->getLog();
|
$log = $db->getLog();
|
||||||
$this->assertEquals($expected, $log['log'][0]);
|
$this->assertEquals($expected, $log['log'][0]);
|
||||||
|
|
||||||
$db->begin();
|
$db->begin();
|
||||||
$expected = array('query' => 'BEGIN', 'affected' => '', 'numRows' => '', 'took' => '');
|
$expected = array('query' => 'BEGIN', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => '');
|
||||||
$log = $db->getLog();
|
$log = $db->getLog();
|
||||||
$this->assertEquals($expected, $log['log'][0]);
|
$this->assertEquals($expected, $log['log'][0]);
|
||||||
|
|
||||||
$db->rollback();
|
$db->rollback();
|
||||||
$expected = array('query' => 'ROLLBACK', 'affected' => '', 'numRows' => '', 'took' => '');
|
$expected = array('query' => 'ROLLBACK', 'params' => array(), 'affected' => '', 'numRows' => '', 'took' => '');
|
||||||
$log = $db->getLog();
|
$log = $db->getLog();
|
||||||
$this->assertEquals($expected, $log['log'][0]);
|
$this->assertEquals($expected, $log['log'][0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,6 +49,20 @@ if ($noLogs || isset($_forced_from_dbo_)):
|
||||||
<?php
|
<?php
|
||||||
foreach ($logInfo['log'] as $k => $i) :
|
foreach ($logInfo['log'] as $k => $i) :
|
||||||
$i += array('error' => '');
|
$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";
|
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;
|
endforeach;
|
||||||
?>
|
?>
|
||||||
|
@ -58,4 +72,3 @@ if ($noLogs || isset($_forced_from_dbo_)):
|
||||||
else:
|
else:
|
||||||
echo '<p>Encountered unexpected $logs cannot generate SQL log</p>';
|
echo '<p>Encountered unexpected $logs cannot generate SQL log</p>';
|
||||||
endif;
|
endif;
|
||||||
?>
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue