Passed
Push — dependabot/composer/yiisoft/al... ( 8bcce7 )
by
unknown
03:30
created

CommandPDO   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 80.77%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 54
ccs 21
cts 26
cp 0.8077
rs 10
c 0
b 0
f 0
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A queryBuilder() 0 3 1
A insertEx() 0 12 2
A schema() 0 3 1
B internalExecute() 0 25 9
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 209
    public function queryBuilder(): QueryBuilderInterface
33
    {
34 209
        return $this->db->getQueryBuilder();
35
    }
36
37
    public function schema(): SchemaInterface
38
    {
39
        return $this->db->getSchema();
40
    }
41
42 198
    protected function internalExecute(string|null $rawSql): void
43
    {
44 198
        $attempt = 0;
45
46 198
        while (true) {
47
            try {
48
                if (
49
                    ++$attempt === 1
50 198
                    && $this->isolationLevel !== null
51 198
                    && $this->db->getTransaction() === null
52
                ) {
53
                    $this->db->transaction(
54
                        fn (string|null $rawSql) => $this->internalExecute($rawSql),
55
                        $this->isolationLevel
56
                    );
57
                } else {
58 198
                    $this->pdoStatement?->execute();
59
                }
60 198
                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