Completed
Pull Request — develop (#3502)
by Sergei
168:11 queued 103:08
created

DB2SchemaManager   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Test Coverage

Coverage 93.1%

Importance

Changes 0
Metric Value
wmc 25
eloc 90
dl 0
loc 175
ccs 81
cts 87
cp 0.931
rs 10
c 0
b 0
f 0

7 Methods

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