<?php
namespace ModulesAbstract\PluginReports\Report\Model\Gatherer; class DatabasePaginatedIterator extends GathererAbstract { protected $query = null; protected $queryParams = null; protected $page = 0; protected $queryResultsIterator = null; protected $preReturnCallback = null; protected $preFilterCallback = null; public function __construct($query, $queryParams=[]) { $this->query = $query; $this->queryParams = $queryParams; $this->options['queueLength'] = 2500; } public function getRequiredFilters() { return []; } public function getAcceptedFilters() { return []; } public function setPreReturnCallback($preReturnCallback) { $this->preReturnCallback = $preReturnCallback; return $this; } public function setPreFilterCallback($preFilterCallback) { $this->preFilterCallback = $preFilterCallback; return $this; } protected function objectInitialize() { } protected function objectDestroy() { $this->queryResultsIterator = null; } protected function buildPaginatedQuery($query, $page, $queueLength) { $query.= " LIMIT __RESULTS_START__,__RESULTS_PER_PAGE__"; $replacements = [ '__RESULTS_START__' => $page*$queueLength, '__RESULTS_PER_PAGE__' => $queueLength, ]; return str_replace(array_keys($replacements), array_values($replacements), $query); } protected function objectNext() { $ret = null; if (!$this->queryResultsIterator) { $db = \DB::getInstance(); $this->queryResultsIterator = $db->Execute($this->buildPaginatedQuery($this->query, $this->page, $this->options['queueLength']), $this->queryParams); if ($db->ErrorNo()) { throw new \DBException("Cannot run paginated query!"); } $this->page++; $this->stats['resultsOnThisPage'] = 0; $this->stats['page:next']++; } if (!$this->queryResultsIterator->EOF) { $this->stats['resultsOnThisPage']++; $ret = $this->queryResultsIterator->FetchRow(); if ($this->preFilterCallback) { $fcn = $this->preFilterCallback; if ($fcn($ret)=='intern:skip') { return 'intern:skip'; } } if ($this->preReturnCallback) { $fcn = $this->preReturnCallback; return $fcn($ret); } return $ret; } else { if ($this->stats['resultsOnThisPage'] < $this->options['queueLength']) { return null; } else { $this->queryResultsIterator = null; return 'intern:skip'; } } return 'intern:skip'; } } 