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\AbstractQueryBuilder; |
14
|
|
|
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; |
15
|
|
|
use Yiisoft\Db\Schema\SchemaInterface; |
16
|
|
|
|
17
|
|
|
use function array_keys; |
18
|
|
|
use function count; |
19
|
|
|
use function implode; |
20
|
|
|
use function strlen; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Implements a database command that can be executed against a PDO (PHP Data Object) database connection for Oracle |
24
|
|
|
* Server. |
25
|
|
|
*/ |
26
|
|
|
final class CommandPDO extends AbstractCommandPDO |
27
|
|
|
{ |
28
|
5 |
|
public function insertWithReturningPks(string $table, array $columns): bool|array |
29
|
|
|
{ |
30
|
5 |
|
$params = []; |
31
|
5 |
|
$sql = $this->getQueryBuilder()->insert($table, $columns, $params); |
32
|
|
|
|
33
|
5 |
|
$tableSchema = $this->db->getSchema()->getTableSchema($table); |
34
|
|
|
|
35
|
5 |
|
$returnColumns = $tableSchema?->getPrimaryKey() ?? []; |
36
|
5 |
|
$columnSchemas = $tableSchema?->getColumns() ?? []; |
37
|
|
|
|
38
|
5 |
|
$returnParams = []; |
39
|
5 |
|
$returning = []; |
40
|
|
|
|
41
|
5 |
|
foreach ($returnColumns as $name) { |
42
|
|
|
/** @noRector \Rector\Php71\Rector\FuncCall\CountOnNullRector */ |
43
|
5 |
|
$phName = AbstractQueryBuilder::PARAM_PREFIX . (count($params) + count($returnParams)); |
44
|
|
|
|
45
|
5 |
|
$returnParams[$phName] = [ |
46
|
5 |
|
'column' => $name, |
47
|
5 |
|
'value' => '', |
48
|
5 |
|
]; |
49
|
|
|
|
50
|
5 |
|
if (!isset($columnSchemas[$name]) || $columnSchemas[$name]->getPhpType() !== SchemaInterface::PHP_TYPE_INTEGER) { |
51
|
1 |
|
$returnParams[$phName]['dataType'] = PDO::PARAM_STR; |
52
|
|
|
} else { |
53
|
4 |
|
$returnParams[$phName]['dataType'] = PDO::PARAM_INT; |
54
|
|
|
} |
55
|
|
|
|
56
|
5 |
|
$returnParams[$phName]['size'] = $columnSchemas[$name]->getSize() ?? -1; |
57
|
|
|
|
58
|
5 |
|
$returning[] = $this->db->getQuoter()->quoteColumnName($name); |
59
|
|
|
} |
60
|
|
|
|
61
|
5 |
|
$sql .= ' RETURNING ' . implode(', ', $returning) . ' INTO ' . implode(', ', array_keys($returnParams)); |
62
|
|
|
|
63
|
5 |
|
$this->setSql($sql)->bindValues($params); |
64
|
5 |
|
$this->prepare(false); |
65
|
|
|
|
66
|
|
|
/** @psalm-var array<string, array{column: string, value: mixed, dataType: int, size: int}> $returnParams */ |
67
|
5 |
|
foreach ($returnParams as $name => &$value) { |
68
|
5 |
|
$this->bindParam($name, $value['value'], $value['dataType'], $value['size']); |
69
|
|
|
} |
70
|
|
|
|
71
|
5 |
|
unset($value); |
72
|
|
|
|
73
|
5 |
|
if (!$this->execute()) { |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
5 |
|
$result = []; |
78
|
|
|
|
79
|
5 |
|
foreach ($returnParams as $value) { |
80
|
|
|
/** @psalm-var mixed */ |
81
|
5 |
|
$result[$value['column']] = $value['value']; |
82
|
|
|
} |
83
|
|
|
|
84
|
5 |
|
return $result; |
85
|
|
|
} |
86
|
|
|
|
87
|
263 |
|
protected function getQueryBuilder(): QueryBuilderInterface |
88
|
|
|
{ |
89
|
263 |
|
return $this->db->getQueryBuilder(); |
90
|
|
|
} |
91
|
|
|
|
92
|
240 |
|
protected function bindPendingParams(): void |
93
|
|
|
{ |
94
|
240 |
|
$paramsPassedByReference = []; |
95
|
|
|
|
96
|
240 |
|
$params = $this->params; |
97
|
|
|
|
98
|
240 |
|
foreach ($params as $name => $value) { |
99
|
203 |
|
if (PDO::PARAM_STR === $value->getType()) { |
100
|
|
|
/** @var mixed */ |
101
|
200 |
|
$paramsPassedByReference[$name] = $value->getValue(); |
102
|
200 |
|
$this->pdoStatement?->bindParam( |
103
|
200 |
|
$name, |
104
|
200 |
|
$paramsPassedByReference[$name], |
105
|
200 |
|
$value->getType(), |
106
|
200 |
|
strlen((string) $value->getValue()) |
107
|
200 |
|
); |
108
|
|
|
} else { |
109
|
33 |
|
$this->pdoStatement?->bindValue($name, $value->getValue(), $value->getType()); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @psalm-suppress UnusedClosureParam |
116
|
|
|
* |
117
|
|
|
* @throws Throwable |
118
|
|
|
*/ |
119
|
239 |
|
protected function internalExecute(?string $rawSql): void |
120
|
|
|
{ |
121
|
239 |
|
$attempt = 0; |
122
|
|
|
|
123
|
239 |
|
while (true) { |
124
|
|
|
try { |
125
|
|
|
if ( |
126
|
239 |
|
++$attempt === 1 |
127
|
239 |
|
&& $this->isolationLevel !== null |
128
|
239 |
|
&& $this->db->getTransaction() === null |
129
|
|
|
) { |
130
|
1 |
|
$this->db->transaction( |
131
|
1 |
|
fn (ConnectionPDOInterface $db) => $this->internalExecute($rawSql), |
|
|
|
|
132
|
1 |
|
$this->isolationLevel |
133
|
1 |
|
); |
134
|
|
|
} else { |
135
|
239 |
|
$this->pdoStatement?->execute(); |
136
|
|
|
} |
137
|
239 |
|
break; |
138
|
7 |
|
} catch (PDOException $e) { |
139
|
7 |
|
$rawSql = $rawSql ?: $this->getRawSql(); |
140
|
7 |
|
$e = (new ConvertException($e, $rawSql))->run(); |
141
|
|
|
|
142
|
7 |
|
if ($this->retryHandler === null || !($this->retryHandler)($e, $attempt)) { |
143
|
7 |
|
throw $e; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.