Skip to content

Commit

Permalink
ResultSet::fetchAssoc() returns associative array by default, added s…
Browse files Browse the repository at this point in the history
…hortcuts
  • Loading branch information
dg committed Aug 29, 2024
1 parent 67feacd commit 881d715
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 21 deletions.
10 changes: 10 additions & 0 deletions src/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,16 @@ public function fetch(#[Language('SQL')] string $sql, #[Language('GenericSQL')]
}


/**
* Shortcut for query()->fetchAssoc()
* @param literal-string $sql
*/
public function fetchAssoc(#[Language('SQL')] string $sql, #[Language('GenericSQL')] ...$params): ?array
{
return $this->query($sql, ...$params)->fetchAssoc();
}


/**
* Shortcut for query()->fetchField()
* @param literal-string $sql
Expand Down
10 changes: 10 additions & 0 deletions src/Database/Explorer.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ public function fetch(#[Language('SQL')] string $sql, #[Language('GenericSQL')]
}


/**
* Shortcut for query()->fetchAssoc()
* @param literal-string $sql
*/
public function fetchAssoc(#[Language('SQL')] string $sql, #[Language('GenericSQL')] ...$params): ?array
{
return $this->connection->query($sql, ...$params)->fetchAssoc();
}


/**
* Shortcut for query()->fetchField()
* @param literal-string $sql
Expand Down
48 changes: 27 additions & 21 deletions src/Database/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Nette\Database;

use Nette;
use Nette\Utils\Arrays;
use PDO;


Expand Down Expand Up @@ -178,11 +179,18 @@ public function valid(): bool
}


/********************* fetch ****************d*g**/


/**
* Fetches single row object.
* Returns the next row as an associative array or null if there are no more rows.
*/
public function fetch(): ?Row
public function fetchAssoc(?string $path = null): ?array
{
if ($path !== null) {
return Arrays::associate($this->fetchAll(), $path);
}

$data = $this->pdoStatement ? $this->pdoStatement->fetch() : null;
if (!$data) {
$this->pdoStatement->closeCursor();
Expand All @@ -193,15 +201,22 @@ public function fetch(): ?Row
trigger_error("Found duplicate columns in database result set: $duplicates.");
}

$row = new Row;
foreach ($this->normalizeRow($data) as $key => $value) {
if ($key !== '') {
$row->$key = $value;
}
return $this->normalizeRow($data);
}


/**
* Returns the next row as an object Row or null if there are no more rows.
*/
public function fetch(): ?Row
{
$data = $this->fetchAssoc();
if ($data === null) {
return null;
}

$this->lastRowKey++;
return $this->lastRow = $row;
return $this->lastRow = Arrays::toObject($data, new Row);
}


Expand All @@ -210,8 +225,8 @@ public function fetch(): ?Row
*/
public function fetchField(): mixed
{
$row = $this->fetch();
return $row ? $row[0] : null;
$row = $this->fetchAssoc();
return $row ? reset($row) : null;
}


Expand All @@ -220,8 +235,8 @@ public function fetchField(): mixed
*/
public function fetchFields(): ?array
{
$row = $this->fetch();
return $row ? array_values((array) $row) : null;
$row = $this->fetchAssoc();
return $row ? array_values($row) : null;
}


Expand All @@ -243,13 +258,4 @@ public function fetchAll(): array
$this->rows ??= iterator_to_array($this);
return $this->rows;
}


/**
* Fetches all rows and returns associative tree.
*/
public function fetchAssoc(string $path): array
{
return Nette\Utils\Arrays::associate($this->fetchAll(), $path);
}
}
9 changes: 9 additions & 0 deletions tests/Database/Connection.fetch.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ test('fetch', function () use ($connection) {
});


test('fetchAssoc', function () use ($connection) {
$row = $connection->fetchAssoc('SELECT name, id FROM author WHERE id = ?', 11);
Assert::same([
'name' => 'Jakub Vrana',
'id' => 11,
], $row);
});


test('fetchField', function () use ($connection) {
Assert::same('Jakub Vrana', $connection->fetchField('SELECT name FROM author ORDER BY id'));
});
Expand Down
9 changes: 9 additions & 0 deletions tests/Database/Explorer.fetch.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ test('fetch', function () use ($explorer) {
});


test('fetchAssoc', function () use ($explorer) {
$row = $explorer->fetchAssoc('SELECT name, id FROM author WHERE id = ?', 11);
Assert::same([
'name' => 'Jakub Vrana',
'id' => 11,
], $row);
});


test('fetchField', function () use ($explorer) {
Assert::same('Jakub Vrana', $explorer->fetchField('SELECT name FROM author ORDER BY id'));
});
Expand Down

0 comments on commit 881d715

Please sign in to comment.