Skip to content

Commit 5569e23

Browse files
authored
Merge pull request #5334 from codeigniter4/toolbar-db-trace
Display file:line and trace information to database queries in debug toolbar
2 parents 92eca19 + ccc4a5e commit 5569e23

File tree

5 files changed

+66
-11
lines changed

5 files changed

+66
-11
lines changed

system/Debug/Toolbar/Collectors/Database.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public static function collect(Query $query)
8989
'query' => $query,
9090
'string' => $queryString,
9191
'duplicate' => in_array($queryString, array_column(static::$queries, 'string', null), true),
92+
'trace' => debug_backtrace(),
9293
];
9394
}
9495
}
@@ -133,11 +134,31 @@ public function display(): array
133134
$data['queries'] = array_map(static function (array $query) {
134135
$isDuplicate = $query['duplicate'] === true;
135136

137+
// Find the first line that doesn't include `system` in the backtrace
138+
$line = [];
139+
140+
foreach ($query['trace'] as &$traceLine) {
141+
// Clean up the file paths
142+
$traceLine['file'] = str_ireplace(APPPATH, 'APPPATH/', $traceLine['file']);
143+
$traceLine['file'] = str_ireplace(SYSTEMPATH, 'SYSTEMPATH/', $traceLine['file']);
144+
$traceLine['file'] = str_ireplace(VENDORPATH, 'VENDORPATH/', $traceLine['file']);
145+
$traceLine['file'] = str_ireplace(ROOTPATH, 'ROOTPATH/', $traceLine['file']);
146+
147+
if (strpos($traceLine['file'], 'SYSTEMPATH') !== false) {
148+
continue;
149+
}
150+
$line = empty($line) ? $traceLine : $line;
151+
}
152+
136153
return [
137-
'hover' => $isDuplicate ? 'This query was called more than once.' : '',
138-
'class' => $isDuplicate ? 'duplicate' : '',
139-
'duration' => ((float) $query['query']->getDuration(5) * 1000) . ' ms',
140-
'sql' => $query['query']->debugToolbarDisplay(),
154+
'hover' => $isDuplicate ? 'This query was called more than once.' : '',
155+
'class' => $isDuplicate ? 'duplicate' : '',
156+
'duration' => ((float) $query['query']->getDuration(5) * 1000) . ' ms',
157+
'sql' => $query['query']->debugToolbarDisplay(),
158+
'trace' => $query['trace'],
159+
'trace-file' => str_replace(ROOTPATH, '/', $line['file'] ?? ''),
160+
'trace-line' => $line['line'] ?? '',
161+
'qid' => md5($query['query'] . microtime()),
141162
];
142163
}, static::$queries);
143164

system/Debug/Toolbar/Views/_database.tpl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@
77
</thead>
88
<tbody>
99
{queries}
10-
<tr class="{class}" title="{hover}">
10+
<tr class="{class}" title="{hover}" data-toggle="{qid}-trace">
1111
<td class="narrow">{duration}</td>
1212
<td>{! sql !}</td>
13+
<td style="text-align: right">{trace-file}:<strong>{trace-line}</strong></td>
14+
</tr>
15+
<tr class="muted" id="{qid}-trace" style="display:none">
16+
<td></td>
17+
<td colspan="2">
18+
{trace}
19+
{file}:<strong>{line}</strong><br/>
20+
{/trace}
21+
</td>
1322
</tr>
1423
{/queries}
1524
</tbody>

system/Debug/Toolbar/Views/toolbar.css

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,8 @@
344344

345345
.debug-view.show-view {
346346
border-color: #DD8615; }
347+
#debug-bar tr[data-toggle] {
348+
cursor: pointer; }
347349

348350
.debug-view-path {
349351
background-color: #FDC894;
@@ -407,7 +409,7 @@
407409
#debug-bar .muted {
408410
color: #DFDFDF; }
409411
#debug-bar .muted td {
410-
color: #434343; }
412+
color: #797979; }
411413
#debug-bar .muted:hover td {
412414
color: #DFDFDF; }
413415
#debug-bar #toolbar-position,
@@ -496,7 +498,7 @@
496498
#toolbarContainer.dark #debug-bar .muted {
497499
color: #DFDFDF; }
498500
#toolbarContainer.dark #debug-bar .muted td {
499-
color: #434343; }
501+
color: #797979; }
500502
#toolbarContainer.dark #debug-bar .muted:hover td {
501503
color: #DFDFDF; }
502504
#toolbarContainer.dark #debug-bar #toolbar-position,
@@ -587,9 +589,9 @@
587589
-moz-box-shadow: 0 1px 4px #DFDFDF;
588590
-webkit-box-shadow: 0 1px 4px #DFDFDF; }
589591
#toolbarContainer.light #debug-bar .muted {
590-
color: #434343; }
592+
color: #797979; }
591593
#toolbarContainer.light #debug-bar .muted td {
592-
color: #DFDFDF; }
594+
color: #797979; }
593595
#toolbarContainer.light #debug-bar .muted:hover td {
594596
color: #434343; }
595597
#toolbarContainer.light #debug-bar #toolbar-position,

system/Debug/Toolbar/Views/toolbar.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ var ciDebugBar = {
5858
{
5959
buttons[i].addEventListener('click', ciDebugBar.showTab, true);
6060
}
61+
62+
// Hook up generic toggle via data attributes `data-toggle="foo"`
63+
var links = document.querySelectorAll('[data-toggle]');
64+
for (var i = 0; i < links.length; i++)
65+
{
66+
links[i].addEventListener('click', ciDebugBar.toggleRows, true);
67+
}
6168
},
6269

6370
showTab: function () {
@@ -124,6 +131,21 @@ var ciDebugBar = {
124131
}
125132
},
126133

134+
/**
135+
* Toggle display of another object based on
136+
* the data-toggle value of this object
137+
*
138+
* @param event
139+
*/
140+
toggleRows : function(event) {
141+
if(event.target)
142+
{
143+
let row = event.target.closest('tr');
144+
let target = document.getElementById(row.getAttribute('data-toggle'));
145+
target.style.display = target.style.display === 'none' ? 'table-row' : 'none';
146+
}
147+
},
148+
127149
/**
128150
* Toggle display of a data table
129151
*
@@ -137,7 +159,7 @@ var ciDebugBar = {
137159

138160
if (obj)
139161
{
140-
obj.style.display = obj.style.display == 'none' ? 'block' : 'none';
162+
obj.style.display = obj.style.display === 'none' ? 'block' : 'none';
141163
}
142164
},
143165

@@ -155,7 +177,7 @@ var ciDebugBar = {
155177

156178
if (par && obj)
157179
{
158-
obj.style.display = obj.style.display == 'none' ? '' : 'none';
180+
obj.style.display = obj.style.display === 'none' ? '' : 'none';
159181
par.classList.toggle('timeline-parent-open');
160182
}
161183
},

user_guide_src/source/changelogs/v4.1.6.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ BREAKING
1414

1515
Enhancements
1616
============
17+
- Database pane on debug toolbar now displays location where Query was called from. Also displays full backtrace.
1718

1819
Changes
1920
=======

0 commit comments

Comments
 (0)