Passed
Push — dev ( cc6591...c9f34d )
by Wilmer
04:23 queued 23s
created

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