sql_dump.ctp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package Cake.View.Elements
  17. * @since CakePHP(tm) v 1.3
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. if (!class_exists('ConnectionManager') || Configure::read('debug') < 2) {
  21. return false;
  22. }
  23. $noLogs = !isset($logs);
  24. if ($noLogs):
  25. $sources = ConnectionManager::sourceList();
  26. $logs = array();
  27. foreach ($sources as $source):
  28. $db = ConnectionManager::getDataSource($source);
  29. if (!method_exists($db, 'getLog')):
  30. continue;
  31. endif;
  32. $logs[$source] = $db->getLog();
  33. endforeach;
  34. endif;
  35. if ($noLogs || isset($_forced_from_dbo_)):
  36. foreach ($logs as $source => $logInfo):
  37. $text = $logInfo['count'] > 1 ? 'queries' : 'query';
  38. printf(
  39. '<table class="cake-sql-log" id="cakeSqlLog_%s" summary="Cake SQL Log" cellspacing="0">',
  40. preg_replace('/[^A-Za-z0-9_]/', '_', uniqid(time(), true))
  41. );
  42. printf('<caption>(%s) %s %s took %s ms</caption>', $source, $logInfo['count'], $text, $logInfo['time']);
  43. ?>
  44. <thead>
  45. <tr><th>Nr</th><th>Query</th><th>Error</th><th>Affected</th><th>Num. rows</th><th>Took (ms)</th></tr>
  46. </thead>
  47. <tbody>
  48. <?php
  49. foreach ($logInfo['log'] as $k => $i) :
  50. $i += array('error' => '');
  51. if (!empty($i['params']) && is_array($i['params'])) {
  52. $bindParam = $bindType = null;
  53. if (preg_match('/.+ :.+/', $i['query'])) {
  54. $bindType = true;
  55. }
  56. foreach ($i['params'] as $bindKey => $bindVal) {
  57. if ($bindType === true) {
  58. $bindParam .= h($bindKey) ." => " . h($bindVal) . ", ";
  59. } else {
  60. $bindParam .= h($bindVal) . ", ";
  61. }
  62. }
  63. $i['query'] .= " , params[ " . rtrim($bindParam, ', ') . " ]";
  64. }
  65. 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";
  66. endforeach;
  67. ?>
  68. </tbody></table>
  69. <?php
  70. endforeach;
  71. else:
  72. echo '<p>Encountered unexpected $logs cannot generate SQL log</p>';
  73. endif;