OraclePlatform.php 33.3 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
use function array_merge;
use function count;
use function explode;
18 19
use function func_get_arg;
use function func_num_args;
20 21 22 23 24 25 26
use function implode;
use function preg_match;
use function sprintf;
use function strlen;
use function strpos;
use function strtoupper;
use function substr;
27

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

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

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

    /**
64
     * @param string $type
65 66
     *
     * @return string
67 68 69 70 71 72 73 74 75 76 77 78
     */
    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\')';
        }
    }

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

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

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

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

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

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

122 123
            default:
                $calculationClause = '';
124

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

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

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

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

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

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

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

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

173
    /**
174
     * {@inheritDoc}
175
     *
176 177 178
     * 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()}
179
     */
180
    public function getCreateSequenceSQL(Sequence $sequence)
181
    {
182
        return 'CREATE SEQUENCE ' . $sequence->getQuotedName($this) .
183
               ' START WITH ' . $sequence->getInitialValue() .
184
               ' MINVALUE ' . $sequence->getInitialValue() .
185 186
               ' INCREMENT BY ' . $sequence->getAllocationSize() .
               $this->getSequenceCacheSQL($sequence);
187
    }
188

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

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

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

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

        return '';
219
    }
220

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            $sql = array_merge($sql, $this->getCreateAutoincrementSql($name, $table));
403
        }
404

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

        return $sql;
    }

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

424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
        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
446
                           WHERE  ucon.index_name = uind_col.index_name
447 448
                       ) AS is_primary
             FROM      user_ind_columns uind_col
449 450
             WHERE     uind_col.table_name = " . $table . '
             ORDER BY  uind_col.column_position ASC';
451 452
    }

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

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

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

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

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

        $nameIdentifier = $this->normalizeIdentifier($name);
499 500
        $quotedName     = $nameIdentifier->getQuotedName($this);
        $unquotedName   = $nameIdentifier->getName();
501

502
        $sql = [];
503

504
        $autoincrementIdentifierName = $this->getAutoincrementIdentifierName($tableIdentifier);
505

506
        $idx = new Index($autoincrementIdentifierName, [$quotedName], true, true);
507

508 509 510
        $sql[] = 'DECLARE
  constraints_Count NUMBER;
BEGIN
511
  SELECT COUNT(CONSTRAINT_NAME) INTO constraints_Count FROM USER_CONSTRAINTS WHERE TABLE_NAME = \'' . $unquotedTableName . '\' AND CONSTRAINT_TYPE = \'P\';
512
  IF constraints_Count = 0 OR constraints_Count = \'\' THEN
513
    EXECUTE IMMEDIATE \'' . $this->getCreateConstraintSQL($idx, $quotedTableName) . '\';
514
  END IF;
515
END;';
516

517 518 519 520
        $sequenceName = $this->getIdentitySequenceName(
            $tableIdentifier->isQuoted() ? $quotedTableName : $unquotedTableName,
            $nameIdentifier->isQuoted() ? $quotedName : $unquotedName
        );
521 522
        $sequence     = new Sequence($sequenceName, $start);
        $sql[]        = $this->getCreateSequenceSQL($sequence);
523

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

546 547 548
        return $sql;
    }

Benjamin Morel's avatar
Benjamin Morel committed
549
    /**
550 551 552
     * 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
553
     *
554
     * @return string[]
Benjamin Morel's avatar
Benjamin Morel committed
555
     */
556 557
    public function getDropAutoincrementSql($table)
    {
558
        $table                       = $this->normalizeIdentifier($table);
559
        $autoincrementIdentifierName = $this->getAutoincrementIdentifierName($table);
560
        $identitySequenceName        = $this->getIdentitySequenceName(
561 562 563
            $table->isQuoted() ? $table->getQuotedName($this) : $table->getName(),
            ''
        );
564

565
        return [
566
            'DROP TRIGGER ' . $autoincrementIdentifierName,
567 568
            $this->getDropSequenceSQL($identitySequenceName),
            $this->getDropConstraintSQL($autoincrementIdentifierName, $table->getQuotedName($this)),
569
        ];
570
    }
