Merge pull request 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
library/Icinga/Data/Db

View File

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