check(); if (empty($table)) { throw new InvalidArgumentException('Table name cannot be empty.'); } if ($batchSize < 1) { throw new InvalidArgumentException('Batch size must be' . " greater than 0, $batchSize given."); } $this->connection = $connection; $this->table = $connection->quote($table); $this->batchSize = $batchSize; } /** * Return the column titles of the data table. * * @return array */ public function header() : array { return array_keys(iterator_first($this)); } /** * Return an iterator for the records in the data table. * * @return \Generator */ public function getIterator() : Traversable { $query = "SELECT * FROM {$this->table} LIMIT :offset, {$this->batchSize}"; $statement = $this->connection->prepare($query); $offset = 0; $statement->bindParam(':offset', $offset); do { $success = $statement->execute(); if (!$success) { throw new RuntimeException('There was a problem executing the SQL statement.'); } $rows = $statement->fetchAll(PDO::FETCH_ASSOC); if (empty($rows)) { break; } yield from $rows; $more = count($rows) >= $this->batchSize; $offset += $this->batchSize; } while ($more); } }