MySqlSchemaManager.php 8.27 KB
Newer Older
romanb's avatar
romanb committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
Benjamin Eberlei's avatar
Benjamin Eberlei committed
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
romanb's avatar
romanb committed
18 19
 */

20
namespace Doctrine\DBAL\Schema;
romanb's avatar
romanb committed
21

22
use Doctrine\DBAL\Platforms\MySqlPlatform;
23 24
use Doctrine\DBAL\Types\Type;

romanb's avatar
romanb committed
25
/**
26
 * Schema manager for the MySql RDBMS.
romanb's avatar
romanb committed
27
 *
Benjamin Morel's avatar
Benjamin Morel committed
28 29 30 31 32
 * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
 * @author Roman Borschel <roman@code-factory.org>
 * @author Benjamin Eberlei <kontakt@beberlei.de>
 * @since  2.0
romanb's avatar
romanb committed
33
 */
34
class MySqlSchemaManager extends AbstractSchemaManager
35
{
Benjamin Morel's avatar
Benjamin Morel committed
36 37 38
    /**
     * {@inheritdoc}
     */
39 40
    protected function _getPortableViewDefinition($view)
    {
41
        return new View($view['TABLE_NAME'], $view['VIEW_DEFINITION']);
42 43
    }

Benjamin Morel's avatar
Benjamin Morel committed
44 45 46
    /**
     * {@inheritdoc}
     */
47 48
    protected function _getPortableTableDefinition($table)
    {
49
        return array_shift($table);
50 51
    }

Benjamin Morel's avatar
Benjamin Morel committed
52 53 54
    /**
     * {@inheritdoc}
     */
55 56 57
    protected function _getPortableUserDefinition($user)
    {
        return array(
58 59
            'user' => $user['User'],
            'password' => $user['Password'],
60 61 62
        );
    }

Benjamin Morel's avatar
Benjamin Morel committed
63 64 65
    /**
     * {@inheritdoc}
     */
66
    protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
67
    {
Steve Müller's avatar
Steve Müller committed
68
        foreach ($tableIndexes as $k => $v) {
69
            $v = array_change_key_case($v, CASE_LOWER);
Steve Müller's avatar
Steve Müller committed
70
            if ($v['key_name'] == 'PRIMARY') {
71 72 73 74
                $v['primary'] = true;
            } else {
                $v['primary'] = false;
            }
75 76
            if (strpos($v['index_type'], 'FULLTEXT') !== false) {
                $v['flags'] = array('FULLTEXT');
77 78
            } elseif (strpos($v['index_type'], 'SPATIAL') !== false) {
                $v['flags'] = array('SPATIAL');
79
            }
80
            $tableIndexes[$k] = $v;
81
        }
82

83
        return parent::_getPortableTableIndexesList($tableIndexes, $tableName);
84 85
    }

Benjamin Morel's avatar
Benjamin Morel committed
86 87 88
    /**
     * {@inheritdoc}
     */
89 90 91 92 93
    protected function _getPortableSequenceDefinition($sequence)
    {
        return end($sequence);
    }

Benjamin Morel's avatar
Benjamin Morel committed
94 95 96
    /**
     * {@inheritdoc}
     */
97 98
    protected function _getPortableDatabaseDefinition($database)
    {
99
        return $database['Database'];
100
    }
101

102
    /**
Benjamin Morel's avatar
Benjamin Morel committed
103
     * {@inheritdoc}
104
     */
105 106
    protected function _getPortableTableColumnDefinition($tableColumn)
    {
107 108 109
        $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);

        $dbType = strtolower($tableColumn['type']);
110 111 112 113 114 115
        $dbType = strtok($dbType, '(), ');
        if (isset($tableColumn['length'])) {
            $length = $tableColumn['length'];
        } else {
            $length = strtok('(), ');
        }
Benjamin Morel's avatar
Benjamin Morel committed
116

117
        $fixed = null;
118 119 120 121

        if ( ! isset($tableColumn['name'])) {
            $tableColumn['name'] = '';
        }
122

123
        $scale = null;
124
        $precision = null;
125

126
        $type = $this->_platform->getDoctrineTypeMapping($dbType);
127 128 129 130 131 132

        // In cases where not connected to a database DESCRIBE $table does not return 'Comment'
        if (isset($tableColumn['comment'])) {
            $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
            $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
        }
133

134 135
        switch ($dbType) {
            case 'char':
Steve Müller's avatar
Steve Müller committed
136
            case 'binary':
137
                $fixed = true;
138
                break;
139 140 141 142
            case 'float':
            case 'double':
            case 'real':
            case 'numeric':
143
            case 'decimal':
Steve Müller's avatar
Steve Müller committed
144
                if (preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['type'], $match)) {
145 146 147 148
                    $precision = $match[1];
                    $scale = $match[2];
                    $length = null;
                }
149
                break;
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
            case 'tinytext':
                $length = MySqlPlatform::LENGTH_LIMIT_TINYTEXT;
                break;
            case 'text':
                $length = MySqlPlatform::LENGTH_LIMIT_TEXT;
                break;
            case 'mediumtext':
                $length = MySqlPlatform::LENGTH_LIMIT_MEDIUMTEXT;
                break;
            case 'tinyblob':
                $length = MySqlPlatform::LENGTH_LIMIT_TINYBLOB;
                break;
            case 'blob':
                $length = MySqlPlatform::LENGTH_LIMIT_BLOB;
                break;
            case 'mediumblob':
                $length = MySqlPlatform::LENGTH_LIMIT_MEDIUMBLOB;
                break;
168 169 170 171 172 173
            case 'tinyint':
            case 'smallint':
            case 'mediumint':
            case 'int':
            case 'integer':
            case 'bigint':
174 175
            case 'year':
                $length = null;
176
                break;
177 178 179 180
        }

        $length = ((int) $length == 0) ? null : (int) $length;

