Commit 87273b29 authored by Steve Müller's avatar Steve Müller Committed by Martin Hasoň

add column collation support for Drizzle

parent e2a7688a
...@@ -228,6 +228,116 @@ class DrizzlePlatform extends AbstractPlatform ...@@ -228,6 +228,116 @@ class DrizzlePlatform extends AbstractPlatform
return 'DROP DATABASE ' . $name; return 'DROP DATABASE ' . $name;
} }
/**
* {@inheritDoc}
*/
protected function _getCreateTableSQL($tableName, array $columns, array $options = array())
{
$queryFields = $this->getColumnDeclarationListSQL($columns);
if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
foreach ($options['uniqueConstraints'] as $index => $definition) {
$queryFields .= ', ' . $this->getUniqueConstraintDeclarationSQL($index, $definition);
}
}
// add all indexes
if (isset($options['indexes']) && ! empty($options['indexes'])) {
foreach($options['indexes'] as $index => $definition) {
$queryFields .= ', ' . $this->getIndexDeclarationSQL($index, $definition);
}
}
// attach all primary keys
if (isset($options['primary']) && ! empty($options['primary'])) {
$keyColumns = array_unique(array_values($options['primary']));
$queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
}
$query = 'CREATE ';
if (!empty($options['temporary'])) {
$query .= 'TEMPORARY ';
}
$query .= 'TABLE ' . $tableName . ' (' . $queryFields . ') ';
$query .= $this->buildTableOptions($options);
$query .= $this->buildPartitionOptions($options);
$sql[] = $query;
if (isset($options['foreignKeys'])) {
foreach ((array) $options['foreignKeys'] as $definition) {
$sql[] = $this->getCreateForeignKeySQL($definition, $tableName);
}
}
return $sql;
}
/**
* Build SQL for table options
*
* @param array $options
*
* @return string
*/
private function buildTableOptions(array $options)
{
if (isset($options['table_options'])) {
return $options['table_options'];
}
$tableOptions = array();
// Collate
if ( ! isset($options['collate'])) {
$options['collate'] = 'utf8_unicode_ci';
}
$tableOptions[] = sprintf('COLLATE %s', $options['collate']);
// Engine
if ( ! isset($options['engine'])) {
$options['engine'] = 'InnoDB';
}
$tableOptions[] = sprintf('ENGINE = %s', $options['engine']);
// Auto increment
if (isset($options['auto_increment'])) {
$tableOptions[] = sprintf('AUTO_INCREMENT = %s', $options['auto_increment']);
}
// Comment
if (isset($options['comment'])) {
$comment = trim($options['comment'], " '");
$tableOptions[] = sprintf("COMMENT = '%s' ", str_replace("'", "''", $comment));
}
// Row format
if (isset($options['row_format'])) {
$tableOptions[] = sprintf('ROW_FORMAT = %s', $options['row_format']);
}
return implode(' ', $tableOptions);
}
/**
* Build SQL for partition options.
*
* @param array $options
*
* @return string
*/
private function buildPartitionOptions(array $options)
{
return (isset($options['partition_options']))
? ' ' . $options['partition_options']
: '';
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
...@@ -264,7 +374,7 @@ class DrizzlePlatform extends AbstractPlatform ...@@ -264,7 +374,7 @@ class DrizzlePlatform extends AbstractPlatform
} }
return "SELECT COLUMN_NAME, DATA_TYPE, COLUMN_COMMENT, IS_NULLABLE, IS_AUTO_INCREMENT, CHARACTER_MAXIMUM_LENGTH, COLUMN_DEFAULT," . return "SELECT COLUMN_NAME, DATA_TYPE, COLUMN_COMMENT, IS_NULLABLE, IS_AUTO_INCREMENT, CHARACTER_MAXIMUM_LENGTH, COLUMN_DEFAULT," .
" NUMERIC_PRECISION, NUMERIC_SCALE" . " NUMERIC_PRECISION, NUMERIC_SCALE, COLLATION_NAME" .
" FROM DATA_DICTIONARY.COLUMNS" . " FROM DATA_DICTIONARY.COLUMNS" .
" WHERE TABLE_SCHEMA=" . $database . " AND TABLE_NAME = '" . $table . "'"; " WHERE TABLE_SCHEMA=" . $database . " AND TABLE_NAME = '" . $table . "'";
} }
...@@ -333,6 +443,14 @@ class DrizzlePlatform extends AbstractPlatform ...@@ -333,6 +443,14 @@ class DrizzlePlatform extends AbstractPlatform
return false; return false;
} }
/**
* {@inheritdoc}
*/
public function supportsColumnCollation()
{
return true;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
......
...@@ -19,6 +19,8 @@ ...@@ -19,6 +19,8 @@
namespace Doctrine\DBAL\Schema; namespace Doctrine\DBAL\Schema;
use Doctrine\DBAL\Types\Type;
/** /**
* Schema manager for the Drizzle RDBMS. * Schema manager for the Drizzle RDBMS.
* *
...@@ -31,7 +33,6 @@ class DrizzleSchemaManager extends AbstractSchemaManager ...@@ -31,7 +33,6 @@ class DrizzleSchemaManager extends AbstractSchemaManager
*/ */
protected function _getPortableTableColumnDefinition($tableColumn) protected function _getPortableTableColumnDefinition($tableColumn)
{ {
$tableName = $tableColumn['COLUMN_NAME'];
$dbType = strtolower($tableColumn['DATA_TYPE']); $dbType = strtolower($tableColumn['DATA_TYPE']);
$type = $this->_platform->getDoctrineTypeMapping($dbType); $type = $this->_platform->getDoctrineTypeMapping($dbType);
...@@ -48,7 +49,13 @@ class DrizzleSchemaManager extends AbstractSchemaManager ...@@ -48,7 +49,13 @@ class DrizzleSchemaManager extends AbstractSchemaManager
'comment' => (isset($tableColumn['COLUMN_COMMENT']) ? $tableColumn['COLUMN_COMMENT'] : null), 'comment' => (isset($tableColumn['COLUMN_COMMENT']) ? $tableColumn['COLUMN_COMMENT'] : null),
); );
return new Column($tableName, \Doctrine\DBAL\Types\Type::getType($type), $options); $column = new Column($tableColumn['COLUMN_NAME'], Type::getType($type), $options);
if ( ! empty($tableColumn['COLLATION_NAME'])) {
$column->setPlatformOption('collation', $tableColumn['COLLATION_NAME']);
}
return $column;
} }
/** /**
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
namespace Doctrine\Tests\DBAL\Functional\Schema; namespace Doctrine\Tests\DBAL\Functional\Schema;
use Doctrine\DBAL\Schema\Table;
require_once __DIR__ . '/../../../TestInit.php'; require_once __DIR__ . '/../../../TestInit.php';
class DrizzleSchemaManagerTest extends SchemaManagerFunctionalTestCase class DrizzleSchemaManagerTest extends SchemaManagerFunctionalTestCase
...@@ -10,7 +12,7 @@ class DrizzleSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -10,7 +12,7 @@ class DrizzleSchemaManagerTest extends SchemaManagerFunctionalTestCase
{ {
$tableName = 'test_binary_table'; $tableName = 'test_binary_table';
$table = new \Doctrine\DBAL\Schema\Table($tableName); $table = new Table($tableName);
$table->addColumn('id', 'integer'); $table->addColumn('id', 'integer');
$table->addColumn('column_varbinary', 'binary', array()); $table->addColumn('column_varbinary', 'binary', array());
$table->addColumn('column_binary', 'binary', array('fixed' => true)); $table->addColumn('column_binary', 'binary', array('fixed' => true));
...@@ -26,4 +28,22 @@ class DrizzleSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -26,4 +28,22 @@ class DrizzleSchemaManagerTest extends SchemaManagerFunctionalTestCase
$this->assertInstanceOf('Doctrine\DBAL\Types\BinaryType', $table->getColumn('column_binary')->getType()); $this->assertInstanceOf('Doctrine\DBAL\Types\BinaryType', $table->getColumn('column_binary')->getType());
$this->assertFalse($table->getColumn('column_binary')->getFixed()); $this->assertFalse($table->getColumn('column_binary')->getFixed());
} }
public function testColumnCollation()
{
$table = new Table('test_collation');
$table->addOption('collate', $collation = 'utf8_unicode_ci');
$table->addColumn('id', 'integer');
$table->addColumn('text', 'text');
$table->addColumn('foo', 'text')->setPlatformOption('collation', 'utf8_swedish_ci');
$table->addColumn('bar', 'text')->setPlatformOption('collation', 'utf8_general_ci');
$this->_sm->dropAndCreateTable($table);
$columns = $this->_sm->listTableColumns('test_collation');
$this->assertArrayNotHasKey('collation', $columns['id']->getPlatformOptions());
$this->assertEquals('utf8_unicode_ci', $columns['text']->getPlatformOption('collation'));
$this->assertEquals('utf8_swedish_ci', $columns['foo']->getPlatformOption('collation'));
$this->assertEquals('utf8_general_ci', $columns['bar']->getPlatformOption('collation'));
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment