Skip to content
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
1 change: 1 addition & 0 deletions docs/changelogs/v4.7.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Release Date: Unreleased

- **Cache:** Fixed a bug where `PredisHandler::increment()` and `PredisHandler::decrement()` updated the unused `'data'` hash field instead of `'__ci_value'`, causing `get()` to return stale values for existing counters.
- **CORS:** Fixed a bug where `Cors::hasResponseHeaders()` matched header substrings via `str_contains()`, causing false positives when existing exposed headers shared a prefix with configured exposed headers.
- **Database:** Fixed a bug where `BaseConnection::prepare()` did not reset pretend mode when the closure threw an exception, leaving the connection in pretend mode so that subsequent queries on it were silently discarded.
- **Database:** Fixed a bug where `Postgre`, `OCI8`, and `SQLSRV` `Forge::_dropKeyAsConstraint()` interpolated table, constraint, and schema names without escaping.
- **Database:** Fixed a bug where `Postgre\Connection::insertID()` interpolated table, column, and sequence names without escaping.
- **Database:** Fixed a bug where `SQLSRV\Builder::_upsertBatch()` concatenated `$table` unescaped into `Object_ID()`, causing syntax errors for table names with quotes or special characters.
Expand Down
8 changes: 5 additions & 3 deletions system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1188,9 +1188,11 @@ public function prepare(Closure $func, array $options = [])

$this->pretend();

$sql = $func($this);

$this->pretend(false);
try {
$sql = $func($this);
} finally {
$this->pretend(false);
}

if ($sql instanceof QueryInterface) {
$sql = $sql->getOriginalQuery();
Expand Down
20 changes: 20 additions & 0 deletions tests/system/Database/Live/PretendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use PHPUnit\Framework\Attributes\Group;
use RuntimeException;

/**
* @internal
Expand Down Expand Up @@ -46,4 +47,23 @@ public function testPretendReturnsQueryObject(): void

$this->assertInstanceOf(Query::class, $result);
}

public function testPrepareResetsPretendWhenClosureThrowsException(): void
{
$this->db->pretend(false);

$exception = new RuntimeException('Error in prepare closure');

try {
$this->db->prepare(static function () use ($exception): never {
throw $exception;
});

$this->fail('prepare() must rethrow the exception thrown by the closure.');
} catch (RuntimeException $e) {
$this->assertSame($exception, $e, 'prepare() must rethrow the exception thrown by the closure.');
}

$this->assertFalse($this->db->pretend, 'prepare() must reset pretend mode when the closure throws.');
}
}
Loading