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