PartitionedResult.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\DB\QueryBuilder\Partitioned;
  8. use OC\DB\ArrayResult;
  9. use OCP\DB\IResult;
  10. use PDO;
  11. /**
  12. * Combine the results of multiple join parts into a single result
  13. */
  14. class PartitionedResult extends ArrayResult {
  15. private bool $fetched = false;
  16. /**
  17. * @param PartitionQuery[] $splitOfParts
  18. * @param IResult $result
  19. */
  20. public function __construct(
  21. private array $splitOfParts,
  22. private IResult $result,
  23. ) {
  24. parent::__construct([]);
  25. }
  26. public function closeCursor(): bool {
  27. return $this->result->closeCursor();
  28. }
  29. public function fetch(int $fetchMode = PDO::FETCH_ASSOC) {
  30. $this->fetchRows();
  31. return parent::fetch($fetchMode);
  32. }
  33. public function fetchAll(int $fetchMode = PDO::FETCH_ASSOC): array {
  34. $this->fetchRows();
  35. return parent::fetchAll($fetchMode);
  36. }
  37. public function rowCount(): int {
  38. $this->fetchRows();
  39. return parent::rowCount();
  40. }
  41. private function fetchRows(): void {
  42. if (!$this->fetched) {
  43. $this->fetched = true;
  44. $this->rows = $this->result->fetchAll();
  45. foreach ($this->splitOfParts as $part) {
  46. $this->rows = $part->mergeWith($this->rows);
  47. }
  48. $this->count = count($this->rows);
  49. }
  50. }
  51. }