571

572 573 574 575 576 577 578 579 580 581 582 583 584
    /**
     * 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);
585

586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
        return $identifier->isQuoted() ? $identifier : new Identifier(strtoupper($name));
    }

    /**
     * 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)
    {
        $identifierName = $table->getName() . '_AI_PK';

        return $table->isQuoted()
            ? $this->quoteSingleIdentifier($identifierName)
            : $identifierName;
606 607
    }

Benjamin Morel's avatar
Benjamin Morel committed
608 609 610
    /**
     * {@inheritDoc}
     */
611
    public function getListTableForeignKeysSQL($table)
612
    {
613 614
        $table = $this->normalizeIdentifier($table);
        $table = $this->quoteStringLiteral($table->getName());
615

616 617 618 619
        return "SELECT alc.constraint_name,
          alc.DELETE_RULE,
          cols.column_name \"local_column\",
          cols.position,
620 621 622 623 624 625 626 627 628 629 630 631
          (
              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\"
632
     FROM user_cons_columns cols
633
     JOIN user_constraints alc
634 635
       ON alc.constraint_name = cols.constraint_name
      AND alc.constraint_type = 'R'
636 637
      AND alc.table_name = " . $table . '
    ORDER BY cols.constraint_name ASC, cols.position ASC';
638 639
    }

Benjamin Morel's avatar
Benjamin Morel committed
640 641 642
    /**
     * {@inheritDoc}
     */
643
    public function getListTableConstraintsSQL($table)
644
    {
645
        $table = $this->normalizeIdentifier($table);
646
        $table = $this->quoteStringLiteral($table->getName());
647

648
        return 'SELECT * FROM user_constraints WHERE table_name = ' . $table;
649 650
    }

Benjamin Morel's avatar
Benjamin Morel committed
651 652 653
    /**
     * {@inheritDoc}
     */
654
    public function getListTableColumnsSQL($table, $database = null)
655
    {
656
        $table = $this->normalizeIdentifier($table);
657
        $table = $this->quoteStringLiteral($table->getName());
658

659 660 661
        $tabColumnsTableName       = 'user_tab_columns';
        $colCommentsTableName      = 'user_col_comments';
        $tabColumnsOwnerCondition  = '';
662
        $colCommentsOwnerCondition = '';
663

664 665 666 667 668
        if ($database !== null && $database !== '/') {
            $database                  = $this->normalizeIdentifier($database);
            $database                  = $this->quoteStringLiteral($database->getName());
            $tabColumnsTableName       = 'all_tab_columns';
            $colCommentsTableName      = 'all_col_comments';
669 670
            $tabColumnsOwnerCondition  = ' AND c.owner = ' . $database;
            $colCommentsOwnerCondition = ' AND d.OWNER = c.OWNER';
671
        }
672

673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
        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
        );
693 694
    }

695
    /**
696
     * {@inheritDoc}
697
     */
698
    public function getDropSequenceSQL($sequence)
699
    {
700
        if ($sequence instanceof Sequence) {
701
            $sequence = $sequence->getQuotedName($this);
702 703 704
        }

        return 'DROP SEQUENCE ' . $sequence;
705 706
    }

707
    /**
708
     * {@inheritDoc}
709
     */
710
    public function getDropForeignKeySQL($foreignKey, $table)
711
    {
712 713
        if (! $foreignKey instanceof ForeignKeyConstraint) {
            $foreignKey = new Identifier($foreignKey);
714 715
        }

716 717
        if (! $table instanceof Table) {
            $table = new Identifier($table);
718 719
        }

720
        $foreignKey = $foreignKey->getQuotedName($this);
721
        $table      = $table->getQuotedName($this);
722

723 724 725
        return 'ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $foreignKey;
    }

