Passed
Push — type-registry ( f9a1df...0931f1 )
by Michael
24:09
created

DB2SchemaManager::_getPortableTablesList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 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 175
    public function listTableNames()
26
    {
27 175
        $sql  = $this->_platform->getListTablesSQL();
28 175
        $sql .= ' AND CREATOR = UPPER(' . $this->_conn->quote($this->_conn->getUsername()) . ')';
29
30 175
        $tables = $this->_conn->fetchAll($sql);
31
32 175
        return $this->filterAssetNames($this->_getPortableTablesList($tables));
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 48
    protected function _getPortableTableColumnDefinition($tableColumn)
39
    {
40 48
        $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
41
42 48
        $length    = null;
43 48
        $fixed     = null;
44 48
        $scale     = false;
45 48
        $precision = false;
46
47 48
        $default = null;
48
49 48
        if ($tableColumn['default'] !== null && $tableColumn['default'] !== 'NULL') {
50 48
            $default = trim($tableColumn['default'], "'");
51
        }
52
53 48
        $type = $this->_platform->getDoctrineTypeMapping($tableColumn['typename']);
54
55 48
        if (isset($tableColumn['comment'])) {
56 47
            $type                   = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
57 47
            $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
58
        }
59
60 48
        switch (strtolower($tableColumn['typename'])) {
61
            case 'varchar':
62 45
                $length = $tableColumn['length'];
63 45
                $fixed  = false;
64 45
                break;
65
            case 'character':
66 45
                $length = $tableColumn['length'];
67 45
                $fixed  = true;
68 45
                break;
69
            case 'clob':
70 45
                $length = $tableColumn['length'];
71 45
                break;
72
            case 'decimal':
73
            case 'double':
74
            case 'real':
75 44
                $scale     = $tableColumn['scale'];
76 44
                $precision = $tableColumn['length'];
77 48
                break;
78
        }
79
80
        $options = [
81 48
            'length'        => $length,
82
            'unsigned'      => false,
83 48
            'fixed'         => (bool) $fixed,
84 48
            'default'       => $default,
85 48
            'autoincrement' => (bool) $tableColumn['autoincrement'],
86 48
            'notnull'       => (bool) ($tableColumn['nulls'] === 'N'),
87
            'scale'         => null,
88
            'precision'     => null,
89 48
            'comment'       => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
90 46
                ? $tableColumn['comment']
91
                : null,
92
            'platformOptions' => [],
93
        ];
94
95 48
        if ($scale !== null && $precision !== null) {
96 48
            $options['scale']     = $scale;
97 48
            $options['precision'] = $precision;
98
        }
99
100 48
        return new Column($tableColumn['colname'], Type::getType($type), $options);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 175
    protected function _getPortableTablesList($tables)
107
    {
108 175
        $tableNames = [];
109 175
        foreach ($tables as $tableRow) {
110 175
            $tableRow     = array_change_key_case($tableRow, CASE_LOWER);
111 175
            $tableNames[] = $tableRow['name'];
112
        }
113
114 175
        return $tableNames;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120 48
    protected function _getPortableTableIndexesList($tableIndexRows, $tableName = null)
121
    {
122 48
        foreach ($tableIndexRows as &$tableIndexRow) {
123 45
            $tableIndexRow            = array_change_key_case($tableIndexRow, CASE_LOWER);
124 45
            $tableIndexRow['primary'] = (bool) $tableIndexRow['primary'];
125
        }
126
127 48
        return parent::_getPortableTableIndexesList($tableIndexRows, $tableName);
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 37
    protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
134
    {
135 37
        return new ForeignKeyConstraint(
136 37
            $tableForeignKey['local_columns'],
137 37
            $tableForeignKey['foreign_table'],
138 37
            $tableForeignKey['foreign_columns'],
139 37
            $tableForeignKey['name'],
140 37
            $tableForeignKey['options']
141
        );
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 48
    protected function _getPortableTableForeignKeysList($tableForeignKeys)
148
    {
149 48
        $foreignKeys = [];
150
151 48
        foreach ($tableForeignKeys as $tableForeignKey) {
152 37
            $tableForeignKey = array_change_key_case($tableForeignKey, CASE_LOWER);
153
154 37
            if (! isset($foreignKeys[$tableForeignKey['index_name']])) {
155 37
                $foreignKeys[$tableForeignKey['index_name']] = [
156 37
                    'local_columns'   => [$tableForeignKey['local_column']],
157 37
                    'foreign_table'   => $tableForeignKey['foreign_table'],
158 37
                    'foreign_columns' => [$tableForeignKey['foreign_column']],
159 37
                    'name'            => $tableForeignKey['index_name'],
160
                    'options'         => [
161 37
                        'onUpdate' => $tableForeignKey['on_update'],
162 37
                        'onDelete' => $tableForeignKey['on_delete'],
163
                    ],
164
                ];
165
            } else {
166 23
                $foreignKeys[$tableForeignKey['index_name']]['local_columns'][]   = $tableForeignKey['local_column'];
167 23
                $foreignKeys[$tableForeignKey['index_name']]['foreign_columns'][] = $tableForeignKey['foreign_column'];
168
            }
169
        }
170
171 48
        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 33
    protected function _getPortableViewDefinition($view)
194
    {
195 33
        $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 33
        if (! is_resource($view['text'])) {
199 33
            $pos = strpos($view['text'], ' AS ');
200 33
            $sql = substr($view['text'], $pos+4);
201
        } else {
202
            $sql = '';
203
        }
204
205 33
        return new View($view['name'], $sql);
206
    }
207
}
208