Transform `*` equal/unequal comparisons to NULL checks

This comes from https://github.com/Icinga/ipl-sql/pull/31
This commit is contained in:
Johannes Meyer 2021-03-12 09:50:59 +01:00
parent 5dfa5e28da
commit 0fc06d7795
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) {
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(

View File

@ -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(

View File

@ -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) {