Passed
Pull Request — dev (#98)
by Wilmer
43:38 queued 28:30
created

CommandPDOMysql::queryBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mysql\PDO;
6
7
use PDOException;
8
use Yiisoft\Db\Cache\QueryCache;
9
use Yiisoft\Db\Command\Command;
10
use Yiisoft\Db\Connection\ConnectionPDOInterface;
11
use Yiisoft\Db\Exception\Exception;
12
use Yiisoft\Db\Exception\InvalidConfigException;
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 CommandPDOMysql extends Command
16
{
17 342
    public function __construct(private ConnectionPDOInterface $db, QueryCache $queryCache)
18
    {
19 342
        parent::__construct($queryCache);
20
    }
21
22 171
    public function queryBuilder(): QueryBuilderInterface
23
    {
24 171
        return $this->db->getQueryBuilder();
25
    }
26
27
    /**
28
     * @throws Exception|InvalidConfigException|PDOException
29
     */
30 162
    public function prepare(?bool $forRead = null): void
31
    {
32 162
        if (isset($this->pdoStatement)) {
33 5
            $this->bindPendingParams();
34
35 5
            return;
36
        }
37
38 162
        $sql = $this->getSql() ?? '';
39
40 162
        if ($this->db->getTransaction()) {
41
            /** master is in a transaction. use the same connection. */
42 5
            $forRead = false;
43
        }
44
45 162
        if ($forRead || ($forRead === null && $this->db->getSchema()->isReadQuery($sql))) {
46 156
            $pdo = $this->db->getSlavePdo();
47
        } else {
48 59
            $pdo = $this->db->getMasterPdo();
49
        }
50
51
        try {
52 162
            $this->pdoStatement = $pdo?->prepare($sql);
53 162
            $this->bindPendingParams();
54
        } catch (PDOException $e) {
55
            $message = $e->getMessage() . "\nFailed to prepare SQL: $sql";
56
            /** @var array|null */
57
            $errorInfo = $e->errorInfo ?? null;
58
59
            throw new Exception($message, $errorInfo, $e);
60
        }
61
    }
62
63 2
    protected function getCacheKey(string $method, ?int $fetchMode, string $rawSql): array
64
    {
65
        return [
66
            __CLASS__,
67
            $method,
68
            $fetchMode,
69 2
            $this->db->getDriver()->getDsn(),
70 2
            $this->db->getDriver()->getUsername(),
71
            $rawSql,
72
        ];
73
    }
74
75 161
    protected function internalExecute(?string $rawSql): void
76
    {
77 161
        $attempt = 0;
78
79 161
        while (true) {
80
            try {
81
                if (
82 161
                    ++$attempt === 1
83 161
                    && $this->isolationLevel !== null
84 161
                    && $this->db->getTransaction() === null
85
                ) {
86
                    $this->db->transaction(fn (?string $rawSql) => $this->internalExecute($rawSql), $this->isolationLevel);
87
                } else {
88 161
                    $this->pdoStatement?->execute();
89
                }
90 158
                break;
91 23
            } catch (PDOException $e) {
92 23
                $rawSql = $rawSql ?: $this->getRawSql();
93 23
                $e = $this->db->getSchema()->convertException($e, $rawSql);
94
95 23
                if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
96 23
                    throw $e;
97
                }
98
            }
99
        }
100
    }
101
}
102