2008-08-01 22:46:14 +04:00
|
|
|
<?php
|
2008-08-16 23:40:59 +04:00
|
|
|
/*
|
|
|
|
* $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-03-28 20:10:41 +03:00
|
|
|
* <http://www.doctrine-project.org>.
|
2008-08-16 23:40:59 +04:00
|
|
|
*/
|
2008-08-01 22:46:14 +04:00
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\DBAL\Platforms;
|
2008-09-12 12:51:56 +04:00
|
|
|
|
2009-12-06 12:47:54 +03:00
|
|
|
use Doctrine\Common\DoctrineException,
|
|
|
|
Doctrine\DBAL\Schema\TableDiff;
|
2009-03-28 23:59:07 +03:00
|
|
|
|
2008-08-01 22:46:14 +04:00
|
|
|
/**
|
|
|
|
* The MySqlPlatform provides the behavior, features and SQL dialect of the
|
2009-08-28 16:36:06 +04:00
|
|
|
* MySQL database platform. This platform represents a MySQL 5.0 or greater platform that
|
|
|
|
* uses the InnoDB storage engine.
|
2008-08-01 22:46:14 +04:00
|
|
|
*
|
|
|
|
* @since 2.0
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2010-02-11 17:29:12 +03:00
|
|
|
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
2008-08-01 22:46:14 +04:00
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
class MySqlPlatform extends AbstractPlatform
|
2010-02-11 17:29:12 +03:00
|
|
|
{
|
2008-08-02 21:41:37 +04:00
|
|
|
/**
|
|
|
|
* Gets the character used for identifier quoting.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function getIdentifierQuoteCharacter()
|
|
|
|
{
|
|
|
|
return '`';
|
2008-08-01 22:46:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-08-02 21:41:37 +04:00
|
|
|
* Returns the regular expression operator.
|
2008-08-01 22:46:14 +04:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function getRegexpExpression()
|
|
|
|
{
|
|
|
|
return 'RLIKE';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns global unique identifier
|
|
|
|
*
|
|
|
|
* @return string to get global unique identifier
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function getGuidExpression()
|
|
|
|
{
|
|
|
|
return 'UUID()';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a series of strings concatinated
|
|
|
|
*
|
|
|
|
* concat() accepts an arbitrary number of parameters. Each parameter
|
|
|
|
* must contain an expression or an array with expressions.
|
|
|
|
*
|
|
|
|
* @param string|array(string) strings that will be concatinated.
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function getConcatExpression()
|
|
|
|
{
|
|
|
|
$args = func_get_args();
|
|
|
|
return 'CONCAT(' . join(', ', (array) $args) . ')';
|
|
|
|
}
|
2009-05-28 06:04:51 +04:00
|
|
|
|
|
|
|
public function getListDatabasesSql()
|
|
|
|
{
|
|
|
|
return 'SHOW DATABASES';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getListTableConstraintsSql($table)
|
|
|
|
{
|
2009-08-11 14:51:38 +04:00
|
|
|
return 'SHOW INDEX FROM ' . $table;
|
2009-05-28 06:04:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getListTableIndexesSql($table)
|
|
|
|
{
|
2009-08-11 14:51:38 +04:00
|
|
|
return 'SHOW INDEX FROM ' . $table;
|
2009-05-28 06:04:51 +04:00
|
|
|
}
|
|
|
|
|
2010-02-11 02:41:35 +03:00
|
|
|
public function getListViewsSql($database)
|
2009-05-28 06:04:51 +04:00
|
|
|
{
|
2010-02-11 02:41:35 +03:00
|
|
|
return "SELECT * FROM information_schema.VIEWS WHERE TABLE_SCHEMA = '".$database."'";
|
2009-05-28 06:04:51 +04:00
|
|
|
}
|
|
|
|
|
2009-05-29 01:34:35 +04:00
|
|
|
public function getListTableForeignKeysSql($table, $database = null)
|
|
|
|
{
|
2010-02-07 15:36:30 +03:00
|
|
|
$sql = "SELECT DISTINCT k.`CONSTRAINT_NAME`, k.`COLUMN_NAME`, k.`REFERENCED_TABLE_NAME`, ".
|
|
|
|
"k.`REFERENCED_COLUMN_NAME` /*!50116 , c.update_rule, c.delete_rule */ ".
|
|
|
|
"FROM information_schema.key_column_usage k /*!50116 ".
|
|
|
|
"INNER JOIN information_schema.referential_constraints c ON k.`CONSTRAINT_NAME` = c.constraint_name AND ".
|
|
|
|
" c.constraint_name = k.constraint_name AND ".
|
2010-02-09 21:48:09 +03:00
|
|
|
" c.table_name = '$table' */ WHERE k.table_name = '$table'";
|
2009-05-29 01:34:35 +04:00
|
|
|
|
|
|
|
if ( ! is_null($database)) {
|
2009-05-30 14:16:54 +04:00
|
|
|
$sql .= " AND table_schema = '$database'";
|
2009-05-29 01:34:35 +04:00
|
|
|
}
|
|
|
|
|
2009-11-30 22:02:05 +03:00
|
|
|
$sql .= " AND `REFERENCED_COLUMN_NAME` is not NULL";
|
2009-05-29 01:34:35 +04:00
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
2009-05-28 06:04:51 +04:00
|
|
|
public function getCreateViewSql($name, $sql)
|
|
|
|
{
|
|
|
|
return 'CREATE VIEW ' . $name . ' AS ' . $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDropViewSql($name)
|
|
|
|
{
|
|
|
|
return 'DROP VIEW '. $name;
|
|
|
|
}
|
|
|
|
|
2008-08-22 13:05:14 +04:00
|
|
|
/**
|
2009-01-07 20:46:02 +03:00
|
|
|
* Gets the SQL snippet used to declare a VARCHAR column on the MySql platform.
|
|
|
|
*
|
|
|
|
* @params array $field
|
2008-08-22 13:05:14 +04:00
|
|
|
*/
|
2009-05-03 22:07:57 +04:00
|
|
|
public function getVarcharTypeDeclarationSql(array $field)
|
2008-08-22 13:05:14 +04:00
|
|
|
{
|
|
|
|
if ( ! isset($field['length'])) {
|
|
|
|
if (array_key_exists('default', $field)) {
|
|
|
|
$field['length'] = $this->getVarcharMaxLength();
|
|
|
|
} else {
|
|
|
|
$field['length'] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$length = ($field['length'] <= $this->getVarcharMaxLength()) ? $field['length'] : false;
|
2009-01-07 20:46:02 +03:00
|
|
|
$fixed = (isset($field['fixed'])) ? $field['fixed'] : false;
|
2008-08-22 13:05:14 +04:00
|
|
|
|
|
|
|
return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
|
2009-08-21 22:13:22 +04:00
|
|
|
: ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)');
|
2008-08-22 13:05:14 +04:00
|
|
|
}
|
2009-05-29 01:34:35 +04:00
|
|
|
|
2009-10-01 16:00:14 +04:00
|
|
|
/** @override */
|
|
|
|
public function getClobTypeDeclarationSql(array $field)
|
2008-08-31 22:27:16 +04:00
|
|
|
{
|
|
|
|
if ( ! empty($field['length'])) {
|
|
|
|
$length = $field['length'];
|
|
|
|
if ($length <= 255) {
|
|
|
|
return 'TINYTEXT';
|
|
|
|
} else if ($length <= 65532) {
|
|
|
|
return 'TEXT';
|
|
|
|
} else if ($length <= 16777215) {
|
|
|
|
return 'MEDIUMTEXT';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 'LONGTEXT';
|
|
|
|
}
|
2008-08-01 22:46:14 +04:00
|
|
|
|
2009-06-20 16:59:33 +04:00
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function getDateTimeTypeDeclarationSql(array $fieldDeclaration)
|
|
|
|
{
|
2009-11-05 21:55:02 +03:00
|
|
|
if (isset($fieldDeclaration['version']) && $fieldDeclaration['version'] == true) {
|
2009-09-08 21:09:32 +04:00
|
|
|
return 'TIMESTAMP';
|
|
|
|
} else {
|
|
|
|
return 'DATETIME';
|
|
|
|
}
|
2009-06-20 16:59:33 +04:00
|
|
|
}
|
2009-08-28 21:25:28 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function getDateTypeDeclarationSql(array $fieldDeclaration)
|
|
|
|
{
|
|
|
|
return 'DATE';
|
|
|
|
}
|
2009-06-20 16:59:33 +04:00
|
|
|
|
2009-11-03 19:56:05 +03:00
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function getTimeTypeDeclarationSql(array $fieldDeclaration)
|
|
|
|
{
|
|
|
|
return 'TIME';
|
|
|
|
}
|
|
|
|
|
2009-06-20 19:09:59 +04:00
|
|
|
/**
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function getBooleanTypeDeclarationSql(array $field)
|
|
|
|
{
|
|
|
|
return 'TINYINT(1)';
|
|
|
|
}
|
|
|
|
|
2008-08-01 22:46:14 +04:00
|
|
|
/**
|
|
|
|
* Obtain DBMS specific SQL code portion needed to set the COLLATION
|
|
|
|
* of a field declaration to be used in statements like CREATE TABLE.
|
|
|
|
*
|
|
|
|
* @param string $collation name of the collation
|
|
|
|
* @return string DBMS specific SQL code portion needed to set the COLLATION
|
|
|
|
* of a field declaration.
|
|
|
|
*/
|
|
|
|
public function getCollationFieldDeclaration($collation)
|
|
|
|
{
|
|
|
|
return 'COLLATE ' . $collation;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the platform prefers identity columns for ID generation.
|
|
|
|
* MySql prefers "autoincrement" identity columns since sequences can only
|
|
|
|
* be emulated with a table.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function prefersIdentityColumns()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2008-08-02 21:41:37 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the platform supports identity columns.
|
|
|
|
* MySql supports this through AUTO_INCREMENT columns.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function supportsIdentityColumns()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the platform supports savepoints. MySql does not.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function supportsSavepoints()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-29 01:34:35 +04:00
|
|
|
|
2008-08-31 22:27:16 +04:00
|
|
|
public function getShowDatabasesSql()
|
|
|
|
{
|
|
|
|
return 'SHOW DATABASES';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getListTablesSql()
|
|
|
|
{
|
2009-12-01 02:12:26 +03:00
|
|
|
return 'SHOW FULL TABLES WHERE Table_type = "BASE TABLE"';
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
2009-05-28 02:14:27 +04:00
|
|
|
|
|
|
|
public function getListTableColumnsSql($table)
|
|
|
|
{
|
2009-08-11 14:51:38 +04:00
|
|
|
return 'DESCRIBE ' . $table;
|
2009-05-28 02:14:27 +04:00
|
|
|
}
|
|
|
|
|
2008-08-31 22:27:16 +04:00
|
|
|
/**
|
|
|
|
* create a new database
|
|
|
|
*
|
|
|
|
* @param string $name name of the database that should be created
|
|
|
|
* @return string
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function getCreateDatabaseSql($name)
|
|
|
|
{
|
2009-08-11 14:51:38 +04:00
|
|
|
return 'CREATE DATABASE ' . $name;
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* drop an existing database
|
|
|
|
*
|
|
|
|
* @param string $name name of the database that should be dropped
|
|
|
|
* @return string
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function getDropDatabaseSql($name)
|
|
|
|
{
|
2009-08-11 14:51:38 +04:00
|
|
|
return 'DROP DATABASE ' . $name;
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create a new table
|
|
|
|
*
|
2009-12-02 21:52:21 +03:00
|
|
|
* @param string $tableName Name of the database that should be created
|
|
|
|
* @param array $columns Associative array that contains the definition of each field of the new table
|
2008-08-31 22:27:16 +04:00
|
|
|
* The indexes of the array entries are the names of the fields of the table an
|
|
|
|
* the array entry values are associative arrays like those that are meant to be
|
|
|
|
* passed with the field definitions to get[Type]Declaration() functions.
|
|
|
|
* array(
|
|
|
|
* 'id' => array(
|
|
|
|
* 'type' => 'integer',
|
|
|
|
* 'unsigned' => 1
|
|
|
|
* 'notnull' => 1
|
|
|
|
* 'default' => 0
|
|
|
|
* ),
|
|
|
|
* 'name' => array(
|
|
|
|
* 'type' => 'text',
|
|
|
|
* 'length' => 12
|
|
|
|
* ),
|
|
|
|
* 'password' => array(
|
|
|
|
* 'type' => 'text',
|
|
|
|
* 'length' => 12
|
|
|
|
* )
|
|
|
|
* );
|
|
|
|
* @param array $options An associative array of table options:
|
|
|
|
* array(
|
|
|
|
* 'comment' => 'Foo',
|
|
|
|
* 'charset' => 'utf8',
|
|
|
|
* 'collate' => 'utf8_unicode_ci',
|
|
|
|
* 'type' => 'innodb',
|
|
|
|
* );
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @override
|
|
|
|
*/
|
2009-12-02 21:52:21 +03:00
|
|
|
protected function _getCreateTableSql($tableName, array $columns, array $options = array())
|
2008-08-31 22:27:16 +04:00
|
|
|
{
|
2009-12-02 21:52:21 +03:00
|
|
|
if ( ! $tableName) {
|
2009-08-27 02:03:39 +04:00
|
|
|
throw DoctrineException::missingTableName();
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
2009-12-02 21:52:21 +03:00
|
|
|
if (empty($columns)) {
|
|
|
|
throw DoctrineException::missingFieldsArrayForTable($tableName);
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
2009-12-02 21:52:21 +03:00
|
|
|
$queryFields = $this->getColumnDeclarationListSql($columns);
|
2008-08-31 22:27:16 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
|
2010-01-06 16:23:56 +03:00
|
|
|
foreach ($options['uniqueConstraints'] as $index => $definition) {
|
2010-01-06 16:27:09 +03:00
|
|
|
$queryFields .= ', ' . $this->getUniqueConstraintDeclarationSql($index, $definition);
|
2009-08-21 22:13:22 +04:00
|
|
|
}
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// add all indexes
|
|
|
|
if (isset($options['indexes']) && ! empty($options['indexes'])) {
|
|
|
|
foreach($options['indexes'] as $index => $definition) {
|
|
|
|
$queryFields .= ', ' . $this->getIndexDeclarationSql($index, $definition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// attach all primary keys
|
|
|
|
if (isset($options['primary']) && ! empty($options['primary'])) {
|
2009-05-03 14:58:16 +04:00
|
|
|
$keyColumns = array_unique(array_values($options['primary']));
|
2008-08-31 22:27:16 +04:00
|
|
|
$queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = 'CREATE ';
|
|
|
|
if (!empty($options['temporary'])) {
|
|
|
|
$query .= 'TEMPORARY ';
|
|
|
|
}
|
2009-12-02 21:52:21 +03:00
|
|
|
$query.= 'TABLE ' . $tableName . ' (' . $queryFields . ')';
|
2008-08-31 22:27:16 +04:00
|
|
|
|
|
|
|
$optionStrings = array();
|
|
|
|
|
|
|
|
if (isset($options['comment'])) {
|
|
|
|
$optionStrings['comment'] = 'COMMENT = ' . $this->quote($options['comment'], 'text');
|
|
|
|
}
|
|
|
|
if (isset($options['charset'])) {
|
|
|
|
$optionStrings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset'];
|
|
|
|
if (isset($options['collate'])) {
|
|
|
|
$optionStrings['charset'] .= ' COLLATE ' . $options['collate'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the type of the table
|
2009-08-28 16:36:06 +04:00
|
|
|
if (isset($options['engine'])) {
|
|
|
|
$optionStrings[] = 'ENGINE = ' . $engine;
|
|
|
|
} else {
|
|
|
|
// default to innodb
|
|
|
|
$optionStrings[] = 'ENGINE = InnoDB';
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
2009-08-28 16:36:06 +04:00
|
|
|
|
2008-08-31 22:27:16 +04:00
|
|
|
if ( ! empty($optionStrings)) {
|
|
|
|
$query.= ' '.implode(' ', $optionStrings);
|
|
|
|
}
|
|
|
|
$sql[] = $query;
|
|
|
|
|
|
|
|
if (isset($options['foreignKeys'])) {
|
2009-12-02 21:52:21 +03:00
|
|
|
foreach ((array) $options['foreignKeys'] as $definition) {
|
|
|
|
$sql[] = $this->getCreateForeignKeySql($definition, $tableName);
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-05-03 22:07:57 +04:00
|
|
|
* Gets the SQL to alter an existing table.
|
2008-08-31 22:27:16 +04:00
|
|
|
*
|
2009-05-03 22:07:57 +04:00
|
|
|
* @param string $name The name of the table that is intended to be changed.
|
|
|
|
* @param array $changes Associative array that contains the details of each type
|
2008-08-31 22:27:16 +04:00
|
|
|
* of change that is intended to be performed. The types of
|
|
|
|
* changes that are currently supported are defined as follows:
|
|
|
|
*
|
|
|
|
* name
|
|
|
|
*
|
|
|
|
* New name for the table.
|
|
|
|
*
|
|
|
|
* add
|
|
|
|
*
|
|
|
|
* Associative array with the names of fields to be added as
|
|
|
|
* indexes of the array. The value of each entry of the array
|
|
|
|
* should be set to another associative array with the properties
|
|
|
|
* of the fields to be added. The properties of the fields should
|
|
|
|
* be the same as defined by the Metabase parser.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* remove
|
|
|
|
*
|
|
|
|
* Associative array with the names of fields to be removed as indexes
|
|
|
|
* of the array. Currently the values assigned to each entry are ignored.
|
|
|
|
* An empty array should be used for future compatibility.
|
|
|
|
*
|
|
|
|
* rename
|
|
|
|
*
|
|
|
|
* Associative array with the names of fields to be renamed as indexes
|
|
|
|
* of the array. The value of each entry of the array should be set to
|
|
|
|
* another associative array with the entry named name with the new
|
|
|
|
* field name and the entry named Declaration that is expected to contain
|
|
|
|
* the portion of the field declaration already in DBMS specific SQL code
|
|
|
|
* as it is used in the CREATE TABLE statement.
|
|
|
|
*
|
|
|
|
* change
|
|
|
|
*
|
|
|
|
* Associative array with the names of the fields to be changed as indexes
|
|
|
|
* of the array. Keep in mind that if it is intended to change either the
|
|
|
|
* name of a field and any other properties, the change array entries
|
|
|
|
* should have the new names of the fields as array indexes.
|
|
|
|
*
|
|
|
|
* The value of each entry of the array should be set to another associative
|
|
|
|
* array with the properties of the fields to that are meant to be changed as
|
|
|
|
* array entries. These entries should be assigned to the new values of the
|
|
|
|
* respective properties. The properties of the fields should be the same
|
|
|
|
* as defined by the Metabase parser.
|
|
|
|
*
|
|
|
|
* Example
|
|
|
|
* array(
|
|
|
|
* 'name' => 'userlist',
|
|
|
|
* 'add' => array(
|
|
|
|
* 'quota' => array(
|
|
|
|
* 'type' => 'integer',
|
|
|
|
* 'unsigned' => 1
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* 'remove' => array(
|
|
|
|
* 'file_limit' => array(),
|
|
|
|
* 'time_limit' => array()
|
|
|
|
* ),
|
|
|
|
* 'change' => array(
|
|
|
|
* 'name' => array(
|
|
|
|
* 'length' => '20',
|
|
|
|
* 'definition' => array(
|
|
|
|
* 'type' => 'text',
|
|
|
|
* 'length' => 20,
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
* ),
|
|
|
|
* 'rename' => array(
|
|
|
|
* 'sex' => array(
|
|
|
|
* 'name' => 'gender',
|
|
|
|
* 'definition' => array(
|
|
|
|
* 'type' => 'text',
|
|
|
|
* 'length' => 1,
|
|
|
|
* 'default' => 'M',
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @param boolean $check indicates whether the function should just check if the DBMS driver
|
|
|
|
* can perform the requested table alterations if the value is true or
|
|
|
|
* actually perform them otherwise.
|
|
|
|
* @return boolean
|
|
|
|
* @override
|
|
|
|
*/
|
2009-12-06 12:47:54 +03:00
|
|
|
public function getAlterTableSql(TableDiff $diff)
|
2008-08-31 22:27:16 +04:00
|
|
|
{
|
2009-12-06 12:47:54 +03:00
|
|
|
$queryParts = array();
|
|
|
|
if ($diff->newName !== false) {
|
|
|
|
$queryParts[] = 'RENAME TO ' . $diff->newName;
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
|
|
|
|
2009-12-06 12:47:54 +03:00
|
|
|
foreach ($diff->addedColumns AS $fieldName => $column) {
|
|
|
|
$queryParts[] = 'ADD ' . $this->getColumnDeclarationSql($column->getName(), $column->toArray());
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
|
|
|
|
2009-12-06 12:47:54 +03:00
|
|
|
foreach ($diff->removedColumns AS $column) {
|
|
|
|
$queryParts[] = 'DROP ' . $column->getName();
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
|
|
|
|
2009-12-06 12:47:54 +03:00
|
|
|
foreach ($diff->changedColumns AS $columnDiff) {
|
|
|
|
/* @var $columnDiff Doctrine\DBAL\Schema\ColumnDiff */
|
|
|
|
$column = $columnDiff->column;
|
|
|
|
$queryParts[] = 'CHANGE ' . ($columnDiff->oldColumnName) . ' '
|
|
|
|
. $this->getColumnDeclarationSql($column->getName(), $column->toArray());
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
|
|
|
|
2009-12-06 12:47:54 +03:00
|
|
|
foreach ($diff->renamedColumns AS $oldColumnName => $column) {
|
|
|
|
$queryParts[] = 'CHANGE ' . $oldColumnName . ' '
|
|
|
|
. $this->getColumnDeclarationSql($column->getName(), $column->toArray());
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
|
|
|
|
2009-12-06 16:00:49 +03:00
|
|
|
$sql = array();
|
2009-12-06 15:13:15 +03:00
|
|
|
if (count($queryParts) > 0) {
|
|
|
|
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . implode(", ", $queryParts);
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
2009-12-06 16:00:49 +03:00
|
|
|
$sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySql($diff));
|
2009-12-06 15:13:15 +03:00
|
|
|
return $sql;
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain DBMS specific SQL code portion needed to declare an integer type
|
|
|
|
* field to be used in statements like CREATE TABLE.
|
|
|
|
*
|
|
|
|
* @param string $name name the field to be declared.
|
|
|
|
* @param string $field associative array with the name of the properties
|
|
|
|
* of the field being declared as array indexes.
|
|
|
|
* Currently, the types of supported field
|
|
|
|
* properties are as follows:
|
|
|
|
*
|
|
|
|
* unsigned
|
|
|
|
* Boolean flag that indicates whether the field
|
|
|
|
* should be declared as unsigned integer if
|
|
|
|
* possible.
|
|
|
|
*
|
|
|
|
* default
|
|
|
|
* Integer value to be used as default for this
|
|
|
|
* field.
|
|
|
|
*
|
|
|
|
* notnull
|
|
|
|
* Boolean flag that indicates whether this field is
|
|
|
|
* constrained to not be set to null.
|
|
|
|
* @return string DBMS specific SQL code portion that should be used to
|
|
|
|
* declare the specified field.
|
|
|
|
* @override
|
|
|
|
*/
|
2009-01-07 20:46:02 +03:00
|
|
|
public function getIntegerTypeDeclarationSql(array $field)
|
|
|
|
{
|
2009-02-17 15:25:03 +03:00
|
|
|
return 'INT' . $this->_getCommonIntegerTypeDeclarationSql($field);
|
2009-01-07 20:46:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @override */
|
|
|
|
public function getBigIntTypeDeclarationSql(array $field)
|
|
|
|
{
|
2009-02-17 15:25:03 +03:00
|
|
|
return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSql($field);
|
2009-01-07 20:46:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @override */
|
2009-01-12 16:34:41 +03:00
|
|
|
public function getSmallIntTypeDeclarationSql(array $field)
|
2009-01-07 20:46:02 +03:00
|
|
|
{
|
2009-02-17 15:25:03 +03:00
|
|
|
return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSql($field);
|
2009-01-07 20:46:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @override */
|
|
|
|
protected function _getCommonIntegerTypeDeclarationSql(array $columnDef)
|
2008-08-31 22:27:16 +04:00
|
|
|
{
|
2009-02-17 15:25:03 +03:00
|
|
|
$autoinc = '';
|
2009-01-12 16:34:41 +03:00
|
|
|
if ( ! empty($columnDef['autoincrement'])) {
|
2008-08-31 22:27:16 +04:00
|
|
|
$autoinc = ' AUTO_INCREMENT';
|
|
|
|
}
|
2009-01-12 16:34:41 +03:00
|
|
|
$unsigned = (isset($columnDef['unsigned']) && $columnDef['unsigned']) ? ' UNSIGNED' : '';
|
2008-08-31 22:27:16 +04:00
|
|
|
|
2009-02-17 15:25:03 +03:00
|
|
|
return $unsigned . $autoinc;
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain DBMS specific SQL code portion needed to set an index
|
|
|
|
* declaration to be used in statements like CREATE TABLE.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function getIndexFieldDeclarationListSql(array $fields)
|
|
|
|
{
|
|
|
|
$declFields = array();
|
|
|
|
|
|
|
|
foreach ($fields as $fieldName => $field) {
|
2009-08-11 14:51:38 +04:00
|
|
|
$fieldString = $fieldName;
|
2008-08-31 22:27:16 +04:00
|
|
|
|
|
|
|
if (is_array($field)) {
|
|
|
|
if (isset($field['length'])) {
|
|
|
|
$fieldString .= '(' . $field['length'] . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($field['sorting'])) {
|
|
|
|
$sort = strtoupper($field['sorting']);
|
|
|
|
switch ($sort) {
|
|
|
|
case 'ASC':
|
|
|
|
case 'DESC':
|
|
|
|
$fieldString .= ' ' . $sort;
|
|
|
|
break;
|
|
|
|
default:
|
2009-08-27 02:03:39 +04:00
|
|
|
throw DoctrineException::unknownIndexSortingOption($sort);
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2009-08-11 14:51:38 +04:00
|
|
|
$fieldString = $field;
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
|
|
|
$declFields[] = $fieldString;
|
|
|
|
}
|
|
|
|
return implode(', ', $declFields);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the FOREIGN KEY query section dealing with non-standard options
|
|
|
|
* as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
|
|
|
|
*
|
2009-12-02 21:52:21 +03:00
|
|
|
* @param ForeignKeyConstraint $foreignKey
|
2008-08-31 22:27:16 +04:00
|
|
|
* @return string
|
|
|
|
* @override
|
|
|
|
*/
|
2009-12-02 21:52:21 +03:00
|
|
|
public function getAdvancedForeignKeyOptionsSql(\Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey)
|
2008-08-31 22:27:16 +04:00
|
|
|
{
|
|
|
|
$query = '';
|
2009-12-02 21:52:21 +03:00
|
|
|
if ($foreignKey->hasOption('match')) {
|
|
|
|
$query .= ' MATCH ' . $foreignKey->getOption('match');
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
2009-12-02 21:52:21 +03:00
|
|
|
$query .= parent::getAdvancedForeignKeyOptionsSql($foreignKey);
|
2008-08-31 22:27:16 +04:00
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-05-03 22:07:57 +04:00
|
|
|
* Gets the SQL to drop an index of a table.
|
2008-08-31 22:27:16 +04:00
|
|
|
*
|
2009-12-02 21:52:21 +03:00
|
|
|
* @param Index $index name of the index to be dropped
|
|
|
|
* @param string|Table $table name of table that should be used in method
|
2008-08-31 22:27:16 +04:00
|
|
|
* @override
|
|
|
|
*/
|
2009-12-02 21:52:21 +03:00
|
|
|
public function getDropIndexSql($index, $table=null)
|
2008-08-31 22:27:16 +04:00
|
|
|
{
|
2009-12-02 21:52:21 +03:00
|
|
|
if($index instanceof \Doctrine\DBAL\Schema\Index) {
|
|
|
|
$index = $index->getName();
|
|
|
|
} else if(!is_string($index)) {
|
|
|
|
throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSql() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if($table instanceof \Doctrine\DBAL\Schema\Table) {
|
|
|
|
$table = $table->getName();
|
|
|
|
} else if(!is_string($table)) {
|
|
|
|
throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSql() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'DROP INDEX ' . $index . ' ON ' . $table;
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-05-03 22:07:57 +04:00
|
|
|
* Gets the SQL to drop a table.
|
2008-08-31 22:27:16 +04:00
|
|
|
*
|
2009-05-03 22:07:57 +04:00
|
|
|
* @param string $table The name of table to drop.
|
2008-08-31 22:27:16 +04:00
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function getDropTableSql($table)
|
|
|
|
{
|
2009-12-06 21:36:46 +03:00
|
|
|
if ($table instanceof \Doctrine\DBAL\Schema\Table) {
|
|
|
|
$table = $table->getName();
|
|
|
|
} else if(!is_string($table)) {
|
|
|
|
throw new \InvalidArgumentException('MysqlPlatform::getDropTableSql() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.');
|
|
|
|
}
|
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
return 'DROP TABLE ' . $table;
|
2008-08-31 22:27:16 +04:00
|
|
|
}
|
2009-05-29 01:34:35 +04:00
|
|
|
|
2008-09-12 21:25:38 +04:00
|
|
|
public function getSetTransactionIsolationSql($level)
|
|
|
|
{
|
|
|
|
return 'SET SESSION TRANSACTION ISOLATION LEVEL ' . $this->_getTransactionIsolationLevelSql($level);
|
|
|
|
}
|
2009-05-27 22:54:40 +04:00
|
|
|
|
|
|
|
/**
|
2009-08-21 22:13:22 +04:00
|
|
|
* Get the platform name for this instance.
|
2009-05-27 22:54:40 +04:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'mysql';
|
|
|
|
}
|
2009-12-06 21:55:08 +03:00
|
|
|
|
|
|
|
public function createsExplicitIndexForForeignKeys()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2009-11-03 19:56:05 +03:00
|
|
|
}
|