MySqlSchemaManager.php 6.31 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 80 81 82 83 84 85
    
    /**
     * Gets a portable column definition.
     * 
     * The database type is mapped to a corresponding Doctrine mapping type.
     * 
     * @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
        switch ($dbType) {
            case 'char':
112
                $fixed = true;
113
                break;
114 115 116 117
            case 'float':
            case 'double':
            case 'real':
            case 'numeric':
118
            case 'decimal':
119
                if(preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['type'], $match)) {
120 121 122 123
                    $precision = $match[1];
                    $scale = $match[2];
                    $length = null;
                }
124
                break;
125 126 127 128 129 130
            case 'tinyint':
            case 'smallint':
            case 'mediumint':
            case 'int':
            case 'integer':
            case 'bigint':
131 132 133 134 135 136 137 138
            case 'tinyblob':
            case 'mediumblob':
            case 'longblob':
            case 'blob':
            case 'binary':
            case 'varbinary':
            case 'year':
                $length = null;
139
                break;
140 141 142 143 144 145 146 147 148 149
        }

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

150
        $options = array(
151 152 153
            'length'        => $length,
            'unsigned'      => (bool)$unsigned,
            'fixed'         => (bool)$fixed,
154 155
            'default'       => $tableColumn['default'],
            'notnull'       => (bool) ($tableColumn['null'] != 'YES'),
156 157
            'scale'         => null,
            'precision'     => null,
158
            'autoincrement' => (bool) (strpos($tableColumn['extra'], 'auto_increment') !== false),
159 160
        );

161
        if ($scale !== null && $precision !== null) {
162 163
            $options['scale'] = $scale;
            $options['precision'] = $precision;
164 165
        }

166
        return new Column($tableColumn['field'], \Doctrine\DBAL\Types\Type::getType($type), $options);
167 168
    }

169
    public function _getPortableTableForeignKeyDefinition($tableForeignKey)
romanb's avatar
romanb committed
170
    {
171
        $tableForeignKey = array_change_key_case($tableForeignKey, CASE_LOWER);
172

173
        if (!isset($tableForeignKey['delete_rule']) || $tableForeignKey['delete_rule'] == "RESTRICT") {
174 175
            $tableForeignKey['delete_rule'] = null;
        }
176
        if (!isset($tableForeignKey['update_rule']) || $tableForeignKey['update_rule'] == "RESTRICT") {
177 178
            $tableForeignKey['update_rule'] = null;
        }
179
        
180
        return new ForeignKeyConstraint(
181 182 183 184
            (array)$tableForeignKey['column_name'],
            $tableForeignKey['referenced_table_name'],
            (array)$tableForeignKey['referenced_column_name'],
            $tableForeignKey['constraint_name'],
185 186 187 188
            array(
                'onUpdate' => $tableForeignKey['update_rule'],
                'onDelete' => $tableForeignKey['delete_rule'],
            )
189
        );
romanb's avatar
romanb committed
190
    }
191
}