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

Counting should not interfere with iterator #74

Open
wants to merge 6 commits into
base: 3.0
Choose a base branch
from
Open
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
14 changes: 11 additions & 3 deletions code/SQLite3Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class SQLite3Query extends Query
*/
protected $handle;

private 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.
Expand All @@ -35,6 +37,8 @@ public function __construct(SQLite3Connector $database, SQLite3Result $handle)
{
$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()
Expand All @@ -44,18 +48,22 @@ public function __destruct()
}
}

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(): int
{
// Some queries are not iterable using fetchArray like CREATE statement
if (!$this->handle->numColumns()) {
return 0;
}

$this->handle->reset();
$c=0;
$c = 0;
while ($this->handle->fetchArray()) {
$c++;
}
Expand Down