sql_dump.ctp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. * @since CakePHP(tm) v 1.3
  17. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  18. */
  19. if (!class_exists('ConnectionManager') || Configure::read('debug') < 2) {
  20. return false;
  21. }
  22. $noLogs = !isset($sqlLogs);
  23. if ($noLogs):
  24. $sources = ConnectionManager::sourceList();
  25. $sqlLogs = array();
  26. foreach ($sources as $source):
  27. $db = ConnectionManager::getDataSource($source);
  28. if (!method_exists($db, 'getLog')):
  29. continue;
  30. endif;
  31. $sqlLogs[$source] = $db->getLog();
  32. endforeach;
  33. endif;
  34. if ($noLogs || isset($_forced_from_dbo_)):
  35. foreach ($sqlLogs 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. printf('<tr><td>%d</td><td>%s</td><td>%s</td><td style="text-align: right">%d</td><td style="text-align: right">%d</td><td style="text-align: right">%d</td></tr>%s',
  65. $k + 1,
  66. h($i['query']),
  67. $i['error'],
  68. $i['affected'],
  69. $i['numRows'],
  70. $i['took'],
  71. "\n"
  72. );
  73. endforeach;
  74. ?>
  75. </tbody></table>
  76. <?php
  77. endforeach;
  78. else:
  79. printf('<p>%s</p>', __d('cake_dev', 'Encountered unexpected %s. Cannot generate SQL log.', '$sqlLogs'));
  80. endif;