DB2SchemaManager.php 6.55 KB
Newer Older
1 2
<?php

Michael Moravec's avatar
Michael Moravec committed
3 4
declare(strict_types=1);

5 6
namespace Doctrine\DBAL\Schema;

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

18
/**
Benjamin Morel's avatar
Benjamin Morel committed
19
 * IBM Db2 Schema Manager.
20
 */
Benjamin Eberlei's avatar
Benjamin Eberlei committed
21
class DB2SchemaManager extends AbstractSchemaManager
22 23
{
    /**
Benjamin Morel's avatar
Benjamin Morel committed
24
     * {@inheritdoc}
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}
     */
29
    public function listTableNames() : array
30
    {
31
        $sql = $this->_platform->getListTablesSQL() . ' AND CREATOR = CURRENT_USER';
32

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

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

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

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

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

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

57 58
        $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'])
            ?? $this->_platform->getDoctrineTypeMapping($tableColumn['typename']);
59

60 61 62 63 64 65
        switch (strtolower($tableColumn['typename'])) {
            case 'varchar':
                $length = $tableColumn['length'];
                break;
            case 'character':
                $length = $tableColumn['length'];
66
                $fixed  = true;
67 68 69 70 71 72 73
                break;
            case 'clob':
                $length = $tableColumn['length'];
                break;
            case 'decimal':
            case 'double':
            case 'real':
74
                $scale     = $tableColumn['scale'];
75 76 77 78
                $precision = $tableColumn['length'];
                break;
        }

79
        $options = [
80
            'length'        => $length,
Sergei Morozov's avatar
Sergei Morozov committed
81
            'unsigned'      => false,
82
            'fixed'         => $fixed,
83
            'default'       => $default,
84
            'autoincrement' => (bool) $tableColumn['autoincrement'],
85
            'notnull'       => $tableColumn['nulls'] === 'N',
86 87 88
            'comment'       => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
                ? $tableColumn['comment']
                : null,
89 90
            'platformOptions' => [],
        ];
91 92

        if ($scale !== null && $precision !== null) {
93
            $options['scale']     = $scale;
94 95 96
            $options['precision'] = $precision;
        }

97
        return new Column($tableColumn['colname'], Type::getType($type), $options);
98 99
    }

Benjamin Morel's avatar
Benjamin Morel committed
100 101 102
    /**
     * {@inheritdoc}
     */
103
    protected function _getPortableTablesList(array $tables) : array
104
    {
105
        $tableNames = [];
106
        foreach ($tables as $tableRow) {
107
            $tableRow     = array_change_key_case($tableRow, CASE_LOWER);
108 109
            $tableNames[] = $tableRow['name'];
        }
Benjamin Morel's avatar
Benjamin Morel committed
110

111 112 113
        return $tableNames;
    }

Benjamin Morel's avatar
Benjamin Morel committed
114 115 116
    /**
     * {@inheritdoc}
     */
117
    protected function _getPortableTableIndexesList(array $tableIndexRows, string $tableName) : array
118
    {
119
        foreach ($tableIndexRows as &$tableIndexRow) {
120 121
            $tableIndexRow            = array_change_key_case($tableIndexRow, CASE_LOWER);
            $tableIndexRow['primary'] = (bool) $tableIndexRow['primary'];
122 123
        }

124
        return parent::_getPortableTableIndexesList($tableIndexRows, $tableName);
125 126
    }

Benjamin Morel's avatar
Benjamin Morel committed
127 128 129
    /**
     * {@inheritdoc}
     */
130
    protected function _getPortableTableForeignKeyDefinition(array $tableForeignKey) : ForeignKeyConstraint
131 132
    {
        return new ForeignKeyConstraint(
133 134 135 136 137
            $tableForeignKey['local_columns'],
            $tableForeignKey['foreign_table'],
            $tableForeignKey['foreign_columns'],
            $tableForeignKey['name'],
            $tableForeignKey['options']
138 139 140
        );
    }

141 142 143
    /**
     * {@inheritdoc}
     */
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 153
                $foreignKeys[$tableForeignKey['index_name']] = [
                    '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 159
                        'onUpdate' => $tableForeignKey['on_update'],
                        'onDelete' => $tableForeignKey['on_delete'],
160
                    ],
161
                ];
162
            } else {
163
                $foreignKeys[$tableForeignKey['index_name']]['local_columns'][]   = $tableForeignKey['local_column'];
164 165 166 167 168 169 170
                $foreignKeys[$tableForeignKey['index_name']]['foreign_columns'][] = $tableForeignKey['foreign_column'];
            }
        }

        return parent::_getPortableTableForeignKeysList($foreignKeys);
    }

Benjamin Morel's avatar
Benjamin Morel committed
171 172 173
    /**
     * {@inheritdoc}
     */
174
    protected function _getPortableViewDefinition(array $view) : View
175
    {
176
        $view = array_change_key_case($view, CASE_LOWER);
177 178
        // 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']);
179
        if (! is_resource($view['text'])) {
180 181 182 183 184
            $pos = strpos($view['text'], ' AS ');
            $sql = substr($view['text'], $pos+4);
        } else {
            $sql = '';
        }
185 186 187

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

189
    public function listTableDetails(string $tableName) : Table
190 191 192 193 194 195 196 197 198 199 200 201
    {
        $table = parent::listTableDetails($tableName);

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

        $tableOptions = $this->_conn->fetchAssoc($sql);
        $table->addOption('comment', $tableOptions['REMARKS']);

        return $table;
    }
202
}