Completed
Pull Request — develop (#3524)
by Jonathan
95:41 queued 92:25
created

OraclePlatform::getBitOrComparisonExpression()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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