Updating DboSource::getLog() and showLog() to use a View element.

Updating tests to reflect changes in getLog()
This commit is contained in:
Mark Story 2009-12-03 23:29:28 -05:00
parent cbb29dcc5d
commit a89fcba243
3 changed files with 29 additions and 28 deletions

View file

@ -494,12 +494,15 @@ class DboSource extends DataSource {
* @param boolean $sorted Get the queries sorted by time taken, defaults to false. * @param boolean $sorted Get the queries sorted by time taken, defaults to false.
* @return array Array of queries run as an array * @return array Array of queries run as an array
*/ */
function getLog($sorted = false) { function getLog($sorted = false, $clear = true) {
if ($sorted) { if ($sorted) {
$log = sortByKey($this->_queriesLog, 'took', 'desc', SORT_NUMERIC); $log = sortByKey($this->_queriesLog, 'took', 'desc', SORT_NUMERIC);
} else { } else {
$log = $this->_queriesLog; $log = $this->_queriesLog;
} }
if ($clear) {
$this->_queriesLog = array();
}
return array('log' => $log, 'count' => $this->_queriesCnt, 'time' => $this->_queriesTime); return array('log' => $log, 'count' => $this->_queriesCnt, 'time' => $this->_queriesTime);
} }
@ -509,26 +512,18 @@ class DboSource extends DataSource {
* @param boolean $sorted Get the queries sorted by time taken, defaults to false * @param boolean $sorted Get the queries sorted by time taken, defaults to false
*/ */
function showLog($sorted = false) { function showLog($sorted = false) {
return false; $log = $this->getLog($sorted, false);
if (empty($log['log'])) {
$log = $this->getLog($sorted); return;
if ($this->_queriesCnt > 1) {
$text = 'queries';
} else {
$text = 'query';
} }
if (PHP_SAPI != 'cli') { if (PHP_SAPI != 'cli') {
print ("<table class=\"cake-sql-log\" id=\"cakeSqlLog_" . preg_replace('/[^A-Za-z0-9_]/', '_', uniqid(time(), true)) . "\" summary=\"Cake SQL Log\" cellspacing=\"0\" border = \"0\">\n<caption>({$this->configKeyName}) {$this->_queriesCnt} {$text} took {$this->_queriesTime} ms</caption>\n"); App::import('Core', 'View');
print ("<thead>\n<tr><th>Nr</th><th>Query</th><th>Error</th><th>Affected</th><th>Num. rows</th><th>Took (ms)</th></tr>\n</thead>\n<tbody>\n"); $controller = null;
$View =& new View($controller, false);
foreach ($log as $k => $i) { $View->set('logs', array($this->configKeyName => $log));
print ("<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 $View->element('sql_dump');
}
print ("</tbody></table>\n");
} else { } else {
foreach ($log as $k => $i) { foreach ($log['log'] as $k => $i) {
print (($k + 1) . ". {$i['query']} {$i['error']}\n"); print (($k + 1) . ". {$i['query']} {$i['error']}\n");
} }
} }

View file

@ -1,16 +1,22 @@
<?php <?php
if (!class_exists('ConnectionManager') || Configure::read('debug') < 2) { if (!class_exists('ConnectionManager') || Configure::read('debug') > 1) {
return false; return false;
} }
$sources = ConnectionManager::sourceList(); $sources = ConnectionManager::sourceList();
foreach ($sources as $source): if (!isset($logs)):
$db =& ConnectionManager::getDataSource($source); $logs = array();
if (!$db->isInterfaceSupported('getLog')) { foreach ($sources as $source):
continue; $db =& ConnectionManager::getDataSource($source);
} if (!$db->isInterfaceSupported('getLog')):
$logInfo = $db->getLog(); continue;
endif;
$logs[$source] = $db->getLog();
endforeach;
endif;
foreach ($logs as $source => $logInfo):
$text = $logInfo['count'] > 1 ? 'queries' : 'query'; $text = $logInfo['count'] > 1 ? 'queries' : 'query';
printf( printf(
'<table class="cake-sql-log" id="cakeSqlLog_%s" summary="Cake SQL Log" cellspacing="0" border = "0">', '<table class="cake-sql-log" id="cakeSqlLog_%s" summary="Cake SQL Log" cellspacing="0" border = "0">',

View file

@ -4011,11 +4011,11 @@ class DboSourceTest extends CakeTestCase {
$log = $this->testDb->getLog(); $log = $this->testDb->getLog();
$expected = array('query' => 'Query 1', 'error' => '', 'affected' => '', 'numRows' => '', 'took' => ''); $expected = array('query' => 'Query 1', 'error' => '', 'affected' => '', 'numRows' => '', 'took' => '');
$this->assertEqual($log[0], $expected); $this->assertEqual($log['log'][0], $expected);
$expected = array('query' => 'Query 2', 'error' => '', 'affected' => '', 'numRows' => '', 'took' => ''); $expected = array('query' => 'Query 2', 'error' => '', 'affected' => '', 'numRows' => '', 'took' => '');
$this->assertEqual($log[1], $expected); $this->assertEqual($log['log'][1], $expected);
$expected = array('query' => 'Error 1', 'error' => true, 'affected' => '', 'numRows' => '', 'took' => ''); $expected = array('query' => 'Error 1', 'error' => true, 'affected' => '', 'numRows' => '', 'took' => '');
$this->assertEqual($log[2], $expected); $this->assertEqual($log['log'][2], $expected);
} }
/** /**