Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/PostgresDocumentStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ public function filterDocs(string $collectionName, Filter $filter, int $skip = n
{
[$filterStr, $args] = $this->filterToWhereClause($filter);

$where = $filterStr? "WHERE $filterStr" : '';
$where = $filterStr ? "WHERE $filterStr" : '';

$offset = $skip !== null ? "OFFSET $skip" : '';
$limit = $limit !== null ? "LIMIT $limit" : '';
Expand All @@ -524,6 +524,29 @@ public function filterDocs(string $collectionName, Filter $filter, int $skip = n
}
}

/**
* @param string $collectionName
* @param Filter $filter
* @return array
*/
public function filterDocIds(string $collectionName, Filter $filter): array
{
[$filterStr, $args] = $this->filterToWhereClause($filter);

$where = $filterStr ? "WHERE {$filterStr}" : '';
$query = "SELECT id FROM {$this->schemaName($collectionName)}.{$this->tableName($collectionName)} {$where}";

$stmt = $this->connection->prepare($query);
$stmt->execute($args);

$docIds = [];
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
$docIds[] = $row['id'];
}

return $docIds;
}

private function transactional(callable $callback)
{
if($this->manageTransactions) {
Expand Down
27 changes: 27 additions & 0 deletions tests/PostgresDocumentStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
use EventEngine\DocumentStore\Filter\AnyOfFilter;
use EventEngine\DocumentStore\Filter\DocIdFilter;
use EventEngine\DocumentStore\Filter\EqFilter;
use EventEngine\DocumentStore\Filter\GtFilter;
use EventEngine\DocumentStore\Filter\InArrayFilter;
use EventEngine\DocumentStore\Filter\LtFilter;
use EventEngine\DocumentStore\Filter\NotFilter;
use EventEngine\DocumentStore\Filter\OrFilter;
use PHPUnit\Framework\TestCase;
use EventEngine\DocumentStore\FieldIndex;
use EventEngine\DocumentStore\Index;
Expand Down Expand Up @@ -434,6 +437,30 @@ public function it_handles_not_filter_nested_in_and_filter()
$this->assertEquals([$thirdDocId], $refs);
}

/**
* @test
*/
public function it_retrieves_doc_ids_by_filter()
{
$collectionName = 'test_not_filter_nested_in_and_filter';
$this->documentStore->addCollection($collectionName);

$firstDocId = Uuid::uuid4()->toString();
$secondDocId = Uuid::uuid4()->toString();
$thirdDocId = Uuid::uuid4()->toString();

$this->documentStore->addDoc($collectionName, $firstDocId, ['number' => 10]);
$this->documentStore->addDoc($collectionName, $secondDocId, ['number' => 20]);
$this->documentStore->addDoc($collectionName, $thirdDocId, ['number' => 30]);

$result = $this->documentStore->filterDocIds($collectionName, new OrFilter(
new GtFilter('number', 21),
new LtFilter('number', 19)
));

$this->assertEquals([$firstDocId, $thirdDocId], $result);
}

private function getIndexes(string $collectionName): array
{
return TestUtil::getIndexes($this->connection, self::TABLE_PREFIX.$collectionName);
Expand Down