From 0fc06d77958215835c0f471bf5aaa3df36bbaa6e Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 12 Mar 2021 09:50:59 +0100 Subject: [PATCH] Transform `*` equal/unequal comparisons to NULL checks This comes from https://github.com/Icinga/ipl-sql/pull/31 --- library/Icinga/Data/Db/DbConnection.php | 10 ++-------- library/Icinga/Data/Db/DbQuery.php | 4 ++-- .../library/Monitoring/Backend/Ido/Query/IdoQuery.php | 8 -------- 3 files changed, 4 insertions(+), 18 deletions(-) diff --git a/library/Icinga/Data/Db/DbConnection.php b/library/Icinga/Data/Db/DbConnection.php index db9962a54..15aa50e74 100644 --- a/library/Icinga/Data/Db/DbConnection.php +++ b/library/Icinga/Data/Db/DbConnection.php @@ -554,19 +554,13 @@ class DbConnection implements Selectable, Extensible, Updatable, Reducible, Insp ); } elseif ($sign === '=' && strpos($value, '*') !== false) { if ($value === '*') { - // We'll ignore such filters as it prevents index usage and because "*" means anything, so whether we're - // using a real column with a valid comparison here or just an expression which can only be evaluated to - // true makes no difference, except for performance reasons... - return new Zend_Db_Expr('TRUE'); + return $column . ' IS NOT NULL'; } return $column . ' LIKE ' . $this->dbAdapter->quote(preg_replace('~\*~', '%', $value)); } elseif ($sign === '!=' && strpos($value, '*') !== false) { if ($value === '*') { - // We'll ignore such filters as it prevents index usage and because "*" means nothing, so whether we're - // using a real column with a valid comparison here or just an expression which cannot be evaluated to - // true makes no difference, except for performance reasons... - return new Zend_Db_Expr('FALSE'); + return $column . ' IS NULL'; } return sprintf( diff --git a/library/Icinga/Data/Db/DbQuery.php b/library/Icinga/Data/Db/DbQuery.php index 04f96ddb8..ead86fe21 100644 --- a/library/Icinga/Data/Db/DbQuery.php +++ b/library/Icinga/Data/Db/DbQuery.php @@ -327,13 +327,13 @@ class DbQuery extends SimpleQuery return '(' . implode(" $operator ", $sql) . ')'; } elseif ($sign === '=' && strpos($expression, '*') !== false) { if ($expression === '*') { - return new Zend_Db_Expr('TRUE'); + return $col . ' IS NOT NULL'; } return $col . ' LIKE ' . $this->escapeForSql($this->escapeWildcards($expression)); } elseif ($sign === '!=' && strpos($expression, '*') !== false) { if ($expression === '*') { - return new Zend_Db_Expr('FALSE'); + return $col . ' IS NULL'; } return sprintf( diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php index 0d80bf5ee..da41f149a 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php @@ -665,10 +665,6 @@ abstract class IdoQuery extends DbQuery protected function requireFilterColumns(Filter $filter) { if ($filter instanceof FilterExpression) { - if ($filter->getExpression() === '*') { - return; // Wildcard only filters are ignored so stop early here to avoid joining a table for nothing - } - $alias = $filter->getColumn(); $virtualTable = $this->aliasToTableName($alias); @@ -774,10 +770,6 @@ abstract class IdoQuery extends DbQuery public function where($condition, $value = null) { - if ($value === '*') { - return $this; // Wildcard only filters are ignored so stop early here to avoid joining a table for nothing - } - $this->requireColumn($condition); $col = $this->getMappedField($condition); if ($col === null) {