Search in sources :

Example 11 with ForeignKey

use of org.obeonetwork.dsl.database.ForeignKey in project InformationSystem by ObeoNetwork.

the class ForeignKeyChangeImpl method setForeignKey.

/**
 * <!-- begin-user-doc --> <!-- end-user-doc -->
 * @generated
 */
public void setForeignKey(ForeignKey newForeignKey) {
    ForeignKey oldForeignKey = foreignKey;
    foreignKey = newForeignKey;
    if (eNotificationRequired())
        eNotify(new ENotificationImpl(this, Notification.SET, DbevolutionPackage.FOREIGN_KEY_CHANGE__FOREIGN_KEY, oldForeignKey, foreignKey));
}
Also used : ENotificationImpl(org.eclipse.emf.ecore.impl.ENotificationImpl) ForeignKey(org.obeonetwork.dsl.database.ForeignKey)

Example 12 with ForeignKey

use of org.obeonetwork.dsl.database.ForeignKey in project InformationSystem by ObeoNetwork.

the class EntityToMLD method createJoinTableForeignKey.

private void createJoinTableForeignKey(Table joinTable, Table targetTable) {
    // FK for table
    ForeignKey fk = null;
    for (ForeignKey existingFK : joinTable.getForeignKeys()) {
        if (targetTable.equals(existingFK.getTarget())) {
            fk = existingFK;
            break;
        }
    }
    if (fk == null) {
        // We have to create a FK
        fk = DatabaseFactory.eINSTANCE.createForeignKey();
        joinTable.getForeignKeys().add(fk);
        fk.setTarget(targetTable);
    }
    addToObjectsToBeKept(fk);
    createFkElements(fk, joinTable, targetTable, false, "PK,FK de la table " + joinTable.getName());
    // Add FK columns to the PK
    Column fkColumn = findExistingFKColumn(joinTable, targetTable, true);
    if (fkColumn == null) {
        fkColumn = DatabaseFactory.eINSTANCE.createColumn();
        fkColumn.setName(targetTable.getPrimaryKey().getColumns().get(0).getName());
        joinTable.getColumns().add(fkColumn);
        addToObjectsToBeKept(fkColumn);
    }
    // Add to PK when needed
    fkColumn.addToPrimaryKey();
    fk.setName(getFKName(fk));
    fk.setComments("Contrainte FK avec la table " + targetTable.getName());
}
Also used : Column(org.obeonetwork.dsl.database.Column) ForeignKey(org.obeonetwork.dsl.database.ForeignKey)

Example 13 with ForeignKey

use of org.obeonetwork.dsl.database.ForeignKey in project InformationSystem by ObeoNetwork.

the class EntityToMLD method createForeignKey.

private void createForeignKey(Reference reference, Table sourceTable, Table targetTable, boolean nullable) {
    // Try to retrieve existing foreign key
    ForeignKey fk = getFromInputTraceabilityMap(reference, DatabasePackage.Literals.FOREIGN_KEY);
    if (fk != null) {
        // Ensure it is contained by the right table
        if (!EcoreUtil.equals(fk.getSourceTable(), sourceTable)) {
            sourceTable.getForeignKeys().add(fk);
        }
        // Ensure it references the right table
        if (!EcoreUtil.equals(fk.getTargetTable(), targetTable)) {
            fk.setTarget(targetTable);
        }
    } else {
        // The FK does not already exist, we have to create a new one
        fk = DatabaseFactory.eINSTANCE.createForeignKey();
        sourceTable.getForeignKeys().add(fk);
        fk.setTarget(targetTable);
    }
    addToOutputTraceability(reference, fk);
    String referenceDescription = reference.getDescription();
    if (referenceDescription != null && referenceDescription.trim().length() > 0) {
        fk.setComments(referenceDescription);
    } else {
        fk.setComments("Contrainte FK avec la table " + fk.getTarget().getName());
    }
    createFkElements(fk, sourceTable, targetTable, nullable, referenceDescription);
    String fkName = LabelProvider.getFKNameFromReference(reference);
    if (fkName == null) {
        fkName = getFKName(fk);
    }
    fk.setName(fkName);
}
Also used : ForeignKey(org.obeonetwork.dsl.database.ForeignKey)

Example 14 with ForeignKey

use of org.obeonetwork.dsl.database.ForeignKey in project InformationSystem by ObeoNetwork.

the class EntityToMLD method createIndices.

