Skip to content

Commit

Permalink
ResultSet: added fetchList() as alias for fetchFields() & shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 29, 2024
1 parent 881d715 commit af0f6f9
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 13 deletions.
14 changes: 12 additions & 2 deletions src/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,22 @@ public function fetchField(#[Language('SQL')] string $sql, #[Language('GenericSQ


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


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


Expand Down
14 changes: 12 additions & 2 deletions src/Database/Explorer.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,22 @@ public function fetchField(#[Language('SQL')] string $sql, #[Language('GenericSQ


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


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


Expand Down
15 changes: 12 additions & 3 deletions src/Database/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public function fetch(): ?Row


/**
* Fetches single field.
* Returns the first field of the next row or null if there are no more rows.
*/
public function fetchField(): mixed
{
Expand All @@ -231,15 +231,24 @@ public function fetchField(): mixed


/**
* Fetches array of fields.
* Returns the next row as indexes array or null if there are no more rows.
*/
public function fetchFields(): ?array
public function fetchList(): ?array
{
$row = $this->fetchAssoc();
return $row ? array_values($row) : null;
}


/**
* Alias for fetchList().
*/
public function fetchFields(): ?array
{
return $this->fetchList();
}


/**
* Fetches all rows as associative array.
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/Connection.fetch.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ test('fetchField', function () use ($connection) {
});


test('fetchFields', function () use ($connection) {
Assert::same([11, 'Jakub Vrana'], $connection->fetchFields('SELECT id, name FROM author ORDER BY id'));
test('fetchList', function () use ($connection) {
Assert::same([11, 'Jakub Vrana'], $connection->fetchList('SELECT id, name FROM author ORDER BY id'));
});


Expand Down
4 changes: 2 additions & 2 deletions tests/Database/Explorer.fetch.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ test('fetchField', function () use ($explorer) {
});


test('fetchFields', function () use ($explorer) {
Assert::same([11, 'Jakub Vrana'], $explorer->fetchFields('SELECT id, name FROM author ORDER BY id'));
test('fetchList', function () use ($explorer) {
Assert::same([11, 'Jakub Vrana'], $explorer->fetchList('SELECT id, name FROM author ORDER BY id'));
});


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName
test('', function () use ($connection) {
$res = $connection->query('SELECT name, id FROM author ORDER BY id');

Assert::same(['Jakub Vrana', 11], $res->fetchFields());
Assert::same(['Jakub Vrana', 11], $res->fetchList());
});


test('', function () use ($connection) {
$res = $connection->query('SELECT id FROM author WHERE id = ?', 666);

Assert::null($res->fetchFields());
Assert::null($res->fetchList());
});

0 comments on commit af0f6f9

Please sign in to comment.