Test Failed
Pull Request — dev (#124)
by Wilmer
04:00 queued 01:28
created

CommandPDOPgsql   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 88%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 27
c 4
b 0
f 0
dl 0
loc 54
ccs 22
cts 25
cp 0.88
rs 10
wmc 13

4 Methods

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