MySqlSchemaManager.php 7.32 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

/**
23
 * Schema manager for the MySql RDBMS.
romanb's avatar
romanb committed
24
 *
Benjamin Morel's avatar
Benjamin Morel committed
25 26 27 28 29
 * @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
30
 */
31
class MySqlSchemaManager extends AbstractSchemaManager
32
{
Benjamin Morel's avatar
Benjamin Morel committed
33 34 35
    /**
     * {@inheritdoc}
     */
36 37
    protected function _getPortableViewDefinition($view)
    {
38
        return new View($view['TABLE_NAME'], $view['VIEW_DEFINITION']);
39 40
    }

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

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

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

78
        return parent::_getPortableTableIndexesList($tableIndexes, $tableName);
79 80
    }

Benjamin Morel's avatar
Benjamin Morel committed
81 82 83
    /**
     * {@inheritdoc}
     */
84 85 86 87 88
    protected function _getPortableSequenceDefinition($sequence)
    {
        return end($sequence);
    }

Benjamin Morel's avatar
Benjamin Morel committed
89 90 91
    /**
     * {@inheritdoc}
     */
92 93
    protected function _getPortableDatabaseDefinition($database)
    {
94
        return $database['Database'];
95
    }
96

97
    /**
Benjamin Morel's avatar
Benjamin Morel committed
98
     * {@inheritdoc}
99
     */
100 101
    protected function _getPortableTableColumnDefinition($tableColumn)
    {
102 103 104
        $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);

        $dbType = strtolower($tableColumn['type']);
105 106 107 108 109 110
        $dbType = strtok($dbType, '(), ');
        if (isset($tableColumn['length'])) {
            $length = $tableColumn['length'];
        } else {
            $length = strtok('(), ');
        }
Benjamin Morel's avatar
Benjamin Morel committed
111

112
        $fixed = null;
113 114 115 116

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

118
        $scale = null;
119
        $precision = null;
120

121
        $type = $this->_platform->getDoctrineTypeMapping($dbType);
122 123 124 125 126 127

        // 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);
        }
128

129 130
        switch ($dbType) {
            case 'char':
Steve Müller's avatar
Steve Müller committed
131
            case 'binary':
132
                $fixed = true;
133
                break;
134 135 136 137
            case 'float':
            case 'double':
            case 'real':
            case 'numeric':
138
            case 'decimal':
Steve Müller's avatar
Steve Müller committed
139
                if (preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['type'], $match)) {
140 141 142 143
                    $precision = $match[1];
                    $scale = $match[2];
                    $length = null;
                }
144
                break;
145 146 147 148 149 150
            case 'tinyint':
            case 'smallint':
            case 'mediumint':
            case 'int':
            case 'integer':
            case 'bigint':
151 152 153 154 155 156
            case 'tinyblob':
            case 'mediumblob':
            case 'longblob':
            case 'blob':
            case 'year':
                $length = null;
157
                break;
158 159 160 161
        }

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

162
        $options = array(
163
            'length'        => $length,
164
            'unsigned'      => (bool) (strpos($tableColumn['type'], 'unsigned') !== false),
165
            'fixed'         => (bool) $fixed,
166
            'default'       => isset($tableColumn['default']) ? $tableColumn['default'] : null,
167
            'notnull'       => (bool) ($tableColumn['null'] != 'YES'),
168 169
            'scale'         => null,
            'precision'     => null,
170
            'autoincrement' => (bool) (strpos($tableColumn['extra'], 'auto_increment') !== false),
171
            'comment'       => (isset($tableColumn['comment'])) ? $tableColumn['comment'] : null
172 173
        );

174
        if ($scale !== null && $precision !== null) {
175 176
            $options['scale'] = $scale;
            $options['precision'] = $precision;
177 178
        }

179
        return new Column($tableColumn['field'], \Doctrine\DBAL\Types\Type::getType($type), $options);
180 181
    }

Benjamin Morel's avatar
Benjamin Morel committed
182 183 184
    /**
     * {@inheritdoc}
     */
185
    protected function _getPortableTableForeignKeysList($tableForeignKeys)
romanb's avatar
romanb committed
186
    {
187
        $list = array();
Benjamin Morel's avatar
Benjamin Morel committed
188
        foreach ($tableForeignKeys as $value) {
189 190 191 192 193 194 195 196
            $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;
                }
197

198 199 200 201 202 203 204 205 206 207 208
                $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'];
209
        }
210 211

        $result = array();
Steve Müller's avatar
Steve Müller committed
212
        foreach ($list as $constraint) {
213 214 215 216 217 218 219 220
            $result[] = new ForeignKeyConstraint(
                array_values($constraint['local']), $constraint['foreignTable'],
                array_values($constraint['foreign']), $constraint['name'],
                array(
                    'onDelete' => $constraint['onDelete'],
                    'onUpdate' => $constraint['onUpdate'],
                )
            );
221
        }
222

223
        return $result;
romanb's avatar
romanb committed
224
    }
225
}