Completed
Push — develop ( fa42c1...0ef7d4 )
by Sergei
22:52
created

DB2SchemaManager::_getPortableViewDefinition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

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