FetchUtils.php 1.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Driver;

/**
 * @internal
 */
final class FetchUtils
{
    /**
     * @return mixed|false
     *
15
     * @throws Exception
16
     */
17
    public static function fetchOne(Result $result)
18
    {
19
        $row = $result->fetchNumeric();
20 21 22 23 24 25 26 27 28 29 30

        if ($row === false) {
            return false;
        }

        return $row[0];
    }

    /**
     * @return array<int,array<int,mixed>>
     *
31
     * @throws Exception
32
     */
33
    public static function fetchAllNumeric(Result $result): array
34 35 36
    {
        $rows = [];

37
        while (($row = $result->fetchNumeric()) !== false) {
38 39 40 41 42 43 44 45 46
            $rows[] = $row;
        }

        return $rows;
    }

    /**
     * @return array<int,array<string,mixed>>
     *
47
     * @throws Exception
48
     */
49
    public static function fetchAllAssociative(Result $result): array
50 51 52
    {
        $rows = [];

53
        while (($row = $result->fetchAssociative()) !== false) {
54 55 56 57 58
            $rows[] = $row;
        }

        return $rows;
    }
59 60 61 62

    /**
     * @return array<int,mixed>
     *
63
     * @throws Exception
64
     */
65
    public static function fetchFirstColumn(Result $result): array
66 67 68
    {
        $rows = [];

69
        while (($row = $result->fetchOne()) !== false) {
70 71 72 73 74
            $rows[] = $row;
        }

        return $rows;
    }
75
}