OraclePlatform.php 33.9 KB
Newer Older
1 2
<?php

3
namespace Doctrine\DBAL\Platforms;
4

5
use Doctrine\DBAL\DBALException;
6
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
7
use Doctrine\DBAL\Schema\Identifier;
8 9 10
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
11
use Doctrine\DBAL\Schema\TableDiff;
12
use Doctrine\DBAL\TransactionIsolationLevel;
Steve Müller's avatar
Steve Müller committed
13
use Doctrine\DBAL\Types\BinaryType;
14
use InvalidArgumentException;
15

16 17 18
use function array_merge;
use function count;
use function explode;
19 20
use function func_get_arg;
use function func_num_args;
21 22 23 24 25 26 27
use function implode;
use function preg_match;
use function sprintf;
use function strlen;
use function strpos;
use function strtoupper;
use function substr;
28

romanb's avatar
romanb committed
29
/**
30
 * OraclePlatform.
romanb's avatar
romanb committed
31
 */
32
class OraclePlatform extends AbstractPlatform
33
{
34
    /**
Benjamin Morel's avatar
Benjamin Morel committed
35
     * Assertion for Oracle identifiers.
36 37
     *
     * @link http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements008.htm
38
     *
Benjamin Morel's avatar
Benjamin Morel committed
39
     * @param string $identifier
40
     *
41 42
     * @return void
     *
43 44
     * @throws DBALException
     */
45
    public static function assertValidIdentifier($identifier)
46
    {
47 48
        if (! preg_match('(^(([a-zA-Z]{1}[a-zA-Z0-9_$#]{0,})|("[^"]+"))$)', $identifier)) {
            throw new DBALException('Invalid Oracle identifier');
49 50 51
        }
    }

52
    /**
53
     * {@inheritDoc}
54 55 56
     */
    public function getSubstringExpression($value, $position, $length = null)
    {
57
        if ($length !== null) {
58
            return sprintf('SUBSTR(%s, %d, %d)', $value, $position, $length);
59
        }
60

61
        return sprintf('SUBSTR(%s, %d)', $value, $position);
62 63 64
    }

    /**
65
     * @param string $type
66 67
     *
     * @return string
68 69 70 71 72 73 74 75 76 77 78 79
     */
    public function getNowExpression($type = 'timestamp')
    {
        switch ($type) {
            case 'date':
            case 'time':
            case 'timestamp':
            default:
                return 'TO_CHAR(CURRENT_TIMESTAMP, \'YYYY-MM-DD HH24:MI:SS\')';
        }
    }

80
    /**
81
     * {@inheritDoc}
82 83 84
     */
    public function getLocateExpression($str, $substr, $startPos = false)
    {
85 86
        if ($startPos === false) {
            return 'INSTR(' . $str . ', ' . $substr . ')';
87
        }
88

89
        return 'INSTR(' . $str . ', ' . $substr . ', ' . $startPos . ')';
90 91
    }

92
    /**
93
     * {@inheritDoc}
94 95
     *
     * @deprecated Use application-generated UUIDs instead
96 97 98 99 100
     */
    public function getGuidExpression()
    {
        return 'SYS_GUID()';
    }
101

102
    /**
103
     * {@inheritdoc}
104
     */
105
    protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit)
106
    {
107
        switch ($unit) {
108 109 110
            case DateIntervalUnit::MONTH:
            case DateIntervalUnit::QUARTER:
            case DateIntervalUnit::YEAR:
111
                switch ($unit) {
112
                    case DateIntervalUnit::QUARTER:
113 114 115
                        $interval *= 3;
                        break;

116
                    case DateIntervalUnit::YEAR:
117 118 119 120 121
                        $interval *= 12;
                        break;
                }

                return 'ADD_MONTHS(' . $date . ', ' . $operator . $interval . ')';
122

123 124
            default:
                $calculationClause = '';
125

126
                switch ($unit) {
127
                    case DateIntervalUnit::SECOND:
128 129
                        $calculationClause = '/24/60/60';
                        break;
130

131
                    case DateIntervalUnit::MINUTE:
132 133
                        $calculationClause = '/24/60';
                        break;
134

135
                    case DateIntervalUnit::HOUR:
136 137
                        $calculationClause = '/24';
                        break;
138

139
                    case DateIntervalUnit::WEEK:
140 141 142 143 144 145
                        $calculationClause = '*7';
                        break;
                }

                return '(' . $date . $operator . $interval . $calculationClause . ')';
        }
146 147
    }

148
    /**
149
     * {@inheritDoc}
150
     */
151
    public function getDateDiffExpression($date1, $date2)
152
    {
153
        return sprintf('TRUNC(%s) - TRUNC(%s)', $date1, $date2);
154
    }
Fabio B. Silva's avatar
Fabio B. Silva committed
155

156
    /**
157
     * {@inheritDoc}
158 159 160
     */
    public function getBitAndComparisonExpression($value1, $value2)
    {
161
        return 'BITAND(' . $value1 . ', ' . $value2 . ')';
162 163 164
    }

    /**
165
     * {@inheritDoc}
166 167 168
     */
    public function getBitOrComparisonExpression($value1, $value2)
    {
169 170
        return '(' . $value1 . '-' .
                $this->getBitAndComparisonExpression($value1, $value2)
171 172
                . '+' . $value2 . ')';
    }
