Search in sources :

Example 1 with TableContainer

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

the class EntityToMLD method prepare.

@Override
protected boolean prepare(EObject sourceObject, EObject targetObject) {
    if (targetObject instanceof TableContainer) {
        sourceNamespaces = new ArrayList<Namespace>();
        if (sourceObject instanceof Root) {
            Root sourceRoot = (Root) sourceObject;
            sourceNamespaces.addAll(sourceRoot.getOwnedNamespaces());
        } else if (sourceObject instanceof Namespace) {
            sourceNamespaces.add((Namespace) sourceObject);
        }
        targetResource = targetObject.eResource();
        defaultTarget = targetObject;
        nativeTypesMap = loadTypesLibrary();
        return (!nativeTypesMap.isEmpty());
    }
    return false;
}
Also used : Root(org.obeonetwork.dsl.entity.Root) TableContainer(org.obeonetwork.dsl.database.TableContainer) Namespace(org.obeonetwork.dsl.environment.Namespace)

Example 2 with TableContainer

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

the class EntityToMLD method createTable.

private void createTable(Entity entity) {
    // Retrieve the existing table
    Table table = getFromInputTraceabilityMap(entity, DatabasePackage.Literals.TABLE);
    Namespace namespace = EntityUtils.getContainingNamespace(entity);
    if (table == null) {
        // The table does not already exist we have to create a new one
        table = DatabaseFactory.eINSTANCE.createTable();
        TableContainer targetTableContainer = getTargetTableContainer(namespace);
        targetTableContainer.getTables().add(table);
    } else {
        // We have to ensure the schema name is correct
        String realSchemaName = getSchemaNameFromNamespace(namespace);
        TableContainer tableContainer = table.getOwner();
        if (!realSchemaName.equals(tableContainer.getName())) {
            tableContainer.setName(realSchemaName);
        }
    }
    // Add to traceability map
    addToOutputTraceability(entity, table);
    // The following properties are modified even if they already existed
    table.setName(LabelProvider.getTableNameFromEntity(entity));
    table.setComments(entity.getDescription());
    // Handle attributes
    boolean hasPKAttribute = false;
    Collection<Attribute> allAttributes = new ArrayList<Attribute>();
    // Attributes from the entity and its supertypes
    allAttributes.addAll(entity.getAttributes());
    // Attributes from associated DTOs
    for (StructuredType associatedType : entity.getAssociatedTypes()) {
        allAttributes.addAll(associatedType.getAttributes());
    }
    for (Attribute attribute : allAttributes) {
        createColumn(attribute, table);
        if (attribute.isIsIdentifier()) {
            hasPKAttribute = true;
        }
    }
    // Create an ID column if no attribute was set as "primary key"
    if (hasPKAttribute == false) {
        createDefaultIdColumn(table);
    }
    // Update comments on PK
    PrimaryKey primaryKey = table.getPrimaryKey();
    if (primaryKey != null) {
        primaryKey.setName(table.getName() + "_PK");
        primaryKey.setComments(getPKComments(primaryKey));
    }
    // Create constraints
    createConstraints(entity, table);
}
Also used : Table(org.obeonetwork.dsl.database.Table) Attribute(org.obeonetwork.dsl.environment.Attribute) TableContainer(org.obeonetwork.dsl.database.TableContainer) ArrayList(java.util.ArrayList) PrimaryKey(org.obeonetwork.dsl.database.PrimaryKey) Namespace(org.obeonetwork.dsl.environment.Namespace) StructuredType(org.obeonetwork.dsl.environment.StructuredType)

Example 3 with TableContainer

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

the class GenerateSQLFromDatabaseHandler method execute.

