2008-08-09 13:45:28 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* 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
|
2009-05-30 14:16:54 +04:00
|
|
|
* <http://www.doctrine-project.org>.
|
2008-08-09 13:45:28 +04:00
|
|
|
*/
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\DBAL\Schema;
|
2008-08-09 13:45:28 +04:00
|
|
|
|
|
|
|
/**
|
2009-10-15 00:18:36 +04:00
|
|
|
* Schema manager for the MySql RDBMS.
|
2008-08-09 13:45:28 +04:00
|
|
|
*
|
|
|
|
* @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)
|
2009-10-15 00:18:36 +04:00
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2009-11-03 11:58:03 +03:00
|
|
|
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
2008-08-09 13:45:28 +04:00
|
|
|
* @version $Revision$
|
|
|
|
* @since 2.0
|
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
class MySqlSchemaManager extends AbstractSchemaManager
|
2009-01-07 20:46:02 +03:00
|
|
|
{
|
2009-05-28 06:04:51 +04:00
|
|
|
protected function _getPortableViewDefinition($view)
|
|
|
|
{
|
2010-02-11 02:41:35 +03:00
|
|
|
return new View($view['TABLE_NAME'], $view['VIEW_DEFINITION']);
|
2009-05-28 06:04:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function _getPortableTableDefinition($table)
|
|
|
|
{
|
2009-12-01 02:12:26 +03:00
|
|
|
return array_shift($table);
|
2009-05-28 06:04:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function _getPortableUserDefinition($user)
|
|
|
|
{
|
|
|
|
return array(
|
2009-08-17 21:58:16 +04:00
|
|
|
'user' => $user['User'],
|
|
|
|
'password' => $user['Password'],
|
2009-05-28 06:04:51 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2009-11-03 11:58:03 +03:00
|
|
|
protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
|
2009-05-28 06:04:51 +04:00
|
|
|
{
|
2009-11-03 11:58:03 +03:00
|
|
|
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;
|
|
|
|
}
|
2009-12-02 21:52:21 +03:00
|
|
|
$tableIndexes[$k] = $v;
|
2009-05-28 06:04:51 +04:00
|
|
|
}
|
2009-11-03 11:58:03 +03:00
|
|
|
|
2009-12-02 21:52:21 +03:00
|
|
|
return parent::_getPortableTableIndexesList($tableIndexes, $tableName);
|
2009-05-28 06:04:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function _getPortableSequenceDefinition($sequence)
|
|
|
|
{
|
|
|
|
return end($sequence);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _getPortableDatabaseDefinition($database)
|
|
|
|
{
|
2009-08-17 21:58:16 +04:00
|
|
|
return $database['Database'];
|
2009-05-28 06:04:51 +04:00
|
|
|
}
|
2009-10-15 00:18:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a portable column definition.
|
|
|
|
*
|
|
|
|
* The database type is mapped to a corresponding Doctrine mapping type.
|
|
|
|
*
|
|
|
|
* @param $tableColumn
|
|
|
|
* @return array
|
|
|
|
*/
|
2009-05-28 02:14:27 +04:00
|
|
|
protected function _getPortableTableColumnDefinition($tableColumn)
|
|
|
|
{
|
2009-08-17 21:58:16 +04:00
|
|
|
$dbType = strtolower($tableColumn['Type']);
|
2009-05-28 02:14:27 +04:00
|
|
|
$dbType = strtok($dbType, '(), ');
|
|
|
|
if ($dbType == 'national') {
|
|
|
|
$dbType = strtok('(), ');
|
|
|
|
}
|
|
|
|
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'] = '';
|
|
|
|
}
|
2009-11-03 19:56:05 +03:00
|
|
|
|
2009-05-28 02:14:27 +04:00
|
|
|
$scale = null;
|
2009-11-03 19:56:05 +03:00
|
|
|
$precision = null;
|
2009-09-13 03:25:47 +04:00
|
|
|
|
2009-10-15 00:18:36 +04:00
|
|
|
// Map db type to Doctrine mapping type
|
2009-05-28 02:14:27 +04:00
|
|
|
switch ($dbType) {
|
|
|
|
case 'tinyint':
|
|
|
|
$type = 'boolean';
|
2009-10-15 00:18:36 +04:00
|
|
|
$length = null;
|
|
|
|
break;
|
2009-05-28 02:14:27 +04:00
|
|
|
case 'smallint':
|
2009-10-15 00:18:36 +04:00
|
|
|
$type = 'smallint';
|
|
|
|
$length = null;
|
|
|
|
break;
|
2009-05-28 02:14:27 +04:00
|
|
|
case 'mediumint':
|
|
|
|
$type = 'integer';
|
2009-10-15 00:18:36 +04:00
|
|
|
$length = null;
|
|
|
|
break;
|
2009-05-28 02:14:27 +04:00
|
|
|
case 'int':
|
|
|
|
case 'integer':
|
|
|
|
$type = 'integer';
|
2009-10-15 00:18:36 +04:00
|
|
|
$length = null;
|
|
|
|
break;
|
2009-05-28 02:14:27 +04:00
|
|
|
case 'bigint':
|
2009-10-15 00:18:36 +04:00
|
|
|
$type = 'bigint';
|
|
|
|
$length = null;
|
|
|
|
break;
|
2009-05-28 02:14:27 +04:00
|
|
|
case 'tinytext':
|
|
|
|
case 'mediumtext':
|
|
|
|
case 'longtext':
|
|
|
|
case 'text':
|
2009-10-15 00:18:36 +04:00
|
|
|
$type = 'text';
|
|
|
|
$fixed = false;
|
|
|
|
break;
|
2009-05-28 02:14:27 +04:00
|
|
|
case 'varchar':
|
|
|
|
$fixed = false;
|
|
|
|
case 'string':
|
|
|
|
case 'char':
|
|
|
|
$type = 'string';
|
|
|
|
if ($length == '1') {
|
|
|
|
$type = 'boolean';
|
|
|
|
if (preg_match('/^(is|has)/', $tableColumn['name'])) {
|
|
|
|
$type = array_reverse($type);
|
|
|
|
}
|
2009-10-15 00:18:36 +04:00
|
|
|
} else if (strstr($dbType, 'text')) {
|
|
|
|
$type = 'text';
|
2009-05-28 02:14:27 +04:00
|
|
|
if ($decimal == 'binary') {
|
|
|
|
$type = 'blob';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($fixed !== false) {
|
|
|
|
$fixed = true;
|
|
|
|
}
|
2009-10-15 00:18:36 +04:00
|
|
|
break;
|
2009-05-28 02:14:27 +04:00
|
|
|
case 'set':
|
|
|
|
$fixed = false;
|
|
|
|
$type = 'text';
|
2009-10-15 00:18:36 +04:00
|
|
|
$type = 'integer'; //FIXME:???
|
|
|
|
break;
|
2009-05-28 02:14:27 +04:00
|
|
|
case 'date':
|
|
|
|
$type = 'date';
|
2009-10-15 00:18:36 +04:00
|
|
|
break;
|
2009-05-28 02:14:27 +04:00
|
|
|
case 'datetime':
|
|
|
|
case 'timestamp':
|
2009-10-15 00:18:36 +04:00
|
|
|
$type = 'datetime';
|
|
|
|
break;
|
2009-05-28 02:14:27 +04:00
|
|
|
case 'time':
|
|
|
|
$type = 'time';
|
2009-10-15 00:18:36 +04:00
|
|
|
break;
|
2009-05-28 02:14:27 +04:00
|
|
|
case 'float':
|
|
|
|
case 'double':
|
|
|
|
case 'real':
|
|
|
|
case 'numeric':
|
2009-10-31 23:50:31 +03:00
|
|
|
case 'decimal':
|
2009-11-03 19:56:05 +03:00
|
|
|
if(preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['Type'], $match)) {
|
|
|
|
$precision = $match[1];
|
|
|
|
$scale = $match[2];
|
|
|
|
$length = null;
|
|
|
|
}
|
2009-05-28 02:14:27 +04:00
|
|
|
$type = 'decimal';
|
2009-10-15 00:18:36 +04:00
|
|
|
break;
|
2009-05-28 02:14:27 +04:00
|
|
|
case 'tinyblob':
|
|
|
|
case 'mediumblob':
|
|
|
|
case 'longblob':
|
|
|
|
case 'blob':
|
|
|
|
case 'binary':
|
|
|
|
case 'varbinary':
|
|
|
|
$type = 'blob';
|
|
|
|
$length = null;
|
2009-10-15 00:18:36 +04:00
|
|
|
break;
|
2009-05-28 02:14:27 +04:00
|
|
|
case 'year':
|
|
|
|
$type = 'integer';
|
|
|
|
$type = 'date';
|
|
|
|
$length = null;
|
2009-10-15 00:18:36 +04:00
|
|
|
break;
|
2009-05-28 02:14:27 +04:00
|
|
|
case 'geometry':
|
|
|
|
case 'geometrycollection':
|
|
|
|
case 'point':
|
|
|
|
case 'multipoint':
|
|
|
|
case 'linestring':
|
|
|
|
case 'multilinestring':
|
|
|
|
case 'polygon':
|
|
|
|
case 'multipolygon':
|
|
|
|
$type = 'blob';
|
|
|
|
$length = null;
|
2009-10-15 00:18:36 +04:00
|
|
|
break;
|
2009-05-28 02:14:27 +04:00
|
|
|
default:
|
|
|
|
$type = 'string';
|
|
|
|
$length = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$length = ((int) $length == 0) ? null : (int) $length;
|
|
|
|
$def = array(
|
|
|
|
'type' => $type,
|
|
|
|
'length' => $length,
|
|
|
|
'unsigned' => (bool) $unsigned,
|
|
|
|
'fixed' => (bool) $fixed
|
|
|
|
);
|
|
|
|
|
2009-11-30 22:02:05 +03:00
|
|
|
$options = array(
|
2009-11-03 19:56:05 +03:00
|
|
|
'length' => $length,
|
|
|
|
'unsigned' => (bool)$unsigned,
|
|
|
|
'fixed' => (bool)$fixed,
|
2009-08-17 21:58:16 +04:00
|
|
|
'default' => $tableColumn['Default'],
|
|
|
|
'notnull' => (bool) ($tableColumn['Null'] != 'YES'),
|
2009-11-03 19:56:05 +03:00
|
|
|
'scale' => null,
|
|
|
|
'precision' => null,
|
2009-11-30 22:02:05 +03:00
|
|
|
'platformOptions' => array(
|
2009-11-03 19:56:05 +03:00
|
|
|
'primary' => (strtolower($tableColumn['Key']) == 'pri') ? true : false,
|
|
|
|
'unique' => (strtolower($tableColumn['Key']) == 'uni') ? true :false,
|
|
|
|
'autoincrement' => (bool) (strpos($tableColumn['Extra'], 'auto_increment') !== false),
|
|
|
|
),
|
2009-05-28 02:14:27 +04:00
|
|
|
);
|
|
|
|
|
2009-11-03 19:56:05 +03:00
|
|
|
if ($scale !== null && $precision !== null) {
|
2009-11-30 22:02:05 +03:00
|
|
|
$options['scale'] = $scale;
|
|
|
|
$options['precision'] = $precision;
|
2009-11-03 19:56:05 +03:00
|
|
|
}
|
|
|
|
|
2009-12-05 01:41:09 +03:00
|
|
|
return new Column($tableColumn['Field'], \Doctrine\DBAL\Types\Type::getType($type), $options);
|
2009-05-28 02:14:27 +04:00
|
|
|
}
|
|
|
|
|
2009-05-29 01:34:35 +04:00
|
|
|
public function _getPortableTableForeignKeyDefinition($tableForeignKey)
|
2008-08-09 13:45:28 +04:00
|
|
|
{
|
2009-05-29 01:34:35 +04:00
|
|
|
$tableForeignKey = array_change_key_case($tableForeignKey, CASE_LOWER);
|
2010-02-07 15:36:30 +03:00
|
|
|
|
2010-02-26 04:26:42 +03:00
|
|
|
if (!isset($tableForeignKey['delete_rule']) || $tableForeignKey['delete_rule'] == "RESTRICT") {
|
2010-02-07 15:36:30 +03:00
|
|
|
$tableForeignKey['delete_rule'] = null;
|
|
|
|
}
|
2010-02-26 04:26:42 +03:00
|
|
|
if (!isset($tableForeignKey['update_rule']) || $tableForeignKey['update_rule'] == "RESTRICT") {
|
2010-02-07 15:36:30 +03:00
|
|
|
$tableForeignKey['update_rule'] = null;
|
|
|
|
}
|
2009-11-30 22:02:05 +03:00
|
|
|
|
2009-12-05 01:41:09 +03:00
|
|
|
return new ForeignKeyConstraint(
|
2009-11-30 22:02:05 +03:00
|
|
|
(array)$tableForeignKey['column_name'],
|
|
|
|
$tableForeignKey['referenced_table_name'],
|
|
|
|
(array)$tableForeignKey['referenced_column_name'],
|
|
|
|
$tableForeignKey['constraint_name'],
|
2010-02-07 15:36:30 +03:00
|
|
|
array(
|
|
|
|
'onUpdate' => $tableForeignKey['update_rule'],
|
|
|
|
'onDelete' => $tableForeignKey['delete_rule'],
|
|
|
|
)
|
2009-05-29 01:34:35 +04:00
|
|
|
);
|
2008-08-09 13:45:28 +04:00
|
|
|
}
|
2009-02-20 08:46:20 +03:00
|
|
|
}
|