From 4522cf5090ef7a514466637f8cae7dbb4c2f67b3 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Tue, 21 Jul 2015 15:27:01 +0200 Subject: [PATCH 1/4] Add missing return to IdoQuery::getCustomvarColumnName() Some filter combinations still seem to not work properly. refs #9692 --- .../Monitoring/Backend/Ido/Query/IdoQuery.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php index c5e9e5333..3f3918a13 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php @@ -496,6 +496,7 @@ abstract class IdoQuery extends DbQuery in_array($filter->getColumn(), $this->columnsWithoutCollation) && strpos($filter->getColumn(), 'LOWER') !== 0 ) { + $filter->setColumn('LOWER(' . $filter->getColumn() . ')'); $filter->setColumn('LOWER(' . $filter->getColumn() . ')'); $expression = $filter->getExpression(); if (is_array($expression)) { @@ -855,7 +856,6 @@ abstract class IdoQuery extends DbQuery protected function customvarNameToTypeName($customvar) { $customvar = strtolower($customvar); - // TODO: Improve this: if (! preg_match('~^_(host|service)_([a-zA-Z0-9_]+)$~', $customvar, $m)) { throw new ProgrammingError( 'Got invalid custom var: "%s"', @@ -870,11 +870,19 @@ abstract class IdoQuery extends DbQuery return array_key_exists($name, $this->joinedVirtualTables); } + /** + * Get the query column of a custom variable or null in case the custom variable has not been joined + * + * @param string $customvar + * + * @return null|string + */ protected function getCustomvarColumnName($customvar) { if (isset($this->customVars[($customvar = strtolower($customvar))])) { - $this->customVars[strtolower($customvar)] . '.varvalue'; + return $this->customVars[$customvar] . '.varvalue'; } + return null; } public function aliasToColumnName($alias) From 3ed13366a80abab42934bffee961a8fddb059670 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 22 Jul 2015 09:05:18 +0200 Subject: [PATCH 2/4] Remove duplicate line in IdoQuery I introduced this w/ 4522cf5090ef7a514466637f8cae7dbb4c2f67b3. --- .../monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php index 3f3918a13..52485d9cb 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php @@ -496,7 +496,6 @@ abstract class IdoQuery extends DbQuery in_array($filter->getColumn(), $this->columnsWithoutCollation) && strpos($filter->getColumn(), 'LOWER') !== 0 ) { - $filter->setColumn('LOWER(' . $filter->getColumn() . ')'); $filter->setColumn('LOWER(' . $filter->getColumn() . ')'); $expression = $filter->getExpression(); if (is_array($expression)) { From 15cb24c6dc38090f2b3d84503bc795d10ce1821b Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 22 Jul 2015 12:00:59 +0200 Subject: [PATCH 3/4] Throw an exception in IdoQuery::getCustomvarColumnName() in case the custom variable has not been joined Queries extending IdoQuery and using the method getCustomvarColumnName() must be notified in case the custom variable has not been joined. refs #9692 --- library/Icinga/Exception/QueryException.php | 2 +- .../Monitoring/Backend/Ido/Query/IdoQuery.php | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/library/Icinga/Exception/QueryException.php b/library/Icinga/Exception/QueryException.php index bfe98855a..abfd7402d 100644 --- a/library/Icinga/Exception/QueryException.php +++ b/library/Icinga/Exception/QueryException.php @@ -4,7 +4,7 @@ namespace Icinga\Exception; /** - * Exception thrown if a query contains invalid parameters + * Exception thrown if a query encountered an error */ class QueryException extends IcingaException { diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php index 52485d9cb..b79229f8f 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php @@ -12,6 +12,7 @@ use Icinga\Data\Filter\FilterExpression; use Icinga\Exception\IcingaException; use Icinga\Exception\NotImplementedError; use Icinga\Exception\ProgrammingError; +use Icinga\Exception\QueryException; use Icinga\Web\Session; /** @@ -870,18 +871,19 @@ abstract class IdoQuery extends DbQuery } /** - * Get the query column of a custom variable or null in case the custom variable has not been joined + * Get the query column of a already joined custom variable * * @param string $customvar * - * @return null|string + * @return string + * @throws QueryException If the custom variable has not been joined */ protected function getCustomvarColumnName($customvar) { - if (isset($this->customVars[($customvar = strtolower($customvar))])) { - return $this->customVars[$customvar] . '.varvalue'; + if (! isset($this->customVars[($customvar = strtolower($customvar))])) { + throw new QueryException('Custom variable %s has not been joined', $customvar); } - return null; + return $this->customVars[$customvar] . '.varvalue'; } public function aliasToColumnName($alias) From a75b1a35c6f22dbecd55f6ee38fb09bd1aa9baa1 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 22 Jul 2015 13:47:52 +0200 Subject: [PATCH 4/4] monitoring: Don't fail when selecting custom variables while having a group by clause refs #9692 --- .../Monitoring/Backend/Ido/Query/IdoQuery.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php index b79229f8f..de89d23bb 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php @@ -891,9 +891,19 @@ abstract class IdoQuery extends DbQuery return $this->idxAliasColumn[$alias]; } + /** + * Get the alias of a column expression as defined in the {@link $columnMap} property. + * + * @param string $alias Potential custom alias + * + * @return string + */ public function customAliasToAlias($alias) { - return $this->idxCustomAliases[$alias]; + if (isset($this->idxCustomAliases[$alias])) { + return $this->idxCustomAliases[$alias]; + } + return $alias; } /**