Skip to content

Commit

Permalink
Merge pull request #6127 from kenjis/fix-trigger-DBQuery-on-failed-query
Browse files Browse the repository at this point in the history
fix: event DBQuery is not fired on failed query when DBDebug is true
  • Loading branch information
kenjis authored Jun 15, 2022
2 parents 6699530 + 0d8f818 commit ea5b283
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion phpstan-baseline.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ parameters:

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

-
Expand Down
19 changes: 18 additions & 1 deletion system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Closure;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Events\Events;
use Exception;
use stdClass;
use Throwable;

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

// Run the query for real
if (! $this->pretend && false === ($this->resultID = $this->simpleQuery($query->getQuery()))) {
try {
$exception = null;
$this->resultID = $this->simpleQuery($query->getQuery());
} catch (Exception $exception) {
$this->resultID = false;
}

if (! $this->pretend && $this->resultID === false) {
$query->setDuration($startTime, $startTime);

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

if (! $this->pretend) {
// Let others do something with this query.
Events::trigger('DBQuery', $query);
}

if ($exception !== null) {
throw $exception;
}

return false;
}

Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/extending/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,5 @@ The following is a list of available event points within the CodeIgniter core co
* **post_controller_constructor** Called immediately after your controller is instantiated, but prior to any method calls happening.
* **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.
* **email** Called after an email sent successfully from ``CodeIgniter\Email\Email``. Receives an array of the ``Email`` class's properties as a parameter.
* **DBQuery** Called after a successfully-completed database query. Receives the ``Query`` object.
* **DBQuery** Called after a database query whether successful or not. Receives the ``Query`` object.
* **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 comments on commit ea5b283

Please sign in to comment.