alpha2 (#2)
- Change multiple field matching to enum - Implement auto-generated IDs Reviewed-on: #2
This commit was merged in pull request #2.
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace BitBadger\PDODocument;
|
||||
|
||||
use Random\RandomException;
|
||||
|
||||
/**
|
||||
* Query construction functions
|
||||
*/
|
||||
@@ -22,12 +24,14 @@ class Query
|
||||
* Create a WHERE clause fragment to implement a comparison on fields in a JSON document
|
||||
*
|
||||
* @param Field[] $fields The field comparison to generate
|
||||
* @param string $conjunction How to join multiple conditions (optional; defaults to AND)
|
||||
* @param FieldMatch|null $match How to join multiple conditions (optional; defaults to All)
|
||||
* @return string The WHERE clause fragment matching the given fields and parameter
|
||||
* @throws DocumentException If the database mode has not been set
|
||||
*/
|
||||
public static function whereByFields(array $fields, string $conjunction = 'AND'): string
|
||||
public static function whereByFields(array $fields, ?FieldMatch $match = null): string
|
||||
{
|
||||
return implode(" $conjunction ", array_map(fn($it) => $it->toWhere(), $fields));
|
||||
return implode(' ' . ($match ?? FieldMatch::All)->toString() . ' ',
|
||||
array_map(fn($it) => $it->toWhere(), $fields));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -35,6 +39,7 @@ class Query
|
||||
*
|
||||
* @param string $paramName The parameter name where the value of the ID will be provided (optional; default @id)
|
||||
* @return string The WHERE clause fragment to match by ID
|
||||
* @throws DocumentException If the database mode has not been set
|
||||
*/
|
||||
public static function whereById(string $paramName = ':id'): string
|
||||
{
|
||||
@@ -42,14 +47,39 @@ class Query
|
||||
}
|
||||
|
||||
/**
|
||||
* Query to insert a document
|
||||
* Create an `INSERT` statement for a document
|
||||
*
|
||||
* @param string $tableName The name of the table into which a document should be inserted
|
||||
* @return string The INSERT statement for the given table
|
||||
* @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
|
||||
*/
|
||||
public static function insert(string $tableName): string
|
||||
public static function insert(string $tableName, ?AutoId $autoId = null): string
|
||||
{
|
||||
return "INSERT INTO $tableName VALUES (:data)";
|
||||
try {
|
||||
$id = Configuration::$idField;
|
||||
$values = match (Configuration::$mode) {
|
||||
Mode::SQLite => match ($autoId ?? AutoId::None) {
|
||||
AutoId::None => ':data',
|
||||
AutoId::Number => "json_set(:data, '$.$id', "
|
||||
. "(SELECT coalesce(max(data->>'$id'), 0) + 1 FROM $tableName))",
|
||||
AutoId::UUID => "json_set(:data, '$.$id', '" . AutoId::generateUUID() . "')",
|
||||
AutoId::RandomString => "json_set(:data, '$.$id', '" . AutoId::generateRandom() ."')"
|
||||
},
|
||||
Mode::PgSQL => match ($autoId ?? AutoId::None) {
|
||||
AutoId::None => ':data',
|
||||
AutoId::Number => ":data || ('{\"$id\":' || "
|
||||
. "(SELECT COALESCE(MAX(data->>'$id'), 0) + 1 FROM $tableName) || '}')",
|
||||
AutoId::UUID => ":data || '{\"$id\":\"" . AutoId::generateUUID() . "\"}'",
|
||||
AutoId::RandomString => ":data || '{\"$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) {
|
||||
throw new DocumentException('Unable to generate ID: ' . $ex->getMessage(), previous: $ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,8 +90,8 @@ class Query
|
||||
*/
|
||||
public static function save(string $tableName): string
|
||||
{
|
||||
return self::insert($tableName)
|
||||
. " ON CONFLICT ((data->>'" . Configuration::$idField . "')) DO UPDATE SET data = EXCLUDED.data";
|
||||
$id = Configuration::$idField;
|
||||
return "INSERT INTO $tableName VALUES (:data) ON CONFLICT ((data->>'$id')) DO UPDATE SET data = EXCLUDED.data";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,6 +99,7 @@ class Query
|
||||
*
|
||||
* @param string $tableName The name of the table in which the document should be updated
|
||||
* @return string The UPDATE query for the document
|
||||
* @throws DocumentException If the database mode has not been set
|
||||
*/
|
||||
public static function update(string $tableName): string
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user