Commit 107b8700 authored by Benjamin Eberlei's avatar Benjamin Eberlei

[DBAL-166] Default MySQL to UTF8/utf8_general_ci. Allow to set default table...

[DBAL-166] Default MySQL to UTF8/utf8_general_ci. Allow to set default table options from SchemaConfig.
parent 1de0f9d9
......@@ -410,31 +410,28 @@ class MySqlPlatform extends AbstractPlatform
if (!empty($options['temporary'])) {
$query .= 'TEMPORARY ';
}
$query.= 'TABLE ' . $tableName . ' (' . $queryFields . ')';
$optionStrings = array();
$query .= 'TABLE ' . $tableName . ' (' . $queryFields . ') ';
if (isset($options['comment'])) {
$optionStrings['comment'] = 'COMMENT = ' . $options['comment'];
}
if (isset($options['charset'])) {
$optionStrings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset'];
if (isset($options['collate'])) {
$optionStrings['charset'] .= ' COLLATE ' . $options['collate'];
$query .= 'COMMENT = ' . $options['comment'] . ' ';
}
if ( ! isset($options['charset'])) {
$options['charset'] = 'utf8';
}
// get the type of the table
if (isset($options['engine'])) {
$optionStrings[] = 'ENGINE = ' . $options['engine'];
} else {
// default to innodb
$optionStrings[] = 'ENGINE = InnoDB';
if ( ! isset($options['collate'])) {
$options['collate'] = 'utf8_general_ci';
}
if ( ! empty($optionStrings)) {
$query.= ' '.implode(' ', $optionStrings);
$query .= 'DEFAULT CHARACTER SET ' . $options['charset'];
$query .= ' COLLATE ' . $options['collate'];
if ( ! isset($options['engine'])) {
$options['engine'] = 'InnoDB';
}
$query .= ' ENGINE = ' . $options['engine'];
$sql[] = $query;
if (isset($options['foreignKeys'])) {
......
......@@ -848,6 +848,11 @@ abstract class AbstractSchemaManager
$schemaConfig->setName($searchPaths[0]);
}
$params = $this->_conn->getParams();
if (isset($params['defaultTableOptions'])) {
$schemaConfig->setDefaultTableOptions($params['defautTableOptions']);
}
return $schemaConfig;
}
......
......@@ -225,6 +225,11 @@ class Schema extends AbstractAsset
{
$table = new Table($tableName);
$this->_addTable($table);
foreach ($this->_schemaConfig->getDefaultTableOptions() as $name => $value) {
$table->addOption($name, $value);
}
return $table;
}
......
......@@ -32,24 +32,29 @@ class SchemaConfig
/**
* @var bool
*/
protected $_hasExplicitForeignKeyIndexes = false;
protected $hasExplicitForeignKeyIndexes = false;
/**
* @var int
*/
protected $_maxIdentifierLength = 63;
protected $maxIdentifierLength = 63;
/**
* @var string
*/
protected $_name;
protected $name;
/**
* @var array
*/
protected $defaultTableOptions = array();
/**
* @return bool
*/
public function hasExplicitForeignKeyIndexes()
{
return $this->_hasExplicitForeignKeyIndexes;
return $this->hasExplicitForeignKeyIndexes;
}
/**
......@@ -57,7 +62,7 @@ class SchemaConfig
*/
public function setExplicitForeignKeyIndexes($flag)
{
$this->_hasExplicitForeignKeyIndexes = (bool)$flag;
$this->hasExplicitForeignKeyIndexes = (bool)$flag;
}
/**
......@@ -65,7 +70,7 @@ class SchemaConfig
*/
public function setMaxIdentifierLength($length)
{
$this->_maxIdentifierLength = (int)$length;
$this->maxIdentifierLength = (int)$length;
}
/**
......@@ -73,7 +78,7 @@ class SchemaConfig
*/
public function getMaxIdentifierLength()
{
return $this->_maxIdentifierLength;
return $this->maxIdentifierLength;
}
/**
......@@ -83,7 +88,7 @@ class SchemaConfig
*/
public function getName()
{
return $this->_name;
return $this->name;
}
/**
......@@ -93,6 +98,22 @@ class SchemaConfig
*/
public function setName($name)
{
$this->_name = $name;
$this->name = $name;
}
/**
* Get the default options that are passed to Table instances created with
* Schema#createTable().
*
* @return array
*/
public function getDefaultTableOptions()
{
return $this->defaultTableOptions;
}
public function setDefaultTableOptions(array $defaultTableOptions)
{
$this->defaultTableOptions = $defaultTableOptions;
}
}
......@@ -22,18 +22,18 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
$table->addColumn("Bar", "integer");
$sql = $this->_platform->getCreateTableSQL($table);
$this->assertEquals('CREATE TABLE Foo (Bar INT NOT NULL) ENGINE = InnoDB', array_shift($sql));
$this->assertEquals('CREATE TABLE Foo (Bar INT NOT NULL) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = InnoDB', array_shift($sql));
}
public function getGenerateTableSql()
{
return 'CREATE TABLE test (id INT AUTO_INCREMENT NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id)) ENGINE = InnoDB';
return 'CREATE TABLE test (id INT AUTO_INCREMENT NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = InnoDB';
}
public function getGenerateTableWithMultiColumnUniqueIndexSql()
{
return array(
'CREATE TABLE test (foo VARCHAR(255) DEFAULT NULL, bar VARCHAR(255) DEFAULT NULL, UNIQUE INDEX UNIQ_D87F7E0C8C73652176FF8CAA (foo, bar)) ENGINE = InnoDB'
'CREATE TABLE test (foo VARCHAR(255) DEFAULT NULL, bar VARCHAR(255) DEFAULT NULL, UNIQUE INDEX UNIQ_D87F7E0C8C73652176FF8CAA (foo, bar)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = InnoDB'
);
}
......@@ -197,7 +197,7 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
public function getCreateTableColumnCommentsSQL()
{
return array("CREATE TABLE test (id INT NOT NULL COMMENT 'This is a comment', PRIMARY KEY(id)) ENGINE = InnoDB");
return array("CREATE TABLE test (id INT NOT NULL COMMENT 'This is a comment', PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = InnoDB");
}
public function getAlterTableColumnCommentsSQL()
......@@ -207,7 +207,7 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
public function getCreateTableColumnTypeCommentsSQL()
{
return array("CREATE TABLE test (id INT NOT NULL, data LONGTEXT NOT NULL COMMENT '(DC2Type:array)', PRIMARY KEY(id)) ENGINE = InnoDB");
return array("CREATE TABLE test (id INT NOT NULL, data LONGTEXT NOT NULL COMMENT '(DC2Type:array)', PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = InnoDB");
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment