Failed Conditions
Pull Request — master (#2766)
by mon
18:50
created

OraclePlatform::getListColumnsSQL()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

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