Skip to content

Commit cc0d0b9

Browse files
authored
Merge pull request #1032 from cakephp/5.x-transaction-styling
add styling for queries which are run inside a transaction
2 parents bf377ad + 95c834a commit cc0d0b9

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

src/Database/Log/DebugLog.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ class DebugLog extends AbstractLogger
6565
*/
6666
protected bool $_includeSchema = false;
6767

68+
/**
69+
* Whether a transaction is currently open or not.
70+
*
71+
* @var bool
72+
*/
73+
protected bool $inTransaction = false;
74+
6875
/**
6976
* Constructor
7077
*
@@ -162,11 +169,25 @@ public function log($level, string|Stringable $message, array $context = []): vo
162169

163170
$this->_totalTime += $data['took'];
164171

172+
$sql = (string)$query;
173+
$isBegin = $sql === 'BEGIN';
174+
$isCommitOrRollback = $sql === 'COMMIT' || $sql === 'ROLLBACK';
175+
176+
if ($isBegin) {
177+
$this->inTransaction = true;
178+
}
179+
165180
$this->_queries[] = [
166-
'query' => (string)$query,
181+
'query' => $sql,
167182
'took' => $data['took'],
168183
'rows' => $data['numRows'],
184+
'inTransaction' => $this->inTransaction,
185+
'isCommitOrRollback' => $isCommitOrRollback,
169186
];
187+
188+
if ($isCommitOrRollback) {
189+
$this->inTransaction = false;
190+
}
170191
}
171192

172193
/**

templates/element/sql_log_panel.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
</thead>
7474
<tbody>
7575
<?php foreach ($queries as $query) : ?>
76-
<tr>
76+
<tr<?= $query['inTransaction'] ? ' class="in-transaction"' : '' ?>>
7777
<td>
7878
<?=
7979
(new SqlFormatter(
@@ -91,6 +91,12 @@
9191
<td><?= h($query['rows']) ?></td>
9292
<td><?= h($query['took']) ?></td>
9393
</tr>
94+
<?php if ($query['isCommitOrRollback']): ?>
95+
<tr>
96+
<td colspan="3" class="commit-or-rollback">
97+
</td>
98+
</tr>
99+
<?php endif; ?>
94100
<?php endforeach; ?>
95101
</tbody>
96102
</table>
@@ -99,6 +105,6 @@
99105
<?php endif; ?>
100106

101107
<?php if ($noOutput) : ?>
102-
<div class="c-flash c-flash--warning">No active database connections</div>
108+
<div class="c-flash c-flash--warning">No active database connections</div>
103109
<?php endif ?>
104110
</div>

webroot/css/style.css

+4
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,10 @@ strong {
605605
box-shadow: 0 2px 0 var(--routes-btn-active-border);
606606
}
607607

608+
.c-sql-log-panel__entry .in-transaction {
609+
background: var(--cake-light-gray);
610+
}
611+
608612
.c-toolbar {
609613
display: flex;
610614
background: var(--toolbar-bg);

0 commit comments

Comments
 (0)