Completed
Pull Request — master (#2960)
by Sergei
13:23
created

_getPortableTableForeignKeyDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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