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 ...@@ -410,31 +410,28 @@ class MySqlPlatform extends AbstractPlatform
if (!empty($options['temporary'])) { if (!empty($options['temporary'])) {
$query .= 'TEMPORARY '; $query .= 'TEMPORARY ';
} }
$query.= 'TABLE ' . $tableName . ' (' . $queryFields . ')'; $query .= 'TABLE ' . $tableName . ' (' . $queryFields . ') ';
$optionStrings = array();
if (isset($options['comment'])) { if (isset($options['comment'])) {
$optionStrings['comment'] = 'COMMENT = ' . $options['comment']; $query .= 'COMMENT = ' . $options['comment'] . ' ';
} }
if (isset($options['charset'])) {
$optionStrings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset']; if ( ! isset($options['charset'])) {
if (isset($options['collate'])) { $options['charset'] = 'utf8';
$optionStrings['charset'] .= ' COLLATE ' . $options['collate'];
}
} }
// get the type of the table if ( ! isset($options['collate'])) {
if (isset($options['engine'])) { $options['collate'] = 'utf8_general_ci';
$optionStrings[] = 'ENGINE = ' . $options['engine'];
} else {
// default to innodb
$optionStrings[] = 'ENGINE = InnoDB';
} }
if ( ! empty($optionStrings)) { $query .= 'DEFAULT CHARACTER SET ' . $options['charset'];
$query.= ' '.implode(' ', $optionStrings); $query .= ' COLLATE ' . $options['collate'];
if ( ! isset($options['engine'])) {
$options['engine'] = 'InnoDB';
} }
$query .= ' ENGINE = ' . $options['engine'];
$sql[] = $query; $sql[] = $query;
if (isset($options['foreignKeys'])) { if (isset($options['foreignKeys'])) {
......
...@@ -848,6 +848,11 @@ abstract class AbstractSchemaManager ...@@ -848,6 +848,11 @@ abstract class AbstractSchemaManager
$schemaConfig->setName($searchPaths[0]); $schemaConfig->setName($searchPaths[0]);
} }
$params = $this->_conn->getParams();
if (isset($params['defaultTableOptions'])) {
$schemaConfig->setDefaultTableOptions($params['defautTableOptions']);
}
return $schemaConfig; return $schemaConfig;
} }
......
...@@ -225,6 +225,11 @@ class Schema extends AbstractAsset ...@@ -225,6 +225,11 @@ class Schema extends AbstractAsset
{ {
$table = new Table($tableName); $table = new Table($tableName);
$this->_addTable($table); $this->_addTable($table);
foreach ($this->_schemaConfig->getDefaultTableOptions() as $name => $value) {
$table->addOption($name, $value);
}
return $table; return $table;
} }
......
...@@ -32,24 +32,29 @@ class SchemaConfig ...@@ -32,24 +32,29 @@ class SchemaConfig
/** /**
* @var bool * @var bool
*/ */
protected $_hasExplicitForeignKeyIndexes = false; protected $hasExplicitForeignKeyIndexes = false;
/** /**
* @var int * @var int
*/ */
protected $_maxIdentifierLength = 63; protected $maxIdentifierLength = 63;
/** /**
* @var string * @var string
*/ */
protected $_name; protected $name;
/**
* @var array
*/
protected $defaultTableOptions = array();
/** /**
* @return bool * @return bool
*/ */
public function hasExplicitForeignKeyIndexes() public function hasExplicitForeignKeyIndexes()
{ {
return $this->_hasExplicitForeignKeyIndexes; return $this->hasExplicitForeignKeyIndexes;
} }
/** /**
...@@ -57,7 +62,7 @@ class SchemaConfig ...@@ -57,7 +62,7 @@ class SchemaConfig
*/ */
public function setExplicitForeignKeyIndexes($flag) public function setExplicitForeignKeyIndexes($flag)
{ {
$this->_hasExplicitForeignKeyIndexes = (bool)$flag; $this->hasExplicitForeignKeyIndexes = (bool)$flag;
} }
/** /**
...@@ -65,7 +70,7 @@ class SchemaConfig ...@@ -65,7 +70,7 @@ class SchemaConfig
*/ */
public function setMaxIdentifierLength($length) public function setMaxIdentifierLength($length)
{ {
$this->_maxIdentifierLength = (int)$length; $this->maxIdentifierLength = (int)$length;
} }
/** /**
...@@ -73,7 +78,7 @@ class SchemaConfig ...@@ -73,7 +78,7 @@ class SchemaConfig
*/ */
public function getMaxIdentifierLength() public function getMaxIdentifierLength()
{ {
return $this->_maxIdentifierLength; return $this->maxIdentifierLength;
} }
/** /**
...@@ -83,7 +88,7 @@ class SchemaConfig ...@@ -83,7 +88,7 @@ class SchemaConfig
*/ */
public function getName() public function getName()
{ {
return $this->_name; return $this->name;
} }
/** /**
...@@ -93,6 +98,22 @@ class SchemaConfig ...@@ -93,6 +98,22 @@ class SchemaConfig
*/ */
public function setName($name) 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 ...@@ -22,18 +22,18 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
$table->addColumn("Bar", "integer"); $table->addColumn("Bar", "integer");
$sql = $this->_platform->getCreateTableSQL($table); $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() 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() public function getGenerateTableWithMultiColumnUniqueIndexSql()
{ {
return array( 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 ...@@ -197,7 +197,7 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
public function getCreateTableColumnCommentsSQL() 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() public function getAlterTableColumnCommentsSQL()
...@@ -207,7 +207,7 @@ class MySqlPlatformTest extends AbstractPlatformTestCase ...@@ -207,7 +207,7 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
public function getCreateTableColumnTypeCommentsSQL() 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