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
|
|
|
|
|
|
|
/**
|
|
|
|
* xxx
|
|
|
|
*
|
|
|
|
* @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)
|
|
|
|
* @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)
|
|
|
|
{
|
2009-08-17 21:58:16 +04:00
|
|
|
return $view['TABLE_NAME'];
|
2009-05-28 06:04:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function _getPortableTableDefinition($table)
|
|
|
|
{
|
|
|
|
return end($table);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _getPortableTableIndexDefinition($tableIndex)
|
|
|
|
{
|
|
|
|
$tableIndex = array_change_key_case($tableIndex, CASE_LOWER);
|
|
|
|
|
|
|
|
$result = array();
|
|
|
|
if ($tableIndex['key_name'] != 'PRIMARY' && ($index = $tableIndex['key_name'])) {
|
|
|
|
$result['name'] = $index;
|
|
|
|
$result['column'] = $tableIndex['column_name'];
|
|
|
|
$result['unique'] = $tableIndex['non_unique'] ? false : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _getPortableTableConstraintDefinition($tableConstraint)
|
|
|
|
{
|
|
|
|
$tableConstraint = array_change_key_case($tableConstraint, CASE_LOWER);
|
|
|
|
|
|
|
|
if ( ! $tableConstraint['non_unique']) {
|
|
|
|
$index = $tableConstraint['key_name'];
|
|
|
|
if ( ! empty($index)) {
|
2009-05-29 01:34:35 +04:00
|
|
|
return $index;
|
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-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'] = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$values = null;
|
|
|
|
$scale = null;
|
|
|
|
|
|
|
|
switch ($dbType) {
|
|
|
|
case 'tinyint':
|
|
|
|
$type = 'integer';
|
|
|
|
$type = 'boolean';
|
|
|
|
if (preg_match('/^(is|has)/', $tableColumn['name'])) {
|
|
|
|
$type = array_reverse($type);
|
|
|
|
}
|
2009-08-17 21:58:16 +04:00
|
|
|
$unsigned = preg_match('/ unsigned/i', $tableColumn['Type']);
|
2009-05-28 02:14:27 +04:00
|
|
|
$length = 1;
|
|
|
|
break;
|
|
|
|
case 'smallint':
|
|
|
|
$type = 'integer';
|
2009-08-17 21:58:16 +04:00
|
|
|
$unsigned = preg_match('/ unsigned/i', $tableColumn['Type']);
|
2009-05-28 02:14:27 +04:00
|
|
|
$length = 2;
|
|
|
|
break;
|
|
|
|
case 'mediumint':
|
|
|
|
$type = 'integer';
|
2009-08-17 21:58:16 +04:00
|
|
|
$unsigned = preg_match('/ unsigned/i', $tableColumn['Type']);
|
2009-05-28 02:14:27 +04:00
|
|
|
$length = 3;
|
|
|
|
break;
|
|
|
|
case 'int':
|
|
|
|
case 'integer':
|
|
|
|
$type = 'integer';
|
2009-08-17 21:58:16 +04:00
|
|
|
$unsigned = preg_match('/ unsigned/i', $tableColumn['Type']);
|
2009-05-28 02:14:27 +04:00
|
|
|
$length = 4;
|
|
|
|
break;
|
|
|
|
case 'bigint':
|
|
|
|
$type = 'integer';
|
2009-08-17 21:58:16 +04:00
|
|
|
$unsigned = preg_match('/ unsigned/i', $tableColumn['Type']);
|
2009-05-28 02:14:27 +04:00
|
|
|
$length = 8;
|
|
|
|
break;
|
|
|
|
case 'tinytext':
|
|
|
|
case 'mediumtext':
|
|
|
|
case 'longtext':
|
|
|
|
case 'text':
|
|
|
|
case 'text':
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
} elseif (strstr($dbType, 'text')) {
|
|
|
|
$type = 'clob';
|
|
|
|
if ($decimal == 'binary') {
|
|
|
|
$type = 'blob';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($fixed !== false) {
|
|
|
|
$fixed = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'set':
|
|
|
|
$fixed = false;
|
|
|
|
$type = 'text';
|
|
|
|
$type = 'integer';
|
|
|
|
break;
|
|
|
|
case 'date':
|
|
|
|
$type = 'date';
|
|
|
|
$length = null;
|
|
|
|
break;
|
|
|
|
case 'datetime':
|
|
|
|
case 'timestamp':
|
|
|
|
$type = 'timestamp';
|
|
|
|
$length = null;
|
|
|
|
break;
|
|
|
|
case 'time':
|
|
|
|
$type = 'time';
|
|
|
|
$length = null;
|
|
|
|
break;
|
|
|
|
case 'float':
|
|
|
|
case 'double':
|
|
|
|
case 'real':
|
|
|
|
$type = 'float';
|
|
|
|
$unsigned = preg_match('/ unsigned/i', $tableColumn['type']);
|
|
|
|
break;
|
|
|
|
case 'unknown':
|
|
|
|
case 'decimal':
|
|
|
|
if ($decimal !== null) {
|
|
|
|
$scale = $decimal;
|
|
|
|
}
|
|
|
|
case 'numeric':
|
|
|
|
$type = 'decimal';
|
|
|
|
$unsigned = preg_match('/ unsigned/i', $tableColumn['type']);
|
|
|
|
break;
|
|
|
|
case 'tinyblob':
|
|
|
|
case 'mediumblob':
|
|
|
|
case 'longblob':
|
|
|
|
case 'blob':
|
|
|
|
case 'binary':
|
|
|
|
case 'varbinary':
|
|
|
|
$type = 'blob';
|
|
|
|
$length = null;
|
|
|
|
break;
|
|
|
|
case 'year':
|
|
|
|
$type = 'integer';
|
|
|
|
$type = 'date';
|
|
|
|
$length = null;
|
|
|
|
break;
|
|
|
|
case 'bit':
|
|
|
|
$type = 'bit';
|
|
|
|
break;
|
|
|
|
case 'geometry':
|
|
|
|
case 'geometrycollection':
|
|
|
|
case 'point':
|
|
|
|
case 'multipoint':
|
|
|
|
case 'linestring':
|
|
|
|
case 'multilinestring':
|
|
|
|
case 'polygon':
|
|
|
|
case 'multipolygon':
|
|
|
|
$type = 'blob';
|
|
|
|
$length = null;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$type = 'string';
|
|
|
|
$length = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$length = ((int) $length == 0) ? null : (int) $length;
|
|
|
|
$def = array(
|
|
|
|
'type' => $type,
|
|
|
|
'length' => $length,
|
|
|
|
'unsigned' => (bool) $unsigned,
|
|
|
|
'fixed' => (bool) $fixed
|
|
|
|
);
|
|
|
|
if ($values !== null) {
|
|
|
|
$def['values'] = $values;
|
|
|
|
}
|
|
|
|
if ($scale !== null) {
|
|
|
|
$def['scale'] = $scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
$values = isset($def['values']) ? $def['values'] : array();
|
|
|
|
|
|
|
|
$column = array(
|
2009-08-17 21:58:16 +04:00
|
|
|
'name' => $tableColumn['Field'],
|
2009-05-28 02:14:27 +04:00
|
|
|
'values' => $values,
|
2009-08-17 21:58:16 +04:00
|
|
|
'primary' => (bool) (strtolower($tableColumn['Key']) == 'pri'),
|
|
|
|
'default' => $tableColumn['Default'],
|
|
|
|
'notnull' => (bool) ($tableColumn['Null'] != 'YES'),
|
|
|
|
'autoincrement' => (bool) (strpos($tableColumn['Extra'], 'auto_increment') !== false),
|
2009-05-28 02:14:27 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
$column = array_merge($column, $def);
|
|
|
|
|
|
|
|
return $column;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
$foreignKey = array(
|
|
|
|
'table' => $tableForeignKey['referenced_table_name'],
|
|
|
|
'local' => $tableForeignKey['column_name'],
|
|
|
|
'foreign' => $tableForeignKey['referenced_column_name']
|
|
|
|
);
|
|
|
|
return $foreignKey;
|
2008-08-09 13:45:28 +04:00
|
|
|
}
|
2009-05-30 22:38:00 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function createSequence($sequenceName, $start = 1, $allocationSize = 1)
|
2008-08-09 13:45:28 +04:00
|
|
|
{
|
2009-05-30 22:38:00 +04:00
|
|
|
$seqColumnName = 'mysql_sequence';
|
2008-08-09 13:45:28 +04:00
|
|
|
|
2009-05-30 22:38:00 +04:00
|
|
|
/* No support for options yet. Might add 4th options parameter later
|
2008-08-09 13:45:28 +04:00
|
|
|
$optionsStrings = array();
|
2009-05-30 22:38:00 +04:00
|
|
|
|
2008-08-09 13:45:28 +04:00
|
|
|
if (isset($options['comment']) && ! empty($options['comment'])) {
|
|
|
|
$optionsStrings['comment'] = 'COMMENT = ' . $this->_conn->quote($options['comment'], 'string');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($options['charset']) && ! empty($options['charset'])) {
|
|
|
|
$optionsStrings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset'];
|
|
|
|
|
|
|
|
if (isset($options['collate'])) {
|
|
|
|
$optionsStrings['collate'] .= ' COLLATE ' . $options['collate'];
|
|
|
|
}
|
|
|
|
}
|
2009-05-30 22:38:00 +04:00
|
|
|
|
2008-08-09 13:45:28 +04:00
|
|
|
$type = false;
|
|
|
|
|
|
|
|
if (isset($options['type'])) {
|
|
|
|
$type = $options['type'];
|
|
|
|
} else {
|
|
|
|
$type = $this->_conn->default_table_type;
|
|
|
|
}
|
|
|
|
if ($type) {
|
|
|
|
$optionsStrings[] = 'ENGINE = ' . $type;
|
2009-05-30 22:38:00 +04:00
|
|
|
}*/
|
2008-08-09 13:45:28 +04:00
|
|
|
|
|
|
|
try {
|
|
|
|
$query = 'CREATE TABLE ' . $sequenceName
|
|
|
|
. ' (' . $seqcolName . ' INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ('
|
|
|
|
. $seqcolName . '))';
|
|
|
|
|
2009-05-30 22:38:00 +04:00
|
|
|
/*if (!empty($options_strings)) {
|
2008-08-09 13:45:28 +04:00
|
|
|
$query .= ' '.implode(' ', $options_strings);
|
2009-05-30 22:38:00 +04:00
|
|
|
}*/
|
|
|
|
$query .= ' ENGINE = INNODB';
|
2008-08-09 13:45:28 +04:00
|
|
|
|
2009-05-30 22:38:00 +04:00
|
|
|
$res = $this->_conn->exec($query);
|
2009-02-20 08:46:20 +03:00
|
|
|
} catch(Doctrine\DBAL\ConnectionException $e) {
|
2009-08-27 02:03:39 +04:00
|
|
|
throw \Doctrine\Common\DoctrineException::createSequenceFailed($query);
|
2008-08-09 13:45:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($start == 1) {
|
2009-05-30 22:38:00 +04:00
|
|
|
return;
|
|
|
|
}
|
2008-08-09 13:45:28 +04:00
|
|
|
|
2009-05-30 22:38:00 +04:00
|
|
|
$query = 'INSERT INTO ' . $sequenceName
|
2008-08-09 13:45:28 +04:00
|
|
|
. ' (' . $seqcolName . ') VALUES (' . ($start - 1) . ')';
|
|
|
|
|
2009-05-30 22:38:00 +04:00
|
|
|
$res = $this->_conn->exec($query);
|
2008-08-09 13:45:28 +04:00
|
|
|
|
2009-05-30 22:38:00 +04:00
|
|
|
// Handle error
|
|
|
|
try {
|
|
|
|
$res = $this->_conn->exec('DROP TABLE ' . $sequenceName);
|
|
|
|
} catch (\Exception $e) {
|
2009-08-27 02:03:39 +04:00
|
|
|
throw \Doctrine\Common\DoctrineException::couldNotDropSequenceTable($sequenceName);
|
2009-05-30 22:38:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $res;
|
2008-08-09 13:45:28 +04:00
|
|
|
}
|
2009-02-20 08:46:20 +03:00
|
|
|
}
|