Search in sources :

Example 6 with DBVEntity

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

the class DBUtils method injectAndFilterAttributeBindings.

public static DBDAttributeBinding[] injectAndFilterAttributeBindings(@NotNull DBPDataSource dataSource, @NotNull DBSDataContainer dataContainer, DBDAttributeBinding[] bindings, boolean filterAttributes) {
    // Add custom attributes
    DBVEntity vEntity = DBVUtils.getVirtualEntity(dataContainer, false);
    if (vEntity != null) {
        List<DBVEntityAttribute> customAttributes = DBVUtils.getCustomAttributes(vEntity);
        if (!CommonUtils.isEmpty(customAttributes)) {
            DBDAttributeBinding[] customBindings = new DBDAttributeBinding[customAttributes.size()];
            for (int i = 0; i < customAttributes.size(); i++) {
                customBindings[i] = new DBDAttributeBindingCustom(null, dataContainer, dataSource, customAttributes.get(i), bindings.length + i);
            }
            DBDAttributeBinding[] combinedAttrs = new DBDAttributeBinding[bindings.length + customBindings.length];
            System.arraycopy(bindings, 0, combinedAttrs, 0, bindings.length);
            System.arraycopy(customBindings, 0, combinedAttrs, bindings.length, customBindings.length);
            bindings = combinedAttrs;
        }
    }
    if (filterAttributes && dataContainer instanceof DBDAttributeFilter) {
        return ((DBDAttributeFilter) dataContainer).filterAttributeBindings(bindings);
    } else {
        return bindings;
    }
}
Also used : DBVEntityAttribute(org.jkiss.dbeaver.model.virtual.DBVEntityAttribute) DBVEntity(org.jkiss.dbeaver.model.virtual.DBVEntity) DBVEntityConstraint(org.jkiss.dbeaver.model.virtual.DBVEntityConstraint)

Example 7 with DBVEntity

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

the class VirtualAttributeAddAction method run.

@Override
public void run() {
    DBVEntity vEntity = resultSetViewer.getModel().getVirtualEntity(false);
    DBVEntityAttribute vAttr = new DBVEntityAttribute(vEntity, null, "vcolumn");
    if (new EditVirtualAttributePage(resultSetViewer, vAttr).edit(resultSetViewer.getControl().getShell())) {
        vAttr.setCustom(true);
        vEntity.addVirtualAttribute(vAttr);
        vEntity.persistConfiguration();
        resultSetViewer.refreshMetaData();
        DBDAttributeConstraint vAttrConstr = resultSetViewer.getModel().getDataFilter().getConstraint(vAttr, false);
        if (vAttrConstr != null) {
        }
    }
}
Also used : DBDAttributeConstraint(org.jkiss.dbeaver.model.data.DBDAttributeConstraint) DBVEntityAttribute(org.jkiss.dbeaver.model.virtual.DBVEntityAttribute) DBVEntity(org.jkiss.dbeaver.model.virtual.DBVEntity)

Example 8 with DBVEntity

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

the class VirtualAttributeEditAction method run.

@Override
public void run() {
    if (attr == null) {
        return;
    }
    DBVEntityAttribute vAttr = ((DBDAttributeBindingCustom) attr).getEntityAttribute();
    DBVEntity vEntity = resultSetViewer.getModel().getVirtualEntity(false);
    if (new EditVirtualAttributePage(resultSetViewer, vAttr).edit(resultSetViewer.getControl().getShell())) {
        vEntity.persistConfiguration();
        resultSetViewer.refreshMetaData();
    }
}
Also used : DBVEntityAttribute(org.jkiss.dbeaver.model.virtual.DBVEntityAttribute) DBDAttributeBindingCustom(org.jkiss.dbeaver.model.data.DBDAttributeBindingCustom) DBVEntity(org.jkiss.dbeaver.model.virtual.DBVEntity)

Example 9 with DBVEntity

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

the class ReferencesResultsContainer method refreshReferenceKeyList.

/**
 * Load list of referencing keys
 */
