MySqlSchemaManager.php 7.34 KB
Newer Older
romanb's avatar
romanb committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?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
 * and is licensed under the LGPL. 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 25 26 27
 *
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
28
 * @author      Roman Borschel <roman@code-factory.org>
29
 * @author      Benjamin Eberlei <kontakt@beberlei.de>
romanb's avatar
romanb committed
30 31 32
 * @version     $Revision$
 * @since       2.0
 */
33
class MySqlSchemaManager extends AbstractSchemaManager
34
{
35 36
    protected function _getPortableViewDefinition($view)
    {
37
        return new View($view['TABLE_NAME'], $view['VIEW_DEFINITION']);
38 39 40 41
    }

    protected function _getPortableTableDefinition($table)
    {
42
        return array_shift($table);
43 44 45 46 47
    }

    protected function _getPortableUserDefinition($user)
    {
        return array(
48 49
            'user' => $user['User'],
            'password' => $user['Password'],
50 51 52
        );
    }

53
    protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
54
    {
55 56 57 58 59 60 61
        foreach($tableIndexes AS $k => $v) {
            $v = array_change_key_case($v, CASE_LOWER);
            if($v['key_name'] == 'PRIMARY') {
                $v['primary'] = true;
            } else {
                $v['primary'] = false;
            }
62
            $tableIndexes[$k] = $v;
63
        }
64

65
        return parent::_getPortableTableIndexesList($tableIndexes, $tableName);
66 67 68 69 70 71 72 73 74
    }

    protected function _getPortableSequenceDefinition($sequence)
    {
        return end($sequence);
    }

    protected function _getPortableDatabaseDefinition($database)
    {
75
        return $database['Database'];
76
    }
77

78 79
    /**
     * Gets a portable column definition.
80
     *
81
     * The database type is mapped to a corresponding Doctrine mapping type.
82
     *
83 84 85
     * @param $tableColumn
     * @return array
     */
86 87
    protected function _getPortableTableColumnDefinition($tableColumn)
    {
88 89 90
        $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);

        $dbType = strtolower($tableColumn['type']);
91 92 93 94 95 96 97 98 99 100 101 102 103 104
        $dbType = strtok($dbType, '(), ');
        if (isset($tableColumn['length'])) {
            $length = $tableColumn['length'];
            $decimal = '';
        } else {
            $length = strtok('(), ');
            $decimal = strtok('(), ') ? strtok('(), '):null;
        }
        $type = array();
        $unsigned = $fixed = null;

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

106
        $scale = null;
107
        $precision = null;
108

109
        $type = $this->_platform->getDoctrineTypeMapping($dbType);
110 111 112
        $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
        $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);

113 114
        switch ($dbType) {
            case 'char':
115
                $fixed = true;
116
                break;
117 118 119 120
            case 'float':
            case 'double':
            case 'real':
            case 'numeric':
121
            case 'decimal':
122
                if(preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['type'], $match)) {
123 124 125 126
                    $precision = $match[1];
                    $scale = $match[2];
                    $length = null;
                }
127
                break;
128 129 130 131 132 133
            case 'tinyint':
            case 'smallint':
            case 'mediumint':
            case 'int':
            case 'integer':
            case 'bigint':
134 135 136 137 138 139 140 141
            case 'tinyblob':
            case 'mediumblob':
            case 'longblob':
            case 'blob':
            case 'binary':
            case 'varbinary':
            case 'year':
                $length = null;
142
                break;
143 144 145 146 147 148 149 150 151 152
        }

        $length = ((int) $length == 0) ? null : (int) $length;
        $def =  array(
            'type' => $type,
            'length' => $length,
            'unsigned' => (bool) $unsigned,
            'fixed' => (bool) $fixed
        );

153
        $options = array(
154 155 156
            'length'        => $length,
            'unsigned'      => (bool)$unsigned,
            'fixed'         => (bool)$fixed,
157
            'default'       => isset($tableColumn['default']) ? $tableColumn['default'] : null,
158
            'notnull'       => (bool) ($tableColumn['null'] != 'YES'),
159 160
            'scale'         => null,
            'precision'     => null,
161
            'autoincrement' => (bool) (strpos($tableColumn['extra'], 'auto_increment') !== false),
162
            'comment'       => (isset($tableColumn['comment'])) ? $tableColumn['comment'] : null
163 164
        );

165
        if ($scale !== null && $precision !== null) {
166 167
            $options['scale'] = $scale;
            $options['precision'] = $precision;
168 169
        }

170
        return new Column($tableColumn['field'], \Doctrine\DBAL\Types\Type::getType($type), $options);
171 172
    }

173
    protected function _getPortableTableForeignKeysList($tableForeignKeys)
romanb's avatar
romanb committed
174
    {
175 176 177 178 179 180 181 182 183 184
        $list = array();
        foreach ($tableForeignKeys as $key => $value) {
            $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;
                }
185

186 187 188 189 190 191 192 193 194 195 196
                $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'];
197
        }
198 199 200 201 202 203 204 205 206 207 208

        $result = array();
        foreach($list AS $constraint) {
            $result[] = new ForeignKeyConstraint(
                array_values($constraint['local']), $constraint['foreignTable'],
                array_values($constraint['foreign']), $constraint['name'],
                array(
                    'onDelete' => $constraint['onDelete'],
                    'onUpdate' => $constraint['onUpdate'],
                )
            );
209
        }
210

211
        return $result;
romanb's avatar
romanb committed
212
    }
213
}