OraclePlatform::getSQLResultCasing()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Platforms;
21
22
use Doctrine\DBAL\DBALException;
23
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
24
use Doctrine\DBAL\Schema\Identifier;
25
use Doctrine\DBAL\Schema\Index;
26
use Doctrine\DBAL\Schema\Sequence;
27
use Doctrine\DBAL\Schema\Table;
28
use Doctrine\DBAL\Schema\TableDiff;
29
use Doctrine\DBAL\TransactionIsolationLevel;
30
use Doctrine\DBAL\Types\BinaryType;
31
use function array_merge;
32
use function count;
33
use function explode;
34
use function func_get_arg;
35
use function func_num_args;
36
use function implode;
37
use function preg_match;
38
use function sprintf;
39
use function str_replace;
40
use function strlen;
41
use function strpos;
42
use function strtoupper;
43
use function substr;
44
45
/**
46
 * OraclePlatform.
47
 *
48
 * @since 2.0
49
 * @author Roman Borschel <[email protected]>
50
 * @author Lukas Smith <[email protected]> (PEAR MDB2 library)
51
 * @author Benjamin Eberlei <[email protected]>
52
 */
53
class OraclePlatform extends AbstractPlatform
54
{
55
    /**
56
     * Assertion for Oracle identifiers.
57
     *
58
     * @link http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements008.htm
59
     *
60
     * @param string $identifier
61
     *
62
     * @throws DBALException
63
     */
64
    public static function assertValidIdentifier($identifier)
65
    {
66
        if ( ! preg_match('(^(([a-zA-Z]{1}[a-zA-Z0-9_$#]{0,})|("[^"]+"))$)', $identifier)) {
67
            throw new DBALException("Invalid Oracle identifier");
68
        }
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74
    public function getSubstringExpression($value, $position, $length = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
75
    {
76
        if ($length !== null) {
77
            return "SUBSTR($value, $position, $length)";
78
        }
79
80
        return "SUBSTR($value, $position)";
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     */
86
    public function getNowExpression($type = 'timestamp')
87
    {
88
        switch ($type) {
89
            case 'date':
90
            case 'time':
91
            case 'timestamp':
92
            default:
93
                return 'TO_CHAR(CURRENT_TIMESTAMP, \'YYYY-MM-DD HH24:MI:SS\')';
94
        }
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     */
100 View Code Duplication
    public function getLocateExpression($str, $substr, $startPos = false)
101
    {
102
        if ($startPos == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
103
            return 'INSTR('.$str.', '.$substr.')';
104
        }
105
106
        return 'INSTR('.$str.', '.$substr.', '.$startPos.')';
107
    }
108
109
    /**
110
     * {@inheritDoc}
111
     *
112
     * @deprecated Use application-generated UUIDs instead
113
     */
114
    public function getGuidExpression()
115
    {
116
        return 'SYS_GUID()';
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit)
123
    {
124
        switch ($unit) {
125
            case DateIntervalUnit::MONTH:
126
            case DateIntervalUnit::QUARTER:
127
            case DateIntervalUnit::YEAR:
128
                switch ($unit) {
129
                    case DateIntervalUnit::QUARTER:
130
                        $interval *= 3;
131
                        break;
132
133
                    case DateIntervalUnit::YEAR:
134
                        $interval *= 12;
135
                        break;
136
                }
137
138
                return 'ADD_MONTHS(' . $date . ', ' . $operator . $interval . ')';
139
140
            default:
141
                $calculationClause = '';
142
143
                switch ($unit) {
144
                    case DateIntervalUnit::SECOND:
145
                        $calculationClause = '/24/60/60';
146
                        break;
147
148
                    case DateIntervalUnit::MINUTE:
149
                        $calculationClause = '/24/60';
150
                        break;
151
152
                    case DateIntervalUnit::HOUR:
153
                        $calculationClause = '/24';
154
                        break;
155
156
                    case DateIntervalUnit::WEEK:
157
                        $calculationClause = '*7';
158
                        break;
159
                }
160
161
                return '(' . $date . $operator . $interval . $calculationClause . ')';
162
        }
163
    }
164
165
    /**
166
     * {@inheritDoc}
167
     */
168
    public function getDateDiffExpression($date1, $date2)
169
    {
170
        return sprintf('TRUNC(%s) - TRUNC(%s)', $date1, $date2);
171
    }
172
173
    /**
174
     * {@inheritDoc}
175
     */
176
    public function getBitAndComparisonExpression($value1, $value2)
177
    {
178
        return 'BITAND('.$value1 . ', ' . $value2 . ')';
179
    }
180
181
    /**
182
     * {@inheritDoc}
183
     */
184
    public function getBitOrComparisonExpression($value1, $value2)
185
    {
186
        return '(' . $value1 . '-' .
187
                $this->getBitAndComparisonExpression($value1, $value2)
188
                . '+' . $value2 . ')';
189
    }
190
191
    /**
192
     * {@inheritDoc}
193
     *
194
     * Need to specifiy minvalue, since start with is hidden in the system and MINVALUE <= START WITH.
195
     * Therefore we can use MINVALUE to be able to get a hint what START WITH was for later introspection
196
     * in {@see listSequences()}
197
     */
198 View Code Duplication
    public function getCreateSequenceSQL(Sequence $sequence)
199
    {
200
        return 'CREATE SEQUENCE ' . $sequence->getQuotedName($this) .
201
               ' START WITH ' . $sequence->getInitialValue() .
202
               ' MINVALUE ' . $sequence->getInitialValue() .
203
               ' INCREMENT BY ' . $sequence->getAllocationSize() .
204
               $this->getSequenceCacheSQL($sequence);
205
    }
206
207
    /**
208
     * {@inheritDoc}
209
     */
210
    public function getAlterSequenceSQL(Sequence $sequence)
211
    {
212
        return 'ALTER SEQUENCE ' . $sequence->getQuotedName($this) .
213
               ' INCREMENT BY ' . $sequence->getAllocationSize()
214
               . $this->getSequenceCacheSQL($sequence);
215
    }
216
217
    /**
218
     * Cache definition for sequences
219
     *
220
     * @param Sequence $sequence
221
     *
222
     * @return string
223
     */
224
    private function getSequenceCacheSQL(Sequence $sequence)
225
    {
226
        if ($sequence->getCache() === 0) {
227
            return ' NOCACHE';
228
        } else if ($sequence->getCache() === 1) {
229
            return ' NOCACHE';
230
        } else if ($sequence->getCache() > 1) {
231
            return ' CACHE ' . $sequence->getCache();
232
        }
233
234
        return '';
235
    }
236
237
    /**
238
     * {@inheritDoc}
239
     */
240
    public function getSequenceNextValSQL($sequenceName)
241
    {
242
        return 'SELECT ' . $sequenceName . '.nextval FROM DUAL';
243
    }
244
245
    /**
246
     * {@inheritDoc}
247
     */
248
    public function getSetTransactionIsolationSQL($level)
249
    {
250
        return 'SET TRANSACTION ISOLATION LEVEL ' . $this->_getTransactionIsolationLevelSQL($level);
251
    }
252
253
    /**
254
     * {@inheritDoc}
255
     */
256 View Code Duplication
    protected function _getTransactionIsolationLevelSQL($level)
257
    {
258
        switch ($level) {
259
            case TransactionIsolationLevel::READ_UNCOMMITTED:
260
                return 'READ UNCOMMITTED';
261
            case TransactionIsolationLevel::READ_COMMITTED:
262
                return 'READ COMMITTED';
263
            case TransactionIsolationLevel::REPEATABLE_READ:
264
            case TransactionIsolationLevel::SERIALIZABLE:
265
                return 'SERIALIZABLE';
266
            default:
267
                return parent::_getTransactionIsolationLevelSQL($level);
268
        }
269
    }
270
271
    /**
272
     * {@inheritDoc}
273
     */
274
    public function getBooleanTypeDeclarationSQL(array $field)
275
    {
276
        return 'NUMBER(1)';
277
    }
278
279
    /**
280
     * {@inheritDoc}
281
     */
282
    public function getIntegerTypeDeclarationSQL(array $field)
283
    {
284
        return 'NUMBER(10)';
285
    }
286
287
    /**
288
     * {@inheritDoc}
289
     */
290
    public function getBigIntTypeDeclarationSQL(array $field)
291
    {
292
        return 'NUMBER(20)';
293
    }
294
295
    /**
296
     * {@inheritDoc}
297
     */
298
    public function getSmallIntTypeDeclarationSQL(array $field)
299
    {
300
        return 'NUMBER(5)';
301
    }
302
303
    /**
304
     * {@inheritDoc}
305
     */
306
    public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
307
    {
308
        return 'TIMESTAMP(0)';
309
    }
310
311
    /**
312
     * {@inheritDoc}
313
     */
314
    public function getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration)
315
    {
316
        return 'TIMESTAMP(0) WITH TIME ZONE';
317
    }
318
319
    /**
320
     * {@inheritDoc}
321
     */
322
    public function getDateTypeDeclarationSQL(array $fieldDeclaration)
323
    {
324
        return 'DATE';
325
    }
326
327
    /**
328
     * {@inheritDoc}
329
     */
330
    public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
331
    {
332
        return 'DATE';
333
    }
334
335
    /**
336
     * {@inheritDoc}
337
     */
338
    protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
339
    {
340
        return '';
341
    }
342
343
    /**
344
     * {@inheritDoc}
345
     */
346
    protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
347
    {
348
        return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(2000)')
349
                : ($length ? 'VARCHAR2(' . $length . ')' : 'VARCHAR2(4000)');
350
    }
351
352
    /**
353
     * {@inheritdoc}
354
     */
355
    protected function getBinaryTypeDeclarationSQLSnippet($length, $fixed)
356
    {
357
        return 'RAW(' . ($length ?: $this->getBinaryMaxLength()) . ')';
358
    }
359
360
    /**
361
     * {@inheritdoc}
362
     */
363
    public function getBinaryMaxLength()
364
    {
365
        return 2000;
366
    }
367
368
    /**
369
     * {@inheritDoc}
370
     */
371
    public function getClobTypeDeclarationSQL(array $field)
372
    {
373
        return 'CLOB';
374
    }
375
376
    /**
377
     * {@inheritDoc}
378
     */
379
    public function getListDatabasesSQL()
380
    {
381
        return 'SELECT username FROM all_users';
382
    }
383
384
    /**
385
     * {@inheritDoc}
386
     */
387
    public function getListSequencesSQL($database)
388
    {
389
        $database = $this->normalizeIdentifier($database);
390
        $database = $this->quoteStringLiteral($database->getName());
391
392
        return "SELECT sequence_name, min_value, increment_by FROM sys.all_sequences ".
393
               "WHERE SEQUENCE_OWNER = " . $database;
394
    }
395
396
    /**
397
     * {@inheritDoc}
398
     */
399
    protected function _getCreateTableSQL($table, array $columns, array $options = [])
400
    {
401
        $indexes            = $options['indexes'] ?? [];
402
        $options['indexes'] = [];
403
        $sql                = parent::_getCreateTableSQL($table, $columns, $options);
404
405
        foreach ($columns as $name => $column) {
406
            if (isset($column['sequence'])) {
407
                $sql[] = $this->getCreateSequenceSQL($column['sequence']);
408
            }
409
410
            if (isset($column['autoincrement']) && $column['autoincrement'] ||
411
               (isset($column['autoinc']) && $column['autoinc'])) {
412
                $sql = array_merge($sql, $this->getCreateAutoincrementSql($name, $table));
413
            }
414
        }
415
416
        if (isset($indexes) && ! empty($indexes)) {
417
            foreach ($indexes as $index) {
418
                $sql[] = $this->getCreateIndexSQL($index, $table);
419
            }
420
        }
421
422
        return $sql;
423
    }
424
425
    /**
426
     * {@inheritDoc}
427
     *
428
     * @license New BSD License
429
     * @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaOracleReader.html
430
     */
431 View Code Duplication
    public function getListTableIndexesSQL($table, $currentDatabase = null)
432
    {
433
        $table = $this->normalizeIdentifier($table);
434
        $table = $this->quoteStringLiteral($table->getName());
435
436
        return "SELECT uind_col.index_name AS name,
437
                       (
438
                           SELECT uind.index_type
439
                           FROM   user_indexes uind
440
                           WHERE  uind.index_name = uind_col.index_name
441
                       ) AS type,
442
                       decode(
443
                           (
444
                               SELECT uind.uniqueness
445
                               FROM   user_indexes uind
446
                               WHERE  uind.index_name = uind_col.index_name
447
                           ),
448
                           'NONUNIQUE',
449
                           0,
450
                           'UNIQUE',
451
                           1
452
                       ) AS is_unique,
453
                       uind_col.column_name AS column_name,
454
                       uind_col.column_position AS column_pos,
455
                       (
456
                           SELECT ucon.constraint_type
457
                           FROM   user_constraints ucon
458
                           WHERE  ucon.index_name = uind_col.index_name
459
                       ) AS is_primary
460
             FROM      user_ind_columns uind_col
461
             WHERE     uind_col.table_name = " . $table . "
462
             ORDER BY  uind_col.column_position ASC";
463
    }
464
465
    /**
466
     * {@inheritDoc}
467
     */
468
    public function getListTablesSQL()
469
    {
470
        return 'SELECT * FROM sys.user_tables';
471
    }
472
473
    /**
474
     * {@inheritDoc}
475
     */
476
    public function getListViewsSQL($database)
477
    {
478
        return 'SELECT view_name, text FROM sys.user_views';
479
    }
480
481
    /**
482
     * {@inheritDoc}
483
     */
484
    public function getCreateViewSQL($name, $sql)
485
    {
486
        return 'CREATE VIEW ' . $name . ' AS ' . $sql;
487
    }
488
489
    /**
490
     * {@inheritDoc}
491
     */
492
    public function getDropViewSQL($name)
493
    {
494
        return 'DROP VIEW '. $name;
495
    }
496
497
    /**
498
     * @param string $name
499
     * @param string $table
500
     * @param int    $start
501
     *
502
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
503
     */
504
    public function getCreateAutoincrementSql($name, $table, $start = 1)
505
    {
506
        $tableIdentifier = $this->normalizeIdentifier($table);
507
        $quotedTableName = $tableIdentifier->getQuotedName($this);
508
        $unquotedTableName = $tableIdentifier->getName();
509
510
        $nameIdentifier = $this->normalizeIdentifier($name);
511
        $quotedName = $nameIdentifier->getQuotedName($this);
512
        $unquotedName = $nameIdentifier->getName();
513
514
        $sql = [];
515
516
        $autoincrementIdentifierName = $this->getAutoincrementIdentifierName($tableIdentifier);
517
518
        $idx = new Index($autoincrementIdentifierName, [$quotedName], true, true);
519
520
        $sql[] = 'DECLARE
521
  constraints_Count NUMBER;
522
BEGIN
523
  SELECT COUNT(CONSTRAINT_NAME) INTO constraints_Count FROM USER_CONSTRAINTS WHERE TABLE_NAME = \'' . $unquotedTableName . '\' AND CONSTRAINT_TYPE = \'P\';
524
  IF constraints_Count = 0 OR constraints_Count = \'\' THEN
525
    EXECUTE IMMEDIATE \''.$this->getCreateConstraintSQL($idx, $quotedTableName).'\';
526
  END IF;
527
END;';
528
529
        $sequenceName = $this->getIdentitySequenceName(
530
            $tableIdentifier->isQuoted() ? $quotedTableName : $unquotedTableName,
531
            $nameIdentifier->isQuoted() ? $quotedName : $unquotedName
532
        );
533
        $sequence = new Sequence($sequenceName, $start);
534
        $sql[] = $this->getCreateSequenceSQL($sequence);
535
536
        $sql[] = 'CREATE TRIGGER ' . $autoincrementIdentifierName . '
537
   BEFORE INSERT
538
   ON ' . $quotedTableName . '
539
   FOR EACH ROW
540
DECLARE
541
   last_Sequence NUMBER;
542
   last_InsertID NUMBER;
543
BEGIN
544
   SELECT ' . $sequenceName . '.NEXTVAL INTO :NEW.' . $quotedName . ' FROM DUAL;
545
   IF (:NEW.' . $quotedName . ' IS NULL OR :NEW.'.$quotedName.' = 0) THEN
546
      SELECT ' . $sequenceName . '.NEXTVAL INTO :NEW.' . $quotedName . ' FROM DUAL;
547
   ELSE
548
      SELECT NVL(Last_Number, 0) INTO last_Sequence
549
        FROM User_Sequences
550
       WHERE Sequence_Name = \'' . $sequence->getName() . '\';
551
      SELECT :NEW.' . $quotedName . ' INTO last_InsertID FROM DUAL;
552
      WHILE (last_InsertID > last_Sequence) LOOP
553
         SELECT ' . $sequenceName . '.NEXTVAL INTO last_Sequence FROM DUAL;
554
      END LOOP;
555
   END IF;
556
END;';
557
558
        return $sql;
559
    }
560
561
    /**
562
     * Returns the SQL statements to drop the autoincrement for the given table name.
563
     *
564
     * @param string $table The table name to drop the autoincrement for.
565
     *
566
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
567
     */
568
    public function getDropAutoincrementSql($table)
569
    {
570
        $table = $this->normalizeIdentifier($table);
571
        $autoincrementIdentifierName = $this->getAutoincrementIdentifierName($table);
572
        $identitySequenceName = $this->getIdentitySequenceName(
573
            $table->isQuoted() ? $table->getQuotedName($this) : $table->getName(),
574
            ''
575
        );
576
577
        return [
578
            'DROP TRIGGER ' . $autoincrementIdentifierName,
579
            $this->getDropSequenceSQL($identitySequenceName),
580
            $this->getDropConstraintSQL($autoincrementIdentifierName, $table->getQuotedName($this)),
581
        ];
582
    }
583
584
    /**
585
     * Normalizes the given identifier.
586
     *
587
     * Uppercases the given identifier if it is not quoted by intention
588
     * to reflect Oracle's internal auto uppercasing strategy of unquoted identifiers.
589
     *
590
     * @param string $name The identifier to normalize.
591
     *
592
     * @return Identifier The normalized identifier.
593
     */
594
    private function normalizeIdentifier($name)
595
    {
596
        $identifier = new Identifier($name);
597
598
        return $identifier->isQuoted() ? $identifier : new Identifier(strtoupper($name));
599
    }
600
601
    /**
602
     * Returns the autoincrement primary key identifier name for the given table identifier.
603
     *
604
     * Quotes the autoincrement primary key identifier name
605
     * if the given table name is quoted by intention.
606
     *
607
     * @param Identifier $table The table identifier to return the autoincrement primary key identifier name for.
608
     *
609
     * @return string
610
     */
611
    private function getAutoincrementIdentifierName(Identifier $table)
612
    {
613
        $identifierName = $table->getName() . '_AI_PK';
614
615
        return $table->isQuoted()
616
            ? $this->quoteSingleIdentifier($identifierName)
617
            : $identifierName;
618
    }
619
620
    /**
621
     * {@inheritDoc}
622
     */
623 View Code Duplication
    public function getListTableForeignKeysSQL($table)
624
    {
625
        $table = $this->normalizeIdentifier($table);
626
        $table = $this->quoteStringLiteral($table->getName());
627
628
        return "SELECT alc.constraint_name,
629
          alc.DELETE_RULE,
630
          cols.column_name \"local_column\",
631
          cols.position,
632
          (
633
              SELECT r_cols.table_name
634
              FROM   user_cons_columns r_cols
635
              WHERE  alc.r_constraint_name = r_cols.constraint_name
636
              AND    r_cols.position = cols.position
637
          ) AS \"references_table\",
638
          (
639
              SELECT r_cols.column_name
640
              FROM   user_cons_columns r_cols
641
              WHERE  alc.r_constraint_name = r_cols.constraint_name
642
              AND    r_cols.position = cols.position
643
          ) AS \"foreign_column\"
644
     FROM user_cons_columns cols
645
     JOIN user_constraints alc
646
       ON alc.constraint_name = cols.constraint_name
647
      AND alc.constraint_type = 'R'
648
      AND alc.table_name = " . $table . "
649
    ORDER BY cols.constraint_name ASC, cols.position ASC";
650
    }
651
652
    /**
653
     * {@inheritDoc}
654
     */
655
    public function getListTableConstraintsSQL($table)
656
    {
657
        $table = $this->normalizeIdentifier($table);
658
        $table = $this->quoteStringLiteral($table->getName());
659
660
        return "SELECT * FROM user_constraints WHERE table_name = " . $table;
661
    }
662
663
    /**
664
     * {@inheritDoc}
665
     */
666
    public function getListTableColumnsSQL($table, $database = null)
667
    {
668
        $table = $this->normalizeIdentifier($table);
669
        $table = $this->quoteStringLiteral($table->getName());
670
671
        $tabColumnsTableName = "user_tab_columns";
672
        $colCommentsTableName = "user_col_comments";
673
        $tabColumnsOwnerCondition = '';
674
        $colCommentsOwnerCondition = '';
675
676
        if (null !== $database && '/' !== $database) {
677
            $database = $this->normalizeIdentifier($database);
678
            $database = $this->quoteStringLiteral($database->getName());
679
            $tabColumnsTableName = "all_tab_columns";
680
            $colCommentsTableName = "all_col_comments";
681
            $tabColumnsOwnerCondition = "AND c.owner = " . $database;
682
            $colCommentsOwnerCondition = "AND d.OWNER = c.OWNER";
683
        }
684
685
        return "SELECT   c.*,
686
                         (
687
                             SELECT d.comments
688
                             FROM   $colCommentsTableName d
689
                             WHERE  d.TABLE_NAME = c.TABLE_NAME " . $colCommentsOwnerCondition . "
690
                             AND    d.COLUMN_NAME = c.COLUMN_NAME
691
                         ) AS comments
692
                FROM     $tabColumnsTableName c
693
                WHERE    c.table_name = " . $table . " $tabColumnsOwnerCondition
694
                ORDER BY c.column_id";
695
    }
696
697
    /**
698
     * {@inheritDoc}
699
     */
700
    public function getDropSequenceSQL($sequence)
701
    {
702
        if ($sequence instanceof Sequence) {
703
            $sequence = $sequence->getQuotedName($this);
704
        }
705
706
        return 'DROP SEQUENCE ' . $sequence;
707
    }
708
709
    /**
710
     * {@inheritDoc}
711
     */
712
    public function getDropForeignKeySQL($foreignKey, $table)
713
    {
714
        if (! $foreignKey instanceof ForeignKeyConstraint) {
715
            $foreignKey = new Identifier($foreignKey);
716
        }
717
718
        if (! $table instanceof Table) {
719
            $table = new Identifier($table);
720
        }
721
722
        $foreignKey = $foreignKey->getQuotedName($this);
723
        $table = $table->getQuotedName($this);
724
725
        return 'ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $foreignKey;
726
    }
727
728
    /**
729
     * {@inheritdoc}
730
     */
731
    public function getAdvancedForeignKeyOptionsSQL(ForeignKeyConstraint $foreignKey)
732
    {
733
        $referentialAction = null;
734
735
        if ($foreignKey->hasOption('onDelete')) {
736
            $referentialAction = $this->getForeignKeyReferentialActionSQL($foreignKey->getOption('onDelete'));
737
        }
738
739
        return $referentialAction ? ' ON DELETE ' . $referentialAction : '';
740
    }
741
742
    /**
743
     * {@inheritdoc}
744
     */
745
    public function getForeignKeyReferentialActionSQL($action)
746
    {
747
        $action = strtoupper($action);
748
749
        switch ($action) {
750
            case 'RESTRICT': // RESTRICT is not supported, therefore falling back to NO ACTION.
751
            case 'NO ACTION':
752
                // NO ACTION cannot be declared explicitly,
753
                // therefore returning empty string to indicate to OMIT the referential clause.
754
                return '';
755
756
            case 'CASCADE':
757
            case 'SET NULL':
758
                return $action;
759
760
            default:
761
                // SET DEFAULT is not supported, throw exception instead.
762
                throw new \InvalidArgumentException('Invalid foreign key action: ' . $action);
763
        }
764
    }
765
766
    /**
767
     * {@inheritDoc}
768
     */
769
    public function getDropDatabaseSQL($database)
770
    {
771
        return 'DROP USER ' . $database . ' CASCADE';
772
    }
773
774
    /**
775
     * {@inheritDoc}
776
     */
777
    public function getAlterTableSQL(TableDiff $diff)
778
    {
779
        $sql = [];
780
        $commentsSQL = [];
781
        $columnSql = [];
782
783
        $fields = [];
784
785
        foreach ($diff->addedColumns as $column) {
786
            if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
787
                continue;
788
            }
789
790
            $fields[] = $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
791 View Code Duplication
            if ($comment = $this->getColumnComment($column)) {
792
                $commentsSQL[] = $this->getCommentOnColumnSQL(
793
                    $diff->getName($this)->getQuotedName($this),
794
                    $column->getQuotedName($this),
795
                    $comment
796
                );
797
            }
798
        }
799
800
        if (count($fields)) {
801
            $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ADD (' . implode(', ', $fields) . ')';
802
        }
803
804
        $fields = [];
805
        foreach ($diff->changedColumns as $columnDiff) {
806
            if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) {
807
                continue;
808
            }
809
810
            /* @var $columnDiff \Doctrine\DBAL\Schema\ColumnDiff */
811
            $column = $columnDiff->column;
812
813
            // Do not generate column alteration clause if type is binary and only fixed property has changed.
814
            // Oracle only supports binary type columns with variable length.
815
            // Avoids unnecessary table alteration statements.
816
            if ($column->getType() instanceof BinaryType &&
817
                $columnDiff->hasChanged('fixed') &&
818
                count($columnDiff->changedProperties) === 1
819
            ) {
820
                continue;
821
            }
822
823
            $columnHasChangedComment = $columnDiff->hasChanged('comment');
824
825
            /**
826
             * Do not add query part if only comment has changed
827
             */
828
            if ( ! ($columnHasChangedComment && count($columnDiff->changedProperties) === 1)) {
829
                $columnInfo = $column->toArray();
830
831
                if ( ! $columnDiff->hasChanged('notnull')) {
832
                    unset($columnInfo['notnull']);
833
                }
834
835
                $fields[] = $column->getQuotedName($this) . $this->getColumnDeclarationSQL('', $columnInfo);
836
            }
837
838 View Code Duplication
            if ($columnHasChangedComment) {
839
                $commentsSQL[] = $this->getCommentOnColumnSQL(
840
                    $diff->getName($this)->getQuotedName($this),
841
                    $column->getQuotedName($this),
842
                    $this->getColumnComment($column)
843
                );
844
            }
845
        }
846
847
        if (count($fields)) {
848
            $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' MODIFY (' . implode(', ', $fields) . ')';
849
        }
850
851 View Code Duplication
        foreach ($diff->renamedColumns as $oldColumnName => $column) {
852
            if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) {
853
                continue;
854
            }
855
856
            $oldColumnName = new Identifier($oldColumnName);
857
858
            $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) .
859
                ' RENAME COLUMN ' . $oldColumnName->getQuotedName($this) .' TO ' . $column->getQuotedName($this);
860
        }
861
862
        $fields = [];
863 View Code Duplication
        foreach ($diff->removedColumns as $column) {
864
            if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) {
865
                continue;
866
            }
867
868
            $fields[] = $column->getQuotedName($this);
869
        }
