sql_dump.ctp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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-2012, 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-2012, 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">',
  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. if (!empty($i['params']) && is_array($i['params'])) {
  51. $bindParam = $bindType = null;
  52. if (preg_match('/.+ :.+/', $i['query'])) {
  53. $bindType = true;
  54. }
  55. foreach ($i['params'] as $bindKey => $bindVal) {
  56. if ($bindType === true) {
  57. $bindParam .= h($bindKey) ." => " . h($bindVal) . ", ";
  58. } else {
  59. $bindParam .= h($bindVal) . ", ";
  60. }
  61. }
  62. $i['query'] .= " , params[ " . rtrim($bindParam, ', ') . " ]";
  63. }
  64. 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";
  65. endforeach;
  66. ?>
  67. </tbody></table>
  68. <?php
  69. endforeach;
  70. else:
  71. echo '<p>Encountered unexpected $logs cannot generate SQL log</p>';
  72. endif;