Failed Conditions
Pull Request — master (#3546)
by Sergei
14:16
created

OraclePlatform::_getCreateTableSQL()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 10.0296

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 26
ccs 14
cts 15
cp 0.9333
rs 7.6666
c 0
b 0
f 0
cc 10
nc 10
nop 3
crap 10.0296

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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