Total Complexity | 165 |
Total Lines | 1181 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like OraclePlatform often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use OraclePlatform, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class OraclePlatform extends AbstractPlatform |
||
32 | { |
||
33 | /** |
||
34 | * Assertion for Oracle identifiers. |
||
35 | * |
||
36 | * @link http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements008.htm |
||
37 | * |
||
38 | * @param string $identifier |
||
39 | * |
||
40 | * @return void |
||
41 | * |
||
42 | * @throws DBALException |
||
43 | */ |
||
44 | public static function assertValidIdentifier($identifier) |
||
45 | { |
||
46 | if (preg_match('(^(([a-zA-Z]{1}[a-zA-Z0-9_$#]{0,})|("[^"]+"))$)', $identifier) === 0) { |
||
47 | throw new DBALException('Invalid Oracle identifier'); |
||
48 | } |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * {@inheritDoc} |
||
53 | */ |
||
54 | public function getSubstringExpression($value, $position, $length = null) |
||
55 | { |
||
56 | if ($length !== null) { |
||
57 | return sprintf('SUBSTR(%s, %d, %d)', $value, $position, $length); |
||
58 | } |
||
59 | |||
60 | return sprintf('SUBSTR(%s, %d)', $value, $position); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param string $type |
||
65 | * |
||
66 | * @return string |
||
67 | */ |
||
68 | public function getNowExpression($type = 'timestamp') |
||
69 | { |
||
70 | switch ($type) { |
||
71 | case 'date': |
||
72 | case 'time': |
||
73 | case 'timestamp': |
||
74 | default: |
||
75 | return 'TO_CHAR(CURRENT_TIMESTAMP, \'YYYY-MM-DD HH24:MI:SS\')'; |
||
76 | } |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * {@inheritDoc} |
||
81 | */ |
||
82 | public function getLocateExpression($str, $substr, $startPos = false) |
||
83 | { |
||
84 | if ($startPos === false) { |
||
85 | return 'INSTR(' . $str . ', ' . $substr . ')'; |
||
86 | } |
||
87 | |||
88 | return 'INSTR(' . $str . ', ' . $substr . ', ' . $startPos . ')'; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * {@inheritDoc} |
||
93 | * |
||
94 | * @deprecated Use application-generated UUIDs instead |
||
95 | */ |
||
96 | public function getGuidExpression() |
||
97 | { |
||
98 | return 'SYS_GUID()'; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit) |
||
105 | { |
||
106 | switch ($unit) { |
||
107 | case DateIntervalUnit::MONTH: |
||
108 | case DateIntervalUnit::QUARTER: |
||
109 | case DateIntervalUnit::YEAR: |
||
110 | switch ($unit) { |
||
111 | case DateIntervalUnit::QUARTER: |
||
112 | $interval *= 3; |
||
113 | break; |
||
114 | |||
115 | case DateIntervalUnit::YEAR: |
||
116 | $interval *= 12; |
||
117 | break; |
||
118 | } |
||
119 | |||
120 | return 'ADD_MONTHS(' . $date . ', ' . $operator . $interval . ')'; |
||
121 | |||
122 | default: |
||
123 | $calculationClause = ''; |
||
124 | |||
125 | switch ($unit) { |
||
126 | case DateIntervalUnit::SECOND: |
||
127 | $calculationClause = '/24/60/60'; |
||
128 | break; |
||
129 | |||
130 | case DateIntervalUnit::MINUTE: |
||
131 | $calculationClause = '/24/60'; |
||
132 | break; |
||
133 | |||
134 | case DateIntervalUnit::HOUR: |
||
135 | $calculationClause = '/24'; |
||
136 | break; |
||
137 | |||
138 | case DateIntervalUnit::WEEK: |
||
139 | $calculationClause = '*7'; |
||
140 | break; |
||
141 | } |
||
142 | |||
143 | return '(' . $date . $operator . $interval . $calculationClause . ')'; |
||
144 | } |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * {@inheritDoc} |
||
149 | */ |
||
150 | public function getDateDiffExpression($date1, $date2) |
||
151 | { |
||
152 | return sprintf('TRUNC(%s) - TRUNC(%s)', $date1, $date2); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * {@inheritDoc} |
||
157 | */ |
||
158 | public function getBitAndComparisonExpression($value1, $value2) |
||
159 | { |
||
160 | return 'BITAND(' . $value1 . ', ' . $value2 . ')'; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * {@inheritDoc} |
||
165 | */ |
||
166 | public function getBitOrComparisonExpression($value1, $value2) |
||
167 | { |
||
168 | return '(' . $value1 . '-' . |
||
169 | $this->getBitAndComparisonExpression($value1, $value2) |
||
170 | . '+' . $value2 . ')'; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * {@inheritDoc} |
||
175 | * |
||
176 | * Need to specifiy minvalue, since start with is hidden in the system and MINVALUE <= START WITH. |
||
177 | * Therefore we can use MINVALUE to be able to get a hint what START WITH was for later introspection |
||
178 | * in {@see listSequences()} |
||
179 | */ |
||
180 | public function getCreateSequenceSQL(Sequence $sequence) |
||
181 | { |
||
182 | return 'CREATE SEQUENCE ' . $sequence->getQuotedName($this) . |
||
183 | ' START WITH ' . $sequence->getInitialValue() . |
||
184 | ' MINVALUE ' . $sequence->getInitialValue() . |
||
185 | ' INCREMENT BY ' . $sequence->getAllocationSize() . |
||
186 | $this->getSequenceCacheSQL($sequence); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * {@inheritDoc} |
||
191 | */ |
||
192 | public function getAlterSequenceSQL(Sequence $sequence) |
||
193 | { |
||
194 | return 'ALTER SEQUENCE ' . $sequence->getQuotedName($this) . |
||
195 | ' INCREMENT BY ' . $sequence->getAllocationSize() |
||
196 | . $this->getSequenceCacheSQL($sequence); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Cache definition for sequences |
||
201 | * |
||
202 | * @return string |
||
203 | */ |
||
204 | private function getSequenceCacheSQL(Sequence $sequence) |
||
205 | { |
||
206 | if ($sequence->getCache() === 0) { |
||
207 | return ' NOCACHE'; |
||
208 | } |
||
209 | |||
210 | if ($sequence->getCache() === 1) { |
||
211 | return ' NOCACHE'; |
||
212 | } |
||
213 | |||
214 | if ($sequence->getCache() > 1) { |
||
215 | return ' CACHE ' . $sequence->getCache(); |
||
216 | } |
||
217 | |||
218 | return ''; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * {@inheritDoc} |
||
223 | */ |
||
224 | public function getSequenceNextValSQL($sequenceName) |
||
225 | { |
||
226 | return 'SELECT ' . $sequenceName . '.nextval FROM DUAL'; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * {@inheritDoc} |
||
231 | */ |
||
232 | public function getSetTransactionIsolationSQL($level) |
||
233 | { |
||
234 | return 'SET TRANSACTION ISOLATION LEVEL ' . $this->_getTransactionIsolationLevelSQL($level); |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * {@inheritDoc} |
||
239 | */ |
||
240 | protected function _getTransactionIsolationLevelSQL($level) |
||
241 | { |
||
242 | switch ($level) { |
||
243 | case TransactionIsolationLevel::READ_UNCOMMITTED: |
||
244 | return 'READ UNCOMMITTED'; |
||
245 | case TransactionIsolationLevel::READ_COMMITTED: |
||
246 | return 'READ COMMITTED'; |
||
247 | case TransactionIsolationLevel::REPEATABLE_READ: |
||
248 | case TransactionIsolationLevel::SERIALIZABLE: |
||
249 | return 'SERIALIZABLE'; |
||
250 | default: |
||
251 | return parent::_getTransactionIsolationLevelSQL($level); |
||
252 | } |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * {@inheritDoc} |
||
257 | */ |
||
258 | public function getBooleanTypeDeclarationSQL(array $field) |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * {@inheritDoc} |
||
265 | */ |
||
266 | public function getIntegerTypeDeclarationSQL(array $field) |
||
267 | { |
||
268 | return 'NUMBER(10)'; |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * {@inheritDoc} |
||
273 | */ |
||
274 | public function getBigIntTypeDeclarationSQL(array $field) |
||
275 | { |
||
276 | return 'NUMBER(20)'; |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * {@inheritDoc} |
||
281 | */ |
||
282 | public function getSmallIntTypeDeclarationSQL(array $field) |
||
283 | { |
||
284 | return 'NUMBER(5)'; |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * {@inheritDoc} |
||
289 | */ |
||
290 | public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration) |
||
291 | { |
||
292 | return 'TIMESTAMP(0)'; |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * {@inheritDoc} |
||
297 | */ |
||
298 | public function getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration) |
||
299 | { |
||
300 | return 'TIMESTAMP(0) WITH TIME ZONE'; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * {@inheritDoc} |
||
305 | */ |
||
306 | public function getDateTypeDeclarationSQL(array $fieldDeclaration) |
||
307 | { |
||
308 | return 'DATE'; |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * {@inheritDoc} |
||
313 | */ |
||
314 | public function getTimeTypeDeclarationSQL(array $fieldDeclaration) |
||
315 | { |
||
316 | return 'DATE'; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * {@inheritDoc} |
||
321 | */ |
||
322 | protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef) |
||
323 | { |
||
324 | return ''; |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * {@inheritDoc} |
||
329 | */ |
||
330 | protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed) |
||
331 | { |
||
332 | return $fixed ? ($length > 0 ? 'CHAR(' . $length . ')' : 'CHAR(2000)') |
||
333 | : ($length > 0 ? 'VARCHAR2(' . $length . ')' : 'VARCHAR2(4000)'); |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * {@inheritdoc} |
||
338 | */ |
||
339 | protected function getBinaryTypeDeclarationSQLSnippet($length, $fixed) |
||
340 | { |
||
341 | return 'RAW(' . ($length > 0 ? $length : $this->getBinaryMaxLength()) . ')'; |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * {@inheritdoc} |
||
346 | */ |
||
347 | public function getBinaryMaxLength() |
||
348 | { |
||
349 | return 2000; |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * {@inheritDoc} |
||
354 | */ |
||
355 | public function getClobTypeDeclarationSQL(array $field) |
||
356 | { |
||
357 | return 'CLOB'; |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * {@inheritDoc} |
||
362 | */ |
||
363 | public function getListDatabasesSQL() |
||
364 | { |
||
365 | return 'SELECT username FROM all_users'; |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * {@inheritDoc} |
||
370 | */ |
||
371 | public function getListSequencesSQL($database) |
||
372 | { |
||
373 | $database = $this->normalizeIdentifier($database); |
||
374 | $database = $this->quoteStringLiteral($database->getName()); |
||
375 | |||
376 | return 'SELECT sequence_name, min_value, increment_by FROM sys.all_sequences ' . |
||
377 | 'WHERE SEQUENCE_OWNER = ' . $database; |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * {@inheritDoc} |
||
382 | */ |
||
383 | protected function _getCreateTableSQL($table, array $columns, array $options = []) |
||
384 | { |
||
385 | $indexes = $options['indexes'] ?? []; |
||
386 | $options['indexes'] = []; |
||
387 | $sql = parent::_getCreateTableSQL($table, $columns, $options); |
||
388 | |||
389 | foreach ($columns as $name => $column) { |
||
390 | if (isset($column['sequence'])) { |
||
391 | $sql[] = $this->getCreateSequenceSQL($column['sequence']); |
||
392 | } |
||
393 | |||
394 | if (! isset($column['autoincrement']) || ! $column['autoincrement'] && |
||
395 | (! isset($column['autoinc']) || ! $column['autoinc'])) { |
||
396 | continue; |
||
397 | } |
||
398 | |||
399 | $sql = array_merge($sql, $this->getCreateAutoincrementSql($name, $table)); |
||
400 | } |
||
401 | |||
402 | if (isset($indexes) && ! empty($indexes)) { |
||
403 | foreach ($indexes as $index) { |
||
404 | $sql[] = $this->getCreateIndexSQL($index, $table); |
||
405 | } |
||
406 | } |
||
407 | |||
408 | return $sql; |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * {@inheritDoc} |
||
413 | * |
||
414 | * @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaOracleReader.html |
||
415 | */ |
||
416 | public function getListTableIndexesSQL($table, $currentDatabase = null) |
||
417 | { |
||
418 | $table = $this->normalizeIdentifier($table); |
||
419 | $table = $this->quoteStringLiteral($table->getName()); |
||
420 | |||
421 | return "SELECT uind_col.index_name AS name, |
||
422 | ( |
||
423 | SELECT uind.index_type |
||
424 | FROM user_indexes uind |
||
425 | WHERE uind.index_name = uind_col.index_name |
||
426 | ) AS type, |
||
427 | decode( |
||
428 | ( |
||
429 | SELECT uind.uniqueness |
||
430 | FROM user_indexes uind |
||
431 | WHERE uind.index_name = uind_col.index_name |
||
432 | ), |
||
433 | 'NONUNIQUE', |
||
434 | 0, |
||
435 | 'UNIQUE', |
||
436 | 1 |
||
437 | ) AS is_unique, |
||
438 | uind_col.column_name AS column_name, |
||
439 | uind_col.column_position AS column_pos, |
||
440 | ( |
||
441 | SELECT ucon.constraint_type |
||
442 | FROM user_constraints ucon |
||
443 | WHERE ucon.index_name = uind_col.index_name |
||
444 | ) AS is_primary |
||
445 | FROM user_ind_columns uind_col |
||
446 | WHERE uind_col.table_name = " . $table . ' |
||
447 | ORDER BY uind_col.column_position ASC'; |
||
448 | } |
||
449 | |||
450 | /** |
||
451 | * {@inheritDoc} |
||
452 | */ |
||
453 | public function getListTablesSQL() |
||
454 | { |
||
455 | return 'SELECT * FROM sys.user_tables'; |
||
456 | } |
||
457 | |||
458 | /** |
||
459 | * {@inheritDoc} |
||
460 | */ |
||
461 | public function getListViewsSQL($database) |
||
462 | { |
||
463 | return 'SELECT view_name, text FROM sys.user_views'; |
||
464 | } |
||
465 | |||
466 | /** |
||
467 | * {@inheritDoc} |
||
468 | */ |
||
469 | public function getCreateViewSQL($name, $sql) |
||
470 | { |
||
471 | return 'CREATE VIEW ' . $name . ' AS ' . $sql; |
||
472 | } |
||
473 | |||
474 | /** |
||
475 | * {@inheritDoc} |
||
476 | */ |
||
477 | public function getDropViewSQL($name) |
||
478 | { |
||
479 | return 'DROP VIEW ' . $name; |
||
480 | } |
||
481 | |||
482 | /** |
||
483 | * @param string $name |
||
484 | * @param string $table |
||
485 | * @param int $start |
||
486 | * |
||
487 | * @return string[] |
||
488 | */ |
||
489 | public function getCreateAutoincrementSql($name, $table, $start = 1) |
||
490 | { |
||
491 | $tableIdentifier = $this->normalizeIdentifier($table); |
||
492 | $quotedTableName = $tableIdentifier->getQuotedName($this); |
||
493 | $unquotedTableName = $tableIdentifier->getName(); |
||
494 | |||
495 | $nameIdentifier = $this->normalizeIdentifier($name); |
||
496 | $quotedName = $nameIdentifier->getQuotedName($this); |
||
497 | $unquotedName = $nameIdentifier->getName(); |
||
498 | |||
499 | $sql = []; |
||
500 | |||
501 | $autoincrementIdentifierName = $this->getAutoincrementIdentifierName($tableIdentifier); |
||
502 | |||
503 | $idx = new Index($autoincrementIdentifierName, [$quotedName], true, true); |
||
504 | |||
505 | $sql[] = 'DECLARE |
||
506 | constraints_Count NUMBER; |
||
507 | BEGIN |
||
508 | SELECT COUNT(CONSTRAINT_NAME) INTO constraints_Count FROM USER_CONSTRAINTS WHERE TABLE_NAME = \'' . $unquotedTableName . '\' AND CONSTRAINT_TYPE = \'P\'; |
||
509 | IF constraints_Count = 0 OR constraints_Count = \'\' THEN |
||
510 | EXECUTE IMMEDIATE \'' . $this->getCreateConstraintSQL($idx, $quotedTableName) . '\'; |
||
511 | END IF; |
||
512 | END;'; |
||
513 | |||
514 | $sequenceName = $this->getIdentitySequenceName( |
||
515 | $tableIdentifier->isQuoted() ? $quotedTableName : $unquotedTableName, |
||
516 | $nameIdentifier->isQuoted() ? $quotedName : $unquotedName |
||
517 | ); |
||
518 | $sequence = new Sequence($sequenceName, $start); |
||
519 | $sql[] = $this->getCreateSequenceSQL($sequence); |
||
520 | |||
521 | $sql[] = 'CREATE TRIGGER ' . $autoincrementIdentifierName . ' |
||
522 | BEFORE INSERT |
||
523 | ON ' . $quotedTableName . ' |
||
524 | FOR EACH ROW |
||
525 | DECLARE |
||
526 | last_Sequence NUMBER; |
||
527 | last_InsertID NUMBER; |
||
528 | BEGIN |
||
529 | SELECT ' . $sequenceName . '.NEXTVAL INTO :NEW.' . $quotedName . ' FROM DUAL; |
||
530 | IF (:NEW.' . $quotedName . ' IS NULL OR :NEW.' . $quotedName . ' = 0) THEN |
||
531 | SELECT ' . $sequenceName . '.NEXTVAL INTO :NEW.' . $quotedName . ' FROM DUAL; |
||
532 | ELSE |
||
533 | SELECT NVL(Last_Number, 0) INTO last_Sequence |
||
534 | FROM User_Sequences |
||
535 | WHERE Sequence_Name = \'' . $sequence->getName() . '\'; |
||
536 | SELECT :NEW.' . $quotedName . ' INTO last_InsertID FROM DUAL; |
||
537 | WHILE (last_InsertID > last_Sequence) LOOP |
||
538 | SELECT ' . $sequenceName . '.NEXTVAL INTO last_Sequence FROM DUAL; |
||
539 | END LOOP; |
||
540 | END IF; |
||
541 | END;'; |
||
542 | |||
543 | return $sql; |
||
544 | } |
||
545 | |||
546 | /** |
||
547 | * Returns the SQL statements to drop the autoincrement for the given table name. |
||
548 | * |
||
549 | * @param string $table The table name to drop the autoincrement for. |
||
550 | * |
||
551 | * @return string[] |
||
552 | */ |
||
553 | public function getDropAutoincrementSql($table) |
||
554 | { |
||
555 | $table = $this->normalizeIdentifier($table); |
||
556 | $autoincrementIdentifierName = $this->getAutoincrementIdentifierName($table); |
||
557 | $identitySequenceName = $this->getIdentitySequenceName( |
||
558 | $table->isQuoted() ? $table->getQuotedName($this) : $table->getName(), |
||
559 | '' |
||
560 | ); |
||
561 | |||
562 | return [ |
||
563 | 'DROP TRIGGER ' . $autoincrementIdentifierName, |
||
564 | $this->getDropSequenceSQL($identitySequenceName), |
||
565 | $this->getDropConstraintSQL($autoincrementIdentifierName, $table->getQuotedName($this)), |
||
566 | ]; |
||
567 | } |
||
568 | |||
569 | /** |
||
570 | * Normalizes the given identifier. |
||
571 | * |
||
572 | * Uppercases the given identifier if it is not quoted by intention |
||
573 | * to reflect Oracle's internal auto uppercasing strategy of unquoted identifiers. |
||
574 | * |
||
575 | * @param string $name The identifier to normalize. |
||
576 | * |
||
577 | * @return Identifier The normalized identifier. |
||
578 | */ |
||
579 | private function normalizeIdentifier($name) |
||
580 | { |
||
581 | $identifier = new Identifier($name); |
||
582 | |||
583 | return $identifier->isQuoted() ? $identifier : new Identifier(strtoupper($name)); |
||
584 | } |
||
585 | |||
586 | /** |
||
587 | * Returns the autoincrement primary key identifier name for the given table identifier. |
||
588 | * |
||
589 | * Quotes the autoincrement primary key identifier name |
||
590 | * if the given table name is quoted by intention. |
||
591 | * |
||
592 | * @param Identifier $table The table identifier to return the autoincrement primary key identifier name for. |
||
593 | * |
||
594 | * @return string |
||
595 | */ |
||
596 | private function getAutoincrementIdentifierName(Identifier $table) |
||
597 | { |
||
598 | $identifierName = $table->getName() . '_AI_PK'; |
||
599 | |||
600 | return $table->isQuoted() |
||
601 | ? $this->quoteSingleIdentifier($identifierName) |
||
602 | : $identifierName; |
||
603 | } |
||
604 | |||
605 | /** |
||
606 | * {@inheritDoc} |
||
607 | */ |
||
608 | public function getListTableForeignKeysSQL($table) |
||
634 | ORDER BY cols.constraint_name ASC, cols.position ASC'; |
||
635 | } |
||
636 | |||
637 | /** |
||
638 | * {@inheritDoc} |
||
639 | */ |
||
640 | public function getListTableConstraintsSQL($table) |
||
641 | { |
||
642 | $table = $this->normalizeIdentifier($table); |
||
643 | $table = $this->quoteStringLiteral($table->getName()); |
||
644 | |||
645 | return 'SELECT * FROM user_constraints WHERE table_name = ' . $table; |
||
646 | } |
||
647 | |||
648 | /** |
||
649 | * {@inheritDoc} |
||
650 | */ |
||
651 | public function getListTableColumnsSQL($table, $database = null) |
||
689 | ); |
||
690 | } |
||
691 | |||
692 | /** |
||
693 | * {@inheritDoc} |
||
694 | */ |
||
695 | public function getDropSequenceSQL($sequence) |
||
696 | { |
||
697 | if ($sequence instanceof Sequence) { |
||
698 | $sequence = $sequence->getQuotedName($this); |
||
699 | } |
||
700 | |||
701 | return 'DROP SEQUENCE ' . $sequence; |
||
702 | } |
||
703 | |||
704 | /** |
||
705 | * {@inheritDoc} |
||
706 | */ |
||
707 | public function getDropForeignKeySQL($foreignKey, $table) |
||
708 | { |
||
709 | if (! $foreignKey instanceof ForeignKeyConstraint) { |
||
710 | $foreignKey = new Identifier($foreignKey); |
||
711 | } |
||
712 | |||
713 | if (! $table instanceof Table) { |
||
721 | } |
||
722 | |||
723 | /** |
||
724 | * {@inheritdoc} |
||
725 | */ |
||
726 | public function getAdvancedForeignKeyOptionsSQL(ForeignKeyConstraint $foreignKey) |
||
727 | { |
||
728 | $referentialAction = ''; |
||
729 | |||
730 | if ($foreignKey->hasOption('onDelete')) { |
||
731 | $referentialAction = $this->getForeignKeyReferentialActionSQL($foreignKey->getOption('onDelete')); |
||
732 | } |
||
733 | |||
734 | if ($referentialAction !== '') { |
||
735 | return ' ON DELETE ' . $referentialAction; |
||
736 | } |
||
737 | |||
738 | return ''; |
||
739 | } |
||
740 | |||
741 | /** |
||
742 | * {@inheritdoc} |
||
743 | */ |
||
744 | public function getForeignKeyReferentialActionSQL($action) |
||
745 | { |
||
746 | $action = strtoupper($action); |
||
747 | |||
748 | switch ($action) { |
||
749 | case 'RESTRICT': // RESTRICT is not supported, therefore falling back to NO ACTION. |
||
750 | case 'NO ACTION': |
||
751 | // NO ACTION cannot be declared explicitly, |
||
752 | // therefore returning empty string to indicate to OMIT the referential clause. |
||
753 | return ''; |
||
754 | |||
755 | case 'CASCADE': |
||
756 | case 'SET NULL': |
||
757 | return $action; |
||
758 | |||
759 | default: |
||
760 | // SET DEFAULT is not supported, throw exception instead. |
||
761 | throw new InvalidArgumentException('Invalid foreign key action: ' . $action); |
||
762 | } |
||
763 | } |
||
764 | |||
765 | /** |
||
766 | * {@inheritDoc} |
||
767 | */ |
||
768 | public function getDropDatabaseSQL($database) |
||
769 | { |
||
770 | return 'DROP USER ' . $database . ' CASCADE'; |
||
771 | } |
||
772 | |||
773 | /** |
||
774 | * {@inheritDoc} |
||
775 | */ |
||
776 | public function getAlterTableSQL(TableDiff $diff) |
||
777 | { |
||
778 | $sql = []; |
||
779 | $commentsSQL = []; |
||
780 | $columnSql = []; |
||
781 | |||
782 | $fields = []; |
||
783 | |||
784 | foreach ($diff->addedColumns as $column) { |
||
785 | if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) { |
||
786 | continue; |
||
787 | } |
||
788 | |||
789 | $fields[] = $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray()); |
||
790 | $comment = $this->getColumnComment($column); |
||
791 | |||
792 | if ($comment === null || $comment === '') { |
||
793 | continue; |
||
794 | } |
||
795 | |||
796 | $commentsSQL[] = $this->getCommentOnColumnSQL( |
||
797 | $diff->getName($this)->getQuotedName($this), |
||
798 | $column->getQuotedName($this), |
||
799 | $comment |
||
800 | ); |
||
801 | } |
||
802 | |||
803 | if (count($fields) > 0) { |
||
804 | $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ADD (' . implode(', ', $fields) . ')'; |
||
805 | } |
||
806 | |||
807 | $fields = []; |
||
808 | foreach ($diff->changedColumns as $columnDiff) { |
||
809 | if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) { |
||
810 | continue; |
||
811 | } |
||
812 | |||
813 | $column = $columnDiff->column; |
||
814 | |||
815 | // Do not generate column alteration clause if type is binary and only fixed property has changed. |
||
816 | // Oracle only supports binary type columns with variable length. |
||
817 | // Avoids unnecessary table alteration statements. |
||
818 | if ($column->getType() instanceof BinaryType && |
||
819 | $columnDiff->hasChanged('fixed') && |
||
820 | count($columnDiff->changedProperties) === 1 |
||
821 | ) { |
||
822 | continue; |
||
823 | } |
||
824 | |||
825 | $columnHasChangedComment = $columnDiff->hasChanged('comment'); |
||
826 | |||
827 | /** |
||
828 | * Do not add query part if only comment has changed |
||
829 | */ |
||
830 | if (! ($columnHasChangedComment && count($columnDiff->changedProperties) === 1)) { |
||
831 | $columnInfo = $column->toArray(); |
||
832 | |||
833 | if (! $columnDiff->hasChanged('notnull')) { |
||
834 | unset($columnInfo['notnull']); |
||
835 | } |
||
836 | |||
837 | $fields[] = $column->getQuotedName($this) . $this->getColumnDeclarationSQL('', $columnInfo); |
||
838 | } |
||
839 | |||
840 | if (! $columnHasChangedComment) { |
||
841 | continue; |
||
842 | } |
||
843 | |||
844 | $commentsSQL[] = $this->getCommentOnColumnSQL( |
||
845 | $diff->getName($this)->getQuotedName($this), |
||
846 | $column->getQuotedName($this), |
||
847 | $this->getColumnComment($column) |
||
848 | ); |
||
849 | } |
||
850 | |||
851 | if (count($fields) > 0) { |
||
852 | $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' MODIFY (' . implode(', ', $fields) . ')'; |
||
853 | } |
||
854 | |||
855 | foreach ($diff->renamedColumns as $oldColumnName => $column) { |
||
856 | if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) { |
||
857 | continue; |
||
858 | } |
||
859 | |||
860 | $oldColumnName = new Identifier($oldColumnName); |
||
861 | |||
862 | $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . |
||
863 | ' RENAME COLUMN ' . $oldColumnName->getQuotedName($this) . ' TO ' . $column->getQuotedName($this); |
||
864 | } |
||
865 | |||
866 | $fields = []; |
||
867 | foreach ($diff->removedColumns as $column) { |
||
868 | if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) { |
||
869 | continue; |
||
870 | } |
||
871 | |||
872 | $fields[] = $column->getQuotedName($this); |
||
873 | } |
||
874 | |||
875 | if (count($fields) > 0) { |
||
876 | $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' DROP (' . implode(', ', $fields) . ')'; |
||
877 | } |
||
878 | |||
879 | $tableSql = []; |
||
880 | |||
881 | if (! $this->onSchemaAlterTable($diff, $tableSql)) { |
||
882 | $sql = array_merge($sql, $commentsSQL); |
||
883 | |||
884 | $newName = $diff->getNewName(); |
||
885 | |||
886 | if ($newName !== false) { |
||
887 | $sql[] = sprintf( |
||
888 | 'ALTER TABLE %s RENAME TO %s', |
||
889 | $diff->getName($this)->getQuotedName($this), |
||
890 | $newName->getQuotedName($this) |
||
891 | ); |
||
892 | } |
||
893 | |||
894 | $sql = array_merge( |
||
895 | $this->getPreAlterTableIndexForeignKeySQL($diff), |
||
896 | $sql, |
||
897 | $this->getPostAlterTableIndexForeignKeySQL($diff) |
||
898 | ); |
||
899 | } |
||
900 | |||
901 | return array_merge($sql, $tableSql, $columnSql); |
||
902 | } |
||
903 | |||
904 | /** |
||
905 | * {@inheritdoc} |
||
906 | */ |
||
907 | public function getColumnDeclarationSQL($name, array $field) |
||
908 | { |
||
909 | if (isset($field['columnDefinition'])) { |
||
910 | $columnDef = $this->getCustomTypeDeclarationSQL($field); |
||
911 | } else { |
||
912 | $default = $this->getDefaultValueDeclarationSQL($field); |
||
913 | |||
914 | $notnull = ''; |
||
915 | |||
916 | if (isset($field['notnull'])) { |
||
917 | $notnull = $field['notnull'] ? ' NOT NULL' : ' NULL'; |
||
918 | } |
||
919 | |||
920 | $unique = ! empty($field['unique']) ? |
||
921 | ' ' . $this->getUniqueFieldDeclarationSQL() : ''; |
||
922 | |||
923 | $check = ! empty($field['check']) ? |
||
924 | ' ' . $field['check'] : ''; |
||
925 | |||
926 | $typeDecl = $field['type']->getSQLDeclaration($field, $this); |
||
927 | $columnDef = $typeDecl . $default . $notnull . $unique . $check; |
||
928 | } |
||
929 | |||
930 | return $name . ' ' . $columnDef; |
||
931 | } |
||
932 | |||
933 | /** |
||
934 | * {@inheritdoc} |
||
935 | */ |
||
936 | protected function getRenameIndexSQL($oldIndexName, Index $index, $tableName) |
||
937 | { |
||
938 | if (strpos($tableName, '.') !== false) { |
||
939 | [$schema] = explode('.', $tableName); |
||
940 | $oldIndexName = $schema . '.' . $oldIndexName; |
||
941 | } |
||
942 | |||
943 | return ['ALTER INDEX ' . $oldIndexName . ' RENAME TO ' . $index->getQuotedName($this)]; |
||
944 | } |
||
945 | |||
946 | /** |
||
947 | * {@inheritDoc} |
||
948 | */ |
||
949 | public function prefersSequences() |
||
950 | { |
||
951 | return true; |
||
952 | } |
||
953 | |||
954 | /** |
||
955 | * {@inheritdoc} |
||
956 | */ |
||
957 | public function usesSequenceEmulatedIdentityColumns() |
||
960 | } |
||
961 | |||
962 | /** |
||
963 | * {@inheritdoc} |
||
964 | */ |
||
965 | public function getIdentitySequenceName($tableName, $columnName) |
||
966 | { |
||
967 | $table = new Identifier($tableName); |
||
968 | |||
969 | // No usage of column name to preserve BC compatibility with <2.5 |
||
970 | $identitySequenceName = $table->getName() . '_SEQ'; |
||
971 | |||
972 | if ($table->isQuoted()) { |
||
973 | $identitySequenceName = '"' . $identitySequenceName . '"'; |
||
974 | } |
||
975 | |||
976 | $identitySequenceIdentifier = $this->normalizeIdentifier($identitySequenceName); |
||
977 | |||
978 | return $identitySequenceIdentifier->getQuotedName($this); |
||
979 | } |
||
980 | |||
981 | /** |
||
982 | * {@inheritDoc} |
||
983 | */ |
||
984 | public function supportsCommentOnStatement() |
||
985 | { |
||
986 | return true; |
||
987 | } |
||
988 | |||
989 | /** |
||
990 | * {@inheritDoc} |
||
991 | */ |
||
992 | public function getName() |
||
993 | { |
||
994 | return 'oracle'; |
||
995 | } |
||
996 | |||
997 | /** |
||
998 | * {@inheritDoc} |
||
999 | */ |
||
1000 | protected function doModifyLimitQuery($query, $limit, $offset = null) |
||
1001 | { |
||
1002 | if ($limit === null && $offset <= 0) { |
||
1003 | return $query; |
||
1004 | } |
||
1005 | |||
1006 | if (preg_match('/^\s*SELECT/i', $query) === 1) { |
||
1007 | if (preg_match('/\sFROM\s/i', $query) === 0) { |
||
1008 | $query .= ' FROM dual'; |
||
1009 | } |
||
1010 | |||
1011 | $columns = ['a.*']; |
||
1012 | |||
1013 | if ($offset > 0) { |
||
1014 | $columns[] = 'ROWNUM AS doctrine_rownum'; |
||
1015 | } |
||
1016 | |||
1017 | $query = sprintf('SELECT %s FROM (%s) a', implode(', ', $columns), $query); |
||
1018 | |||
1019 | if ($limit !== null) { |
||
1020 | $query .= sprintf(' WHERE ROWNUM <= %d', $offset + $limit); |
||
1021 | } |
||
1022 | |||
1023 | if ($offset > 0) { |
||
1024 | $query = sprintf('SELECT * FROM (%s) WHERE doctrine_rownum >= %d', $query, $offset + 1); |
||
1025 | } |
||
1026 | } |
||
1027 | |||
1028 | return $query; |
||
1029 | } |
||
1030 | |||
1031 | /** |
||
1032 | * {@inheritDoc} |
||
1033 | * |
||
1034 | * Oracle returns all column names in SQL result sets in uppercase. |
||
1035 | */ |
||
1036 | public function getSQLResultCasing($column) |
||
1037 | { |
||
1038 | return strtoupper($column); |
||
1039 | } |
||
1040 | |||
1041 | /** |
||
1042 | * {@inheritDoc} |
||
1043 | */ |
||
1044 | public function getCreateTemporaryTableSnippetSQL() |
||
1045 | { |
||
1046 | return 'CREATE GLOBAL TEMPORARY TABLE'; |
||
1047 | } |
||
1048 | |||
1049 | /** |
||
1050 | * {@inheritDoc} |
||
1051 | */ |
||
1052 | public function getDateTimeTzFormatString() |
||
1053 | { |
||
1054 | return 'Y-m-d H:i:sP'; |
||
1055 | } |
||
1056 | |||
1057 | /** |
||
1058 | * {@inheritDoc} |
||
1059 | */ |
||
1060 | public function getDateFormatString() |
||
1061 | { |
||
1062 | return 'Y-m-d 00:00:00'; |
||
1063 | } |
||
1064 | |||
1065 | /** |
||
1066 | * {@inheritDoc} |
||
1067 | */ |
||
1068 | public function getTimeFormatString() |
||
1069 | { |
||
1070 | return '1900-01-01 H:i:s'; |
||
1071 | } |
||
1072 | |||
1073 | /** |
||
1074 | * {@inheritDoc} |
||
1075 | */ |
||
1076 | public function fixSchemaElementName($schemaElementName) |
||
1077 | { |
||
1078 | if (strlen($schemaElementName) > 30) { |
||
1079 | // Trim it |
||
1080 | return substr($schemaElementName, 0, 30); |
||
1081 | } |
||
1082 | |||
1083 | return $schemaElementName; |
||
1084 | } |
||
1085 | |||
1086 | /** |
||
1087 | * {@inheritDoc} |
||
1088 | */ |
||
1089 | public function getMaxIdentifierLength() |
||
1090 | { |
||
1091 | return 30; |
||
1092 | } |
||
1093 | |||
1094 | /** |
||
1095 | * {@inheritDoc} |
||
1096 | */ |
||
1097 | public function supportsSequences() |
||
1098 | { |
||
1099 | return true; |
||
1100 | } |
||
1101 | |||
1102 | /** |
||
1103 | * {@inheritDoc} |
||
1104 | */ |
||
1105 | public function supportsForeignKeyOnUpdate() |
||
1106 | { |
||
1107 | return false; |
||
1108 | } |
||
1109 | |||
1110 | /** |
||
1111 | * {@inheritDoc} |
||
1112 | */ |
||
1113 | public function supportsReleaseSavepoints() |
||
1114 | { |
||
1115 | return false; |
||
1116 | } |
||
1117 | |||
1118 | /** |
||
1119 | * {@inheritDoc} |
||
1120 | */ |
||
1121 | public function getTruncateTableSQL($tableName, $cascade = false) |
||
1122 | { |
||
1123 | $tableIdentifier = new Identifier($tableName); |
||
1124 | |||
1125 | return 'TRUNCATE TABLE ' . $tableIdentifier->getQuotedName($this); |
||
1126 | } |
||
1127 | |||
1128 | /** |
||
1129 | * {@inheritDoc} |
||
1130 | */ |
||
1131 | public function getDummySelectSQL() |
||
1132 | { |
||
1133 | $expression = func_num_args() > 0 ? func_get_arg(0) : '1'; |
||
1134 | |||
1135 | return sprintf('SELECT %s FROM DUAL', $expression); |
||
1136 | } |
||
1137 | |||
1138 | /** |
||
1139 | * {@inheritDoc} |
||
1140 | */ |
||
1141 | protected function initializeDoctrineTypeMappings() |
||
1142 | { |
||
1143 | $this->doctrineTypeMapping = [ |
||
1144 | 'binary_double' => 'float', |
||
1145 | 'binary_float' => 'float', |
||
1146 | 'binary_integer' => 'boolean', |
||
1147 | 'blob' => 'blob', |
||
1148 | 'char' => 'string', |
||
1149 | 'clob' => 'text', |
||
1150 | 'date' => 'date', |
||
1151 | 'float' => 'float', |
||
1152 | 'integer' => 'integer', |
||
1153 | 'long' => 'string', |
||
1154 | 'long raw' => 'blob', |
||
1155 | 'nchar' => 'string', |
||
1156 | 'nclob' => 'text', |
||
1157 | 'number' => 'integer', |
||
1158 | 'nvarchar2' => 'string', |
||
1159 | 'pls_integer' => 'boolean', |
||
1160 | 'raw' => 'binary', |
||
1161 | 'rowid' => 'string', |
||
1162 | 'timestamp' => 'datetime', |
||
1163 | 'timestamptz' => 'datetimetz', |
||
1164 | 'urowid' => 'string', |
||
1165 | 'varchar' => 'string', |
||
1166 | 'varchar2' => 'string', |
||
1167 | ]; |
||
1168 | } |
||
1169 | |||
1170 | /** |
||
1171 | * {@inheritDoc} |
||
1172 | */ |
||
1173 | public function releaseSavePoint($savepoint) |
||
1174 | { |
||
1175 | return ''; |
||
1176 | } |
||
1177 | |||
1178 | /** |
||
1179 | * {@inheritDoc} |
||
1180 | */ |
||
1181 | protected function getReservedKeywordsClass() |
||
1184 | } |
||
1185 | |||
1186 | /** |
||
1187 | * {@inheritDoc} |
||
1188 | */ |
||
1189 | public function getBlobTypeDeclarationSQL(array $field) |
||
1190 | { |
||
1191 | return 'BLOB'; |
||
1192 | } |
||
1193 | |||
1194 | public function getListTableCommentsSQL(string $table, ?string $database = null) : string |
||
1195 | { |
||
1196 | $tableCommentsName = 'user_tab_comments'; |
||
1197 | $ownerCondition = ''; |
||
1212 | ); |
||
1213 | } |
||
1214 | } |
||
1215 |