181
        $options = array(
182
            'length'        => $length,
183
            'unsigned'      => (bool) (strpos($tableColumn['type'], 'unsigned') !== false),
184
            'fixed'         => (bool) $fixed,
185
            'default'       => isset($tableColumn['default']) ? $tableColumn['default'] : null,
186
            'notnull'       => (bool) ($tableColumn['null'] != 'YES'),
187 188
            'scale'         => null,
            'precision'     => null,
189
            'autoincrement' => (bool) (strpos($tableColumn['extra'], 'auto_increment') !== false),
190 191 192
            'comment'       => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
                ? $tableColumn['comment']
                : null,
193 194
        );

195
        if ($scale !== null && $precision !== null) {
196 197
            $options['scale'] = $scale;
            $options['precision'] = $precision;
198 199
        }

200 201 202 203 204 205 206
        $column = new Column($tableColumn['field'], Type::getType($type), $options);

        if (isset($tableColumn['collation'])) {
            $column->setPlatformOption('collation', $tableColumn['collation']);
        }

        return $column;
207 208
    }

Benjamin Morel's avatar
Benjamin Morel committed
209 210 211
    /**
     * {@inheritdoc}
     */
212
    protected function _getPortableTableForeignKeysList($tableForeignKeys)
romanb's avatar
romanb committed
213
    {
214
        $list = array();
Benjamin Morel's avatar
Benjamin Morel committed
215
        foreach ($tableForeignKeys as $value) {
216 217 218 219 220 221 222 223
            $value = array_change_key_case($value, CASE_LOWER);
            if (!isset($list[$value['constraint_name']])) {
                if (!isset($value['delete_rule']) || $value['delete_rule'] == "RESTRICT") {
                    $value['delete_rule'] = null;
                }
                if (!isset($value['update_rule']) || $value['update_rule'] == "RESTRICT") {
                    $value['update_rule'] = null;
                }
224

225 226 227 228 229 230 231 232 233 234 235
                $list[$value['constraint_name']] = array(
                    'name' => $value['constraint_name'],
                    'local' => array(),
                    'foreign' => array(),
                    'foreignTable' => $value['referenced_table_name'],
                    'onDelete' => $value['delete_rule'],
                    'onUpdate' => $value['update_rule'],
                );
            }
            $list[$value['constraint_name']]['local'][] = $value['column_name'];
            $list[$value['constraint_name']]['foreign'][] = $value['referenced_column_name'];
236
        }
237 238

        $result = array();
Steve Müller's avatar
Steve Müller committed
239
        foreach ($list as $constraint) {
240 241 242 243 244 245 246 247
            $result[] = new ForeignKeyConstraint(
                array_values($constraint['local']), $constraint['foreignTable'],
                array_values($constraint['foreign']), $constraint['name'],
                array(
                    'onDelete' => $constraint['onDelete'],
                    'onUpdate' => $constraint['onUpdate'],
                )
            );
248
        }
249

250
        return $result;
romanb's avatar
romanb committed
251
    }
252
}