Failed Conditions
Push — type-registry ( 0931f1...33c798 )
by Michael
23:21
created

DB2SchemaManager   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Test Coverage

Coverage 11.83%

Importance

Changes 0
Metric Value
wmc 28
eloc 94
dl 0
loc 189
ccs 11
cts 93
cp 0.1183
rs 10
c 0
b 0
f 0

8 Methods

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