2024-06-08 23:58:45 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace BitBadger\PDODocument;
|
|
|
|
|
|
|
|
use BitBadger\PDODocument\Mapper\CountMapper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Functions to count documents
|
|
|
|
*/
|
|
|
|
class Count
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Count all documents in a table
|
|
|
|
*
|
|
|
|
* @param string $tableName The name of the table in which documents should be counted
|
|
|
|
* @return int The count of documents in the table
|
|
|
|
* @throws DocumentException If one is encountered
|
|
|
|
*/
|
|
|
|
public static function all(string $tableName): int
|
|
|
|
{
|
|
|
|
return Custom::scalar(Query\Count::all($tableName), [], new CountMapper());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Count matching documents using a comparison on JSON fields
|
|
|
|
*
|
|
|
|
* @param string $tableName The name of the table in which documents should be counted
|
|
|
|
* @param array|Field[] $fields The field comparison to match
|
2024-06-11 11:07:56 +00:00
|
|
|
* @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All)
|
2024-06-08 23:58:45 +00:00
|
|
|
* @return int The count of documents matching the field comparison
|
|
|
|
* @throws DocumentException If one is encountered
|
|
|
|
*/
|
2024-06-11 11:07:56 +00:00
|
|
|
public static function byFields(string $tableName, array $fields, ?FieldMatch $match = null): int
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
|
|
|
$namedFields = Parameters::nameFields($fields);
|
2024-06-11 11:07:56 +00:00
|
|
|
return Custom::scalar(Query\Count::byFields($tableName, $namedFields, $match),
|
2024-06-08 23:58:45 +00:00
|
|
|
Parameters::addFields($namedFields, []), new CountMapper());
|
|
|
|
}
|
2024-06-21 13:46:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Count matching documents using a JSON containment query (`@>`; PostgreSQL only)
|
|
|
|
*
|
|
|
|
* @param string $tableName The name of the table in which documents should be counted
|
|
|
|
* @param array|object $criteria The criteria for the JSON containment query
|
|
|
|
* @return int The number of documents matching the JSON containment query
|
|
|
|
* @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs
|
|
|
|
*/
|
|
|
|
public static function byContains(string $tableName, array|object $criteria): int
|
|
|
|
{
|
|
|
|
return Custom::scalar(Query\Count::byContains($tableName), Parameters::json(':criteria', $criteria),
|
|
|
|
new CountMapper());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Count matching documents using a JSON Path match query (`@?`; PostgreSQL only)
|
|
|
|
*
|
|
|
|
* @param string $tableName The name of the table in which documents should be counted
|
|
|
|
* @param string $path The JSON Path match string
|
|
|
|
* @return int The number of documents matching the given JSON Path criteria
|
|
|
|
* @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs
|
|
|
|
*/
|
|
|
|
public static function byJsonPath(string $tableName, string $path): int
|
|
|
|
{
|
|
|
|
return Custom::scalar(Query\Count::byJsonPath($tableName), [':path' => $path], new CountMapper());
|
|
|
|
}
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|