private void refreshReferenceKeyList() {
    activeReferenceKey = null;
    referenceKeys.clear();
    UIUtils.syncExec(() -> {
        dataViewer.clearData();
        fkCombo.removeAll();
        dataViewer.showEmptyPresentation();
    });
    List<DBDAttributeBinding> visibleAttributes = parentController.getModel().getVisibleAttributes();
    if (visibleAttributes.isEmpty()) {
        return;
    }
    parentDataContainer = parentController.getDataContainer();
    if (parentDataContainer == null) {
        return;
    }
    Set<DBSEntity> allEntities = new LinkedHashSet<>();
    for (DBDAttributeBinding attr : visibleAttributes) {
        DBSEntityAttribute entityAttribute = attr.getEntityAttribute();
        if (entityAttribute != null) {
            allEntities.add(entityAttribute.getParentObject());
        }
    }
    if (allEntities.isEmpty() && parentDataContainer instanceof DBSEntity) {
        allEntities.add((DBSEntity) parentDataContainer);
    }
    List<ReferenceKeyMemo> refKeyMemos = new ArrayList<>();
    {
        DBVEntity vEntityOwner = DBVUtils.getVirtualEntity(parentDataContainer, false);
        if (vEntityOwner != null) {
            Object activeAssociations = vEntityOwner.getProperty(V_PROP_ACTIVE_ASSOCIATIONS);
            if (activeAssociations instanceof Collection) {
                for (Object refKeyMemoMap : (Collection) activeAssociations) {
                    if (refKeyMemoMap instanceof Map) {
                        refKeyMemos.add(new ReferenceKeyMemo((Map) refKeyMemoMap));
                    }
                }
            }
        }
    }
    if (!allEntities.isEmpty()) {
        new AbstractJob("Load reference keys") {

            @Override
            protected IStatus run(DBRProgressMonitor monitor) {
                monitor.beginTask("Load references", allEntities.size());
                try {
                    List<ReferenceKey> refs = new ArrayList<>();
                    for (DBSEntity entity : allEntities) {
                        monitor.subTask(entity.getName());
                        if (entity instanceof DBVEntity) {
                            // Skip virtual entities
                            continue;
                        }
                        // Foreign keys
                        Collection<? extends DBSEntityAssociation> associations = DBVUtils.getAllAssociations(monitor, entity);
                        for (DBSEntityAssociation assoc : associations) {
                            if (assoc instanceof DBSEntityReferrer) {
                                List<? extends DBSEntityAttributeRef> attrs = ((DBSEntityReferrer) assoc).getAttributeReferences(monitor);
                                if (!CommonUtils.isEmpty(attrs)) {
                                    ReferenceKey referenceKey = new ReferenceKey(monitor, false, assoc.getAssociatedEntity(), assoc, attrs);
                                    refs.add(referenceKey);
                                }
                            }
                        }
                        // References
                        Collection<? extends DBSEntityAssociation> references = DBVUtils.getAllReferences(monitor, entity);
                        {
                            for (DBSEntityAssociation assoc : references) {
                                if (assoc instanceof DBSEntityReferrer) {
                                    List<? extends DBSEntityAttributeRef> attrs = ((DBSEntityReferrer) assoc).getAttributeReferences(monitor);
                                    if (!CommonUtils.isEmpty(attrs)) {
                                        ReferenceKey referenceKey = new ReferenceKey(monitor, true, entity, assoc, attrs);
                                        refs.add(referenceKey);
                                    }
                                }
                            }
                        }
                        monitor.worked(1);
                    }
                    synchronized (referenceKeys) {
                        referenceKeys.clear();
                        referenceKeys.addAll(refs);
                        // Detect active ref key from memo
                        if (!referenceKeys.isEmpty()) {
                            if (!refKeyMemos.isEmpty()) {
                                for (ReferenceKey key : referenceKeys) {
                                    for (ReferenceKeyMemo memo : refKeyMemos) {
                                        if (key.matches(memo)) {
                                            activeReferenceKey = key;
                                            break;
                                        }
                                    }
                                    if (activeReferenceKey != null)
                                        break;
                                }
                            }
                            if (activeReferenceKey == null) {
                                activeReferenceKey = referenceKeys.get(0);
                            }
                        }
                    }
                    UIUtils.syncExec(() -> fillKeysCombo());
                } catch (DBException e) {
                    log.debug("Error reading references", e);
                // Do not show errors. References or FKs may be unsupported by current database
                } finally {
                    monitor.done();
                }
                return Status.OK_STATUS;
            }
        }.schedule();
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) IStatus(org.eclipse.core.runtime.IStatus) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) DBVEntity(org.jkiss.dbeaver.model.virtual.DBVEntity) AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 10 with DBVEntity

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

the class VirtualAttributeDeleteAction method run.

@Override
public void run() {
    if (!(attr instanceof DBDAttributeBindingCustom)) {
        return;
    }
    DBVEntityAttribute vAttr = ((DBDAttributeBindingCustom) attr).getEntityAttribute();
    if (!UIUtils.confirmAction(resultSetViewer.getControl().getShell(), "Delete column '" + vAttr.getName() + "'", "Are you sure you want to delete virtual column '" + vAttr.getName() + "'?")) {
        return;
    }
    DBVEntity vEntity = resultSetViewer.getModel().getVirtualEntity(false);
    vEntity.removeVirtualAttribute(vAttr);
    vEntity.persistConfiguration();
    resultSetViewer.refreshMetaData();
}
Also used : DBVEntityAttribute(org.jkiss.dbeaver.model.virtual.DBVEntityAttribute) DBDAttributeBindingCustom(org.jkiss.dbeaver.model.data.DBDAttributeBindingCustom) DBVEntity(org.jkiss.dbeaver.model.virtual.DBVEntity)

Aggregations

DBVEntity (org.jkiss.dbeaver.model.virtual.DBVEntity)22 DBException (org.jkiss.dbeaver.DBException)5 DBVEntityAttribute (org.jkiss.dbeaver.model.virtual.DBVEntityAttribute)5 DBSEntity (org.jkiss.dbeaver.model.struct.DBSEntity)4 DBVEntityForeignKey (org.jkiss.dbeaver.model.virtual.DBVEntityForeignKey)4 DBSTable (org.jkiss.dbeaver.model.struct.rdb.DBSTable)3 DBSTableIndex (org.jkiss.dbeaver.model.struct.rdb.DBSTableIndex)3 DBVColorOverride (org.jkiss.dbeaver.model.virtual.DBVColorOverride)3 RGB (org.eclipse.swt.graphics.RGB)2 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)2 DBDAttributeBindingCustom (org.jkiss.dbeaver.model.data.DBDAttributeBindingCustom)2 DBSEntityAssociation (org.jkiss.dbeaver.model.struct.DBSEntityAssociation)2 DBVEntityConstraint (org.jkiss.dbeaver.model.virtual.DBVEntityConstraint)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 java.util (java.util)1 IStatus (org.eclipse.core.runtime.IStatus)1 Color (org.eclipse.swt.graphics.Color)1 ColorDialog (org.eclipse.swt.widgets.ColorDialog)1 Shell (org.eclipse.swt.widgets.Shell)1 NotNull (org.jkiss.code.NotNull)1