Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[DBAL-166] Default MySQL to UTF8/utf8_general_ci. Allow to set defaul…

…t table options from SchemaConfig.
  • Loading branch information...
commit 107b8700fe36a3bed163a689b55a127265e4071f 1 parent 1de0f9d
@beberlei beberlei authored
View
31 lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
@@ -410,31 +410,28 @@ protected function _getCreateTableSQL($tableName, array $columns, array $options
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'];
+ $query .= 'COMMENT = ' . $options['comment'] . ' ';
}
- if (isset($options['charset'])) {
- $optionStrings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset'];
- if (isset($options['collate'])) {
- $optionStrings['charset'] .= ' COLLATE ' . $options['collate'];
- }
+
+ 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'])) {
View
5 lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php
@@ -848,6 +848,11 @@ public function createSchemaConfig()
$schemaConfig->setName($searchPaths[0]);
}
+ $params = $this->_conn->getParams();
+ if (isset($params['defaultTableOptions'])) {
+ $schemaConfig->setDefaultTableOptions($params['defautTableOptions']);
+ }
+
return $schemaConfig;
}
View
5 lib/Doctrine/DBAL/Schema/Schema.php
@@ -225,6 +225,11 @@ public function createTable($tableName)
{
$table = new Table($tableName);
$this->_addTable($table);
+
+ foreach ($this->_schemaConfig->getDefaultTableOptions() as $name => $value) {
+ $table->addOption($name, $value);
+ }
+
return $table;
}
View
39 lib/Doctrine/DBAL/Schema/SchemaConfig.php
@@ -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 @@ public function hasExplicitForeignKeyIndexes()
*/
public function setExplicitForeignKeyIndexes($flag)
{
- $this->_hasExplicitForeignKeyIndexes = (bool)$flag;
+ $this->hasExplicitForeignKeyIndexes = (bool)$flag;
}
/**
@@ -65,7 +70,7 @@ public function setExplicitForeignKeyIndexes($flag)
*/
public function setMaxIdentifierLength($length)
{
- $this->_maxIdentifierLength = (int)$length;
+ $this->maxIdentifierLength = (int)$length;
}
/**
@@ -73,7 +78,7 @@ public function setMaxIdentifierLength($length)
*/
public function getMaxIdentifierLength()
{
- return $this->_maxIdentifierLength;
+ return $this->maxIdentifierLength;
}
/**
@@ -83,7 +88,7 @@ public function getMaxIdentifierLength()
*/
public function getName()
{
- return $this->_name;
+ return $this->name;
}
/**
@@ -93,6 +98,22 @@ public function getName()
*/
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;
}
}
View
10 tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php
@@ -22,18 +22,18 @@ public function testGenerateMixedCaseTableCreate()
$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 @@ public function testGetDateTimeTypeDeclarationSql()
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 @@ public function getAlterTableColumnCommentsSQL()
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");
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.