Test Failed
Pull Request — master (#126)
by Wilmer
04:30
created

CommandPDO::internalExecute()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 14.8607

Importance

Changes 0
Metric Value
cc 9
eloc 18
nc 9
nop 1
dl 0
loc 25
ccs 7
cts 12
cp 0.5833
crap 14.8607
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql;
6
7
use PDOException;
8
use Yiisoft\Db\Driver\PDO\CommandPDO as AbstractCommandPDO;
9
use Yiisoft\Db\Exception\ConvertException;
10
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
11
12
use function is_array;
13
14
final class CommandPDO extends AbstractCommandPDO
15
{
16
    /**
17
     * @inheritDoc
18
     */
19
    public function insertEx(string $table, array $columns): bool|array
20 6
    {
21
        $params = [];
22 6
        $sql = $this->queryBuilder()->insertEx($table, $columns, $params);
23 6
24
        $this->setSql($sql)->bindValues($params);
25 6
        $this->prepare(false);
26 6
27
        /** @psalm-var array|bool $result */
28
        $result = $this->queryOne();
29 6
30
        return is_array($result) ? $result : false;
31 6
    }
32
33
    public function queryBuilder(): QueryBuilderInterface
34 232
    {
35
        return $this->db->getQueryBuilder();
36 232
    }
37
38
    protected function internalExecute(string|null $rawSql): void
39
    {
40
        $attempt = 0;
41
42
        while (true) {
43
            try {
44 221
                if (
45
                    ++$attempt === 1
46 221
                    && $this->isolationLevel !== null
47
                    && $this->db->getTransaction() === null
48 221
                ) {
49
                    $this->db->transaction(
50
                        fn (string $rawSql) => $this->internalExecute($rawSql),
51
                        $this->isolationLevel
52 221
                    );
53 221
                } else {
54
                    $this->pdoStatement?->execute();
55
                }
56
                break;
57
            } catch (PDOException $e) {
58
                $rawSql = $rawSql ?: $this->getRawSql();
59
                $e = (new ConvertException($e, $rawSql))->run();
60 221
61
                if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
62 220
                    throw $e;
63 6
                }
64 6
            }
65 6
        }
66
    }
67
}
68