|
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\CommandPDO as AbstractCommandPDO; |
|
11
|
|
|
use Yiisoft\Db\Driver\PDO\ConnectionPDOInterface; |
|
12
|
|
|
use Yiisoft\Db\Exception\ConvertException; |
|
13
|
|
|
use Yiisoft\Db\QueryBuilder\QueryBuilder; |
|
|
|
|
|
|
14
|
|
|
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; |
|
15
|
|
|
use Yiisoft\Db\Schema\Schema; |
|
|
|
|
|
|
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 CommandPDO extends AbstractCommandPDO |
|
26
|
|
|
{ |
|
27
|
240 |
|
public function queryBuilder(): QueryBuilderInterface |
|
28
|
|
|
{ |
|
29
|
240 |
|
return $this->db->getQueryBuilder(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
3 |
|
public function insertEx(string $table, array $columns): bool|array |
|
33
|
|
|
{ |
|
34
|
3 |
|
$params = []; |
|
35
|
3 |
|
$sql = $this->queryBuilder()->insert($table, $columns, $params); |
|
36
|
|
|
|
|
37
|
3 |
|
$tableSchema = $this->db->getSchema()->getTableSchema($table); |
|
38
|
|
|
|
|
39
|
3 |
|
$returnColumns = $tableSchema?->getPrimaryKey() ?? []; |
|
40
|
3 |
|
$columnSchemas = $tableSchema?->getColumns() ?? []; |
|
41
|
|
|
|
|
42
|
3 |
|
$returnParams = []; |
|
43
|
3 |
|
$returning = []; |
|
44
|
|
|
|
|
45
|
3 |
|
foreach ($returnColumns as $name) { |
|
46
|
|
|
/** @noRector \Rector\Php71\Rector\FuncCall\CountOnNullRector */ |
|
47
|
3 |
|
$phName = QueryBuilder::PARAM_PREFIX . (count($params) + count($returnParams)); |
|
48
|
|
|
|
|
49
|
3 |
|
$returnParams[$phName] = [ |
|
50
|
3 |
|
'column' => $name, |
|
51
|
3 |
|
'value' => '', |
|
52
|
3 |
|
]; |
|
53
|
|
|
|
|
54
|
3 |
|
if (!isset($columnSchemas[$name]) || $columnSchemas[$name]->getPhpType() !== Schema::PHP_TYPE_INTEGER) { |
|
55
|
1 |
|
$returnParams[$phName]['dataType'] = PDO::PARAM_STR; |
|
56
|
|
|
} else { |
|
57
|
2 |
|
$returnParams[$phName]['dataType'] = PDO::PARAM_INT; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
3 |
|
$returnParams[$phName]['size'] = $columnSchemas[$name]->getSize() ?? -1; |
|
61
|
|
|
|
|
62
|
3 |
|
$returning[] = $this->db->getQuoter()->quoteColumnName($name); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
3 |
|
$sql .= ' RETURNING ' . implode(', ', $returning) . ' INTO ' . implode(', ', array_keys($returnParams)); |
|
66
|
|
|
|
|
67
|
3 |
|
$this->setSql($sql)->bindValues($params); |
|
68
|
3 |
|
$this->prepare(false); |
|
69
|
|
|
|
|
70
|
|
|
/** @psalm-var array<string, array{column: string, value: mixed, dataType: int, size: int}> $returnParams */ |
|
71
|
3 |
|
foreach ($returnParams as $name => &$value) { |
|
72
|
3 |
|
$this->bindParam($name, $value['value'], $value['dataType'], $value['size']); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
3 |
|
if (!$this->execute()) { |
|
76
|
|
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
3 |
|
$result = []; |
|
80
|
|
|
|
|
81
|
3 |
|
foreach ($returnParams as $value) { |
|
82
|
|
|
/** @psalm-var mixed */ |
|
83
|
3 |
|
$result[$value['column']] = $value['value']; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
3 |
|
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
|
198 |
|
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
|
28 |
|
$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
|
8 |
|
} catch (PDOException $e) { |
|
136
|
8 |
|
$rawSql = $rawSql ?: $this->getRawSql(); |
|
137
|
8 |
|
$e = (new ConvertException($e, $rawSql))->run(); |
|
138
|
|
|
|
|
139
|
8 |
|
if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) { |
|
140
|
|
|
throw $e; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: