Passed
Pull Request — 2.11.x (#3971)
by Grégoire
03:18
created

OraclePlatform   F

Complexity

Total Complexity 166

Size/Duplication

Total Lines 1177
Duplicated Lines 0 %

Test Coverage

Coverage 89.63%

Importance

Changes 0
Metric Value
wmc 166
eloc 375
dl 0
loc 1177
ccs 363
cts 405
cp 0.8963
rs 2
c 0
b 0
f 0

74 Methods

Rating   Name   Duplication   Size   Complexity  
A getDropViewSQL() 0 3 1
A getListTablesSQL() 0 3 1
A getCreateViewSQL() 0 3 1
A getListViewsSQL() 0 3 1
A assertValidIdentifier() 0 4 2
A getSubstringExpression() 0 7 2
A usesSequenceEmulatedIdentityColumns() 0 3 1
A getForeignKeyReferentialActionSQL() 0 18 5
A getReservedKeywordsClass() 0 3 1
A fixSchemaElementName() 0 8 2
A initializeDoctrineTypeMappings() 0 26 1
F getAlterTableSQL() 0 126 22
A getBooleanTypeDeclarationSQL() 0 3 1
A getListTableForeignKeysSQL() 0 26 1
A getSequenceNextValSQL() 0 3 1
A supportsCommentOnStatement() 0 3 1
A getListTableColumnsSQL() 0 38 3
A getBinaryTypeDeclarationSQLSnippet() 0 3 2
A getDropAutoincrementSql() 0 13 2
A getAutoincrementIdentifierName() 0 7 2
A getBigIntTypeDeclarationSQL() 0 3 1
A getIdentitySequenceName() 0 14 2
A supportsSequences() 0 3 1
A getAdvancedForeignKeyOptionsSQL() 0 9 3
A _getTransactionIsolationLevelSQL() 0 12 5
A getSQLResultCasing() 0 3 1
A getDateDiffExpression() 0 3 1
A getVarcharTypeDeclarationSQLSnippet() 0 4 4
A getAlterSequenceSQL() 0 5 1
A getDateFormatString() 0 3 1
A getGuidExpression() 0 3 1
A getTimeFormatString() 0 3 1
B getColumnDeclarationSQL() 0 24 8
A getListTableIndexesSQL() 0 31 1
A supportsForeignKeyOnUpdate() 0 3 1
A getListTableCommentsSQL() 0 18 3
A getTruncateTableSQL() 0 5 1
A getDummySelectSQL() 0 5 2
A getBitAndComparisonExpression() 0 3 1
A getName() 0 3 1
A getBinaryMaxLength() 0 3 1
A getCreateSequenceSQL() 0 7 1
A getDateTypeDeclarationSQL() 0 3 1
A getSetTransactionIsolationSQL() 0 3 1
A getRenameIndexSQL() 0 8 2
A getCreateTemporaryTableSnippetSQL() 0 3 1
A getDateTimeTzFormatString() 0 3 1
A getClobTypeDeclarationSQL() 0 3 1
A prefersSequences() 0 3 1
A getDateTimeTzTypeDeclarationSQL() 0 3 1
A _getCommonIntegerTypeDeclarationSQL() 0 3 1
A getListDatabasesSQL() 0 3 1
A getLocateExpression() 0 7 2
A getTimeTypeDeclarationSQL() 0 3 1
A getListSequencesSQL() 0 7 1
A getDropForeignKeySQL() 0 14 3
A getBlobTypeDeclarationSQL() 0 3 1
A normalizeIdentifier() 0 5 2
A getDateTimeTypeDeclarationSQL() 0 3 1
B doModifyLimitQuery() 0 29 8
B _getCreateTableSQL() 0 26 10
A releaseSavePoint() 0 3 1
A getMaxIdentifierLength() 0 3 1
A getSmallIntTypeDeclarationSQL() 0 3 1
A getSequenceCacheSQL() 0 15 4
A getDropDatabaseSQL() 0 3 1
A supportsReleaseSavepoints() 0 3 1
B getDateArithmeticIntervalExpression() 0 40 10
A getDropSequenceSQL() 0 7 2
A getCreateAutoincrementSql() 0 55 3
A getListTableConstraintsSQL() 0 6 1
A getIntegerTypeDeclarationSQL() 0 3 1
A getNowExpression() 0 8 4
A getBitOrComparisonExpression() 0 5 1

How to fix   Complexity   

Complex Class

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
2
3
namespace Doctrine\DBAL\Platforms;
4
5
use Doctrine\DBAL\DBALException;
6
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
7
use Doctrine\DBAL\Schema\Identifier;
8
use Doctrine\DBAL\Schema\Index;
9
use Doctrine\DBAL\Schema\Sequence;
10
use Doctrine\DBAL\Schema\Table;
11
use Doctrine\DBAL\Schema\TableDiff;
12
use Doctrine\DBAL\TransactionIsolationLevel;
13
use Doctrine\DBAL\Types\BinaryType;
14
use InvalidArgumentException;
15
use function array_merge;
16
use function count;
17
use function explode;
18
use function func_get_arg;
19
use function func_num_args;
20
use function implode;
21
use function preg_match;
22
use function sprintf;
23
use function strlen;
24
use function strpos;
25
use function strtoupper;
26
use function substr;
27
28
/**
29
 * OraclePlatform.
30
 */
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 2199
    public static function assertValidIdentifier($identifier)
45
    {
46 2199
        if (! preg_match('(^(([a-zA-Z]{1}[a-zA-Z0-9_$#]{0,})|("[^"]+"))$)', $identifier)) {
47 2167
            throw new DBALException('Invalid Oracle identifier');
48
        }
49 2194
    }
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 1
    public function getLocateExpression($str, $substr, $startPos = false)
83
    {
84 1
        if ($startPos === false) {
85 1
            return 'INSTR(' . $str . ', ' . $substr . ')';
86
        }
87
88 1
        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 1
    protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit)
105
    {
106 1
        switch ($unit) {
107
            case DateIntervalUnit::MONTH:
108
            case DateIntervalUnit::QUARTER:
109
            case DateIntervalUnit::YEAR:
110 1
                switch ($unit) {
111
                    case DateIntervalUnit::QUARTER:
112 1
                        $interval *= 3;
113 1
                        break;
114
115
                    case DateIntervalUnit::YEAR:
116 1
                        $interval *= 12;
117 1
                        break;
118
                }
119
120 1
                return 'ADD_MONTHS(' . $date . ', ' . $operator . $interval . ')';
121
122
            default:
123 1
                $calculationClause = '';
124
125 1
                switch ($unit) {
126
                    case DateIntervalUnit::SECOND:
127 1
                        $calculationClause = '/24/60/60';
128 1
                        break;
129
130
                    case DateIntervalUnit::MINUTE:
131 1
                        $calculationClause = '/24/60';
132 1
                        break;
133
134
                    case DateIntervalUnit::HOUR:
135 1
                        $calculationClause = '/24';
136 1
                        break;
137
138
                    case DateIntervalUnit::WEEK:
139 1
                        $calculationClause = '*7';
140 1
                        break;
141
                }
142
143 1
                return '(' . $date . $operator . $interval . $calculationClause . ')';
144
        }
145
    }
146
147
    /**
148
     * {@inheritDoc}
149
     */
150 3
    public function getDateDiffExpression($date1, $date2)
151
    {
152 3
        return sprintf('TRUNC(%s) - TRUNC(%s)', $date1, $date2);
153
    }
154
155
    /**
156
     * {@inheritDoc}
157
     */
158 785
    public function getBitAndComparisonExpression($value1, $value2)
159
    {
160 785
        return 'BITAND(' . $value1 . ', ' . $value2 . ')';
161
    }
162
163
    /**
164
     * {@inheritDoc}
165
     */
166 761
    public function getBitOrComparisonExpression($value1, $value2)
167
    {
168 761
        return '(' . $value1 . '-' .
169 761
                $this->getBitAndComparisonExpression($value1, $value2)
170 761
                . '+' . $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 1849
    public function getCreateSequenceSQL(Sequence $sequence)
181
    {
182 1849
        return 'CREATE SEQUENCE ' . $sequence->getQuotedName($this) .
183 1849
               ' START WITH ' . $sequence->getInitialValue() .
184 1849
               ' MINVALUE ' . $sequence->getInitialValue() .
185 1849
               ' INCREMENT BY ' . $sequence->getAllocationSize() .
186 1849
               $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 1849
    private function getSequenceCacheSQL(Sequence $sequence)
205
    {
206 1849
        if ($sequence->getCache() === 0) {
207 1611
            return ' NOCACHE';
208
        }
209
210 1848
        if ($sequence->getCache() === 1) {
211 1634
            return ' NOCACHE';
212
        }
213
214 1847
        if ($sequence->getCache() > 1) {
215 1588
            return ' CACHE ' . $sequence->getCache();
216
        }
217
218 1846
        return '';
219
    }
220
221
    /**
222
     * {@inheritDoc}
223
     */
224 1
    public function getSequenceNextValSQL($sequenceName)
225
    {
226 1
        return 'SELECT ' . $sequenceName . '.nextval FROM DUAL';
227
    }
228
229
    /**
230
     * {@inheritDoc}
231
     */
232 2140
    public function getSetTransactionIsolationSQL($level)
233
    {
234 2140
        return 'SET TRANSACTION ISOLATION LEVEL ' . $this->_getTransactionIsolationLevelSQL($level);
235
    }
236
237
    /**
238
     * {@inheritDoc}
239
     */
240 2140
    protected function _getTransactionIsolationLevelSQL($level)
241
    {
242 1
        switch ($level) {
243 2139
            case TransactionIsolationLevel::READ_UNCOMMITTED:
244 2140
                return 'READ UNCOMMITTED';
245 2139
            case TransactionIsolationLevel::READ_COMMITTED:
246 2140
                return 'READ COMMITTED';
247 2139
            case TransactionIsolationLevel::REPEATABLE_READ:
248 2139
            case TransactionIsolationLevel::SERIALIZABLE:
249 2140
                return 'SERIALIZABLE';
250
            default:
251
                return parent::_getTransactionIsolationLevelSQL($level);
252
        }
253
    }
254
255
    /**
256
     * {@inheritDoc}
257
     */
258 753
    public function getBooleanTypeDeclarationSQL(array $field)
259
    {
260 753
        return 'NUMBER(1)';
261
    }
262
263
    /**
264
     * {@inheritDoc}
265
     */
266 2238
    public function getIntegerTypeDeclarationSQL(array $field)
267
    {
268 2238
        return 'NUMBER(10)';
269
    }
270
271
    /**
272
     * {@inheritDoc}
273
     */
274 15
    public function getBigIntTypeDeclarationSQL(array $field)
275
    {
276 15
        return 'NUMBER(20)';
277
    }
278
279
    /**
280
     * {@inheritDoc}
281
     */
282 1
    public function getSmallIntTypeDeclarationSQL(array $field)
283
    {
284 1
        return 'NUMBER(5)';
285
    }
286
287
    /**
288
     * {@inheritDoc}
289
     */
290 24
    public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
291
    {
292 24
        return 'TIMESTAMP(0)';
293
    }
294
295
    /**
296
     * {@inheritDoc}
297
     */
298 16
    public function getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration)
299
    {
300 16
        return 'TIMESTAMP(0) WITH TIME ZONE';
301
    }
302
303
    /**
304
     * {@inheritDoc}
305
     */
306 20
    public function getDateTypeDeclarationSQL(array $fieldDeclaration)
307
    {
308 20
        return 'DATE';
309
    }
310
311
    /**
312
     * {@inheritDoc}
313
     */
314 18
    public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
315
    {
316 18
        return 'DATE';
317
    }
318
319
    /**
320
     * {@inheritDoc}
321
     */
322
    protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
323
    {
324
        return '';
325
    }
326
327
    /**
328
     * {@inheritDoc}
329
     */
330 2160
    protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
331
    {
332 2160
        return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(2000)')
333 2160
                : ($length ? 'VARCHAR2(' . $length . ')' : 'VARCHAR2(4000)');
334
    }
335
336
    /**
337
     * {@inheritdoc}
338
     */
339 1751
    protected function getBinaryTypeDeclarationSQLSnippet($length, $fixed)
340
    {
341 1751
        return 'RAW(' . ($length ?: $this->getBinaryMaxLength()) . ')';
342
    }
343
344
    /**
345
     * {@inheritdoc}
346
     */
347 1753
    public function getBinaryMaxLength()
348
    {
349 1753
        return 2000;
350
    }
351
352
    /**
353
     * {@inheritDoc}
354
     */
355 644
    public function getClobTypeDeclarationSQL(array $field)
356
    {
357 644
        return 'CLOB';
358
    }
359
360
    /**
361
     * {@inheritDoc}
362
     */
363 2
    public function getListDatabasesSQL()
364
    {
365 2
        return 'SELECT username FROM all_users';
366
    }
367
368
    /**
369
     * {@inheritDoc}
370
     */
371 1363
    public function getListSequencesSQL($database)
372
    {
373 1363
        $database = $this->normalizeIdentifier($database);
374 1363
        $database = $this->quoteStringLiteral($database->getName());
375
376
        return 'SELECT sequence_name, min_value, increment_by FROM sys.all_sequences ' .
377 1363
               'WHERE SEQUENCE_OWNER = ' . $database;
378
    }
379
380
    /**
381
     * {@inheritDoc}
382
     */
383 1974
    protected function _getCreateTableSQL($table, array $columns, array $options = [])
384
    {
385 1974
        $indexes            = $options['indexes'] ?? [];
386 1974
        $options['indexes'] = [];
387 1974
        $sql                = parent::_getCreateTableSQL($table, $columns, $options);
388
389 1974
        foreach ($columns as $name => $column) {
390 1974
            if (isset($column['sequence'])) {
391
                $sql[] = $this->getCreateSequenceSQL($column['sequence']);
392
            }
393
394 1974
            if (! isset($column['autoincrement']) || ! $column['autoincrement'] &&
395 1974
               (! isset($column['autoinc']) || ! $column['autoinc'])) {
396 1098
                continue;
397
            }
398
399 1843
            $sql = array_merge($sql, $this->getCreateAutoincrementSql($name, $table));
400
        }
401
402 1974
        if (isset($indexes) && ! empty($indexes)) {
403 937
            foreach ($indexes as $index) {
404 937
                $sql[] = $this->getCreateIndexSQL($index, $table);
405
            }
406
        }
407
408 1974
        return $sql;
409
    }
410
411
    /**
412
     * {@inheritDoc}
413
     *
414
     * @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaOracleReader.html
415
     */
416 1381
    public function getListTableIndexesSQL($table, $currentDatabase = null)
417
    {
418 1381
        $table = $this->normalizeIdentifier($table);
419 1381
        $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 1381
             WHERE     uind_col.table_name = " . $table . '
447
             ORDER BY  uind_col.column_position ASC';
448
    }
449
450
    /**
451
     * {@inheritDoc}
452
     */
453 64
    public function getListTablesSQL()
454
    {
455 64
        return 'SELECT * FROM sys.user_tables';
456
    }
457
458
    /**
459
     * {@inheritDoc}
460
     */
461 1
    public function getListViewsSQL($database)
462
    {
463 1
        return 'SELECT view_name, text FROM sys.user_views';
464
    }
465
466
    /**
467
     * {@inheritDoc}
468
     */
469 1
    public function getCreateViewSQL($name, $sql)
470
    {
471 1
        return 'CREATE VIEW ' . $name . ' AS ' . $sql;
472
    }
473
474
    /**
475
     * {@inheritDoc}
476
     */
477 1
    public function getDropViewSQL($name)
478
    {
479 1
        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 1843
    public function getCreateAutoincrementSql($name, $table, $start = 1)
490
    {
491 1843
        $tableIdentifier   = $this->normalizeIdentifier($table);
492 1843
        $quotedTableName   = $tableIdentifier->getQuotedName($this);
493 1843
        $unquotedTableName = $tableIdentifier->getName();
494
495 1843
        $nameIdentifier = $this->normalizeIdentifier($name);
496 1843
        $quotedName     = $nameIdentifier->getQuotedName($this);
497 1843
        $unquotedName   = $nameIdentifier->getName();
498
499 1843
        $sql = [];
500
501 1843
        $autoincrementIdentifierName = $this->getAutoincrementIdentifierName($tableIdentifier);
502
503 1843
        $idx = new Index($autoincrementIdentifierName, [$quotedName], true, true);
504
505 1843
        $sql[] = 'DECLARE
506
  constraints_Count NUMBER;
507
BEGIN
508 1843
  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 1843
    EXECUTE IMMEDIATE \'' . $this->getCreateConstraintSQL($idx, $quotedTableName) . '\';
511
  END IF;
512
END;';
513
514 1843
        $sequenceName = $this->getIdentitySequenceName(
515 1843
            $tableIdentifier->isQuoted() ? $quotedTableName : $unquotedTableName,
516 1843
            $nameIdentifier->isQuoted() ? $quotedName : $unquotedName
517
        );
518 1843
        $sequence     = new Sequence($sequenceName, $start);
519 1843
        $sql[]        = $this->getCreateSequenceSQL($sequence);
520
521 1843
        $sql[] = 'CREATE TRIGGER ' . $autoincrementIdentifierName . '
522
   BEFORE INSERT
523 1843
   ON ' . $quotedTableName . '
524
   FOR EACH ROW
525
DECLARE
526
   last_Sequence NUMBER;
527
   last_InsertID NUMBER;
528
BEGIN
529 1843
   SELECT ' . $sequenceName . '.NEXTVAL INTO :NEW.' . $quotedName . ' FROM DUAL;
530 1843
   IF (:NEW.' . $quotedName . ' IS NULL OR :NEW.' . $quotedName . ' = 0) THEN
531 1843
      SELECT ' . $sequenceName . '.NEXTVAL INTO :NEW.' . $quotedName . ' FROM DUAL;
532
   ELSE
533
      SELECT NVL(Last_Number, 0) INTO last_Sequence
534
        FROM User_Sequences
535 1843
       WHERE Sequence_Name = \'' . $sequence->getName() . '\';
536 1843
      SELECT :NEW.' . $quotedName . ' INTO last_InsertID FROM DUAL;
537
      WHILE (last_InsertID > last_Sequence) LOOP
538 1843
         SELECT ' . $sequenceName . '.NEXTVAL INTO last_Sequence FROM DUAL;
539
      END LOOP;
540
   END IF;
541
END;';
542
543 1843
        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 1663
    public function getDropAutoincrementSql($table)
554
    {
555 1663
        $table                       = $this->normalizeIdentifier($table);
556 1663
        $autoincrementIdentifierName = $this->getAutoincrementIdentifierName($table);
557 1663
        $identitySequenceName        = $this->getIdentitySequenceName(
558 1663
            $table->isQuoted() ? $table->getQuotedName($this) : $table->getName(),
559 1663
            ''
560
        );
561
562
        return [
563 1663
            'DROP TRIGGER ' . $autoincrementIdentifierName,
564 1663
            $this->getDropSequenceSQL($identitySequenceName),
565 1663
            $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 1986
    private function normalizeIdentifier($name)
580
    {
581 1986
        $identifier = new Identifier($name);
582
583 1986
        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 1960
    private function getAutoincrementIdentifierName(Identifier $table)
597
    {
598 1960
        $identifierName = $table->getName() . '_AI_PK';
599
600 1960
        return $table->isQuoted()
601 1521
            ? $this->quoteSingleIdentifier($identifierName)
602 1960
            : $identifierName;
603
    }
604
605
    /**
606
     * {@inheritDoc}
607
     */
608 1355
    public function getListTableForeignKeysSQL($table)
609
    {
610 1355
        $table = $this->normalizeIdentifier($table);
611 1355
        $table = $this->quoteStringLiteral($table->getName());
612
613
        return "SELECT alc.constraint_name,
614
          alc.DELETE_RULE,
615
          cols.column_name \"local_column\",
616
          cols.position,
617
          (
618
              SELECT r_cols.table_name
619
              FROM   user_cons_columns r_cols
620
              WHERE  alc.r_constraint_name = r_cols.constraint_name
621
              AND    r_cols.position = cols.position
622
          ) AS \"references_table\",
623
          (
624
              SELECT r_cols.column_name
625
              FROM   user_cons_columns r_cols
626
              WHERE  alc.r_constraint_name = r_cols.constraint_name
627
              AND    r_cols.position = cols.position
628
          ) AS \"foreign_column\"
629
     FROM user_cons_columns cols
630
     JOIN user_constraints alc
631
       ON alc.constraint_name = cols.constraint_name
632
      AND alc.constraint_type = 'R'
633 1355
      AND alc.table_name = " . $table . '
634
    ORDER BY cols.constraint_name ASC, cols.position ASC';
635
    }
636
637
    /**
638
     * {@inheritDoc}
639
     */
640 1289
    public function getListTableConstraintsSQL($table)
641
    {
642 1289
        $table = $this->normalizeIdentifier($table);
643 1289
        $table = $this->quoteStringLiteral($table->getName());
644
645 1289
        return 'SELECT * FROM user_constraints WHERE table_name = ' . $table;
646
    }
647
648
    /**
649
     * {@inheritDoc}
650
     */
651 1484
    public function getListTableColumnsSQL($table, $database = null)
652
    {
653 1484
        $table = $this->normalizeIdentifier($table);
654 1484
        $table = $this->quoteStringLiteral($table->getName());
655
656 1484
        $tabColumnsTableName       = 'user_tab_columns';
657 1484
        $colCommentsTableName      = 'user_col_comments';
658 1484
        $tabColumnsOwnerCondition  = '';
659 1484
        $colCommentsOwnerCondition = '';
660
661 1484
        if ($database !== null && $database !== '/') {
662 1435
            $database                  = $this->normalizeIdentifier($database);
663 1435
            $database                  = $this->quoteStringLiteral($database->getName());
664 1435
            $tabColumnsTableName       = 'all_tab_columns';
665 1435
            $colCommentsTableName      = 'all_col_comments';
666 1435
            $tabColumnsOwnerCondition  = ' AND c.owner = ' . $database;
667 1435
            $colCommentsOwnerCondition = ' AND d.OWNER = c.OWNER';
668
        }
669
670 1484
        return sprintf(
671
            <<<'SQL'
672 58
SELECT   c.*,
673
         (
674
             SELECT d.comments
675
             FROM   %s d
676
             WHERE  d.TABLE_NAME = c.TABLE_NAME%s
677
             AND    d.COLUMN_NAME = c.COLUMN_NAME
678
         ) AS comments
679
FROM     %s c
680
WHERE    c.table_name = %s%s
681
ORDER BY c.column_id
682
SQL
683
            ,
684 1484
            $colCommentsTableName,
685 1484
            $colCommentsOwnerCondition,
686 1484
            $tabColumnsTableName,
687 1484
            $table,
688 1484
            $tabColumnsOwnerCondition
689
        );
690
    }
691
692
    /**
693
     * {@inheritDoc}
694
     */
695 1663
    public function getDropSequenceSQL($sequence)
696
    {
697 1663
        if ($sequence instanceof Sequence) {
698
            $sequence = $sequence->getQuotedName($this);
699
        }
700
701 1663
        return 'DROP SEQUENCE ' . $sequence;
702
    }
703
704
    /**
705
     * {@inheritDoc}
706
     */
707 210
    public function getDropForeignKeySQL($foreignKey, $table)
708
    {
709 210
        if (! $foreignKey instanceof ForeignKeyConstraint) {
710 208
            $foreignKey = new Identifier($foreignKey);
711
        }
712
713 210
        if (! $table instanceof Table) {
714 210
            $table = new Identifier($table);
715
        }
716
717 210
        $foreignKey = $foreignKey->getQuotedName($this);
718 210
        $table      = $table->getQuotedName($this);
719
720 210
        return 'ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $foreignKey;
721
    }
722
723
    /**
724
     * {@inheritdoc}
725
     */
726 2069
    public function getAdvancedForeignKeyOptionsSQL(ForeignKeyConstraint $foreignKey)
727
    {
728 2069
        $referentialAction = null;
729
730 2069
        if ($foreignKey->hasOption('onDelete')) {
731 2029
            $referentialAction = $this->getForeignKeyReferentialActionSQL($foreignKey->getOption('onDelete'));
732
        }
733
734 2069
        return $referentialAction ? ' ON DELETE ' . $referentialAction : '';
735
    }
736
737
    /**
738
     * {@inheritdoc}
739
     */
740 2035
    public function getForeignKeyReferentialActionSQL($action)
741
    {
742 2035
        $action = strtoupper($action);
743
744 11
        switch ($action) {
745 2035
            case 'RESTRICT': // RESTRICT is not supported, therefore falling back to NO ACTION.
746 2033
            case 'NO ACTION':
747
                // NO ACTION cannot be declared explicitly,
748
                // therefore returning empty string to indicate to OMIT the referential clause.
749 2005
                return '';
750
751 2031
            case 'CASCADE':
752 1958
            case 'SET NULL':
753 2030
                return $action;
754
755
            default:
756
                // SET DEFAULT is not supported, throw exception instead.
757 1059
                throw new InvalidArgumentException('Invalid foreign key action: ' . $action);
758
        }
759
    }
760
761
    /**
762
     * {@inheritDoc}
763
     */
764 2119
    public function getDropDatabaseSQL($database)
765
    {
766 2119
        return 'DROP USER ' . $database . ' CASCADE';
767
    }
768
769
    /**
770
     * {@inheritDoc}
771
     */
772 1827
    public function getAlterTableSQL(TableDiff $diff)
773
    {
774 1827
        $sql         = [];
775 1827
        $commentsSQL = [];
776 1827
        $columnSql   = [];
777
778 1827
        $fields = [];
779
780 1827
        foreach ($diff->addedColumns as $column) {
781 741
            if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
782
                continue;
783
            }
784
785 741
            $fields[] = $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
786 741
            $comment  = $this->getColumnComment($column);
787
788 741
            if (! $comment) {
789 740
                continue;
790
            }
791
792 622
            $commentsSQL[] = $this->getCommentOnColumnSQL(
793 622
                $diff->getName($this)->getQuotedName($this),
794 622
                $column->getQuotedName($this),
795
                $comment
796
            );
797
        }
798
799 1827
        if (count($fields)) {
800 741
            $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ADD (' . implode(', ', $fields) . ')';
801
        }
802
803 1827
        $fields = [];
804 1827
        foreach ($diff->changedColumns as $columnDiff) {
805 1816
            if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) {
806
                continue;
807
            }
808
809 1816
            $column = $columnDiff->column;
810
811
            // Do not generate column alteration clause if type is binary and only fixed property has changed.
812
            // Oracle only supports binary type columns with variable length.
813
            // Avoids unnecessary table alteration statements.
814 1816
            if ($column->getType() instanceof BinaryType &&
815 1816
                $columnDiff->hasChanged('fixed') &&
816 1816
                count($columnDiff->changedProperties) === 1
817
            ) {
818 1703
                continue;
819
            }
820
821 1815
            $columnHasChangedComment = $columnDiff->hasChanged('comment');
822
823
            /**
824
             * Do not add query part if only comment has changed
825
             */
826 1815
            if (! ($columnHasChangedComment && count($columnDiff->changedProperties) === 1)) {
827 1803
                $columnInfo = $column->toArray();
828
829 1803
                if (! $columnDiff->hasChanged('notnull')) {
830 1801
                    unset($columnInfo['notnull']);
831
                }
832
833 1803
                $fields[] = $column->getQuotedName($this) . $this->getColumnDeclarationSQL('', $columnInfo);
834
            }
835
836 1815
            if (! $columnHasChangedComment) {
837 1803
                continue;
838
            }
839
840 1484
            $commentsSQL[] = $this->getCommentOnColumnSQL(
841 1484
                $diff->getName($this)->getQuotedName($this),
842 1484
                $column->getQuotedName($this),
843 1484
                $this->getColumnComment($column)
844
            );
845
        }
846
847 1827
        if (count($fields)) {
848 1803
            $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' MODIFY (' . implode(', ', $fields) . ')';
849
        }
850
851 1827
        foreach ($diff->renamedColumns as $oldColumnName => $column) {
852 672
            if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) {
853
                continue;
854
            }
855
856 672
            $oldColumnName = new Identifier($oldColumnName);
857
858 672
            $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) .
859 672
                ' RENAME COLUMN ' . $oldColumnName->getQuotedName($this) . ' TO ' . $column->getQuotedName($this);
860
        }
861
862 1827
        $fields = [];
863 1827
        foreach ($diff->removedColumns as $column) {
864 740
            if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) {
865
                continue;
866
            }
867
868 740
            $fields[] = $column->getQuotedName($this);
869
        }
870
871 1827
        if (count($fields)) {
872 740
            $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' DROP (' . implode(', ', $fields) . ')';
873
        }
874
875 1827
        $tableSql = [];
876
877 1827
        if (! $this->onSchemaAlterTable($diff, $tableSql)) {
878 1827
            $sql = array_merge($sql, $commentsSQL);
879
880 1827
            $newName = $diff->getNewName();
881
882 1827
            if ($newName !== false) {
883 739
                $sql[] = sprintf(
884 3
                    'ALTER TABLE %s RENAME TO %s',
885 739
                    $diff->getName($this)->getQuotedName($this),
886 739
                    $newName->getQuotedName($this)
887
                );
888
            }
889
890 1827
            $sql = array_merge(
891 1827
                $this->getPreAlterTableIndexForeignKeySQL($diff),
892 1827
                $sql,
893 1827
                $this->getPostAlterTableIndexForeignKeySQL($diff)
894
            );
895
        }
896
897 1827
        return array_merge($sql, $tableSql, $columnSql);
898
    }
899
900
    /**
901
     * {@inheritdoc}
902
     */
903 1982
    public function getColumnDeclarationSQL($name, array $field)
904
    {
905 1982
        if (isset($field['columnDefinition'])) {
906 714
            $columnDef = $this->getCustomTypeDeclarationSQL($field);
907
        } else {
908 1981
            $default = $this->getDefaultValueDeclarationSQL($field);
909
910 1981
            $notnull = '';
911
912 1981
            if (isset($field['notnull'])) {
913 1979
                $notnull = $field['notnull'] ? ' NOT NULL' : ' NULL';
914
            }
915
916 1981
            $unique = isset($field['unique']) && $field['unique'] ?
917 1981
                ' ' . $this->getUniqueFieldDeclarationSQL() : '';
918
919 1981
            $check = isset($field['check']) && $field['check'] ?
920 1981
                ' ' . $field['check'] : '';
921
922 1981
            $typeDecl  = $field['type']->getSQLDeclaration($field, $this);
923 1981
            $columnDef = $typeDecl . $default . $notnull . $unique . $check;
924
        }
925
926 1982
        return $name . ' ' . $columnDef;
927
    }
928
929
    /**
930
     * {@inheritdoc}
931
     */
932 329
    protected function getRenameIndexSQL($oldIndexName, Index $index, $tableName)
933
    {
934 329
        if (strpos($tableName, '.') !== false) {
935 255
            [$schema]     = explode('.', $tableName);
936 255
            $oldIndexName = $schema . '.' . $oldIndexName;
937
        }
938
939 329
        return ['ALTER INDEX ' . $oldIndexName . ' RENAME TO ' . $index->getQuotedName($this)];
940
    }
941
942
    /**
943
     * {@inheritDoc}
944
     */
945
    public function prefersSequences()
946
    {
947
        return true;
948
    }
949
950
    /**
951
     * {@inheritdoc}
952
     */
953 1681
    public function usesSequenceEmulatedIdentityColumns()
954
    {
955 1681
        return true;
956
    }
957
958
    /**
959
     * {@inheritdoc}
960
     */
961 1961
    public function getIdentitySequenceName($tableName, $columnName)
962
    {
963 1961
        $table = new Identifier($tableName);
964
965
        // No usage of column name to preserve BC compatibility with <2.5
966 1961
        $identitySequenceName = $table->getName() . '_SEQ';
967
968 1961
        if ($table->isQuoted()) {
969 1660
            $identitySequenceName = '"' . $identitySequenceName . '"';
970
        }
971
972 1961
        $identitySequenceIdentifier = $this->normalizeIdentifier($identitySequenceName);
973
974 1961
        return $identitySequenceIdentifier->getQuotedName($this);
975
    }
976
977
    /**
978
     * {@inheritDoc}
979
     */
980 1975
    public function supportsCommentOnStatement()
981
    {
982 1975
        return true;
983
    }
984
985
    /**
986
     * {@inheritDoc}
987
     */
988 1285
    public function getName()
989
    {
990 1285
        return 'oracle';
991
    }
992
993
    /**
994
     * {@inheritDoc}
995
     */
996 1947
    protected function doModifyLimitQuery($query, $limit, $offset = null)
997
    {
998 1947
        if ($limit === null && $offset <= 0) {
999 25
            return $query;
1000
        }
1001
1002 1945
        if (preg_match('/^\s*SELECT/i', $query)) {
1003 1945
            if (! preg_match('/\sFROM\s/i', $query)) {
1004
                $query .= ' FROM dual';
1005
            }
1006
1007 1945
            $columns = ['a.*'];
1008
1009 1945
            if ($offset > 0) {
1010 1895
                $columns[] = 'ROWNUM AS doctrine_rownum';
1011
            }
1012
1013 1945
            $query = sprintf('SELECT %s FROM (%s) a', implode(', ', $columns), $query);
1014
1015 1945
            if ($limit !== null) {
1016 1944
                $query .= sprintf(' WHERE ROWNUM <= %d', $offset + $limit);
1017
            }
1018
1019 1945
            if ($offset > 0) {
1020 1895
                $query = sprintf('SELECT * FROM (%s) WHERE doctrine_rownum >= %d', $query, $offset + 1);
1021
            }
1022
        }
1023
1024 1945
        return $query;
1025
    }
1026
1027
    /**
1028
     * {@inheritDoc}
1029
     *
1030
     * Oracle returns all column names in SQL result sets in uppercase.
1031
     */
1032
    public function getSQLResultCasing($column)
1033
    {
1034
        return strtoupper($column);
1035
    }
1036
1037
    /**
1038
     * {@inheritDoc}
1039
     */
1040
    public function getCreateTemporaryTableSnippetSQL()
1041
    {
1042
        return 'CREATE GLOBAL TEMPORARY TABLE';
1043
    }
1044
1045
    /**
1046
     * {@inheritDoc}
1047
     */
1048 1
    public function getDateTimeTzFormatString()
1049
    {
1050 1
        return 'Y-m-d H:i:sP';
1051
    }
1052
1053
    /**
1054
     * {@inheritDoc}
1055
     */
1056 1
    public function getDateFormatString()
1057
    {
1058 1
        return 'Y-m-d 00:00:00';
1059
    }
1060
1061
    /**
1062
     * {@inheritDoc}
1063
     */
1064 1
    public function getTimeFormatString()
1065
    {
1066 1
        return '1900-01-01 H:i:s';
1067
    }
1068
1069
    /**
1070
     * {@inheritDoc}
1071
     */
1072
    public function fixSchemaElementName($schemaElementName)
1073
    {
1074
        if (strlen($schemaElementName) > 30) {
1075
            // Trim it
1076
            return substr($schemaElementName, 0, 30);
1077
        }
1078
1079
        return $schemaElementName;
1080
    }
1081
1082
    /**
1083
     * {@inheritDoc}
1084
     */
1085 13
    public function getMaxIdentifierLength()
1086
    {
1087 13
        return 30;
1088
    }
1089
1090
    /**
1091
     * {@inheritDoc}
1092
     */
1093 6
    public function supportsSequences()
1094
    {
1095 6
        return true;
1096
    }
1097
1098
    /**
1099
     * {@inheritDoc}
1100
     */
1101
    public function supportsForeignKeyOnUpdate()
1102
    {
1103
        return false;
1104
    }
1105
1106
    /**
1107
     * {@inheritDoc}
1108
     */
1109 1
    public function supportsReleaseSavepoints()
1110
    {
1111 1
        return false;
1112
    }
1113
1114
    /**
1115
     * {@inheritDoc}
1116
     */
1117 447
    public function getTruncateTableSQL($tableName, $cascade = false)
1118
    {
1119 447
        $tableIdentifier = new Identifier($tableName);
1120
1121 447
        return 'TRUNCATE TABLE ' . $tableIdentifier->getQuotedName($this);
1122
    }
1123
1124
    /**
1125
     * {@inheritDoc}
1126
     */
1127 9
    public function getDummySelectSQL()
1128
    {
1129 9
        $expression = func_num_args() > 0 ? func_get_arg(0) : '1';
1130
1131 9
        return sprintf('SELECT %s FROM DUAL', $expression);
1132
    }
1133
1134
    /**
1135
     * {@inheritDoc}
1136
     */
1137 1777
    protected function initializeDoctrineTypeMappings()
1138
    {
1139 1777
        $this->doctrineTypeMapping = [
1140
            'integer'           => 'integer',
1141
            'number'            => 'integer',
1142
            'pls_integer'       => 'boolean',
1143
            'binary_integer'    => 'boolean',
1144
            'varchar'           => 'string',
1145
            'varchar2'          => 'string',
1146
            'nvarchar2'         => 'string',
1147
            'char'              => 'string',
1148
            'nchar'             => 'string',
1149
            'date'              => 'date',
1150
            'timestamp'         => 'datetime',
1151
            'timestamptz'       => 'datetimetz',
1152
            'float'             => 'float',
1153
            'binary_float'      => 'float',
1154
            'binary_double'     => 'float',
1155
            'long'              => 'string',
1156
            'clob'              => 'text',
1157
            'nclob'             => 'text',
1158
            'raw'               => 'binary',
1159
            'long raw'          => 'blob',
1160
            'rowid'             => 'string',
1161
            'urowid'            => 'string',
1162
            'blob'              => 'blob',
1163
        ];
1164 1777
    }
1165
1166
    /**
1167
     * {@inheritDoc}
1168
     */
1169
    public function releaseSavePoint($savepoint)
1170
    {
1171
        return '';
1172
    }
1173
1174
    /**
1175
     * {@inheritDoc}
1176
     */
1177 1879
    protected function getReservedKeywordsClass()
1178
    {
1179 1879
        return Keywords\OracleKeywords::class;
1180
    }
1181
1182
    /**
1183
     * {@inheritDoc}
1184
     */
1185 1731
    public function getBlobTypeDeclarationSQL(array $field)
1186
    {
1187 1731
        return 'BLOB';
1188
    }
1189
1190 41
    public function getListTableCommentsSQL(string $table, ?string $database = null) : string
1191
    {
1192 41
        $tableCommentsName = 'user_tab_comments';
1193 41
        $ownerCondition    = '';
1194
1195 41
        if ($database !== null && $database !== '/') {
1196
            $tableCommentsName = 'all_tab_comments';
1197
            $ownerCondition    = ' AND owner = ' . $this->quoteStringLiteral($this->normalizeIdentifier($database)->getName());
1198
        }
1199
1200 41
        return sprintf(
1201
            <<<'SQL'
1202 41
SELECT comments FROM %s WHERE table_name = %s%s
1203
SQL
1204
            ,
1205 41
            $tableCommentsName,
1206 41
            $this->quoteStringLiteral($this->normalizeIdentifier($table)->getName()),
1207 41
            $ownerCondition
1208
        );
1209
    }
1210
}
1211