. */ namespace Doctrine\DBAL\Schema; /** * xxx * * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @author Konsta Vesterinen * @author Lukas Smith (PEAR MDB2 library) * @version $Revision$ * @since 2.0 */ class MySqlSchemaManager extends AbstractSchemaManager { protected function _getPortableTableColumnDefinition($tableColumn) { $dbType = strtolower($tableColumn['type']); $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); } $unsigned = preg_match('/ unsigned/i', $tableColumn['type']); $length = 1; break; case 'smallint': $type = 'integer'; $unsigned = preg_match('/ unsigned/i', $tableColumn['type']); $length = 2; break; case 'mediumint': $type = 'integer'; $unsigned = preg_match('/ unsigned/i', $tableColumn['type']); $length = 3; break; case 'int': case 'integer': $type = 'integer'; $unsigned = preg_match('/ unsigned/i', $tableColumn['type']); $length = 4; break; case 'bigint': $type = 'integer'; $unsigned = preg_match('/ unsigned/i', $tableColumn['type']); $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 'enum': $type = 'enum'; preg_match_all('/\'((?:\'\'|[^\'])*)\'/', $tableColumn['type'], $matches); $length = 0; $fixed = false; if (is_array($matches)) { foreach ($matches[1] as &$value) { $value = str_replace('\'\'', '\'', $value); $length = max($length, strlen($value)); } if ($length == '1' && count($matches[1]) == 2) { $type = 'boolean'; if (preg_match('/^(is|has)/', $tableColumn['name'])) { $type = array_reverse($type); } } $values = $matches[1]; } $type = 'integer'; 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(); $def['type'] = \Doctrine\DBAL\Types\Type::getType($def['type']); $column = array( 'name' => $tableColumn['field'], 'values' => $values, 'primary' => (bool) (strtolower($tableColumn['key']) == 'pri'), 'default' => $tableColumn['default'], 'notnull' => (bool) ($tableColumn['null'] != 'YES'), 'autoincrement' => (bool) (strpos($tableColumn['extra'], 'auto_increment') !== false), ); $column = array_merge($column, $def); return $column; } /** * lists all database sequences * * @param string|null $database * @return array * @override */ public function listSequences($database = null) { $query = 'SHOW TABLES'; if ( ! is_null($database)) { $query .= ' FROM ' . $database; } $tableNames = $this->_conn->fetchColumn($query); return array_map(array($this->_conn->formatter, 'fixSequenceName'), $tableNames); } /** * lists table constraints * * @param string $table database table name * @return array * @override */ public function listTableConstraints($table) { $keyName = 'Key_name'; $nonUnique = 'Non_unique'; if ($this->_conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_FIX_CASE) { if ($this->_conn->getAttribute(Doctrine::ATTR_FIELD_CASE) == CASE_LOWER) { $keyName = strtolower($keyName); $nonUnique = strtolower($nonUnique); } else { $keyName = strtoupper($keyName); $nonUnique = strtoupper($nonUnique); } } $table = $this->_conn->quoteIdentifier($table, true); $query = 'SHOW INDEX FROM ' . $table; $indexes = $this->_conn->fetchAssoc($query); $result = array(); foreach ($indexes as $indexData) { if ( ! $indexData[$nonUnique]) { if ($indexData[$keyName] !== 'PRIMARY') { $index = $this->_conn->formatter->fixIndexName($indexData[$keyName]); } else { $index = 'PRIMARY'; } if ( ! empty($index)) { $result[] = $index; } } } return $result; } /** * lists table foreign keys * * @param string $table database table name * @return array * @override */ public function listTableForeignKeys($table) { $sql = 'SHOW CREATE TABLE ' . $this->_conn->quoteIdentifier($table, true); $definition = $this->_conn->fetchOne($sql); if (!empty($definition)) { $pattern = '/\bCONSTRAINT\s+([^\s]+)\s+FOREIGN KEY\b/i'; if (preg_match_all($pattern, str_replace('`', '', $definition), $matches) > 1) { foreach ($matches[1] as $constraint) { $result[$constraint] = true; } } } if ($this->_conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_FIX_CASE) { $result = array_change_key_case($result, $this->_conn->getAttribute(Doctrine::ATTR_FIELD_CASE)); } return $result; } /** * lists table constraints * * @param string $table database table name * @return array * @override */ public function listTableIndexes($table) { $keyName = 'Key_name'; $nonUnique = 'Non_unique'; if ($this->_conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_FIX_CASE) { if ($this->_conn->getAttribute(Doctrine::ATTR_FIELD_CASE) == CASE_LOWER) { $keyName = strtolower($keyName); $nonUnique = strtolower($nonUnique); } else { $keyName = strtoupper($keyName); $nonUnique = strtoupper($nonUnique); } } $table = $this->_conn->quoteIdentifier($table, true); $query = 'SHOW INDEX FROM ' . $table; $indexes = $this->_conn->fetchAssoc($query); $result = array(); foreach ($indexes as $indexData) { if ($indexData[$nonUnique] && ($index = $this->_conn->formatter->fixIndexName($indexData[$keyName]))) { $result[] = $index; } } return $result; } /** * lists tables * * @param string|null $database * @return array * @override */ public function listTables($database = null) { return $this->_conn->fetchColumn($this->_conn->getDatabasePlatform() ->getListTablesSql()); } /** * lists database views * * @param string|null $database * @return array * @override */ public function listViews($database = null) { if ( ! is_null($database)) { $query = sprintf($this->sql['listViews'], ' FROM ' . $database); } return $this->_conn->fetchColumn($query); } /** * create sequence * * @param string $sequenceName name of the sequence to be created * @param string $start start value of the sequence; default is 1 * @param array $options An associative array of table options: * array( * 'comment' => 'Foo', * 'charset' => 'utf8', * 'collate' => 'utf8_unicode_ci', * 'type' => 'innodb', * ); * @return boolean * @override */ public function createSequence($sequenceName, $start = 1, array $options = array()) { $sequenceName = $this->_conn->quoteIdentifier($this->_conn->getSequenceName($sequenceName), true); $seqcolName = $this->_conn->quoteIdentifier($this->_conn->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true); $optionsStrings = array(); 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']; } } $type = false; if (isset($options['type'])) { $type = $options['type']; } else { $type = $this->_conn->default_table_type; } if ($type) { $optionsStrings[] = 'ENGINE = ' . $type; } try { $query = 'CREATE TABLE ' . $sequenceName . ' (' . $seqcolName . ' INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (' . $seqcolName . '))'; if (!empty($options_strings)) { $query .= ' '.implode(' ', $options_strings); } $res = $this->_conn->exec($query); } catch(Doctrine\DBAL\ConnectionException $e) { throw \Doctrine\Common\DoctrineException::updateMe('could not create sequence table'); } if ($start == 1) { return true; } $query = 'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (' . ($start - 1) . ')'; $res = $this->_conn->exec($query); // Handle error try { $res = $this->_conn->exec('DROP TABLE ' . $sequenceName); } catch(Doctrine\DBAL\ConnectionException $e) { throw \Doctrine\Common\DoctrineException::updateMe('could not drop inconsistent sequence table'); } return $res; } /** * Enter description here... * * @param unknown_type $table * @param unknown_type $name * @return unknown * @override */ public function dropForeignKey($table, $name) { $table = $this->_conn->quoteIdentifier($table); $name = $this->_conn->quoteIdentifier($name); return $this->_conn->exec('ALTER TABLE ' . $table . ' DROP FOREIGN KEY ' . $name); } }