Passed
Push — dev ( 43c131...7ed35a )
by Def
24:05 queued 21:31
created

CommandPDO::internalExecute()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 9.5338

Importance

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