726 727 728 729 730
    /**
     * {@inheritdoc}
     */
    public function getAdvancedForeignKeyOptionsSQL(ForeignKeyConstraint $foreignKey)
    {
731
        $referentialAction = '';
732 733 734 735 736

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

737 738 739 740 741
        if ($referentialAction !== '') {
            return ' ON DELETE ' . $referentialAction;
        }

        return '';
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
    }

    /**
     * {@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 '';
757

758 759 760
            case 'CASCADE':
            case 'SET NULL':
                return $action;
761

762 763
            default:
                // SET DEFAULT is not supported, throw exception instead.
764
                throw new InvalidArgumentException('Invalid foreign key action: ' . $action);
765 766 767
        }
    }

768 769 770
    /**
     * {@inheritDoc}
     */
771
    public function getDropDatabaseSQL($database)
772 773 774 775
    {
        return 'DROP USER ' . $database . ' CASCADE';
    }

776
    /**
777
     * {@inheritDoc}
778
     */
779
    public function getAlterTableSQL(TableDiff $diff)
780
    {
781
        $sql         = [];
782
        $commentsSQL = [];
783
        $columnSql   = [];
784

785
        $fields = [];
786

787
        foreach ($diff->addedColumns as $column) {
788 789
            if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
                continue;
790 791
            }

792
            $fields[] = $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
793 794
            $comment  = $this->getColumnComment($column);

795
            if ($comment === null || $comment === '') {
796
                continue;
797
            }
798 799 800 801 802 803

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

806
        if (count($fields) > 0) {
807
            $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ADD (' . implode(', ', $fields) . ')';
808 809
        }

810
        $fields = [];
811
        foreach ($diff->changedColumns as $columnDiff) {
812 813
            if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) {
                continue;
814 815
            }

816
            $column = $columnDiff->column;
Steve Müller's avatar
Steve Müller committed
817 818 819 820 821 822 823 824 825 826 827

            // 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.
            if ($column->getType() instanceof BinaryType &&
                $columnDiff->hasChanged('fixed') &&
                count($columnDiff->changedProperties) === 1
            ) {
                continue;
            }

828 829 830 831 832
            $columnHasChangedComment = $columnDiff->hasChanged('comment');

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

836
                if (! $columnDiff->hasChanged('notnull')) {
837
                    unset($columnInfo['notnull']);
838 839
                }

840
                $fields[] = $column->getQuotedName($this) . $this->getColumnDeclarationSQL('', $columnInfo);
841 842
            }

843 844
            if (! $columnHasChangedComment) {
                continue;
845
            }
846 847 848 849 850 851

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

854
        if (count($fields) > 0) {
855
            $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' MODIFY (' . implode(', ', $fields) . ')';
856 857
        }

858
        foreach ($diff->renamedColumns as $oldColumnName => $column) {
859 860
            if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) {
                continue;
861 862
            }

863 864
            $oldColumnName = new Identifier($oldColumnName);

865
            $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) .
866
                ' RENAME COLUMN ' . $oldColumnName->getQuotedName($this) . ' TO ' . $column->getQuotedName($this);
867 868
        }

869
        $fields = [];
870
        foreach ($diff->removedColumns as $column) {
871 872
            if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) {
                continue;
873 874
            }

875
            $fields[] = $column->getQuotedName($this);
876
        }
877

878
        if (count($fields) > 0) {
879
            $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' DROP (' . implode(', ', $fields) . ')';
880 881
        }

882
        $tableSql = [];
883

884
        if (! $this->onSchemaAlterTable($diff, $tableSql)) {
885 886
            $sql = array_merge($sql, $commentsSQL);

Sergei Morozov's avatar
Sergei Morozov committed
887 888 889 890 891 892 893 894
            $newName = $diff->getNewName();

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

897 898 899 900 901
            $sql = array_merge(
                $this->getPreAlterTableIndexForeignKeySQL($diff),
                $sql,
                $this->getPostAlterTableIndexForeignKeySQL($diff)
            );
902 903
        }

904
        return array_merge($sql, $tableSql, $columnSql);
905 906
    }