870
871
        if (count($fields)) {
872
            $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' DROP (' . implode(', ', $fields).')';
873
        }
874
875
        $tableSql = [];
876
877 View Code Duplication
        if ( ! $this->onSchemaAlterTable($diff, $tableSql)) {
878
            $sql = array_merge($sql, $commentsSQL);
879
880
            if ($diff->newName !== false) {
881
                $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' RENAME TO ' . $diff->getNewName()->getQuotedName($this);
882
            }
883
884
            $sql = array_merge(
885
                $this->getPreAlterTableIndexForeignKeySQL($diff),
886
                $sql,
887
                $this->getPostAlterTableIndexForeignKeySQL($diff)
888
            );
889
        }
890
891
        return array_merge($sql, $tableSql, $columnSql);
892
    }
893
894
    /**
895
     * {@inheritdoc}
896
     */
897
    public function getColumnDeclarationSQL($name, array $field)
898
    {
899
        if (isset($field['columnDefinition'])) {
900
            $columnDef = $this->getCustomTypeDeclarationSQL($field);
901
        } else {
902
            $default = $this->getDefaultValueDeclarationSQL($field);
903
904
            $notnull = '';
905
906
            if (isset($field['notnull'])) {
907
                $notnull = $field['notnull'] ? ' NOT NULL' : ' NULL';
908
            }
909
910
            $unique = (isset($field['unique']) && $field['unique']) ?
911
                ' ' . $this->getUniqueFieldDeclarationSQL() : '';
912
913
            $check = (isset($field['check']) && $field['check']) ?
914
                ' ' . $field['check'] : '';
915
916
            $typeDecl = $field['type']->getSQLDeclaration($field, $this);
917
            $columnDef = $typeDecl . $default . $notnull . $unique . $check;
918
        }
919
920
        return $name . ' ' . $columnDef;
921
    }
