ResultAdapter.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\DB;
  8. use Doctrine\DBAL\Result;
  9. use OCP\DB\IResult;
  10. use PDO;
  11. /**
  12. * Adapts DBAL 2.6 API for DBAL 3.x for backwards compatibility of a leaked type
  13. */
  14. class ResultAdapter implements IResult {
  15. /** @var Result */
  16. private $inner;
  17. public function __construct(Result $inner) {
  18. $this->inner = $inner;
  19. }
  20. public function closeCursor(): bool {
  21. $this->inner->free();
  22. return true;
  23. }
  24. public function fetch(int $fetchMode = PDO::FETCH_ASSOC) {
  25. return match ($fetchMode) {
  26. PDO::FETCH_ASSOC => $this->inner->fetchAssociative(),
  27. PDO::FETCH_NUM => $this->inner->fetchNumeric(),
  28. PDO::FETCH_COLUMN => $this->inner->fetchOne(),
  29. default => throw new \Exception('Fetch mode needs to be assoc, num or column.'),
  30. };
  31. }
  32. public function fetchAll(int $fetchMode = PDO::FETCH_ASSOC): array {
  33. return match ($fetchMode) {
  34. PDO::FETCH_ASSOC => $this->inner->fetchAllAssociative(),
  35. PDO::FETCH_NUM => $this->inner->fetchAllNumeric(),
  36. PDO::FETCH_COLUMN => $this->inner->fetchFirstColumn(),
  37. default => throw new \Exception('Fetch mode needs to be assoc, num or column.'),
  38. };
  39. }
  40. public function fetchColumn($columnIndex = 0) {
  41. return $this->inner->fetchOne();
  42. }
  43. public function fetchOne() {
  44. return $this->inner->fetchOne();
  45. }
  46. public function rowCount(): int {
  47. return $this->inner->rowCount();
  48. }
  49. }