RepositoryQuery: Implement interface Iterator

refs #8826
This commit is contained in:
Johannes Meyer 2015-05-18 14:05:02 +02:00
parent 3f25cf560e
commit be36809552
1 changed files with 90 additions and 1 deletions

View File

@ -3,6 +3,8 @@
namespace Icinga\Repository;
use Iterator;
use IteratorAggregate;
use Icinga\Application\Logger;
use Icinga\Data\QueryInterface;
use Icinga\Data\Filter\Filter;
@ -11,7 +13,7 @@ use Icinga\Exception\QueryException;
/**
* Query class supposed to mediate between a repository and its datasource's query
*/
class RepositoryQuery implements QueryInterface
class RepositoryQuery implements QueryInterface, Iterator
{
/**
* The repository being used
@ -34,6 +36,13 @@ class RepositoryQuery implements QueryInterface
*/
protected $target;
/**
* The real query's iterator
*
* @var Iterator
*/
protected $iterator;
/**
* Create a new repository query
*
@ -363,6 +372,16 @@ class RepositoryQuery implements QueryInterface
return $this->query->getOffset();
}
/**
* Fetch and return all rows of the result set using an iterator
*
* @return Iterator
*/
public function fetch()
{
return $this;
}
/**
* Fetch and return the first column of this query's first row
*
@ -503,4 +522,74 @@ class RepositoryQuery implements QueryInterface
{
return $this->query->count();
}
/**
* Start or rewind the iteration
*/
public function rewind()
{
if ($this->iterator === null) {
if (! $this->hasOrder()) {
$this->order();
}
$iterator = $this->query->fetch();
if ($iterator instanceof IteratorAggregate) {
$this->iterator = $iterator->getIterator();
} else {
$this->iterator = $iterator;
}
}
$this->iterator->rewind();
}
/**
* Fetch and return the current row of this query's result
*
* @return object
*/
public function current()
{
$row = $this->iterator->current();
if ($this->repository->providesValueConversion()) {
foreach ($this->getColumns() as $alias => $column) {
if (! is_string($alias)) {
$alias = $column;
}
$row->$alias = $this->repository->retrieveColumn($alias, $row->$alias);
}
}
return $row;
}
/**
* Return whether the current row of this query's result is valid
*
* @return bool
*/
public function valid()
{
return $this->iterator->valid();
}
/**
* Return the key for the current row of this query's result
*
* @return mixed
*/
public function key()
{
return $this->iterator->key();
}
/**
* Advance to the next row of this query's result
*/
public function next()
{
$this->iterator->next();
}
}