Failed Conditions
Push — 3.0.x ( 655b6b...430dce )
by Grégoire
16:57 queued 12:22
created

DB2SchemaManager   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Test Coverage

Coverage 94.23%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 104
dl 0
loc 218
ccs 98
cts 104
cp 0.9423
rs 9.92
c 1
b 0
f 0
wmc 31

9 Methods

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