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