From 31d3153d2be1b12cd821645c18f7adea1a458898 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Mon, 12 Aug 2019 13:41:38 +0200 Subject: [PATCH] Support wildcard filters in chains (#3903) --- library/Icinga/Data/Db/DbQuery.php | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/library/Icinga/Data/Db/DbQuery.php b/library/Icinga/Data/Db/DbQuery.php index 78605b7ad..25de96742 100644 --- a/library/Icinga/Data/Db/DbQuery.php +++ b/library/Icinga/Data/Db/DbQuery.php @@ -298,13 +298,31 @@ class DbQuery extends SimpleQuery } if (is_array($expression)) { + $comp = []; + $pattern = []; + foreach ($expression as $value) { + if (strpos($value, '*') === false) { + $comp[] = $value; + } else { + $pattern[] = $this->whereToSql($col, $sign, $value); + } + } + $sql = $pattern; if ($sign === '=') { - return $col . ' IN (' . $this->escapeForSql($expression) . ')'; + if (! empty($comp)) { + $sql[] = $col . ' IN (' . $this->escapeForSql($comp) . ')'; + } } elseif ($sign === '!=') { - return sprintf('(%1$s NOT IN (%2$s) OR %1$s IS NULL)', $col, $this->escapeForSql($expression)); + if (! empty($comp)) { + $sql[] = sprintf('(%1$s NOT IN (%2$s) OR %1$s IS NULL)', $col, $this->escapeForSql($comp)); + } + } else { + throw new QueryException( + 'Unable to render array expressions with operators other than equal or not equal' + ); } - throw new QueryException('Unable to render array expressions with operators other than equal or not equal'); + return '(' . implode(' OR ', $sql) . ')'; } elseif ($sign === '=' && strpos($expression, '*') !== false) { if ($expression === '*') { return new Zend_Db_Expr('TRUE');