From 05aa0e2648e0f493c7ef8a325a324025c3edf390 Mon Sep 17 00:00:00 2001 From: mRoca Date: Wed, 15 Jul 2015 19:25:46 +0200 Subject: [PATCH] Add Mysql per-column charset support --- docs/en/reference/schema-representation.rst | 4 ++-- lib/Doctrine/DBAL/Platforms/MySqlPlatform.php | 8 ++++++++ .../DBAL/Schema/MySqlSchemaManager.php | 4 ++++ .../Schema/MySqlSchemaManagerTest.php | 19 +++++++++++++++++++ .../DBAL/Platforms/MySqlPlatformTest.php | 8 ++++++++ 5 files changed, 41 insertions(+), 2 deletions(-) diff --git a/docs/en/reference/schema-representation.rst b/docs/en/reference/schema-representation.rst index 94669a1550a..0a56dcc2e6a 100644 --- a/docs/en/reference/schema-representation.rst +++ b/docs/en/reference/schema-representation.rst @@ -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``. diff --git a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php index ae75979c30c..22b24c6c25d 100644 --- a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php @@ -1081,4 +1081,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') + ); + } }