diff --git a/src/PostgresDocumentStore.php b/src/PostgresDocumentStore.php index d242afa..1e99cff 100644 --- a/src/PostgresDocumentStore.php +++ b/src/PostgresDocumentStore.php @@ -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" : ''; @@ -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) { diff --git a/tests/PostgresDocumentStoreTest.php b/tests/PostgresDocumentStoreTest.php index a5dcf9c..a116447 100644 --- a/tests/PostgresDocumentStoreTest.php +++ b/tests/PostgresDocumentStoreTest.php @@ -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; @@ -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);