Search in sources :

Example 56 with Column

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

the class EntityToMLD method createDefaultIdColumn.

private void createDefaultIdColumn(Table table) {
    // Check if there is already an ID column
    if (table.getPrimaryKey() != null && table.getPrimaryKey().getColumns().size() == 1) {
        // We don't need to create an ID column
        // but let's ensure it's corretly named
        Column idColumn = table.getPrimaryKey().getColumns().get(0);
        idColumn.setName(table.getName() + "_ID");
        idColumn.setComments(getPKColumnComment(idColumn));
        addToObjectsToBeKept(idColumn);
        return;
    }
    // Let's create a default ID column
    Column idColumn = DatabaseFactory.eINSTANCE.createColumn();
    table.getColumns().add(idColumn);
    idColumn.setName(table.getName() + "_ID");
    idColumn.setComments(getPKColumnComment(idColumn));
    idColumn.addToPrimaryKey();
    TypeInstance typeInstance = TypesLibraryFactory.eINSTANCE.createTypeInstance();
    typeInstance.setNativeType(nativeTypesMap.get("Entier"));
    idColumn.setType(typeInstance);
    addToObjectsToBeKept(idColumn);
}
Also used : Column(org.obeonetwork.dsl.database.Column) TypeInstance(org.obeonetwork.dsl.typeslibrary.TypeInstance)

Example 57 with Column

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

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

Example 59 with Column

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

the class MLDToEntity method createEntity.

private void createEntity(Table table) {
    Entity entity = getFromInputTraceabilityMap(table, EntityPackage.Literals.ENTITY);
    if (entity == null) {
        // The entity does not already exist
        // We have to create a new one
        entity = EntityFactory.eINSTANCE.createEntity();
        getTargetNamespace(table.getOwner()).getTypes().add(entity);
        entity.setName(LabelProvider.getEntityNameFromTable(table));
    }
    // Add to new traceability map
    addToOutputTraceability(table, entity);
    // The following properties are modified even if they already existed
    AnnotationHelper.setPhysicalNameAnnotation(entity, LabelProvider.getEntityPhysicalNameFromTable(table));
    entity.setDescription(table.getComments());
    AnnotationHelper.removePhysicalUniqueAnnotations(entity);
    for (Column column : table.getColumns()) {
        if (!column.isInForeignKey() && !column.isInPrimaryKey() && !AdditionalFieldsUtils.isAdditionalColumn(column)) {
            createAttribute(column, entity);
        }
    }
    AnnotationHelper.removePhysicalCheckAnnotations(entity);
    List<Constraint> constraintsToCreate = new ArrayList<Constraint>();
    for (Constraint constraint : table.getConstraints()) {
        if (!AdditionalFieldsUtils.isAdditionalConstraint(constraint)) {
            constraintsToCreate.add(constraint);
        }
    }
    AnnotationHelper.setAllConstraints(entity, constraintsToCreate);
}
Also used : Entity(org.obeonetwork.dsl.entity.Entity) Column(org.obeonetwork.dsl.database.Column) Constraint(org.obeonetwork.dsl.database.Constraint) ArrayList(java.util.ArrayList)

Example 60 with Column

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

the class MpdToMldBidiRules method createPK.

private void createPK(PrimaryKey sourcePk, Table targetTable) {
    PrimaryKey targetPk = getFromInputTraceabilityMap(sourcePk, DatabasePackage.Literals.PRIMARY_KEY);
    if (targetPk != null && EcoreUtil.equals(targetPk.getOwner(), targetTable)) {
    // Reuse existing PK
    } else {
        // Create a new PK
        targetPk = DatabaseFactory.eINSTANCE.createPrimaryKey();
        targetTable.setPrimaryKey(targetPk);
    }
    addToOutputTraceability(sourcePk, targetPk);
    targetPk.setName(sourcePk.getName());
    targetPk.setComments(sourcePk.getComments());
    for (Column sourcePkColumn : sourcePk.getColumns()) {
        Column targetPkColumn = getFromOutputTraceabilityMap(sourcePkColumn, DatabasePackage.Literals.COLUMN);
        if (!targetPk.getColumns().contains(targetPkColumn)) {
            targetPk.getColumns().add(targetPkColumn);
        }
        if (isTargetMysqlMPD()) {
            targetPkColumn.setAutoincrement(true);
        }
    }
}
Also used : Column(org.obeonetwork.dsl.database.Column) PrimaryKey(org.obeonetwork.dsl.database.PrimaryKey)

Aggregations

Column (org.obeonetwork.dsl.database.Column)83 Table (org.obeonetwork.dsl.database.Table)41 Test (org.junit.Test)31 AbstractTest (org.obeonetwork.database.m2doc.services.common.AbstractTest)31 TypeInstance (org.obeonetwork.dsl.typeslibrary.TypeInstance)11 EObject (org.eclipse.emf.ecore.EObject)9 AbstractTable (org.obeonetwork.dsl.database.AbstractTable)8 ForeignKey (org.obeonetwork.dsl.database.ForeignKey)7 ArrayList (java.util.ArrayList)6 Constraint (org.obeonetwork.dsl.database.Constraint)6 ForeignKeyElement (org.obeonetwork.dsl.database.ForeignKeyElement)6 Sequence (org.obeonetwork.dsl.database.Sequence)6 BigInteger (java.math.BigInteger)5 ResultSet (java.sql.ResultSet)5 SQLException (java.sql.SQLException)5 NativeType (org.obeonetwork.dsl.typeslibrary.NativeType)5 PreparedStatement (java.sql.PreparedStatement)4 Viewer (org.eclipse.jface.viewers.Viewer)4 ViewerFilter (org.eclipse.jface.viewers.ViewerFilter)4 IndexElement (org.obeonetwork.dsl.database.IndexElement)4