Skip to content

Commit

Permalink
[DBAL-166] Default MySQL to UTF8/utf8_general_ci. Allow to set defaul…
Browse files Browse the repository at this point in the history
…t table options from SchemaConfig.
  • Loading branch information
beberlei committed Apr 1, 2012
1 parent 1de0f9d commit 107b870
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 31 deletions.
31 changes: 14 additions & 17 deletions lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
Expand Up @@ -410,31 +410,28 @@ protected function _getCreateTableSQL($tableName, array $columns, array $options
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'])) {
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php
Expand Up @@ -848,6 +848,11 @@ public function createSchemaConfig()
$schemaConfig->setName($searchPaths[0]); $schemaConfig->setName($searchPaths[0]);
} }


$params = $this->_conn->getParams();
if (isset($params['defaultTableOptions'])) {
$schemaConfig->setDefaultTableOptions($params['defautTableOptions']);
}

return $schemaConfig; return $schemaConfig;
} }


Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/DBAL/Schema/Schema.php
Expand Up @@ -225,6 +225,11 @@ public function createTable($tableName)
{ {
$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;
} }


Expand Down
39 changes: 30 additions & 9 deletions lib/Doctrine/DBAL/Schema/SchemaConfig.php
Expand Up @@ -32,48 +32,53 @@ 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;
} }


/** /**
* @param bool $flag * @param bool $flag
*/ */
public function setExplicitForeignKeyIndexes($flag) public function setExplicitForeignKeyIndexes($flag)
{ {
$this->_hasExplicitForeignKeyIndexes = (bool)$flag; $this->hasExplicitForeignKeyIndexes = (bool)$flag;
} }


/** /**
* @param int $length * @param int $length
*/ */
public function setMaxIdentifierLength($length) public function setMaxIdentifierLength($length)
{ {
$this->_maxIdentifierLength = (int)$length; $this->maxIdentifierLength = (int)$length;
} }


/** /**
* @return int * @return int
*/ */
public function getMaxIdentifierLength() public function getMaxIdentifierLength()
{ {
return $this->_maxIdentifierLength; return $this->maxIdentifierLength;
} }


/** /**
Expand All @@ -83,7 +88,7 @@ public function getMaxIdentifierLength()
*/ */
public function getName() public function getName()
{ {
return $this->_name; return $this->name;
} }


/** /**
Expand All @@ -93,6 +98,22 @@ public function getName()
*/ */
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;
} }
} }
10 changes: 5 additions & 5 deletions tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php
Expand Up @@ -22,18 +22,18 @@ public function testGenerateMixedCaseTableCreate()
$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'
); );
} }


Expand Down Expand Up @@ -197,7 +197,7 @@ public function testGetDateTimeTypeDeclarationSql()


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()
Expand All @@ -207,7 +207,7 @@ public function getAlterTableColumnCommentsSQL()


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");
} }


/** /**
Expand Down

0 comments on commit 107b870

Please sign in to comment.