Completed
Pull Request — dev (#78)
by Def
28:49 queued 28:49
created

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