Do not construct a list for a single item query

This commit is contained in:
Daniel J. Summers 2024-06-04 20:18:38 -04:00
parent cecbb51414
commit 7390ae0f61

View File

@ -71,12 +71,20 @@ 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): mixed
public static function single(string $query, array $parameters, Mapper $mapper, ?PDO $pdo = null): mixed
{
return empty($results = self::array("$query LIMIT 1", $parameters, $mapper)) ? false : $results[0];
try {
$stmt = self::runQuery("$query LIMIT 1", $parameters,
is_null($pdo) ? $actualPDO = Configuration::dbConn() : $pdo);
return ($first = $stmt->fetch(PDO::FETCH_ASSOC)) ? $mapper->map($first) : false;
} finally {
$stmt = null;
if (isset($actualPDO)) $actualPDO = null;
}
}
/**