private void createIndices(Entity entity) {
    Table table = getFromOutputTraceabilityMap(entity, DatabasePackage.Literals.TABLE);
    // We collect unique and non unique indices, all indices will be kept unchanged
    Collection<Index> existingUniqueIndices = new ArrayList<Index>();
    Collection<Index> existingNonUniqueIndices = new ArrayList<Index>();
    for (Index index : table.getIndexes()) {
        if (index.isUnique()) {
            existingUniqueIndices.add(index);
        } else {
            existingNonUniqueIndices.add(index);
        }
    }
    // Handle indexes on attributes
    for (Attribute attribute : entity.getOwnedAttributes()) {
        String unicity = AnnotationHelper.getPhysicalUnique(attribute);
        if (unicity != null) {
            List<Column> columns = new ArrayList<Column>();
            Column column = getFromOutputTraceabilityMap(attribute, DatabasePackage.Literals.COLUMN);
            columns.add(column);
            Index index = findIndex(existingUniqueIndices, columns);
            if (index != null) {
                // We reuse the existing index
                index.getElements().get(0).setAsc(isIndexAsc(unicity));
            } else {
                // We have to create a new index
                index = DatabaseFactory.eINSTANCE.createIndex();
                table.getIndexes().add(index);
                existingUniqueIndices.add(index);
                index.setUnique(true);
                IndexElement indexElement = DatabaseFactory.eINSTANCE.createIndexElement();
                index.getElements().add(indexElement);
                indexElement.setAsc(isIndexAsc(unicity));
                indexElement.setColumn(column);
            }
            index.setName(getUniqueIndexName(index));
            index.setComments(getUniqueIndexComments(index));
            addToObjectsToBeKept(index);
        }
    }
    // Handle indexes on entity
    String tableUnicity = AnnotationHelper.getPhysicalUnique(entity);
    if (tableUnicity != null) {
        List<Map<String, Boolean>> listOfIndexInfos = getInfosFromTableUnicity(tableUnicity);
        for (Map<String, Boolean> indexInfos : listOfIndexInfos) {
            List<Column> columns = new ArrayList<Column>();
            for (String columnName : indexInfos.keySet()) {
                for (Column column : table.getColumns()) {
                    if (column.getName().equalsIgnoreCase(columnName)) {
                        columns.add(column);
                        break;
                    }
                }
            }
            // Check if all columns were found
            if (columns.size() != indexInfos.size()) {
                String msg = "Could not understand PHYSICAL_UNIQUE annotation for Entity: " + entity.getName() + " - annotation: \"" + tableUnicity + "\"";
                Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, msg));
            }
            // Try to retrieve an existing index on these columns
            Index index = findIndex(existingUniqueIndices, columns);
            if (index != null) {
                // Update sort orders
                for (IndexElement element : index.getElements()) {
                    element.setAsc(indexInfos.get(element.getColumn().getName().toUpperCase()));
                }
            } else {
                // We have to create a new index
                index = DatabaseFactory.eINSTANCE.createIndex();
                table.getIndexes().add(index);
                existingUniqueIndices.add(index);
                index.setUnique(true);
                for (Column targetColumn : columns) {
                    IndexElement indexElement = DatabaseFactory.eINSTANCE.createIndexElement();
                    index.getElements().add(indexElement);
                    String s = targetColumn.getName();
                    indexElement.setAsc(indexInfos.get(targetColumn.getName().toUpperCase()));
                    indexElement.setColumn(targetColumn);
                }
            }
            index.setName(getUniqueIndexName(index));
            index.setComments(getUniqueIndexComments(index));
            addToObjectsToBeKept(index);
        }
    }
    // Handle indexes on references
    for (Reference reference : getReferencesForIndexCreation(entity)) {
        boolean indexShouldBeUnique = false;
        // if (isMultiplicitySimple(reference)) {
        // indexShouldBeUnique = true;
        // } else if (isMultiplicityMany(reference)) {
        // indexShouldBeUnique = false;
        // }
        String unicity = AnnotationHelper.getPhysicalUnique(reference);
        if (unicity == null) {
            unicity = "ASC";
        } else {
            indexShouldBeUnique = true;
        }
        List<Column> columns = new ArrayList<Column>();
        ForeignKey fk = getFromOutputTraceabilityMap(reference, DatabasePackage.Literals.FOREIGN_KEY);
        // Get columns for FK
        if (fk != null) {
            for (ForeignKeyElement fkElt : fk.getElements()) {
                columns.add(fkElt.getFkColumn());
            }
            Index index = null;
            if (indexShouldBeUnique) {
                index = findIndex(existingUniqueIndices, columns);
            } else {
                index = findIndex(existingNonUniqueIndices, columns);
            }
            if (index != null) {
                // Ensure the index is of the right kind
                index.setUnique(indexShouldBeUnique);
                // We reuse the existing index
                for (IndexElement indexElt : index.getElements()) {
                    indexElt.setAsc(isIndexAsc(unicity));
                }
            } else {
                // We have to create a new index
                index = DatabaseFactory.eINSTANCE.createIndex();
                table.getIndexes().add(index);
                if (indexShouldBeUnique) {
                    existingUniqueIndices.add(index);
                } else {
                    existingNonUniqueIndices.add(index);
                }
                index.setUnique(indexShouldBeUnique);
                for (ForeignKeyElement fkElt : fk.getElements()) {
                    IndexElement indexElement = DatabaseFactory.eINSTANCE.createIndexElement();
                    index.getElements().add(indexElement);
                    indexElement.setAsc(isIndexAsc(unicity));
                    indexElement.setColumn(fkElt.getFkColumn());
                }
            }
            index.setName(fk.getName());
            index.setComments(getFKIndexComments(index));
            addToObjectsToBeKept(index);
        }
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) ForeignKeyElement(org.obeonetwork.dsl.database.ForeignKeyElement) Table(org.obeonetwork.dsl.database.Table) Attribute(org.obeonetwork.dsl.environment.Attribute) Reference(org.obeonetwork.dsl.environment.Reference) EReference(org.eclipse.emf.ecore.EReference) ArrayList(java.util.ArrayList) Index(org.obeonetwork.dsl.database.Index) ForeignKey(org.obeonetwork.dsl.database.ForeignKey) IndexElement(org.obeonetwork.dsl.database.IndexElement) Column(org.obeonetwork.dsl.database.Column) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 15 with ForeignKey

