diff --git a/src/Database/Connection.php b/src/Database/Connection.php index 1d86803f1..4d66a05a1 100644 --- a/src/Database/Connection.php +++ b/src/Database/Connection.php @@ -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 diff --git a/src/Database/Explorer.php b/src/Database/Explorer.php index 06481f8c3..12cbd3f25 100644 --- a/src/Database/Explorer.php +++ b/src/Database/Explorer.php @@ -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 diff --git a/src/Database/ResultSet.php b/src/Database/ResultSet.php index e79484118..6c8c1cfe2 100644 --- a/src/Database/ResultSet.php +++ b/src/Database/ResultSet.php @@ -10,6 +10,7 @@ namespace Nette\Database; use Nette; +use Nette\Utils\Arrays; use PDO; @@ -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(); @@ -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); } @@ -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; } @@ -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; } @@ -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); - } } diff --git a/tests/Database/Connection.fetch.phpt b/tests/Database/Connection.fetch.phpt index be755689c..745d76c80 100644 --- a/tests/Database/Connection.fetch.phpt +++ b/tests/Database/Connection.fetch.phpt @@ -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')); }); diff --git a/tests/Database/Explorer.fetch.phpt b/tests/Database/Explorer.fetch.phpt index 6763aa0a9..bc1024032 100644 --- a/tests/Database/Explorer.fetch.phpt +++ b/tests/Database/Explorer.fetch.phpt @@ -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')); });