Test Failed
Pull Request — dev (#49)
by Def
16:58 queued 08:25
created

CommandPDOOracle   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Test Coverage

Coverage 89.36%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 83
c 2
b 0
f 0
dl 0
loc 149
ccs 42
cts 47
cp 0.8936
rs 10
wmc 29

7 Methods

Rating   Name   Duplication   Size   Complexity  
B insertEx() 0 50 7
A queryBuilder() 0 3 1
A __construct() 0 3 1
B prepare() 0 29 7
B internalExecute() 0 22 9
A getCacheKey() 0 9 1
A bindPendingParams() 0 15 3
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\QueryBuilder;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Query\QueryBuilder 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
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...
16
17
/**
18
 * Command represents an Oracle SQL statement to be executed against a database.
19
 */
20
final class CommandPDOOracle extends Command
21 329
{
22
    public function __construct(private ConnectionPDOInterface $db, QueryCache $queryCache)
23 329
    {
24
        parent::__construct($queryCache);
25
    }
26 167
27
    public function queryBuilder(): QueryBuilderInterface
28 167
    {
29
        return $this->db->getQueryBuilder();
30
    }
31 158
32
    public function insertEx(string $table, array $columns): bool|array
33 158
    {
34 4
        $params = [];
35
        $sql = $this->queryBuilder()->insertEx($table, $columns, $params);
36 4
37
        $tableSchema = $this->queryBuilder()->schema()->getTableSchema($table);
38
39 158
        $returnColumns = $tableSchema?->getPrimaryKey() ?? [];
40
        $columnSchemas = $tableSchema?->getColumns() ?? [];
41 158
42
        $returnParams = [];
43 5
        $returning = [];
44
        foreach ($returnColumns as $name) {
45
            $phName = QueryBuilder::PARAM_PREFIX . (count($params) + count($returnParams));
46 158
47 154
            $returnParams[$phName] = [
48
                'column' => $name,
49 56
                'value' => '',
50
            ];
51
52
            if (!isset($columnSchemas[$name]) || $columnSchemas[$name]->getPhpType() !== 'integer') {
53 158
                $returnParams[$phName]['dataType'] = PDO::PARAM_STR;
54 158
            } else {
55
                $returnParams[$phName]['dataType'] = PDO::PARAM_INT;
56
            }
57
58
            $returnParams[$phName]['size'] = $columnSchemas[$name]->getSize() ?? -1;
59
60
            $returning[] = $this->db->getQuoter()->quoteColumnName($name);
61
        }
62
63 158
        $sql .= ' RETURNING ' . implode(', ', $returning) . ' INTO ' . implode(', ', array_keys($returnParams));
64
65 158
        $this->setSql($sql)->bindValues($params);
66
        $this->prepare(false);
67 158
68 138
        foreach ($returnParams as $name => &$value) {
69 133
            $this->bindParam($name, $value['value'], $value['dataType'], $value['size']);
0 ignored issues
show
Bug introduced by
The method bindParam() does not exist on Yiisoft\Db\Oracle\PDO\CommandPDOOracle. ( Ignorable by Annotation )

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

69
            $this->/** @scrutinizer ignore-call */ 
70
                   bindParam($name, $value['value'], $value['dataType'], $value['size']);

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...
70 133
        }
71
72 133
        if (!$this->execute()) {
73 133
            return false;
74 133
        }
75
76
        $result = [];
77 22
        foreach ($returnParams as $value) {
78
            $result[$value['column']] = $value['value'];
79
        }
80
81
        return $result;
82 2
    }
83
84
    public function prepare(?bool $forRead = null): void
85
    {
86
        if (isset($this->pdoStatement)) {
87
            $this->bindPendingParams();
88 2
89 2
            return;
90
        }
91
92
        $sql = $this->getSql();
93
94 157
        if ($this->db->getTransaction()) {
95
            /** master is in a transaction. use the same connection. */
96 157
            $forRead = false;
97
        }
98 157
99
        if ($forRead || ($forRead === null && $this->db->getSchema()->isReadQuery($sql))) {
100
            $pdo = $this->db->getSlavePdo();
101 157
        } else {
102 157
            $pdo = $this->db->getMasterPdo();
103 157
        }
104
105
        try {
106
            $this->pdoStatement = $pdo->prepare($sql);
107 157
            $this->bindPendingParams();
108
        } catch (PDOException $e) {
109 157
            $message = $e->getMessage() . "\nFailed to prepare SQL: $sql";
110 6
            $errorInfo = $e->errorInfo ?? null;
111 6
112 6
            throw new Exception($message, $errorInfo, $e);
113
        }
114 6
    }
115 6
116
    protected function bindPendingParams(): void
117
    {
118
        $paramsPassedByReference = [];
119
120
        foreach ($this->params as $name => $value) {
121
            if (PDO::PARAM_STR === $value->getType()) {
122
                $paramsPassedByReference[$name] = $value->getValue();
123
                $this->pdoStatement?->bindParam(
124
                    $name,
125
                    $paramsPassedByReference[$name],
126
                    $value->getType(),
127
                    strlen($value->getValue())
128
                );
129
            } else {
130
                $this->pdoStatement?->bindValue($name, $value->getValue(), $value->getType());
131
            }
132
        }
133
    }
134
135
    protected function getCacheKey(string $method, ?int $fetchMode, string $rawSql): array
136
    {
137
        return [
138
            __CLASS__,
139
            $method,
140
            $fetchMode,
141
            $this->db->getDriver()->getDsn(),
142
            $this->db->getDriver()->getUsername(),
143
            $rawSql,
144
        ];
145
    }
146
147
    protected function internalExecute(?string $rawSql): void
148
    {
149
        $attempt = 0;
150
151
        while (true) {
152
            try {
153
                if (
154
                    ++$attempt === 1
155
                    && $this->isolationLevel !== null
156
                    && $this->db->getTransaction() === null
157
                ) {
158
                    $this->db->transaction(fn ($rawSql) => $this->internalExecute($rawSql), $this->isolationLevel);
159
                } else {
160
                    $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

160
                    $this->pdoStatement->/** @scrutinizer ignore-call */ 
161
                                         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...
161
                }
162
                break;
163
            } catch (PDOException $e) {
164
                $rawSql = $rawSql ?: $this->getRawSql();
165
                $e = (new ConvertException($e, $rawSql))->run();
166
167
                if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
168
                    throw $e;
169
                }
170
            }
171
        }
172
    }
173
}
174