Merge pull request #7882 from nextcloud/search-and
Support arbitrary number of arguments for d:or and d:and in search queries
This commit is contained in:
commit
2b70c708ab
|
@ -83,6 +83,16 @@ class QuerySearchHelper {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param IQueryBuilder $builder
|
||||||
|
* @param ISearchOperator $operator
|
||||||
|
*/
|
||||||
|
public function searchOperatorArrayToDBExprArray(IQueryBuilder $builder, array $operators) {
|
||||||
|
return array_map(function ($operator) use ($builder) {
|
||||||
|
return $this->searchOperatorToDBExpr($builder, $operator);
|
||||||
|
}, $operators);
|
||||||
|
}
|
||||||
|
|
||||||
public function searchOperatorToDBExpr(IQueryBuilder $builder, ISearchOperator $operator) {
|
public function searchOperatorToDBExpr(IQueryBuilder $builder, ISearchOperator $operator) {
|
||||||
$expr = $builder->expr();
|
$expr = $builder->expr();
|
||||||
if ($operator instanceof ISearchBinaryOperator) {
|
if ($operator instanceof ISearchBinaryOperator) {
|
||||||
|
@ -95,9 +105,9 @@ class QuerySearchHelper {
|
||||||
throw new \InvalidArgumentException('Binary operators inside "not" is not supported');
|
throw new \InvalidArgumentException('Binary operators inside "not" is not supported');
|
||||||
}
|
}
|
||||||
case ISearchBinaryOperator::OPERATOR_AND:
|
case ISearchBinaryOperator::OPERATOR_AND:
|
||||||
return $expr->andX($this->searchOperatorToDBExpr($builder, $operator->getArguments()[0]), $this->searchOperatorToDBExpr($builder, $operator->getArguments()[1]));
|
return call_user_func_array([$expr, 'andX'], $this->searchOperatorArrayToDBExprArray($builder, $operator->getArguments()));
|
||||||
case ISearchBinaryOperator::OPERATOR_OR:
|
case ISearchBinaryOperator::OPERATOR_OR:
|
||||||
return $expr->orX($this->searchOperatorToDBExpr($builder, $operator->getArguments()[0]), $this->searchOperatorToDBExpr($builder, $operator->getArguments()[1]));
|
return call_user_func_array([$expr, 'orX'], $this->searchOperatorArrayToDBExprArray($builder, $operator->getArguments()));
|
||||||
default:
|
default:
|
||||||
throw new \InvalidArgumentException('Invalid operator type: ' . $operator->getType());
|
throw new \InvalidArgumentException('Invalid operator type: ' . $operator->getType());
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,8 +151,13 @@ class QuerySearchHelperTest extends TestCase {
|
||||||
[new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', 'image/%'), [0, 1]],
|
[new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', 'image/%'), [0, 1]],
|
||||||
[new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_AND, [
|
[new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_AND, [
|
||||||
new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'size', 50),
|
new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'size', 50),
|
||||||
new SearchComparison(ISearchComparison::COMPARE_LESS_THAN, 'mtime', 125), [0]
|
new SearchComparison(ISearchComparison::COMPARE_LESS_THAN, 'mtime', 125)
|
||||||
]), [0]],
|
]), [0]],
|
||||||
|
[new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_AND, [
|
||||||
|
new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'size', 50),
|
||||||
|
new SearchComparison(ISearchComparison::COMPARE_LESS_THAN, 'mtime', 125),
|
||||||
|
new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', 'text/%')
|
||||||
|
]), []],
|
||||||
[new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, [
|
[new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, [
|
||||||
new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mtime', 100),
|
new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mtime', 100),
|
||||||
new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mtime', 150),
|
new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mtime', 150),
|
||||||
|
|
Loading…
Reference in New Issue