Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: event DBQuery is not fired on failed query when DBDebug is true #6127

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.