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);
}
}
}
}
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);
}
}
}
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;
}
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);
}
}
}
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());
}
Aggregations