|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Oracle\PDO; |
|
6
|
|
|
|
|
7
|
|
|
use Generator; |
|
8
|
|
|
use Throwable; |
|
9
|
|
|
use Yiisoft\Db\Command\CommandInterface; |
|
10
|
|
|
use Yiisoft\Db\Exception\Exception; |
|
11
|
|
|
use Yiisoft\Db\Exception\InvalidArgumentException; |
|
12
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
|
13
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
|
14
|
|
|
use Yiisoft\Db\Expression\ExpressionInterface; |
|
15
|
|
|
use Yiisoft\Db\Oracle\Conditions\InConditionBuilder; |
|
16
|
|
|
use Yiisoft\Db\Oracle\Conditions\LikeConditionBuilder; |
|
17
|
|
|
use Yiisoft\Db\Oracle\DDLQueryBuilder; |
|
18
|
|
|
use Yiisoft\Db\Oracle\DMLQueryBuilder; |
|
19
|
|
|
use Yiisoft\Db\Query\Conditions\InCondition; |
|
20
|
|
|
use Yiisoft\Db\Query\Conditions\LikeCondition; |
|
21
|
|
|
use Yiisoft\Db\Query\Query; |
|
22
|
|
|
use Yiisoft\Db\Query\QueryBuilder; |
|
|
|
|
|
|
23
|
|
|
use Yiisoft\Db\Schema\QuoterInterface; |
|
24
|
|
|
use Yiisoft\Db\Schema\Schema; |
|
25
|
|
|
use Yiisoft\Db\Schema\SchemaInterface; |
|
26
|
|
|
use Yiisoft\Strings\NumericHelper; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* QueryBuilder is the query builder for Oracle databases. |
|
30
|
|
|
*/ |
|
31
|
|
|
final class QueryBuilderPDOOracle extends QueryBuilder |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var array mapping from abstract column types (keys) to physical column types (values). |
|
35
|
|
|
*/ |
|
36
|
|
|
protected array $typeMap = [ |
|
37
|
|
|
Schema::TYPE_PK => 'NUMBER(10) NOT NULL PRIMARY KEY', |
|
38
|
|
|
Schema::TYPE_UPK => 'NUMBER(10) UNSIGNED NOT NULL PRIMARY KEY', |
|
39
|
|
|
Schema::TYPE_BIGPK => 'NUMBER(20) NOT NULL PRIMARY KEY', |
|
40
|
|
|
Schema::TYPE_UBIGPK => 'NUMBER(20) UNSIGNED NOT NULL PRIMARY KEY', |
|
41
|
|
|
Schema::TYPE_CHAR => 'CHAR(1)', |
|
42
|
|
|
Schema::TYPE_STRING => 'VARCHAR2(255)', |
|
43
|
|
|
Schema::TYPE_TEXT => 'CLOB', |
|
44
|
|
|
Schema::TYPE_TINYINT => 'NUMBER(3)', |
|
45
|
|
|
Schema::TYPE_SMALLINT => 'NUMBER(5)', |
|
46
|
|
|
Schema::TYPE_INTEGER => 'NUMBER(10)', |
|
47
|
|
|
Schema::TYPE_BIGINT => 'NUMBER(20)', |
|
48
|
|
|
Schema::TYPE_FLOAT => 'NUMBER', |
|
49
|
|
|
Schema::TYPE_DOUBLE => 'NUMBER', |
|
50
|
|
|
Schema::TYPE_DECIMAL => 'NUMBER', |
|
51
|
|
|
Schema::TYPE_DATETIME => 'TIMESTAMP', |
|
52
|
|
|
Schema::TYPE_TIMESTAMP => 'TIMESTAMP', |
|
53
|
|
|
Schema::TYPE_TIME => 'TIMESTAMP', |
|
54
|
|
|
Schema::TYPE_DATE => 'DATE', |
|
55
|
|
|
Schema::TYPE_BINARY => 'BLOB', |
|
56
|
|
|
Schema::TYPE_BOOLEAN => 'NUMBER(1)', |
|
57
|
|
|
Schema::TYPE_MONEY => 'NUMBER(19,4)', |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
329 |
|
public function __construct( |
|
61
|
|
|
private CommandInterface $command, |
|
62
|
|
|
private Query $query, |
|
63
|
|
|
private QuoterInterface $quoter, |
|
64
|
|
|
private SchemaInterface $schema |
|
65
|
|
|
) { |
|
66
|
329 |
|
$this->ddlBuilder = new DDLQueryBuilder($this); |
|
|
|
|
|
|
67
|
329 |
|
$this->dmlBuilder = new DMLQueryBuilder($this); |
|
|
|
|
|
|
68
|
329 |
|
parent::__construct($quoter, $schema); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
4 |
|
public function addForeignKey( |
|
72
|
|
|
string $name, |
|
73
|
|
|
string $table, |
|
74
|
|
|
$columns, |
|
75
|
|
|
string $refTable, |
|
76
|
|
|
$refColumns, |
|
77
|
|
|
?string $delete = null, |
|
78
|
|
|
?string $update = null |
|
79
|
|
|
): string { |
|
80
|
4 |
|
$sql = 'ALTER TABLE ' . $this->quoter->quoteTableName($table) |
|
81
|
4 |
|
. ' ADD CONSTRAINT ' . $this->quoter->quoteColumnName($name) |
|
82
|
4 |
|
. ' FOREIGN KEY (' . $this->buildColumns($columns) . ')' |
|
83
|
4 |
|
. ' REFERENCES ' . $this->quoter->quoteTableName($refTable) |
|
84
|
4 |
|
. ' (' . $this->buildColumns($refColumns) . ')'; |
|
85
|
|
|
|
|
86
|
4 |
|
if ($delete !== null) { |
|
87
|
2 |
|
$sql .= ' ON DELETE ' . $delete; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
4 |
|
if ($update !== null) { |
|
91
|
|
|
throw new Exception('Oracle does not support ON UPDATE clause.'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
4 |
|
return $sql; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Builds a SQL statement for changing the definition of a column. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $table the table whose column is to be changed. The table name will be properly quoted by the |
|
101
|
|
|
* method. |
|
102
|
|
|
* @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
|
103
|
|
|
* @param string $type the new column type. The [[getColumnType]] method will be invoked to convert abstract column |
|
104
|
|
|
* type (if any) into the physical one. Anything that is not recognized as abstract type will be kept in the |
|
105
|
|
|
* generated SQL. |
|
106
|
|
|
* |
|
107
|
|
|
* For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become |
|
108
|
|
|
* 'varchar(255) not null'. |
|
109
|
|
|
* |
|
110
|
|
|
* @return string the SQL statement for changing the definition of a column. |
|
111
|
|
|
*/ |
|
112
|
|
|
public function alterColumn(string $table, string $column, string $type): string |
|
113
|
|
|
{ |
|
114
|
|
|
$type = $this->getColumnType($type); |
|
115
|
|
|
|
|
116
|
|
|
return 'ALTER TABLE ' |
|
117
|
|
|
. $this->quoter->quoteTableName($table) |
|
118
|
|
|
. ' MODIFY ' |
|
119
|
|
|
. $this->quoter->quoteColumnName($column) |
|
120
|
|
|
. ' ' . $this->getColumnType($type); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Generates a batch INSERT SQL statement. |
|
125
|
|
|
* |
|
126
|
|
|
* For example, |
|
127
|
|
|
* |
|
128
|
|
|
* ```php |
|
129
|
|
|
* $sql = $queryBuilder->batchInsert('user', ['name', 'age'], [ |
|
130
|
|
|
* ['Tom', 30], |
|
131
|
|
|
* ['Jane', 20], |
|
132
|
|
|
* ['Linda', 25], |
|
133
|
|
|
* ]); |
|
134
|
|
|
* ``` |
|
135
|
|
|
* |
|
136
|
|
|
* Note that the values in each row must match the corresponding column names. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $table the table that new rows will be inserted into. |
|
139
|
|
|
* @param array $columns the column names. |
|
140
|
|
|
* @param Generator|iterable $rows the rows to be batched inserted into the table. |
|
141
|
|
|
* @param array $params |
|
142
|
|
|
* |
|
143
|
|
|
* @throws \Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException |
|
144
|
|
|
* |
|
145
|
|
|
* @return string the batch INSERT SQL statement. |
|
146
|
|
|
*/ |
|
147
|
14 |
|
public function batchInsert(string $table, array $columns, iterable|Generator $rows, array &$params = []): string |
|
148
|
|
|
{ |
|
149
|
14 |
|
if (empty($rows)) { |
|
150
|
2 |
|
return ''; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
13 |
|
$schema = $this->schema; |
|
154
|
|
|
|
|
155
|
13 |
|
if (($tableSchema = $schema->getTableSchema($table)) !== null) { |
|
156
|
9 |
|
$columnSchemas = $tableSchema->getColumns(); |
|
157
|
|
|
} else { |
|
158
|
4 |
|
$columnSchemas = []; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
13 |
|
$values = []; |
|
162
|
|
|
|
|
163
|
13 |
|
foreach ($rows as $row) { |
|
164
|
12 |
|
$vs = []; |
|
165
|
12 |
|
foreach ($row as $i => $value) { |
|
166
|
12 |
|
if (isset($columns[$i], $columnSchemas[$columns[$i]])) { |
|
167
|
5 |
|
$value = $columnSchemas[$columns[$i]]->dbTypecast($value); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
12 |
|
if (is_string($value)) { |
|
171
|
8 |
|
$value = $this->quoter->quoteValue($value); |
|
172
|
7 |
|
} elseif (is_float($value)) { |
|
173
|
|
|
/* ensure type cast always has . as decimal separator in all locales */ |
|
174
|
1 |
|
$value = NumericHelper::normalize($value); |
|
175
|
7 |
|
} elseif ($value === false) { |
|
176
|
3 |
|
$value = 0; |
|
177
|
7 |
|
} elseif ($value === null) { |
|
178
|
4 |
|
$value = 'NULL'; |
|
179
|
4 |
|
} elseif ($value instanceof ExpressionInterface) { |
|
180
|
3 |
|
$value = $this->buildExpression($value, $params); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
12 |
|
$vs[] = $value; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
12 |
|
$values[] = '(' . implode(', ', $vs) . ')'; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
13 |
|
if (empty($values)) { |
|
190
|
1 |
|
return ''; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
12 |
|
foreach ($columns as $i => $name) { |
|
194
|
11 |
|
$columns[$i] = $this->quoter->quoteColumnName($name); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
12 |
|
$tableAndColumns = ' INTO ' . $this->quoter->quoteTableName($table) |
|
198
|
12 |
|
. ' (' . implode(', ', $columns) . ') VALUES '; |
|
199
|
|
|
|
|
200
|
12 |
|
return 'INSERT ALL ' . $tableAndColumns . implode($tableAndColumns, $values) . ' SELECT 1 FROM SYS.DUAL'; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
169 |
|
public function buildOrderByAndLimit(string $sql, array $orderBy, $limit, $offset, array &$params = []): string |
|
204
|
|
|
{ |
|
205
|
169 |
|
$orderBy = $this->buildOrderBy($orderBy, $params); |
|
206
|
|
|
|
|
207
|
169 |
|
if ($orderBy !== '') { |
|
208
|
6 |
|
$sql .= $this->separator . $orderBy; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
169 |
|
$filters = []; |
|
212
|
|
|
|
|
213
|
169 |
|
if ($this->hasOffset($offset)) { |
|
214
|
1 |
|
$filters[] = 'rowNumId > ' . $offset; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
169 |
|
if ($this->hasLimit($limit)) { |
|
218
|
9 |
|
$filters[] = 'rownum <= ' . $limit; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
169 |
|
if (empty($filters)) { |
|
222
|
163 |
|
return $sql; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
9 |
|
$filter = implode(' AND ', $filters); |
|
226
|
|
|
return <<<SQL |
|
227
|
9 |
|
WITH USER_SQL AS ($sql), PAGINATION AS (SELECT USER_SQL.*, rownum as rowNumId FROM USER_SQL) |
|
228
|
|
|
SELECT * FROM PAGINATION WHERE $filter |
|
229
|
|
|
SQL; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string |
|
233
|
|
|
{ |
|
234
|
|
|
throw new NotSupportedException('Oracle does not support enabling/disabling integrity check.'); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
public function command(): CommandInterface |
|
238
|
|
|
{ |
|
239
|
|
|
return $this->command; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Builds a SQL statement for dropping an index. |
|
244
|
|
|
* |
|
245
|
|
|
* @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
|
246
|
|
|
* @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
|
247
|
|
|
* |
|
248
|
|
|
* @return string the SQL statement for dropping an index. |
|
249
|
|
|
*/ |
|
250
|
2 |
|
public function dropIndex(string $name, string $table): string |
|
251
|
|
|
{ |
|
252
|
2 |
|
return 'DROP INDEX ' . $this->quoter->quoteTableName($name); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Creates a SQL statement for resetting the sequence value of a table's primary key. |
|
257
|
|
|
* |
|
258
|
|
|
* The sequence will be reset such that the primary key of the next new row inserted will have the specified value |
|
259
|
|
|
* or 1. |
|
260
|
|
|
* |
|
261
|
|
|
* @param string $tableName the name of the table whose primary key sequence will be reset. |
|
262
|
|
|
* @param array|int|string|null $value the value for the primary key of the next new row inserted. If this is not |
|
263
|
|
|
* set, the next new row's primary key will have a value 1. |
|
264
|
|
|
* |
|
265
|
|
|
* @throws Exception|InvalidArgumentException|InvalidConfigException|Throwable |
|
266
|
|
|
*/ |
|
267
|
1 |
|
public function executeResetSequence(string $tableName, array|int|string $value = null): void |
|
268
|
|
|
{ |
|
269
|
1 |
|
$tableSchema = $this->schema->getTableSchema($tableName); |
|
270
|
|
|
|
|
271
|
1 |
|
if ($tableSchema === null) { |
|
272
|
|
|
throw new InvalidArgumentException("Unknown table: $tableName"); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
1 |
|
if ($tableSchema->getSequenceName() === null) { |
|
276
|
|
|
throw new InvalidArgumentException("There is no sequence associated with table: $tableName"); |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
1 |
|
if ($value !== null) { |
|
280
|
1 |
|
$value = (int) $value; |
|
281
|
|
|
} else { |
|
282
|
1 |
|
if (count($tableSchema->getPrimaryKey()) > 1) { |
|
283
|
|
|
throw new InvalidArgumentException( |
|
284
|
|
|
"Can't reset sequence for composite primary key in table: $tableName" |
|
285
|
|
|
); |
|
286
|
|
|
} |
|
287
|
1 |
|
$value = $this->command->setSql( |
|
288
|
1 |
|
'SELECT MAX("' . $tableSchema->getPrimaryKey()[0] . '") FROM "' . $tableSchema->getName() . '"' |
|
289
|
1 |
|
)->queryScalar() + 1; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Oracle needs at least two queries to reset sequence (see adding transactions and/or use alter method to |
|
294
|
|
|
* avoid grants' issue?) |
|
295
|
|
|
*/ |
|
296
|
1 |
|
$this->command->setSQl('DROP SEQUENCE "' . $tableSchema->getSequenceName() . '"')->execute(); |
|
297
|
1 |
|
$this->command->setSql( |
|
298
|
|
|
'CREATE SEQUENCE "' . |
|
299
|
1 |
|
$tableSchema->getSequenceName() . |
|
300
|
1 |
|
'" START WITH ' . |
|
301
|
|
|
$value . |
|
302
|
1 |
|
' INCREMENT BY 1 NOMAXVALUE NOCACHE' |
|
303
|
|
|
)->execute(); |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
41 |
|
public function prepareInsertValues(string $table, $columns, array $params = []): array |
|
307
|
|
|
{ |
|
308
|
41 |
|
[$names, $placeholders, $values, $params] = parent::prepareInsertValues($table, $columns, $params); |
|
309
|
|
|
|
|
310
|
38 |
|
if (!$columns instanceof Query && empty($names)) { |
|
311
|
|
|
$tableSchema = $this->schema->getTableSchema($table); |
|
312
|
|
|
|
|
313
|
|
|
if ($tableSchema !== null) { |
|
314
|
|
|
$tableColumns = $tableSchema->getColumns(); |
|
315
|
|
|
$columns = !empty($tableSchema->getPrimaryKey()) |
|
316
|
|
|
? $tableSchema->getPrimaryKey() : [reset($tableColumns)->getName()]; |
|
317
|
|
|
foreach ($columns as $name) { |
|
318
|
|
|
$names[] = $this->quoter->quoteColumnName($name); |
|
319
|
|
|
$placeholders[] = 'DEFAULT'; |
|
320
|
|
|
} |
|
321
|
|
|
} |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
38 |
|
return [$names, $placeholders, $values, $params]; |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
6 |
|
public function query(): Query |
|
328
|
|
|
{ |
|
329
|
6 |
|
return $this->query; |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
271 |
|
public function quoter(): QuoterInterface |
|
333
|
|
|
{ |
|
334
|
271 |
|
return $this->quoter; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* Builds a SQL statement for renaming a DB table. |
|
339
|
|
|
* |
|
340
|
|
|
* @param string $oldName |
|
341
|
|
|
* @param string $newName the new table name. The name will be properly quoted by the method. |
|
342
|
|
|
* |
|
343
|
|
|
* @return string the SQL statement for renaming a DB table. |
|
344
|
|
|
*/ |
|
345
|
2 |
|
public function renameTable(string $oldName, string $newName): string |
|
346
|
|
|
{ |
|
347
|
2 |
|
return 'ALTER TABLE ' . $this->quoter->quoteTableName($oldName) . ' RENAME TO ' . |
|
348
|
2 |
|
$this->quoter->quoteTableName($newName); |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
1 |
|
public function selectExists(string $rawSql): string |
|
352
|
|
|
{ |
|
353
|
1 |
|
return 'SELECT CASE WHEN EXISTS(' . $rawSql . ') THEN 1 ELSE 0 END FROM DUAL'; |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
144 |
|
public function schema(): SchemaInterface |
|
357
|
|
|
{ |
|
358
|
144 |
|
return $this->schema; |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
329 |
|
protected function defaultExpressionBuilders(): array |
|
362
|
|
|
{ |
|
363
|
329 |
|
return array_merge(parent::defaultExpressionBuilders(), [ |
|
364
|
|
|
InCondition::class => InConditionBuilder::class, |
|
365
|
|
|
LikeCondition::class => LikeConditionBuilder::class, |
|
366
|
|
|
]); |
|
367
|
|
|
} |
|
368
|
|
|
} |
|
369
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths