Search in sources :

Example 6 with IndexElement

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

the class MySQLDataBaseBuilder method removeIndexForFK.

private void removeIndexForFK(TableContainer owner) {
    HashSet<String> fkNames = new HashSet<String>();
    for (AbstractTable table : owner.getTables()) {
        if (table instanceof Table) {
            for (ForeignKey foreignKey : ((Table) table).getForeignKeys()) {
                fkNames.add(foreignKey.getName());
            }
        }
    }
    for (AbstractTable t : owner.getTables()) {
        if (t instanceof Table) {
            Table table = (Table) t;
            ArrayList<Index> indices = new ArrayList<Index>();
            if (table.getIndexes().size() > 0) {
                for (Index index : table.getIndexes()) {
                    if (!fkNames.contains(index.getName())) {
                        indices.add(index);
                    } else {
                        for (IndexElement indexElt : index.getElements()) {
                            indexElt.getColumn().getIndexElements().remove(indexElt);
                        }
                    }
                }
                table.getIndexes().clear();
                table.getIndexes().addAll(indices);
            }
        }
    }
}
Also used : AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Table(org.obeonetwork.dsl.database.Table) AbstractTable(org.obeonetwork.dsl.database.AbstractTable) ArrayList(java.util.ArrayList) Index(org.obeonetwork.dsl.database.Index) ForeignKey(org.obeonetwork.dsl.database.ForeignKey) HashSet(java.util.HashSet) IndexElement(org.obeonetwork.dsl.database.IndexElement)

Example 7 with IndexElement

use of org.obeonetwork.dsl.database.IndexElement 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 8 with IndexElement

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

the class EntityToMLD method getFKIndexComments.

private String getFKIndexComments(Index index) {
    String comments = "Index sur la FK (";
    for (int i = 0; i < index.getElements().size(); i++) {
        if (i > 0) {
            comments += ", ";
        }
        IndexElement elt = index.getElements().get(i);
        comments += elt.getColumn().getName();
    }
    comments += ") de la table " + index.getOwner().getName();
    return comments;
}
Also used : Constraint(org.obeonetwork.dsl.database.Constraint) IndexElement(org.obeonetwork.dsl.database.IndexElement)

Example 9 with IndexElement

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

the class MLDToEntity method addPhysicalUnique.

private void addPhysicalUnique(Index index) {
    if (index.isUnique()) {
        if (index.getElements().size() == 1) {
            // Add annotation on Attribute
            IndexElement indexElement = index.getElements().get(0);
            String annotationValue = indexElement.isAsc() ? "ASC" : "DESC";
            Attribute uniqueAttribute = getFromOutputTraceabilityMap(indexElement.getColumn(), EnvironmentPackage.Literals.ATTRIBUTE);
            if (uniqueAttribute != null) {
                AnnotationHelper.setPhysicalUniqueAnnotation(uniqueAttribute, annotationValue);
            } else {
                // We can only handle the FK situation
                if (indexElement.getColumn().isInForeignKey()) {
                    // Search the reference corresponding to a FK
                    for (ForeignKey fk : indexElement.getColumn().getForeignKeys()) {
                        Reference reference = getFromOutputTraceabilityMap(fk, EnvironmentPackage.Literals.REFERENCE);
                        if (reference != null) {
                            AnnotationHelper.setPhysicalUniqueAnnotation(reference, annotationValue);
                        }
                    }
                }
            }
        } else {
            // Add annotation on Table
            Entity entity = getFromOutputTraceabilityMap(index.getOwner(), EntityPackage.Literals.ENTITY);
            // Retrieve the current value of the annotation.
            String annotationValue = AnnotationHelper.getPhysicalUnique(entity);
            // Construct the term corresponding to the index being considered.
            String annotationTerm = "";
            int i = 0;
            for (IndexElement indexElement : index.getElements()) {
                if (i > 0) {
                    annotationTerm += ",";
                }
                annotationTerm += indexElement.getColumn().getName() + ":";
                if (indexElement.isAsc()) {
                    annotationTerm += "ASC";
                } else {
                    annotationTerm += "DESC";
                }
                i++;
            }
            if (annotationValue != null && !annotationValue.isEmpty()) {
                annotationValue += " | ";
            } else if (annotationValue == null) {
                annotationValue = "";
            }
            annotationValue += annotationTerm;
            AnnotationHelper.setPhysicalUniqueAnnotation(entity, annotationValue);
        }
    }
}
Also used : Entity(org.obeonetwork.dsl.entity.Entity) Attribute(org.obeonetwork.dsl.environment.Attribute) Reference(org.obeonetwork.dsl.environment.Reference) ForeignKey(org.obeonetwork.dsl.database.ForeignKey) Constraint(org.obeonetwork.dsl.database.Constraint) IndexElement(org.obeonetwork.dsl.database.IndexElement)

Example 10 with IndexElement

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

the class MpdToMldBidiRules method createIndexElement.

private void createIndexElement(IndexElement sourceElementIndex, Index targetIndex) {
    IndexElement targetIndexElement = getFromInputTraceabilityMap(sourceElementIndex, DatabasePackage.Literals.INDEX_ELEMENT);
    if (targetIndexElement != null) {
        // Ensure it is in the right index
        if (!EcoreUtil.equals(targetIndexElement.eContainer(), targetIndex)) {
            targetIndex.getElements().add(targetIndexElement);
        }
    } else {
        // We have to create a new element
        targetIndexElement = DatabaseFactory.eINSTANCE.createIndexElement();
        targetIndex.getElements().add(targetIndexElement);
    }
    addToOutputTraceability(sourceElementIndex, targetIndexElement);
    Column targetColumn = getFromOutputTraceabilityMap(sourceElementIndex.getColumn(), DatabasePackage.Literals.COLUMN);
    targetIndexElement.setColumn(targetColumn);
    targetIndexElement.setAsc(sourceElementIndex.isAsc());
}
Also used : Column(org.obeonetwork.dsl.database.Column) IndexElement(org.obeonetwork.dsl.database.IndexElement)

Aggregations

IndexElement (org.obeonetwork.dsl.database.IndexElement)14 Index (org.obeonetwork.dsl.database.Index)7 Column (org.obeonetwork.dsl.database.Column)4 EObject (org.eclipse.emf.ecore.EObject)3 Constraint (org.obeonetwork.dsl.database.Constraint)3 ForeignKey (org.obeonetwork.dsl.database.ForeignKey)3 Table (org.obeonetwork.dsl.database.Table)3 ArrayList (java.util.ArrayList)2 Viewer (org.eclipse.jface.viewers.Viewer)2 ViewerFilter (org.eclipse.jface.viewers.ViewerFilter)2 Attribute (org.obeonetwork.dsl.environment.Attribute)2 Reference (org.obeonetwork.dsl.environment.Reference)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 IStatus (org.eclipse.core.runtime.IStatus)1 Status (org.eclipse.core.runtime.Status)1 EReference (org.eclipse.emf.ecore.EReference)1 EObjectPropertiesEditionContext (org.eclipse.emf.eef.runtime.context.impl.EObjectPropertiesEditionContext)1