1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Oracle; |
6
|
|
|
|
7
|
|
|
use PDO; |
8
|
|
|
use PDOException; |
9
|
|
|
use Throwable; |
10
|
|
|
use Yiisoft\Db\Driver\PDO\AbstractCommandPDO; |
11
|
|
|
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface; |
12
|
|
|
use Yiisoft\Db\Exception\ConvertException; |
13
|
|
|
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; |
14
|
|
|
use Yiisoft\Db\Schema\Schema; |
|
|
|
|
15
|
|
|
|
16
|
|
|
use function array_keys; |
17
|
|
|
use function count; |
18
|
|
|
use function implode; |
19
|
|
|
use function strlen; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Command represents an Oracle SQL statement to be executed against a database. |
23
|
|
|
*/ |
24
|
|
|
final class CommandPDO extends AbstractCommandPDO |
25
|
|
|
{ |
26
|
249 |
|
public function queryBuilder(): QueryBuilderInterface |
27
|
|
|
{ |
28
|
249 |
|
return $this->db->getQueryBuilder(); |
29
|
|
|
} |
30
|
|
|
|
31
|
4 |
|
public function insertWithReturningPks(string $table, array $columns): bool|array |
32
|
|
|
{ |
33
|
4 |
|
$params = []; |
34
|
4 |
|
$sql = $this->queryBuilder()->insert($table, $columns, $params); |
35
|
|
|
|
36
|
4 |
|
$tableSchema = $this->db->getSchema()->getTableSchema($table); |
37
|
|
|
|
38
|
4 |
|
$returnColumns = $tableSchema?->getPrimaryKey() ?? []; |
39
|
4 |
|
$columnSchemas = $tableSchema?->getColumns() ?? []; |
40
|
|
|
|
41
|
4 |
|
$returnParams = []; |
42
|
4 |
|
$returning = []; |
43
|
|
|
|
44
|
4 |
|
foreach ($returnColumns as $name) { |
45
|
|
|
/** @noRector \Rector\Php71\Rector\FuncCall\CountOnNullRector */ |
46
|
4 |
|
$phName = QueryBuilder::PARAM_PREFIX . (count($params) + count($returnParams)); |
47
|
|
|
|
48
|
4 |
|
$returnParams[$phName] = [ |
49
|
4 |
|
'column' => $name, |
50
|
4 |
|
'value' => '', |
51
|
4 |
|
]; |
52
|
|
|
|
53
|
4 |
|
if (!isset($columnSchemas[$name]) || $columnSchemas[$name]->getPhpType() !== Schema::PHP_TYPE_INTEGER) { |
54
|
1 |
|
$returnParams[$phName]['dataType'] = PDO::PARAM_STR; |
55
|
|
|
} else { |
56
|
3 |
|
$returnParams[$phName]['dataType'] = PDO::PARAM_INT; |
57
|
|
|
} |
58
|
|
|
|
59
|
4 |
|
$returnParams[$phName]['size'] = $columnSchemas[$name]->getSize() ?? -1; |
60
|
|
|
|
61
|
4 |
|
$returning[] = $this->db->getQuoter()->quoteColumnName($name); |
62
|
|
|
} |
63
|
|
|
|
64
|
4 |
|
$sql .= ' RETURNING ' . implode(', ', $returning) . ' INTO ' . implode(', ', array_keys($returnParams)); |
65
|
|
|
|
66
|
4 |
|
$this->setSql($sql)->bindValues($params); |
67
|
4 |
|
$this->prepare(false); |
68
|
|
|
|
69
|
|
|
/** @psalm-var array<string, array{column: string, value: mixed, dataType: int, size: int}> $returnParams */ |
70
|
4 |
|
foreach ($returnParams as $name => &$value) { |
71
|
4 |
|
$this->bindParam($name, $value['value'], $value['dataType'], $value['size']); |
72
|
|
|
} |
73
|
4 |
|
unset($value); |
74
|
|
|
|
75
|
4 |
|
if (!$this->execute()) { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
4 |
|
$result = []; |
80
|
|
|
|
81
|
4 |
|
foreach ($returnParams as $value) { |
82
|
|
|
/** @psalm-var mixed */ |
83
|
4 |
|
$result[$value['column']] = $value['value']; |
84
|
|
|
} |
85
|
|
|
|
86
|
4 |
|
return $result; |
87
|
|
|
} |
88
|
|
|
|
89
|
227 |
|
protected function bindPendingParams(): void |
90
|
|
|
{ |
91
|
227 |
|
$paramsPassedByReference = []; |
92
|
|
|
|
93
|
227 |
|
$params = $this->params; |
94
|
|
|
|
95
|
227 |
|
foreach ($params as $name => $value) { |
96
|
194 |
|
if (PDO::PARAM_STR === $value->getType()) { |
97
|
|
|
/** @var mixed */ |
98
|
191 |
|
$paramsPassedByReference[$name] = $value->getValue(); |
99
|
191 |
|
$this->pdoStatement?->bindParam( |
100
|
191 |
|
$name, |
101
|
191 |
|
$paramsPassedByReference[$name], |
102
|
191 |
|
$value->getType(), |
103
|
191 |
|
strlen((string) $value->getValue()) |
104
|
191 |
|
); |
105
|
|
|
} else { |
106
|
32 |
|
$this->pdoStatement?->bindValue($name, $value->getValue(), $value->getType()); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @psalm-suppress UnusedClosureParam |
113
|
|
|
* |
114
|
|
|
* @throws Throwable |
115
|
|
|
*/ |
116
|
226 |
|
protected function internalExecute(?string $rawSql): void |
117
|
|
|
{ |
118
|
226 |
|
$attempt = 0; |
119
|
|
|
|
120
|
226 |
|
while (true) { |
121
|
|
|
try { |
122
|
|
|
if ( |
123
|
226 |
|
++$attempt === 1 |
124
|
226 |
|
&& $this->isolationLevel !== null |
125
|
226 |
|
&& $this->db->getTransaction() === null |
126
|
|
|
) { |
127
|
1 |
|
$this->db->transaction( |
128
|
1 |
|
fn (ConnectionPDOInterface $db) => $this->internalExecute($rawSql), |
|
|
|
|
129
|
1 |
|
$this->isolationLevel |
130
|
1 |
|
); |
131
|
|
|
} else { |
132
|
226 |
|
$this->pdoStatement?->execute(); |
133
|
|
|
} |
134
|
226 |
|
break; |
135
|
7 |
|
} catch (PDOException $e) { |
136
|
7 |
|
$rawSql = $rawSql ?: $this->getRawSql(); |
137
|
7 |
|
$e = (new ConvertException($e, $rawSql))->run(); |
138
|
|
|
|
139
|
7 |
|
if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) { |
140
|
7 |
|
throw $e; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: