*/ class common_persistence_sql_QueryIterator implements Iterator { private const CACHE_SIZE = 100; /** * @var common_persistence_SqlPersistence */ private $persistence; /** * @var string */ private $query; /** * @var array */ private $params; /** * Id of the current instance * * @var int */ private $currentResult; /** * Return statements of the last query * * @var array */ private $cache; /** * @var array */ private $types; /** * @var int */ private $limit; public function __construct( common_persistence_SqlPersistence $persistence, string $query, array $params = [], array $types = [], int $limit = self::CACHE_SIZE ) { $this->persistence = $persistence; $this->query = $query; $this->params = $params; $this->types = $types; $this->limit = $limit; $this->rewind(); } public function rewind() { $this->load(0); } /** * @return core_kernel_classes_Triple */ public function current() { return $this->cache[$this->currentResult]; } public function key() { return $this->currentResult; } public function next() { if ($this->valid()) { $last = $this->key(); $this->currentResult++; if (!isset($this->cache[$this->currentResult])) { $this->load($last + 1); } } } public function valid() { return !empty($this->cache); } /** * Loads the next n results, starting with $offset * * @param int $offset */ protected function load($offset) { $query = $this->persistence->getPlatForm()->limitStatement($this->query, $this->limit, $offset); $result = $this->persistence->query($query, $this->params, $this->types); $this->cache = []; $pos = $offset; while ($statement = $result->fetch()) { $this->cache[$pos++] = $statement; } $this->currentResult = $offset; } }