Search in sources :

Example 1 with DBVEntityForeignKey

use of org.jkiss.dbeaver.model.virtual.DBVEntityForeignKey in project dbeaver by serge-rider.

the class AssociationDeleteCommand method execute.

/**
 * Removes the relationship
 */
@Override
public void execute() {
    DBSEntityAssociation entityAssociation = association.getObject();
    if (entityAssociation instanceof DBVEntityForeignKey) {
        if (!UIUtils.confirmAction("Delete logical key", "Are you sure you want to delete logical key '" + part.getAssociation().getName() + "'?")) {
            return;
        }
        DBVEntity vEntity = DBVUtils.getVirtualEntity(entityAssociation.getParentObject(), false);
        if (vEntity == null) {
            UIUtils.showMessageBox(UIUtils.getActiveWorkbenchShell(), "No virtual entity", "Can't find association owner virtual entity", SWT.ICON_ERROR);
            return;
        }
        vEntity.removeForeignKey((DBVEntityForeignKey) entityAssociation);
    }
    part.markAssociatedAttributes(EditPart.SELECTED_NONE);
    targetEntity.removeReferenceAssociation(association, true);
    sourceEntity.removeAssociation(association, true);
    association.setSourceEntity(null);
    association.setTargetEntity(null);
}
Also used : DBSEntityAssociation(org.jkiss.dbeaver.model.struct.DBSEntityAssociation) DBVEntityForeignKey(org.jkiss.dbeaver.model.virtual.DBVEntityForeignKey) DBVEntity(org.jkiss.dbeaver.model.virtual.DBVEntity)

Example 2 with DBVEntityForeignKey

use of org.jkiss.dbeaver.model.virtual.DBVEntityForeignKey in project dbeaver by serge-rider.

the class AssociationCreateCommand method execute.

@Override
public void execute() {
    if (sourceEntity instanceof ERDEntity && targetEntity instanceof ERDEntity) {
        DBSEntity srcEntityObject = ((ERDEntity) sourceEntity).getObject();
        DBSEntity targetEntityObject = ((ERDEntity) targetEntity).getObject();
        List<DBSEntityAttribute> srcAttrs = ERDUtils.getObjectsFromERD(sourceAttributes);
        List<DBSEntityAttribute> refAttrs = ERDUtils.getObjectsFromERD(targetAttributes);
        DBVEntity vEntity = DBVUtils.getVirtualEntity(srcEntityObject, true);
        DBVEntityForeignKey vfk = EditForeignKeyPage.createVirtualForeignKey(vEntity, targetEntityObject, new EditForeignKeyPage.FKType[] { EditForeignKeyPage.FK_TYPE_LOGICAL }, srcAttrs, refAttrs);
        if (vfk == null) {
            return;
        }
        vEntity.persistConfiguration();
        association = new ERDAssociation(vfk, (ERDEntity) sourceEntity, (ERDEntity) targetEntity, true);
    } else {
        association = createAssociation(sourceEntity, targetEntity, true);
    }
}
Also used : DBVEntityForeignKey(org.jkiss.dbeaver.model.virtual.DBVEntityForeignKey) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) EditForeignKeyPage(org.jkiss.dbeaver.ui.editors.object.struct.EditForeignKeyPage) DBSEntity(org.jkiss.dbeaver.model.struct.DBSEntity) DBVEntity(org.jkiss.dbeaver.model.virtual.DBVEntity)

Example 3 with DBVEntityForeignKey

use of org.jkiss.dbeaver.model.virtual.DBVEntityForeignKey in project dbeaver by serge-rider.

the class AssociationDeleteCommand method undo.

/**
 * Restores the relationship
 */
@Override
public void undo() {
    if (association.getSourceEntity() != null) {
        return;
    }
    DBSEntityAssociation entityAssociation = association.getObject();
    if (entityAssociation instanceof DBVEntityForeignKey) {
        DBVEntity vEntity = DBVUtils.getVirtualEntity(entityAssociation.getParentObject(), false);
        if (vEntity != null) {
            vEntity.addForeignKey((DBVEntityForeignKey) entityAssociation);
        }
    }
    association.setSourceEntity(sourceEntity);
    association.setTargetEntity(targetEntity);
    sourceEntity.addAssociation(association, true);
    targetEntity.addReferenceAssociation(association, true);
}
Also used : DBSEntityAssociation(org.jkiss.dbeaver.model.struct.DBSEntityAssociation) DBVEntityForeignKey(org.jkiss.dbeaver.model.virtual.DBVEntityForeignKey) DBVEntity(org.jkiss.dbeaver.model.virtual.DBVEntity)

Example 4 with DBVEntityForeignKey

use of org.jkiss.dbeaver.model.virtual.DBVEntityForeignKey in project dbeaver by serge-rider.

the class PostgreFDWConfigWizard method collectAvailableDataSources.

