Failed Conditions
Pull Request — master (#3509)
by Sergei
47:36 queued 44:45
created

DB2SchemaManager::_getPortableForeignKeyRuleDef()   A

Complexity

Conditions 3
Paths 3

Size

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