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