907 908 909 910 911 912 913 914 915 916
    /**
     * {@inheritdoc}
     */
    public function getColumnDeclarationSQL($name, array $field)
    {
        if (isset($field['columnDefinition'])) {
            $columnDef = $this->getCustomTypeDeclarationSQL($field);
        } else {
            $default = $this->getDefaultValueDeclarationSQL($field);

917 918 919 920 921
            $notnull = '';

            if (isset($field['notnull'])) {
                $notnull = $field['notnull'] ? ' NOT NULL' : ' NULL';
            }
922

923
            $unique = ! empty($field['unique']) ?
924 925
                ' ' . $this->getUniqueFieldDeclarationSQL() : '';

926
            $check = ! empty($field['check']) ?
927 928
                ' ' . $field['check'] : '';

929
            $typeDecl  = $field['type']->getSQLDeclaration($field, $this);
930 931 932 933 934 935
            $columnDef = $typeDecl . $default . $notnull . $unique . $check;
        }

        return $name . ' ' . $columnDef;
    }

936 937 938 939 940
    /**
     * {@inheritdoc}
     */
    protected function getRenameIndexSQL($oldIndexName, Index $index, $tableName)
    {
941
        if (strpos($tableName, '.') !== false) {
942
            [$schema]     = explode('.', $tableName);
943 944 945
            $oldIndexName = $schema . '.' . $oldIndexName;
        }

946
        return ['ALTER INDEX ' . $oldIndexName . ' RENAME TO ' . $index->getQuotedName($this)];
947 948
    }

949
    /**
950
     * {@inheritDoc}
951 952 953 954 955
     */
    public function prefersSequences()
    {
        return true;
    }
956

957 958 959 960 961 962 963 964 965 966 967 968 969
    /**
     * {@inheritdoc}
     */
    public function usesSequenceEmulatedIdentityColumns()
    {
        return true;
    }

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

972 973 974 975
        // No usage of column name to preserve BC compatibility with <2.5
        $identitySequenceName = $table->getName() . '_SEQ';

        if ($table->isQuoted()) {
976 977 978 979 980 981
            $identitySequenceName = '"' . $identitySequenceName . '"';
        }

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

        return $identitySequenceIdentifier->getQuotedName($this);
982 983
    }

984 985 986
    /**
     * {@inheritDoc}
     */
987 988 989 990 991
    public function supportsCommentOnStatement()
    {
        return true;
    }

992
    /**
993
     * {@inheritDoc}
994 995 996 997 998
     */
    public function getName()
    {
        return 'oracle';
    }
999 1000

    /**
1001
     * {@inheritDoc}
1002
     */
1003
    protected function doModifyLimitQuery($query, $limit, $offset = null)
1004
    {
1005
        if ($limit === null && $offset <= 0) {
1006 1007
            return $query;
        }
1008

1009 1010
        if (preg_match('/^\s*SELECT/i', $query) === 1) {
            if (preg_match('/\sFROM\s/i', $query) === 0) {
1011
                $query .= ' FROM dual';
1012
            }
1013

1014
            $columns = ['a.*'];
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027

            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);
1028 1029
            }
        }
1030

1031 1032
        return $query;
    }
1033

1034
    /**
1035
     * {@inheritDoc}
1036
     *
1037 1038
     * Oracle returns all column names in SQL result sets in uppercase.
     */
1039
    public function getSQLResultCasing($column)
1040 1041 1042
    {
        return strtoupper($column);
    }
1043

Benjamin Morel's avatar
Benjamin Morel committed
1044 1045 1046
    /**
     * {@inheritDoc}
     */
1047
    public function getCreateTemporaryTableSnippetSQL()
1048
    {
1049
        return 'CREATE GLOBAL TEMPORARY TABLE';
1050
    }
1051

1052 1053 1054
    /**
     * {@inheritDoc}
     */
1055
    public function getDateTimeTzFormatString()
1056 1057 1058
    {
        return 'Y-m-d H:i:sP';
    }
1059

1060 1061 1062
    /**
     * {@inheritDoc}
     */
1063 1064 1065 1066 1067
    public function getDateFormatString()
    {
        return 'Y-m-d 00:00:00';
    }