public Object execute(ExecutionEvent event) throws ExecutionException {
    ISelection selection = HandlerUtil.getCurrentSelection(event);
    // We have to compute an emfdiff file from a comparison with an empty database
    // ---
    // First let's create an empty model containing only
    Resource resource = new DatabaseResourceImpl(URI.createURI("memory:/empty_database"));
    TableContainer rootContainer = getFirstTableContainer(selection);
    TableContainer newContainer = null;
    if (rootContainer instanceof DataBase) {
        newContainer = copyDatabase((DataBase) rootContainer);
        resource.getContents().add(newContainer);
    } else if (rootContainer instanceof Schema) {
        newContainer = copySchema((Schema) rootContainer);
        resource.getContents().add(newContainer);
    }
    // Then compare the two models
    Comparison comparison = null;
    try {
        comparison = DatabaseCompareService.compare(rootContainer, newContainer);
    } catch (Exception e) {
    }
    // The diff model can now be used to generate the SQL script
    if (comparison != null) {
        ExportAsSQLScriptsAction action = new ExportAsSQLScriptsAction();
        action.exportComparison(comparison);
    }
    return null;
}
Also used : Comparison(org.eclipse.emf.compare.Comparison) TableContainer(org.obeonetwork.dsl.database.TableContainer) DatabaseResourceImpl(org.obeonetwork.dsl.database.util.DatabaseResourceImpl) Schema(org.obeonetwork.dsl.database.Schema) ISelection(org.eclipse.jface.viewers.ISelection) IResource(org.eclipse.core.resources.IResource) Resource(org.eclipse.emf.ecore.resource.Resource) ExecutionException(org.eclipse.core.commands.ExecutionException) DataBase(org.obeonetwork.dsl.database.DataBase)

Example 4 with TableContainer

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

the class DataBaseServices method allSequences.

/**
 * Returns all the sequences contained and referenced by the database (including the sequences associated to tables of external
 * databases referenced through foreign
 * keys).
 *
 * @param database
 * @return the set of all sequences contained and referenced by the database.
 */
public Set<Sequence> allSequences(DataBase database) {
    Set<Sequence> result = new HashSet<Sequence>();
    result.addAll(database.getSequences());
    Set<Table> tables = allTables(database);
    for (Table table : tables) {
        if (table.eContainer() instanceof TableContainer) {
            result.addAll(((TableContainer) table.eContainer()).getSequences());
        }
    }
    return result;
}
Also used : Table(org.obeonetwork.dsl.database.Table) AbstractTable(org.obeonetwork.dsl.database.AbstractTable) TableContainer(org.obeonetwork.dsl.database.TableContainer) Sequence(org.obeonetwork.dsl.database.Sequence) HashSet(java.util.HashSet)

Example 5 with TableContainer

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

the class AbstractDatabaseCompareTest method testDatabaseCompare.

public void testDatabaseCompare(String folder) {
    TableContainer tableContainer1 = (TableContainer) loadRootObject(folder, INPUT_DATABASE_1, TableContainer.class);
    TableContainer tableContainer2 = (TableContainer) loadRootObject(folder, INPUT_DATABASE_2, TableContainer.class);
    // Comparison expectedSnapshot = (Comparison)loadRootObject(folder, EXPECTED_COMPARISON, Comparison.class);
    Comparison actualSnapshot = null;
    try {
        actualSnapshot = DatabaseCompareService.compare(tableContainer1, tableContainer2);
    } catch (Exception e) {
        Assert.fail("Exception during comparison : " + e.getMessage());
    }
    try {
        Generate generate = new Generate(actualSnapshot, new File("models/" + folder), new ArrayList<Object>());
        generate.doGenerate(new BasicMonitor());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : Comparison(org.eclipse.emf.compare.Comparison) TableContainer(org.obeonetwork.dsl.database.TableContainer) Generate(org.obeonetwork.dsl.database.compare.tests.gen.main.Generate) EObject(org.eclipse.emf.ecore.EObject) IOException(java.io.IOException) File(java.io.File) BasicMonitor(org.eclipse.emf.common.util.BasicMonitor) IOException(java.io.IOException)

Aggregations

TableContainer (org.obeonetwork.dsl.database.TableContainer)12 Comparison (org.eclipse.emf.compare.Comparison)4 EObject (org.eclipse.emf.ecore.EObject)4 Resource (org.eclipse.emf.ecore.resource.Resource)3 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)3 Table (org.obeonetwork.dsl.database.Table)3 Namespace (org.obeonetwork.dsl.environment.Namespace)3 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)2 DataBase (org.obeonetwork.dsl.database.DataBase)2 Schema (org.obeonetwork.dsl.database.Schema)2 Root (org.obeonetwork.dsl.entity.Root)2 ScaffoldInfo (fr.gouv.mindef.safran.database.scaffold.ScaffoldInfo)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ExecutionException (org.eclipse.core.commands.ExecutionException)1 IResource (org.eclipse.core.resources.IResource)1 BasicMonitor (org.eclipse.emf.common.util.BasicMonitor)1