Completed
Push — 2.10.x ( 6c789b...72d908 )
by Grégoire
22s queued 15s
created

DB2SchemaManager::_getPortableTablesList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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