173

174
    /**
175
     * {@inheritDoc}
176
     *
177 178 179
     * Need to specifiy minvalue, since start with is hidden in the system and MINVALUE <= START WITH.
     * Therefore we can use MINVALUE to be able to get a hint what START WITH was for later introspection
     * in {@see listSequences()}
180
     */
181
    public function getCreateSequenceSQL(Sequence $sequence)
182
    {
183
        return 'CREATE SEQUENCE ' . $sequence->getQuotedName($this) .
184
               ' START WITH ' . $sequence->getInitialValue() .
185
               ' MINVALUE ' . $sequence->getInitialValue() .
186 187
               ' INCREMENT BY ' . $sequence->getAllocationSize() .
               $this->getSequenceCacheSQL($sequence);
188
    }
189

190 191 192
    /**
     * {@inheritDoc}
     */
jeroendedauw's avatar
jeroendedauw committed
193
    public function getAlterSequenceSQL(Sequence $sequence)
194
    {
195
        return 'ALTER SEQUENCE ' . $sequence->getQuotedName($this) .
196 197 198 199 200 201 202 203 204
               ' INCREMENT BY ' . $sequence->getAllocationSize()
               . $this->getSequenceCacheSQL($sequence);
    }

    /**
     * Cache definition for sequences
     *
     * @return string
     */
jeroendedauw's avatar
jeroendedauw committed
205
    private function getSequenceCacheSQL(Sequence $sequence)
206 207 208
    {
        if ($sequence->getCache() === 0) {
            return ' NOCACHE';
209 210 211
        }

        if ($sequence->getCache() === 1) {
212
            return ' NOCACHE';
213 214 215
        }

        if ($sequence->getCache() > 1) {
216 217 218 219
            return ' CACHE ' . $sequence->getCache();
        }

        return '';
220
    }
221

romanb's avatar
romanb committed
222
    /**
223
     * {@inheritDoc}
romanb's avatar
romanb committed
224
     */
225
    public function getSequenceNextValSQL($sequence)
romanb's avatar
romanb committed
226
    {
227
        return 'SELECT ' . $sequence . '.nextval FROM DUAL';
romanb's avatar
romanb committed
228
    }
229

romanb's avatar
romanb committed
230
    /**
231
     * {@inheritDoc}
romanb's avatar
romanb committed
232
     */
233
    public function getSetTransactionIsolationSQL($level)
romanb's avatar
romanb committed
234
    {
235
        return 'SET TRANSACTION ISOLATION LEVEL ' . $this->_getTransactionIsolationLevelSQL($level);
romanb's avatar
romanb committed
236
    }
237

238 239 240
    /**
     * {@inheritDoc}
     */
241
    protected function _getTransactionIsolationLevelSQL($level)
romanb's avatar
romanb committed
242 243
    {
        switch ($level) {
244
            case TransactionIsolationLevel::READ_UNCOMMITTED:
245
                return 'READ UNCOMMITTED';
Sergei Morozov's avatar
Sergei Morozov committed
246

247
            case TransactionIsolationLevel::READ_COMMITTED:
248
                return 'READ COMMITTED';
Sergei Morozov's avatar
Sergei Morozov committed
249

250 251
            case TransactionIsolationLevel::REPEATABLE_READ:
            case TransactionIsolationLevel::SERIALIZABLE:
romanb's avatar
romanb committed
252
                return 'SERIALIZABLE';
Sergei Morozov's avatar
Sergei Morozov committed
253

romanb's avatar
romanb committed
254
            default:
255
                return parent::_getTransactionIsolationLevelSQL($level);
romanb's avatar
romanb committed
256 257
        }
    }
258

259
    /**
260
     * {@inheritDoc}
261
     */
262
    public function getBooleanTypeDeclarationSQL(array $column)
263 264 265
    {
        return 'NUMBER(1)';
    }
266

267
    /**
268
     * {@inheritDoc}
269
     */
270
    public function getIntegerTypeDeclarationSQL(array $column)
271 272 273 274 275
    {
        return 'NUMBER(10)';
    }

    /**
276
     * {@inheritDoc}
277
     */
278
    public function getBigIntTypeDeclarationSQL(array $column)
279 280 281 282 283
    {
        return 'NUMBER(20)';
    }

    /**
284
     * {@inheritDoc}
285
     */
286
    public function getSmallIntTypeDeclarationSQL(array $column)
287 288 289 290
    {
        return 'NUMBER(5)';
    }

291
    /**
292
     * {@inheritDoc}
293
     */
294
    public function getDateTimeTypeDeclarationSQL(array $column)
295 296 297 298 299
    {
        return 'TIMESTAMP(0)';
    }

    /**
300
     * {@inheritDoc}
301
     */
302
    public function getDateTimeTzTypeDeclarationSQL(array $column)
303
    {
304
        return 'TIMESTAMP(0) WITH TIME ZONE';
305 306
    }

307
    /**
308
     * {@inheritDoc}
309
     */
310
    public function getDateTypeDeclarationSQL(array $column)
311 312 313 314 315
    {
        return 'DATE';
    }

    /**
316
     * {@inheritDoc}
317
     */
318
    public function getTimeTypeDeclarationSQL(array $column)
