DB2SchemaManager.php 7.04 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Schema;

5
use Doctrine\DBAL\DBALException;
6
use Doctrine\DBAL\Platforms\DB2Platform;
7
use Doctrine\DBAL\Types\Type;
8

9
use function array_change_key_case;
Grégoire Paris's avatar
Grégoire Paris committed
10
use function assert;
11 12
use function preg_match;
use function str_replace;
13 14 15
use function strpos;
use function strtolower;
use function substr;
16

Grégoire Paris's avatar
Grégoire Paris committed
17
use const CASE_LOWER;
18

19
/**
Benjamin Morel's avatar
Benjamin Morel committed
20
 * IBM Db2 Schema Manager.
21
 */
Benjamin Eberlei's avatar
Benjamin Eberlei committed
22
class DB2SchemaManager extends AbstractSchemaManager
23 24
{
    /**
Benjamin Morel's avatar
Benjamin Morel committed
25
     * {@inheritdoc}
26 27 28 29 30 31
     *
     * Apparently creator is the schema not the user who created it:
     * {@link http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.sqlref/db2z_sysibmsystablestable.htm}
     */
    public function listTableNames()
    {
32
        $sql = $this->_platform->getListTablesSQL() . ' AND CREATOR = CURRENT_USER';
33

34
        $tables = $this->_conn->fetchAllAssociative($sql);
35

36
        return $this->filterAssetNames($this->_getPortableTablesList($tables));
37 38 39
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
40
     * {@inheritdoc}
41 42
     *
     * @throws DBALException
43 44 45
     */
    protected function _getPortableTableColumnDefinition($tableColumn)
    {
46
        $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
47

48 49 50
        $length    = null;
        $fixed     = null;
        $scale     = false;
51
        $precision = false;
52

53 54
        $default = null;

55
        if ($tableColumn['default'] !== null && $tableColumn['default'] !== 'NULL') {
56 57
            $default = $tableColumn['default'];

58
            if (preg_match('/^\'(.*)\'$/s', $default, $matches) === 1) {
59 60
                $default = str_replace("''", "'", $matches[1]);
            }
61 62
        }

63
        $type = $this->_platform->getDoctrineTypeMapping($tableColumn['typename']);
64

65
        if (isset($tableColumn['comment'])) {
66
            $type                   = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
67 68 69
            $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
        }

70 71 72
        switch (strtolower($tableColumn['typename'])) {
            case 'varchar':
                $length = $tableColumn['length'];
73
                $fixed  = false;
74
                break;
Sergei Morozov's avatar
Sergei Morozov committed
75

76 77
            case 'character':
                $length = $tableColumn['length'];
78
                $fixed  = true;
79
                break;
Sergei Morozov's avatar
Sergei Morozov committed
80

81 82 83
            case 'clob':
                $length = $tableColumn['length'];
                break;
Sergei Morozov's avatar
Sergei Morozov committed
84

85 86 87
            case 'decimal':
            case 'double':
            case 'real':
88
                $scale     = $tableColumn['scale'];
89 90 91 92
                $precision = $tableColumn['length'];
                break;
        }

93
        $options = [
94
            'length'        => $length,
Sergei Morozov's avatar
Sergei Morozov committed
95
            'unsigned'      => false,
96
            'fixed'         => (bool) $fixed,
97
            'default'       => $default,
98 99
            'autoincrement' => (bool) $tableColumn['autoincrement'],
            'notnull'       => (bool) ($tableColumn['nulls'] === 'N'),
100 101
            'scale'         => null,
            'precision'     => null,
102 103 104
            'comment'       => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
                ? $tableColumn['comment']
                : null,
105 106
            'platformOptions' => [],
        ];
107 108

        if ($scale !== null && $precision !== null) {
109
            $options['scale']     = $scale;
110 111 112
            $options['precision'] = $precision;
        }

113
        return new Column($tableColumn['colname'], Type::getType($type), $options);
114 115
    }

Benjamin Morel's avatar
Benjamin Morel committed
116 117 118
    /**
     * {@inheritdoc}
     */
119 120
    protected function _getPortableTablesList($tables)
    {
121
        $tableNames = [];
122
        foreach ($tables as $tableRow) {
123
            $tableRow     = array_change_key_case($tableRow, CASE_LOWER);
124 125
            $tableNames[] = $tableRow['name'];
        }
Benjamin Morel's avatar
Benjamin Morel committed
126

127 128 129
        return $tableNames;
    }

Benjamin Morel's avatar
Benjamin Morel committed
130 131 132
    /**
     * {@inheritdoc}
     */
133
    protected function _getPortableTableIndexesList($tableIndexRows, $tableName = null)
134
    {
135
        foreach ($tableIndexRows as &$tableIndexRow) {
136 137
            $tableIndexRow            = array_change_key_case($tableIndexRow, CASE_LOWER);
            $tableIndexRow['primary'] = (bool) $tableIndexRow['primary'];
138 139
        }

140
        return parent::_getPortableTableIndexesList($tableIndexRows, $tableName);
141 142
    }

Benjamin Morel's avatar
Benjamin Morel committed
143 144 145
    /**
     * {@inheritdoc}
     */
146 147 148
    protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
    {
        return new ForeignKeyConstraint(
149 150 151 152 153
            $tableForeignKey['local_columns'],
            $tableForeignKey['foreign_table'],
            $tableForeignKey['foreign_columns'],
            $tableForeignKey['name'],
            $tableForeignKey['options']
154 155 156
        );
    }

157 158 159 160 161
    /**
     * {@inheritdoc}
     */
    protected function _getPortableTableForeignKeysList($tableForeignKeys)
    {
162
        $foreignKeys = [];
163 164

        foreach ($tableForeignKeys as $tableForeignKey) {
165
            $tableForeignKey = array_change_key_case($tableForeignKey, CASE_LOWER);
166

167
            if (! isset($foreignKeys[$tableForeignKey['index_name']])) {
168 169
                $foreignKeys[$tableForeignKey['index_name']] = [
                    'local_columns'   => [$tableForeignKey['local_column']],
170
                    'foreign_table'   => $tableForeignKey['foreign_table'],
171
                    'foreign_columns' => [$tableForeignKey['foreign_column']],
172
                    'name'            => $tableForeignKey['index_name'],
173
                    'options'         => [
174 175
                        'onUpdate' => $tableForeignKey['on_update'],
                        'onDelete' => $tableForeignKey['on_delete'],
176
                    ],
177
                ];
178
            } else {
179
                $foreignKeys[$tableForeignKey['index_name']]['local_columns'][]   = $tableForeignKey['local_column'];
180 181 182 183 184 185 186
                $foreignKeys[$tableForeignKey['index_name']]['foreign_columns'][] = $tableForeignKey['foreign_column'];
            }
        }

        return parent::_getPortableTableForeignKeysList($foreignKeys);
    }

Benjamin Morel's avatar
Benjamin Morel committed
187
    /**
188
     * @param string $def
189 190
     *
     * @return string|null
Benjamin Morel's avatar
Benjamin Morel committed
191
     */
192 193
    protected function _getPortableForeignKeyRuleDef($def)
    {
194 195
        if ($def === 'C') {
            return 'CASCADE';
196 197 198
        }

        if ($def === 'N') {
199
            return 'SET NULL';
200
        }
Benjamin Morel's avatar
Benjamin Morel committed
201

202 203 204
        return null;
    }

Benjamin Morel's avatar
Benjamin Morel committed
205 206 207
    /**
     * {@inheritdoc}
     */
208 209
    protected function _getPortableViewDefinition($view)
    {
210
        $view = array_change_key_case($view, CASE_LOWER);
211 212 213 214 215

        $position = strpos($view['text'], ' AS ');

        if ($position !== false) {
            $sql = substr($view['text'], $position + 4);
216 217 218
        } else {
            $sql = '';
        }
219 220 221

        return new View($view['name'], $sql);
    }
222

Grégoire Paris's avatar
Grégoire Paris committed
223 224 225
    /**
     * {@inheritdoc}
     */
226
    public function listTableDetails($name): Table
227
    {
228
        $table = parent::listTableDetails($name);
229 230

        $platform = $this->_platform;
Grégoire Paris's avatar
Grégoire Paris committed
231
        assert($platform instanceof DB2Platform);
232
        $sql = $platform->getListTableCommentsSQL($name);
233

234
        $tableOptions = $this->_conn->fetchAssociative($sql);
235 236 237 238

        if ($tableOptions !== false) {
            $table->addOption('comment', $tableOptions['REMARKS']);
        }
239 240 241

        return $table;
    }
242
}