Change param prefix from @ to :

This commit is contained in:
2024-06-04 20:59:24 -04:00
parent 7390ae0f61
commit 3d45bbcabc
13 changed files with 175 additions and 54 deletions

View File

@@ -8,14 +8,14 @@ namespace BitBadger\PDODocument;
class Parameters
{
/**
* Create an ID parameter (name "@id", key will be treated as a string)
* 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_string($key) ? $key : "$key"];
return [':id' => is_string($key) ? $key : "$key"];
}
/**
@@ -39,7 +39,7 @@ class Parameters
public static function nameFields(array $fields): array
{
for ($idx = 0; $idx < sizeof($fields); $idx++) {
if ($fields[$idx]->paramName == '') $fields[$idx]->paramName = "@field$idx";
if ($fields[$idx]->paramName == '') $fields[$idx]->paramName = ":field$idx";
}
return $fields;
}

View File

@@ -21,7 +21,7 @@ class Patch
public static function byId(string $tableName, mixed $docId, array|object $patch, ?PDO $pdo = null): void
{
Custom::nonQuery(Query\Patch::byId($tableName),
array_merge(Parameters::id($docId), Parameters::json('@data', $patch)), $pdo);
array_merge(Parameters::id($docId), Parameters::json(':data', $patch)), $pdo);
}
/**
@@ -39,6 +39,6 @@ class Patch
{
$namedFields = Parameters::nameFields($fields);
Custom::nonQuery(Query\Patch::byFields($tableName, $namedFields, $conjunction),
Parameters::addFields($namedFields, Parameters::json('@data', $patch)), $pdo);
Parameters::addFields($namedFields, Parameters::json(':data', $patch)), $pdo);
}
}

View File

@@ -36,7 +36,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
*/
public static function whereById(string $paramName = '@id'): string
public static function whereById(string $paramName = ':id'): string
{
return self::whereByFields([Field::EQ(Configuration::$idField, 0, $paramName)]);
}
@@ -49,7 +49,7 @@ class Query
*/
public static function insert(string $tableName): string
{
return "INSERT INTO $tableName VALUES (@data)";
return "INSERT INTO $tableName VALUES (:data)";
}
/**
@@ -72,6 +72,6 @@ class Query
*/
public static function update(string $tableName): string
{
return "UPDATE $tableName SET data = @data WHERE " . self::whereById();
return "UPDATE $tableName SET data = :data WHERE " . self::whereById();
}
}

View File

@@ -24,8 +24,8 @@ class Patch
public static function update(string $tableName, string $whereClause): string
{
$setValue = match (Configuration::$mode) {
Mode::PgSQL => 'data || @data',
Mode::SQLite => 'json_patch(data, json(@data))',
Mode::PgSQL => 'data || :data',
Mode::SQLite => 'json_patch(data, json(:data))',
default => throw new DocumentException('Database mode not set; cannot make patch statement')
};
return "UPDATE $tableName SET data = $setValue WHERE $whereClause";

View File

@@ -20,7 +20,7 @@ class RemoveFields
*/
public static function byId(string $tableName, mixed $docId, array $fieldNames, ?PDO $pdo = null): void
{
$nameParams = Parameters::fieldNames('@name', $fieldNames);
$nameParams = Parameters::fieldNames(':name', $fieldNames);
Custom::nonQuery(Query\RemoveFields::byId($tableName, $nameParams),
array_merge(Parameters::id($docId), $nameParams), $pdo);
}
@@ -38,7 +38,7 @@ class RemoveFields
public static function byFields(string $tableName, array $fields, array $fieldNames, ?PDO $pdo = null,
string $conjunction = 'AND'): void
{
$nameParams = Parameters::fieldNames('@name', $fieldNames);
$nameParams = Parameters::fieldNames(':name', $fieldNames);
$namedFields = Parameters::nameFields($fields);
Custom::nonQuery(Query\RemoveFields::byFields($tableName, $namedFields, $nameParams, $conjunction),
Parameters::addFields($namedFields, $nameParams), $pdo);