Passed
Branch dev (1bbda8)
by Wilmer
14:10 queued 05:58
created

CommandPDOOracle::internalExecute()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 9.0294

Importance

Changes 0
Metric Value
cc 9
eloc 16
nc 9
nop 1
dl 0
loc 22
ccs 13
cts 14
cp 0.9286
crap 9.0294
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Oracle\PDO;
6
7
use PDO;
8
use PDOException;
9
use Yiisoft\Db\Cache\QueryCache;
10
use Yiisoft\Db\Command\Command;
11
use Yiisoft\Db\Connection\ConnectionPDOInterface;
12
use Yiisoft\Db\Exception\ConvertException;
13
use Yiisoft\Db\Exception\Exception;
14
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...
15
16
/**
17
 * Command represents an Oracle SQL statement to be executed against a database.
18
 */
19
final class CommandPDOOracle extends Command
20
{
21 329
    public function __construct(private ConnectionPDOInterface $db, QueryCache $queryCache)
22
    {
23 329
        parent::__construct($queryCache);
24
    }
25
26 167
    public function queryBuilder(): QueryBuilderInterface
27
    {
28 167
        return $this->db->getQueryBuilder();
29
    }
30
31 158
    public function prepare(?bool $forRead = null): void
32
    {
33 158
        if (isset($this->pdoStatement)) {
34 4
            $this->bindPendingParams();
35
36 4
            return;
37
        }
38
39 158
        $sql = $this->getSql();
40
41 158
        if ($this->db->getTransaction()) {
42
            /** master is in a transaction. use the same connection. */
43 5
            $forRead = false;
44
        }
45
46 158
        if ($forRead || ($forRead === null && $this->db->getSchema()->isReadQuery($sql))) {
47 154
            $pdo = $this->db->getSlavePdo();
48
        } else {
49 56
            $pdo = $this->db->getMasterPdo();
50
        }
51
52
        try {
53 158
            $this->pdoStatement = $pdo->prepare($sql);
54 158
            $this->bindPendingParams();
55
        } catch (PDOException $e) {
56
            $message = $e->getMessage() . "\nFailed to prepare SQL: $sql";
57
            $errorInfo = $e->errorInfo ?? null;
58
59
            throw new Exception($message, $errorInfo, $e);
60
        }
61
    }
62
63 158
    protected function bindPendingParams(): void
64
    {
65 158
        $paramsPassedByReference = [];
66
67 158
        foreach ($this->params as $name => $value) {
68 138
            if (PDO::PARAM_STR === $value->getType()) {
69 133
                $paramsPassedByReference[$name] = $value->getValue();
70 133
                $this->pdoStatement?->bindParam(
71
                    $name,
72 133
                    $paramsPassedByReference[$name],
73 133
                    $value->getType(),
74 133
                    strlen($value->getValue())
75
                );
76
            } else {
77 22
                $this->pdoStatement?->bindValue($name, $value->getValue(), $value->getType());
78
            }
79
        }
80
    }
81
82 2
    protected function getCacheKey(string $method, ?int $fetchMode, string $rawSql): array
83
    {
84
        return [
85
            __CLASS__,
86
            $method,
87
            $fetchMode,
88 2
            $this->db->getDriver()->getDsn(),
89 2
            $this->db->getDriver()->getUsername(),
90
            $rawSql,
91
        ];
92
    }
93
94 157
    protected function internalExecute(?string $rawSql): void
95
    {
96 157
        $attempt = 0;
97
98 157
        while (true) {
99
            try {
100
                if (
101 157
                    ++$attempt === 1
102 157
                    && $this->isolationLevel !== null
103 157
                    && $this->db->getTransaction() === null
104
                ) {
105
                    $this->db->transaction(fn ($rawSql) => $this->internalExecute($rawSql), $this->isolationLevel);
106
                } else {
107 157
                    $this->pdoStatement->execute();
0 ignored issues
show
Bug introduced by
The method execute() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
                    $this->pdoStatement->/** @scrutinizer ignore-call */ 
108
                                         execute();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
                }
109 157
                break;
110 6
            } catch (PDOException $e) {
111 6
                $rawSql = $rawSql ?: $this->getRawSql();
112 6
                $e = (new ConvertException($e, $rawSql))->run();
113
114 6
                if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
115 6
                    throw $e;
116
                }
117
            }
118
        }
119
    }
120
}
121