Skip to content

Commit

Permalink
Merge pull request #12 from mdrillin/TEIIDDES-1458
Browse files Browse the repository at this point in the history
TEIIDDES-1458
  • Loading branch information
blafond committed Aug 27, 2012
2 parents 047897a + 22c28bb commit 010aff9
Showing 1 changed file with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
Expand Down Expand Up @@ -53,6 +52,7 @@
import org.teiid.designer.compare.selector.ModelResourceSelector;
import org.teiid.designer.compare.selector.TransientModelSelector;
import org.teiid.designer.core.ModelerCore;
import org.teiid.designer.core.validation.rules.StringNameValidator;
import org.teiid.designer.core.workspace.ModelResource;
import org.teiid.designer.core.workspace.ModelUtil;
import org.teiid.designer.metamodels.core.ModelAnnotation;
Expand Down Expand Up @@ -221,8 +221,8 @@ private void createKey( final AstNode node,
}
} else if (DdlConstants.FOREIGN_KEY.equals(type)) {
final ForeignKey key = FACTORY.createForeignKey();
initializeFK(table.getForeignKeys(), key, node);
table.getForeignKeys().add(key);
initialize(key, node);
BaseTable foreignTable = null;
final Set<Column> foreignColumns = new HashSet<Column>();
for (final AstNode node1 : node) {
Expand All @@ -248,6 +248,7 @@ else if (StandardDdlLexicon.TYPE_FK_COLUMN_REFERENCE.equals(mixinType)) {
if (foreignTable != null) {
final PrimaryKey primaryKey = foreignTable.getPrimaryKey();
final List<Column> primaryKeyColumns = primaryKey.getColumns();
if (foreignColumns.isEmpty()) key.setUniqueKey(primaryKey);
if (primaryKeyColumns.containsAll(foreignColumns) && primaryKeyColumns.size() == foreignColumns.size()) key.setUniqueKey(primaryKey);
else for (final Object obj : foreignTable.getUniqueConstraints()) {
final UniqueConstraint uniqueKey = (UniqueConstraint)obj;
Expand Down Expand Up @@ -453,6 +454,40 @@ private void initialize( final RelationalEntity entity,
initialize(entity, node, node.getName().getLocalName());
}

private void initializeFK( final List<ForeignKey> currentFKs,
final ForeignKey key,
final AstNode node ) {
// Get Name from DDL node
String fkName = node.getName().getLocalName();
// Make sure not to add duplicate FK names
String uniqueName = getUniqueFKName(currentFKs, fkName);

initialize(key, node, uniqueName);
}

/*
* Helper method for creating unique FK names
* @param currentFKs the List of ForeignKeys currently on the table
* @param newFKName the proposed name for the new FK
* @return the unique name - generated from the proposed name
*/
private String getUniqueFKName( List<ForeignKey> currentFKs,
String newFKName ) {
// If current list is empty, no need to check names
if (currentFKs == null || currentFKs.isEmpty()) return newFKName;

// Use name validator for unique name generation
StringNameValidator nameValidator = new StringNameValidator();

// Add the current FK names to the validator
for (ForeignKey fk : currentFKs) {
nameValidator.addExistingName(fk.getName());
}

// Make the proposed name unique
return nameValidator.createValidUniqueName(newFKName);
}

private void initialize( final RelationalEntity entity,
final AstNode node,
final String name ) {
Expand Down

0 comments on commit 010aff9

Please sign in to comment.