Initial SQLite development (#1)
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
81
src/Parameters.php
Normal file
81
src/Parameters.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace BitBadger\PDODocument;
|
||||
|
||||
/**
|
||||
* Functions to create parameters for queries
|
||||
*/
|
||||
class Parameters
|
||||
{
|
||||
/**
|
||||
* Create an ID parameter (name ":id", key will be treated as a string)
|
||||
*
|
||||
* @param mixed $key The key representing the ID of the document
|
||||
* @return array|string[] An associative array with an "@id" parameter/value pair
|
||||
*/
|
||||
public static function id(mixed $key): array
|
||||
{
|
||||
return [':id' => is_int($key) || is_string($key) ? $key : "$key"];
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a parameter with a JSON value
|
||||
*
|
||||
* @param string $name The name of the JSON parameter
|
||||
* @param object|array $document The value that should be passed as a JSON string
|
||||
* @return array An associative array with the named parameter/value pair
|
||||
*/
|
||||
public static function json(string $name, object|array $document): array
|
||||
{
|
||||
return [$name => json_encode($document, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)];
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill in parameter names for any fields missing one
|
||||
*
|
||||
* @param array|Field[] $fields The fields for the query
|
||||
* @return array|Field[] The fields, all with non-blank parameter names
|
||||
*/
|
||||
public static function nameFields(array $fields): array
|
||||
{
|
||||
for ($idx = 0; $idx < sizeof($fields); $idx++) {
|
||||
if ($fields[$idx]->paramName == '') $fields[$idx]->paramName = ":field$idx";
|
||||
}
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add field parameters to the given set of parameters
|
||||
*
|
||||
* @param array|Field[] $fields The fields being compared in the query
|
||||
* @param array $parameters An associative array of parameters to which the fields should be added
|
||||
* @return array An associative array of parameter names and values with the fields added
|
||||
*/
|
||||
public static function addFields(array $fields, array $parameters): array
|
||||
{
|
||||
return array_reduce($fields, fn($carry, $item) => $item->appendParameter($carry), $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create JSON field name parameters for the given field names to the given parameter
|
||||
*
|
||||
* @param string $paramName The name of the parameter for the field names
|
||||
* @param array|string[] $fieldNames The names of the fields for the parameter
|
||||
* @return array An associative array of parameter/value pairs for the field names
|
||||
* @throws DocumentException If the database mode has not been set
|
||||
*/
|
||||
public static function fieldNames(string $paramName, array $fieldNames): array
|
||||
{
|
||||
switch (Configuration::$mode) {
|
||||
case Mode::PgSQL:
|
||||
return [$paramName => "ARRAY['" . implode("','", $fieldNames) . "']"];
|
||||
case Mode::SQLite:
|
||||
$it = [];
|
||||
$idx = 0;
|
||||
foreach ($fieldNames as $field) $it[$paramName . $idx++] = "$.$field";
|
||||
return $it;
|
||||
default:
|
||||
throw new DocumentException('Database mode not set; cannot generate field name parameters');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user