use of org.obeonetwork.dsl.database.ForeignKey in project InformationSystem by ObeoNetwork.

the class EntityToMLD method handleMultipleForeignKeys.

private void handleMultipleForeignKeys(Table table) {
    // multipleFKs contains foreign keys targetting a same table (the table is used as key)
    Map<Table, List<ForeignKey>> multipleFKs = new HashMap<Table, List<ForeignKey>>();
    for (ForeignKey fk : table.getForeignKeys()) {
        for (ForeignKey otherFK : table.getForeignKeys()) {
            Table targetTable = fk.getTargetTable();
            if (fk != otherFK && targetTable == otherFK.getTargetTable()) {
                if (multipleFKs.get(targetTable) == null) {
                    multipleFKs.put(targetTable, new ArrayList<ForeignKey>());
                }
                if (!multipleFKs.get(targetTable).contains(otherFK)) {
                    multipleFKs.get(targetTable).add(otherFK);
                }
            }
        }
    }
    // Rename columns targetted by the FKs
    for (List<ForeignKey> fks : multipleFKs.values()) {
        int counter = 0;
        // Sort FKs
        List<ForeignKey> sortedFks = new ArrayList<ForeignKey>(fks);
        Collections.sort(sortedFks, new Comparator<ForeignKey>() {

            @Override
            public int compare(ForeignKey fk1, ForeignKey fk2) {
                return columnIndex(fk1.getElements().get(0).getFkColumn()) - columnIndex(fk2.getElements().get(0).getFkColumn());
            }

            private int columnIndex(Column column) {
                return column.getOwner().getColumns().indexOf(column);
            }
        });
        for (ForeignKey fk : sortedFks) {
            counter = counter + 1;
            for (ForeignKeyElement fkElt : fk.getElements()) {
                fkElt.getFkColumn().getOwner().getColumns().indexOf(fkElt.getFkColumn());
                fkElt.getFkColumn().setName(getFKColumnName(fkElt, counter));
            }
        }
    }
}
Also used : ForeignKeyElement(org.obeonetwork.dsl.database.ForeignKeyElement) Table(org.obeonetwork.dsl.database.Table) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) ForeignKey(org.obeonetwork.dsl.database.ForeignKey) Constraint(org.obeonetwork.dsl.database.Constraint) Column(org.obeonetwork.dsl.database.Column) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

ForeignKey (org.obeonetwork.dsl.database.ForeignKey)27 Table (org.obeonetwork.dsl.database.Table)16 EObject (org.eclipse.emf.ecore.EObject)8 AbstractTable (org.obeonetwork.dsl.database.AbstractTable)8 ForeignKeyElement (org.obeonetwork.dsl.database.ForeignKeyElement)8 Column (org.obeonetwork.dsl.database.Column)7 ArrayList (java.util.ArrayList)6 Reference (org.obeonetwork.dsl.environment.Reference)4 HashMap (java.util.HashMap)3 IStatus (org.eclipse.core.runtime.IStatus)3 Status (org.eclipse.core.runtime.Status)3 Viewer (org.eclipse.jface.viewers.Viewer)3 ViewerFilter (org.eclipse.jface.viewers.ViewerFilter)3 Constraint (org.obeonetwork.dsl.database.Constraint)3 Index (org.obeonetwork.dsl.database.Index)3 IndexElement (org.obeonetwork.dsl.database.IndexElement)3 Entity (org.obeonetwork.dsl.entity.Entity)3 Attribute (org.obeonetwork.dsl.environment.Attribute)3 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2