319 320 321 322
    {
        return 'DATE';
    }

323
    /**
324
     * {@inheritDoc}
325
     */
326
    protected function _getCommonIntegerTypeDeclarationSQL(array $column)
327 328 329 330 331
    {
        return '';
    }

    /**
332
     * {@inheritDoc}
333
     */
334 335
    protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
    {
336 337 338
        return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(2000)')
                : ($length ? 'VARCHAR2(' . $length . ')' : 'VARCHAR2(4000)');
    }
339

Steve Müller's avatar
Steve Müller committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
    /**
     * {@inheritdoc}
     */
    protected function getBinaryTypeDeclarationSQLSnippet($length, $fixed)
    {
        return 'RAW(' . ($length ?: $this->getBinaryMaxLength()) . ')';
    }

    /**
     * {@inheritdoc}
     */
    public function getBinaryMaxLength()
    {
        return 2000;
    }

356 357 358
    /**
     * {@inheritDoc}
     */
359
    public function getClobTypeDeclarationSQL(array $column)
360 361 362
    {
        return 'CLOB';
    }
363

Benjamin Morel's avatar
Benjamin Morel committed
364 365 366
    /**
     * {@inheritDoc}
     */
367
    public function getListDatabasesSQL()
368
    {
369
        return 'SELECT username FROM all_users';
370 371
    }

Benjamin Morel's avatar
Benjamin Morel committed
372 373 374
    /**
     * {@inheritDoc}
     */
375
    public function getListSequencesSQL($database)
jwage's avatar
jwage committed
376
    {
377
        $database = $this->normalizeIdentifier($database);
378
        $database = $this->quoteStringLiteral($database->getName());
379

380 381
        return 'SELECT sequence_name, min_value, increment_by FROM sys.all_sequences ' .
               'WHERE SEQUENCE_OWNER = ' . $database;
jwage's avatar
jwage committed
382 383
    }

384
    /**
385
     * {@inheritDoc}
386
     */
387
    protected function _getCreateTableSQL($table, array $columns, array $options = [])
388
    {
Gabriel Caruso's avatar
Gabriel Caruso committed
389
        $indexes            = $options['indexes'] ?? [];
390
        $options['indexes'] = [];
Gabriel Caruso's avatar
Gabriel Caruso committed
391
        $sql                = parent::_getCreateTableSQL($table, $columns, $options);
392 393 394

        foreach ($columns as $name => $column) {
            if (isset($column['sequence'])) {
395
                $sql[] = $this->getCreateSequenceSQL($column['sequence']);
396 397
            }

398 399 400 401
            if (
                ! isset($column['autoincrement']) || ! $column['autoincrement'] &&
                (! isset($column['autoinc']) || ! $column['autoinc'])
            ) {
402
                continue;
403
            }
404 405

            $sql = array_merge($sql, $this->getCreateAutoincrementSql($name, $table));
406
        }
407

408
        if (isset($indexes) && ! empty($indexes)) {
409
            foreach ($indexes as $index) {
410
                $sql[] = $this->getCreateIndexSQL($index, $table);
411 412 413 414 415 416
            }
        }

        return $sql;
    }

417
    /**
418 419
     * {@inheritDoc}
     *
420 421
     * @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaOracleReader.html
     */
422
    public function getListTableIndexesSQL($table, $database = null)
423
    {
424
        $table = $this->normalizeIdentifier($table);
425
        $table = $this->quoteStringLiteral($table->getName());
426

427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
        return "SELECT uind_col.index_name AS name,
                       (
                           SELECT uind.index_type
                           FROM   user_indexes uind
                           WHERE  uind.index_name = uind_col.index_name
                       ) AS type,
                       decode(
                           (
                               SELECT uind.uniqueness
                               FROM   user_indexes uind
                               WHERE  uind.index_name = uind_col.index_name
                           ),
                           'NONUNIQUE',
                           0,
                           'UNIQUE',
                           1
                       ) AS is_unique,
                       uind_col.column_name AS column_name,
                       uind_col.column_position AS column_pos,
                       (
                           SELECT ucon.constraint_type
                           FROM   user_constraints ucon
449
                           WHERE  ucon.index_name = uind_col.index_name
450 451
                       ) AS is_primary
             FROM      user_ind_columns uind_col
452 453
             WHERE     uind_col.table_name = " . $table . '
             ORDER BY  uind_col.column_position ASC';
454 455
    }

Benjamin Morel's avatar
Benjamin Morel committed
456 457 458
    /**
     * {@inheritDoc}
     */
459
    public function getListTablesSQL()
460 461 462 463
    {
        return 'SELECT * FROM sys.user_tables';
    }

464 465 466
    /**
     * {@inheritDoc}
     */
467
    public function getListViewsSQL($database)
468
    {
469
        return 'SELECT view_name, text FROM sys.user_views';
470 471
    }

Benjamin Morel's avatar
Benjamin Morel committed
472 473 474
    /**
     * {@inheritDoc}
     */
475
    public function getCreateViewSQL($name, $sql)
476 477 478 479
    {
        return 'CREATE VIEW ' . $name . ' AS ' . $sql;
    }

Benjamin Morel's avatar
Benjamin Morel committed
480 481 482
    /**
     * {@inheritDoc}
     */
