Passed
Push — dev ( 260d54...749693 )
by Wilmer
19:22 queued 09:38
created

CommandPDOOracle::__construct()   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
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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\CommandPDO;
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
use function array_keys;
18
use function count;
19
use function implode;
20
use function strlen;
21
22
/**
23
 * Command represents an Oracle SQL statement to be executed against a database.
24
 */
25
final class CommandPDOOracle extends CommandPDO
26
{
27 331
    public function __construct(private ConnectionPDOInterface $db, QueryCache $queryCache)
28
    {
29 331
        parent::__construct($queryCache);
30
    }
31
32 169
    public function queryBuilder(): QueryBuilderInterface
33
    {
34 169
        return $this->db->getQueryBuilder();
35
    }
36
37 1
    public function insertEx(string $table, array $columns): bool|array
38
    {
39 1
        $params = [];
40 1
        $sql = $this->queryBuilder()->insertEx($table, $columns, $params);
41
42 1
        $tableSchema = $this->queryBuilder()->schema()->getTableSchema($table);
43
44 1
        $returnColumns = $tableSchema?->getPrimaryKey() ?? [];
45 1
        $columnSchemas = $tableSchema?->getColumns() ?? [];
46
47 1
        $returnParams = [];
48 1
        $returning = [];
49 1
        foreach ($returnColumns as $name) {
50 1
            $phName = QueryBuilder::PARAM_PREFIX . (count($params) + count($returnParams));
51
52 1
            $returnParams[$phName] = [
53
                'column' => $name,
54
                'value' => '',
55
            ];
56
57 1
            if (!isset($columnSchemas[$name]) || $columnSchemas[$name]->getPhpType() !== 'integer') {
58
                $returnParams[$phName]['dataType'] = PDO::PARAM_STR;
59
            } else {
60 1
                $returnParams[$phName]['dataType'] = PDO::PARAM_INT;
61
            }
62
63 1
            $returnParams[$phName]['size'] = $columnSchemas[$name]->getSize() ?? -1;
64
65 1
            $returning[] = $this->db->getQuoter()->quoteColumnName($name);
66
        }
67
68 1
        $sql .= ' RETURNING ' . implode(', ', $returning) . ' INTO ' . implode(', ', array_keys($returnParams));
69
70 1
        $this->setSql($sql)->bindValues($params);
71 1
        $this->prepare(false);
72
73
        /** @psalm-var array<string, array{column: string, value: mixed, dataType: int, size: int}> $returnParams */
74 1
        foreach ($returnParams as $name => &$value) {
75 1
            $this->bindParam($name, $value['value'], $value['dataType'], $value['size']);
76
        }
77
78 1
        if (!$this->execute()) {
79
            return false;
80
        }
81
82 1
        $result = [];
83
84 1
        foreach ($returnParams as $value) {
85
            /** @var mixed */
86 1
            $result[$value['column']] = $value['value'];
87
        }
88
89 1
        return $result;
90
    }
91
92 160
    public function prepare(?bool $forRead = null): void
93
    {
94 160
        if (isset($this->pdoStatement)) {
95 6
            $this->bindPendingParams();
96
97 6
            return;
98
        }
99
100 160
        $sql = $this->getSql();
101
102 160
        if ($this->db->getTransaction()) {
103
            /** master is in a transaction. use the same connection. */
104 5
            $forRead = false;
105
        }
106
107 160
        if ($forRead || ($forRead === null && $this->db->getSchema()->isReadQuery($sql))) {
108 156
            $pdo = $this->db->getSlavePdo();
109
        } else {
110 58
            $pdo = $this->db->getMasterPdo();
111
        }
112
113
        try {
114 160
            $this->pdoStatement = $pdo?->prepare($sql);
115 160
            $this->bindPendingParams();
116
        } catch (PDOException $e) {
117
            $message = $e->getMessage() . "\nFailed to prepare SQL: $sql";
118
            /** @var array|null */
119
            $errorInfo = $e->errorInfo ?? null;
120
121
            throw new Exception($message, $errorInfo, $e);
122
        }
123
    }
124
125 160
    protected function bindPendingParams(): void
126
    {
127 160
        $paramsPassedByReference = [];
128
129 160
        foreach ($this->params as $name => $value) {
130 140
            if (PDO::PARAM_STR === $value->getType()) {
131
                /** @var mixed */
132 135
                $paramsPassedByReference[$name] = $value->getValue();
133 135
                $this->pdoStatement?->bindParam(
134
                    $name,
135 135
                    $paramsPassedByReference[$name],
136 135
                    $value->getType(),
137 135
                    strlen((string) $value->getValue())
138
                );
139
            } else {
140 22
                $this->pdoStatement?->bindValue($name, $value->getValue(), $value->getType());
141
            }
142
        }
143
    }
144
145 153
    protected function getCacheKey(int $queryMode, string $rawSql): array
146
    {
147
        return [
148 153
            __CLASS__,
149
            $queryMode,
150 153
            $this->db->getDriver()->getDsn(),
151 153
            $this->db->getDriver()->getUsername(),
152
            $rawSql,
153
        ];
154
    }
155
156 159
    protected function internalExecute(?string $rawSql): void
157
    {
158 159
        $attempt = 0;
159
160 159
        while (true) {
161
            try {
162
                if (
163 159
                    ++$attempt === 1
164 159
                    && $this->isolationLevel !== null
165 159
                    && $this->db->getTransaction() === null
166
                ) {
167
                    $this->db->transaction(fn (string $rawSql) => $this->internalExecute($rawSql), $this->isolationLevel);
168
                } else {
169 159
                    $this->pdoStatement?->execute();
170
                }
171 159
                break;
172 6
            } catch (PDOException $e) {
173 6
                $rawSql = $rawSql ?: $this->getRawSql();
174 6
                $e = (new ConvertException($e, $rawSql))->run();
175
176 6
                if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) {
177 6
                    throw $e;
178
                }
179
            }
180
        }
181
    }
182
}
183