Failed Conditions
Pull Request — master (#3133)
by Michael
25:15 queued 21:18
created

OracleSchemaManager::_getPortableViewDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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\Schema;
21
22
use Doctrine\DBAL\DBALException;
23
use Doctrine\DBAL\Driver\DriverException;
24
use Doctrine\DBAL\Types\Type;
25
use const CASE_LOWER;
26
use function array_change_key_case;
27
use function array_values;
28
use function is_null;
29
use function preg_match;
30
use function sprintf;
31
use function strpos;
32
use function strtolower;
33
use function strtoupper;
34
use function trim;
35
36
/**
37
 * Oracle Schema Manager.
38
 *
39
 * @author Konsta Vesterinen <[email protected]>
40
 * @author Lukas Smith <[email protected]> (PEAR MDB2 library)
41
 * @author Benjamin Eberlei <[email protected]>
42
 * @since  2.0
43
 */
44
class OracleSchemaManager extends AbstractSchemaManager
45
{
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function dropDatabase($database)
50
    {
51
        try {
52
            parent::dropDatabase($database);
53
        } catch (DBALException $exception) {
54
            $exception = $exception->getPrevious();
55
56
            if (! $exception instanceof DriverException) {
57
                throw $exception;
58
            }
59
60
            // If we have a error code 1940 (ORA-01940), the drop database operation failed
61
            // because of active connections on the database.
62
            // To force dropping the database, we first have to close all active connections
63
            // on that database and issue the drop database operation again.
64
            if ($exception->getErrorCode() !== 1940) {
65
                throw $exception;
66
            }
67
68
            $this->killUserSessions($database);
69
70
            parent::dropDatabase($database);
71
        }
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    protected function _getPortableViewDefinition($view)
78
    {
79
        $view = \array_change_key_case($view, CASE_LOWER);
80
81
        return new View($this->getQuotedIdentifierName($view['view_name']), $view['text']);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    protected function _getPortableUserDefinition($user)
88
    {
89
        $user = \array_change_key_case($user, CASE_LOWER);
90
91
        return [
92
            'user' => $user['username'],
93
        ];
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    protected function _getPortableTableDefinition($table)
100
    {
101
        $table = \array_change_key_case($table, CASE_LOWER);
102
103
        return $this->getQuotedIdentifierName($table['table_name']);
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     *
109
     * @license New BSD License
110
     * @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaPgsqlReader.html
111
     */
112
    protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
113
    {
114
        $indexBuffer = [];
115
        foreach ($tableIndexes as $tableIndex) {
116
            $tableIndex = \array_change_key_case($tableIndex, CASE_LOWER);
117
118
            $keyName = strtolower($tableIndex['name']);
119
120
            if (strtolower($tableIndex['is_primary']) == "p") {
121
                $keyName = 'primary';
122
                $buffer['primary'] = true;
123
                $buffer['non_unique'] = false;
124
            } else {
125
                $buffer['primary'] = false;
126
                $buffer['non_unique'] = ($tableIndex['is_unique'] == 0) ? true : false;
127
            }
128
            $buffer['key_name'] = $keyName;
129
            $buffer['column_name'] = $this->getQuotedIdentifierName($tableIndex['column_name']);
130
            $indexBuffer[] = $buffer;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $buffer seems to be defined later in this foreach loop on line 122. Are you sure it is defined here?
Loading history...
131
        }
132
133
        return parent::_getPortableTableIndexesList($indexBuffer, $tableName);
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    protected function _getPortableTableColumnDefinition($tableColumn)
140
    {
141
        $tableColumn = \array_change_key_case($tableColumn, CASE_LOWER);
142
143
        $dbType = strtolower($tableColumn['data_type']);
144
        if (strpos($dbType, "timestamp(") === 0) {
145
            if (strpos($dbType, "with time zone")) {
146
                $dbType = "timestamptz";
147
            } else {
148
                $dbType = "timestamp";
149
            }
150
        }
151
152
        $unsigned = $fixed = null;
153
154
        if ( ! isset($tableColumn['column_name'])) {
155
            $tableColumn['column_name'] = '';
156
        }
157
158
        // Default values returned from database sometimes have trailing spaces.
159
        $tableColumn['data_default'] = trim($tableColumn['data_default']);
160
161
        if ($tableColumn['data_default'] === '' || $tableColumn['data_default'] === 'NULL') {
162
            $tableColumn['data_default'] = null;
163
        }
164
165
        if (null !== $tableColumn['data_default']) {
166
            // Default values returned from database are enclosed in single quotes.
167
            $tableColumn['data_default'] = trim($tableColumn['data_default'], "'");
168
        }
169
170
        $precision = null;
171
        $scale = null;
172
173
        $type = $this->_platform->getDoctrineTypeMapping($dbType);
174
        $type = $this->extractDoctrineTypeFromComment($tableColumn['comments'], $type);
175
        $tableColumn['comments'] = $this->removeDoctrineTypeFromComment($tableColumn['comments'], $type);
176
177
        switch ($dbType) {
178
            case 'number':
179
                if ($tableColumn['data_precision'] == 20 && $tableColumn['data_scale'] == 0) {
180
                    $precision = 20;
181
                    $scale = 0;
182
                    $type = 'bigint';
183
                } elseif ($tableColumn['data_precision'] == 5 && $tableColumn['data_scale'] == 0) {
184
                    $type = 'smallint';
185
                    $precision = 5;
186
                    $scale = 0;
187
                } elseif ($tableColumn['data_precision'] == 1 && $tableColumn['data_scale'] == 0) {
188
                    $precision = 1;
189
                    $scale = 0;
190
                    $type = 'boolean';
191
                } elseif ($tableColumn['data_scale'] > 0) {
192
                    $precision = $tableColumn['data_precision'];
193
                    $scale = $tableColumn['data_scale'];
194
                    $type = 'decimal';
195
                }
196
                $length = null;
197
                break;
198
            case 'pls_integer':
199
            case 'binary_integer':
200
                $length = null;
201
                break;
202
            case 'varchar':
203
            case 'varchar2':
204
            case 'nvarchar2':
205
                $length = $tableColumn['char_length'];
206
                $fixed = false;
207
                break;
208
            case 'char':
209
            case 'nchar':
210
                $length = $tableColumn['char_length'];
211
                $fixed = true;
212
                break;
213
            case 'date':
214
            case 'timestamp':
215
                $length = null;
216
                break;
217
            case 'float':
218
            case 'binary_float':
219
            case 'binary_double':
220
                $precision = $tableColumn['data_precision'];
221
                $scale = $tableColumn['data_scale'];
222
                $length = null;
223
                break;
224
            case 'clob':
225
            case 'nclob':
226
                $length = null;
227
                break;
228
            case 'blob':
229
            case 'raw':
230
            case 'long raw':
231
            case 'bfile':
232
                $length = null;
233
                break;
234
            case 'rowid':
235
            case 'urowid':
236
            default:
237
                $length = null;
238
        }
239
240
        $options = [
241
            'notnull'    => (bool) ($tableColumn['nullable'] === 'N'),
242
            'fixed'      => (bool) $fixed,
243
            'unsigned'   => (bool) $unsigned,
244
            'default'    => $tableColumn['data_default'],
245
            'length'     => $length,
246
            'precision'  => $precision,
247
            'scale'      => $scale,
248
            'comment'    => isset($tableColumn['comments']) && '' !== $tableColumn['comments']
249
                ? $tableColumn['comments']
250
                : null,
251
        ];
252
253
        return new Column($this->getQuotedIdentifierName($tableColumn['column_name']), Type::getType($type), $options);
254
    }
255
256
    /**
257
     * {@inheritdoc}
258
     */
259
    protected function _getPortableTableForeignKeysList($tableForeignKeys)
260
    {
261
        $list = [];
262
        foreach ($tableForeignKeys as $value) {
263
            $value = \array_change_key_case($value, CASE_LOWER);
264
            if (!isset($list[$value['constraint_name']])) {
265
                if ($value['delete_rule'] == "NO ACTION") {
266
                    $value['delete_rule'] = null;
267
                }
268
269
                $list[$value['constraint_name']] = [
270
                    'name' => $this->getQuotedIdentifierName($value['constraint_name']),
271
                    'local' => [],
272
                    'foreign' => [],
273
                    'foreignTable' => $value['references_table'],
274
                    'onDelete' => $value['delete_rule'],
275
                ];
276
            }
277
278
            $localColumn = $this->getQuotedIdentifierName($value['local_column']);
279
            $foreignColumn = $this->getQuotedIdentifierName($value['foreign_column']);
280
281
            $list[$value['constraint_name']]['local'][$value['position']] = $localColumn;
282
            $list[$value['constraint_name']]['foreign'][$value['position']] = $foreignColumn;
283
        }
284
285
        $result = [];
286
        foreach ($list as $constraint) {
287
            $result[] = new ForeignKeyConstraint(
288
                array_values($constraint['local']), $this->getQuotedIdentifierName($constraint['foreignTable']),
289
                array_values($constraint['foreign']), $this->getQuotedIdentifierName($constraint['name']),
290
                ['onDelete' => $constraint['onDelete']]
291
            );
292
        }
293
294
        return $result;
295
    }
296
297
    /**
298
     * {@inheritdoc}
299
     */
300
    protected function _getPortableSequenceDefinition($sequence)
301
    {
302
        $sequence = \array_change_key_case($sequence, CASE_LOWER);
303
304
        return new Sequence(
305
            $this->getQuotedIdentifierName($sequence['sequence_name']),
306
            (int) $sequence['increment_by'],
307
            (int) $sequence['min_value']
308
        );
309
    }
310
311
    /**
312
     * {@inheritdoc}
313
     */
314
    protected function _getPortableFunctionDefinition($function)
315
    {
316
        $function = \array_change_key_case($function, CASE_LOWER);
317
318
        return $function['name'];
319
    }
320
321
    /**
322
     * {@inheritdoc}
323
     */
324
    protected function _getPortableDatabaseDefinition($database)
325
    {
326
        $database = \array_change_key_case($database, CASE_LOWER);
327
328
        return $database['username'];
329
    }
330
331
    /**
332
     * {@inheritdoc}
333
     */
334
    public function createDatabase($database = null)
335
    {
336
        if (is_null($database)) {
337
            $database = $this->_conn->getDatabase();
338
        }
339
340
        $params = $this->_conn->getParams();
341
        $username   = $database;
342
        $password   = $params['password'];
343
344
        $query  = 'CREATE USER ' . $username . ' IDENTIFIED BY ' . $password;
345
        $this->_conn->executeUpdate($query);
346
347
        $query = 'GRANT DBA TO ' . $username;
348
        $this->_conn->executeUpdate($query);
349
350
        return true;
351
    }
352
353
    /**
354
     * @param string $table
355
     *
356
     * @return bool
357
     */
358
    public function dropAutoincrement($table)
359
    {
360
        $sql = $this->_platform->getDropAutoincrementSql($table);
0 ignored issues
show
Bug introduced by
The method getDropAutoincrementSql() does not exist on Doctrine\DBAL\Platforms\AbstractPlatform. It seems like you code against a sub-type of Doctrine\DBAL\Platforms\AbstractPlatform such as Doctrine\DBAL\Platforms\OraclePlatform. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

360
        /** @scrutinizer ignore-call */ 
361
        $sql = $this->_platform->getDropAutoincrementSql($table);
Loading history...
361
        foreach ($sql as $query) {
362
            $this->_conn->executeUpdate($query);
363
        }
364
365
        return true;
366
    }
367
368
    /**
369
     * {@inheritdoc}
370
     */
371
    public function dropTable($name)
372
    {
373
        $this->tryMethod('dropAutoincrement', $name);
374
375
        parent::dropTable($name);
376
    }
377
378
    /**
379
     * Returns the quoted representation of the given identifier name.
380
     *
381
     * Quotes non-uppercase identifiers explicitly to preserve case
382
     * and thus make references to the particular identifier work.
383
     *
384
     * @param string $identifier The identifier to quote.
385
     *
386
     * @return string The quoted identifier.
387
     */
388
    private function getQuotedIdentifierName($identifier)
389
    {
390
        if (preg_match('/[a-z]/', $identifier)) {
391
            return $this->_platform->quoteIdentifier($identifier);
392
        }
393
394
        return $identifier;
395
    }
396
397
    /**
398
     * Kills sessions connected with the given user.
399
     *
400
     * This is useful to force DROP USER operations which could fail because of active user sessions.
401
     *
402
     * @param string $user The name of the user to kill sessions for.
403
     *
404
     * @return void
405
     */
406
    private function killUserSessions($user)
407
    {
408
        $sql = <<<SQL
409
SELECT
410
    s.sid,
411
    s.serial#
412
FROM
413
    gv\$session s,
414
    gv\$process p
415
WHERE
416
    s.username = ?
417
    AND p.addr(+) = s.paddr
418
SQL;
419
420
        $activeUserSessions = $this->_conn->fetchAll($sql, [strtoupper($user)]);
421
422
        foreach ($activeUserSessions as $activeUserSession) {
423
            $activeUserSession = array_change_key_case($activeUserSession, \CASE_LOWER);
424
425
            $this->_execSql(
426
                sprintf(
427
                    "ALTER SYSTEM KILL SESSION '%s, %s' IMMEDIATE",
428
                    $activeUserSession['sid'],
429
                    $activeUserSession['serial#']
430
                )
431
            );
432
        }
433
    }
434
}
435