Completed
Pull Request — develop (#3565)
by Jonathan
13:02
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
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Schema;
6
7
use Doctrine\DBAL\Types\Type;
8
use const CASE_LOWER;
9
use function array_change_key_case;
10
use function assert;
11
use function is_resource;
12
use function is_string;
13
use function preg_match;
14
use function str_replace;
15
use function strpos;
16
use function strtolower;
17
use function substr;
18
19
/**
20
 * IBM Db2 Schema Manager.
21
 */
22
class DB2SchemaManager extends AbstractSchemaManager
23
{
24
    /**
25
     * {@inheritdoc}
26
     *
27
     * Apparently creator is the schema not the user who created it:
28
     * {@link http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.sqlref/db2z_sysibmsystablestable.htm}
29
     */
30 137
    public function listTableNames() : array
31
    {
32 137
        $username = $this->_conn->getUsername();
33 137
        assert(is_string($username));
34
35 137
        $sql = $this->_platform->getListTablesSQL() . ' AND CREATOR = UPPER(?)';
36
37 137
        $tables = $this->_conn->fetchAll($sql, [$username]);
38
39 137
        return $this->filterAssetNames($this->_getPortableTablesList($tables));
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 98
    protected function _getPortableTableColumnDefinition(array $tableColumn) : Column
46
    {
47 98
        $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
48
49 98
        $length = $precision = $default = null;
50 98
        $scale  = 0;
51 98
        $fixed  = false;
52
53 98
        if ($tableColumn['default'] !== null && $tableColumn['default'] !== 'NULL') {
54 98
            $default = $tableColumn['default'];
55
56 98
            if (preg_match('/^\'(.*)\'$/s', $default, $matches)) {
57 96
                $default = str_replace("''", "'", $matches[1]);
58
            }
59
        }
60
61 98
        $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'])
62 98
            ?? $this->_platform->getDoctrineTypeMapping($tableColumn['typename']);
63
64 98
        switch (strtolower($tableColumn['typename'])) {
65
            case 'varchar':
66 92
                $length = $tableColumn['length'];
67 92
                break;
68
            case 'character':
69 92
                $length = $tableColumn['length'];
70 92
                $fixed  = true;
71 92
                break;
72
            case 'clob':
73 92
                $length = $tableColumn['length'];
74 92
                break;
75
            case 'decimal':
76
            case 'double':
77
            case 'real':
78 90
                $scale     = $tableColumn['scale'];
79 90
                $precision = $tableColumn['length'];
80 98
                break;
81
        }
82
83
        $options = [
84 98
            'length'        => $length,
85
            'unsigned'      => false,
86 98
            'fixed'         => $fixed,
87 98
            'default'       => $default,
88 98
            'autoincrement' => (bool) $tableColumn['autoincrement'],
89 98
            'notnull'       => $tableColumn['nulls'] === 'N',
90 98
            'comment'       => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
91 94
                ? $tableColumn['comment']
92
                : null,
93
            'platformOptions' => [],
94
        ];
95
96 98
        if ($scale !== null && $precision !== null) {
97 90
            $options['scale']     = $scale;
98 90
            $options['precision'] = $precision;
99
        }
100
101 98
        return new Column($tableColumn['colname'], Type::getType($type), $options);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 137
    protected function _getPortableTablesList(array $tables) : array
108
    {
109 137
        $tableNames = [];
110 137
        foreach ($tables as $tableRow) {
111 137
            $tableRow     = array_change_key_case($tableRow, CASE_LOWER);
112 137
            $tableNames[] = $tableRow['name'];
113
        }
114
115 137
        return $tableNames;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 98
    protected function _getPortableTableIndexesList(array $tableIndexRows, string $tableName) : array
122
    {
123 98
        foreach ($tableIndexRows as &$tableIndexRow) {
124 92
            $tableIndexRow            = array_change_key_case($tableIndexRow, CASE_LOWER);
125 92
            $tableIndexRow['primary'] = (bool) $tableIndexRow['primary'];
126
        }
127
128 98
        return parent::_getPortableTableIndexesList($tableIndexRows, $tableName);
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 76
    protected function _getPortableTableForeignKeyDefinition(array $tableForeignKey) : ForeignKeyConstraint
135
    {
136 76
        return new ForeignKeyConstraint(
137 76
            $tableForeignKey['local_columns'],
138 76
            $tableForeignKey['foreign_table'],
139 76
            $tableForeignKey['foreign_columns'],
140 76
            $tableForeignKey['name'],
141 76
            $tableForeignKey['options']
142
        );
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 98
    protected function _getPortableTableForeignKeysList(array $tableForeignKeys) : array
149
    {
150 98
        $foreignKeys = [];
151
152 98
        foreach ($tableForeignKeys as $tableForeignKey) {
153 76
            $tableForeignKey = array_change_key_case($tableForeignKey, CASE_LOWER);
154
155 76
            if (! isset($foreignKeys[$tableForeignKey['index_name']])) {
156 76
                $foreignKeys[$tableForeignKey['index_name']] = [
157 76
                    'local_columns'   => [$tableForeignKey['local_column']],
158 76
                    'foreign_table'   => $tableForeignKey['foreign_table'],
159 76
                    'foreign_columns' => [$tableForeignKey['foreign_column']],
160 76
                    'name'            => $tableForeignKey['index_name'],
161
                    'options'         => [
162 76
                        'onUpdate' => $tableForeignKey['on_update'],
163 76
                        'onDelete' => $tableForeignKey['on_delete'],
164
                    ],
165
                ];
166
            } else {
167 48
                $foreignKeys[$tableForeignKey['index_name']]['local_columns'][]   = $tableForeignKey['local_column'];
168 48
                $foreignKeys[$tableForeignKey['index_name']]['foreign_columns'][] = $tableForeignKey['foreign_column'];
169
            }
170
        }
171
172 98
        return parent::_getPortableTableForeignKeysList($foreignKeys);
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178 68
    protected function _getPortableViewDefinition(array $view) : View
179
    {
180 68
        $view = array_change_key_case($view, CASE_LOWER);
181
        // sadly this still segfaults on PDO_IBM, see http://pecl.php.net/bugs/bug.php?id=17199
182
        //$view['text'] = (is_resource($view['text']) ? stream_get_contents($view['text']) : $view['text']);
183 68
        if (! is_resource($view['text'])) {
184 68
            $pos = strpos($view['text'], ' AS ');
185 68
            $sql = substr($view['text'], $pos+4);
186
        } else {
187
            $sql = '';
188
        }
189
190 68
        return new View($view['name'], $sql);
191
    }
192
}
193