Passed
Pull Request — dev (#109)
by Wilmer
03:08
created

CommandPDOPgsql::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 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 18
c 1
b 0
f 0
nc 9
nop 1
dl 0
loc 25
ccs 13
cts 16
cp 0.8125
crap 9.5338
rs 8.0555
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Pgsql\PDO;
6
7
use PDOException;
8
use Yiisoft\Db\Cache\QueryCache;
9
use Yiisoft\Db\Command\CommandPDO;
10
use Yiisoft\Db\Connection\ConnectionPDOInterface;
11
use Yiisoft\Db\Exception\ConvertException;
12
use Yiisoft\Db\Exception\Exception;
13
use Yiisoft\Db\Query\QueryBuilderInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Query\QueryBuilderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
final class CommandPDOPgsql extends CommandPDO
16
{
17 401
    public function __construct(private ConnectionPDOInterface $db, QueryCache $queryCache)
18
    {
19 401
        parent::__construct($queryCache);
20
    }
21
22
    /**
23
     * @inheritDoc
24
     */
25 1
    public function insertEx(string $table, array $columns): bool|array
26
    {
27 1
        $params = [];
28 1
        $sql = $this->queryBuilder()->insertEx($table, $columns, $params);
29
30 1
        $this->setSql($sql)->bindValues($params);
31 1
        $this->prepare(false);
32
33
        /** @var mixed */
34 1
        $queryOne = $this->queryOne();
35
36 1
        return is_array($queryOne) ? $queryOne : false;
37
    }
38
39 191
    public function queryBuilder(): QueryBuilderInterface
40
    {
41 191
        return $this->db->getQueryBuilder();
42
    }
43
44 182
    public function prepare(?bool $forRead = null): void
45
    {
46 182
        if (isset($this->pdoStatement)) {
47 6
            $this->bindPendingParams();
48
49 6
            return;
50
        }
51
52 182
        $sql = $this->getSql();
53
54 182
        if ($this->db->getTransaction()) {
55
            /** master is in a transaction. use the same connection. */
56 6
            $forRead = false;
57
        }
58
59 182
        if ($forRead || ($forRead === null && $this->db->getSchema()->isReadQuery($sql))) {
60 177
            $pdo = $this->db->getSlavePdo();
61
        } else {
62 65
            $pdo = $this->db->getMasterPdo();
63
        }
64
65
        try {
66 182
            $this->pdoStatement = $pdo?->prepare($sql);
67 182
            $this->bindPendingParams();
68
        } catch (PDOException $e) {
69
            $message = $e->getMessage() . "\nFailed to prepare SQL: $sql";
70
            /** @var array|null */
71
            $errorInfo = $e->errorInfo ?? null;
72
73
            throw new Exception($message, $errorInfo, $e);
74
        }
75
    }
76
77 174
    protected function getCacheKey(int $queryMode, string $rawSql): array
78
    {
79
        return [
80 174
            __CLASS__,
81
            $queryMode,
82 174
            $this->db->getDriver()->getDsn(),
83 174
            $this->db->getDriver()->getUsername(),
84
            $rawSql,
85
        ];
86
    }
87
88 181
    protected function internalExecute(?string $rawSql): void
89
    {
90 181
        $attempt = 0;
91
92 181
        while (true) {
93
            try {
94
                if (
95 181
                    ++$attempt === 1
96 181
                    && $this->isolationLevel !== null
97 181
                    && $this->db->getTransaction() === null
98
                ) {
99
                    $this->db->transaction(
100
                        fn (?string $rawSql): ?string => $this->internalExecute($rawSql),
101
                        $this->isolationLevel
102
                    );
103
                } else {
104 181
                    $this->pdoStatement?->execute();
105
                }
106 181
                break;
107 7
            } catch (\Exception $e) {
108 7
                $rawSql = $rawSql ?: $this->getRawSql();
109 7
                $e = (new ConvertException($e, $rawSql))->run();
110
111 7
                if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
112 7
                    throw $e;
113
                }
114
            }
115
        }
116
    }
117
}
118