Completed
Push — master ( 6d673d...7f4ef4 )
by Sergei
40:47 queued 40:43
created

DB2SchemaManager::_getPortableTableIndexesList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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