Permalink
Browse files

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

Updating tests to reflect changes in getLog()
  • Loading branch information...
1 parent cbb29dc commit a89fcba2432d48e31ba43f526679510f4d1b47cf @markstory markstory committed Dec 4, 2009
@@ -494,12 +494,15 @@ function hasResult() {
* @param boolean $sorted Get the queries sorted by time taken, defaults to false.
* @return array Array of queries run as an array
*/
- function getLog($sorted = false) {
+ function getLog($sorted = false, $clear = true) {
if ($sorted) {
$log = sortByKey($this->_queriesLog, 'took', 'desc', SORT_NUMERIC);
} else {
$log = $this->_queriesLog;
}
+ if ($clear) {
+ $this->_queriesLog = array();
+ }
return array('log' => $log, 'count' => $this->_queriesCnt, 'time' => $this->_queriesTime);
}
@@ -509,26 +512,18 @@ function getLog($sorted = false) {
* @param boolean $sorted Get the queries sorted by time taken, defaults to false
*/
function showLog($sorted = false) {
- return false;
-
- $log = $this->getLog($sorted);
-
- if ($this->_queriesCnt > 1) {
- $text = 'queries';
- } else {
- $text = 'query';
+ $log = $this->getLog($sorted, false);
+ if (empty($log['log'])) {
+ return;
}
-
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");
- 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");
-
- foreach ($log as $k => $i) {
- 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");
- }
- print ("</tbody></table>\n");
+ App::import('Core', 'View');
+ $controller = null;
+ $View =& new View($controller, false);
+ $View->set('logs', array($this->configKeyName => $log));
+ echo $View->element('sql_dump');
} else {
- foreach ($log as $k => $i) {
+ foreach ($log['log'] as $k => $i) {
print (($k + 1) . ". {$i['query']} {$i['error']}\n");
}
}
@@ -1,16 +1,22 @@
<?php
-if (!class_exists('ConnectionManager') || Configure::read('debug') < 2) {
+if (!class_exists('ConnectionManager') || Configure::read('debug') > 1) {
return false;
}
$sources = ConnectionManager::sourceList();
-foreach ($sources as $source):
- $db =& ConnectionManager::getDataSource($source);
- if (!$db->isInterfaceSupported('getLog')) {
- continue;
- }
- $logInfo = $db->getLog();
+if (!isset($logs)):
+ $logs = array();
+ foreach ($sources as $source):
+ $db =& ConnectionManager::getDataSource($source);
+ if (!$db->isInterfaceSupported('getLog')):
+ continue;
+ endif;
+ $logs[$source] = $db->getLog();
+ endforeach;
+endif;
+
+foreach ($logs as $source => $logInfo):
$text = $logInfo['count'] > 1 ? 'queries' : 'query';
printf(
'<table class="cake-sql-log" id="cakeSqlLog_%s" summary="Cake SQL Log" cellspacing="0" border = "0">',
@@ -4011,11 +4011,11 @@ function testGetLog() {
$log = $this->testDb->getLog();
$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' => '');
- $this->assertEqual($log[1], $expected);
+ $this->assertEqual($log['log'][1], $expected);
$expected = array('query' => 'Error 1', 'error' => true, 'affected' => '', 'numRows' => '', 'took' => '');
- $this->assertEqual($log[2], $expected);
+ $this->assertEqual($log['log'][2], $expected);
}
/**

0 comments on commit a89fcba

Please sign in to comment.