From 3770741ba557ea1f96df30f51eeb69f4f886798b Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 15 May 2015 14:23:40 +0200 Subject: [PATCH] DataView: Implement QueryInterface An instance of DataView should implement the full interface, not only parts of it. --- .../library/Monitoring/DataView/DataView.php | 119 ++++++++++++++++-- 1 file changed, 112 insertions(+), 7 deletions(-) diff --git a/modules/monitoring/library/Monitoring/DataView/DataView.php b/modules/monitoring/library/Monitoring/DataView/DataView.php index be5862abe..0a47c0d3b 100644 --- a/modules/monitoring/library/Monitoring/DataView/DataView.php +++ b/modules/monitoring/library/Monitoring/DataView/DataView.php @@ -3,14 +3,11 @@ namespace Icinga\Module\Monitoring\DataView; -use Countable; +use Icinga\Data\QueryInterface; use Icinga\Data\Filter\Filter; use Icinga\Data\Filter\FilterMatch; -use Icinga\Data\Browsable; use Icinga\Data\PivotTable; -use Icinga\Data\Sortable; use Icinga\Data\ConnectionInterface; -use Icinga\Data\Filterable; use Icinga\Exception\QueryException; use Icinga\Web\Request; use Icinga\Web\Url; @@ -19,12 +16,12 @@ use Icinga\Module\Monitoring\Backend\MonitoringBackend; /** * A read-only view of an underlying query */ -abstract class DataView implements Browsable, Countable, Filterable, Sortable +abstract class DataView implements QueryInterface, IteratorAggregate { /** * The query used to populate the view * - * @var \Icinga\Data\SimpleQuery + * @var QueryInterface */ protected $query; @@ -403,6 +400,114 @@ abstract class DataView implements Browsable, Countable, Filterable, Sortable */ public function count() { - return count($this->query); + return $this->query->count(); + } + + /** + * Set a limit count and offset + * + * @param int $count Number of rows to return + * @param int $offset Start returning after this many rows + * + * @return self + */ + public function limit($count = null, $offset = null) + { + $this->query->limit($count, $offset); + return $this; + } + + /** + * Whether a limit is set + * + * @return bool + */ + public function hasLimit() + { + return $this->query->hasLimit(); + } + + /** + * Get the limit if any + * + * @return int|null + */ + public function getLimit() + { + return $this->query->getLimit(); + } + + /** + * Whether an offset is set + * + * @return bool + */ + public function hasOffset() + { + return $this->query->hasOffset(); + } + + /** + * Get the offset if any + * + * @return int|null + */ + public function getOffset() + { + return $this->query->hasOffset(); + } + + /** + * Retrieve an array containing all rows of the result set + * + * @return array + */ + public function fetchAll() + { + return $this->getQuery()->fetchAll(); + } + + /** + * Fetch the first row of the result set + * + * @return mixed + */ + public function fetchRow() + { + return $this->getQuery()->fetchRow(); + } + + /** + * Fetch a column of all rows of the result set as an array + * + * @param int $columnIndex Index of the column to fetch + * + * @return array + */ + public function fetchColumn($columnIndex = 0) + { + return $this->getQuery()->fetchColumn($columnIndex); + } + + /** + * Fetch the first column of the first row of the result set + * + * @return string + */ + public function fetchOne() + { + return $this->getQuery()->fetchOne(); + } + + /** + * Fetch all rows of the result set as an array of key-value pairs + * + * The first column is the key, the second column is the value. + * + * @return array + */ + public function fetchPairs() + { + return $this->getQuery()->fetchPairs(); } }