1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Oracle; |
6
|
|
|
|
7
|
|
|
use JsonException; |
8
|
|
|
use Yiisoft\Db\Exception\Exception; |
9
|
|
|
use Yiisoft\Db\Exception\InvalidArgumentException; |
10
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
11
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
12
|
|
|
use Yiisoft\Db\Expression\Expression; |
13
|
|
|
use Yiisoft\Db\Expression\ExpressionInterface; |
14
|
|
|
use Yiisoft\Db\Query\QueryInterface; |
15
|
|
|
use Yiisoft\Db\QueryBuilder\AbstractDMLQueryBuilder; |
16
|
|
|
|
17
|
|
|
use function array_map; |
18
|
|
|
use function implode; |
19
|
|
|
use function count; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Implements a DML (Data Manipulation Language) SQL statements for Oracle Server. |
23
|
|
|
*/ |
24
|
|
|
final class DMLQueryBuilder extends AbstractDMLQueryBuilder |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @throws Exception |
28
|
|
|
* @throws InvalidArgumentException |
29
|
|
|
* @throws InvalidConfigException |
30
|
|
|
* @throws NotSupportedException |
31
|
|
|
*/ |
32
|
22 |
|
public function batchInsert(string $table, array $columns, iterable $rows, array &$params = []): string |
33
|
|
|
{ |
34
|
22 |
|
if (empty($rows)) { |
35
|
1 |
|
return ''; |
36
|
|
|
} |
37
|
|
|
|
38
|
21 |
|
$columnSchemas = $this->schema->getTableSchema($table)?->getColumns() ?? []; |
39
|
21 |
|
$columns = $this->extractColumnNames($columns, $columnSchemas, $rows); |
|
|
|
|
40
|
21 |
|
$values = $this->prepareBatchInsertValues($columns, $columnSchemas, $rows, $params); |
|
|
|
|
41
|
21 |
|
|
42
|
21 |
|
if (empty($values)) { |
43
|
|
|
return ''; |
44
|
21 |
|
} |
45
|
20 |
|
|
46
|
20 |
|
$tableAndColumns = ' INTO ' . $this->quoter->quoteTableName($table); |
47
|
|
|
|
48
|
20 |
|
if (count($columns) > 0) { |
49
|
|
|
$quotedColumnNames = array_map( |
50
|
20 |
|
[$this->quoter, 'quoteColumnName'], |
51
|
|
|
$columns, |
52
|
20 |
|
); |
53
|
19 |
|
|
54
|
|
|
$tableAndColumns .= ' (' . implode(', ', $quotedColumnNames) . ') VALUES '; |
55
|
|
|
} |
56
|
20 |
|
|
57
|
3 |
|
return 'INSERT ALL ' . $tableAndColumns . implode($tableAndColumns, $values) . ' SELECT 1 FROM SYS.DUAL'; |
58
|
|
|
} |
59
|
20 |
|
|
60
|
|
|
/** |
61
|
|
|
* @throws Exception |
62
|
20 |
|
* @throws NotSupportedException |
63
|
|
|
*/ |
64
|
|
|
public function insertWithReturningPks(string $table, QueryInterface|array $columns, array &$params = []): string |
65
|
20 |
|
{ |
66
|
|
|
throw new NotSupportedException(__METHOD__ . ' is not supported by Oracle.'); |
67
|
|
|
} |
68
|
21 |
|
|
69
|
1 |
|
/** |
70
|
|
|
* @link https://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_9016.htm#SQLRF01606 |
71
|
|
|
* |
72
|
20 |
|
* @throws Exception |
73
|
20 |
|
* @throws InvalidArgumentException |
74
|
20 |
|
* @throws InvalidConfigException |
75
|
20 |
|
* @throws JsonException |
76
|
|
|
* @throws NotSupportedException |
77
|
20 |
|
*/ |
78
|
20 |
|
public function upsert( |
79
|
|
|
string $table, |
80
|
20 |
|
QueryInterface|array $insertColumns, |
81
|
|
|
array|bool $updateColumns, |
82
|
|
|
array &$params = [] |
83
|
|
|
): string { |
84
|
|
|
$constraints = []; |
85
|
|
|
|
86
|
|
|
[$uniqueNames, $insertNames, $updateNames] = $this->prepareUpsertColumns( |
87
|
1 |
|
$table, |
88
|
|
|
$insertColumns, |
89
|
1 |
|
$updateColumns, |
90
|
|
|
$constraints |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
if (empty($uniqueNames)) { |
94
|
|
|
return $this->insert($table, $insertColumns, $params); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$onCondition = ['or']; |
98
|
|
|
$quotedTableName = $this->quoter->quoteTableName($table); |
99
|
|
|
|
100
|
|
|
foreach ($constraints as $constraint) { |
101
|
36 |
|
$columnNames = (array) $constraint->getColumnNames(); |
102
|
|
|
$constraintCondition = ['and']; |
103
|
|
|
/** @psalm-var string[] $columnNames */ |
104
|
|
|
foreach ($columnNames as $name) { |
105
|
|
|
$quotedName = $this->quoter->quoteColumnName($name); |
106
|
|
|
$constraintCondition[] = "$quotedTableName.$quotedName=\"EXCLUDED\".$quotedName"; |
107
|
36 |
|
} |
108
|
|
|
|
109
|
36 |
|
$onCondition[] = $constraintCondition; |
110
|
36 |
|
} |
111
|
36 |
|
|
112
|
36 |
|
$on = $this->queryBuilder->buildCondition($onCondition, $params); |
113
|
36 |
|
|
114
|
36 |
|
[, $placeholders, $values, $params] = $this->prepareInsertValues($table, $insertColumns, $params); |
115
|
|
|
|
116
|
36 |
|
if (!empty($placeholders)) { |
117
|
2 |
|
$usingSelectValues = []; |
118
|
|
|
|
119
|
|
|
foreach ($insertNames as $index => $name) { |
120
|
34 |
|
$usingSelectValues[$name] = new Expression($placeholders[$index]); |
121
|
34 |
|
} |
122
|
|
|
|
123
|
34 |
|
$values = $this->queryBuilder->buildSelect($usingSelectValues, $params) |
124
|
34 |
|
. ' ' . $this->queryBuilder->buildFrom(['DUAL'], $params); |
125
|
34 |
|
} |
126
|
|
|
|
127
|
34 |
|
$insertValues = []; |
128
|
34 |
|
$mergeSql = 'MERGE INTO ' . $quotedTableName . ' USING (' . $values . ') "EXCLUDED" ON (' . $on . ')'; |
129
|
34 |
|
|
130
|
|
|
foreach ($insertNames as $quotedName) { |
131
|
|
|
$insertValues[] = '"EXCLUDED".' . $quotedName; |
132
|
34 |
|
} |
133
|
|
|
|
134
|
|
|
$insertSql = 'INSERT (' . implode(', ', $insertNames) . ')' . ' VALUES (' . implode(', ', $insertValues) . ')'; |
135
|
34 |
|
|
136
|
|
|
if ($updateColumns === false || $updateNames === []) { |
|
|
|
|
137
|
34 |
|
/** there are no columns to update */ |
138
|
|
|
return "$mergeSql WHEN NOT MATCHED THEN $insertSql"; |
139
|
34 |
|
} |
140
|
21 |
|
|
141
|
|
|
if ($updateColumns === true) { |
|
|
|
|
142
|
21 |
|
$updateColumns = []; |
143
|
21 |
|
/** @psalm-var string[] $updateNames */ |
144
|
|
|
foreach ($updateNames as $quotedName) { |
145
|
|
|
$updateColumns[$quotedName] = new Expression('"EXCLUDED".' . $quotedName); |
146
|
21 |
|
} |
147
|
21 |
|
} |
148
|
|
|
|
149
|
|
|
[$updates, $params] = $this->prepareUpdateSets($table, $updateColumns, $params); |
150
|
34 |
|
$updateSql = 'UPDATE SET ' . implode(', ', $updates); |
151
|
34 |
|
|
152
|
|
|
return "$mergeSql WHEN MATCHED THEN $updateSql WHEN NOT MATCHED THEN $insertSql"; |
153
|
34 |
|
} |
154
|
34 |
|
|
155
|
|
|
protected function prepareInsertValues(string $table, array|QueryInterface $columns, array $params = []): array |
156
|
|
|
{ |
157
|
34 |
|
if (empty($columns)) { |
158
|
|
|
$names = []; |
159
|
34 |
|
$placeholders = []; |
160
|
|
|
$tableSchema = $this->schema->getTableSchema($table); |
161
|
14 |
|
|
162
|
|
|
if ($tableSchema !== null) { |
163
|
|
|
if (!empty($tableSchema->getPrimaryKey())) { |
164
|
20 |
|
$columns = $tableSchema->getPrimaryKey(); |
165
|
10 |
|
} else { |
166
|
|
|
$columns = [current($tableSchema->getColumns())->getName()]; |
167
|
10 |
|
} |
168
|
10 |
|
|
169
|
|
|
foreach ($columns as $name) { |
170
|
|
|
$names[] = $this->quoter->quoteColumnName($name); |
171
|
|
|
$placeholders[] = 'DEFAULT'; |
172
|
20 |
|
} |
173
|
20 |
|
} |
174
|
|
|
|
175
|
20 |
|
return [$names, $placeholders, '', $params]; |
176
|
|
|
} |
177
|
|
|
|
178
|
77 |
|
return parent::prepareInsertValues($table, $columns, $params); |
179
|
|
|
} |
180
|
77 |
|
|
181
|
4 |
|
public function resetSequence(string $table, int|string $value = null): string |
182
|
4 |
|
{ |
183
|
4 |
|
$tableSchema = $this->schema->getTableSchema($table); |
184
|
|
|
|
185
|
4 |
|
if ($tableSchema === null) { |
186
|
4 |
|
throw new InvalidArgumentException("Table not found: '$table'."); |
187
|
2 |
|
} |
188
|
|
|
|
189
|
2 |
|
$sequenceName = $tableSchema->getSequenceName(); |
190
|
|
|
|
191
|
|
|
if ($sequenceName === null) { |
192
|
4 |
|
throw new InvalidArgumentException("There is not sequence associated with table '$table'."); |
193
|
4 |
|
} |
194
|
4 |
|
|
195
|
|
|
if ($value === null && count($tableSchema->getPrimaryKey()) > 1) { |
196
|
|
|
throw new InvalidArgumentException("Can't reset sequence for composite primary key in table: $table"); |
197
|
|
|
} |
198
|
4 |
|
|
199
|
|
|
/** |
200
|
|
|
* Oracle needs at least many queries to reset a sequence (see adding transactions and/or use an alter method to |
201
|
73 |
|
* avoid grant issue?) |
202
|
|
|
*/ |
203
|
|
|
return 'declare |
204
|
5 |
|
lastSeq number' . ($value !== null ? (' := ' . $value) : '') . '; |
205
|
|
|
begin' . ($value === null ? ' |
206
|
5 |
|
SELECT MAX("' . $tableSchema->getPrimaryKey()[0] . '") + 1 INTO lastSeq FROM "' . $tableSchema->getName() . '";' : '') . ' |
207
|
|
|
if lastSeq IS NULL then lastSeq := 1; end if; |
208
|
5 |
|
execute immediate \'DROP SEQUENCE "' . $sequenceName . '"\'; |
209
|
1 |
|
execute immediate \'CREATE SEQUENCE "' . $sequenceName . '" START WITH \' || lastSeq || \' INCREMENT BY 1 NOMAXVALUE NOCACHE\'; |
210
|
|
|
end;'; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.