Passed
Push — dev ( 58c565...d3c6f8 )
by Wilmer
32:05 queued 19:41
created

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