Skip to content

Commit

Permalink
Add Mysql per-column charset support
Browse files Browse the repository at this point in the history
  • Loading branch information
mRoca committed Jul 15, 2015
1 parent eae7b0d commit 05aa0e2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/en/reference/schema-representation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ The following options are completely vendor specific and absolutely not portable

- **charset** (string): The character set to use for the column. Currently only supported
on MySQL and Drizzle.
- **collate** (string): The collation to use for the column. Currently only supported on
SQL Server.
- **collation** (string): The collation to use for the column. Supported by MySQL, PostgreSQL,
Sqlite, SQL Server and Drizzle.
- **check** (string): The check constraint clause to add to the column.
Defaults to ``null``.
8 changes: 8 additions & 0 deletions lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -1081,4 +1081,12 @@ public function getBlobTypeDeclarationSQL(array $field)

return 'LONGBLOB';
}

/**
* {@inheritDoc}
*/
public function getColumnCharsetDeclarationSQL($charset)
{
return 'CHARACTER SET ' . $charset;
}
}
4 changes: 4 additions & 0 deletions lib/Doctrine/DBAL/Schema/MySqlSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ protected function _getPortableTableColumnDefinition($tableColumn)

$column = new Column($tableColumn['field'], Type::getType($type), $options);

if (isset($tableColumn['characterset'])) {
$column->setPlatformOption('charset', $tableColumn['characterset']);
}

if (isset($tableColumn['collation'])) {
$column->setPlatformOption('collation', $tableColumn['collation']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,25 @@ public function testColumnCollation()
$this->assertEquals('utf8_general_ci', $columns['bar']->getPlatformOption('collation'));
}

public function testColumnCharset()
{
$table = new Table('test_charset');
$table->addOption('collate', $collation = 'latin1_swedish_ci');
$table->addOption('charset', 'latin1');
$table->addColumn('id', 'integer');
$table->addColumn('text', 'text');
$table->addColumn('foo', 'text')->setPlatformOption('charset', 'latin1');
$table->addColumn('bar', 'text')->setPlatformOption('charset', 'utf8');
$this->_sm->dropAndCreateTable($table);

$columns = $this->_sm->listTableColumns('test_charset');

$this->assertArrayNotHasKey('charset', $columns['id']->getPlatformOptions());
$this->assertEquals('latin1', $columns['text']->getPlatformOption('charset'));
$this->assertEquals('latin1', $columns['foo']->getPlatformOption('charset'));
$this->assertEquals('utf8', $columns['bar']->getPlatformOption('charset'));
}

/**
* @group DBAL-843
*/
Expand Down
8 changes: 8 additions & 0 deletions tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ public function createPlatform()
{
return new MysqlPlatform;
}

public function testColumnCharsetDeclarationSQL()
{
$this->assertEquals(
'CHARACTER SET utf8',
$this->_platform->getColumnCharsetDeclarationSQL('utf8')
);
}
}

0 comments on commit 05aa0e2

Please sign in to comment.