Initial SQLite development (#1)
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
47
src/Query/Exists.php
Normal file
47
src/Query/Exists.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace BitBadger\PDODocument\Query;
|
||||
|
||||
use BitBadger\PDODocument\{Field, Query};
|
||||
|
||||
/**
|
||||
* Queries to determine document existence
|
||||
*/
|
||||
class Exists
|
||||
{
|
||||
/**
|
||||
* The shell of a query for an existence check
|
||||
*
|
||||
* @param string $tableName The name of the table in which existence should be checked
|
||||
* @param string $whereClause The conditions for which existence should be checked
|
||||
* @return string The query to determine existence of documents
|
||||
*/
|
||||
public static function query(string $tableName, string $whereClause): string
|
||||
{
|
||||
return "SELECT EXISTS (SELECT 1 FROM $tableName WHERE $whereClause)";
|
||||
}
|
||||
|
||||
/**
|
||||
* Query to determine if a document exists for the given ID
|
||||
*
|
||||
* @param string $tableName The name of the table in which document existence should be checked
|
||||
* @return string The query to determine document existence by ID
|
||||
*/
|
||||
public static function byId(string $tableName): string
|
||||
{
|
||||
return self::query($tableName, Query::whereById());
|
||||
}
|
||||
|
||||
/**
|
||||
* Query to determine if documents exist using a comparison on JSON fields
|
||||
*
|
||||
* @param string $tableName The name of the table in which document existence should be checked
|
||||
* @param Field[] $fields The field comparison to match
|
||||
* @param string $conjunction How to handle multiple conditions (optional; defaults to `AND`)
|
||||
* @return string The query to determine document existence by field comparison
|
||||
*/
|
||||
public static function byFields(string $tableName, array $fields, string $conjunction = 'AND'): string
|
||||
{
|
||||
return self::query($tableName, Query::whereByFields($fields, $conjunction));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user