483
    public function getDropViewSQL($name)
484
    {
485
        return 'DROP VIEW ' . $name;
486 487
    }

Benjamin Morel's avatar
Benjamin Morel committed
488
    /**
489 490 491
     * @param string $name
     * @param string $table
     * @param int    $start
Benjamin Morel's avatar
Benjamin Morel committed
492
     *
493
     * @return string[]
Benjamin Morel's avatar
Benjamin Morel committed
494
     */
495 496
    public function getCreateAutoincrementSql($name, $table, $start = 1)
    {
497 498
        $tableIdentifier   = $this->normalizeIdentifier($table);
        $quotedTableName   = $tableIdentifier->getQuotedName($this);
499 500 501
        $unquotedTableName = $tableIdentifier->getName();

        $nameIdentifier = $this->normalizeIdentifier($name);
502 503
        $quotedName     = $nameIdentifier->getQuotedName($this);
        $unquotedName   = $nameIdentifier->getName();
504

505
        $sql = [];
506

507
        $autoincrementIdentifierName = $this->getAutoincrementIdentifierName($tableIdentifier);
508

509
        $idx = new Index($autoincrementIdentifierName, [$quotedName], true, true);
510

Sergei Morozov's avatar
Sergei Morozov committed
511
        $sql[] = "DECLARE
512 513
  constraints_Count NUMBER;
BEGIN
Sergei Morozov's avatar
Sergei Morozov committed
514 515 516 517
  SELECT COUNT(CONSTRAINT_NAME) INTO constraints_Count FROM USER_CONSTRAINTS WHERE TABLE_NAME = '" . $unquotedTableName
            . "' AND CONSTRAINT_TYPE = 'P';
  IF constraints_Count = 0 OR constraints_Count = '' THEN
    EXECUTE IMMEDIATE '" . $this->getCreateConstraintSQL($idx, $quotedTableName) . "';
518
  END IF;
Sergei Morozov's avatar
Sergei Morozov committed
519
END;";
520

521 522 523 524
        $sequenceName = $this->getIdentitySequenceName(
            $tableIdentifier->isQuoted() ? $quotedTableName : $unquotedTableName,
            $nameIdentifier->isQuoted() ? $quotedName : $unquotedName
        );
525 526
        $sequence     = new Sequence($sequenceName, $start);
        $sql[]        = $this->getCreateSequenceSQL($sequence);
527

528
        $sql[] = 'CREATE TRIGGER ' . $autoincrementIdentifierName . '
529
   BEFORE INSERT
530
   ON ' . $quotedTableName . '
531 532 533 534 535
   FOR EACH ROW
DECLARE
   last_Sequence NUMBER;
   last_InsertID NUMBER;
BEGIN
536
   SELECT ' . $sequenceName . '.NEXTVAL INTO :NEW.' . $quotedName . ' FROM DUAL;
537
   IF (:NEW.' . $quotedName . ' IS NULL OR :NEW.' . $quotedName . ' = 0) THEN
538
      SELECT ' . $sequenceName . '.NEXTVAL INTO :NEW.' . $quotedName . ' FROM DUAL;
539 540 541
   ELSE
      SELECT NVL(Last_Number, 0) INTO last_Sequence
        FROM User_Sequences
542
       WHERE Sequence_Name = \'' . $sequence->getName() . '\';
543
      SELECT :NEW.' . $quotedName . ' INTO last_InsertID FROM DUAL;
544
      WHILE (last_InsertID > last_Sequence) LOOP
545
         SELECT ' . $sequenceName . '.NEXTVAL INTO last_Sequence FROM DUAL;
546 547 548
      END LOOP;
   END IF;
END;';
549

550 551 552
        return $sql;
    }

Benjamin Morel's avatar
Benjamin Morel committed
553
    /**
554 555 556
     * Returns the SQL statements to drop the autoincrement for the given table name.
     *
     * @param string $table The table name to drop the autoincrement for.
Benjamin Morel's avatar
Benjamin Morel committed
557
     *
558
     * @return string[]
Benjamin Morel's avatar
Benjamin Morel committed
559
     */
560 561
    public function getDropAutoincrementSql($table)
    {
562
        $table                       = $this->normalizeIdentifier($table);
563
        $autoincrementIdentifierName = $this->getAutoincrementIdentifierName($table);
564
        $identitySequenceName        = $this->getIdentitySequenceName(
565 566 567
            $table->isQuoted() ? $table->getQuotedName($this) : $table->getName(),
            ''
        );
568

569
        return [
570
            'DROP TRIGGER ' . $autoincrementIdentifierName,
571 572
            $this->getDropSequenceSQL($identitySequenceName),
            $this->getDropConstraintSQL($autoincrementIdentifierName, $table->getQuotedName($this)),
573
        ];
574
    }
575

576 577 578 579 580 581 582 583 584 585 586 587 588
    /**
     * Normalizes the given identifier.
     *
     * Uppercases the given identifier if it is not quoted by intention
     * to reflect Oracle's internal auto uppercasing strategy of unquoted identifiers.
     *
     * @param string $name The identifier to normalize.
     *
     * @return Identifier The normalized identifier.
     */
    private function normalizeIdentifier($name)
    {
        $identifier = new Identifier($name);
589

590 591 592
        return $identifier->isQuoted() ? $identifier : new Identifier(strtoupper($name));
    }

