Skip to content

Commit

Permalink
add styling for queries which are run inside a transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
LordSimal committed Dec 29, 2024
1 parent bf377ad commit d97e298
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
23 changes: 22 additions & 1 deletion src/Database/Log/DebugLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ class DebugLog extends AbstractLogger
*/
protected bool $_includeSchema = false;

/**
* Whether a transaction is currently open or not.
*
* @var bool
*/
protected bool $inTransaction = false;

/**
* Constructor
*
Expand Down Expand Up @@ -162,11 +169,25 @@ public function log($level, string|Stringable $message, array $context = []): vo

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

$sql = (string)$query;
$isBegin = $sql === 'BEGIN';
$isCommitOrRollback = $sql === 'COMMIT' || $sql === 'ROLLBACK';

if ($isBegin) {
$this->inTransaction = true;
}

$this->_queries[] = [
'query' => (string)$query,
'query' => $sql,
'took' => $data['took'],
'rows' => $data['numRows'],
'inTransaction' => $this->inTransaction,
'isCommitOrRollback' => $isCommitOrRollback,
];

if ($isCommitOrRollback) {
$this->inTransaction = false;
}
}

/**
Expand Down
10 changes: 8 additions & 2 deletions templates/element/sql_log_panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
</thead>
<tbody>
<?php foreach ($queries as $query) : ?>
<tr>
<tr<?= $query['inTransaction'] ? ' class="in-transaction"' : '' ?>>
<td>
<?=
(new SqlFormatter(
Expand All @@ -91,6 +91,12 @@
<td><?= h($query['rows']) ?></td>
<td><?= h($query['took']) ?></td>
</tr>
<?php if($query['isCommitOrRollback']): ?>
<tr>
<td colspan="3" class="commit-or-rollback">
</td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>
Expand All @@ -99,6 +105,6 @@
<?php endif; ?>

<?php if ($noOutput) : ?>
<div class="c-flash c-flash--warning">No active database connections</div>
<div class="c-flash c-flash--warning">No active database connections</div>
<?php endif ?>
</div>
6 changes: 6 additions & 0 deletions webroot/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
--packages-warning: #ffcc00;
--packages-link: #000;

--sql-log-transaction: #d8d8d8;

--routes-btn-active-bg: #fff;
--routes-btn-active-text: #555;
--routes-btn-active-border: #6f6f6f;
Expand Down Expand Up @@ -605,6 +607,10 @@ strong {
box-shadow: 0 2px 0 var(--routes-btn-active-border);
}

.c-sql-log-panel__entry .in-transaction {
background: var(--sql-log-transaction);
}

.c-toolbar {
display: flex;
background: var(--toolbar-bg);
Expand Down

0 comments on commit d97e298

Please sign in to comment.