Skip to content
Merged
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
23 changes: 23 additions & 0 deletions src/PostgresDocumentStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,29 @@ public function filterDocs(string $collectionName, Filter $filter, int $skip = n
}
}

/**
* @param string $collectionName
* @param Filter $filter
* @return int number of docs
*/
public function countDocs(string $collectionName, Filter $filter): int
{
[$filterStr, $args] = $this->filterToWhereClause($filter);

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

$query = <<<EOT
SELECT count(doc)
FROM {$this->schemaName($collectionName)}.{$this->tableName($collectionName)}
$where;
EOT;
$stmt = $this->connection->prepare($query);

$stmt->execute($args);

return (int) $stmt->fetchColumn(0);
}

private function transactional(callable $callback)
{
if($this->manageTransactions) {
Expand Down
26 changes: 26 additions & 0 deletions tests/PostgresDocumentStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,32 @@ public function it_handles_not_filter_nested_in_and_filter()
$this->assertEquals([$thirdDocId], $refs);
}

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

$doc1 = ["foo" => "bar"];
$doc2 = ["foo" => "baz"];
$doc3 = ["foo" => "bat"];

$docs = [$doc1, $doc2, $doc3];

array_walk($docs, function (array $doc) use ($collectionName) {
$this->documentStore->addDoc($collectionName, Uuid::uuid4()->toString(), $doc);
});

$count = $this->documentStore->countDocs(
$collectionName,
new AnyOfFilter("foo", ["bar", "bat"])
);

$this->assertSame(2, $count);
}

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