593 594 595 596 597 598
    /**
     * Adds suffix to identifier,
     *
     * if the new string exceeds max identifier length,
     * keeps $suffix, cuts from $identifier as much as the part exceeding.
     */
599
    private function addSuffix(string $identifier, string $suffix): string
600 601 602 603 604 605 606 607 608
    {
        $maxPossibleLengthWithoutSuffix = $this->getMaxIdentifierLength() - strlen($suffix);
        if (strlen($identifier) > $maxPossibleLengthWithoutSuffix) {
            $identifier = substr($identifier, 0, $maxPossibleLengthWithoutSuffix);
        }

        return $identifier . $suffix;
    }

609 610 611 612 613 614 615 616 617 618 619 620
    /**
     * Returns the autoincrement primary key identifier name for the given table identifier.
     *
     * Quotes the autoincrement primary key identifier name
     * if the given table name is quoted by intention.
     *
     * @param Identifier $table The table identifier to return the autoincrement primary key identifier name for.
     *
     * @return string
     */
    private function getAutoincrementIdentifierName(Identifier $table)
    {
621
        $identifierName = $this->addSuffix($table->getName(), '_AI_PK');
622 623 624 625

        return $table->isQuoted()
            ? $this->quoteSingleIdentifier($identifierName)
            : $identifierName;
626 627
    }

Benjamin Morel's avatar
Benjamin Morel committed
628 629 630
    /**
     * {@inheritDoc}
     */
631
    public function getListTableForeignKeysSQL($table)
632
    {
633 634
        $table = $this->normalizeIdentifier($table);
        $table = $this->quoteStringLiteral($table->getName());
635

636 637 638 639
        return "SELECT alc.constraint_name,
          alc.DELETE_RULE,
          cols.column_name \"local_column\",
          cols.position,
640 641 642 643 644 645 646 647 648 649 650 651
          (
              SELECT r_cols.table_name
              FROM   user_cons_columns r_cols
              WHERE  alc.r_constraint_name = r_cols.constraint_name
              AND    r_cols.position = cols.position
          ) AS \"references_table\",
          (
              SELECT r_cols.column_name
              FROM   user_cons_columns r_cols
              WHERE  alc.r_constraint_name = r_cols.constraint_name
              AND    r_cols.position = cols.position
          ) AS \"foreign_column\"
652
     FROM user_cons_columns cols
653
     JOIN user_constraints alc
654 655
       ON alc.constraint_name = cols.constraint_name
      AND alc.constraint_type = 'R'
656 657
      AND alc.table_name = " . $table . '
    ORDER BY cols.constraint_name ASC, cols.position ASC';
658 659
    }

Benjamin Morel's avatar
Benjamin Morel committed
660 661 662
    /**
     * {@inheritDoc}
     */
663
    public function getListTableConstraintsSQL($table)
664
    {
665
        $table = $this->normalizeIdentifier($table);
666
        $table = $this->quoteStringLiteral($table->getName());
667

668
        return 'SELECT * FROM user_constraints WHERE table_name = ' . $table;
669 670
    }

Benjamin Morel's avatar
Benjamin Morel committed
671 672 673
    /**
     * {@inheritDoc}
     */
674
    public function getListTableColumnsSQL($table, $database = null)
675
    {
676
        $table = $this->normalizeIdentifier($table);
677
        $table = $this->quoteStringLiteral($table->getName());
678

679 680 681
        $tabColumnsTableName       = 'user_tab_columns';
        $colCommentsTableName      = 'user_col_comments';
        $tabColumnsOwnerCondition  = '';
682
        $colCommentsOwnerCondition = '';
683

684 685 686 687 688
        if ($database !== null && $database !== '/') {
            $database                  = $this->normalizeIdentifier($database);
            $database                  = $this->quoteStringLiteral($database->getName());
            $tabColumnsTableName       = 'all_tab_columns';
            $colCommentsTableName      = 'all_col_comments';
689 690
            $tabColumnsOwnerCondition  = ' AND c.owner = ' . $database;
            $colCommentsOwnerCondition = ' AND d.OWNER = c.OWNER';
691
        }
692

693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
        return sprintf(
            <<<'SQL'
SELECT   c.*,
         (
             SELECT d.comments
             FROM   %s d
             WHERE  d.TABLE_NAME = c.TABLE_NAME%s
             AND    d.COLUMN_NAME = c.COLUMN_NAME
         ) AS comments
FROM     %s c
WHERE    c.table_name = %s%s
ORDER BY c.column_id
SQL
            ,
            $colCommentsTableName,
            $colCommentsOwnerCondition,
            $tabColumnsTableName,
            $table,
            $tabColumnsOwnerCondition
        );
713 714
    }

715
    /**
716
     * {@inheritDoc}
717
     */
718
    public function getDropSequenceSQL($sequence)
719
    {
720
        if ($sequence instanceof Sequence) {
721
            $sequence = $sequence->getQuotedName($this);
722 723 724
        }

        return 'DROP SEQUENCE ' . $sequence;
725 726
    }

727
    /**
728
     * {@inheritDoc}
729
     */
730
    public function getDropForeignKeySQL($foreignKey, $table)
