Merge pull request #4332 from Icinga/feature/support-for-null-comparisons

Transform `*` equal/unequal comparisons to NULL checks
This commit is contained in:
Johannes Meyer 2021-03-12 11:23:00 +01:00 committed by GitHub
commit 1890e0e745
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 4 additions and 18 deletions

View File

@ -554,19 +554,13 @@ class DbConnection implements Selectable, Extensible, Updatable, Reducible, Insp
); );
} elseif ($sign === '=' && strpos($value, '*') !== false) { } elseif ($sign === '=' && strpos($value, '*') !== false) {
if ($value === '*') { if ($value === '*') {
// We'll ignore such filters as it prevents index usage and because "*" means anything, so whether we're return $column . ' IS NOT NULL';
// 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 . ' LIKE ' . $this->dbAdapter->quote(preg_replace('~\*~', '%', $value)); return $column . ' LIKE ' . $this->dbAdapter->quote(preg_replace('~\*~', '%', $value));
} elseif ($sign === '!=' && strpos($value, '*') !== false) { } elseif ($sign === '!=' && strpos($value, '*') !== false) {
if ($value === '*') { if ($value === '*') {
// We'll ignore such filters as it prevents index usage and because "*" means nothing, so whether we're return $column . ' IS NULL';
// 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 sprintf( return sprintf(

View File

@ -327,13 +327,13 @@ class DbQuery extends SimpleQuery
return '(' . implode(" $operator ", $sql) . ')'; return '(' . implode(" $operator ", $sql) . ')';
} elseif ($sign === '=' && strpos($expression, '*') !== false) { } elseif ($sign === '=' && strpos($expression, '*') !== false) {
if ($expression === '*') { if ($expression === '*') {
return new Zend_Db_Expr('TRUE'); return $col . ' IS NOT NULL';
} }
return $col . ' LIKE ' . $this->escapeForSql($this->escapeWildcards($expression)); return $col . ' LIKE ' . $this->escapeForSql($this->escapeWildcards($expression));
} elseif ($sign === '!=' && strpos($expression, '*') !== false) { } elseif ($sign === '!=' && strpos($expression, '*') !== false) {
if ($expression === '*') { if ($expression === '*') {
return new Zend_Db_Expr('FALSE'); return $col . ' IS NULL';
} }
return sprintf( return sprintf(

View File

@ -665,10 +665,6 @@ abstract class IdoQuery extends DbQuery
protected function requireFilterColumns(Filter $filter) protected function requireFilterColumns(Filter $filter)
{ {
if ($filter instanceof FilterExpression) { 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(); $alias = $filter->getColumn();
$virtualTable = $this->aliasToTableName($alias); $virtualTable = $this->aliasToTableName($alias);
@ -774,10 +770,6 @@ abstract class IdoQuery extends DbQuery
public function where($condition, $value = null) 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); $this->requireColumn($condition);
$col = $this->getMappedField($condition); $col = $this->getMappedField($condition);
if ($col === null) { if ($col === null) {