Skip to content

Commit e9721e2

Browse files
authored
Merge pull request #14 from heiglandreas/handleEmptyArrayWithInClause
Fix issue with empty array for IN-clause
2 parents 7be5362 + 8344693 commit e9721e2

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

src/PostgresDocumentStore.php

+11
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";

tests/PostgresDocumentStoreTest.php

+117
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,32 @@ public function it_handles_any_of_filter()
203203
$this->assertCount(2, $filteredDocs);
204204
}
205205

206+
/**
207+
* @test
208+
*/
209+
public function it_handles_any_of_filter_with_empty_args()
210+
{
211+
$collectionName = 'test_any_of_filter_with_empty_args';
212+
$this->documentStore->addCollection($collectionName);
213+
214+
$doc1 = ["foo" => "bar"];
215+
$doc2 = ["foo" => "baz"];
216+
$doc3 = ["foo" => "bat"];
217+
218+
$docs = [$doc1, $doc2, $doc3];
219+
220+
array_walk($docs, function (array $doc) use ($collectionName) {
221+
$this->documentStore->addDoc($collectionName, Uuid::uuid4()->toString(), $doc);
222+
});
223+
224+
$filteredDocs = $this->documentStore->filterDocs(
225+
$collectionName,
226+
new AnyOfFilter("foo", [])
227+
);
228+
229+
$this->assertCount(0, $filteredDocs);
230+
}
231+
206232
/**
207233
* @test
208234
*/
@@ -262,6 +288,37 @@ public function it_handles_not_any_of_filter()
262288
$this->assertSame('baz', $filteredDocs[0]['foo']);
263289
}
264290

291+
/**
292+
* @test
293+
*/
294+
public function it_handles_not_any_of_filter_with_empty_args()
295+
{
296+
$collectionName = 'test_not_any_of_filter_with_empty_args';
297+
$this->documentStore->addCollection($collectionName);
298+
299+
$doc1 = ["foo" => "bar"];
300+
$doc2 = ["foo" => "baz"];
301+
$doc3 = ["foo" => "bat"];
302+
303+
$docs = [$doc1, $doc2, $doc3];
304+
305+
array_walk($docs, function (array $doc) use ($collectionName) {
306+
$this->documentStore->addDoc($collectionName, Uuid::uuid4()->toString(), $doc);
307+
});
308+
309+
$filteredDocs = $this->documentStore->filterDocs(
310+
$collectionName,
311+
new NotFilter(new AnyOfFilter("foo", []))
312+
);
313+
314+
$filteredDocs = iterator_to_array($filteredDocs);
315+
316+
$this->assertCount(3, $filteredDocs);
317+
318+
$this->assertSame('baz', $filteredDocs[1]['foo']);
319+
$this->assertSame('bat', $filteredDocs[2]['foo']);
320+
}
321+
265322
/**
266323
* @test
267324
*/
@@ -316,6 +373,36 @@ public function it_handles_any_of_doc_id_filter()
316373
$this->assertEquals(['bar', 'baz'], $vals);
317374
}
318375

376+
/**
377+
* @test
378+
*/
379+
public function it_handles_any_of_doc_id_filter_with_empty_args()
380+
{
381+
$collectionName = 'test_any_of_doc_id_filter_with_empty_args';
382+
$this->documentStore->addCollection($collectionName);
383+
384+
$firstDocId = Uuid::uuid4()->toString();
385+
$secondDocId = Uuid::uuid4()->toString();
386+
$thirdDocId = Uuid::uuid4()->toString();
387+
388+
$this->documentStore->addDoc($collectionName, $firstDocId, ['foo' => 'bar']);
389+
$this->documentStore->addDoc($collectionName, $secondDocId, ['foo' => 'bat']);
390+
$this->documentStore->addDoc($collectionName, $thirdDocId, ['foo' => 'baz']);
391+
392+
$filteredDocs = \iterator_to_array($this->documentStore->filterDocs(
393+
$collectionName,
394+
new AnyOfDocIdFilter([])
395+
));
396+
397+
$this->assertCount(0, $filteredDocs);
398+
399+
$vals = array_map(function (array $doc) {
400+
return $doc['foo'];
401+
}, $filteredDocs);
402+
403+
$this->assertEquals([], $vals);
404+
}
405+
319406
/**
320407
* @test
321408
*/
@@ -346,6 +433,36 @@ public function it_handles_not_any_of_id_filter()
346433
$this->assertEquals(['bat'], $vals);
347434
}
348435

436+
/**
437+
* @test
438+
*/
439+
public function it_handles_not_any_of_id_filter_with_empty_args()
440+
{
441+
$collectionName = 'test_any_of_doc_id_filter_with_empty_args';
442+
$this->documentStore->addCollection($collectionName);
443+
444+
$firstDocId = Uuid::uuid4()->toString();
445+
$secondDocId = Uuid::uuid4()->toString();
446+
$thirdDocId = Uuid::uuid4()->toString();
447+
448+
$this->documentStore->addDoc($collectionName, $firstDocId, ['foo' => 'bar']);
449+
$this->documentStore->addDoc($collectionName, $secondDocId, ['foo' => 'bat']);
450+
$this->documentStore->addDoc($collectionName, $thirdDocId, ['foo' => 'baz']);
451+
452+
$filteredDocs = \iterator_to_array($this->documentStore->filterDocs(
453+
$collectionName,
454+
new NotFilter(new AnyOfDocIdFilter([]))
455+
));
456+
457+
$this->assertCount(3, $filteredDocs);
458+
459+
$vals = array_map(function (array $doc) {
460+
return $doc['foo'];
461+
}, $filteredDocs);
462+
463+
$this->assertEquals(['bar', 'bat', 'baz'], $vals);
464+
}
465+
349466
/**
350467
* @test
351468
*/

0 commit comments

Comments
 (0)