Search in sources :

Example 11 with Schema

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

the class EntityToMLD method getTargetTableContainer.

protected TableContainer getTargetTableContainer(Namespace namespace) {
    if (!cacheNamespaceToTableContainer.containsKey(namespace)) {
        TableContainer target = null;
        // First we look for a schema with the right name
        String schemaName = getSchemaNameFromNamespace(namespace);
        // Look for a schema with the right name
        target = getSchemaByName(schemaName);
        if (target == null) {
            if (defaultTarget instanceof Schema) {
                if (((Schema) defaultTarget).getTables().isEmpty()) {
                    // We consider the schema is contained is a newly created model
                    // we can change its name
                    target = (Schema) defaultTarget;
                    target.setName(schemaName);
                }
            } else if (defaultTarget instanceof DataBase) {
                DataBase database = (DataBase) defaultTarget;
                if (database.getSchemas().size() == 1 && database.getSchemas().get(0).getTables().isEmpty()) {
                    target = database.getSchemas().get(0);
                    target.setName(schemaName);
                }
            }
            // We create a schema if the target object is a database
            if (target == null) {
                target = createSchema(schemaName);
            }
            // Last solution
            if (target == null && defaultTarget instanceof TableContainer) {
                target = (TableContainer) defaultTarget;
            }
        }
        cacheNamespaceToTableContainer.put(namespace, target);
    }
    // Ensure schema comments are retrieved from namespace
    TableContainer tableContainer = cacheNamespaceToTableContainer.get(namespace);
    tableContainer.setComments(namespace.getDescription());
    return tableContainer;
}
Also used : TableContainer(org.obeonetwork.dsl.database.TableContainer) Schema(org.obeonetwork.dsl.database.Schema) DataBase(org.obeonetwork.dsl.database.DataBase)

Example 12 with Schema

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

the class MpdToMldBidiRules method createOrUpdateSequences.

/**
 * Create needed new sequences or update existing ones
 * @param sourceTableContainer
 * @return list of sequences processed so that all other sequences (now useless) could be removed
 */
/**
 * @param sourceTableContainer
 * @return
 */
private Collection<Sequence> createOrUpdateSequences(TableContainer sourceTableContainer) {
    Collection<Sequence> sequences = new ArrayList<Sequence>();
    // for each non-composite PK we create a sequence and associate it with the PK column
    for (AbstractTable sourceAbstractTable : sourceTableContainer.getTables()) {
        if (sourceAbstractTable instanceof Table) {
            Table sourceTable = (Table) sourceAbstractTable;
            // Get associated table
            Table targetTable = getFromOutputTraceabilityMap(sourceTable, DatabasePackage.Literals.TABLE);
            if (targetTable != null) {
                PrimaryKey pk = targetTable.getPrimaryKey();
                // Only for non-composite PK
                if (pk != null && pk.getColumns().size() == 1) {
                    Column targetColumn = pk.getColumns().get(0);
                    // Retrieve the potentially existing sequence
                    Sequence existingSequence = targetColumn.getSequence();
                    String sequenceName = targetTable.getName() + "_SEQ";
                    if (existingSequence != null) {
                        // Update name
                        existingSequence.setName(sequenceName);
                        // Ensure the sequence is in the right container
                        if (!targetTable.getOwner().getSequences().contains(existingSequence)) {
                            targetTable.getOwner().getSequences().add(existingSequence);
                        }
                        sequences.add(existingSequence);
                    } else {
                        // Create a new sequence
                        Sequence newSequence = DatabaseFactory.eINSTANCE.createSequence();
                        newSequence.setName(sequenceName);
                        newSequence.setIncrement(new BigInteger("1"));
                        newSequence.setStart(new BigInteger("1"));
                        newSequence.setComments(String.format(SEQUENCE_INITIAL_COMMENTS, targetTable.getName()));
                        targetTable.getOwner().getSequences().add(newSequence);
                        // Retrieve the associated column and associate it with the sequence
                        targetColumn.setSequence(newSequence);
                        sequences.add(newSequence);
                    }
                }
            }
        }
    }
    if (sourceTableContainer instanceof DataBase) {
        DataBase sourceDataBase = (DataBase) sourceTableContainer;
        for (Schema sourceSchema : sourceDataBase.getSchemas()) {
            sequences.addAll(createOrUpdateSequences(sourceSchema));
        }
    }
    return sequences;
}
Also used : AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Table(org.obeonetwork.dsl.database.Table) AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Column(org.obeonetwork.dsl.database.Column) Schema(org.obeonetwork.dsl.database.Schema) ArrayList(java.util.ArrayList) PrimaryKey(org.obeonetwork.dsl.database.PrimaryKey) BigInteger(java.math.BigInteger) Sequence(org.obeonetwork.dsl.database.Sequence) DataBase(org.obeonetwork.dsl.database.DataBase)

Example 13 with Schema

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

