Failed Conditions
Pull Request — develop (#3367)
by Benjamin
10:59
created

DB2SchemaManager::_getPortableForeignKeyRuleDef()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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