Failed Conditions
Pull Request — master (#3973)
by Grégoire
03:04
created

DB2SchemaManager   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Test Coverage

Coverage 10.87%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 90
dl 0
loc 187
ccs 10
cts 92
cp 0.1087
rs 10
c 1
b 0
f 0
wmc 26

8 Methods

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