1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Oracle; |
6
|
|
|
|
7
|
|
|
use PDO; |
8
|
|
|
use PDOException; |
9
|
|
|
use Yiisoft\Db\Driver\PDO\CommandPDO as AbstractCommandPDO; |
10
|
|
|
use Yiisoft\Db\Exception\ConvertException; |
11
|
|
|
use Yiisoft\Db\QueryBuilder\QueryBuilder; |
|
|
|
|
12
|
|
|
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; |
13
|
|
|
use Yiisoft\Db\Schema\Schema; |
|
|
|
|
14
|
|
|
use Yiisoft\Db\Schema\SchemaInterface; |
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
|
183 |
|
public function queryBuilder(): QueryBuilderInterface |
27
|
|
|
{ |
28
|
183 |
|
return $this->db->getQueryBuilder(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function schema(): SchemaInterface |
32
|
|
|
{ |
33
|
|
|
return $this->db->getSchema(); |
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
public function insertEx(string $table, array $columns): bool|array |
37
|
|
|
{ |
38
|
1 |
|
$params = []; |
39
|
1 |
|
$sql = $this->queryBuilder()->insertEx($table, $columns, $params); |
40
|
|
|
|
41
|
1 |
|
$tableSchema = $this->db->getSchema()->getTableSchema($table); |
42
|
|
|
|
43
|
1 |
|
$returnColumns = $tableSchema?->getPrimaryKey() ?? []; |
44
|
1 |
|
$columnSchemas = $tableSchema?->getColumns() ?? []; |
45
|
|
|
|
46
|
1 |
|
$returnParams = []; |
47
|
1 |
|
$returning = []; |
48
|
1 |
|
foreach ($returnColumns as $name) { |
49
|
1 |
|
$phName = QueryBuilder::PARAM_PREFIX . (count($params) + count($returnParams)); |
50
|
|
|
|
51
|
1 |
|
$returnParams[$phName] = [ |
52
|
|
|
'column' => $name, |
53
|
|
|
'value' => '', |
54
|
|
|
]; |
55
|
|
|
|
56
|
1 |
|
if (!isset($columnSchemas[$name]) || $columnSchemas[$name]->getPhpType() !== Schema::PHP_TYPE_INTEGER) { |
57
|
|
|
$returnParams[$phName]['dataType'] = PDO::PARAM_STR; |
58
|
|
|
} else { |
59
|
1 |
|
$returnParams[$phName]['dataType'] = PDO::PARAM_INT; |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
$returnParams[$phName]['size'] = $columnSchemas[$name]->getSize() ?? -1; |
63
|
|
|
|
64
|
1 |
|
$returning[] = $this->db->getQuoter()->quoteColumnName($name); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
$sql .= ' RETURNING ' . implode(', ', $returning) . ' INTO ' . implode(', ', array_keys($returnParams)); |
68
|
|
|
|
69
|
1 |
|
$this->setSql($sql)->bindValues($params); |
70
|
1 |
|
$this->prepare(false); |
71
|
|
|
|
72
|
|
|
/** @psalm-var array<string, array{column: string, value: mixed, dataType: int, size: int}> $returnParams */ |
73
|
1 |
|
foreach ($returnParams as $name => &$value) { |
74
|
1 |
|
$this->bindParam($name, $value['value'], $value['dataType'], $value['size']); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
if (!$this->execute()) { |
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
$result = []; |
82
|
|
|
|
83
|
1 |
|
foreach ($returnParams as $value) { |
84
|
|
|
/** @var mixed */ |
85
|
1 |
|
$result[$value['column']] = $value['value']; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
return $result; |
89
|
|
|
} |
90
|
|
|
|
91
|
173 |
|
protected function bindPendingParams(): void |
92
|
|
|
{ |
93
|
173 |
|
$paramsPassedByReference = []; |
94
|
|
|
|
95
|
173 |
|
$params = $this->params; |
96
|
|
|
|
97
|
173 |
|
foreach ($params as $name => $value) { |
98
|
150 |
|
if (PDO::PARAM_STR === $value->getType()) { |
99
|
|
|
/** @var mixed */ |
100
|
144 |
|
$paramsPassedByReference[$name] = $value->getValue(); |
101
|
144 |
|
$this->pdoStatement?->bindParam( |
102
|
|
|
$name, |
103
|
144 |
|
$paramsPassedByReference[$name], |
104
|
144 |
|
$value->getType(), |
105
|
144 |
|
strlen((string) $value->getValue()) |
106
|
|
|
); |
107
|
|
|
} else { |
108
|
24 |
|
$this->pdoStatement?->bindValue($name, $value->getValue(), $value->getType()); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
172 |
|
protected function internalExecute(?string $rawSql): void |
114
|
|
|
{ |
115
|
172 |
|
$attempt = 0; |
116
|
|
|
|
117
|
172 |
|
while (true) { |
118
|
|
|
try { |
119
|
|
|
if ( |
120
|
172 |
|
++$attempt === 1 |
121
|
172 |
|
&& $this->isolationLevel !== null |
122
|
172 |
|
&& $this->db->getTransaction() === null |
123
|
|
|
) { |
124
|
|
|
$this->db->transaction(fn (string $rawSql) => $this->internalExecute($rawSql), $this->isolationLevel); |
125
|
|
|
} else { |
126
|
172 |
|
$this->pdoStatement?->execute(); |
127
|
|
|
} |
128
|
172 |
|
break; |
129
|
6 |
|
} catch (PDOException $e) { |
130
|
6 |
|
$rawSql = $rawSql ?: $this->getRawSql(); |
131
|
6 |
|
$e = (new ConvertException($e, $rawSql))->run(); |
132
|
|
|
|
133
|
6 |
|
if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) { |
134
|
6 |
|
throw $e; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
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: