From f2ad2838f431e7f3da1cdeb3998361c67eed2fbe Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 6 May 2015 09:39:43 +0200 Subject: [PATCH] ArrayDatasource: Apply a query's limit and offset when creating the result --- .../Icinga/Data/DataArray/ArrayDatasource.php | 33 +++++++------------ 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/library/Icinga/Data/DataArray/ArrayDatasource.php b/library/Icinga/Data/DataArray/ArrayDatasource.php index b2ecce533..243fb1d7d 100644 --- a/library/Icinga/Data/DataArray/ArrayDatasource.php +++ b/library/Icinga/Data/DataArray/ArrayDatasource.php @@ -171,8 +171,11 @@ class ArrayDatasource implements Selectable $columns = $query->getColumns(); $filter = $query->getFilter(); + $offset = $query->hasOffset() ? $query->getOffset() : 0; + $limit = $query->hasLimit() ? $query->getLimit() : 0; $foundStringKey = false; $result = array(); + $skipped = 0; foreach ($this->data as $key => $row) { if (is_string($key) && $this->keyColumn !== null && !isset($row->{$this->keyColumn})) { $row = clone $row; // Make sure that this won't affect the actual data @@ -181,6 +184,9 @@ class ArrayDatasource implements Selectable if (! $filter->matches($row)) { continue; + } elseif ($skipped < $offset) { + $skipped++; + continue; } // Get only desired columns if asked so @@ -203,6 +209,10 @@ class ArrayDatasource implements Selectable $foundStringKey |= is_string($key); $result[$key] = $filteredRow; + + if (count($result) === $limit) { + break; + } } // Sort the result @@ -220,27 +230,6 @@ class ArrayDatasource implements Selectable return $this; } - /** - * Apply the limit, if any, of the given query to the current result and return the result - * - * @param SimpleQuery $query - * - * @return array - */ - protected function getLimitedResult(SimpleQuery $query) - { - if ($query->hasLimit()) { - if ($query->hasOffset()) { - $offset = $query->getOffset(); - } else { - $offset = 0; - } - return array_slice($this->result, $offset, $query->getLimit()); - } else { - return $this->result; - } - } - /** * Return whether a query result exists * @@ -276,6 +265,6 @@ class ArrayDatasource implements Selectable if (! $this->hasResult()) { $this->createResult($query); } - return $this->getLimitedResult($query); + return $this->result; } }