Commit b0f42d54 authored by Benjamin Eberlei's avatar Benjamin Eberlei

[DBAL-42] Add support to reverse engineer Comments from MySQL, PostgreSQL and...

[DBAL-42] Add support to reverse engineer Comments from MySQL, PostgreSQL and ORacle into Doctrine\DBAL\Schema\Column instances and added comment comparison into Comparator.
parent 96fffa83
......@@ -826,7 +826,7 @@ abstract class AbstractPlatform
public function getCommentOnColumnSQL($tableName, $columnName, $comment)
{
return "COMMENT ON " . $tableName . "." . $columnName . " IS '" . $comment . "'";
return "COMMENT ON COLUMN " . $tableName . "." . $columnName . " IS '" . $comment . "'";
}
/**
......@@ -1634,7 +1634,7 @@ abstract class AbstractPlatform
throw DBALException::notSupported(__METHOD__);
}
public function getListTableColumnsSQL($table)
public function getListTableColumnsSQL($table, $database = null)
{
throw DBALException::notSupported(__METHOD__);
}
......
......@@ -208,7 +208,7 @@ class DB2Platform extends AbstractPlatform
* @param string $table
* @return string
*/
public function getListTableColumnsSQL($table)
public function getListTableColumnsSQL($table, $database = null)
{
return "SELECT DISTINCT c.tabschema, c.tabname, c.colname, c.colno,
c.typename, c.default, c.nulls, c.length, c.scale,
......
......@@ -311,7 +311,7 @@ class MsSqlPlatform extends AbstractPlatform
/**
* @override
*/
public function getListTableColumnsSQL($table)
public function getListTableColumnsSQL($table, $database = null)
{
return 'exec sp_columns @table_name = ' . $table;
}
......
......@@ -274,10 +274,17 @@ class MySqlPlatform extends AbstractPlatform
return "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'";
}
public function getListTableColumnsSQL($table)
public function getListTableColumnsSQL($table, $database = null)
{
if ($database) {
return "SELECT COLUMN_NAME AS Field, COLUMN_TYPE AS Type, IS_NULLABLE AS `Null`, ".
"COLUMN_KEY AS `Key`, COLUMN_DEFAULT AS `Default`, EXTRA AS Extra, COLUMN_COMMENT AS Comment, " .
"CHARACTER_SET_NAME AS CharacterSet, COLLATION_NAME AS CollactionName ".
"FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '" . $database . "' AND TABLE_NAME = '" . $table . "'";
} else {
return 'DESCRIBE ' . $table;
}
}
/**
* create a new database
......
......@@ -441,10 +441,12 @@ LEFT JOIN all_cons_columns r_cols
return 'SELECT * FROM user_constraints WHERE table_name = \'' . $table . '\'';
}
public function getListTableColumnsSQL($table)
public function getListTableColumnsSQL($table, $database = null)
{
$table = strtoupper($table);
return "SELECT * FROM all_tab_columns WHERE table_name = '" . $table . "' ORDER BY column_name";
return "SELECT c.*, d.comments FROM all_tab_columns c ".
"INNER JOIN all_col_comments d ON d.OWNER = c.OWNER AND d.TABLE_NAME = c.TABLE_NAME AND d.COLUMN_NAME = c.COLUMN_NAME ".
"WHERE c.table_name = '" . $table . "' ORDER BY c.column_name";
}
/**
......
......@@ -246,7 +246,7 @@ class PostgreSqlPlatform extends AbstractPlatform
return $whereClause;
}
public function getListTableColumnsSQL($table)
public function getListTableColumnsSQL($table, $database = null)
{
return "SELECT
a.attnum,
......@@ -267,7 +267,10 @@ class PostgreSqlPlatform extends AbstractPlatform
FROM pg_attrdef
WHERE c.oid = pg_attrdef.adrelid
AND pg_attrdef.adnum=a.attnum
) AS default
) AS default,
(SELECT pg_description.description
FROM pg_description WHERE pg_description.objoid = c.oid
) AS comment
FROM pg_attribute a, pg_class c, pg_type t, pg_namespace n
WHERE ".$this->getTableWhereClause($table, 'c', 'n') ."
AND a.attnum > 0
......
......@@ -326,7 +326,7 @@ class SqlitePlatform extends AbstractPlatform
return "SELECT sql FROM sqlite_master WHERE type='index' AND tbl_name = '$table' AND sql NOT NULL ORDER BY name";
}
public function getListTableColumnsSQL($table)
public function getListTableColumnsSQL($table, $database = null)
{
return "PRAGMA table_info($table)";
}
......
......@@ -141,11 +141,16 @@ abstract class AbstractSchemaManager
* in the platformDetails array.
*
* @param string $table The name of the table.
* @param string $database
* @return Column[]
*/
public function listTableColumns($table)
public function listTableColumns($table, $database = null)
{
$sql = $this->_platform->getListTableColumnsSQL($table);
if (!$database) {
$database = $this->_conn->getDatabase();
}
$sql = $this->_platform->getListTableColumnsSQL($table, $database);
$tableColumns = $this->_conn->fetchAll($sql);
......
......@@ -344,6 +344,10 @@ class Comparator
$changedProperties[] = 'autoincrement';
}
if ($column1->getComment() != $column2->getComment()) {
$changedProperties[] = 'comment';
}
return $changedProperties;
}
......
......@@ -156,6 +156,7 @@ class MySqlSchemaManager extends AbstractSchemaManager
'scale' => null,
'precision' => null,
'autoincrement' => (bool) (strpos($tableColumn['extra'], 'auto_increment') !== false),
'comment' => (isset($tableColumn['comment'])) ? $tableColumn['comment'] : null
);
if ($scale !== null && $precision !== null) {
......
......@@ -186,6 +186,7 @@ class OracleSchemaManager extends AbstractSchemaManager
'length' => $length,
'precision' => $precision,
'scale' => $scale,
'comment' => (isset($tableColumn['comments'])) ? $tableColumn['comments'] : null,
'platformDetails' => array(),
);
......
......@@ -279,6 +279,7 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
'fixed' => $fixed,
'unsigned' => false,
'autoincrement' => $autoincrement,
'comment' => $tableColumn['comment'],
);
return new Column($tableColumn['field'], \Doctrine\DBAL\Types\Type::getType($type), $options);
......
......@@ -397,10 +397,32 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
$inferredTable = $this->_sm->listTableDetails('test_autoincrement');
$this->assertTrue($inferredTable->hasColumn('id'));
$this->assertTrue($inferredTable->getColumn('id')->getAutoincrement());
}
/**
* @group DBAL-42
*/
public function testGetColumnComment()
{
if (!$this->_conn->getDatabasePlatform()->supportsInlineColumnComments() && !$this->_conn->getDatabasePlatform()->supportsCommentOnStatement()) {
$this->markTestSkipped('Database does not support column comments.');
}
$table = new \Doctrine\DBAL\Schema\Table('test');
$table->addColumn('id', 'integer', array('comment' => 'This is a comment'));
$table->setPrimaryKey(array('id'));
$this->_sm->createTable($table);
$columns = $this->_sm->listTableColumns("test");
$this->assertEquals(1, count($columns));
$this->assertEquals('This is a comment', $columns['id']->getComment());
}
/**
* @param string $name
* @param array $data
*/
protected function createTestTable($name = 'test_table', $data = array())
{
$options = array();
......
......@@ -191,7 +191,7 @@ class OraclePlatformTest extends AbstractPlatformTestCase
{
return array(
"CREATE TABLE test (id NUMBER(10) NOT NULL, PRIMARY KEY(id))",
"COMMENT ON test.id IS 'This is a comment'",
"COMMENT ON COLUMN test.id IS 'This is a comment'",
);
}
......@@ -200,8 +200,8 @@ class OraclePlatformTest extends AbstractPlatformTestCase
return array(
"ALTER TABLE mytable ADD (quota NUMBER(10) NOT NULL)",
"ALTER TABLE mytable MODIFY (baz VARCHAR2(255) NOT NULL)",
"COMMENT ON mytable.quota IS 'A comment'",
"COMMENT ON mytable.baz IS 'B comment'",
"COMMENT ON COLUMN mytable.quota IS 'A comment'",
"COMMENT ON COLUMN mytable.baz IS 'B comment'",
);
}
}
\ No newline at end of file
......@@ -204,7 +204,7 @@ class PostgreSqlPlatformTest extends AbstractPlatformTestCase
{
return array(
"CREATE TABLE test (id INT NOT NULL, PRIMARY KEY(id))",
"COMMENT ON test.id IS 'This is a comment'",
"COMMENT ON COLUMN test.id IS 'This is a comment'",
);
}
......@@ -212,7 +212,7 @@ class PostgreSqlPlatformTest extends AbstractPlatformTestCase
{
return array(
"ALTER TABLE mytable ADD quota INT NOT NULL",
"COMMENT ON mytable.baz IS 'B comment'",
"COMMENT ON COLUMN mytable.baz IS 'B comment'",
);
}
}
\ 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