Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,18 @@ public function fetchAll(#[Language('SQL')] string $sql, #[Language('GenericSQL'
}


/**
* Executes SQL query and returns all rows as an array of arrays.
* Useful because the rows can be typed using @var.
* @param literal-string $sql
* @return list<array>
*/
public function fetchAllArray(#[Language('SQL')] string $sql, #[Language('GenericSQL')] mixed ...$params): array
{
return $this->query($sql, ...$params)->fetchAllArray();
}


/**
* Creates SQL literal value.
*/
Expand Down
11 changes: 11 additions & 0 deletions src/Database/Explorer.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,17 @@ public function fetchAll(#[Language('SQL')] string $sql, #[Language('GenericSQL'
}


/**
* Executes SQL query and returns all rows as an array of Row objects.
* @param literal-string $sql
* @return list<array>
*/
public function fetchAllArray(#[Language('SQL')] string $sql, #[Language('GenericSQL')] mixed ...$params): array
{
return $this->connection->query($sql, ...$params)->fetchAllArray();
}


/**
* Creates SQL literal value.
*/
Expand Down
21 changes: 21 additions & 0 deletions src/Database/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class ResultSet implements \Iterator, IRowContainer

/** @var list<Row> */
private array $rows;
/** @var list<array> */
private array $rowsArray;
private float $time;

/** @var array<string, string> column name => type */
Expand Down Expand Up @@ -287,4 +289,23 @@ public function fetchAll(): array
$this->rows ??= iterator_to_array($this, preserve_keys: false);
return $this->rows;
}


/**
* Returns all remaining rows as array of arrays.
* Useful because the rows can be typed using @var
* @return list<array>
*/
public function fetchAllArray(): array
{
if(!isset($this->rowsArray)){
$this->rowsArray = [];
while(true){
$data = $this->fetchAssoc();
if($data === null) break;
$this->rowsArray[] = $data;
}
}
return $this->rowsArray;
}
}
6 changes: 6 additions & 0 deletions tests/types/database-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ function testResultSetFetchAll(ResultSet $resultSet): void
}


function testResultSetFetchAllArray(ResultSet $resultSet): void
{
assertType('list<array>', $resultSet->fetchAllArray());
}


function testResultSetFetchPairs(ResultSet $resultSet): void
{
assertType('array<mixed>', $resultSet->fetchPairs());
Expand Down