Use PDO singleton

This commit is contained in:
2024-06-07 20:57:12 -04:00
parent bcca9f5ace
commit d9ffc36fe6
10 changed files with 61 additions and 95 deletions

View File

@@ -16,14 +16,13 @@ class Custom
*
* @param string $query The query to be run
* @param array $parameters The parameters for the query
* @param PDO $pdo The database connection on which the query should be run
* @return PDOStatement The result of executing the query
* @throws DocumentException If the query execution is unsuccessful
*/
public static function runQuery(string $query, array $parameters, PDO $pdo): PDOStatement
public static function &runQuery(string $query, array $parameters): PDOStatement
{
$debug = defined('PDO_DOC_DEBUG_SQL');
$stmt = $pdo->prepare($query);
$stmt = Configuration::dbConn()->prepare($query);
foreach ($parameters as $key => $value) {
if ($debug) echo "<pre>Binding $value to $key\n</pre>";
$dataType = match (true) {
@@ -77,19 +76,16 @@ class Custom
* @param string $query The query to be executed (will have "LIMIT 1" appended)
* @param array $parameters Parameters to use in executing the query
* @param Mapper<TDoc> $mapper Mapper to deserialize the result
* @param PDO|null $pdo The database connection to use (optional; will obtain one if not provided)
* @return false|TDoc The item if it is found, false if not
* @throws DocumentException If any is encountered
*/
public static function single(string $query, array $parameters, Mapper $mapper, ?PDO $pdo = null): mixed
public static function single(string $query, array $parameters, Mapper $mapper): mixed
{
try {
$stmt = self::runQuery("$query LIMIT 1", $parameters,
is_null($pdo) ? $actualPDO = Configuration::dbConn() : $pdo);
$stmt = &self::runQuery("$query LIMIT 1", $parameters);
return ($first = $stmt->fetch(PDO::FETCH_ASSOC)) ? $mapper->map($first) : false;
} finally {
$stmt = null;
if (isset($actualPDO)) $actualPDO = null;
}
}
@@ -98,16 +94,14 @@ class Custom
*
* @param string $query The query to execute
* @param array $parameters Parameters to use in executing the query
* @param PDO|null $pdo The database connection to use (optional; will obtain one if not provided)
* @throws DocumentException If any is encountered
*/
public static function nonQuery(string $query, array $parameters, ?PDO $pdo = null): void
public static function nonQuery(string $query, array $parameters): void
{
try {
$stmt = self::runQuery($query, $parameters, is_null($pdo) ? $actualPDO = Configuration::dbConn() : $pdo);
$stmt = &self::runQuery($query, $parameters);
} finally {
$stmt = null;
if (isset($actualPDO)) $actualPDO = null;
}
}
@@ -118,18 +112,16 @@ class Custom
* @param string $query The query to retrieve the value
* @param array $parameters Parameters to use in executing the query
* @param Mapper<T> $mapper The mapper to obtain the result
* @param PDO|null $pdo The database connection to use (optional; will obtain one if not provided)
* @return mixed|false|T The scalar value if found, false if not
* @throws DocumentException If any is encountered
*/
public static function scalar(string $query, array $parameters, Mapper $mapper, ?PDO $pdo = null): mixed
{
try {
$stmt = self::runQuery($query, $parameters, is_null($pdo) ? $actualPDO = Configuration::dbConn() : $pdo);
$stmt = &self::runQuery($query, $parameters);
return ($first = $stmt->fetch(PDO::FETCH_NUM)) ? $mapper->map($first) : false;
} finally {
$stmt = null;
if (isset($actualPDO)) $actualPDO = null;
}
}
}