Derive mode from DSN function
- Add headers in all files - Minor field name changes
This commit is contained in:
@@ -1,7 +1,14 @@
|
||||
<?php declare(strict_types=1);
|
||||
<?php
|
||||
/**
|
||||
* @author Daniel J. Summers <daniel@bitbadger.solutions>
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace BitBadger\PDODocument;
|
||||
|
||||
use Exception;
|
||||
use Random\RandomException;
|
||||
|
||||
/**
|
||||
@@ -30,8 +37,7 @@ class Query
|
||||
*/
|
||||
public static function whereByFields(array $fields, ?FieldMatch $match = null): string
|
||||
{
|
||||
return implode(' ' . ($match ?? FieldMatch::All)->toString() . ' ',
|
||||
array_map(fn($it) => $it->toWhere(), $fields));
|
||||
return implode(' ' . ($match ?? FieldMatch::All)->toSQL() . ' ', array_map(fn($it) => $it->toWhere(), $fields));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,11 +59,11 @@ class Query
|
||||
*
|
||||
* @param string $paramName The name of the parameter (optional; defaults to `:criteria`)
|
||||
* @return string The WHERE clause fragment for a JSON containment query
|
||||
* @throws DocumentException If the database mode is not PostgreSQL
|
||||
* @throws Exception|DocumentException If the database mode is not PostgreSQL
|
||||
*/
|
||||
public static function whereDataContains(string $paramName = ':criteria'): string
|
||||
{
|
||||
if (Configuration::$mode <> Mode::PgSQL) {
|
||||
if (Configuration::mode() <> Mode::PgSQL) {
|
||||
throw new DocumentException('JSON containment is only supported on PostgreSQL');
|
||||
}
|
||||
return "data @> $paramName";
|
||||
@@ -68,11 +74,11 @@ class Query
|
||||
*
|
||||
* @param string $paramName The name of the parameter (optional; defaults to `:path`)
|
||||
* @return string The WHERE clause fragment for a JSON Path match query
|
||||
* @throws DocumentException If the database mode is not PostgreSQL
|
||||
* @throws Exception|DocumentException If the database mode is not PostgreSQL
|
||||
*/
|
||||
public static function whereJsonPathMatches(string $paramName = ':path'): string
|
||||
{
|
||||
if (Configuration::$mode <> Mode::PgSQL) {
|
||||
if (Configuration::mode() <> Mode::PgSQL) {
|
||||
throw new DocumentException('JSON Path matching is only supported on PostgreSQL');
|
||||
}
|
||||
return "jsonb_path_exists(data, $paramName::jsonpath)";
|
||||
@@ -84,13 +90,13 @@ class Query
|
||||
* @param string $tableName The name of the table into which the document will be inserted
|
||||
* @param AutoId|null $autoId The version of automatic ID query to generate (optional, defaults to None)
|
||||
* @return string The `INSERT` statement to insert a document
|
||||
* @throws DocumentException If the database mode is not set
|
||||
* @throws Exception|DocumentException If the database mode is not set
|
||||
*/
|
||||
public static function insert(string $tableName, ?AutoId $autoId = null): string
|
||||
{
|
||||
try {
|
||||
$id = Configuration::$idField;
|
||||
$values = match (Configuration::$mode) {
|
||||
$id = Configuration::$idField;
|
||||
$values = match (Configuration::mode('generate auto-ID INSERT statement')) {
|
||||
Mode::SQLite => match ($autoId ?? AutoId::None) {
|
||||
AutoId::None => ':data',
|
||||
AutoId::Number => "json_set(:data, '$.$id', "
|
||||
@@ -105,9 +111,7 @@ class Query
|
||||
. "FROM $tableName) || '}')::jsonb",
|
||||
AutoId::UUID => ":data::jsonb || '{\"$id\":\"" . AutoId::generateUUID() . "\"}'",
|
||||
AutoId::RandomString => ":data::jsonb || '{\"$id\":\"" . AutoId::generateRandom() . "\"}'",
|
||||
},
|
||||
default =>
|
||||
throw new DocumentException('Database mode not set; cannot generate auto-ID INSERT statement'),
|
||||
}
|
||||
};
|
||||
return "INSERT INTO $tableName VALUES ($values)";
|
||||
} catch (RandomException $ex) {
|
||||
|
||||
Reference in New Issue
Block a user