Finish tests

- Add sub-doc handling for SQLite fields
- Add casting for PostgreSQL BT on numeric value
- Add hasItems() to DocumentList
- Fix SQL syntax problems exposed by tests
This commit is contained in:
2024-06-08 10:49:52 -04:00
parent a96fdf9d14
commit 2d8f8b6e87
21 changed files with 691 additions and 42 deletions

View File

@@ -15,13 +15,23 @@ use PDOStatement;
*/
class DocumentList
{
/** @var TDoc|null $_first The first item from the results */
private mixed $_first = null;
/**
* Constructor
*
* @param PDOStatement|null $result The result of the query
* @param Mapper<TDoc> $mapper The mapper to deserialize JSON
*/
private function __construct(private ?PDOStatement &$result, private readonly Mapper $mapper) { }
private function __construct(private ?PDOStatement &$result, private readonly Mapper $mapper)
{
if ($row = $this->result->fetch(PDO::FETCH_ASSOC)) {
$this->_first = $this->mapper->map($row);
} else {
$this->result = null;
}
}
/**
* Construct a new document list
@@ -45,9 +55,29 @@ class DocumentList
*/
public function items(): Generator
{
if ($this->result) {
while ($row = $this->result->fetch(PDO::FETCH_ASSOC)) yield $this->mapper->map($row);
if (!$this->result) return;
yield $this->_first;
while ($row = $this->result->fetch(PDO::FETCH_ASSOC)) {
yield $this->mapper->map($row);
}
$this->result = null;
}
/**
* Does this list have items remaining?
*
* @return bool True if there are items still to be retrieved from the list, false if not
*/
public function hasItems(): bool
{
return !is_null($this->result);
}
/**
* Ensure the statement is destroyed if the generator is not exhausted
*/
public function __destruct()
{
if (!is_null($this->result)) $this->result = null;
}
}