void collectAvailableDataSources(DBRProgressMonitor monitor) {
    if (availableDataSources != null) {
        return;
    }
    Set<DBPDataSourceContainer> dataSources = new LinkedHashSet<>();
    Set<DBSEntity> entities = new LinkedHashSet<>();
    DBPDataSourceContainer curDataSource = database.getDataSource().getContainer();
    // Find all virtual connections
    DBVModel vModel = curDataSource.getVirtualModel();
    monitor.beginTask("Check virtual foreign keys", 1);
    collectAvailableDataSources(monitor, vModel, dataSources, entities);
    monitor.done();
    DBNModel navModel = DBWorkbench.getPlatform().getNavigatorModel();
    // Check global FK references cache
    Map<String, List<DBVEntityForeignKey>> grCache = DBVModel.getGlobalReferenceCache();
    monitor.beginTask("Check external references", grCache.size());
    for (Map.Entry<String, List<DBVEntityForeignKey>> grEntry : grCache.entrySet()) {
        DBNDataSource refDataSource = navModel.getDataSourceByPath(database.getDataSource().getContainer().getProject(), grEntry.getKey());
        if (refDataSource != null && refDataSource.getDataSourceContainer() == curDataSource) {
            try {
                for (DBVEntityForeignKey rfk : grEntry.getValue()) {
                    monitor.subTask("Check " + rfk.getEntity().getFullyQualifiedName(DBPEvaluationContext.UI));
                    DBSEntity refEntity = rfk.getEntity().getRealEntity(monitor);
                    if (refEntity != null) {
                        dataSources.add(refEntity.getDataSource().getContainer());
                        entities.add(refEntity);
                    }
                }
            } catch (DBException e) {
                log.debug("Error getting referenced entity", e);
            }
        }
        monitor.worked(1);
    }
    monitor.done();
    // Check already configured FDW
    // Done
    availableDataSources = new ArrayList<>(dataSources);
    proposedEntities = new ArrayList<>(entities);
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBVEntityForeignKey(org.jkiss.dbeaver.model.virtual.DBVEntityForeignKey) DBNDataSource(org.jkiss.dbeaver.model.navigator.DBNDataSource) DBVModel(org.jkiss.dbeaver.model.virtual.DBVModel) DBNModel(org.jkiss.dbeaver.model.navigator.DBNModel) DBSEntity(org.jkiss.dbeaver.model.struct.DBSEntity) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Example 5 with DBVEntityForeignKey

use of org.jkiss.dbeaver.model.virtual.DBVEntityForeignKey in project dbeaver by serge-rider.

the class DBDAttributeBinding method findVirtualReferrers.

protected List<DBSEntityReferrer> findVirtualReferrers() {
    DBSDataContainer dataContainer = getDataContainer();
    if (dataContainer instanceof DBSEntity) {
        DBSEntity attrEntity = (DBSEntity) dataContainer;
        DBVEntity vEntity = DBVUtils.getVirtualEntity(attrEntity, false);
        if (vEntity != null) {
            List<DBVEntityForeignKey> foreignKeys = vEntity.getForeignKeys();
            if (!CommonUtils.isEmpty(foreignKeys)) {
                List<DBSEntityReferrer> referrers = null;
                for (DBVEntityForeignKey vfk : foreignKeys) {
                    for (DBVEntityForeignKeyColumn vfkc : vfk.getAttributes()) {
                        if (CommonUtils.equalObjects(vfkc.getAttributeName(), getFullyQualifiedName(DBPEvaluationContext.DML))) {
                            if (referrers == null) {
                                referrers = new ArrayList<>();
                            }
                            referrers.add(vfk);
                        }
                    }
                }
                return referrers;
            }
        }
    }
    return null;
}
Also used : DBVEntityForeignKeyColumn(org.jkiss.dbeaver.model.virtual.DBVEntityForeignKeyColumn) DBVEntityForeignKey(org.jkiss.dbeaver.model.virtual.DBVEntityForeignKey) DBVEntity(org.jkiss.dbeaver.model.virtual.DBVEntity)

Aggregations

DBVEntityForeignKey (org.jkiss.dbeaver.model.virtual.DBVEntityForeignKey)5 DBVEntity (org.jkiss.dbeaver.model.virtual.DBVEntity)4 DBSEntity (org.jkiss.dbeaver.model.struct.DBSEntity)2 DBSEntityAssociation (org.jkiss.dbeaver.model.struct.DBSEntityAssociation)2 DBException (org.jkiss.dbeaver.DBException)1 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)1 DBNDataSource (org.jkiss.dbeaver.model.navigator.DBNDataSource)1 DBNModel (org.jkiss.dbeaver.model.navigator.DBNModel)1 DBSEntityAttribute (org.jkiss.dbeaver.model.struct.DBSEntityAttribute)1 DBVEntityForeignKeyColumn (org.jkiss.dbeaver.model.virtual.DBVEntityForeignKeyColumn)1 DBVModel (org.jkiss.dbeaver.model.virtual.DBVModel)1 EditForeignKeyPage (org.jkiss.dbeaver.ui.editors.object.struct.EditForeignKeyPage)1