Merge pull request #4785 from Icinga/get-dbconnection-renderFilterExpression-up2date

Get `DbConnection::renderFilterExpression()` up to date
This commit is contained in:
Johannes Meyer 2022-05-13 17:01:20 +02:00 committed by GitHub
commit 9c04776431
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 9 deletions

View File

@ -545,22 +545,47 @@ class DbConnection implements Selectable, Extensible, Updatable, Reducible, Insp
$value = $filter->getExpression();
if (is_array($value)) {
if ($sign === '=') {
return $column . ' IN (' . $this->dbAdapter->quote($value) . ')';
} elseif ($sign === '!=') {
return sprintf('(%1$s NOT IN (%2$s) OR %1$s IS NULL)', $column, $this->dbAdapter->quote($value));
$comp = [];
$pattern = [];
foreach ($value as $val) {
if (strpos($val, '*') === false) {
$comp[] = $val;
} else {
$pattern[] = $this->renderFilterExpression(Filter::expression($column, $sign, $val));
}
}
throw new ProgrammingError(
'Unable to render array expressions with operators other than equal or not equal'
);
} elseif ($filter instanceof FilterMatch && strpos($value, '*') !== false) {
$sql = $pattern;
if ($sign === '=') {
if (! empty($comp)) {
$sql[] = $column . ' IN (' . $this->dbAdapter->quote($comp) . ')';
}
$operator = 'OR';
} elseif ($sign === '!=') {
if (! empty($comp)) {
$sql[] = sprintf(
'(%1$s NOT IN (%2$s) OR %1$s IS NULL)',
$column,
$this->dbAdapter->quote($comp)
);
}
$operator = 'AND';
} else {
throw new ProgrammingError(
'Unable to render array expressions with operators other than equal or not equal'
);
}
return count($sql) === 1 ? $sql[0] : '(' . implode(" $operator ", $sql) . ')';
} elseif ($filter instanceof FilterMatch && $value !== null && strpos($value, '*') !== false) {
if ($value === '*') {
return $column . ' IS NOT NULL';
}
return $column . ' LIKE ' . $this->dbAdapter->quote(preg_replace('~\*~', '%', $value));
} elseif ($filter instanceof FilterMatchNot && strpos($value, '*') !== false) {
} elseif ($filter instanceof FilterMatchNot && $value !== null && strpos($value, '*') !== false) {
if ($value === '*') {
return $column . ' IS NULL';
}