731
    {
732 733
        if (! $foreignKey instanceof ForeignKeyConstraint) {
            $foreignKey = new Identifier($foreignKey);
734 735
        }

736 737
        if (! $table instanceof Table) {
            $table = new Identifier($table);
738 739
        }

740
        $foreignKey = $foreignKey->getQuotedName($this);
741
        $table      = $table->getQuotedName($this);
742

743 744 745
        return 'ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $foreignKey;
    }

746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
    /**
     * {@inheritdoc}
     */
    public function getAdvancedForeignKeyOptionsSQL(ForeignKeyConstraint $foreignKey)
    {
        $referentialAction = null;

        if ($foreignKey->hasOption('onDelete')) {
            $referentialAction = $this->getForeignKeyReferentialActionSQL($foreignKey->getOption('onDelete'));
        }

        return $referentialAction ? ' ON DELETE ' . $referentialAction : '';
    }

    /**
     * {@inheritdoc}
     */
    public function getForeignKeyReferentialActionSQL($action)
    {
        $action = strtoupper($action);

        switch ($action) {
            case 'RESTRICT': // RESTRICT is not supported, therefore falling back to NO ACTION.
            case 'NO ACTION':
                // NO ACTION cannot be declared explicitly,
                // therefore returning empty string to indicate to OMIT the referential clause.
                return '';
773

774 775 776
            case 'CASCADE':
            case 'SET NULL':
                return $action;
777

778 779
            default:
                // SET DEFAULT is not supported, throw exception instead.
780
                throw new InvalidArgumentException('Invalid foreign key action: ' . $action);
781 782 783
        }
    }

784 785 786
    /**
     * {@inheritDoc}
     */
787
    public function getDropDatabaseSQL($database)
788 789 790 791
    {
        return 'DROP USER ' . $database . ' CASCADE';
    }

792
    /**
793
     * {@inheritDoc}
794
     */
795
    public function getAlterTableSQL(TableDiff $diff)
796
    {
797
        $sql         = [];
798
        $commentsSQL = [];
799
        $columnSql   = [];
800

801
        $fields = [];
802

803
        foreach ($diff->addedColumns as $column) {
804 805
            if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
                continue;
806 807
            }

808
            $fields[] = $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
809 810 811
            $comment  = $this->getColumnComment($column);

            if (! $comment) {
812
                continue;
813
            }
814 815 816 817 818 819

            $commentsSQL[] = $this->getCommentOnColumnSQL(
                $diff->getName($this)->getQuotedName($this),
                $column->getQuotedName($this),
                $comment
            );
820
        }
821

822
        if (count($fields)) {
Sergei Morozov's avatar
Sergei Morozov committed
823 824
            $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this)
                . ' ADD (' . implode(', ', $fields) . ')';
825 826
        }

827
        $fields = [];
828
        foreach ($diff->changedColumns as $columnDiff) {
829 830
            if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) {
                continue;
831 832
            }

833
            $column = $columnDiff->column;
Steve Müller's avatar
Steve Müller committed
834 835 836 837

            // Do not generate column alteration clause if type is binary and only fixed property has changed.
            // Oracle only supports binary type columns with variable length.
            // Avoids unnecessary table alteration statements.
838 839
            if (
                $column->getType() instanceof BinaryType &&
Steve Müller's avatar
Steve Müller committed
840 841 842 843 844 845
                $columnDiff->hasChanged('fixed') &&
                count($columnDiff->changedProperties) === 1
            ) {
                continue;
            }

846 847 848 849 850
            $columnHasChangedComment = $columnDiff->hasChanged('comment');

            /**
             * Do not add query part if only comment has changed
             */
851
            if (! ($columnHasChangedComment && count($columnDiff->changedProperties) === 1)) {
852 853
                $columnInfo = $column->toArray();

854
                if (! $columnDiff->hasChanged('notnull')) {
855
                    unset($columnInfo['notnull']);
856 857
                }

858
                $fields[] = $column->getQuotedName($this) . $this->getColumnDeclarationSQL('', $columnInfo);
859 860
            }

861 862
            if (! $columnHasChangedComment) {
                continue;
863
            }
864 865 866 867 868 869

            $commentsSQL[] = $this->getCommentOnColumnSQL(
                $diff->getName($this)->getQuotedName($this),
                $column->getQuotedName($this),
                $this->getColumnComment($column)
            );
870
        }
871

872
        if (count($fields)) {
Sergei Morozov's avatar
Sergei Morozov committed
873 874
            $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this)
                . ' MODIFY (' . implode(', ', $fields) . ')';
875 876
        }

877
        foreach ($diff->renamedColumns as $oldColumnName => $column) {
878 879
            if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) {
                continue;
880 881
            }

882 883
            $oldColumnName = new Identifier($oldColumnName);

884
            $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) .
885
                ' RENAME COLUMN ' . $oldColumnName->getQuotedName($this) . ' TO ' . $column->getQuotedName($this);
886 887
        }

888
        $fields = [];
889
        foreach ($diff->removedColumns as $column) {
890 891
            if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) {
                continue;
892 893
            }

894
            $fields[] = $column->getQuotedName($this);
895
        }
896

