Commit 9cd0379f authored by beberlei's avatar beberlei

[2.0] DDC-200 Implemented support for @columnDefinition - even with support to...

[2.0] DDC-200 Implemented support for @columnDefinition - even with support to pass the definition to join columns if necessary for relations.
parent 46ed63d1
...@@ -583,7 +583,7 @@ abstract class AbstractPlatform ...@@ -583,7 +583,7 @@ abstract class AbstractPlatform
$columnData['precision'] = $column->getPrecision(); $columnData['precision'] = $column->getPrecision();
$columnData['scale'] = $column->getScale(); $columnData['scale'] = $column->getScale();
$columnData['default'] = $column->getDefault(); $columnData['default'] = $column->getDefault();
// TODO: Fixed? Unsigned? $columnData['columnDefinition'] = $column->getColumnDefinition();
if(in_array($column->getName(), $options['primary'])) { if(in_array($column->getName(), $options['primary'])) {
$columnData['primary'] = true; $columnData['primary'] = true;
...@@ -903,11 +903,16 @@ abstract class AbstractPlatform ...@@ -903,11 +903,16 @@ abstract class AbstractPlatform
* unique constraint * unique constraint
* check * check
* column check constraint * column check constraint
* columnDefinition
* a string that defines the complete column
* *
* @return string DBMS specific SQL code portion that should be used to declare the column. * @return string DBMS specific SQL code portion that should be used to declare the column.
*/ */
public function getColumnDeclarationSql($name, array $field) public function getColumnDeclarationSql($name, array $field)
{ {
if (isset($field['columnDefinition'])) {
$columnDef = $this->getCustomTypeDeclarationSql($field);
} else {
$default = $this->getDefaultValueDeclarationSql($field); $default = $this->getDefaultValueDeclarationSql($field);
$charset = (isset($field['charset']) && $field['charset']) ? $charset = (isset($field['charset']) && $field['charset']) ?
...@@ -925,8 +930,10 @@ abstract class AbstractPlatform ...@@ -925,8 +930,10 @@ abstract class AbstractPlatform
' ' . $field['check'] : ''; ' ' . $field['check'] : '';
$typeDecl = $field['type']->getSqlDeclaration($field, $this); $typeDecl = $field['type']->getSqlDeclaration($field, $this);
$columnDef = $typeDecl . $charset . $default . $notnull . $unique . $check . $collation;
}
return $name . ' ' . $typeDecl . $charset . $default . $notnull . $unique . $check . $collation; return $name . ' ' . $columnDef;
} }
/** /**
...@@ -1081,6 +1088,19 @@ abstract class AbstractPlatform ...@@ -1081,6 +1088,19 @@ abstract class AbstractPlatform
. ')'; . ')';
} }
/**
* getCustomTypeDeclarationSql
* Obtail SQL code portion needed to create a custom column,
* e.g. when a field has the "columnDefinition" keyword.
* Only "AUTOINCREMENT" and "PRIMARY KEY" are added if appropriate.
*
* @return string
*/
public function getCustomTypeDeclarationSql(array $columnDef)
{
return $columnDef['columnDefinition'];
}
/** /**
* getIndexFieldDeclarationList * getIndexFieldDeclarationList
* Obtain DBMS specific SQL code portion needed to set an index * Obtain DBMS specific SQL code portion needed to set an index
......
This diff is collapsed.
...@@ -80,6 +80,11 @@ class Column extends AbstractAsset ...@@ -80,6 +80,11 @@ class Column extends AbstractAsset
*/ */
protected $_platformOptions = array(); protected $_platformOptions = array();
/**
* @var string
*/
protected $_columnDefinition = null;
/** /**
* Create a new Column * Create a new Column
* *
...@@ -226,6 +231,17 @@ class Column extends AbstractAsset ...@@ -226,6 +231,17 @@ class Column extends AbstractAsset
return $this; return $this;
} }
/**
*
* @param string
* @return Column
*/
public function setColumnDefinition($value)
{
$this->_columnDefinition = $value;
return $this;
}
public function getType() public function getType()
{ {
return $this->_type; return $this->_type;
...@@ -281,6 +297,11 @@ class Column extends AbstractAsset ...@@ -281,6 +297,11 @@ class Column extends AbstractAsset
return $this->_platformOptions[$name]; return $this->_platformOptions[$name];
} }
public function getColumnDefinition()
{
return $this->_columnDefinition;
}
/** /**
* @param Visitor $visitor * @param Visitor $visitor
*/ */
...@@ -304,6 +325,7 @@ class Column extends AbstractAsset ...@@ -304,6 +325,7 @@ class Column extends AbstractAsset
'scale' => $this->_scale, 'scale' => $this->_scale,
'fixed' => $this->_fixed, 'fixed' => $this->_fixed,
'unsigned' => $this->_unsigned, 'unsigned' => $this->_unsigned,
'columnDefinition' => $this->_columnDefinition,
), $this->_platformOptions); ), $this->_platformOptions);
} }
} }
\ No newline at end of file
...@@ -200,6 +200,10 @@ class AnnotationDriver implements Driver ...@@ -200,6 +200,10 @@ class AnnotationDriver implements Driver
$mapping['columnName'] = $columnAnnot->name; $mapping['columnName'] = $columnAnnot->name;
} }
if (isset($columnAnnot->columnDefinition)) {
$mapping['columnDefinition'] = $columnAnnot->columnDefinition;
}
if ($idAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Id')) { if ($idAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Id')) {
$mapping['id'] = true; $mapping['id'] = true;
} }
......
This diff is collapsed.
...@@ -63,6 +63,7 @@ final class Column extends Annotation { ...@@ -63,6 +63,7 @@ final class Column extends Annotation {
public $default; //TODO: remove? public $default; //TODO: remove?
public $name; public $name;
public $options = array(); public $options = array();
public $columnDefinition;
} }
final class OneToOne extends Annotation { final class OneToOne extends Annotation {
public $targetEntity; public $targetEntity;
......
...@@ -159,6 +159,10 @@ class XmlDriver extends AbstractFileDriver ...@@ -159,6 +159,10 @@ class XmlDriver extends AbstractFileDriver
$metadata->setVersionMapping($mapping); $metadata->setVersionMapping($mapping);
} }
if (isset($fieldMapping['columnDefinition'])) {
$mapping['columnDefinition'] = (string)$fieldMapping['columnDefinition'];
}
$metadata->mapField($mapping); $metadata->mapField($mapping);
} }
} }
......
This diff is collapsed.
...@@ -204,6 +204,9 @@ class YamlDriver extends AbstractFileDriver ...@@ -204,6 +204,9 @@ class YamlDriver extends AbstractFileDriver
if (isset($fieldMapping['version']) && $fieldMapping['version']) { if (isset($fieldMapping['version']) && $fieldMapping['version']) {
$metadata->setVersionMapping($mapping); $metadata->setVersionMapping($mapping);
} }
if (isset($fieldMapping['columnDefinition'])) {
$mapping['columnDefinition'] = $fieldMapping['columnDefinition'];
}
$metadata->mapField($mapping); $metadata->mapField($mapping);
} }
......
This diff is collapsed.
...@@ -307,6 +307,10 @@ class SchemaTool ...@@ -307,6 +307,10 @@ class SchemaTool
$options['default'] = $mapping['default']; $options['default'] = $mapping['default'];
} }
if (isset($mapping['columnDefinition'])) {
$options['columnDefinition'] = $mapping['columnDefinition'];
}
if ($table->hasColumn($columnName)) { if ($table->hasColumn($columnName)) {
// required in some inheritence scenarios // required in some inheritence scenarios
$table->changeColumn($columnName, $options); $table->changeColumn($columnName, $options);
...@@ -392,8 +396,9 @@ class SchemaTool ...@@ -392,8 +396,9 @@ class SchemaTool
foreach ($joinColumns as $joinColumn) { foreach ($joinColumns as $joinColumn) {
// Note that this thing might be quoted, i.e. `foo`, [foo], ... // Note that this thing might be quoted, i.e. `foo`, [foo], ...
$columnName = $mapping->getQuotedJoinColumnName($joinColumn['name'], $this->_platform); $columnName = $mapping->getQuotedJoinColumnName($joinColumn['name'], $this->_platform);
$referencedFieldName = $class->getFieldName($joinColumn['referencedColumnName']);
if (!$class->hasField($class->getFieldName($joinColumn['referencedColumnName']))) { if (!$class->hasField($referencedFieldName)) {
throw new \Doctrine\Common\DoctrineException( throw new \Doctrine\Common\DoctrineException(
"Column name `".$joinColumn['referencedColumnName']."` referenced for relation from ". "Column name `".$joinColumn['referencedColumnName']."` referenced for relation from ".
"$mapping->sourceEntityName towards $mapping->targetEntityName does not exist." "$mapping->sourceEntityName towards $mapping->targetEntityName does not exist."
...@@ -408,8 +413,13 @@ class SchemaTool ...@@ -408,8 +413,13 @@ class SchemaTool
// Only add the column to the table if it does not exist already. // Only add the column to the table if it does not exist already.
// It might exist already if the foreign key is mapped into a regular // It might exist already if the foreign key is mapped into a regular
// property as well. // property as well.
$fieldMapping = $class->getFieldMapping($referencedFieldName);
$columnDef = isset($fieldMapping['columnDefinition']) ? $fieldMapping['columnDefinition'] : null;
$columnOptions = array('notnull' => false, 'columnDefinition' => $columnDef);
$theJoinTable->createColumn( $theJoinTable->createColumn(
$columnName, $class->getTypeOfColumn($joinColumn['referencedColumnName']), array('notnull' => false) $columnName, $class->getTypeOfColumn($joinColumn['referencedColumnName']), $columnOptions
); );
} }
......
This diff is collapsed.
...@@ -4,6 +4,9 @@ namespace Doctrine\Tests\DBAL\Platforms; ...@@ -4,6 +4,9 @@ namespace Doctrine\Tests\DBAL\Platforms;
abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
{ {
/**
* @var Doctrine\DBAL\Platforms\AbstractPlatform
*/
protected $_platform; protected $_platform;
abstract public function createPlatform(); abstract public function createPlatform();
...@@ -125,4 +128,10 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase ...@@ -125,4 +128,10 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
$this->assertEquals($expectedSql, $sql); $this->assertEquals($expectedSql, $sql);
} }
public function testGetCustomColumnDeclarationSql()
{
$field = array('columnDefinition' => 'MEDIUMINT(6) UNSIGNED');
$this->assertEquals('foo MEDIUMINT(6) UNSIGNED', $this->_platform->getColumnDeclarationSql('foo', $field));
}
} }
...@@ -44,6 +44,7 @@ class ColumnTest extends \PHPUnit_Framework_TestCase ...@@ -44,6 +44,7 @@ class ColumnTest extends \PHPUnit_Framework_TestCase
'scale' => 2, 'scale' => 2,
'fixed' => true, 'fixed' => true,
'unsigned' => true, 'unsigned' => true,
'columnDefinition' => null,
'foo' => 'bar', 'foo' => 'bar',
); );
......
...@@ -18,10 +18,11 @@ class AllTests ...@@ -18,10 +18,11 @@ class AllTests
public static function suite() public static function suite()
{ {
$suite = new \Doctrine\Tests\DoctrineTestSuite('Doctrine Orm Hydration'); $suite = new \Doctrine\Tests\DoctrineTestSuite('Doctrine Orm Tools');
$suite->addTestSuite('Doctrine\Tests\ORM\Tools\Export\ClassMetadataExporterTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Tools\Export\ClassMetadataExporterTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Tools\ConvertDoctrine1SchemaTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Tools\ConvertDoctrine1SchemaTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Tools\SchemaToolTest');
return $suite; return $suite;
} }
......
...@@ -32,4 +32,28 @@ class SchemaToolTest extends \Doctrine\Tests\OrmTestCase ...@@ -32,4 +32,28 @@ class SchemaToolTest extends \Doctrine\Tests\OrmTestCase
array_map('strtolower', $schema->getTable('cms_users')->getIndex('cms_users_username_uniq')->getColumns()) array_map('strtolower', $schema->getTable('cms_users')->getIndex('cms_users_username_uniq')->getColumns())
); );
} }
/**
* @group DDC-200
*/
public function testPassColumnDefinitionToJoinColumn()
{
$customColumnDef = "MEDIUMINT(6) UNSIGNED NOT NULL";
$em = $this->_getTestEntityManager();
$schemaTool = new SchemaTool($em);
$avatar = $em->getClassMetadata('Doctrine\Tests\Models\Forum\ForumAvatar');
$avatar->fieldMappings['id']['columnDefinition'] = $customColumnDef;
$user = $em->getClassMetadata('Doctrine\Tests\Models\Forum\ForumUser');
$classes = array($avatar, $user);
$schema = $schemaTool->getSchemaFromMetadata($classes);
$this->assertTrue($schema->hasTable('forum_users'));
$table = $schema->getTable("forum_users");
$this->assertTrue($table->hasColumn('avatar_id'));
$this->assertEquals($customColumnDef, $table->getColumn('avatar_id')->getColumnDefinition());
}
} }
\ No newline at end of file
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