From adbccdf15a8fcc9a5fc0d039e17dcf9f5bf11f01 Mon Sep 17 00:00:00 2001 From: mRoca Date: Thu, 17 Sep 2015 10:53:42 +0200 Subject: [PATCH] Add Mysql per-column charset support --- lib/Doctrine/DBAL/Platforms/MySqlPlatform.php | 8 ++++++++ .../DBAL/Schema/MySqlSchemaManager.php | 4 ++++ .../Schema/MySqlSchemaManagerTest.php | 19 +++++++++++++++++++ .../DBAL/Platforms/MySqlPlatformTest.php | 8 ++++++++ 4 files changed, 39 insertions(+) diff --git a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php index db9d6829f46..50f88e0ada2 100644 --- a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php @@ -1080,4 +1080,12 @@ public function getBlobTypeDeclarationSQL(array $field) return 'LONGBLOB'; } + + /** + * {@inheritDoc} + */ + public function getColumnCharsetDeclarationSQL($charset) + { + return 'CHARACTER SET ' . $charset; + } } diff --git a/lib/Doctrine/DBAL/Schema/MySqlSchemaManager.php b/lib/Doctrine/DBAL/Schema/MySqlSchemaManager.php index 0b36762b6ce..0fee8e66d79 100644 --- a/lib/Doctrine/DBAL/Schema/MySqlSchemaManager.php +++ b/lib/Doctrine/DBAL/Schema/MySqlSchemaManager.php @@ -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']); } diff --git a/tests/Doctrine/Tests/DBAL/Functional/Schema/MySqlSchemaManagerTest.php b/tests/Doctrine/Tests/DBAL/Functional/Schema/MySqlSchemaManagerTest.php index 9383848500b..052a09f11eb 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Schema/MySqlSchemaManagerTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Schema/MySqlSchemaManagerTest.php @@ -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 */ diff --git a/tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php b/tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php index ac1e686be7a..576573fa036 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php @@ -10,4 +10,12 @@ public function createPlatform() { return new MysqlPlatform; } + + public function testColumnCharsetDeclarationSQL() + { + $this->assertEquals( + 'CHARACTER SET utf8', + $this->_platform->getColumnCharsetDeclarationSQL('utf8') + ); + } }