Failed Conditions
Pull Request — develop (#3348)
by Sergei
60:44
created

DB2SchemaManager   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Test Coverage

Coverage 14.46%

Importance

Changes 0
Metric Value
wmc 26
eloc 86
dl 0
loc 172
ccs 12
cts 83
cp 0.1446
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A listTableNames() 0 10 1
A _getPortableTableForeignKeyDefinition() 0 8 1
F _getPortableTableColumnDefinition() 0 60 15
A _getPortableTablesList() 0 9 2
A _getPortableTableForeignKeysList() 0 25 3
A _getPortableViewDefinition() 0 13 2
A _getPortableTableIndexesList() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Schema;
6
7
use Doctrine\DBAL\Types\Type;
8
use const CASE_LOWER;
9
use function array_change_key_case;
10
use function assert;
11
use function is_resource;
12
use function is_string;
13
use function preg_match;
14
use function str_replace;
15
use function strpos;
16
use function strtolower;
17
use function substr;
18
19
/**
20
 * IBM Db2 Schema Manager.
21
 */
22
class DB2SchemaManager extends AbstractSchemaManager
23
{
24
    /**
25
     * {@inheritdoc}
26
     *
27
     * Apparently creator is the schema not the user who created it:
28
     * {@link http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.sqlref/db2z_sysibmsystablestable.htm}
29 135
     */
30
    public function listTableNames() : array
31 135
    {
32 135
        $username = $this->_conn->getUsername();
33
        assert(is_string($username));
34 135
35
        $sql = $this->_platform->getListTablesSQL() . ' AND CREATOR = UPPER(?)';
36 135
37
        $tables = $this->_conn->fetchAll($sql, [$username]);
38 135
39
        return $this->filterAssetNames($this->_getPortableTablesList($tables));
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    protected function _getPortableTableColumnDefinition(array $tableColumn) : Column
46
    {
47
        $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
48
49
        $length = $precision = $default = null;
50
        $scale  = 0;
51
        $fixed  = false;
52
53
        if ($tableColumn['default'] !== null && $tableColumn['default'] !== 'NULL') {
54
            $default = $tableColumn['default'];
55
56
            if (preg_match('/^\'(.*)\'$/s', $default, $matches)) {
57
                $default = str_replace("''", "'", $matches[1]);
58
            }
59
        }
60
61
        $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'])
62
            ?? $this->_platform->getDoctrineTypeMapping($tableColumn['typename']);
63
64
        switch (strtolower($tableColumn['typename'])) {
65
            case 'varchar':
66
                $length = $tableColumn['length'];
67
                break;
68
            case 'character':
69
                $length = $tableColumn['length'];
70
                $fixed  = true;
71
                break;
72
            case 'clob':
73
                $length = $tableColumn['length'];
74
                break;
75
            case 'decimal':
76
            case 'double':
77
            case 'real':
78
                $scale     = $tableColumn['scale'];
79
                $precision = $tableColumn['length'];
80
                break;
81
        }
82
83
        $options = [
84
            'length'        => $length,
85
            'unsigned'      => false,
86
            'default'       => $default,
87
            'autoincrement' => (bool) $tableColumn['autoincrement'],
88
            'notnull'       => $tableColumn['nulls'] === 'N',
89
            'comment'       => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
90
                ? $tableColumn['comment']
91
                : null,
92
            'platformOptions' => [],
93
        ];
94
95
        if ($fixed !== null) {
0 ignored issues
show
introduced by
The condition $fixed !== null is always true.
Loading history...
96
            $options['fixed'] = $fixed;
97
        }
98
99
        if ($scale !== null && $precision !== null) {
100
            $options['scale']     = $scale;
101
            $options['precision'] = $precision;
102 135
        }
103
104 135
        return new Column($tableColumn['colname'], Type::getType($type), $options);
105 135
    }
106 135
107 135
    /**
108
     * {@inheritdoc}
109
     */
110 135
    protected function _getPortableTablesList(array $tables) : array
111
    {
112
        $tableNames = [];
113
        foreach ($tables as $tableRow) {
114
            $tableRow     = array_change_key_case($tableRow, CASE_LOWER);
115
            $tableNames[] = $tableRow['name'];
116
        }
117
118
        return $tableNames;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    protected function _getPortableTableIndexesList(array $tableIndexRows, string $tableName) : array
125
    {
126
        foreach ($tableIndexRows as &$tableIndexRow) {
127
            $tableIndexRow            = array_change_key_case($tableIndexRow, CASE_LOWER);
128
            $tableIndexRow['primary'] = (bool) $tableIndexRow['primary'];
129
        }
130
131
        return parent::_getPortableTableIndexesList($tableIndexRows, $tableName);
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    protected function _getPortableTableForeignKeyDefinition($tableForeignKey) : ForeignKeyConstraint
138
    {
139
        return new ForeignKeyConstraint(
140
            $tableForeignKey['local_columns'],
141
            $tableForeignKey['foreign_table'],
142
            $tableForeignKey['foreign_columns'],
143
            $tableForeignKey['name'],
144
            $tableForeignKey['options']
145
        );
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    protected function _getPortableTableForeignKeysList(array $tableForeignKeys) : array
152
    {
153
        $foreignKeys = [];
154
155
        foreach ($tableForeignKeys as $tableForeignKey) {
156
            $tableForeignKey = array_change_key_case($tableForeignKey, CASE_LOWER);
157
158
            if (! isset($foreignKeys[$tableForeignKey['index_name']])) {
159
                $foreignKeys[$tableForeignKey['index_name']] = [
160
                    'local_columns'   => [$tableForeignKey['local_column']],
161
                    'foreign_table'   => $tableForeignKey['foreign_table'],
162
                    'foreign_columns' => [$tableForeignKey['foreign_column']],
163
                    'name'            => $tableForeignKey['index_name'],
164
                    'options'         => [
165
                        'onUpdate' => $tableForeignKey['on_update'],
166
                        'onDelete' => $tableForeignKey['on_delete'],
167
                    ],
168
                ];
169
            } else {
170
                $foreignKeys[$tableForeignKey['index_name']]['local_columns'][]   = $tableForeignKey['local_column'];
171
                $foreignKeys[$tableForeignKey['index_name']]['foreign_columns'][] = $tableForeignKey['foreign_column'];
172
            }
173
        }
174
175
        return parent::_getPortableTableForeignKeysList($foreignKeys);
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181
    protected function _getPortableViewDefinition(array $view) : 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