diff --git a/src/Database/Connection.php b/src/Database/Connection.php index 4d66a05a1..233c2d6c2 100644 --- a/src/Database/Connection.php +++ b/src/Database/Connection.php @@ -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(); } diff --git a/src/Database/Explorer.php b/src/Database/Explorer.php index 12cbd3f25..fe1a486c2 100644 --- a/src/Database/Explorer.php +++ b/src/Database/Explorer.php @@ -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(); } diff --git a/src/Database/ResultSet.php b/src/Database/ResultSet.php index 6c8c1cfe2..05a96ff06 100644 --- a/src/Database/ResultSet.php +++ b/src/Database/ResultSet.php @@ -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 { @@ -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. */ diff --git a/tests/Database/Connection.fetch.phpt b/tests/Database/Connection.fetch.phpt index 745d76c80..e4e09421f 100644 --- a/tests/Database/Connection.fetch.phpt +++ b/tests/Database/Connection.fetch.phpt @@ -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')); }); diff --git a/tests/Database/Explorer.fetch.phpt b/tests/Database/Explorer.fetch.phpt index bc1024032..8b07a8e1b 100644 --- a/tests/Database/Explorer.fetch.phpt +++ b/tests/Database/Explorer.fetch.phpt @@ -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')); }); diff --git a/tests/Database/ResultSet.fetchFields().phpt b/tests/Database/ResultSet.fetchList().phpt similarity index 85% rename from tests/Database/ResultSet.fetchFields().phpt rename to tests/Database/ResultSet.fetchList().phpt index 92457b389..f13e91b57 100644 --- a/tests/Database/ResultSet.fetchFields().phpt +++ b/tests/Database/ResultSet.fetchList().phpt @@ -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()); });