Failed Conditions
Pull Request — 2.10 (#3762)
by Benjamin
09:16
created

DB2SchemaManager   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Test Coverage

Coverage 10.78%

Importance

Changes 0
Metric Value
wmc 30
eloc 102
dl 0
loc 207
ccs 11
cts 102
cp 0.1078
rs 10
c 0
b 0
f 0

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