From b20291a606547f66db484aef40170fc83f944e64 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 26 Jun 2019 09:57:19 +0200 Subject: [PATCH] Fix double query execution Our monitoring list views call SimpleQuery::hasResult() first in order to determine whether there are results to display. This calls fetchRow() which executes the underlying query the first time. If there are resulsts, the query is iterated which executes the query again. With this patch, SimpleQuery::hasResult() makes use of the inner iterator instead of calling fetchRow(). The query is now executed only once. --- library/Icinga/Data/SimpleQuery.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/library/Icinga/Data/SimpleQuery.php b/library/Icinga/Data/SimpleQuery.php index 2d0eeef6f..ae1e589b1 100644 --- a/library/Icinga/Data/SimpleQuery.php +++ b/library/Icinga/Data/SimpleQuery.php @@ -450,7 +450,17 @@ class SimpleQuery implements QueryInterface, Queryable, Iterator */ public function hasResult() { - return $this->iteratorPosition !== null || $this->fetchRow() !== false; + if ($this->iteratorPosition !== null) { + return true; + } + + $hasResult = false; + foreach ($this as $row) { + $hasResult = true; + break; + } + + return $hasResult; } /**