Skip to content

Commit 0d3d935

Browse files
committed
Fix issue with empty array for IN-clause
When an IN-clause is created with an empty array PostgreSQL will blow up with an SQL-Error as the resulting SQL would be ` x IN()` which is not allowed. This commit works around that by returning a query-part that always returns false as querying for something that is not part of something else will always return false.
1 parent 7be5362 commit 0d3d935

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/PostgresDocumentStore.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,14 @@ private function filterToWhereClause(Filter $filter, $argsCount = 0): array
750750
[$innerFilterStr, $args, $argsCount] = $this->filterToWhereClause($innerFilter, $argsCount);
751751

752752
if($innerFilter instanceof DocumentStore\Filter\AnyOfFilter || $innerFilter instanceof DocumentStore\Filter\AnyOfDocIdFilter) {
753+
if ($argsCount === 0) {
754+
return [
755+
substr_replace(' 1 != 1 ', ' 1 == 1 ', $innerFilterStr),
756+
$args,
757+
$argsCount
758+
];
759+
}
760+
753761
$inPos = strpos($innerFilterStr, ' IN(');
754762
$filterStr = substr_replace($innerFilterStr, ' NOT IN(', $inPos, 4 /* " IN(" */);
755763
return [$filterStr, $args, $argsCount];
@@ -795,6 +803,9 @@ private function isPropFilter(Filter $filter): bool
795803

796804
private function makeInClause(string $prop, array $valList, int $argsCount, bool $jsonEncode = false): array
797805
{
806+
if ($valList === []) {
807+
return [' 1 != 1 ', [], 0];
808+
}
798809
$argList = [];
799810
$params = \implode(",", \array_map(function ($val) use (&$argsCount, &$argList, $jsonEncode) {
800811
$param = ":a$argsCount";

0 commit comments

Comments
 (0)