FetchUtils.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\DBAL\Driver;
  4. /**
  5. * @internal
  6. */
  7. final class FetchUtils
  8. {
  9. /**
  10. * @return mixed|false
  11. *
  12. * @throws Exception
  13. */
  14. public static function fetchOne(Result $result)
  15. {
  16. $row = $result->fetchNumeric();
  17. if ($row === false) {
  18. return false;
  19. }
  20. return $row[0];
  21. }
  22. /**
  23. * @return array<int,array<int,mixed>>
  24. *
  25. * @throws Exception
  26. */
  27. public static function fetchAllNumeric(Result $result): array
  28. {
  29. $rows = [];
  30. while (($row = $result->fetchNumeric()) !== false) {
  31. $rows[] = $row;
  32. }
  33. return $rows;
  34. }
  35. /**
  36. * @return array<int,array<string,mixed>>
  37. *
  38. * @throws Exception
  39. */
  40. public static function fetchAllAssociative(Result $result): array
  41. {
  42. $rows = [];
  43. while (($row = $result->fetchAssociative()) !== false) {
  44. $rows[] = $row;
  45. }
  46. return $rows;
  47. }
  48. /**
  49. * @return array<int,mixed>
  50. *
  51. * @throws Exception
  52. */
  53. public static function fetchFirstColumn(Result $result): array
  54. {
  55. $rows = [];
  56. while (($row = $result->fetchOne()) !== false) {
  57. $rows[] = $row;
  58. }
  59. return $rows;
  60. }
  61. }