pdo-document/src/Query/Count.php
Daniel J. Summers d8330d828a Derive mode from DSN function
- Add headers in all files
- Minor field name changes
2024-07-20 21:47:21 -04:00

67 lines
2.3 KiB
PHP

<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
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);
}
/**
* Query to count matching documents using a JSON containment query (PostgreSQL only)
*
* @param string $tableName The name of the table in which documents should be counted
* @return string The query to count documents using a JSON containment query
* @throws DocumentException If the database mode is not PostgreSQL
*/
public static function byContains(string $tableName): string
{
return self::all($tableName) . ' WHERE ' . Query::whereDataContains();
}
/**
* Query to count matching documents using a JSON Path match (PostgreSQL only)
*
* @param string $tableName The name of the table in which documents should be counted
* @return string The query to count documents using a JSON Path match
* @throws DocumentException If the database mode is not PostgreSQL
*/
public static function byJsonPath(string $tableName): string
{
return self::all($tableName) . ' WHERE ' . Query::whereJsonPathMatches();
}
}