Passed
Pull Request — 2.10.x (#3979)
by Ben
08:48
created

DB2SchemaManager   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Test Coverage

Coverage 10.58%

Importance

Changes 0
Metric Value
wmc 31
eloc 104
c 0
b 0
f 0
dl 0
loc 218
ccs 11
cts 104
cp 0.1058
rs 9.92

9 Methods

Rating   Name   Duplication   Size   Complexity  
A listTableNames() 0 8 1
A _getPortableTablesList() 0 9 2
A _getPortableForeignKeyRuleDef() 0 11 3
A _getPortableTableForeignKeyDefinition() 0 8 1
F _getPortableTableColumnDefinition() 0 70 15
A listTableDetails() 0 15 2
A _getPortableTableForeignKeysList() 0 25 3
A _getPortableViewDefinition() 0 13 2
A _getPortableTableIndexesList() 0 8 2
1
<?php
2
3
namespace Doctrine\DBAL\Schema;
4
5
use Doctrine\DBAL\Platforms\DB2Platform;
6
use Doctrine\DBAL\Types\Type;
7
use function array_change_key_case;
8
use function assert;
9
use function is_resource;
10
use function preg_match;
11
use function str_replace;
12
use function strpos;
13
use function strtolower;
14
use function substr;
15
use const CASE_LOWER;
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 130
    public function listTableNames()
29
    {
30 130
        $sql  = $this->_platform->getListTablesSQL();
31 130
        $sql .= ' AND CREATOR = UPPER(' . $this->_conn->quote($this->_conn->getUsername()) . ')';
32
33 130
        $tables = $this->_conn->fetchAll($sql);
34
35 130
        return $this->filterAssetNames($this->_getPortableTablesList($tables));
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function _getPortableTableColumnDefinition($tableColumn)
42
    {
43
        $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
44
45
        $length    = null;
46
        $fixed     = null;
47
        $scale     = false;
48
        $precision = false;
49
50
        $default = null;
51
52
        if ($tableColumn['default'] !== null && $tableColumn['default'] !== 'NULL') {
53
            $default = $tableColumn['default'];
54
55
            if (preg_match('/^\'(.*)\'$/s', $default, $matches)) {
56
                $default = str_replace("''", "'", $matches[1]);
57
            }
58
        }
59
60
        $type = $this->_platform->getDoctrineTypeMapping($tableColumn['typename']);
61
62
        if (isset($tableColumn['comment'])) {
63
            $type                   = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
64
            $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
65
        }
66
67
        switch (strtolower($tableColumn['typename'])) {
68
            case 'varchar':
69
                $length = $tableColumn['length'];
70
                $fixed  = false;
71
                break;
72
73
            case 'character':
74
                $length = $tableColumn['length'];
75
                $fixed  = true;
76
                break;
77
78
            case 'clob':
79
                $length = $tableColumn['length'];
80
                break;
81
82
            case 'decimal':
83
            case 'double':
84
            case 'real':
85
                $scale     = $tableColumn['scale'];
86
                $precision = $tableColumn['length'];
87
                break;
88
        }
89
90
        $options = [
91
            'length'        => $length,
92
            'unsigned'      => false,
93
            'fixed'         => (bool) $fixed,
94
            'default'       => $default,
95
            'autoincrement' => (bool) $tableColumn['autoincrement'],
96
            'notnull'       => (bool) ($tableColumn['nulls'] === 'N'),
97
            'scale'         => null,
98
            'precision'     => null,
99
            'comment'       => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
100
                ? $tableColumn['comment']
101
                : null,
102
            'platformOptions' => [],
103
        ];
104
105
        if ($scale !== null && $precision !== null) {
106
            $options['scale']     = $scale;
107
            $options['precision'] = $precision;
108
        }
109
110
        return new Column($tableColumn['colname'], Type::getType($type), $options);
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 130
    protected function _getPortableTablesList($tables)
117
    {
118 130
        $tableNames = [];
119 130
        foreach ($tables as $tableRow) {
120 130
            $tableRow     = array_change_key_case($tableRow, CASE_LOWER);
121 130
            $tableNames[] = $tableRow['name'];
122
        }
123
124 130
        return $tableNames;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    protected function _getPortableTableIndexesList($tableIndexRows, $tableName = null)
131
    {
132
        foreach ($tableIndexRows as &$tableIndexRow) {
133
            $tableIndexRow            = array_change_key_case($tableIndexRow, CASE_LOWER);
134
            $tableIndexRow['primary'] = (bool) $tableIndexRow['primary'];
135
        }
136
137
        return parent::_getPortableTableIndexesList($tableIndexRows, $tableName);
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
144
    {
145
        return new ForeignKeyConstraint(
146
            $tableForeignKey['local_columns'],
147
            $tableForeignKey['foreign_table'],
148
            $tableForeignKey['foreign_columns'],
149
            $tableForeignKey['name'],
150
            $tableForeignKey['options']
151
        );
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    protected function _getPortableTableForeignKeysList($tableForeignKeys)
158
    {
159
        $foreignKeys = [];
160
161
        foreach ($tableForeignKeys as $tableForeignKey) {
162
            $tableForeignKey = array_change_key_case($tableForeignKey, CASE_LOWER);
163
164
            if (! isset($foreignKeys[$tableForeignKey['index_name']])) {
165
                $foreignKeys[$tableForeignKey['index_name']] = [
166
                    'local_columns'   => [$tableForeignKey['local_column']],
167
                    'foreign_table'   => $tableForeignKey['foreign_table'],
168
                    'foreign_columns' => [$tableForeignKey['foreign_column']],
169
                    'name'            => $tableForeignKey['index_name'],
170
                    'options'         => [
171
                        'onUpdate' => $tableForeignKey['on_update'],
172
                        'onDelete' => $tableForeignKey['on_delete'],
173
                    ],
174
                ];
175
            } else {
176
                $foreignKeys[$tableForeignKey['index_name']]['local_columns'][]   = $tableForeignKey['local_column'];
177
                $foreignKeys[$tableForeignKey['index_name']]['foreign_columns'][] = $tableForeignKey['foreign_column'];
178
            }
179
        }
180
181
        return parent::_getPortableTableForeignKeysList($foreignKeys);
182
    }
183
184
    /**
185
     * @param string $def
186
     *
187
     * @return string|null
188
     */
189
    protected function _getPortableForeignKeyRuleDef($def)
190
    {
191
        if ($def === 'C') {
192
            return 'CASCADE';
193
        }
194
195
        if ($def === 'N') {
196
            return 'SET NULL';
197
        }
198
199
        return null;
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205
    protected function _getPortableViewDefinition($view)
206
    {
207
        $view = array_change_key_case($view, CASE_LOWER);
208
        // sadly this still segfaults on PDO_IBM, see http://pecl.php.net/bugs/bug.php?id=17199
209
        //$view['text'] = (is_resource($view['text']) ? stream_get_contents($view['text']) : $view['text']);
210
        if (! is_resource($view['text'])) {
211
            $pos = strpos($view['text'], ' AS ');
212
            $sql = substr($view['text'], $pos+4);
213
        } else {
214
            $sql = '';
215
        }
216
217
        return new View($view['name'], $sql);
218
    }
219
220
    /**
221
     * {@inheritdoc}
222
     */
223
    public function listTableDetails($tableName) : Table
224
    {
225
        $table = parent::listTableDetails($tableName);
226
227
        $platform = $this->_platform;
228
        assert($platform instanceof DB2Platform);
229
        $sql = $platform->getListTableCommentsSQL($tableName);
230
231
        $tableOptions = $this->_conn->fetchAssoc($sql);
232
233
        if ($tableOptions !== false) {
234
            $table->addOption('comment', $tableOptions['REMARKS']);
235
        }
236
237
        return $table;
238
    }
239
}
240