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