Skip to content

Instantly share code, notes, and snippets.

@doctrinebot
Created December 13, 2015 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doctrinebot/2226e3fbd8aba2d5448f to your computer and use it in GitHub Desktop.
Save doctrinebot/2226e3fbd8aba2d5448f to your computer and use it in GitHub Desktop.
Attachments to Doctrine Jira Issue DDC-200 - https://github.com/doctrine/doctrine2/issues/2677
diff --git lib/Doctrine/DBAL/Platforms/AbstractPlatform.php lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
index 742832f..15ad868 100644
--- lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
+++ lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
@@ -583,6 +583,7 @@ abstract class AbstractPlatform
$columnData['precision'] = $column->getPrecision();
$columnData['scale'] = $column->getScale();
$columnData['default'] = $column->getDefault();
+ $columnData['columnDefinition'] = $column->getColumnDefinition();
// TODO: Fixed? Unsigned?
if(in_array($column->getName(), $options['primary'])) {
@@ -924,7 +925,11 @@ abstract class AbstractPlatform
$check = (isset($field['check']) && $field['check']) ?
' ' . $field['check'] : '';
- $typeDecl = $field['type']->getSqlDeclaration($field, $this);
+ if (isset($field['columnDefinition'])) {
+ $typeDecl = $this->getCustomTypeDeclarationSql($field);
+ } else {
+ $typeDecl = $field['type']->getSqlDeclaration($field, $this);
+ }
return $name . ' ' . $typeDecl . $charset . $default . $notnull . $unique . $check . $collation;
}
@@ -1064,6 +1069,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
* Obtain DBMS specific SQL code portion needed to set an index
* declaration to be used in statements like CREATE TABLE.
diff --git lib/Doctrine/DBAL/Platforms/MySqlPlatform.php lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
index d95ecac..71032f9 100644
--- lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
+++ lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
@@ -683,6 +683,17 @@ class MySqlPlatform extends AbstractPlatform
return $unsigned . $autoinc;
}
+ /** @override */
+ public function getCustomTypeDeclarationSql(array $columnDef)
+ {
+ $autoinc = '';
+ if ( ! empty($columnDef['autoincrement'])) {
+ $autoinc = ' AUTO_INCREMENT';
+ }
+
+ return $columnDef['columnDefinition'] . $autoinc;
+ }
+
/**
* Obtain DBMS specific SQL code portion needed to set an index
* declaration to be used in statements like CREATE TABLE.
diff --git lib/Doctrine/DBAL/Platforms/SqlitePlatform.php lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
index a5c35fb..e5cb89f 100644
--- lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
+++ lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
@@ -284,6 +284,17 @@ class SqlitePlatform extends AbstractPlatform
return 'INTEGER' . $pk . $autoinc;
}
+ /**
+ * @override
+ */
+ public function getCustomTypeDeclarationSql(array $columnDef)
+ {
+ $autoinc = ! empty($columnDef['autoincrement']) ? ' AUTOINCREMENT' : '';
+ $pk = ! empty($columnDef['primary']) && ! empty($autoinc) ? ' PRIMARY KEY' : '';
+
+ return $columnDef['columnDefinition'] . $pk . $autoinc;
+ }
+
/**
* create a new table
*
diff --git lib/Doctrine/DBAL/Schema/Column.php lib/Doctrine/DBAL/Schema/Column.php
index 23d44e9..3b44688 100644
--- lib/Doctrine/DBAL/Schema/Column.php
+++ lib/Doctrine/DBAL/Schema/Column.php
@@ -81,6 +81,11 @@ class Column extends AbstractAsset
protected $_platformOptions = array();
/**
+ * @var string
+ */
+ protected $_columnDefinition = null;
+
+ /**
* Create a new Column
*
* @param string $columnName
@@ -226,6 +231,17 @@ class Column extends AbstractAsset
return $this;
}
+ /**
+ *
+ * @param string
+ * @return Column
+ */
+ public function setColumnDefinition($value)
+ {
+ $this->_columnDefinition = $value;
+ return $this;
+ }
+
public function getType()
{
return $this->_type;
@@ -281,6 +297,11 @@ class Column extends AbstractAsset
return $this->_platformOptions[$name];
}
+ public function getColumnDefinition()
+ {
+ return $this->_columnDefinition;
+ }
+
/**
* @param Visitor $visitor
*/
@@ -304,6 +325,7 @@ class Column extends AbstractAsset
'scale' => $this->_scale,
'fixed' => $this->_fixed,
'unsigned' => $this->_unsigned,
+ 'columnDefinition' => $this->_columnDefinition,
), $this->_platformOptions);
}
}
\ No newline at end of file
diff --git lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php
index 74bf53b..163bc08 100644
--- lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php
+++ lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php
@@ -196,6 +196,10 @@ class AnnotationDriver implements Driver
$mapping['columnName'] = $columnAnnot->name;
}
+ if (isset($columnAnnot->columnDefinition)) {
+ $mapping['columnDefinition'] = $columnAnnot->columnDefinition;
+ }
+
if ($idAnnot = $this->_reader->getPropertyAnnotation($property, 'Doctrine\ORM\Mapping\Id')) {
$mapping['id'] = true;
}
diff --git lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php
index e6a2f32..3d0ae89 100644
--- lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php
+++ lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php
@@ -63,6 +63,7 @@ final class Column extends Annotation {
public $default; //TODO: remove?
public $name;
public $options = array();
+ public $columnDefinition;
}
final class OneToOne extends Annotation {
public $targetEntity;
diff --git lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php
index ba64abf..3c2e9f9 100644
--- lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php
+++ lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php
@@ -154,6 +154,10 @@ class XmlDriver extends AbstractFileDriver
$metadata->setVersionMapping($mapping);
}
+ if (isset($fieldMapping['columnDefinition'])) {
+ $mapping['columnDefinition'] = (string)$fieldMapping['columnDefinition'];
+ }
+
$metadata->mapField($mapping);
}
}
diff --git lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php
index 822bf7b..2e8f949 100644
--- lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php
+++ lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php
@@ -195,6 +195,9 @@ class YamlDriver extends AbstractFileDriver
if (isset($fieldMapping['version']) && $fieldMapping['version']) {
$metadata->setVersionMapping($mapping);
}
+ if (isset($fieldMapping['columnDefinition'])) {
+ $mapping['columnDefinition'] = $fieldMapping['columnDefinition'];
+ }
$metadata->mapField($mapping);
}
diff --git lib/Doctrine/ORM/Tools/SchemaTool.php lib/Doctrine/ORM/Tools/SchemaTool.php
index 6889659..d8e1087 100644
--- lib/Doctrine/ORM/Tools/SchemaTool.php
+++ lib/Doctrine/ORM/Tools/SchemaTool.php
@@ -305,6 +305,10 @@ class SchemaTool
if (isset($mapping['default'])) {
$options['default'] = $mapping['default'];
}
+
+ if (isset($mapping['columnDefinition'])) {
+ $options['columnDefinition'] = $mapping['columnDefinition'];
+ }
if ($table->hasColumn($columnName)) {
// required in some inheritence scenarios
diff --git tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php
index 3d2fe59..75de9d9 100644
--- tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php
+++ tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php
@@ -112,4 +112,22 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
$this->assertEquals($expectedSql, $sql);
}
+
+ public function testGeneratesCustomTypeDeclaration()
+ {
+ $this->assertEquals(
+ 'Foo',
+ $this->_platform->getCustomTypeDeclarationSql(array('columnDefinition' => 'Foo')
+ ));
+ $this->assertEquals(
+ 'Foo',
+ $this->_platform->getCustomTypeDeclarationSql(
+ array('columnDefinition' => 'Foo', 'autoincrement' => true)
+ ));
+ $this->assertEquals(
+ 'Foo',
+ $this->_platform->getCustomTypeDeclarationSql(
+ array('columnDefinition' => 'Foo', 'autoincrement' => true, 'primary' => true)
+ ));
+ }
}
diff --git tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php
index a36a479..e436fb5 100644
--- tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php
+++ tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php
@@ -109,6 +109,24 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
'Long string declaration is not correct'
);
}
+
+ public function testGeneratesCustomTypeDeclaration()
+ {
+ $this->assertEquals(
+ 'Foo',
+ $this->_platform->getCustomTypeDeclarationSql(array('columnDefinition' => 'Foo')
+ ));
+ $this->assertEquals(
+ 'Foo AUTO_INCREMENT',
+ $this->_platform->getCustomTypeDeclarationSql(
+ array('columnDefinition' => 'Foo', 'autoincrement' => true)
+ ));
+ $this->assertEquals(
+ 'Foo AUTO_INCREMENT',
+ $this->_platform->getCustomTypeDeclarationSql(
+ array('columnDefinition' => 'Foo', 'autoincrement' => true, 'primary' => true)
+ ));
+ }
public function testPrefersIdentityColumns()
{
diff --git tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php
index f059750..61a0f71 100644
--- tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php
+++ tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php
@@ -134,6 +134,24 @@ class OraclePlatformTest extends AbstractPlatformTestCase
);
}
+ public function testGeneratesCustomTypeDeclaration()
+ {
+ $this->assertEquals(
+ 'Foo',
+ $this->_platform->getCustomTypeDeclarationSql(array('columnDefinition' => 'Foo')
+ ));
+ $this->assertEquals(
+ 'Foo',
+ $this->_platform->getCustomTypeDeclarationSql(
+ array('columnDefinition' => 'Foo', 'autoincrement' => true)
+ ));
+ $this->assertEquals(
+ 'Foo',
+ $this->_platform->getCustomTypeDeclarationSql(
+ array('columnDefinition' => 'Foo', 'autoincrement' => true, 'primary' => true)
+ ));
+ }
+
public function testPrefersIdentityColumns()
{
$this->assertFalse($this->_platform->prefersIdentityColumns());
diff --git tests/Doctrine/Tests/DBAL/Platforms/SqlitePlatformTest.php tests/Doctrine/Tests/DBAL/Platforms/SqlitePlatformTest.php
index 04f52ea..dc423d9 100644
--- tests/Doctrine/Tests/DBAL/Platforms/SqlitePlatformTest.php
+++ tests/Doctrine/Tests/DBAL/Platforms/SqlitePlatformTest.php
@@ -74,6 +74,24 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
'Long string declaration is not correct'
);
}
+
+ public function testGeneratesCustomTypeDeclaration()
+ {
+ $this->assertEquals(
+ 'Foo',
+ $this->_platform->getCustomTypeDeclarationSql(array('columnDefinition' => 'Foo')
+ ));
+ $this->assertEquals(
+ 'Foo AUTOINCREMENT',
+ $this->_platform->getCustomTypeDeclarationSql(
+ array('columnDefinition' => 'Foo', 'autoincrement' => true)
+ ));
+ $this->assertEquals(
+ 'Foo PRIMARY KEY AUTOINCREMENT',
+ $this->_platform->getCustomTypeDeclarationSql(
+ array('columnDefinition' => 'Foo', 'autoincrement' => true, 'primary' => true)
+ ));
+ }
public function getGenerateIndexSql()
{
diff --git tests/Doctrine/Tests/DBAL/Schema/ColumnTest.php tests/Doctrine/Tests/DBAL/Schema/ColumnTest.php
index 931dace..354e034 100644
--- tests/Doctrine/Tests/DBAL/Schema/ColumnTest.php
+++ tests/Doctrine/Tests/DBAL/Schema/ColumnTest.php
@@ -44,6 +44,7 @@ class ColumnTest extends \PHPUnit_Framework_TestCase
'scale' => 2,
'fixed' => true,
'unsigned' => true,
+ 'columnDefinition' => 'Foo',
'foo' => 'bar',
);
@@ -63,6 +64,7 @@ class ColumnTest extends \PHPUnit_Framework_TestCase
'notnull' => false,
'fixed' => true,
'default' => 'baz',
+ 'columnDefinition' => 'Foo',
'platformOptions' => array('foo' => 'bar'),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment