Commit e3ba4ba0 authored by Steve Müller's avatar Steve Müller Committed by Marco Pivetta

fix reverse engineering boolean type columns on DB2

- fixes platform's COMMENT ON statement support
- implements reverse engineering for column comments
- marks boolean column type commented for distinction

fixes #2182
parent a6d3d162
...@@ -25,6 +25,7 @@ use Doctrine\DBAL\Schema\Identifier; ...@@ -25,6 +25,7 @@ use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\Index; use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff; use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types\Type;
class DB2Platform extends AbstractPlatform class DB2Platform extends AbstractPlatform
{ {
...@@ -77,6 +78,20 @@ class DB2Platform extends AbstractPlatform ...@@ -77,6 +78,20 @@ class DB2Platform extends AbstractPlatform
); );
} }
/**
* {@inheritdoc}
*/
public function isCommentedDoctrineType(Type $doctrineType)
{
if ($doctrineType->getName() === Type::BOOLEAN) {
// We require a commented boolean type in order to distinguish between boolean and smallint
// as both (have to) map to the same native type.
return true;
}
return parent::isCommentedDoctrineType($doctrineType);
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
...@@ -271,6 +286,7 @@ class DB2Platform extends AbstractPlatform ...@@ -271,6 +286,7 @@ class DB2Platform extends AbstractPlatform
c.scale, c.scale,
c.identity, c.identity,
tc.type AS tabconsttype, tc.type AS tabconsttype,
c.remarks AS comment,
k.colseq, k.colseq,
CASE CASE
WHEN c.generated = 'D' THEN 1 WHEN c.generated = 'D' THEN 1
...@@ -417,6 +433,14 @@ class DB2Platform extends AbstractPlatform ...@@ -417,6 +433,14 @@ class DB2Platform extends AbstractPlatform
return false; return false;
} }
/**
* {@inheritdoc}
*/
public function supportsCommentOnStatement()
{
return true;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
......
...@@ -65,6 +65,11 @@ class DB2SchemaManager extends AbstractSchemaManager ...@@ -65,6 +65,11 @@ class DB2SchemaManager extends AbstractSchemaManager
$type = $this->_platform->getDoctrineTypeMapping($tableColumn['typename']); $type = $this->_platform->getDoctrineTypeMapping($tableColumn['typename']);
if (isset($tableColumn['comment'])) {
$type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
$tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
}
switch (strtolower($tableColumn['typename'])) { switch (strtolower($tableColumn['typename'])) {
case 'varchar': case 'varchar':
$length = $tableColumn['length']; $length = $tableColumn['length'];
...@@ -94,6 +99,9 @@ class DB2SchemaManager extends AbstractSchemaManager ...@@ -94,6 +99,9 @@ class DB2SchemaManager extends AbstractSchemaManager
'notnull' => (bool) ($tableColumn['nulls'] == 'N'), 'notnull' => (bool) ($tableColumn['nulls'] == 'N'),
'scale' => null, 'scale' => null,
'precision' => null, 'precision' => null,
'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
? $tableColumn['comment']
: null,
'platformOptions' => array(), 'platformOptions' => array(),
); );
......
...@@ -2,7 +2,27 @@ ...@@ -2,7 +2,27 @@
namespace Doctrine\Tests\DBAL\Functional\Schema; namespace Doctrine\Tests\DBAL\Functional\Schema;
use Doctrine\DBAL\Schema\Table;
class Db2SchemaManagerTest extends SchemaManagerFunctionalTestCase class Db2SchemaManagerTest extends SchemaManagerFunctionalTestCase
{ {
/**
* @group DBAL-939
*/
public function testGetBooleanColumn()
{
$table = new Table('boolean_column_test');
$table->addColumn('bool', 'boolean');
$table->addColumn('bool_commented', 'boolean', array('comment' => "That's a comment"));
$this->_sm->createTable($table);
$columns = $this->_sm->listTableColumns('boolean_column_test');
$this->assertInstanceOf('Doctrine\DBAL\Types\BooleanType', $columns['bool']->getType());
$this->assertInstanceOf('Doctrine\DBAL\Types\BooleanType', $columns['bool_commented']->getType());
$this->assertNull($columns['bool']->getComment());
$this->assertSame("That's a comment", $columns['bool_commented']->getComment());
}
} }
...@@ -107,6 +107,34 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase ...@@ -107,6 +107,34 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
$this->_platform->registerDoctrineTypeMapping('foo', 'bar'); $this->_platform->registerDoctrineTypeMapping('foo', 'bar');
} }
/**
* @group DBAL-939
*
* @dataProvider getIsCommentedDoctrineType
*/
public function testIsCommentedDoctrineType(Type $type, $commented)
{
$this->assertSame($commented, $this->_platform->isCommentedDoctrineType($type));
}
public function getIsCommentedDoctrineType()
{
$this->setUp();
$data = array();
foreach (Type::getTypesMap() as $typeName => $className) {
$type = Type::getType($typeName);
$data[$typeName] = array(
$type,
$type->requiresSQLCommentHint($this->_platform),
);
}
return $data;
}
public function testCreateWithNoColumns() public function testCreateWithNoColumns()
{ {
$table = new Table('test'); $table = new Table('test');
...@@ -673,6 +701,19 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase ...@@ -673,6 +701,19 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
return true; return true;
} }
public function testSupportsCommentOnStatement()
{
$this->assertSame($this->supportsCommentOnStatement(), $this->_platform->supportsCommentOnStatement());
}
/**
* @return bool
*/
protected function supportsCommentOnStatement()
{
return false;
}
/** /**
* @expectedException \Doctrine\DBAL\DBALException * @expectedException \Doctrine\DBAL\DBALException
*/ */
......
...@@ -227,6 +227,14 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa ...@@ -227,6 +227,14 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa
$this->assertTrue($this->_platform->supportsSequences()); $this->assertTrue($this->_platform->supportsSequences());
} }
/**
* {@inheritdoc}
*/
protected function supportsCommentOnStatement()
{
return true;
}
public function testModifyLimitQuery() public function testModifyLimitQuery()
{ {
$sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0); $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
......
...@@ -115,6 +115,7 @@ class DB2PlatformTest extends AbstractPlatformTestCase ...@@ -115,6 +115,7 @@ class DB2PlatformTest extends AbstractPlatformTestCase
{ {
return array( return array(
"CREATE TABLE test (id INTEGER NOT NULL, PRIMARY KEY(id))", "CREATE TABLE test (id INTEGER NOT NULL, PRIMARY KEY(id))",
"COMMENT ON COLUMN test.id IS 'This is a comment'",
); );
} }
...@@ -134,6 +135,7 @@ class DB2PlatformTest extends AbstractPlatformTestCase ...@@ -134,6 +135,7 @@ class DB2PlatformTest extends AbstractPlatformTestCase
{ {
return array( return array(
'CREATE TABLE test (id INTEGER NOT NULL, "data" CLOB(1M) NOT NULL, PRIMARY KEY(id))', 'CREATE TABLE test (id INTEGER NOT NULL, "data" CLOB(1M) NOT NULL, PRIMARY KEY(id))',
'COMMENT ON COLUMN test."data" IS \'(DC2Type:array)\'',
); );
} }
...@@ -285,6 +287,15 @@ class DB2PlatformTest extends AbstractPlatformTestCase ...@@ -285,6 +287,15 @@ class DB2PlatformTest extends AbstractPlatformTestCase
$this->assertSame('datetime', $this->_platform->getDoctrineTypeMapping('timestamp')); $this->assertSame('datetime', $this->_platform->getDoctrineTypeMapping('timestamp'));
} }
public function getIsCommentedDoctrineType()
{
$data = parent::getIsCommentedDoctrineType();
$data[Type::BOOLEAN] = array(Type::getType(Type::BOOLEAN), true);
return $data;
}
public function testGeneratesDDLSnippets() public function testGeneratesDDLSnippets()
{ {
$this->assertEquals("CREATE DATABASE foobar", $this->_platform->getCreateDatabaseSQL('foobar')); $this->assertEquals("CREATE DATABASE foobar", $this->_platform->getCreateDatabaseSQL('foobar'));
...@@ -656,6 +667,14 @@ class DB2PlatformTest extends AbstractPlatformTestCase ...@@ -656,6 +667,14 @@ class DB2PlatformTest extends AbstractPlatformTestCase
return false; return false;
} }
/**
* {@inheritdoc}
*/
protected function supportsCommentOnStatement()
{
return true;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -187,6 +187,14 @@ class OraclePlatformTest extends AbstractPlatformTestCase ...@@ -187,6 +187,14 @@ class OraclePlatformTest extends AbstractPlatformTestCase
$this->assertTrue($this->_platform->supportsSavepoints()); $this->assertTrue($this->_platform->supportsSavepoints());
} }
/**
* {@inheritdoc}
*/
protected function supportsCommentOnStatement()
{
return true;
}
public function getGenerateIndexSql() public function getGenerateIndexSql()
{ {
return 'CREATE INDEX my_idx ON mytable (user_name, last_login)'; return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
......
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