37 lines
1.2 KiB
PHP

<?php declare(strict_types=1);
namespace BitBadger\PDODocument\Query;
use BitBadger\PDODocument\{DocumentException, Field, FieldMatch, Query};
/**
* Queries for counting documents
*/
class Count
{
/**
* Query to count all documents in a table
*
* @param string $tableName The name of the table in which documents should be counted
* @return string The query to count all documents in a table
*/
public static function all(string $tableName): string
{
return "SELECT COUNT(*) FROM $tableName";
}
/**
* Query to count matching documents using a text comparison on JSON fields
*
* @param string $tableName The name of the table in which documents should be counted
* @param Field[] $fields The field comparison to match
* @param FieldMatch|null $match How to join multiple conditions (optional; defaults to All)
* @return string The query to count documents using a field comparison
* @throws DocumentException If the database mode has not been set
*/
public static function byFields(string $tableName, array $fields, ?FieldMatch $match = null): string
{
return self::all($tableName) . ' WHERE ' . Query::whereByFields($fields, $match);
}
}