Failed Conditions
Push — phpstan ( 752f9f...1acc4a )
by Michael
21:57
created

DB2SchemaManager::_getPortableTableIndexesList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 0
cts 5
cp 0
cc 2
nc 2
nop 2
crap 6
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 17
    public function listTableNames()
45
    {
46 17
        $sql = $this->_platform->getListTablesSQL();
47 17
        $sql .= " AND CREATOR = UPPER('".$this->_conn->getUsername()."')";
48
49 17
        $tables = $this->_conn->fetchAll($sql);
50
51 17
        return $this->filterAssetNames($this->_getPortableTablesList($tables));
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected function _getPortableTableColumnDefinition($tableColumn)
58
    {
59
        $tableColumn = array_change_key_case($tableColumn, \CASE_LOWER);
60
61
        $length = null;
62
        $fixed = null;
63
        $unsigned = false;
64
        $scale = false;
65
        $precision = false;
66
67
        $default = null;
68
69
        if (null !== $tableColumn['default'] && 'NULL' != $tableColumn['default']) {
70
            $default = trim($tableColumn['default'], "'");
71
        }
72
73
        $type = $this->_platform->getDoctrineTypeMapping($tableColumn['typename']);
74
75
        if (isset($tableColumn['comment'])) {
76
            $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
77
            $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
78
        }
79
80
        switch (strtolower($tableColumn['typename'])) {
81
            case 'varchar':
82
                $length = $tableColumn['length'];
83
                $fixed = false;
84
                break;
85
            case 'character':
86
                $length = $tableColumn['length'];
87
                $fixed = true;
88
                break;
89
            case 'clob':
90
                $length = $tableColumn['length'];
91
                break;
92
            case 'decimal':
93
            case 'double':
94
            case 'real':
95
                $scale = $tableColumn['scale'];
96
                $precision = $tableColumn['length'];
97
                break;
98
        }
99
100
        $options = [
101
            'length'        => $length,
102
            'unsigned'      => (bool) $unsigned,
103
            'fixed'         => (bool) $fixed,
104
            'default'       => $default,
105
            'autoincrement' => (boolean) $tableColumn['autoincrement'],
106
            'notnull'       => (bool) ($tableColumn['nulls'] == 'N'),
107
            'scale'         => null,
108
            'precision'     => null,
109
            'comment'       => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
110
                ? $tableColumn['comment']
111
                : null,
112
            'platformOptions' => [],
113
        ];
114
115
        if ($scale !== null && $precision !== null) {
116
            $options['scale'] = $scale;
117
            $options['precision'] = $precision;
118
        }
119
120
        return new Column($tableColumn['colname'], \Doctrine\DBAL\Types\Type::getType($type), $options);
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 17
    protected function _getPortableTablesList($tables)
127
    {
128 17
        $tableNames = [];
129 17
        foreach ($tables as $tableRow) {
130 17
            $tableRow = array_change_key_case($tableRow, \CASE_LOWER);
131 17
            $tableNames[] = $tableRow['name'];
132
        }
133
134 17
        return $tableNames;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    protected function _getPortableTableIndexesList($tableIndexRows, $tableName = null)
141
    {
142
        foreach ($tableIndexRows as &$tableIndexRow) {
143
            $tableIndexRow = array_change_key_case($tableIndexRow, \CASE_LOWER);
144
            $tableIndexRow['primary'] = (boolean) $tableIndexRow['primary'];
145
        }
146
147
        return parent::_getPortableTableIndexesList($tableIndexRows, $tableName);
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
154
    {
155
        return new ForeignKeyConstraint(
156
            $tableForeignKey['local_columns'],
157
            $tableForeignKey['foreign_table'],
158
            $tableForeignKey['foreign_columns'],
159
            $tableForeignKey['name'],
160
            $tableForeignKey['options']
161
        );
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    protected function _getPortableTableForeignKeysList($tableForeignKeys)
168
    {
169
        $foreignKeys = [];
170
171
        foreach ($tableForeignKeys as $tableForeignKey) {
172
            $tableForeignKey = array_change_key_case($tableForeignKey, \CASE_LOWER);
173
174
            if (!isset($foreignKeys[$tableForeignKey['index_name']])) {
175
                $foreignKeys[$tableForeignKey['index_name']] = [
176
                    'local_columns'   => [$tableForeignKey['local_column']],
177
                    'foreign_table'   => $tableForeignKey['foreign_table'],
178
                    'foreign_columns' => [$tableForeignKey['foreign_column']],
179
                    'name'            => $tableForeignKey['index_name'],
180
                    'options'         => [
181
                        'onUpdate' => $tableForeignKey['on_update'],
182
                        'onDelete' => $tableForeignKey['on_delete'],
183
                    ]
184
                ];
185
            } else {
186
                $foreignKeys[$tableForeignKey['index_name']]['local_columns'][] = $tableForeignKey['local_column'];
187
                $foreignKeys[$tableForeignKey['index_name']]['foreign_columns'][] = $tableForeignKey['foreign_column'];
188
            }
189
        }
190
191
        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
    protected function _getPortableViewDefinition($view)
212
    {
213
        $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']);
216
        if (!is_resource($view['text'])) {
217
            $pos = strpos($view['text'], ' AS ');
218
            $sql = substr($view['text'], $pos+4);
219
        } else {
220
            $sql = '';
221
        }
222
223
        return new View($view['name'], $sql);
224
    }
225
}
226