Commit 4b20cd73 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 0045a7ac
......@@ -26,6 +26,7 @@ use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types\Type;
class DB2Platform extends AbstractPlatform
{
......@@ -78,6 +79,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}
*/
......@@ -272,6 +287,7 @@ class DB2Platform extends AbstractPlatform
c.scale,
c.identity,
tc.type AS tabconsttype,
c.remarks AS comment,
k.colseq,
CASE
WHEN c.generated = 'D' THEN 1
......@@ -418,6 +434,14 @@ class DB2Platform extends AbstractPlatform
return false;
}
/**
* {@inheritdoc}
*/
public function supportsCommentOnStatement()
{
return true;
}
/**
* {@inheritDoc}
*/
......
......@@ -65,6 +65,11 @@ class DB2SchemaManager extends AbstractSchemaManager
$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'])) {
case 'varchar':
$length = $tableColumn['length'];
......@@ -94,6 +99,9 @@ class DB2SchemaManager extends AbstractSchemaManager
'notnull' => (bool) ($tableColumn['nulls'] == 'N'),
'scale' => null,
'precision' => null,
'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
? $tableColumn['comment']
: null,
'platformOptions' => array(),
);
......
......@@ -2,9 +2,27 @@
namespace Doctrine\Tests\DBAL\Functional\Schema;
require_once __DIR__ . '/../../../TestInit.php';
use Doctrine\DBAL\Schema\Table;
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
$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()
{
$table = new Table('test');
......@@ -667,6 +695,19 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
return true;
}
public function testSupportsCommentOnStatement()
{
$this->assertSame($this->supportsCommentOnStatement(), $this->_platform->supportsCommentOnStatement());
}
/**
* @return bool
*/
protected function supportsCommentOnStatement()
{
return false;
}
/**
* @expectedException \Doctrine\DBAL\DBALException
*/
......
......@@ -227,6 +227,14 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa
$this->assertTrue($this->_platform->supportsSequences());
}
/**
* {@inheritdoc}
*/
protected function supportsCommentOnStatement()
{
return true;
}
public function testModifyLimitQuery()
{
$sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
......
......@@ -115,6 +115,7 @@ class DB2PlatformTest extends AbstractPlatformTestCase
{
return array(
"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
{
return array(
'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
$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()
{
$this->assertEquals("CREATE DATABASE foobar", $this->_platform->getCreateDatabaseSQL('foobar'));
......@@ -656,6 +667,14 @@ class DB2PlatformTest extends AbstractPlatformTestCase
return false;
}
/**
* {@inheritdoc}
*/
protected function supportsCommentOnStatement()
{
return true;
}
/**
* {@inheritdoc}
*/
......
......@@ -189,6 +189,14 @@ class OraclePlatformTest extends AbstractPlatformTestCase
$this->assertTrue($this->_platform->supportsSavepoints());
}
/**
* {@inheritdoc}
*/
protected function supportsCommentOnStatement()
{
return true;
}
public function getGenerateIndexSql()
{
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