Failed Conditions
Pull Request — develop (#3348)
by Sergei
60:53
created

DB2SchemaManager   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Test Coverage

Coverage 14.46%

Importance

Changes 0
Metric Value
wmc 26
eloc 84
dl 0
loc 169
ccs 12
cts 83
cp 0.1446
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A _getPortableTablesList() 0 9 2
A _getPortableTableIndexesList() 0 8 2
A _getPortableViewDefinition() 0 13 2
A _getPortableTableForeignKeyDefinition() 0 8 1
F _getPortableTableColumnDefinition() 0 60 15
A listTableNames() 0 7 1
A _getPortableTableForeignKeysList() 0 25 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Schema;
6
7
use Doctrine\DBAL\Types\Type;
8
use const CASE_LOWER;
9
use function array_change_key_case;
10
use function is_resource;
11
use function preg_match;
12
use function str_replace;
13
use function strpos;
14
use function strtolower;
15
use function substr;
16
17
/**
18
 * IBM Db2 Schema Manager.
19
 */
20
class DB2SchemaManager extends AbstractSchemaManager
21
{
22
    /**
23
     * {@inheritdoc}
24
     *
25
     * Apparently creator is the schema not the user who created it:
26
     * {@link http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.sqlref/db2z_sysibmsystablestable.htm}
27
     */
28
    public function listTableNames() : array
29 135
    {
30
        $sql = $this->_platform->getListTablesSQL() . ' AND CREATOR = CURRENT_USER';
31 135
32 135
        $tables = $this->_conn->fetchAll($sql);
33
34 135
        return $this->filterAssetNames($this->_getPortableTablesList($tables));
35
    }
36 135
37
    /**
38 135
     * {@inheritdoc}
39
     */
40
    protected function _getPortableTableColumnDefinition(array $tableColumn) : Column
41
    {
42
        $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
43
44
        $length = $precision = $default = null;
45
        $scale  = 0;
46
        $fixed  = false;
47
48
        if ($tableColumn['default'] !== null && $tableColumn['default'] !== 'NULL') {
49
            $default = $tableColumn['default'];
50
51
            if (preg_match('/^\'(.*)\'$/s', $default, $matches)) {
52
                $default = str_replace("''", "'", $matches[1]);
53
            }
54
        }
55
56
        $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'])
57
            ?? $this->_platform->getDoctrineTypeMapping($tableColumn['typename']);
58
59
        switch (strtolower($tableColumn['typename'])) {
60
            case 'varchar':
61
                $length = $tableColumn['length'];
62
                break;
63
            case 'character':
64
                $length = $tableColumn['length'];
65
                $fixed  = true;
66
                break;
67
            case 'clob':
68
                $length = $tableColumn['length'];
69
                break;
70
            case 'decimal':
71
            case 'double':
72
            case 'real':
73
                $scale     = $tableColumn['scale'];
74
                $precision = $tableColumn['length'];
75
                break;
76
        }
77
78
        $options = [
79
            'length'        => $length,
80
            'unsigned'      => false,
81
            'default'       => $default,
82
            'autoincrement' => (bool) $tableColumn['autoincrement'],
83
            'notnull'       => $tableColumn['nulls'] === 'N',
84
            'comment'       => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
85
                ? $tableColumn['comment']
86
                : null,
87
            'platformOptions' => [],
88
        ];
89
90
        if ($fixed !== null) {
0 ignored issues
show
introduced by
The condition $fixed !== null is always true.
Loading history...
91
            $options['fixed'] = $fixed;
92
        }
93
94
        if ($scale !== null && $precision !== null) {
95
            $options['scale']     = $scale;
96
            $options['precision'] = $precision;
97
        }
98
99
        return new Column($tableColumn['colname'], Type::getType($type), $options);
100
    }
101
102 135
    /**
103
     * {@inheritdoc}
104 135
     */
105 135
    protected function _getPortableTablesList(array $tables) : array
106 135
    {
107 135
        $tableNames = [];
108
        foreach ($tables as $tableRow) {
109
            $tableRow     = array_change_key_case($tableRow, CASE_LOWER);
110 135
            $tableNames[] = $tableRow['name'];
111
        }
112
113
        return $tableNames;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    protected function _getPortableTableIndexesList(array $tableIndexRows, string $tableName) : array
120
    {
121
        foreach ($tableIndexRows as &$tableIndexRow) {
122
            $tableIndexRow            = array_change_key_case($tableIndexRow, CASE_LOWER);
123
            $tableIndexRow['primary'] = (bool) $tableIndexRow['primary'];
124
        }
125
126
        return parent::_getPortableTableIndexesList($tableIndexRows, $tableName);
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    protected function _getPortableTableForeignKeyDefinition(array $tableForeignKey) : ForeignKeyConstraint
133
    {
134
        return new ForeignKeyConstraint(
135
            $tableForeignKey['local_columns'],
136
            $tableForeignKey['foreign_table'],
137
            $tableForeignKey['foreign_columns'],
138
            $tableForeignKey['name'],
139
            $tableForeignKey['options']
140
        );
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    protected function _getPortableTableForeignKeysList(array $tableForeignKeys) : array
147
    {
148
        $foreignKeys = [];
149
150
        foreach ($tableForeignKeys as $tableForeignKey) {
151
            $tableForeignKey = array_change_key_case($tableForeignKey, CASE_LOWER);
152
153
            if (! isset($foreignKeys[$tableForeignKey['index_name']])) {
154
                $foreignKeys[$tableForeignKey['index_name']] = [
155
                    'local_columns'   => [$tableForeignKey['local_column']],
156
                    'foreign_table'   => $tableForeignKey['foreign_table'],
157
                    'foreign_columns' => [$tableForeignKey['foreign_column']],
158
                    'name'            => $tableForeignKey['index_name'],
159
                    'options'         => [
160
                        'onUpdate' => $tableForeignKey['on_update'],
161
                        'onDelete' => $tableForeignKey['on_delete'],
162
                    ],
163
                ];
164
            } else {
165
                $foreignKeys[$tableForeignKey['index_name']]['local_columns'][]   = $tableForeignKey['local_column'];
166
                $foreignKeys[$tableForeignKey['index_name']]['foreign_columns'][] = $tableForeignKey['foreign_column'];
167
            }
168
        }
169
170
        return parent::_getPortableTableForeignKeysList($foreignKeys);
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176
    protected function _getPortableViewDefinition(array $view) : View
177
    {
178
        $view = array_change_key_case($view, CASE_LOWER);
179
        // sadly this still segfaults on PDO_IBM, see http://pecl.php.net/bugs/bug.php?id=17199
180
        //$view['text'] = (is_resource($view['text']) ? stream_get_contents($view['text']) : $view['text']);
181
        if (! is_resource($view['text'])) {
182
            $pos = strpos($view['text'], ' AS ');
183
            $sql = substr($view['text'], $pos+4);
184
        } else {
185
            $sql = '';
186
        }
187
188
        return new View($view['name'], $sql);
189
    }
190
}
191