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() { if ($this->handle) { $this->handle->finalize(); } } public function numRecords() { return $this->count; } /** * @todo This looks terrible but there is no SQLite3::get_num_rows() implementation */ private function countRecords(): int { // Some queries are not iterable using fetchArray like CREATE statement if (!$this->handle->numColumns()) { return 0; } $c = 0; while ($this->handle->fetchArray()) { $c++; } $this->handle->reset(); return $c; } public function getIterator(): Traversable { while ($data = $this->handle->fetchArray(SQLITE3_ASSOC)) { yield $data; } } }