Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mysql per-column charset support #881

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -1080,4 +1080,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']);
Copy link

@dolmen dolmen Oct 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the option named characterset instead of charset?

Hint: if I'm asking this, it means that at least a comment in the code is needed.

}

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'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong. The charset is set implicit here through the table's default charset. However why is it latin1 here? As far as I can see, we set it implicitly to utf8 here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, somehow didn't see that you're using custom table collation here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I've set the default table charset to latin1 : https://github.com/doctrine/dbal/pull/881/files#diff-921db63349be598a376afb1584ccc8b5R206, the test is verifying the default charset behavior.

$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')
);
}
}