1068 1069 1070
    /**
     * {@inheritDoc}
     */
1071 1072 1073 1074
    public function getTimeFormatString()
    {
        return '1900-01-01 H:i:s';
    }
1075

1076 1077 1078
    /**
     * {@inheritDoc}
     */
1079 1080 1081 1082 1083 1084
    public function fixSchemaElementName($schemaElementName)
    {
        if (strlen($schemaElementName) > 30) {
            // Trim it
            return substr($schemaElementName, 0, 30);
        }
1085

1086 1087
        return $schemaElementName;
    }
1088

1089
    /**
1090
     * {@inheritDoc}
1091 1092 1093 1094 1095 1096
     */
    public function getMaxIdentifierLength()
    {
        return 30;
    }

1097
    /**
1098
     * {@inheritDoc}
1099 1100 1101 1102 1103
     */
    public function supportsSequences()
    {
        return true;
    }
1104

1105 1106 1107
    /**
     * {@inheritDoc}
     */
1108 1109 1110 1111
    public function supportsForeignKeyOnUpdate()
    {
        return false;
    }
1112

1113
    /**
1114
     * {@inheritDoc}
1115 1116 1117 1118 1119 1120
     */
    public function supportsReleaseSavepoints()
    {
        return false;
    }

1121
    /**
1122
     * {@inheritDoc}
1123
     */
1124
    public function getTruncateTableSQL($tableName, $cascade = false)
1125
    {
1126 1127 1128
        $tableIdentifier = new Identifier($tableName);

        return 'TRUNCATE TABLE ' . $tableIdentifier->getQuotedName($this);
1129
    }
1130 1131

    /**
1132
     * {@inheritDoc}
1133 1134 1135
     */
    public function getDummySelectSQL()
    {
1136 1137 1138
        $expression = func_num_args() > 0 ? func_get_arg(0) : '1';

        return sprintf('SELECT %s FROM DUAL', $expression);
1139
    }
1140

1141 1142 1143
    /**
     * {@inheritDoc}
     */
1144 1145
    protected function initializeDoctrineTypeMappings()
    {
1146
        $this->doctrineTypeMapping = [
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
            'binary_double'  => 'float',
            'binary_float'   => 'float',
            'binary_integer' => 'boolean',
            'blob'           => 'blob',
            'char'           => 'string',
            'clob'           => 'text',
            'date'           => 'date',
            'float'          => 'float',
            'integer'        => 'integer',
            'long'           => 'string',
            'long raw'       => 'blob',
            'nchar'          => 'string',
            'nclob'          => 'text',
            'number'         => 'integer',
            'nvarchar2'      => 'string',
            'pls_integer'    => 'boolean',
            'raw'            => 'binary',
            'rowid'          => 'string',
            'timestamp'      => 'datetime',
            'timestamptz'    => 'datetimetz',
            'urowid'         => 'string',
            'varchar'        => 'string',
            'varchar2'       => 'string',
1170
        ];
1171
    }
1172 1173

    /**
1174
     * {@inheritDoc}
1175 1176 1177 1178 1179
     */
    public function releaseSavePoint($savepoint)
    {
        return '';
    }
1180

1181 1182 1183
    /**
     * {@inheritDoc}
     */
1184 1185
    protected function getReservedKeywordsClass()
    {
1186
        return Keywords\OracleKeywords::class;
1187
    }
1188 1189

    /**
1190
     * {@inheritDoc}
1191 1192 1193 1194 1195
     */
    public function getBlobTypeDeclarationSQL(array $field)
    {
        return 'BLOB';
    }
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216

    public function getListTableCommentsSQL(string $table, ?string $database = null) : string
    {
        $tableCommentsName = 'user_tab_comments';
        $ownerCondition    = '';

        if ($database !== null && $database !== '/') {
            $tableCommentsName = 'all_tab_comments';
            $ownerCondition    = ' AND owner = ' . $this->quoteStringLiteral($this->normalizeIdentifier($database)->getName());
        }

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