sql_dump.ctp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * SQL Dump element. Dumps out SQL log information
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.View.Elements
  16. * @since CakePHP(tm) v 1.3
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. if (!class_exists('ConnectionManager') || Configure::read('debug') < 2) {
  20. return false;
  21. }
  22. $noLogs = !isset($logs);
  23. if ($noLogs):
  24. $sources = ConnectionManager::sourceList();
  25. $logs = array();
  26. foreach ($sources as $source):
  27. $db = ConnectionManager::getDataSource($source);
  28. if (!method_exists($db, 'getLog')):
  29. continue;
  30. endif;
  31. $logs[$source] = $db->getLog();
  32. endforeach;
  33. endif;
  34. if ($noLogs || isset($_forced_from_dbo_)):
  35. foreach ($logs as $source => $logInfo):
  36. $text = $logInfo['count'] > 1 ? 'queries' : 'query';
  37. printf(
  38. '<table class="cake-sql-log" id="cakeSqlLog_%s" summary="Cake SQL Log" cellspacing="0" border = "0">',
  39. preg_replace('/[^A-Za-z0-9_]/', '_', uniqid(time(), true))
  40. );
  41. printf('<caption>(%s) %s %s took %s ms</caption>', $source, $logInfo['count'], $text, $logInfo['time']);
  42. ?>
  43. <thead>
  44. <tr><th>Nr</th><th>Query</th><th>Error</th><th>Affected</th><th>Num. rows</th><th>Took (ms)</th></tr>
  45. </thead>
  46. <tbody>
  47. <?php
  48. foreach ($logInfo['log'] as $k => $i) :
  49. $i += array('error' => '');
  50. 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";
  51. endforeach;
  52. ?>
  53. </tbody></table>
  54. <?php
  55. endforeach;
  56. else:
  57. echo '<p>Encountered unexpected $logs cannot generate SQL log</p>';
  58. endif;
  59. ?>