the class MpdToMldBidiRules method createForeignKeys.

private void createForeignKeys(TableContainer sourceTableContainer) {
    // create all foreignkeys
    for (AbstractTable sourceAbstractTable : sourceTableContainer.getTables()) {
        if (sourceAbstractTable instanceof Table) {
            Table sourceTable = (Table) sourceAbstractTable;
            Table targetTable = getFromOutputTraceabilityMap(sourceTable, DatabasePackage.Literals.TABLE);
            for (ForeignKey foreignKey : sourceTable.getForeignKeys()) {
                createFK(foreignKey, targetTable);
            }
        }
    }
    if (sourceTableContainer instanceof DataBase) {
        DataBase sourceDataBase = (DataBase) sourceTableContainer;
        for (Schema sourceSchema : sourceDataBase.getSchemas()) {
            createForeignKeys(sourceSchema);
        }
    }
}
Also used : AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Table(org.obeonetwork.dsl.database.Table) AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Schema(org.obeonetwork.dsl.database.Schema) ForeignKey(org.obeonetwork.dsl.database.ForeignKey) DataBase(org.obeonetwork.dsl.database.DataBase)

Example 14 with Schema

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

the class GenerateSQLFromDatabaseHandler method copyDatabase.

private DataBase copyDatabase(DataBase database) {
    DataBase newDatabase = DatabaseFactory.eINSTANCE.createDataBase();
    newDatabase.setName(database.getName());
    for (Schema schema : database.getSchemas()) {
        newDatabase.getSchemas().add((Schema) copySchema(schema));
    }
    return newDatabase;
}
Also used : Schema(org.obeonetwork.dsl.database.Schema) DataBase(org.obeonetwork.dsl.database.DataBase)

Example 15 with Schema

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

the class OracleDataBaseBuilder method buildColumnConstraint.

@Override
protected void buildColumnConstraint(DatabaseMetaData metaData, TableContainer owner, Column column) {
    Schema schema = (Schema) owner;
    String key = schema.getName() + column.getOwner().getName() + column.getName();
    if (cacheConstraints == null) {
        cacheConstraints = new HashMap<String, OracleConstraint>();
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        try {
            PreparedStatement psmt = metaData.getConnection().prepareStatement(" SELECT dc.constraint_name, dc.search_condition, dcc.table_name, dcc.column_name " + " FROM all_constraints dc, all_cons_columns dcc " + " where dc.owner=?" + " AND dcc.owner=?" + " AND constraint_type='C'" + " AND substr(dc.constraint_name,1,3) <> 'SYS'" + " AND dc.owner=dcc.owner" + " AND dc.constraint_name=dcc.constraint_name" + " ORDER BY dc.constraint_name");
            psmt.setString(1, schema.getName());
            psmt.setString(2, schema.getName());
            rs = psmt.executeQuery();
            while (rs.next()) {
                String name = rs.getString(1);
                // do not reference recyclebin internal name
                if (name.startsWith("BIN$")) {
                    name = "";
                }
                String expression = rs.getString(2);
                String tableName = rs.getString(3);
                String columnName = rs.getString(4);
                if (!expression.endsWith("IS NOT NULL")) {
                    OracleConstraint oracleConstraint = new OracleConstraint(tableName, name, expression);
                    cacheConstraints.put(schema.getName() + tableName + columnName, oracleConstraint);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            JdbcUtils.closeStatement(pstmt);
            JdbcUtils.closeResultSet(rs);
        }
    }
    if (cacheConstraints.containsKey(key)) {
        OracleConstraint oracleConstraint = cacheConstraints.get(key);
        AbstractTable table = queries.getTable(owner, oracleConstraint.tableName);
        Constraint constraint = CreationUtils.createConstraint((Table) table, oracleConstraint.name);
        constraint.setExpression(oracleConstraint.expression);
    }
}
Also used : AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Constraint(org.obeonetwork.dsl.database.Constraint) Schema(org.obeonetwork.dsl.database.Schema) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException)

Aggregations

Schema (org.obeonetwork.dsl.database.Schema)25 DataBase (org.obeonetwork.dsl.database.DataBase)8 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)3 AbstractTable (org.obeonetwork.dsl.database.AbstractTable)3 AddSchema (org.obeonetwork.dsl.database.dbevolution.AddSchema)3 AlterSchema (org.obeonetwork.dsl.database.dbevolution.AlterSchema)3 RemoveSchema (org.obeonetwork.dsl.database.dbevolution.RemoveSchema)3 Table (org.obeonetwork.dsl.database.Table)2 TableContainer (org.obeonetwork.dsl.database.TableContainer)2 File (java.io.File)1 BigInteger (java.math.BigInteger)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Timestamp (java.sql.Timestamp)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 ExecutionException (org.eclipse.core.commands.ExecutionException)1 IResource (org.eclipse.core.resources.IResource)1 Comparison (org.eclipse.emf.compare.Comparison)1