922
923
    /**
924
     * {@inheritdoc}
925
     */
926 View Code Duplication
    protected function getRenameIndexSQL($oldIndexName, Index $index, $tableName)
927
    {
928
        if (strpos($tableName, '.') !== false) {
929
            list($schema) = explode('.', $tableName);
930
            $oldIndexName = $schema . '.' . $oldIndexName;
931
        }
932
933
        return ['ALTER INDEX ' . $oldIndexName . ' RENAME TO ' . $index->getQuotedName($this)];
934
    }
935
936
    /**
937
     * {@inheritDoc}
938
     */
939
    public function prefersSequences()
940
    {
941
        return true;
942
    }
943
944
    /**
945
     * {@inheritdoc}
946
     */
947
    public function usesSequenceEmulatedIdentityColumns()
948
    {
949
        return true;
950
    }
951
952
    /**
953
     * {@inheritdoc}
954
     */
955
    public function getIdentitySequenceName($tableName, $columnName)
956
    {
957
        $table = new Identifier($tableName);
958
959
        // No usage of column name to preserve BC compatibility with <2.5
960
        $identitySequenceName = $table->getName() . '_SEQ';
961
962
        if ($table->isQuoted()) {
963
            $identitySequenceName = '"' . $identitySequenceName . '"';
964
        }
965
966
        $identitySequenceIdentifier = $this->normalizeIdentifier($identitySequenceName);
967
968
        return $identitySequenceIdentifier->getQuotedName($this);
969
    }
