Passed
Pull Request — master (#126)
by Wilmer
25:49 queued 21:45
created

BatchQueryResult   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 31
ccs 11
cts 11
cp 1
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A getRows() 0 19 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql;
6
7
use PDOException;
8
use Yiisoft\Db\Query\BatchQueryResult as BaseBatchQueryResult;
9
10
class BatchQueryResult extends BaseBatchQueryResult
11
{
12
    /**
13
     * @var int MSSQL error code for exception that is thrown when last batch is size less than specified batch size
14
     *
15
     * {@see https://github.com/yiisoft/yii2/issues/10023}
16
     */
17
    private int $mssqlNoMoreRowsErrorCode = -13;
18
19
    /**
20
     * Reads and collects rows for batch.
21
     */
22 3
    protected function getRows(): array
23
    {
24 3
        $rows = [];
25 3
        $count = 0;
26
27
        try {
28
            do {
29 3
                $this->dataReader?->next();
30
                /** @psalm-var array|bool $row */
31 3
                $row = $this->dataReader?->current();
32 3
            } while ($row && ($rows[] = $row) && ++$count < $this->batchSize);
33 3
        } catch (PDOException $e) {
34
            /** @psalm-var array{0:string, 1:int|null, 2:string} $errorInfo */
35 3
            $errorInfo = $e->errorInfo;
36 3
            $errorInfo[1] ??= null;
37 3
            $rows = $errorInfo[1] !== $this->mssqlNoMoreRowsErrorCode ? throw $e : [];
38
        }
39
40 3
        return $rows;
41
    }
42
}
43