diff --git a/src/Custom.php b/src/Custom.php index 24371b0..b3ce240 100644 --- a/src/Custom.php +++ b/src/Custom.php @@ -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 $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; + } } /**