diff --git a/src/Database.php b/src/Database.php index f49b260..868945a 100644 --- a/src/Database.php +++ b/src/Database.php @@ -147,6 +147,7 @@ public function getConnection(): \PDO public function closeConnection(): void { $this->connection = null; + $this->statementPool->clear(); } /** diff --git a/src/Exception/ClosedConnectionException.php b/src/Exception/ClosedConnectionException.php index c716303..59c047a 100644 --- a/src/Exception/ClosedConnectionException.php +++ b/src/Exception/ClosedConnectionException.php @@ -1,5 +1,16 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + namespace Access\Exception; use Access\Exception; diff --git a/src/StatementPool.php b/src/StatementPool.php index 3c3eeaa..9841108 100644 --- a/src/StatementPool.php +++ b/src/StatementPool.php @@ -14,6 +14,7 @@ namespace Access; use Access\Database; +use Countable; /** * Query statement pool @@ -22,7 +23,7 @@ * * @author Tim */ -final class StatementPool +final class StatementPool implements Countable { /** * All open prepared statements @@ -72,4 +73,14 @@ public function clear(): void { $this->stmtPool = []; } + + /** + * Get number of prepared statements + * + * @return int + */ + public function count(): int + { + return count($this->stmtPool); + } } diff --git a/tests/base/BaseDatabaseTest.php b/tests/base/BaseDatabaseTest.php index db8f09b..310a97e 100644 --- a/tests/base/BaseDatabaseTest.php +++ b/tests/base/BaseDatabaseTest.php @@ -331,13 +331,18 @@ public function testCloseConnection(): void { $db = static::createDatabaseWithDummyData(); + $db->getStatementPool()->clear(); + $db->getStatementPool()->prepare('SELECT 1'); + $this->assertCount(1, $db->getStatementPool()); + $connection = $db->getConnection(); - self::assertInstanceOf(PDO::class, $connection); + $this->assertInstanceOf(PDO::class, $connection); $db->closeConnection(); + $this->assertEmpty($db->getStatementPool()); - self::expectException(ClosedConnectionException::class); - self::expectExceptionMessage('Connection is closed'); + $this->expectException(ClosedConnectionException::class); + $this->expectExceptionMessage('Connection is closed'); $db->getConnection(); } }