Commit 9ad13c47 authored by guilhermeblanco's avatar guilhermeblanco

[2.0][DDC-236] Enhanced unique constraints to support names. Fixed general...

[2.0][DDC-236] Enhanced unique constraints to support names. Fixed general issues on XML and YAML exporters. Fixed issues on XML, YAML, Doctrine 1.X and Annotation drivers.
parent d8a1c5c5
...@@ -617,8 +617,8 @@ abstract class AbstractPlatform ...@@ -617,8 +617,8 @@ abstract class AbstractPlatform
$columnListSql = $this->getColumnDeclarationListSql($columns); $columnListSql = $this->getColumnDeclarationListSql($columns);
if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) { if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
foreach ($options['uniqueConstraints'] as $uniqueConstraint) { foreach ($options['uniqueConstraints'] as $name => $definition) {
$columnListSql .= ', UNIQUE(' . implode(', ', array_values($uniqueConstraint)) . ')'; $columnListSql .= ', ' . $this->getUniqueConstraintDeclarationSql($name, $definition);
} }
} }
...@@ -1035,32 +1035,50 @@ abstract class AbstractPlatform ...@@ -1035,32 +1035,50 @@ abstract class AbstractPlatform
return implode(', ', $constraints); return implode(', ', $constraints);
} }
/**
* Obtain DBMS specific SQL code portion needed to set a unique
* constraint declaration to be used in statements like CREATE TABLE.
*
* @param string $name name of the unique constraint
* @param Index $index index definition
* @return string DBMS specific SQL code portion needed
* to set a constraint
*/
public function getUniqueConstraintDeclarationSql($name, Index $index)
{
if (count($index->getColumns()) == 0) {
throw \InvalidArgumentException("Incomplete definition. 'columns' required.");
}
return 'CONSTRAINT' . $name . ' UNIQUE ('
. $this->getIndexFieldDeclarationListSql($index->getColumns())
. ')';
}
/** /**
* Obtain DBMS specific SQL code portion needed to set an index * Obtain DBMS specific SQL code portion needed to set an index
* declaration to be used in statements like CREATE TABLE. * declaration to be used in statements like CREATE TABLE.
* *
* @param string $name name of the index * @param string $name name of the index
* @param Index $index index definition * @param Index $index index definition
* @return string DBMS specific SQL code portion needed to set an index * @return string DBMS specific SQL code portion needed to set an index
*/ */
public function getIndexDeclarationSql($name, Index $index) public function getIndexDeclarationSql($name, Index $index)
{ {
$type = ''; $type = '';
if($index->isUnique()) { if($index->isUnique()) {
$type = "UNIQUE"; $type = 'UNIQUE ';
} }
if (count($index->getColumns()) == 0) { if (count($index->getColumns()) == 0) {
throw \InvalidArgumentException("Incomplete definition. 'columns' required."); throw \InvalidArgumentException("Incomplete definition. 'columns' required.");
} }
$query = $type . ' INDEX ' . $name; return $type . 'INDEX ' . $name . ' ('
. $this->getIndexFieldDeclarationListSql($index->getColumns())
$query .= ' (' . $this->getIndexFieldDeclarationListSql($index->getColumns()) . ')'; . ')';
return $query;
} }
/** /**
......
...@@ -446,8 +446,8 @@ class MySqlPlatform extends AbstractPlatform ...@@ -446,8 +446,8 @@ class MySqlPlatform extends AbstractPlatform
$queryFields = $this->getColumnDeclarationListSql($columns); $queryFields = $this->getColumnDeclarationListSql($columns);
if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) { if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
foreach ($options['uniqueConstraints'] as $uniqueConstraint) { foreach ($options['uniqueConstraints'] as $index => $definition) {
$queryFields .= ', UNIQUE(' . implode(', ', array_values($uniqueConstraint)) . ')'; $queryFields .= ', ' . $this->getUniqueIndexDeclarationSql($index, $definition);
} }
} }
......
...@@ -146,9 +146,9 @@ class Table extends AbstractAsset ...@@ -146,9 +146,9 @@ class Table extends AbstractAsset
* @param string $indexName * @param string $indexName
* @return Table * @return Table
*/ */
public function setPrimaryKey(array $columns, $indexName=false) public function setPrimaryKey(array $columns, $indexName = false)
{ {
return $this->_createIndex($columns, $indexName?:"primary", true, true); return $this->_createIndex($columns, $indexName ?: "primary", true, true);
} }
/** /**
...@@ -166,7 +166,7 @@ class Table extends AbstractAsset ...@@ -166,7 +166,7 @@ class Table extends AbstractAsset
* @param string $indexName * @param string $indexName
* @return Table * @return Table
*/ */
public function addIndex(array $columnNames, $indexName=null) public function addIndex(array $columnNames, $indexName = null)
{ {
if($indexName == null) { if($indexName == null) {
$indexName = $this->_generateIdentifierName( $indexName = $this->_generateIdentifierName(
...@@ -183,7 +183,7 @@ class Table extends AbstractAsset ...@@ -183,7 +183,7 @@ class Table extends AbstractAsset
* @param string $indexName * @param string $indexName
* @return Table * @return Table
*/ */
public function addUniqueIndex(array $columnNames, $indexName=null) public function addUniqueIndex(array $columnNames, $indexName = null)
{ {
if ($indexName == null) { if ($indexName == null) {
$indexName = $this->_generateIdentifierName( $indexName = $this->_generateIdentifierName(
......
...@@ -92,13 +92,17 @@ class AnnotationDriver implements Driver ...@@ -92,13 +92,17 @@ class AnnotationDriver implements Driver
if ($tableAnnot->indexes !== null) { if ($tableAnnot->indexes !== null) {
foreach ($tableAnnot->indexes as $indexAnnot) { foreach ($tableAnnot->indexes as $indexAnnot) {
$primaryTable['indexes'][$indexAnnot->name] = array('columns' => $indexAnnot->columns); $primaryTable['indexes'][$indexAnnot->name] = array(
'columns' => $indexAnnot->columns
);
} }
} }
if ($tableAnnot->uniqueConstraints !== null) { if ($tableAnnot->uniqueConstraints !== null) {
foreach ($tableAnnot->uniqueConstraints as $uniqueConstraint) { foreach ($tableAnnot->uniqueConstraints as $uniqueConstraint) {
$primaryTable['uniqueConstraints'][] = $uniqueConstraint->columns; $primaryTable['uniqueConstraints'][$uniqueConstraint->name] = array(
'columns' => $uniqueConstraint->columns
);
} }
} }
......
...@@ -101,6 +101,7 @@ class XmlDriver extends AbstractFileDriver ...@@ -101,6 +101,7 @@ class XmlDriver extends AbstractFileDriver
} else { } else {
$columns = $index['columns']; $columns = $index['columns'];
} }
$metadata->primaryTable['indexes'][$index['name']] = array( $metadata->primaryTable['indexes'][$index['name']] = array(
'columns' => $columns 'columns' => $columns
); );
...@@ -115,7 +116,10 @@ class XmlDriver extends AbstractFileDriver ...@@ -115,7 +116,10 @@ class XmlDriver extends AbstractFileDriver
} else { } else {
$columns = $unique['columns']; $columns = $unique['columns'];
} }
$metadata->primaryTable['uniqueConstraints'][] = $columns;
$metadata->primaryTable['uniqueConstraints'][$unique['name']] = array(
'columns' => $columns
);
} }
} }
......
...@@ -101,11 +101,13 @@ class YamlDriver extends AbstractFileDriver ...@@ -101,11 +101,13 @@ class YamlDriver extends AbstractFileDriver
if ( ! isset($index['name'])) { if ( ! isset($index['name'])) {
$index['name'] = $name; $index['name'] = $name;
} }
if (is_string($index['columns'])) { if (is_string($index['columns'])) {
$columns = explode(',', $index['columns']); $columns = explode(',', $index['columns']);
} else { } else {
$columns = $index['columns']; $columns = $index['columns'];
} }
$metadata->primaryTable['indexes'][$index['name']] = array( $metadata->primaryTable['indexes'][$index['name']] = array(
'columns' => $columns 'columns' => $columns
); );
...@@ -114,13 +116,20 @@ class YamlDriver extends AbstractFileDriver ...@@ -114,13 +116,20 @@ class YamlDriver extends AbstractFileDriver
// Evaluate uniqueConstraints // Evaluate uniqueConstraints
if (isset($element['uniqueConstraints'])) { if (isset($element['uniqueConstraints'])) {
foreach ($element['uniqueConstraints'] as $unique) { foreach ($element['uniqueConstraints'] as $name => $unique) {
if (is_string($index['columns'])) { if ( ! isset($unique['name'])) {
$unique['name'] = $name;
}
if (is_string($unique['columns'])) {
$columns = explode(',', $unique['columns']); $columns = explode(',', $unique['columns']);
} else { } else {
$columns = $unique['columns']; $columns = $unique['columns'];
} }
$metadata->primaryTable['uniqueConstraints'][] = $columns;
$metadata->primaryTable['uniqueConstraints'][$unique['name']] = array(
'columns' => $columns
);
} }
} }
......
...@@ -213,13 +213,12 @@ class ConvertDoctrine1Schema ...@@ -213,13 +213,12 @@ class ConvertDoctrine1Schema
{ {
if (isset($model['indexes']) && $model['indexes']) { if (isset($model['indexes']) && $model['indexes']) {
foreach ($model['indexes'] as $name => $index) { foreach ($model['indexes'] as $name => $index) {
$metadata->primaryTable['indexes'][$name] = array( $type = (isset($index['type']) && $index['type'] == 'unique')
? 'uniqueConstraints' : 'indexes';
$metadata->primaryTable[$type][$name] = array(
'columns' => $index['fields'] 'columns' => $index['fields']
); );
if (isset($index['type']) && $index['type'] == 'unique') {
$metadata->primaryTable['uniqueConstraints'][] = $index['fields'];
}
} }
} }
} }
......
...@@ -99,8 +99,9 @@ class XmlExporter extends AbstractExporter ...@@ -99,8 +99,9 @@ class XmlExporter extends AbstractExporter
if (isset($metadata->primaryTable['indexes'])) { if (isset($metadata->primaryTable['indexes'])) {
$indexesXml = $root->addChild('indexes'); $indexesXml = $root->addChild('indexes');
foreach ($metadata->primaryTable['indexes'] as $name => $index) { foreach ($metadata->primaryTable['indexes'] as $name => $index) {
$indexXml = $root->addChild('index'); $indexXml = $indexesXml->addChild('index');
$indexXml->addAttribute('name', $name); $indexXml->addAttribute('name', $name);
$indexXml->addAttribute('columns', implode(',', $index['columns'])); $indexXml->addAttribute('columns', implode(',', $index['columns']));
} }
...@@ -108,9 +109,11 @@ class XmlExporter extends AbstractExporter ...@@ -108,9 +109,11 @@ class XmlExporter extends AbstractExporter
if (isset($metadata->primaryTable['uniqueConstraints'])) { if (isset($metadata->primaryTable['uniqueConstraints'])) {
$uniqueConstraintsXml = $root->addChild('unique-constraints'); $uniqueConstraintsXml = $root->addChild('unique-constraints');
foreach ($metadata->primaryTable['uniqueConstraints'] as $uniqueConstraint) {
foreach ($metadata->primaryTable['uniqueConstraints'] as $unique) {
$uniqueConstraintXml = $uniqueConstraintsXml->addChild('unique-constraint'); $uniqueConstraintXml = $uniqueConstraintsXml->addChild('unique-constraint');
$uniqueConstraintXml->addAttribute('columns', $uniqueConstraint['columns']); $uniqueConstraintXml->addAttribute('name', $name);
$uniqueConstraintXml->addAttribute('columns', implode(',', $unique['columns']));
} }
} }
......
...@@ -92,9 +92,7 @@ class YamlExporter extends AbstractExporter ...@@ -92,9 +92,7 @@ class YamlExporter extends AbstractExporter
} }
if (isset($metadata->primaryTable['uniqueConstraints'])) { if (isset($metadata->primaryTable['uniqueConstraints'])) {
foreach ($metadata->primaryTable['uniqueConstraints'] as $uniqueConstraint) { $array['uniqueConstraints'] = $metadata->primaryTable['uniqueConstraints'];
$array['uniqueConstraints'][]['columns'] = $uniqueConstraint;
}
} }
$fieldMappings = $metadata->fieldMappings; $fieldMappings = $metadata->fieldMappings;
......
...@@ -201,7 +201,7 @@ class SchemaTool ...@@ -201,7 +201,7 @@ class SchemaTool
if (isset($class->primaryTable['uniqueConstraints'])) { if (isset($class->primaryTable['uniqueConstraints'])) {
foreach ($class->primaryTable['uniqueConstraints'] AS $indexName => $indexData) { foreach ($class->primaryTable['uniqueConstraints'] AS $indexName => $indexData) {
$table->addUniqueIndex($indexData, $indexName); $table->addUniqueIndex($indexData['columns'], $indexName);
} }
} }
...@@ -354,8 +354,7 @@ class SchemaTool ...@@ -354,8 +354,7 @@ class SchemaTool
$theJoinTable = $schema->createTable($mapping->getQuotedJoinTableName($this->_platform)); $theJoinTable = $schema->createTable($mapping->getQuotedJoinTableName($this->_platform));
$primaryKeyColumns = array(); $primaryKeyColumns = $uniqueConstraints = array();
$uniqueConstraints = array();
// Build first FK constraint (relation table => source table) // Build first FK constraint (relation table => source table)
$this->_gatherRelationJoinColumns($joinTable['joinColumns'], $theJoinTable, $class, $mapping, $primaryKeyColumns, $uniqueConstraints); $this->_gatherRelationJoinColumns($joinTable['joinColumns'], $theJoinTable, $class, $mapping, $primaryKeyColumns, $uniqueConstraints);
...@@ -363,8 +362,10 @@ class SchemaTool ...@@ -363,8 +362,10 @@ class SchemaTool
// Build second FK constraint (relation table => target table) // Build second FK constraint (relation table => target table)
$this->_gatherRelationJoinColumns($joinTable['inverseJoinColumns'], $theJoinTable, $foreignClass, $mapping, $primaryKeyColumns, $uniqueConstraints); $this->_gatherRelationJoinColumns($joinTable['inverseJoinColumns'], $theJoinTable, $foreignClass, $mapping, $primaryKeyColumns, $uniqueConstraints);
foreach($uniqueConstraints AS $unique) { foreach($uniqueConstraints AS $indexName => $unique) {
$theJoinTable->addUniqueIndex($unique); $theJoinTable->addUniqueIndex(
$unique['columns'], is_numeric($indexName) ? null : $indexName
);
} }
$theJoinTable->setPrimaryKey($primaryKeyColumns); $theJoinTable->setPrimaryKey($primaryKeyColumns);
...@@ -413,7 +414,7 @@ class SchemaTool ...@@ -413,7 +414,7 @@ class SchemaTool
} }
if (isset($joinColumn['unique']) && $joinColumn['unique'] == true) { if (isset($joinColumn['unique']) && $joinColumn['unique'] == true) {
$uniqueConstraints[] = array($columnName); $uniqueConstraints[] = array('columns' => $columnName);
} }
if (isset($joinColumn['onUpdate'])) { if (isset($joinColumn['onUpdate'])) {
......
...@@ -65,7 +65,7 @@ class ConvertDoctrine1SchemaTest extends \Doctrine\Tests\OrmTestCase ...@@ -65,7 +65,7 @@ class ConvertDoctrine1SchemaTest extends \Doctrine\Tests\OrmTestCase
$this->assertEquals('Profile', $metadatas['Profile']->associationMappings['User']->sourceEntityName); $this->assertEquals('Profile', $metadatas['Profile']->associationMappings['User']->sourceEntityName);
$this->assertEquals('User', $metadatas['Profile']->associationMappings['User']->targetEntityName); $this->assertEquals('User', $metadatas['Profile']->associationMappings['User']->targetEntityName);
$this->assertEquals('username', $metadatas['User']->primaryTable['indexes']['username']['columns'][0]); $this->assertEquals('username', $metadatas['User']->primaryTable['uniqueConstraints']['username']['columns'][0]);
unlink(__DIR__ . '/convert/User.dcm.yml'); unlink(__DIR__ . '/convert/User.dcm.yml');
unlink(__DIR__ . '/convert/Profile.dcm.yml'); unlink(__DIR__ . '/convert/Profile.dcm.yml');
......
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