Completed
Push — development ( 29b82e...e01cc9 )
by Ashutosh
10:05
created

OraclePlatform::getAlterTableSQL()   F

Complexity

Conditions 22
Paths 7776

Size

Total Lines 115
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 60
dl 0
loc 115
rs 0
c 0
b 0
f 0
cc 22
nc 7776
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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