3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\DBAL\Schema;
23 * Schema manager for the MySql RDBMS.
25 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
26 * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
27 * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
28 * @author Roman Borschel <roman@code-factory.org>
29 * @author Benjamin Eberlei <kontakt@beberlei.de>
33 class MySqlSchemaManager extends AbstractSchemaManager
35 protected function _getPortableViewDefinition($view)
37 return new View($view['TABLE_NAME'], $view['VIEW_DEFINITION']);
40 protected function _getPortableTableDefinition($table)
42 return array_shift($table);
45 protected function _getPortableUserDefinition($user)
48 'user' => $user['User'],
49 'password' => $user['Password'],
53 protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
55 foreach($tableIndexes as $k => $v) {
56 $v = array_change_key_case($v, CASE_LOWER);
57 if($v['key_name'] == 'PRIMARY') {
60 $v['primary'] = false;
62 if (strpos($v['index_type'], 'FULLTEXT') !== false) {
63 $v['flags'] = array('FULLTEXT');
65 $tableIndexes[$k] = $v;
68 return parent::_getPortableTableIndexesList($tableIndexes, $tableName);
71 protected function _getPortableSequenceDefinition($sequence)
73 return end($sequence);
76 protected function _getPortableDatabaseDefinition($database)
78 return $database['Database'];
82 * Gets a portable column definition.
84 * The database type is mapped to a corresponding Doctrine mapping type.
89 protected function _getPortableTableColumnDefinition($tableColumn)
91 $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
93 $dbType = strtolower($tableColumn['type']);
94 $dbType = strtok($dbType, '(), ');
95 if (isset($tableColumn['length'])) {
96 $length = $tableColumn['length'];
99 $length = strtok('(), ');
100 $decimal = strtok('(), ') ? strtok('(), '):null;
105 if ( ! isset($tableColumn['name'])) {
106 $tableColumn['name'] = '';
112 $type = $this->_platform->getDoctrineTypeMapping($dbType);
113 $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
114 $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
125 if(preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['type'], $match)) {
126 $precision = $match[1];
146 $length = ((int) $length == 0) ? null : (int) $length;
150 'unsigned' => (bool) (strpos($tableColumn['type'], 'unsigned') !== false),
151 'fixed' => (bool) $fixed,
152 'default' => isset($tableColumn['default']) ? $tableColumn['default'] : null,
153 'notnull' => (bool) ($tableColumn['null'] != 'YES'),
156 'autoincrement' => (bool) (strpos($tableColumn['extra'], 'auto_increment') !== false),
157 'comment' => (isset($tableColumn['comment'])) ? $tableColumn['comment'] : null
160 if ($scale !== null && $precision !== null) {
161 $options['scale'] = $scale;
162 $options['precision'] = $precision;
165 return new Column($tableColumn['field'], \Doctrine\DBAL\Types\Type::getType($type), $options);
168 protected function _getPortableTableForeignKeysList($tableForeignKeys)
171 foreach ($tableForeignKeys as $key => $value) {
172 $value = array_change_key_case($value, CASE_LOWER);
173 if (!isset($list[$value['constraint_name']])) {
174 if (!isset($value['delete_rule']) || $value['delete_rule'] == "RESTRICT") {
175 $value['delete_rule'] = null;
177 if (!isset($value['update_rule']) || $value['update_rule'] == "RESTRICT") {
178 $value['update_rule'] = null;
181 $list[$value['constraint_name']] = array(
182 'name' => $value['constraint_name'],
184 'foreign' => array(),
185 'foreignTable' => $value['referenced_table_name'],
186 'onDelete' => $value['delete_rule'],
187 'onUpdate' => $value['update_rule'],
190 $list[$value['constraint_name']]['local'][] = $value['column_name'];
191 $list[$value['constraint_name']]['foreign'][] = $value['referenced_column_name'];
195 foreach($list as $constraint) {
196 $result[] = new ForeignKeyConstraint(
197 array_values($constraint['local']), $constraint['foreignTable'],
198 array_values($constraint['foreign']), $constraint['name'],
200 'onDelete' => $constraint['onDelete'],
201 'onUpdate' => $constraint['onUpdate'],