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