Completed
Pull Request — 2.10 (#3812)
by Gabriel
12:32
created

DB2SchemaManager   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Test Coverage

Coverage 93.14%

Importance

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

9 Methods

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