970
971
    /**
972
     * {@inheritDoc}
973
     */
974
    public function supportsCommentOnStatement()
975
    {
976
        return true;
977
    }
978
979
    /**
980
     * {@inheritDoc}
981
     */
982
    public function getName()
983
    {
984
        return 'oracle';
985
    }
986
987
    /**
988
     * {@inheritDoc}
989
     */
990
    protected function doModifyLimitQuery($query, $limit, $offset = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
991
    {
992
        if ($limit === null && $offset <= 0) {
993
            return $query;
994
        }
995
996
        if (preg_match('/^\s*SELECT/i', $query)) {
997
            if (!preg_match('/\sFROM\s/i', $query)) {
998
                $query .= " FROM dual";
999
            }
1000
1001
            $columns = ['a.*'];
1002
1003
            if ($offset > 0) {
1004
                $columns[] = 'ROWNUM AS doctrine_rownum';
1005
            }
1006
1007
            $query = sprintf('SELECT %s FROM (%s) a', implode(', ', $columns), $query);
1008
1009
            if ($limit !== null) {
1010
                $query .= sprintf(' WHERE ROWNUM <= %d', $offset + $limit);
1011
            }
1012
1013
            if ($offset > 0) {
1014
                $query = sprintf('SELECT * FROM (%s) WHERE doctrine_rownum >= %d', $query, $offset + 1);
1015
            }
1016
        }
1017
1018
        return $query;
1019
    }
1020
1021
    /**
1022
     * {@inheritDoc}
1023
     *
1024
     * Oracle returns all column names in SQL result sets in uppercase.
1025
     */
1026
    public function getSQLResultCasing($column)
1027
    {
1028
        return strtoupper($column);
1029
    }
1030
1031
    /**
1032
     * {@inheritDoc}
1033
     */
1034
    public function getCreateTemporaryTableSnippetSQL()
1035
    {
1036
        return "CREATE GLOBAL TEMPORARY TABLE";
1037
    }
1038
1039
    /**
1040
     * {@inheritDoc}
1041
     */
1042
    public function getDateTimeTzFormatString()
1043
    {
1044
        return 'Y-m-d H:i:sP';
1045
    }
1046
1047
    /**
1048
     * {@inheritDoc}
1049
     */
1050
    public function getDateFormatString()
1051
    {
1052
        return 'Y-m-d 00:00:00';
1053
    }
1054
1055
    /**
1056
     * {@inheritDoc}
1057
     */
1058
    public function getTimeFormatString()
1059
    {
1060
        return '1900-01-01 H:i:s';
1061
    }
1062
1063
    /**
1064
     * {@inheritDoc}
1065
     */
1066
    public function fixSchemaElementName($schemaElementName)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
1067
    {
1068
        if (strlen($schemaElementName) > 30) {
1069
            // Trim it
1070
            return substr($schemaElementName, 0, 30);
1071
        }
1072
1073
        return $schemaElementName;
1074
    }
1075
1076
    /**
1077
     * {@inheritDoc}
1078
     */
1079
    public function getMaxIdentifierLength()
1080
    {
1081
        return 30;
1082
    }
1083
1084
    /**
1085
     * {@inheritDoc}
1086
     */
1087
    public function supportsSequences()
1088
    {
1089
        return true;
1090
    }
1091
1092
    /**
1093
     * {@inheritDoc}
1094
     */
1095
    public function supportsForeignKeyOnUpdate()
1096
    {
1097
        return false;
1098
    }
1099
1100
    /**
1101
     * {@inheritDoc}
1102
     */
1103
    public function supportsReleaseSavepoints()
1104
    {
1105
        return false;
1106
    }
1107
1108
    /**
1109
     * {@inheritDoc}
1110
     */
1111
    public function getTruncateTableSQL($tableName, $cascade = false)
1112
    {
1113
        $tableIdentifier = new Identifier($tableName);
1114
1115
        return 'TRUNCATE TABLE ' . $tableIdentifier->getQuotedName($this);
1116
    }
1117
1118
    /**
1119
     * {@inheritDoc}
1120
     */
1121
    public function getDummySelectSQL()
1122
    {
1123
        $expression = func_num_args() > 0 ? func_get_arg(0) : '1';
1124
1125
        return sprintf('SELECT %s FROM DUAL', $expression);
1126
    }
1127
1128
    /**
1129
     * {@inheritDoc}
1130
     */
1131
    protected function initializeDoctrineTypeMappings()
1132
    {
1133
        $this->doctrineTypeMapping = [
1134
            'integer'           => 'integer',
1135
            'number'            => 'integer',
1136
            'pls_integer'       => 'boolean',
1137
            'binary_integer'    => 'boolean',
1138
            'varchar'           => 'string',
1139
            'varchar2'          => 'string',
1140
            'nvarchar2'         => 'string',
1141
            'char'              => 'string',
1142
            'nchar'             => 'string',
1143
            'date'              => 'date',
1144
            'timestamp'         => 'datetime',
1145
            'timestamptz'       => 'datetimetz',
1146
            'float'             => 'float',
1147
            'binary_float'      => 'float',
1148
            'binary_double'     => 'float',
1149
            'long'              => 'string',
1150
            'clob'              => 'text',
1151
            'nclob'             => 'text',
1152
            'raw'               => 'binary',
1153
            'long raw'          => 'blob',
1154
            'rowid'             => 'string',
1155
            'urowid'            => 'string',
1156
            'blob'              => 'blob',
1157
        ];
1158
    }
1159
1160
    /**
1161
     * {@inheritDoc}
1162
     */
1163
    public function releaseSavePoint($savepoint)
1164
    {
1165
        return '';
1166
    }
1167
1168
    /**
1169
     * {@inheritDoc}
1170
     */
1171
    protected function getReservedKeywordsClass()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
1172
    {
1173
        return Keywords\OracleKeywords::class;
1174
    }
1175
1176
    /**
1177
     * {@inheritDoc}
1178
     */
1179
    public function getBlobTypeDeclarationSQL(array $field)
1180
    {
1181
        return 'BLOB';
1182
    }
1183
1184
    /**
1185
     * {@inheritdoc}
1186
     */
1187
    public function quoteStringLiteral($str)
1188
    {
1189
        $str = str_replace('\\', '\\\\', $str); // Oracle requires backslashes to be escaped aswell.
1190
1191
        return parent::quoteStringLiteral($str);
1192
    }
1193
}
1194