pictcode / lib / Cake / View / Elements / sql_dump.ctp @ master
履歴 | 表示 | アノテート | ダウンロード (2.586 KB)
1 |
<?php |
---|---|
2 |
/** |
3 |
* SQL Dump element. Dumps out SQL log information |
4 |
* |
5 |
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
6 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
7 |
* |
8 |
* Licensed under The MIT License |
9 |
* For full copyright and license information, please see the LICENSE.txt |
10 |
* Redistributions of files must retain the above copyright notice. |
11 |
* |
12 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) |
13 |
* @link http://cakephp.org CakePHP(tm) Project |
14 |
* @package Cake.View.Elements |
15 |
* @since CakePHP(tm) v 1.3 |
16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
17 |
*/ |
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 |
|
26 |
$sqlLogs = array(); |
27 |
foreach ($sources as $source): |
28 |
$db = ConnectionManager::getDataSource($source); |
29 |
if (!method_exists($db, 'getLog')): |
30 |
continue; |
31 |
endif; |
32 |
$sqlLogs[$source] = $db->getLog(); |
33 |
endforeach; |
34 |
endif; |
35 |
|
36 |
if ($noLogs || isset($_forced_from_dbo_)): |
37 |
foreach ($sqlLogs as $source => $logInfo): |
38 |
$text = $logInfo['count'] > 1 ? 'queries' : 'query'; |
39 |
printf( |
40 |
'<table class="cake-sql-log" id="cakeSqlLog_%s" summary="Cake SQL Log" cellspacing="0">', |
41 |
preg_replace('/[^A-Za-z0-9_]/', '_', uniqid(time(), true)) |
42 |
); |
43 |
printf('<caption>(%s) %s %s took %s ms</caption>', $source, $logInfo['count'], $text, $logInfo['time']); |
44 |
?> |
45 |
<thead> |
46 |
<tr><th>Nr</th><th>Query</th><th>Error</th><th>Affected</th><th>Num. rows</th><th>Took (ms)</th></tr> |
47 |
</thead> |
48 |
<tbody> |
49 |
<?php |
50 |
foreach ($logInfo['log'] as $k => $i) : |
51 |
$i += array('error' => ''); |
52 |
if (!empty($i['params']) && is_array($i['params'])) { |
53 |
$bindParam = $bindType = null; |
54 |
if (preg_match('/.+ :.+/', $i['query'])) { |
55 |
$bindType = true; |
56 |
} |
57 |
foreach ($i['params'] as $bindKey => $bindVal) { |
58 |
if ($bindType === true) { |
59 |
$bindParam .= h($bindKey) . " => " . h($bindVal) . ", "; |
60 |
} else { |
61 |
$bindParam .= h($bindVal) . ", "; |
62 |
} |
63 |
} |
64 |
$i['query'] .= " , params[ " . rtrim($bindParam, ', ') . " ]"; |
65 |
} |
66 |
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', |
67 |
$k + 1, |
68 |
h($i['query']), |
69 |
$i['error'], |
70 |
$i['affected'], |
71 |
$i['numRows'], |
72 |
$i['took'], |
73 |
"\n" |
74 |
); |
75 |
endforeach; |
76 |
?> |
77 |
</tbody></table> |
78 |
<?php |
79 |
endforeach; |
80 |
else: |
81 |
printf('<p>%s</p>', __d('cake_dev', 'Encountered unexpected %s. Cannot generate SQL log.', '$sqlLogs')); |
82 |
endif; |