提交 91d8b9d3 编写于 作者: S serge-rider

#1652 Keep ERD association attributes cached

上级 91d08e32
......@@ -136,6 +136,8 @@ public class ERDEditorEmbedded extends ERDEditorPart implements IDatabaseEditor,
public EntityDiagram evaluate(DBRProgressMonitor monitor)
throws InvocationTargetException, InterruptedException
{
// Do not refresh actual metadata. It is slow and it may corrupt diagram state
/*
if (refreshMetadata && object instanceof DBPRefreshableObject) {
try {
getEditorInput().getNavigatorNode().refreshNode(monitor, ERDEditorEmbedded.this);
......@@ -143,6 +145,7 @@ public class ERDEditorEmbedded extends ERDEditorPart implements IDatabaseEditor,
log.warn("Error refreshing database metadata", e);
}
}
*/
try {
return loadFromDatabase(monitor);
} catch (DBException e) {
......
......@@ -20,9 +20,13 @@ import org.eclipse.draw2d.geometry.Point;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSEntityAssociation;
import org.jkiss.dbeaver.model.struct.DBSEntityAttribute;
import org.jkiss.dbeaver.model.struct.DBSEntityAttributeRef;
import org.jkiss.dbeaver.model.struct.DBSEntityReferrer;
import org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKeyColumn;
import org.jkiss.utils.CommonUtils;
import java.util.ArrayList;
import java.util.Collections;
......@@ -56,7 +60,7 @@ public class ERDAssociation extends ERDObject<DBSEntityAssociation>
sourceEntity,
sourceEntity.getObject().getName() + " -> " + targetEntity.getObject().getName(),
"",
new ERDLogicalPrimaryKey(targetEntity, "Primary key", "")));
new ERDLogicalPrimaryKey(targetEntity, "Logical primary key", "")));
this.targetEntity = targetEntity;
this.sourceEntity = sourceEntity;
this.targetEntity.addPrimaryKeyRelationship(this, reflect);
......@@ -65,20 +69,59 @@ public class ERDAssociation extends ERDObject<DBSEntityAssociation>
/**
* Constructor for physical association
* @param object physical FK
* @param association physical FK
* @param sourceEntity fk table
* @param targetEntity pk table
* @param reflect reflect flag
*/
public ERDAssociation(DBSEntityAssociation object, ERDEntity sourceEntity, ERDEntity targetEntity, boolean reflect)
public ERDAssociation(DBSEntityAssociation association, ERDEntity sourceEntity, ERDEntity targetEntity, boolean reflect)
{
super(object);
super(association);
this.targetEntity = targetEntity;
this.sourceEntity = sourceEntity;
// Resolve association attributes
if (association instanceof DBSEntityReferrer) {
resolveAttributes((DBSEntityReferrer) association, sourceEntity, targetEntity);
}
this.targetEntity.addPrimaryKeyRelationship(this, reflect);
this.sourceEntity.addForeignKeyRelationship(this, reflect);
}
private void resolveAttributes(DBSEntityReferrer association, ERDEntity sourceEntity, ERDEntity targetEntity) {
try {
List<? extends DBSEntityAttributeRef> attrRefs = association.getAttributeReferences(new VoidProgressMonitor());
if (!CommonUtils.isEmpty(attrRefs)) {
for (DBSEntityAttributeRef attrRef : attrRefs) {
if (attrRef instanceof DBSTableForeignKeyColumn) {
DBSEntityAttribute sourceAttr = ((DBSTableForeignKeyColumn) attrRef).getReferencedColumn();
DBSEntityAttribute targetAttr = attrRef.getAttribute();
if (sourceAttr != null && targetAttr != null) {
ERDEntityAttribute erdSourceAttr = getAttributeByModel(sourceEntity, sourceAttr);
ERDEntityAttribute erdTargetAttr = getAttributeByModel(targetEntity, targetAttr);
if (erdSourceAttr != null && erdTargetAttr != null) {
addCondition(erdSourceAttr, erdTargetAttr);
}
}
}
}
}
} catch (DBException e) {
log.error("Error resolving ERD association attributes", e);
}
}
private static ERDEntityAttribute getAttributeByModel(ERDEntity entity, DBSEntityAttribute attr) {
for (ERDEntityAttribute erdAttr : entity.getAttributes()) {
if (erdAttr.getObject() == attr) {
return erdAttr;
}
}
return null;
}
public boolean isLogical()
{
return getObject() instanceof ERDLogicalAssociation;
......
......@@ -30,13 +30,11 @@ import org.jkiss.dbeaver.model.struct.DBSEntityAttribute;
* @author Serge Rider
*/
public class ERDEntityAttribute extends ERDObject<DBSEntityAttribute> {
private ERDEntity entity;
private boolean inPrimaryKey;
private boolean inForeignKey;
public ERDEntityAttribute(ERDEntity entity, DBSEntityAttribute attribute, boolean inPrimaryKey) {
public ERDEntityAttribute(DBSEntityAttribute attribute, boolean inPrimaryKey) {
super(attribute);
this.entity = entity;
this.inPrimaryKey = inPrimaryKey;
}
......
......@@ -24,6 +24,7 @@ import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.struct.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
......@@ -35,7 +36,6 @@ public class ERDLogicalAssociation implements DBSEntityAssociation, DBSEntityRef
private String name;
private String description;
private ERDLogicalPrimaryKey pk;
private List<? extends DBSEntityAttributeRef> attributes = new ArrayList<>();
public ERDLogicalAssociation(ERDEntity entity, String name, String description, ERDLogicalPrimaryKey pk)
{
......@@ -102,6 +102,6 @@ public class ERDLogicalAssociation implements DBSEntityAssociation, DBSEntityRef
@Override
public List<? extends DBSEntityAttributeRef> getAttributeReferences(DBRProgressMonitor monitor)
{
return attributes;
return Collections.emptyList();
}
}
......@@ -136,7 +136,7 @@ public class ERDUtils
break;
}
boolean inPrimaryKey = idColumns != null && idColumns.contains(attribute);
ERDEntityAttribute c1 = new ERDEntityAttribute(erdEntity, attribute, inPrimaryKey);
ERDEntityAttribute c1 = new ERDEntityAttribute(attribute, inPrimaryKey);
erdEntity.addAttribute(c1, false);
}
}
......
......@@ -30,6 +30,7 @@ import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.jkiss.dbeaver.ext.erd.model.ERDAssociation;
import org.jkiss.dbeaver.ext.erd.model.ERDEntityAttribute;
import org.jkiss.dbeaver.ext.erd.model.ERDUtils;
import org.jkiss.dbeaver.ext.erd.policy.AssociationBendEditPolicy;
import org.jkiss.dbeaver.ext.erd.policy.AssociationEditPolicy;
......@@ -212,33 +213,24 @@ public class AssociationPart extends PropertyAwareConnectionPart {
return;
}
DBSEntityAssociation association = getAssociation().getObject();
if (association instanceof DBSEntityReferrer && association.getReferencedConstraint() instanceof DBSEntityReferrer) {
List<AttributePart> sourceAttributes = getEntityAttributes(
(EntityPart)getSource(),
DBUtils.getEntityAttributes(new VoidProgressMonitor(), (DBSEntityReferrer) association.getReferencedConstraint()));
List<AttributePart> targetAttributes = getEntityAttributes(
(EntityPart)getTarget(),
DBUtils.getEntityAttributes(new VoidProgressMonitor(), (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);
}
Color columnColor = value != EditPart.SELECTED_NONE ? Display.getDefault().getSystemColor(SWT.COLOR_RED) : getViewer().getControl().getForeground();
for (AttributePart attrPart : getEntityAttributes((EntityPart)getSource(), getAssociation().getSourceAttributes())) {
attrPart.getFigure().setForegroundColor(columnColor);
}
for (AttributePart attrPart : getEntityAttributes((EntityPart)getTarget(), getAssociation().getTargetAttributes())) {
attrPart.getFigure().setForegroundColor(columnColor);
}
}
private List<AttributePart> getEntityAttributes(EntityPart source, Collection<? extends DBSEntityAttribute> columns)
private List<AttributePart> getEntityAttributes(EntityPart source, List<ERDEntityAttribute> columns)
{
List<AttributePart> erdColumns = new ArrayList<>(source.getChildren());
for (Iterator<AttributePart> iter = erdColumns.iterator(); iter.hasNext(); ) {
if (!columns.contains(iter.next().getAttribute().getObject())) {
iter.remove();
List<AttributePart> result = new ArrayList<>();
for (AttributePart attrPart : (List<AttributePart>)source.getChildren()) {
if (columns.contains(attrPart.getAttribute())) {
result.add(attrPart);
}
}
return erdColumns;
return result;
}
@Override
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册