Search in sources :

Example 16 with Constraint

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

the class MLDToEntity method createEntity.

private void createEntity(Table table) {
    Entity entity = getFromInputTraceabilityMap(table, EntityPackage.Literals.ENTITY);
    if (entity == null) {
        // The entity does not already exist
        // We have to create a new one
        entity = EntityFactory.eINSTANCE.createEntity();
        getTargetNamespace(table.getOwner()).getTypes().add(entity);
        entity.setName(LabelProvider.getEntityNameFromTable(table));
    }
    // Add to new traceability map
    addToOutputTraceability(table, entity);
    // The following properties are modified even if they already existed
    AnnotationHelper.setPhysicalNameAnnotation(entity, LabelProvider.getEntityPhysicalNameFromTable(table));
    entity.setDescription(table.getComments());
    AnnotationHelper.removePhysicalUniqueAnnotations(entity);
    for (Column column : table.getColumns()) {
        if (!column.isInForeignKey() && !column.isInPrimaryKey() && !AdditionalFieldsUtils.isAdditionalColumn(column)) {
            createAttribute(column, entity);
        }
    }
    AnnotationHelper.removePhysicalCheckAnnotations(entity);
    List<Constraint> constraintsToCreate = new ArrayList<Constraint>();
    for (Constraint constraint : table.getConstraints()) {
        if (!AdditionalFieldsUtils.isAdditionalConstraint(constraint)) {
            constraintsToCreate.add(constraint);
        }
    }
    AnnotationHelper.setAllConstraints(entity, constraintsToCreate);
}
Also used : Entity(org.obeonetwork.dsl.entity.Entity) Column(org.obeonetwork.dsl.database.Column) Constraint(org.obeonetwork.dsl.database.Constraint) ArrayList(java.util.ArrayList)

Example 17 with Constraint

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

the class MpdToMldBidiRules method createConstraint.

private void createConstraint(Constraint sourceConstraint, Table targetTable) {
    Constraint targetConstraint = getFromInputTraceabilityMap(sourceConstraint, DatabasePackage.Literals.CONSTRAINT);
    if (targetConstraint != null) {
        // ensure it is contained by the right table
        if (!EcoreUtil.equals(targetConstraint.getOwner(), targetTable)) {
            targetTable.getConstraints().add(targetConstraint);
        }
    } else {
        // We have to create a new constraint
        targetConstraint = DatabaseFactory.eINSTANCE.createConstraint();
        targetTable.getConstraints().add(targetConstraint);
    }
    addToOutputTraceability(sourceConstraint, targetConstraint);
    targetConstraint.setName(sourceConstraint.getName());
    targetConstraint.setExpression(sourceConstraint.getExpression());
    targetConstraint.setComments(sourceConstraint.getComments());
}
Also used : Constraint(org.obeonetwork.dsl.database.Constraint)

Example 18 with Constraint

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

the class TableConstraintsPropertiesEditionComponent method updateSemanticModel.

/**
 * {@inheritDoc}
 * @see org.eclipse.emf.eef.runtime.impl.components.StandardPropertiesEditionComponent#updateSemanticModel(org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionEvent)
 */
public void updateSemanticModel(final IPropertiesEditionEvent event) {
    Table table = (Table) semanticObject;
    if (DatabaseViewsRepository.Constraints.Properties.constraints_ == event.getAffectedEditor()) {
        if (event.getKind() == PropertiesEditionEvent.ADD) {
            EReferencePropertiesEditionContext context = new EReferencePropertiesEditionContext(editingContext, this, constraintsSettings, editingContext.getAdapterFactory());
            PropertiesEditingProvider provider = (PropertiesEditingProvider) editingContext.getAdapterFactory().adapt(semanticObject, PropertiesEditingProvider.class);
            if (provider != null) {
                PropertiesEditingPolicy policy = provider.getPolicy(context);
                if (policy instanceof CreateEditingPolicy) {
                    policy.execute();
                }
            }
        } else if (event.getKind() == PropertiesEditionEvent.EDIT) {
            EObjectPropertiesEditionContext context = new EObjectPropertiesEditionContext(editingContext, this, (EObject) event.getNewValue(), editingContext.getAdapterFactory());
            PropertiesEditingProvider provider = (PropertiesEditingProvider) editingContext.getAdapterFactory().adapt((EObject) event.getNewValue(), PropertiesEditingProvider.class);
            if (provider != null) {
                PropertiesEditingPolicy editionPolicy = provider.getPolicy(context);
                if (editionPolicy != null) {
                    editionPolicy.execute();
                }
            }
        } else if (event.getKind() == PropertiesEditionEvent.REMOVE) {
            constraintsSettings.removeFromReference((EObject) event.getNewValue());
        } else if (event.getKind() == PropertiesEditionEvent.MOVE) {
            constraintsSettings.move(event.getNewIndex(), (Constraint) event.getNewValue());
        }
    }
}
Also used : EObjectPropertiesEditionContext(org.eclipse.emf.eef.runtime.context.impl.EObjectPropertiesEditionContext) Table(org.obeonetwork.dsl.database.Table) PropertiesEditingProvider(org.eclipse.emf.eef.runtime.providers.PropertiesEditingProvider) Constraint(org.obeonetwork.dsl.database.Constraint) EObject(org.eclipse.emf.ecore.EObject) PropertiesEditingPolicy(org.eclipse.emf.eef.runtime.policies.PropertiesEditingPolicy) CreateEditingPolicy(org.eclipse.emf.eef.runtime.policies.impl.CreateEditingPolicy) EReferencePropertiesEditionContext(org.eclipse.emf.eef.runtime.context.impl.EReferencePropertiesEditionContext)

Example 19 with Constraint

use of org.obeonetwork.dsl.database.Constraint 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)

Example 20 with Constraint

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

the class CreationUtils method createConstraint.

public static Constraint createConstraint(Table owner, String name) {
    Constraint constraint = DatabaseFactory.eINSTANCE.createConstraint();
    constraint.setName(name);
    constraint.setOwner(owner);
    return constraint;
}
Also used : Constraint(org.obeonetwork.dsl.database.Constraint)

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