WIP on connection management

This commit is contained in:
2024-06-04 19:55:22 -04:00
parent afc5d80095
commit c2dc111dce
2 changed files with 18 additions and 17 deletions

View File

@@ -13,16 +13,16 @@ use PDOStatement;
*
* @template TDoc The domain class for items returned by this list
*/
readonly class DocumentList
class DocumentList
{
/**
* Constructor
*
* @param PDO $pdo The database connection against which the query was opened
* @param PDOStatement $result The result of the query
* @param PDO|null $pdo The database connection against which the query was opened
* @param PDOStatement|null $result The result of the query
* @param Mapper<TDoc> $mapper The mapper to deserialize JSON
*/
private function __construct(private PDO $pdo, private PDOStatement $result, private Mapper $mapper) { }
private function __construct(private ?PDO $pdo, private ?PDOStatement $result, private Mapper $mapper) { }
/**
* Construct a new document list
@@ -35,12 +35,8 @@ readonly class DocumentList
*/
public static function create(string $query, array $parameters, Mapper $mapper): static
{
$pdo = Configuration::dbConn();
$stmt = Custom::runQuery($query, $parameters, $pdo);
if ($stmt->errorCode()) {
throw new DocumentException('Error retrieving data: ' . $stmt->errorCode());
}
return new static($pdo, $stmt, $mapper);
$pdo = Configuration::dbConn();
return new static($pdo, Custom::runQuery($query, $parameters, $pdo), $mapper);
}
/**
@@ -53,5 +49,7 @@ readonly class DocumentList
if ($this->result) {
while ($row = $this->result->fetch(PDO::FETCH_ASSOC)) yield $this->mapper->map($row);
}
$this->result = null;
$this->pdo = null;
}
}