|
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; |
|
|
|
|
|
|
15
|
|
|
use Yiisoft\Db\Query\QueryBuilderInterface; |
|
|
|
|
|
|
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']); |
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
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
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths