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

namespace Doctrine\DBAL\Schema;

5
use Doctrine\DBAL\Platforms\DB2Platform;
6 7
use Doctrine\DBAL\Types\Type;
use const CASE_LOWER;
8 9
use function array_change_key_case;
use function is_resource;
10 11
use function preg_match;
use function str_replace;
12 13 14 15
use function strpos;
use function strtolower;
use function substr;

16
/**
Benjamin Morel's avatar
Benjamin Morel committed
17
 * IBM Db2 Schema Manager.
18
 */
Benjamin Eberlei's avatar
Benjamin Eberlei committed
19
class DB2SchemaManager extends AbstractSchemaManager
20 21
{
    /**
Benjamin Morel's avatar
Benjamin Morel committed
22
     * {@inheritdoc}
23 24 25 26 27 28
     *
     * 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()
    {
29
        $sql  = $this->_platform->getListTablesSQL();
30
        $sql .= ' AND CREATOR = UPPER(' . $this->_conn->quote($this->_conn->getUsername()) . ')';
31 32

        $tables = $this->_conn->fetchAll($sql);
33

34
        return $this->filterAssetNames($this->_getPortableTablesList($tables));
35 36 37
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
38
     * {@inheritdoc}
39 40 41
     */
    protected function _getPortableTableColumnDefinition($tableColumn)
    {
42
        $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
43

44 45 46
        $length    = null;
        $fixed     = null;
        $scale     = false;
47
        $precision = false;
48

49 50
        $default = null;

51
        if ($tableColumn['default'] !== null && $tableColumn['default'] !== 'NULL') {
52 53 54 55 56
            $default = $tableColumn['default'];

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

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

61
        if (isset($tableColumn['comment'])) {
62
            $type                   = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
63 64 65
            $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
        }

66 67 68
        switch (strtolower($tableColumn['typename'])) {
            case 'varchar':
                $length = $tableColumn['length'];
69
                $fixed  = false;
70 71 72
                break;
            case 'character':
                $length = $tableColumn['length'];
73
                $fixed  = true;
74 75 76 77 78 79 80
                break;
            case 'clob':
                $length = $tableColumn['length'];
                break;
            case 'decimal':
            case 'double':
            case 'real':
81
                $scale     = $tableColumn['scale'];
82 83 84 85
                $precision = $tableColumn['length'];
                break;
        }

86
        $options = [
87
            'length'        => $length,
Sergei Morozov's avatar
Sergei Morozov committed
88
            'unsigned'      => false,
89
            'fixed'         => (bool) $fixed,
90
            'default'       => $default,
91 92
            'autoincrement' => (bool) $tableColumn['autoincrement'],
            'notnull'       => (bool) ($tableColumn['nulls'] === 'N'),
93 94
            'scale'         => null,
            'precision'     => null,
95 96 97
            'comment'       => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
                ? $tableColumn['comment']
                : null,
98 99
            'platformOptions' => [],
        ];
100 101

        if ($scale !== null && $precision !== null) {
102
            $options['scale']     = $scale;
103 104 105
            $options['precision'] = $precision;
        }

106
        return new Column($tableColumn['colname'], Type::getType($type), $options);
107 108
    }

Benjamin Morel's avatar
Benjamin Morel committed
109 110 111
    /**
     * {@inheritdoc}
     */
112 113
    protected function _getPortableTablesList($tables)
    {
114
        $tableNames = [];
115
        foreach ($tables as $tableRow) {
116
            $tableRow     = array_change_key_case($tableRow, CASE_LOWER);
117 118
            $tableNames[] = $tableRow['name'];
        }
Benjamin Morel's avatar
Benjamin Morel committed
119

120 121 122
        return $tableNames;
    }

Benjamin Morel's avatar
Benjamin Morel committed
123 124 125
    /**
     * {@inheritdoc}
     */
126
    protected function _getPortableTableIndexesList($tableIndexRows, $tableName = null)
127
    {
128
        foreach ($tableIndexRows as &$tableIndexRow) {
129 130
            $tableIndexRow            = array_change_key_case($tableIndexRow, CASE_LOWER);
            $tableIndexRow['primary'] = (bool) $tableIndexRow['primary'];
131 132
        }

133
        return parent::_getPortableTableIndexesList($tableIndexRows, $tableName);
134 135
    }

Benjamin Morel's avatar
Benjamin Morel committed
136 137 138
    /**
     * {@inheritdoc}
     */
139 140 141
    protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
    {
        return new ForeignKeyConstraint(
142 143 144 145 146
            $tableForeignKey['local_columns'],
            $tableForeignKey['foreign_table'],
            $tableForeignKey['foreign_columns'],
            $tableForeignKey['name'],
            $tableForeignKey['options']
147 148 149
        );
    }

150 151 152 153 154
    /**
     * {@inheritdoc}
     */
    protected function _getPortableTableForeignKeysList($tableForeignKeys)
    {
155
        $foreignKeys = [];
156 157

        foreach ($tableForeignKeys as $tableForeignKey) {
158
            $tableForeignKey = array_change_key_case($tableForeignKey, CASE_LOWER);
159

160
            if (! isset($foreignKeys[$tableForeignKey['index_name']])) {
161 162
                $foreignKeys[$tableForeignKey['index_name']] = [
                    'local_columns'   => [$tableForeignKey['local_column']],
163
                    'foreign_table'   => $tableForeignKey['foreign_table'],
164
                    'foreign_columns' => [$tableForeignKey['foreign_column']],
165
                    'name'            => $tableForeignKey['index_name'],
166
                    'options'         => [
167 168
                        'onUpdate' => $tableForeignKey['on_update'],
                        'onDelete' => $tableForeignKey['on_delete'],
169
                    ],
170
                ];
171
            } else {
172
                $foreignKeys[$tableForeignKey['index_name']]['local_columns'][]   = $tableForeignKey['local_column'];
173 174 175 176 177 178 179
                $foreignKeys[$tableForeignKey['index_name']]['foreign_columns'][] = $tableForeignKey['foreign_column'];
            }
        }

        return parent::_getPortableTableForeignKeysList($foreignKeys);
    }

Benjamin Morel's avatar
Benjamin Morel committed
180
    /**
181
     * @param string $def
182 183
     *
     * @return string|null
Benjamin Morel's avatar
Benjamin Morel committed
184
     */
185 186
    protected function _getPortableForeignKeyRuleDef($def)
    {
187 188
        if ($def === 'C') {
            return 'CASCADE';
189 190 191
        }

        if ($def === 'N') {
192
            return 'SET NULL';
193
        }
Benjamin Morel's avatar
Benjamin Morel committed
194

195 196 197
        return null;
    }

Benjamin Morel's avatar
Benjamin Morel committed
198 199 200
    /**
     * {@inheritdoc}
     */
201 202
    protected function _getPortableViewDefinition($view)
    {
203
        $view = array_change_key_case($view, CASE_LOWER);
204 205
        // sadly this still segfaults on PDO_IBM, see http://pecl.php.net/bugs/bug.php?id=17199
        //$view['text'] = (is_resource($view['text']) ? stream_get_contents($view['text']) : $view['text']);
206
        if (! is_resource($view['text'])) {
207 208 209 210 211
            $pos = strpos($view['text'], ' AS ');
            $sql = substr($view['text'], $pos+4);
        } else {
            $sql = '';
        }
212 213 214

        return new View($view['name'], $sql);
    }
215 216 217 218 219 220 221 222 223 224

    public function listTableDetails($tableName) : Table
    {
        $table = parent::listTableDetails($tableName);

        /** @var DB2Platform $platform */
        $platform = $this->_platform;
        $sql      = $platform->getListTableCommentsSQL($tableName);

        $tableOptions = $this->_conn->fetchAssoc($sql);
225 226 227 228

        if ($tableOptions !== false) {
            $table->addOption('comment', $tableOptions['REMARKS']);
        }
229 230 231

        return $table;
    }
232
}