897
        if (count($fields)) {
Sergei Morozov's avatar
Sergei Morozov committed
898 899
            $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this)
                . ' DROP (' . implode(', ', $fields) . ')';
900 901
        }

902
        $tableSql = [];
903

904
        if (! $this->onSchemaAlterTable($diff, $tableSql)) {
905 906
            $sql = array_merge($sql, $commentsSQL);

Sergei Morozov's avatar
Sergei Morozov committed
907 908 909 910 911 912 913 914
            $newName = $diff->getNewName();

            if ($newName !== false) {
                $sql[] = sprintf(
                    'ALTER TABLE %s RENAME TO %s',
                    $diff->getName($this)->getQuotedName($this),
                    $newName->getQuotedName($this)
                );
915 916
            }

917 918 919 920 921
            $sql = array_merge(
                $this->getPreAlterTableIndexForeignKeySQL($diff),
                $sql,
                $this->getPostAlterTableIndexForeignKeySQL($diff)
            );
922 923
        }

924
        return array_merge($sql, $tableSql, $columnSql);
925 926
    }

927 928 929
    /**
     * {@inheritdoc}
     */
930
    public function getColumnDeclarationSQL($name, array $column)
931
    {
932 933
        if (isset($column['columnDefinition'])) {
            $columnDef = $this->getCustomTypeDeclarationSQL($column);
934
        } else {
935
            $default = $this->getDefaultValueDeclarationSQL($column);
936

937 938
            $notnull = '';

939 940
            if (isset($column['notnull'])) {
                $notnull = $column['notnull'] ? ' NOT NULL' : ' NULL';
941
            }
942

943
            $unique = isset($column['unique']) && $column['unique'] ?
944 945
                ' ' . $this->getUniqueFieldDeclarationSQL() : '';

946 947
            $check = isset($column['check']) && $column['check'] ?
                ' ' . $column['check'] : '';
948

949
            $typeDecl  = $column['type']->getSQLDeclaration($column, $this);
950 951 952 953 954 955
            $columnDef = $typeDecl . $default . $notnull . $unique . $check;
        }

        return $name . ' ' . $columnDef;
    }

956 957 958 959 960
    /**
     * {@inheritdoc}
     */
    protected function getRenameIndexSQL($oldIndexName, Index $index, $tableName)
    {
961
        if (strpos($tableName, '.') !== false) {
962
            [$schema]     = explode('.', $tableName);
963 964 965
            $oldIndexName = $schema . '.' . $oldIndexName;
        }

966
        return ['ALTER INDEX ' . $oldIndexName . ' RENAME TO ' . $index->getQuotedName($this)];
967 968
    }

969
    /**
970
     * {@inheritDoc}
971 972 973 974 975
     */
    public function prefersSequences()
    {
        return true;
    }
976

