DbConnection: Support wildcards in arrays in renderFilterExpression

This commit is contained in:
Johannes Meyer 2022-05-13 11:49:43 +02:00
parent 0a41c52002
commit 03e7041ccb

View File

@ -545,15 +545,40 @@ 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));
}
} }
$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( throw new ProgrammingError(
'Unable to render array expressions with operators other than equal or not equal' '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 && strpos($value, '*') !== false) { } elseif ($filter instanceof FilterMatch && strpos($value, '*') !== false) {
if ($value === '*') { if ($value === '*') {
return $column . ' IS NOT NULL'; return $column . ' IS NOT NULL';