Passed
Push — 2.9 ( d389f3...7345cd )
by Sergei
31:44 queued 28:51
created

OraclePlatform   F

Complexity

Total Complexity 163

Size/Duplication

Total Lines 1143
Duplicated Lines 0 %

Test Coverage

Coverage 73.12%

Importance

Changes 0
Metric Value
wmc 163
eloc 359
dl 0
loc 1143
ccs 283
cts 387
cp 0.7312
rs 2
c 0
b 0
f 0

73 Methods

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