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