Skip to content

Commit ea5b283

Browse files
authored
Merge pull request #6127 from kenjis/fix-trigger-DBQuery-on-failed-query
fix: event DBQuery is not fired on failed query when DBDebug is true
2 parents 6699530 + 0d8f818 commit ea5b283

3 files changed

Lines changed: 20 additions & 3 deletions

File tree

phpstan-baseline.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ parameters:
137137

138138
-
139139
message: "#^Negated boolean expression is always true\\.$#"
140-
count: 1
140+
count: 2
141141
path: system/Database/BaseConnection.php
142142

143143
-

system/Database/BaseConnection.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Closure;
1515
use CodeIgniter\Database\Exceptions\DatabaseException;
1616
use CodeIgniter\Events\Events;
17+
use Exception;
1718
use stdClass;
1819
use Throwable;
1920

@@ -603,7 +604,14 @@ public function query(string $sql, $binds = null, bool $setEscapeFlags = true, s
603604
$this->lastQuery = $query;
604605

605606
// Run the query for real
606-
if (! $this->pretend && false === ($this->resultID = $this->simpleQuery($query->getQuery()))) {
607+
try {
608+
$exception = null;
609+
$this->resultID = $this->simpleQuery($query->getQuery());
610+
} catch (Exception $exception) {
611+
$this->resultID = false;
612+
}
613+
614+
if (! $this->pretend && $this->resultID === false) {
607615
$query->setDuration($startTime, $startTime);
608616

609617
// This will trigger a rollback if transactions are being used
@@ -626,6 +634,15 @@ public function query(string $sql, $binds = null, bool $setEscapeFlags = true, s
626634
}
627635
}
628636

637+
if (! $this->pretend) {
638+
// Let others do something with this query.
639+
Events::trigger('DBQuery', $query);
640+
}
641+
642+
if ($exception !== null) {
643+
throw $exception;
644+
}
645+
629646
return false;
630647
}
631648

user_guide_src/source/extending/events.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,5 @@ The following is a list of available event points within the CodeIgniter core co
9191
* **post_controller_constructor** Called immediately after your controller is instantiated, but prior to any method calls happening.
9292
* **post_system** Called after the final rendered page is sent to the browser, at the end of system execution after the finalized data is sent to the browser.
9393
* **email** Called after an email sent successfully from ``CodeIgniter\Email\Email``. Receives an array of the ``Email`` class's properties as a parameter.
94-
* **DBQuery** Called after a successfully-completed database query. Receives the ``Query`` object.
94+
* **DBQuery** Called after a database query whether successful or not. Receives the ``Query`` object.
9595
* **migrate** Called after a successful migration call to ``latest()`` or ``regress()``. Receives the current properties of ``MigrationRunner`` as well as the name of the method.

0 commit comments

Comments
 (0)