2024-07-21 01:47:21 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2024-06-08 23:58:45 +00:00
|
|
|
|
|
|
|
namespace BitBadger\PDODocument;
|
|
|
|
|
|
|
|
use BitBadger\PDODocument\Mapper\ExistsMapper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Functions to determine if documents exist
|
|
|
|
*/
|
|
|
|
class Exists
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Determine if a document exists for the given ID
|
|
|
|
*
|
|
|
|
* @param string $tableName The name of the table in which document existence should be determined
|
|
|
|
* @param mixed $docId The ID of the document whose existence should be determined
|
|
|
|
* @return bool True if the document exists, false if not
|
|
|
|
* @throws DocumentException If any is encountered
|
|
|
|
*/
|
|
|
|
public static function byId(string $tableName, mixed $docId): bool
|
|
|
|
{
|
2024-06-21 13:46:41 +00:00
|
|
|
return Custom::scalar(Query\Exists::byId($tableName, $docId), Parameters::id($docId), new ExistsMapper());
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if a document exists using a comparison on JSON fields
|
|
|
|
*
|
|
|
|
* @param string $tableName The name of the table in which document existence should be determined
|
|
|
|
* @param 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 bool True if any documents match the field comparison, false if not
|
|
|
|
* @throws DocumentException If any is encountered
|
|
|
|
*/
|
2024-06-11 11:07:56 +00:00
|
|
|
public static function byFields(string $tableName, array $fields, ?FieldMatch $match = null): bool
|
2024-06-08 23:58:45 +00:00
|
|
|
{
|
2024-09-27 02:15:00 +00:00
|
|
|
Parameters::nameFields($fields);
|
|
|
|
return Custom::scalar(Query\Exists::byFields($tableName, $fields, $match), Parameters::addFields($fields, []),
|
|
|
|
new ExistsMapper());
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|
2024-06-21 13:46:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if documents exist by a JSON containment query (`@>`; PostgreSQL only)
|
|
|
|
*
|
|
|
|
* @param string $tableName The name of the table in which document existence should be determined
|
2024-09-27 02:15:00 +00:00
|
|
|
* @param mixed[]|object $criteria The criteria for the JSON containment query
|
2024-06-21 13:46:41 +00:00
|
|
|
* @return bool True if any documents match the JSON containment query, false if not
|
|
|
|
* @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs
|
|
|
|
*/
|
|
|
|
public static function byContains(string $tableName, array|object $criteria): bool
|
|
|
|
{
|
|
|
|
return Custom::scalar(Query\Exists::byContains($tableName), Parameters::json(':criteria', $criteria),
|
|
|
|
new ExistsMapper());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if documents exist by a JSON Path match query (`@?`; PostgreSQL only)
|
|
|
|
*
|
|
|
|
* @param string $tableName The name of the table in which document existence should be determined
|
|
|
|
* @param string $path The JSON Path match string
|
|
|
|
* @return bool True if any documents match the JSON Path string, false if not
|
|
|
|
* @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs
|
|
|
|
*/
|
|
|
|
public static function byJsonPath(string $tableName, string $path): bool
|
|
|
|
{
|
|
|
|
return Custom::scalar(Query\Exists::byJsonPath($tableName), [':path' => $path], new ExistsMapper());
|
|
|
|
}
|
2024-06-08 23:58:45 +00:00
|
|
|
}
|