Failed Conditions
Push — develop ( b4c2cf...86369f )
by Sergei
18s queued 13s
created

DB2SchemaManager   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Test Coverage

Coverage 12.05%

Importance

Changes 0
Metric Value
wmc 25
eloc 83
dl 0
loc 166
ccs 10
cts 83
cp 0.1205
rs 10
c 0
b 0
f 0

7 Methods

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