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