Passed
Pull Request — master (#3133)
by Michael
17:35
created

DB2SchemaManager   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Test Coverage

Coverage 92.63%

Importance

Changes 0
Metric Value
wmc 28
dl 0
loc 188
ccs 88
cts 95
cp 0.9263
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A listTableNames() 0 8 1
A _getPortableTablesList() 0 9 2
A _getPortableForeignKeyRuleDef() 0 9 3
A _getPortableTableForeignKeyDefinition() 0 8 1
D _getPortableTableColumnDefinition() 0 64 14
B _getPortableTableForeignKeysList() 0 25 3
A _getPortableViewDefinition() 0 13 2
A _getPortableTableIndexesList() 0 8 2
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Schema;
21
22
use function array_change_key_case;
23
use function is_resource;
24
use function strpos;
25
use function strtolower;
26
use function substr;
27
use function trim;
28
29
/**
30
 * IBM Db2 Schema Manager.
31
 *
32
 * @link   www.doctrine-project.org
33
 * @since  1.0
34
 * @author Benjamin Eberlei <[email protected]>
35
 */
36
class DB2SchemaManager extends AbstractSchemaManager
37
{
38
    /**
39
     * {@inheritdoc}
40
     *
41
     * Apparently creator is the schema not the user who created it:
42
     * {@link http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.sqlref/db2z_sysibmsystablestable.htm}
43
     */
44 71
    public function listTableNames()
45
    {
46 71
        $sql = $this->_platform->getListTablesSQL();
47 71
        $sql .= " AND CREATOR = UPPER('".$this->_conn->getUsername()."')";
48
49 71
        $tables = $this->_conn->fetchAll($sql);
50
51 71
        return $this->filterAssetNames($this->_getPortableTablesList($tables));
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 31
    protected function _getPortableTableColumnDefinition($tableColumn)
58
    {
59 31
        $tableColumn = array_change_key_case($tableColumn, \CASE_LOWER);
60
61 31
        $length = null;
62 31
        $fixed = null;
63 31
        $unsigned = false;
64 31
        $scale = false;
65 31
        $precision = false;
66
67 31
        $default = null;
68
69 31
        if (null !== $tableColumn['default'] && 'NULL' != $tableColumn['default']) {
70 7
            $default = trim($tableColumn['default'], "'");
71
        }
72
73 31
        $type = $this->_platform->getDoctrineTypeMapping($tableColumn['typename']);
74
75 31
        if (isset($tableColumn['comment'])) {
76 19
            $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
77 19
            $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
78
        }
79
80 31
        switch (strtolower($tableColumn['typename'])) {
81 31
            case 'varchar':
82 9
                $length = $tableColumn['length'];
83 9
                $fixed = false;
84 9
                break;
85 31
            case 'character':
86 4
                $length = $tableColumn['length'];
87 4
                $fixed = true;
88 4
                break;
89 30
            case 'clob':
90 8
                $length = $tableColumn['length'];
91 8
                break;
92 28
            case 'decimal':
93 28
            case 'double':
94 28
            case 'real':
95 4
                $scale = $tableColumn['scale'];
96 4
                $precision = $tableColumn['length'];
97 4
                break;
98
        }
99
100
        $options = [
101 31
            'length'        => $length,
102 31
            'unsigned'      => (bool) $unsigned,
103 31
            'fixed'         => (bool) $fixed,
104 31
            'default'       => $default,
105 31
            'autoincrement' => (boolean) $tableColumn['autoincrement'],
106 31
            'notnull'       => (bool) ($tableColumn['nulls'] == 'N'),
107
            'scale'         => null,
108
            'precision'     => null,
109 31
            'comment'       => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
110 17
                ? $tableColumn['comment']
111
                : null,
112
            'platformOptions' => [],
113
        ];
114
115 31
        if ($scale !== null && $precision !== null) {
116 31
            $options['scale'] = $scale;
117 31
            $options['precision'] = $precision;
118
        }
119
120 31
        return new Column($tableColumn['colname'], \Doctrine\DBAL\Types\Type::getType($type), $options);
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 71
    protected function _getPortableTablesList($tables)
127
    {
128 71
        $tableNames = [];
129 71
        foreach ($tables as $tableRow) {
130 71
            $tableRow = array_change_key_case($tableRow, \CASE_LOWER);
131 71
            $tableNames[] = $tableRow['name'];
132
        }
133
134 71
        return $tableNames;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 25
    protected function _getPortableTableIndexesList($tableIndexRows, $tableName = null)
141
    {
142 25
        foreach ($tableIndexRows as &$tableIndexRow) {
143 14
            $tableIndexRow = array_change_key_case($tableIndexRow, \CASE_LOWER);
144 14
            $tableIndexRow['primary'] = (boolean) $tableIndexRow['primary'];
145
        }
146
147 25
        return parent::_getPortableTableIndexesList($tableIndexRows, $tableName);
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153 7
    protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
154
    {
155 7
        return new ForeignKeyConstraint(
156 7
            $tableForeignKey['local_columns'],
157 7
            $tableForeignKey['foreign_table'],
158 7
            $tableForeignKey['foreign_columns'],
159 7
            $tableForeignKey['name'],
160 7
            $tableForeignKey['options']
161
        );
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167 23
    protected function _getPortableTableForeignKeysList($tableForeignKeys)
168
    {
169 23
        $foreignKeys = [];
170
171 23
        foreach ($tableForeignKeys as $tableForeignKey) {
172 7
            $tableForeignKey = array_change_key_case($tableForeignKey, \CASE_LOWER);
173
174 7
            if (!isset($foreignKeys[$tableForeignKey['index_name']])) {
175 7
                $foreignKeys[$tableForeignKey['index_name']] = [
176 7
                    'local_columns'   => [$tableForeignKey['local_column']],
177 7
                    'foreign_table'   => $tableForeignKey['foreign_table'],
178 7
                    'foreign_columns' => [$tableForeignKey['foreign_column']],
179 7
                    'name'            => $tableForeignKey['index_name'],
180
                    'options'         => [
181 7
                        'onUpdate' => $tableForeignKey['on_update'],
182 7
                        'onDelete' => $tableForeignKey['on_delete'],
183
                    ]
184
                ];
185
            } else {
186 1
                $foreignKeys[$tableForeignKey['index_name']]['local_columns'][] = $tableForeignKey['local_column'];
187 7
                $foreignKeys[$tableForeignKey['index_name']]['foreign_columns'][] = $tableForeignKey['foreign_column'];
188
            }
189
        }
190
191 23
        return parent::_getPortableTableForeignKeysList($foreignKeys);
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197
    protected function _getPortableForeignKeyRuleDef($def)
198
    {
199
        if ($def == "C") {
200
            return "CASCADE";
201
        } elseif ($def == "N") {
202
            return "SET NULL";
203
        }
204
205
        return null;
206
    }
207
208
    /**
209
     * {@inheritdoc}
210
     */
211 1
    protected function _getPortableViewDefinition($view)
212
    {
213 1
        $view = array_change_key_case($view, \CASE_LOWER);
214
        // sadly this still segfaults on PDO_IBM, see http://pecl.php.net/bugs/bug.php?id=17199
215
        //$view['text'] = (is_resource($view['text']) ? stream_get_contents($view['text']) : $view['text']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
216 1
        if (!is_resource($view['text'])) {
217 1
            $pos = strpos($view['text'], ' AS ');
218 1
            $sql = substr($view['text'], $pos+4);
219
        } else {
220
            $sql = '';
221
        }
222
223 1
        return new View($view['name'], $sql);
224
    }
225
}
226