Failed Conditions
Pull Request — master (#3319)
by Massimiliano
11:33
created

DB2SchemaManager::_getPortableViewDefinition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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