Search in sources :

Example 1 with DBSEntityReferrer

use of org.jkiss.dbeaver.model.struct.DBSEntityReferrer in project dbeaver by serge-rider.

the class AssociationPart method setSelected.

/**
     * Sets the width of the line when selected
     */
@Override
public void setSelected(int value) {
    super.setSelected(value);
    if (value != EditPart.SELECTED_NONE) {
        ((PolylineConnection) getFigure()).setLineWidth(2);
    } else {
        ((PolylineConnection) getFigure()).setLineWidth(1);
    }
    if (getSource() == null || getTarget() == null) {
        // This part seems to be deleted
        return;
    }
    DBSEntityAssociation association = getAssociation().getObject();
    if (association instanceof DBSEntityReferrer && association.getReferencedConstraint() instanceof DBSEntityReferrer) {
        List<AttributePart> sourceAttributes = getEntityAttributes((EntityPart) getSource(), DBUtils.getEntityAttributes(VoidProgressMonitor.INSTANCE, (DBSEntityReferrer) association.getReferencedConstraint()));
        List<AttributePart> targetAttributes = getEntityAttributes((EntityPart) getTarget(), DBUtils.getEntityAttributes(VoidProgressMonitor.INSTANCE, (DBSEntityReferrer) association));
        Color columnColor = value != EditPart.SELECTED_NONE ? Display.getDefault().getSystemColor(SWT.COLOR_RED) : getViewer().getControl().getForeground();
        for (AttributePart attr : sourceAttributes) {
            attr.getFigure().setForegroundColor(columnColor);
        }
        for (AttributePart attr : targetAttributes) {
            attr.getFigure().setForegroundColor(columnColor);
        }
    }
}
Also used : DBSEntityAssociation(org.jkiss.dbeaver.model.struct.DBSEntityAssociation) DBSEntityReferrer(org.jkiss.dbeaver.model.struct.DBSEntityReferrer) Color(org.eclipse.swt.graphics.Color)

Example 2 with DBSEntityReferrer

use of org.jkiss.dbeaver.model.struct.DBSEntityReferrer in project dbeaver by serge-rider.

the class SQLForeignKeyManager method getNestedDeclaration.

@Override
protected StringBuilder getNestedDeclaration(TABLE_TYPE owner, DBECommandAbstract<OBJECT_TYPE> command) {
    OBJECT_TYPE foreignKey = command.getObject();
    boolean legacySyntax = isLegacyForeignKeySyntax(owner);
    // Create column
    String constraintName = DBUtils.getQuotedIdentifier(foreignKey.getDataSource(), foreignKey.getName());
    StringBuilder decl = new StringBuilder(40);
    if (!legacySyntax || !foreignKey.isPersisted()) {
        decl.append("CONSTRAINT ");
    }
    if (!legacySyntax) {
        //$NON-NLS-1$
        decl.append(constraintName).append(" ");
    }
    //$NON-NLS-1$
    decl.append(foreignKey.getConstraintType().getName().toUpperCase(Locale.ENGLISH)).append(//$NON-NLS-1$
    " (");
    try {
        // Get columns using void monitor
        final Collection<? extends DBSEntityAttributeRef> columns = command.getObject().getAttributeReferences(VoidProgressMonitor.INSTANCE);
        boolean firstColumn = true;
        for (DBSEntityAttributeRef constraintColumn : CommonUtils.safeCollection(columns)) {
            final DBSEntityAttribute attribute = constraintColumn.getAttribute();
            //$NON-NLS-1$
            if (!firstColumn)
                decl.append(",");
            firstColumn = false;
            if (attribute != null) {
                decl.append(DBUtils.getQuotedIdentifier(attribute));
            }
        }
    } catch (DBException e) {
        log.error("Can't obtain reference attributes", e);
    }
    final DBSEntityConstraint refConstraint = foreignKey.getReferencedConstraint();
    //$NON-NLS-1$ //$NON-NLS-2$
    decl.append(") REFERENCES ").append(refConstraint == null ? "<?>" : DBUtils.getObjectFullName(refConstraint.getParentObject(), DBPEvaluationContext.DDL)).append("(");
    if (refConstraint instanceof DBSEntityReferrer) {
        try {
            boolean firstColumn = true;
            List<? extends DBSEntityAttributeRef> columns = ((DBSEntityReferrer) refConstraint).getAttributeReferences(VoidProgressMonitor.INSTANCE);
            for (DBSEntityAttributeRef constraintColumn : CommonUtils.safeCollection(columns)) {
                //$NON-NLS-1$
                if (!firstColumn)
                    decl.append(",");
                firstColumn = false;
                final DBSEntityAttribute attribute = constraintColumn.getAttribute();
                if (attribute != null) {
                    decl.append(DBUtils.getQuotedIdentifier(attribute));
                }
            }
        } catch (DBException e) {
            log.error("Can't obtain ref constraint reference attributes", e);
        }
    }
    //$NON-NLS-1$
    decl.append(")");
    if (foreignKey.getDeleteRule() != null && !CommonUtils.isEmpty(foreignKey.getDeleteRule().getClause())) {
        //$NON-NLS-1$
        decl.append(" ON DELETE ").append(foreignKey.getDeleteRule().getClause());
    }
    if (foreignKey.getUpdateRule() != null && !CommonUtils.isEmpty(foreignKey.getUpdateRule().getClause())) {
        //$NON-NLS-1$
        decl.append(" ON UPDATE ").append(foreignKey.getUpdateRule().getClause());
    }
    if (legacySyntax) {
        //$NON-NLS-1$
        decl.append(" CONSTRAINT ").append(constraintName);
    }
    return decl;
}
Also used : DBSEntityAttributeRef(org.jkiss.dbeaver.model.struct.DBSEntityAttributeRef) DBException(org.jkiss.dbeaver.DBException) DBSEntityReferrer(org.jkiss.dbeaver.model.struct.DBSEntityReferrer) DBSEntityConstraint(org.jkiss.dbeaver.model.struct.DBSEntityConstraint) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute)

Aggregations

DBSEntityReferrer (org.jkiss.dbeaver.model.struct.DBSEntityReferrer)2 Color (org.eclipse.swt.graphics.Color)1 DBException (org.jkiss.dbeaver.DBException)1 DBSEntityAssociation (org.jkiss.dbeaver.model.struct.DBSEntityAssociation)1 DBSEntityAttribute (org.jkiss.dbeaver.model.struct.DBSEntityAttribute)1 DBSEntityAttributeRef (org.jkiss.dbeaver.model.struct.DBSEntityAttributeRef)1 DBSEntityConstraint (org.jkiss.dbeaver.model.struct.DBSEntityConstraint)1