Completed
Push — 2.10.x ( 61a6b9...f20ba1 )
by Grégoire
13:37 queued 11s
created

OraclePlatform::getAlterSequenceSQL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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