Passed
Pull Request — dev (#46)
by Def
08:14
created

CommandPDOOracle::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 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 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\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
/**
16
 * Command represents an Oracle SQL statement to be executed against a database.
17
 */
18
final class CommandPDOOracle extends Command
19
{
20 329
    public function __construct(private ConnectionPDOInterface $db, QueryCache $queryCache)
21
    {
22 329
        parent::__construct($queryCache);
23
    }
24
25 167
    public function queryBuilder(): QueryBuilderInterface
26
    {
27 167
        return $this->db->getQueryBuilder();
28
    }
29
30 158
    public function prepare(?bool $forRead = null): void
31
    {
32 158
        if (isset($this->pdoStatement)) {
33 4
            $this->bindPendingParams();
34
35 4
            return;
36
        }
37
38 158
        $sql = $this->getSql();
39
40 158
        if ($this->db->getTransaction()) {
41
            /** master is in a transaction. use the same connection. */
42 5
            $forRead = false;
43
        }
44
45 158
        if ($forRead || ($forRead === null && $this->db->getSchema()->isReadQuery($sql))) {
46 154
            $pdo = $this->db->getSlavePdo();
47
        } else {
48 56
            $pdo = $this->db->getMasterPdo();
49
        }
50
51
        try {
52 158
            $this->pdoStatement = $pdo->prepare($sql);
53 158
            $this->bindPendingParams();
54
        } catch (PDOException $e) {
55
            $message = $e->getMessage() . "\nFailed to prepare SQL: $sql";
56
            $errorInfo = $e->errorInfo ?? null;
57
58
            throw new Exception($message, $errorInfo, $e);
59
        }
60
    }
61
62 158
    protected function bindPendingParams(): void
63
    {
64 158
        $paramsPassedByReference = [];
65
66 158
        foreach ($this->pendingParams as $name => $value) {
67 138
            if (PDO::PARAM_STR === $value[1]) {
68 133
                $paramsPassedByReference[$name] = $value[0];
69 133
                $this->pdoStatement?->bindParam(
70
                    $name,
71
                    $paramsPassedByReference[$name],
72 133
                    $value[1],
73 133
                    strlen($value[0])
74
                );
75
            } else {
76 22
                $this->pdoStatement?->bindValue($name, $value[0], $value[1]);
77
            }
78
        }
79
80 158
        $this->pendingParams = [];
81
    }
82
83 2
    protected function getCacheKey(string $method, ?int $fetchMode, string $rawSql): array
84
    {
85
        return [
86 2
            __CLASS__,
87
            $method,
88
            $fetchMode,
89 2
            $this->db->getDriver()->getDsn(),
90 2
            $this->db->getDriver()->getUsername(),
91
            $rawSql,
92
        ];
93
    }
94
95 157
    protected function internalExecute(?string $rawSql): void
96
    {
97 157
        $attempt = 0;
98
99 157
        while (true) {
100
            try {
101
                if (
102 157
                    ++$attempt === 1
103 157
                    && $this->isolationLevel !== null
104 157
                    && $this->db->getTransaction() === null
105
                ) {
106
                    $this->db->transaction(fn ($rawSql) => $this->internalExecute($rawSql), $this->isolationLevel);
107
                } else {
108 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

108
                    $this->pdoStatement->/** @scrutinizer ignore-call */ 
109
                                         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...
109
                }
110 157
                break;
111 6
            } catch (PDOException $e) {
112 6
                $rawSql = $rawSql ?: $this->getRawSql();
113 6
                $e = $this->db->getSchema()->convertException($e, $rawSql);
114
115 6
                if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
116 6
                    throw $e;
117
                }
118
            }
119
        }
120
    }
121
}
122