use of org.obeonetwork.dsl.database.Constraint in project InformationSystem by ObeoNetwork.
the class EntityToMLD method createConstraints.
private void createConstraints(Entity entity, Table table) {
// Collection constraints to be created
Collection<String> expressions = AnnotationHelper.getAllConstraints(entity);
// Add all newly created constraints
for (EObject object : getObjectsToBeKept()) {
if (object instanceof Constraint) {
Constraint existingConstraint = (Constraint) object;
if (table.equals(existingConstraint.getOwner())) {
expressions.add(existingConstraint.getExpression());
}
}
}
// Reuse some of the existing constraints
Collection<Constraint> constraintsToBeDeleted = new ArrayList<Constraint>();
for (Constraint existingConstraint : table.getConstraints()) {
// Check if the constraint should still exist
if (expressions.contains(existingConstraint.getExpression())) {
// Keep the constraint
expressions.remove(existingConstraint.getExpression());
existingConstraint.setName(getConstraintName(existingConstraint));
addToObjectsToBeKept(existingConstraint);
} else {
// The constraint will be removed
constraintsToBeDeleted.add(existingConstraint);
}
}
// Delete useless constraint
for (Constraint constraint : constraintsToBeDeleted) {
EcoreUtil.delete(constraint, true);
}
// Create new constraints
for (String expression : expressions) {
Constraint constraint = DatabaseFactory.eINSTANCE.createConstraint();
table.getConstraints().add(constraint);
constraint.setName(getConstraintName(constraint));
constraint.setExpression(expression);
addToObjectsToBeKept(constraint);
}
}
use of org.obeonetwork.dsl.database.Constraint in project InformationSystem by ObeoNetwork.
the class MpdToMldBidiRules method createTable.
private void createTable(Table sourceTable, TableContainer targetTableContainer) {
Table targetTable = getFromInputTraceabilityMap(sourceTable, DatabasePackage.Literals.TABLE);
if (targetTable != null) {
// Reuse existing table
} else {
// Create a new table
targetTable = DatabaseFactory.eINSTANCE.createTable();
targetTableContainer.getTables().add(targetTable);
}
addToOutputTraceability(sourceTable, targetTable);
targetTable.setName(sourceTable.getName());
targetTable.setComments(sourceTable.getComments());
for (Column column : sourceTable.getColumns()) {
createColumn(column, targetTable);
}
if (sourceTable.getPrimaryKey() != null) {
createPK(sourceTable.getPrimaryKey(), targetTable);
}
for (Index index : sourceTable.getIndexes()) {
createIndex(index, targetTable);
}
for (Constraint constraint : sourceTable.getConstraints()) {
createConstraint(constraint, targetTable);
}
}
use of org.obeonetwork.dsl.database.Constraint in project InformationSystem by ObeoNetwork.
the class PostGresDataBaseBuilder method buildColumnConstraint.
@Override
protected void buildColumnConstraint(DatabaseMetaData metaData, TableContainer owner, Column column) {
Table table = column.getOwner();
String key = table.getName().toUpperCase();
// we add all constraints for a table at the same time
if (cacheConstraints == null) {
cacheConstraints = new HashMap<String, Collection<PostgreSQLConstraint>>();
ResultSet rs = null;
PreparedStatement pstmt = null;
try {
PreparedStatement psmt = metaData.getConnection().prepareStatement("SELECT tc.constraint_name, pgc.consrc , tc.table_name " + "FROM information_schema.table_constraints tc " + "LEFT JOIN pg_catalog.pg_constraint pgc " + "ON pgc.conname = tc.constraint_name " + "WHERE tc.table_schema = ? and tc.constraint_type = 'CHECK' and tc.constraint_name not like '%_not_null'");
psmt.setString(1, schemaName);
rs = psmt.executeQuery();
while (rs.next()) {
String name = rs.getString(1);
String expression = rs.getString(2);
String tableName = rs.getString(3);
String key2 = tableName.toUpperCase();
Collection<PostgreSQLConstraint> constraints = cacheConstraints.get(key2);
if (constraints == null) {
constraints = new ArrayList<PostgreSQLConstraint>();
}
constraints.add(new PostgreSQLConstraint(tableName, name, expression));
cacheConstraints.put(key2, constraints);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
JdbcUtils.closeStatement(pstmt);
JdbcUtils.closeResultSet(rs);
}
}
if (cacheConstraints.containsKey(key)) {
for (PostgreSQLConstraint pgConstraint : cacheConstraints.get(key)) {
Constraint constraint = CreationUtils.createConstraint(table, pgConstraint.name);
constraint.setExpression(pgConstraint.expression);
}
// We remove from the cache or the constraints would be added for every column in the table
cacheConstraints.remove(key);
}
}
use of org.obeonetwork.dsl.database.Constraint in project InformationSystem by ObeoNetwork.
the class RemoveConstraintItemProvider method getText.
/**
* This returns the label text for the adapted class.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated NOT
*/
@Override
public String getText(Object object) {
RemoveConstraint removeConstraint = (RemoveConstraint) object;
Constraint constraint = removeConstraint.getConstraint();
return getString("_UI_RemoveConstraint_type") + " " + constraint.getName();
}
use of org.obeonetwork.dsl.database.Constraint in project InformationSystem by ObeoNetwork.
the class ConstraintChangeItemProvider method getText.
/**
* This returns the label text for the adapted class.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated NOT
*/
@Override
public String getText(Object object) {
ConstraintChange constraintChange = (ConstraintChange) object;
Constraint constraint = constraintChange.getConstraint();
return getString("_UI_ConstraintChange_type") + " " + constraint.getName();
}
Aggregations