From 2a85e21e3543d02069aef9f54c366d2cad924252 Mon Sep 17 00:00:00 2001 From: Thomas Portelange Date: Tue, 24 Oct 2023 17:54:01 +0200 Subject: [PATCH 1/3] Counting should not interfere with iterator --- code/SQLite3Query.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/code/SQLite3Query.php b/code/SQLite3Query.php index 6b42f77..3ee3c0c 100644 --- a/code/SQLite3Query.php +++ b/code/SQLite3Query.php @@ -26,6 +26,8 @@ class SQLite3Query extends Query */ protected $handle; + protected int $count = 0; + /** * Hook the result-set given into a Query class, suitable for use by framework. * @param SQLite3Connector $database The database object that created this query. @@ -35,6 +37,8 @@ class SQLite3Query extends Query { $this->database = $database; $this->handle = $handle; + // Count early to make sure we don't interfere with the generator and rewind operation + $this->count = $this->countRecords(); } public function __destruct() @@ -44,10 +48,15 @@ class SQLite3Query extends Query } } + public function numRecords() + { + return $this->count; + } + /** * @todo This looks terrible but there is no SQLite3::get_num_rows() implementation */ - public function numRecords() + private function countRecords() { // Some queries are not iterable using fetchArray like CREATE statement if (!$this->handle->numColumns()) { @@ -55,7 +64,7 @@ class SQLite3Query extends Query } $this->handle->reset(); - $c=0; + $c = 0; while ($this->handle->fetchArray()) { $c++; } From 52e29831bc92eb1a04eaa7288ed7894f676b7e00 Mon Sep 17 00:00:00 2001 From: Thomas Portelange Date: Tue, 24 Oct 2023 17:56:26 +0200 Subject: [PATCH 2/3] change scope --- code/SQLite3Query.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/SQLite3Query.php b/code/SQLite3Query.php index 3ee3c0c..9047ef6 100644 --- a/code/SQLite3Query.php +++ b/code/SQLite3Query.php @@ -26,7 +26,7 @@ class SQLite3Query extends Query */ protected $handle; - protected int $count = 0; + private int $count = 0; /** * Hook the result-set given into a Query class, suitable for use by framework. From c77d1c208040caf443fee618e975321f8b46b21c Mon Sep 17 00:00:00 2001 From: Thomas Portelange Date: Wed, 25 Oct 2023 11:07:09 +0200 Subject: [PATCH 3/3] add return type, remove unecessary reset --- code/SQLite3Query.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/code/SQLite3Query.php b/code/SQLite3Query.php index 9047ef6..0fc5b18 100644 --- a/code/SQLite3Query.php +++ b/code/SQLite3Query.php @@ -56,14 +56,13 @@ class SQLite3Query extends Query /** * @todo This looks terrible but there is no SQLite3::get_num_rows() implementation */ - private function countRecords() + private function countRecords(): int { // Some queries are not iterable using fetchArray like CREATE statement if (!$this->handle->numColumns()) { return 0; } - $this->handle->reset(); $c = 0; while ($this->handle->fetchArray()) { $c++;