Commit 6e0bf10e authored by Benjamin Eberlei's avatar Benjamin Eberlei

[DBAL-42] Add support for Oracle and PostgreSQL Doctrine Type detection through comments.

parent 28c7e09d
...@@ -26,6 +26,7 @@ use Doctrine\DBAL\DBALException, ...@@ -26,6 +26,7 @@ use Doctrine\DBAL\DBALException,
Doctrine\DBAL\Schema\Index, Doctrine\DBAL\Schema\Index,
Doctrine\DBAL\Schema\ForeignKeyConstraint, Doctrine\DBAL\Schema\ForeignKeyConstraint,
Doctrine\DBAL\Schema\TableDiff, Doctrine\DBAL\Schema\TableDiff,
Doctrine\DBAL\Schema\Column,
Doctrine\DBAL\Types\Type; Doctrine\DBAL\Types\Type;
/** /**
...@@ -268,6 +269,21 @@ abstract class AbstractPlatform ...@@ -268,6 +269,21 @@ abstract class AbstractPlatform
return '(DC2Type:' . $doctrineType->getName() . ')'; return '(DC2Type:' . $doctrineType->getName() . ')';
} }
/**
* Return the comment of a passed column modified by potential doctrine type comment hints.
*
* @param Column $column
* @return string
*/
protected function getColumnComment(Column $column)
{
$comment = $column->getComment();
if ($this->isCommentedDoctrineType($column->getType())) {
$comment .= $this->getDoctrineTypeComment($column->getType());
}
return $comment;
}
/** /**
* Gets the character used for identifier quoting. * Gets the character used for identifier quoting.
* *
...@@ -856,10 +872,7 @@ abstract class AbstractPlatform ...@@ -856,10 +872,7 @@ abstract class AbstractPlatform
$columnData['default'] = $column->getDefault(); $columnData['default'] = $column->getDefault();
$columnData['columnDefinition'] = $column->getColumnDefinition(); $columnData['columnDefinition'] = $column->getColumnDefinition();
$columnData['autoincrement'] = $column->getAutoincrement(); $columnData['autoincrement'] = $column->getAutoincrement();
$columnData['comment'] = $column->getComment(); $columnData['comment'] = $this->getColumnComment($column);
if ($this->isCommentedDoctrineType($column->getType())) {
$columnData['comment'] .= $this->getDoctrineTypeComment($column->getType());
}
if(in_array($column->getName(), $options['primary'])) { if(in_array($column->getName(), $options['primary'])) {
$columnData['primary'] = true; $columnData['primary'] = true;
...@@ -879,7 +892,7 @@ abstract class AbstractPlatform ...@@ -879,7 +892,7 @@ abstract class AbstractPlatform
if ($this->supportsCommentOnStatement()) { if ($this->supportsCommentOnStatement()) {
foreach ($table->getColumns() AS $column) { foreach ($table->getColumns() AS $column) {
if ($column->getComment()) { if ($column->getComment()) {
$sql[] = $this->getCommentOnColumnSQL($tableName, $column->getName(), $column->getComment()); $sql[] = $this->getCommentOnColumnSQL($tableName, $column->getName(), $this->getColumnComment($column));
} }
} }
} }
......
...@@ -507,7 +507,7 @@ LEFT JOIN all_cons_columns r_cols ...@@ -507,7 +507,7 @@ LEFT JOIN all_cons_columns r_cols
foreach ($diff->addedColumns AS $column) { foreach ($diff->addedColumns AS $column) {
$fields[] = $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray()); $fields[] = $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
if ($column->getComment()) { if ($column->getComment()) {
$commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $column->getComment()); $commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $this->getColumnComment($column));
} }
} }
if (count($fields)) { if (count($fields)) {
...@@ -519,7 +519,7 @@ LEFT JOIN all_cons_columns r_cols ...@@ -519,7 +519,7 @@ LEFT JOIN all_cons_columns r_cols
$column = $columnDiff->column; $column = $columnDiff->column;
$fields[] = $column->getQuotedName($this). ' ' . $this->getColumnDeclarationSQL('', $column->toArray()); $fields[] = $column->getQuotedName($this). ' ' . $this->getColumnDeclarationSQL('', $column->toArray());
if ($columnDiff->hasChanged('comment') && $column->getComment()) { if ($columnDiff->hasChanged('comment') && $column->getComment()) {
$commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $column->getComment()); $commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $this->getColumnComment($column));
} }
} }
if (count($fields)) { if (count($fields)) {
......
...@@ -269,7 +269,7 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -269,7 +269,7 @@ class PostgreSqlPlatform extends AbstractPlatform
AND pg_attrdef.adnum=a.attnum AND pg_attrdef.adnum=a.attnum
) AS default, ) AS default,
(SELECT pg_description.description (SELECT pg_description.description
FROM pg_description WHERE pg_description.objoid = c.oid FROM pg_description WHERE pg_description.objoid = c.oid AND a.attnum = pg_description.objsubid
) AS comment ) AS comment
FROM pg_attribute a, pg_class c, pg_type t, pg_namespace n FROM pg_attribute a, pg_class c, pg_type t, pg_namespace n
WHERE ".$this->getTableWhereClause($table, 'c', 'n') ." WHERE ".$this->getTableWhereClause($table, 'c', 'n') ."
...@@ -348,10 +348,14 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -348,10 +348,14 @@ class PostgreSqlPlatform extends AbstractPlatform
public function getAlterTableSQL(TableDiff $diff) public function getAlterTableSQL(TableDiff $diff)
{ {
$sql = array(); $sql = array();
$commentsSQL = array();
foreach ($diff->addedColumns as $column) { foreach ($diff->addedColumns as $column) {
$query = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray()); $query = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query; $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
if ($column->getComment()) {
$commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $this->getColumnComment($column));
}
} }
foreach ($diff->removedColumns as $column) { foreach ($diff->removedColumns as $column) {
...@@ -393,8 +397,8 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -393,8 +397,8 @@ class PostgreSqlPlatform extends AbstractPlatform
$sql[] = "ALTER TABLE " . $diff->name . " " . $query; $sql[] = "ALTER TABLE " . $diff->name . " " . $query;
} }
} }
if ($columnDiff->hasChanged('comment')) { if ($columnDiff->hasChanged('comment') && $column->getComment()) {
$sql[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $column->getComment()); $commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $this->getColumnComment($column));
} }
} }
...@@ -406,9 +410,7 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -406,9 +410,7 @@ class PostgreSqlPlatform extends AbstractPlatform
$sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName; $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName;
} }
$sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff)); return array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff), $commentsSQL);
return $sql;
} }
/** /**
......
...@@ -117,6 +117,9 @@ class OracleSchemaManager extends AbstractSchemaManager ...@@ -117,6 +117,9 @@ class OracleSchemaManager extends AbstractSchemaManager
$scale = null; $scale = null;
$type = $this->_platform->getDoctrineTypeMapping($dbType); $type = $this->_platform->getDoctrineTypeMapping($dbType);
$type = $this->extractDoctrineTypeFromComment($tableColumn['comments'], $type);
$tableColumn['comments'] = $this->removeDoctrineTypeFromComment($tableColumn['comments'], $type);
switch ($dbType) { switch ($dbType) {
case 'number': case 'number':
if ($tableColumn['data_precision'] == 20 && $tableColumn['data_scale'] == 0) { if ($tableColumn['data_precision'] == 20 && $tableColumn['data_scale'] == 0) {
......
...@@ -201,7 +201,6 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager ...@@ -201,7 +201,6 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
if ((int) $length <= 0) { if ((int) $length <= 0) {
$length = null; $length = null;
} }
$type = array();
$fixed = null; $fixed = null;
if (!isset($tableColumn['name'])) { if (!isset($tableColumn['name'])) {
...@@ -219,6 +218,9 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager ...@@ -219,6 +218,9 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
} }
$type = $this->_platform->getDoctrineTypeMapping($dbType); $type = $this->_platform->getDoctrineTypeMapping($dbType);
$type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
$tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
switch ($dbType) { switch ($dbType) {
case 'smallint': case 'smallint':
case 'int2': case 'int2':
...@@ -270,16 +272,16 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager ...@@ -270,16 +272,16 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
} }
$options = array( $options = array(
'length' => $length, 'length' => $length,
'notnull' => (bool) $tableColumn['isnotnull'], 'notnull' => (bool) $tableColumn['isnotnull'],
'default' => $tableColumn['default'], 'default' => $tableColumn['default'],
'primary' => (bool) ($tableColumn['pri'] == 't'), 'primary' => (bool) ($tableColumn['pri'] == 't'),
'precision' => $precision, 'precision' => $precision,
'scale' => $scale, 'scale' => $scale,
'fixed' => $fixed, 'fixed' => $fixed,
'unsigned' => false, 'unsigned' => false,
'autoincrement' => $autoincrement, 'autoincrement' => $autoincrement,
'comment' => $tableColumn['comment'], 'comment' => $tableColumn['comment'],
); );
return new Column($tableColumn['field'], \Doctrine\DBAL\Types\Type::getType($type), $options); return new Column($tableColumn['field'], \Doctrine\DBAL\Types\Type::getType($type), $options);
......
...@@ -212,6 +212,7 @@ class PostgreSqlPlatformTest extends AbstractPlatformTestCase ...@@ -212,6 +212,7 @@ class PostgreSqlPlatformTest extends AbstractPlatformTestCase
{ {
return array( return array(
"ALTER TABLE mytable ADD quota INT NOT NULL", "ALTER TABLE mytable ADD quota INT NOT NULL",
"COMMENT ON COLUMN mytable.quota IS 'A comment'",
"COMMENT ON COLUMN mytable.baz IS 'B comment'", "COMMENT ON COLUMN mytable.baz IS 'B comment'",
); );
} }
......
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