Derive mode from DSN function

- Add headers in all files
- Minor field name changes
This commit is contained in:
2024-07-20 21:47:21 -04:00
parent 1a37b009ea
commit d8330d828a
81 changed files with 1053 additions and 551 deletions

View File

@@ -1,7 +1,15 @@
<?php declare(strict_types=1);
<?php
/**
* @author Daniel J. Summers <daniel@bitbadger.solutions>
* @license MIT
*/
declare(strict_types=1);
namespace BitBadger\PDODocument;
use Exception;
/**
* Functions to create parameters for queries
*/
@@ -57,7 +65,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 (empty($fields[$idx]->paramName)) $fields[$idx]->paramName = ":field$idx";
}
return $fields;
}
@@ -80,20 +88,18 @@ class Parameters
* @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
* @throws Exception 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 => "{" . 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');
}
$mode = Configuration::mode('generate field name parameters');
if ($mode === Mode::PgSQL) return [$paramName => "{" . implode(",", $fieldNames) . "}"];
// else SQLite
$it = [];
$idx = 0;
foreach ($fieldNames as $field) $it[$paramName . $idx++] = "$.$field";
return $it;
}
}