Search in sources :

Example 1 with ForeignKeyElement

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

the class DatabaseServices method createForeignKey.

public ForeignKey createForeignKey(Table source, Table target) {
    ForeignKey fk = DatabaseFactory.eINSTANCE.createForeignKey();
    source.getForeignKeys().add(fk);
    fk.setTarget(target);
    // Initialize with columns
    if (target.getPrimaryKey() != null) {
        for (Column pkColumn : target.getPrimaryKey().getColumns()) {
            Column fkColumn = getOrCreateColumn(source, pkColumn);
            if (pkColumn != null && fkColumn != null) {
                ForeignKeyElement fke = DatabaseFactory.eINSTANCE.createForeignKeyElement();
                fke.setPkColumn(pkColumn);
                fke.setFkColumn(fkColumn);
                fk.getElements().add(fke);
            }
        }
    }
    return fk;
}
Also used : ForeignKeyElement(org.obeonetwork.dsl.database.ForeignKeyElement) Column(org.obeonetwork.dsl.database.Column) ForeignKey(org.obeonetwork.dsl.database.ForeignKey)

Example 2 with ForeignKeyElement

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

the class EntityToMLD method createFkElements.

private void createFkElements(ForeignKey fk, Table sourceTable, Table targetTable, boolean nullable, String fkComments) {
    // We will recreate all foreign key elements, before we have to check for already existing FK column
    // if we do that after the deletion, the column.isInForeignKey() would always return false
    Map<Column, Column> columnsMappingToBeUsed = new LinkedHashMap<Column, Column>();
    for (Column targetPkColumn : targetTable.getPrimaryKey().getColumns()) {
        Column sourceFkColumn = findExistingFKColumn(sourceTable, targetTable, false);
        if (sourceFkColumn == null) {
            sourceFkColumn = DatabaseFactory.eINSTANCE.createColumn();
            sourceTable.getColumns().add(sourceFkColumn);
        }
        sourceFkColumn.setName(targetPkColumn.getName());
        sourceFkColumn.setType(EcoreUtil.copy(targetPkColumn.getType()));
        sourceFkColumn.setNullable(nullable);
        sourceFkColumn.setComments(fkComments);
        columnsMappingToBeUsed.put(sourceFkColumn, targetPkColumn);
    }
    // Delete FK elements
    List<ForeignKeyElement> fkElements = new ArrayList<ForeignKeyElement>(fk.getElements());
    for (ForeignKeyElement fkElt : fkElements) {
        EcoreUtil.delete(fkElt);
    }
    // Recreate FK elements
    for (Entry<Column, Column> columnsMapping : columnsMappingToBeUsed.entrySet()) {
        Column sourceFkColumn = columnsMapping.getKey();
        Column targetPkColumn = columnsMapping.getValue();
        if (targetPkColumn != null && sourceFkColumn != null) {
            ForeignKeyElement foreignKeyElement = DatabaseFactory.eINSTANCE.createForeignKeyElement();
            fk.getElements().add(foreignKeyElement);
            foreignKeyElement.setFkColumn(sourceFkColumn);
            foreignKeyElement.setPkColumn(targetPkColumn);
        }
        addToObjectsToBeKept(sourceFkColumn);
        addToObjectsToBeKept(targetPkColumn);
    }
}
Also used : ForeignKeyElement(org.obeonetwork.dsl.database.ForeignKeyElement) Column(org.obeonetwork.dsl.database.Column) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with ForeignKeyElement

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

the class CreationUtils method createForeignKeyElement.

public static ForeignKeyElement createForeignKeyElement(int keySequence, ForeignKey foreignKey) {
    ForeignKeyElement foreignKeyElement = DatabaseFactory.eINSTANCE.createForeignKeyElement();
    foreignKey.getElements().add(foreignKeyElement);
    return foreignKeyElement;
}
Also used : ForeignKeyElement(org.obeonetwork.dsl.database.ForeignKeyElement)

Example 4 with ForeignKeyElement

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

the class ForeignKeyElementItemProvider method getText.

/**
 * This returns the label text for the adapted class.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated NOT
 */
@Override
public String getText(Object object) {
    ForeignKeyElement fkElt = (ForeignKeyElement) object;
    String label = "";
    if (fkElt.getFkColumn() != null) {
        String fkColumnName = fkElt.getFkColumn().getName();
        if (fkColumnName != null && fkColumnName.length() != 0) {
            label = fkColumnName;
        } else {
            label = getString("_UI_Column_name_undefined");
        }
    }
    label += " -> ";
    if (fkElt.getPkColumn() != null) {
        String pkColumnName = fkElt.getPkColumn().getName();
        if (pkColumnName != null && pkColumnName.length() != 0) {
            label += pkColumnName;
        } else {
            label += getString("_UI_Column_name_undefined");
        }
    }
    return label == null || label.length() == 0 ? getString("_UI_ForeignKeyElement_type") : label;
}
Also used : ForeignKeyElement(org.obeonetwork.dsl.database.ForeignKeyElement)

Example 5 with ForeignKeyElement

use of org.obeonetwork.dsl.database.ForeignKeyElement 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)

Aggregations

ForeignKeyElement (org.obeonetwork.dsl.database.ForeignKeyElement)11 ForeignKey (org.obeonetwork.dsl.database.ForeignKey)8 Table (org.obeonetwork.dsl.database.Table)7 Column (org.obeonetwork.dsl.database.Column)6 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 EObject (org.eclipse.emf.ecore.EObject)3 Map (java.util.Map)2 IStatus (org.eclipse.core.runtime.IStatus)2 Status (org.eclipse.core.runtime.Status)2 EObjectFlatComboSettings (org.eclipse.emf.eef.runtime.ui.widgets.eobjflatcombo.EObjectFlatComboSettings)2 Viewer (org.eclipse.jface.viewers.Viewer)2 ViewerFilter (org.eclipse.jface.viewers.ViewerFilter)2 AbstractTable (org.obeonetwork.dsl.database.AbstractTable)2 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 List (java.util.List)1 EReference (org.eclipse.emf.ecore.EReference)1 EObjectPropertiesEditionContext (org.eclipse.emf.eef.runtime.context.impl.EObjectPropertiesEditionContext)1