977 978 979 980 981 982 983 984 985 986 987 988 989
    /**
     * {@inheritdoc}
     */
    public function usesSequenceEmulatedIdentityColumns()
    {
        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function getIdentitySequenceName($tableName, $columnName)
    {
990 991
        $table = new Identifier($tableName);

992
        // No usage of column name to preserve BC compatibility with <2.5
993
        $identitySequenceName = $this->addSuffix($table->getName(), '_SEQ');
994 995

        if ($table->isQuoted()) {
996 997 998 999 1000 1001
            $identitySequenceName = '"' . $identitySequenceName . '"';
        }

        $identitySequenceIdentifier = $this->normalizeIdentifier($identitySequenceName);

        return $identitySequenceIdentifier->getQuotedName($this);
1002 1003
    }

1004 1005 1006
    /**
     * {@inheritDoc}
     */
1007 1008 1009 1010 1011
    public function supportsCommentOnStatement()
    {
        return true;
    }

1012
    /**
1013
     * {@inheritDoc}
1014 1015 1016 1017 1018
     */
    public function getName()
    {
        return 'oracle';
    }
1019 1020

    /**
1021
     * {@inheritDoc}
1022
     */
1023
    protected function doModifyLimitQuery($query, $limit, $offset = null)
1024
    {
1025
        if ($limit === null && $offset <= 0) {
1026 1027
            return $query;
        }
1028

1029
        if (preg_match('/^\s*SELECT/i', $query)) {
1030 1031
            if (! preg_match('/\sFROM\s/i', $query)) {
                $query .= ' FROM dual';
1032
            }
1033

1034
            $columns = ['a.*'];
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047

            if ($offset > 0) {
                $columns[] = 'ROWNUM AS doctrine_rownum';
            }

            $query = sprintf('SELECT %s FROM (%s) a', implode(', ', $columns), $query);

            if ($limit !== null) {
                $query .= sprintf(' WHERE ROWNUM <= %d', $offset + $limit);
            }

            if ($offset > 0) {
                $query = sprintf('SELECT * FROM (%s) WHERE doctrine_rownum >= %d', $query, $offset + 1);
1048 1049
            }
        }
1050

1051 1052
        return $query;
    }
1053

1054
    /**
1055
     * {@inheritDoc}
1056
     *
1057 1058
     * Oracle returns all column names in SQL result sets in uppercase.
     */
1059
    public function getSQLResultCasing($column)
1060 1061 1062
    {
        return strtoupper($column);
    }
1063

Benjamin Morel's avatar
Benjamin Morel committed
1064 1065 1066
    /**
     * {@inheritDoc}
     */
1067
    public function getCreateTemporaryTableSnippetSQL()
1068
    {
1069
        return 'CREATE GLOBAL TEMPORARY TABLE';
1070
    }
1071

1072 1073 1074
    /**
     * {@inheritDoc}
     */
1075
    public function getDateTimeTzFormatString()
1076 1077 1078
    {
        return 'Y-m-d H:i:sP';
    }
1079

1080 1081 1082
    /**
     * {@inheritDoc}
     */
1083 1084 1085 1086 1087
    public function getDateFormatString()
    {
        return 'Y-m-d 00:00:00';
    }

1088 1089 1090
    /**
     * {@inheritDoc}
     */
1091 1092 1093 1094
    public function getTimeFormatString()
    {
        return '1900-01-01 H:i:s';
    }
1095

1096 1097 1098
    /**
     * {@inheritDoc}
     */
1099 1100 1101 1102 1103 1104
    public function fixSchemaElementName($schemaElementName)
    {
        if (strlen($schemaElementName) > 30) {
            // Trim it
            return substr($schemaElementName, 0, 30);
        }
1105

1106 1107
        return $schemaElementName;
    }
1108

1109
    /**
1110
     * {@inheritDoc}
1111 1112 1113 1114 1115 1116
     */
    public function getMaxIdentifierLength()
    {
        return 30;
    }

1117
    /**
1118
     * {@inheritDoc}
1119 1120 1121 1122 1123
     */
    public function supportsSequences()
    {
        return true;
    }
1124

1125 1126 1127
    /**
     * {@inheritDoc}
     */
1128 1129 1130 1131
    public function supportsForeignKeyOnUpdate()
    {
        return false;
    }
1132

1133
    /**
1134
     * {@inheritDoc}
1135 1136 1137 1138 1139 1140
     */
    public function supportsReleaseSavepoints()
    {
        return false;
    }

1141
    /**
1142
     * {@inheritDoc}
1143
     */
1144
    public function getTruncateTableSQL($tableName, $cascade = false)
1145
    {
1146 1147 1148
        $tableIdentifier = new Identifier($tableName);

        return 'TRUNCATE TABLE ' . $tableIdentifier->getQuotedName($this);
1149
    }
1150 1151

    /**
1152
     * {@inheritDoc}
1153 1154 1155
     */
    public function getDummySelectSQL()
    {
1156 1157 1158
        $expression = func_num_args() > 0 ? func_get_arg(0) : '1';

        return sprintf('SELECT %s FROM DUAL', $expression);
1159
    }
1160

1161 1162 1163
    /**
     * {@inheritDoc}
     */
1164 1165
    protected function initializeDoctrineTypeMappings()
    {
1166
        $this->doctrineTypeMapping = [
1167 1168 1169 1170 1171 1172 1173 1174 1175
            'integer'           => 'integer',
            'number'            => 'integer',
            'pls_integer'       => 'boolean',
            'binary_integer'    => 'boolean',
            'varchar'           => 'string',
            'varchar2'          => 'string',
            'nvarchar2'         => 'string',
            'char'              => 'string',
            'nchar'             => 'string',
1176
            'date'              => 'date',
1177 1178
            'timestamp'         => 'datetime',
            'timestamptz'       => 'datetimetz',
1179
            'float'             => 'float',
1180 1181
            'binary_float'      => 'float',
            'binary_double'     => 'float',
1182 1183 1184
            'long'              => 'string',
            'clob'              => 'text',
            'nclob'             => 'text',
Steve Müller's avatar
Steve Müller committed
1185 1186
            'raw'               => 'binary',
            'long raw'          => 'blob',
1187
            'rowid'             => 'string',
1188
            'urowid'            => 'string',
1189
            'blob'              => 'blob',
1190
        ];
1191
    }
1192 1193

    /**
1194
     * {@inheritDoc}
1195 1196 1197 1198 1199
     */
    public function releaseSavePoint($savepoint)
    {
        return '';
    }
1200

1201 1202 1203
    /**
     * {@inheritDoc}
     */
1204 1205
    protected function getReservedKeywordsClass()
    {
1206
        return Keywords\OracleKeywords::class;
1207
    }
1208 1209

    /**
1210
     * {@inheritDoc}
1211
     */
1212
    public function getBlobTypeDeclarationSQL(array $column)
1213 1214 1215
    {
        return 'BLOB';
    }
1216

1217
    public function getListTableCommentsSQL(string $table, ?string $database = null): string
1218 1219 1220 1221 1222 1223
    {
        $tableCommentsName = 'user_tab_comments';
        $ownerCondition    = '';

        if ($database !== null && $database !== '/') {
            $tableCommentsName = 'all_tab_comments';
Sergei Morozov's avatar
Sergei Morozov committed
1224 1225 1226
            $ownerCondition    = ' AND owner = ' . $this->quoteStringLiteral(
                $this->normalizeIdentifier($database)->getName()
            );
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
        }

        return sprintf(
            <<<'SQL'
SELECT comments FROM %s WHERE table_name = %s%s
SQL
            ,
            $tableCommentsName,
            $this->quoteStringLiteral($this->normalizeIdentifier($table)->getName()),
            $ownerCondition
        );
    }
1239
}