v2.1 (#10)
- Add `Json` string and output functions - Add documentation for these functions Reviewed-on: #10
This commit was merged in pull request #10.
This commit is contained in:
@@ -10,6 +10,7 @@ namespace BitBadger\PDODocument;
|
||||
|
||||
use BitBadger\InspiredByFSharp\Option;
|
||||
use BitBadger\PDODocument\Mapper\Mapper;
|
||||
use BitBadger\PDODocument\Mapper\StringMapper;
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use PDOStatement;
|
||||
@@ -93,7 +94,42 @@ class Custom
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a query that returns one or no results (returns false if not found)
|
||||
* Execute a query that returns a JSON string of results
|
||||
*
|
||||
* @param string $query The query to be executed
|
||||
* @param array<string, mixed> $parameters Parameters to use in executing the query
|
||||
* @return string A JSON array with the results (empty results will be `[]`)
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function jsonArray(string $query, array $parameters): string
|
||||
{
|
||||
return '[' . implode(',', self::array($query, $parameters, new StringMapper('data'))) . ']';
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a query, echoing the results to the output
|
||||
*
|
||||
* @param string $query The query to be executed
|
||||
* @param array<string, mixed> $parameters Parameters to use in executing the query
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function outputJsonArray(string $query, array $parameters): void
|
||||
{
|
||||
$isFirst = true;
|
||||
echo '[';
|
||||
foreach (self::list($query, $parameters, new StringMapper('data'))->items as $doc) {
|
||||
if ($isFirst) {
|
||||
$isFirst = false;
|
||||
} else {
|
||||
echo ',';
|
||||
}
|
||||
echo $doc;
|
||||
}
|
||||
echo ']';
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a query that returns one or no results
|
||||
*
|
||||
* @template TDoc The domain type of the document to retrieve
|
||||
* @param string $query The query to be executed (will have "LIMIT 1" appended)
|
||||
@@ -112,6 +148,19 @@ class Custom
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a query that returns one or no JSON results
|
||||
*
|
||||
* @param string $query The query to be executed (will have "LIMIT 1" appended)
|
||||
* @param array<string, mixed> $parameters Parameters to use in executing the query
|
||||
* @return string The JSON document (returns `{}` if no document is found)
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function jsonSingle(string $query, array $parameters): string
|
||||
{
|
||||
return self::single($query, $parameters, new StringMapper('data'))->getOrDefault('{}');
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a query that does not return a value
|
||||
*
|
||||
|
||||
@@ -12,7 +12,7 @@ use BitBadger\InspiredByFSharp\Option;
|
||||
use BitBadger\PDODocument\Mapper\DocumentMapper;
|
||||
|
||||
/**
|
||||
* Functions to find documents
|
||||
* Functions to retrieve documents as domain objects
|
||||
*/
|
||||
class Find
|
||||
{
|
||||
|
||||
252
src/Json.php
Normal file
252
src/Json.php
Normal file
@@ -0,0 +1,252 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace BitBadger\PDODocument;
|
||||
|
||||
/**
|
||||
* Functions to retrieve and output documents as JSON
|
||||
*/
|
||||
class Json
|
||||
{
|
||||
/**
|
||||
* Retrieve all JSON documents in the given table
|
||||
*
|
||||
* @param string $tableName The table from which documents should be retrieved
|
||||
* @param Field[] $orderBy Fields by which the results should be ordered (optional, default no ordering)
|
||||
* @return string A JSON array of all documents from the table
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function all(string $tableName, array $orderBy = []): string
|
||||
{
|
||||
return Custom::jsonArray(Query::selectFromTable($tableName) . Query::orderBy($orderBy), []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a JSON document by its ID
|
||||
*
|
||||
* @param string $tableName The table from which the document should be retrieved
|
||||
* @param mixed $docId The ID of the document to retrieve
|
||||
* @return string The JSON document if found, `{}` otherwise
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function byId(string $tableName, mixed $docId): string
|
||||
{
|
||||
return Custom::jsonSingle(Query\Find::byId($tableName, $docId), Parameters::id($docId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve JSON documents via a comparison on JSON fields
|
||||
*
|
||||
* @param string $tableName 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)
|
||||
* @param Field[] $orderBy Fields by which the results should be ordered (optional, default no ordering)
|
||||
* @return string A JSON array of documents matching the given field comparison
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function byFields(string $tableName, array $fields, ?FieldMatch $match = null,
|
||||
array $orderBy = []): string
|
||||
{
|
||||
Parameters::nameFields($fields);
|
||||
return Custom::jsonArray(Query\Find::byFields($tableName, $fields, $match) . Query::orderBy($orderBy),
|
||||
Parameters::addFields($fields, []));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve JSON documents via a JSON containment query (`@>`; PostgreSQL only)
|
||||
*
|
||||
* @param string $tableName The name of the table from which documents should be retrieved
|
||||
* @param mixed[]|object $criteria The criteria for the JSON containment query
|
||||
* @param Field[] $orderBy Fields by which the results should be ordered (optional, default no ordering)
|
||||
* @return string A JSON array of documents matching the JSON containment query
|
||||
* @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs
|
||||
*/
|
||||
public static function byContains(string $tableName, array|object $criteria, array $orderBy = []): string
|
||||
{
|
||||
return Custom::jsonArray(Query\Find::byContains($tableName) . Query::orderBy($orderBy),
|
||||
Parameters::json(':criteria', $criteria));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve JSON documents via a JSON Path match query (`@?`; PostgreSQL only)
|
||||
*
|
||||
* @param string $tableName The name of the table from which documents should be retrieved
|
||||
* @param string $path The JSON Path match string
|
||||
* @param Field[] $orderBy Fields by which the results should be ordered (optional, default no ordering)
|
||||
* @return string A JSON array of documents matching the JSON Path
|
||||
* @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs
|
||||
*/
|
||||
public static function byJsonPath(string $tableName, string $path, array $orderBy = []): string
|
||||
{
|
||||
return Custom::jsonArray(Query\Find::byJsonPath($tableName) . Query::orderBy($orderBy), [':path' => $path]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve JSON documents via a comparison on JSON fields, returning only the first result
|
||||
*
|
||||
* @param string $tableName The table from which the document should be retrieved
|
||||
* @param Field[] $fields The field comparison to match
|
||||
* @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All)
|
||||
* @param Field[] $orderBy Fields by which the results should be ordered (optional, default no ordering)
|
||||
* @return string The first JSON document if any matches are found, `{}` otherwise
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function firstByFields(string $tableName, array $fields, ?FieldMatch $match = null,
|
||||
array $orderBy = []): string
|
||||
{
|
||||
Parameters::nameFields($fields);
|
||||
return Custom::jsonSingle(Query\Find::byFields($tableName, $fields, $match) . Query::orderBy($orderBy),
|
||||
Parameters::addFields($fields, []));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve JSON documents via a JSON containment query (`@>`), returning only the first result (PostgreSQL only)
|
||||
*
|
||||
* @param string $tableName The name of the table from which documents should be retrieved
|
||||
* @param mixed[]|object $criteria The criteria for the JSON containment query
|
||||
* @param Field[] $orderBy Fields by which the results should be ordered (optional, default no ordering)
|
||||
* @return string The first JSON document if any matches are found, `{}` otherwise
|
||||
* @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs
|
||||
*/
|
||||
public static function firstByContains(string $tableName, array|object $criteria, array $orderBy = []): string
|
||||
{
|
||||
return Custom::jsonSingle(Query\Find::byContains($tableName) . Query::orderBy($orderBy),
|
||||
Parameters::json(':criteria', $criteria));
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve JSON documents via a JSON Path match query (`@?`), returning only the first result (PostgreSQL only)
|
||||
*
|
||||
* @param string $tableName The name of the table from which documents should be retrieved
|
||||
* @param string $path The JSON Path match string
|
||||
* @param Field[] $orderBy Fields by which the results should be ordered (optional, default no ordering)
|
||||
* @return string The first JSON document if any matches are found, `{}` otherwise
|
||||
* @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs
|
||||
*/
|
||||
public static function firstByJsonPath(string $tableName, string $path, array $orderBy = []): string
|
||||
{
|
||||
return Custom::jsonSingle(Query\Find::byJsonPath($tableName) . Query::orderBy($orderBy), [':path' => $path]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Output all JSON documents in the given table
|
||||
*
|
||||
* @param string $tableName The table from which documents should be retrieved
|
||||
* @param Field[] $orderBy Fields by which the results should be ordered (optional, default no ordering)
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function outputAll(string $tableName, array $orderBy = []): void
|
||||
{
|
||||
Custom::outputJsonArray(Query::selectFromTable($tableName) . Query::orderBy($orderBy), []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Output a JSON document by its ID
|
||||
*
|
||||
* @param string $tableName The table from which the document should be retrieved
|
||||
* @param mixed $docId The ID of the document to retrieve
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function outputById(string $tableName, mixed $docId): void
|
||||
{
|
||||
echo self::byId($tableName, $docId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Output JSON documents via a comparison on JSON fields
|
||||
*
|
||||
* @param string $tableName 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)
|
||||
* @param Field[] $orderBy Fields by which the results should be ordered (optional, default no ordering)
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function outputByFields(string $tableName, array $fields, ?FieldMatch $match = null,
|
||||
array $orderBy = []): void
|
||||
{
|
||||
Parameters::nameFields($fields);
|
||||
Custom::outputJsonArray(Query\Find::byFields($tableName, $fields, $match) . Query::orderBy($orderBy),
|
||||
Parameters::addFields($fields, []));
|
||||
}
|
||||
|
||||
/**
|
||||
* Output JSON documents via a JSON containment query (`@>`; PostgreSQL only)
|
||||
*
|
||||
* @param string $tableName The name of the table from which documents should be retrieved
|
||||
* @param mixed[]|object $criteria The criteria for the JSON containment query
|
||||
* @param Field[] $orderBy Fields by which the results should be ordered (optional, default no ordering)
|
||||
* @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs
|
||||
*/
|
||||
public static function outputByContains(string $tableName, array|object $criteria, array $orderBy = []): void
|
||||
{
|
||||
Custom::outputJsonArray(Query\Find::byContains($tableName) . Query::orderBy($orderBy),
|
||||
Parameters::json(':criteria', $criteria));
|
||||
}
|
||||
|
||||
/**
|
||||
* Output JSON documents via a JSON Path match query (`@?`; PostgreSQL only)
|
||||
*
|
||||
* @param string $tableName The name of the table from which documents should be retrieved
|
||||
* @param string $path The JSON Path match string
|
||||
* @param Field[] $orderBy Fields by which the results should be ordered (optional, default no ordering)
|
||||
* @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs
|
||||
*/
|
||||
public static function outputByJsonPath(string $tableName, string $path, array $orderBy = []): void
|
||||
{
|
||||
Custom::outputJsonArray(Query\Find::byJsonPath($tableName) . Query::orderBy($orderBy), [':path' => $path]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Output JSON documents via a comparison on JSON fields, returning only the first result
|
||||
*
|
||||
* @param string $tableName The table from which the document should be retrieved
|
||||
* @param Field[] $fields The field comparison to match
|
||||
* @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All)
|
||||
* @param Field[] $orderBy Fields by which the results should be ordered (optional, default no ordering)
|
||||
* @throws DocumentException If any is encountered
|
||||
*/
|
||||
public static function outputFirstByFields(string $tableName, array $fields, ?FieldMatch $match = null,
|
||||
array $orderBy = []): void
|
||||
{
|
||||
echo self::firstByFields($tableName, $fields, $match, $orderBy);
|
||||
}
|
||||
|
||||
/**
|
||||
* Output JSON documents via a JSON containment query (`@>`), returning only the first result (PostgreSQL only)
|
||||
*
|
||||
* @param string $tableName The name of the table from which documents should be retrieved
|
||||
* @param mixed[]|object $criteria The criteria for the JSON containment query
|
||||
* @param Field[] $orderBy Fields by which the results should be ordered (optional, default no ordering)
|
||||
* @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs
|
||||
*/
|
||||
public static function outputFirstByContains(string $tableName, array|object $criteria, array $orderBy = []): void
|
||||
{
|
||||
echo self::firstByContains($tableName, $criteria, $orderBy);
|
||||
}
|
||||
|
||||
/**
|
||||
* Output JSON documents via a JSON Path match query (`@?`), returning only the first result (PostgreSQL only)
|
||||
*
|
||||
* @param string $tableName The name of the table from which documents should be retrieved
|
||||
* @param string $path The JSON Path match string
|
||||
* @param Field[] $orderBy Fields by which the results should be ordered (optional, default no ordering)
|
||||
* @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs
|
||||
*/
|
||||
public static function outputFirstByJsonPath(string $tableName, string $path, array $orderBy = []): void
|
||||
{
|
||||
echo self::firstByJsonPath($tableName, $path, $orderBy);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the content type of this page's output to JSON
|
||||
*/
|
||||
public static function setContentType(): void
|
||||
{
|
||||
header('Content-Type: application/json; charset=UTF-8');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user