* @license MIT */ declare(strict_types=1); namespace BitBadger\PDODocument\Query; use BitBadger\PDODocument\{DocumentException, Field, FieldMatch, Query}; /** * Queries for retrieving documents */ class Find { /** * Query to retrieve a document by its ID * * @param string $tableName The name of the table from which a document should be retrieved * @param mixed $docId The ID of the document to be retrieved (optional; string ID assumed) * @return string The SELECT statement to retrieve a document by its ID * @throws DocumentException If the database mode has not been set */ public static function byId(string $tableName, mixed $docId = null): string { return Query::selectFromTable($tableName) . ' WHERE ' . Query::whereById(docId: $docId); } /** * Query to retrieve documents using a comparison on JSON fields * * @param string $tableName The name of the table from which documents should be retrieved * @param Field[] $fields The field comparison to match * @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All) * @return string The SELECT statement to retrieve documents by 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 Query::selectFromTable($tableName) . ' WHERE ' . Query::whereByFields($fields, $match); } /** * Query to retrieve documents using a JSON containment query (PostgreSQL only) * * @param string $tableName The name of the table from which documents should be retrieved * @return string The SELECT statement to retrieve documents by a JSON containment query * @throws DocumentException If the database mode is not PostgreSQL */ public static function byContains(string $tableName): string { return Query::selectFromTable($tableName) . ' WHERE ' . Query::whereDataContains(); } /** * Query to retrieve documents using a JSON Path match query (PostgreSQL only) * * @param string $tableName The name of the table from which documents should be retrieved * @return string The SELECT statement to retrieve documents by a JSON Path match * @throws DocumentException If the database mode is not PostgreSQL */ public static function byJsonPath(string $tableName): string { return Query::selectFromTable($tableName) . ' WHERE ' . Query::whereJsonPathMatches(); } }