Search in sources :

Example 6 with Constraint

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);
    }
}
Also used : Constraint(org.obeonetwork.dsl.database.Constraint) EObject(org.eclipse.emf.ecore.EObject) ArrayList(java.util.ArrayList)

Example 7 with 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);
    }
}
Also used : Table(org.obeonetwork.dsl.database.Table) AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Column(org.obeonetwork.dsl.database.Column) Constraint(org.obeonetwork.dsl.database.Constraint) Index(org.obeonetwork.dsl.database.Index)

Example 8 with Constraint

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);
    }
}
Also used : Table(org.obeonetwork.dsl.database.Table) AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Constraint(org.obeonetwork.dsl.database.Constraint) ResultSet(java.sql.ResultSet) Collection(java.util.Collection) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException)

Example 9 with Constraint

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();
}
Also used : Constraint(org.obeonetwork.dsl.database.Constraint) RemoveConstraint(org.obeonetwork.dsl.database.dbevolution.RemoveConstraint) RemoveConstraint(org.obeonetwork.dsl.database.dbevolution.RemoveConstraint)

Example 10 with Constraint

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();
}
Also used : Constraint(org.obeonetwork.dsl.database.Constraint) ConstraintChange(org.obeonetwork.dsl.database.dbevolution.ConstraintChange)

Aggregations

Constraint (org.obeonetwork.dsl.database.Constraint)21 Table (org.obeonetwork.dsl.database.Table)5 ArrayList (java.util.ArrayList)4 EObject (org.eclipse.emf.ecore.EObject)4 Column (org.obeonetwork.dsl.database.Column)4 AbstractTable (org.obeonetwork.dsl.database.AbstractTable)3 UpdateConstraint (org.obeonetwork.dsl.database.dbevolution.UpdateConstraint)3 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)2 Index (org.obeonetwork.dsl.database.Index)2 AddConstraint (org.obeonetwork.dsl.database.dbevolution.AddConstraint)2 RemoveConstraint (org.obeonetwork.dsl.database.dbevolution.RemoveConstraint)2 Entity (org.obeonetwork.dsl.entity.Entity)2 Collection (java.util.Collection)1 EObjectPropertiesEditionContext (org.eclipse.emf.eef.runtime.context.impl.EObjectPropertiesEditionContext)1 EReferencePropertiesEditionContext (org.eclipse.emf.eef.runtime.context.impl.EReferencePropertiesEditionContext)1 PropertiesEditingPolicy (org.eclipse.emf.eef.runtime.policies.PropertiesEditingPolicy)1 CreateEditingPolicy (org.eclipse.emf.eef.runtime.policies.impl.CreateEditingPolicy)1