Search in sources :

Example 1 with DBXTreeNode

use of org.jkiss.dbeaver.model.navigator.meta.DBXTreeNode in project dbeaver by serge-rider.

the class CompareObjectsExecutor method compareChildren.

private void compareChildren(DBRProgressMonitor monitor, List<DBNDatabaseNode> nodes) throws DBException, InterruptedException {
    // Compare children
    int nodeCount = nodes.size();
    List<DBNDatabaseNode[]> allChildren = new ArrayList<>(nodeCount);
    for (int i = 0; i < nodeCount; i++) {
        DBNDatabaseNode node = nodes.get(i);
        // Cache structure if possible
        if (node.getObject() instanceof DBSObjectContainer) {
            ((DBSObjectContainer) node.getObject()).cacheStructure(monitor, DBSObjectContainer.STRUCT_ALL);
        }
        try {
            DBNDatabaseNode[] children = node.getChildren(monitor);
            allChildren.add(children);
        } catch (Exception e) {
            log.warn("Error reading child nodes for compare", e);
            allChildren.add(null);
        }
    }
    Set<String> allChildNames = new LinkedHashSet<>();
    for (DBNDatabaseNode[] childList : allChildren) {
        if (childList == null)
            continue;
        for (DBNDatabaseNode child : childList) {
            DBXTreeNode meta = child.getMeta();
            if (meta.isVirtual()) {
                // Skip virtual nodes
                continue;
            }
            if (settings.isSkipSystemObjects() && child.getObject() instanceof DBPSystemObject && ((DBPSystemObject) child.getObject()).isSystem()) {
                // Skip system objects
                continue;
            }
            allChildNames.add(child.getNodeName());
        }
    }
    for (String childName : allChildNames) {
        int[] childIndexes = new int[nodeCount];
        for (int i = 0; i < nodeCount; i++) {
            childIndexes[i] = -1;
            DBNDatabaseNode[] childList = allChildren.get(i);
            if (childList == null)
                continue;
            for (int k = 0; k < childList.length; k++) {
                DBNDatabaseNode child = childList[k];
                if (child.getNodeName().equals(childName)) {
                    childIndexes[i] = k;
                    break;
                }
            }
        }
        List<DBNDatabaseNode> nodesToCompare = new ArrayList<>(nodeCount);
        for (int i = 0; i < nodeCount; i++) {
            if (childIndexes[i] == -1) {
            // Missing
            } else {
                for (int k = 0; k < nodeCount; k++) {
                    if (k != i && childIndexes[k] != childIndexes[i]) {
                        // Wrong index - add to report
                        break;
                    }
                }
                final DBNDatabaseNode[] childList = allChildren.get(i);
                if (childList != null) {
                    nodesToCompare.add(childList[childIndexes[i]]);
                }
            }
        }
        // Compare children recursively
        compareNodes(monitor, nodesToCompare);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBXTreeNode(org.jkiss.dbeaver.model.navigator.meta.DBXTreeNode) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) DBPSystemObject(org.jkiss.dbeaver.model.DBPSystemObject) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 2 with DBXTreeNode

use of org.jkiss.dbeaver.model.navigator.meta.DBXTreeNode in project dbeaver by serge-rider.

the class NodeListControl method createContentProvider.

private static IContentProvider createContentProvider(DBNNode node, DBXTreeNode metaNode) {
    if (node instanceof DBNDatabaseNode) {
        final DBNDatabaseNode dbNode = (DBNDatabaseNode) node;
        if (metaNode == null) {
            metaNode = dbNode.getMeta();
        }
        final List<DBXTreeNode> inlineMetas = collectInlineMetas(dbNode, metaNode);
        if (!inlineMetas.isEmpty()) {
            return new TreeContentProvider() {

                @Override
                public boolean hasChildren(Object parentElement) {
                    return parentElement instanceof DBNDatabaseNode && ((DBNDatabaseNode) parentElement).hasChildren(false);
                }

                @Override
                public Object[] getChildren(Object parentElement) {
                    if (parentElement instanceof DBNDatabaseNode) {
                        try {
                            // Read children with void progress monitor because inline children SHOULD be already cached
                            DBNNode[] children = NavigatorUtils.getNodeChildrenFiltered(VoidProgressMonitor.INSTANCE, (DBNDatabaseNode) parentElement, false);
                            if (ArrayUtils.isEmpty(children)) {
                                return null;
                            } else {
                                return children;
                            }
                        } catch (DBException e) {
                            log.error(e);
                        }
                    }
                    return null;
                }
            };
        }
    }
    return new ListContentProvider();
}
Also used : TreeContentProvider(org.jkiss.dbeaver.ui.controls.TreeContentProvider) DBXTreeNode(org.jkiss.dbeaver.model.navigator.meta.DBXTreeNode) DBException(org.jkiss.dbeaver.DBException) ListContentProvider(org.jkiss.dbeaver.ui.controls.ListContentProvider) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPObject(org.jkiss.dbeaver.model.DBPObject) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 3 with DBXTreeNode

use of org.jkiss.dbeaver.model.navigator.meta.DBXTreeNode in project dbeaver by serge-rider.

the class EditForeignKeyPage method createSchemaSelector.

private void createSchemaSelector(Composite tableGroup) throws DBException {
    // Here is a trick - we need to find schema/catalog container node and list its children
    DBNDatabaseNode schemaContainerNode = null;
    for (DBNNode node = ownerTableNode.getParentNode(); node != null; node = node.getParentNode()) {
        if (node instanceof DBNDatabaseNode) {
            DBSObject nodeObject = ((DBNDatabaseNode) node).getObject();
            if (nodeObject instanceof DBSSchema || nodeObject instanceof DBSCatalog) {
                if (node.getParentNode() instanceof DBNDatabaseNode) {
                    schemaContainerNode = (DBNDatabaseNode) node.getParentNode();
                    break;
                }
            }
        }
    }
    if (schemaContainerNode != null) {
        ILabelProvider labelProvider = new LabelProvider() {

            @Override
            public Image getImage(Object element) {
                return DBeaverIcons.getImage(((DBNDatabaseNode) element).getNodeIcon());
            }

            @Override
            public String getText(Object element) {
                return ((DBNDatabaseNode) element).getNodeName();
            }
        };
        boolean isSchema = (ownTable.getParentObject() instanceof DBSSchema);
        DBPDataSourceInfo dsInfo = ownTable.getDataSource().getInfo();
        UIUtils.createControlLabel(tableGroup, isSchema ? dsInfo.getSchemaTerm() : dsInfo.getCatalogTerm());
        final CSmartCombo<DBNDatabaseNode> schemaCombo = new CSmartCombo<>(tableGroup, SWT.BORDER, labelProvider);
        schemaCombo.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
        DBNDatabaseNode selectedNode = null;
        for (DBNNode node : schemaContainerNode.getChildren(VoidProgressMonitor.INSTANCE)) {
            if (node instanceof DBNDatabaseNode && ((DBNDatabaseNode) node).getObject() instanceof DBSObjectContainer) {
                schemaCombo.addItem((DBNDatabaseNode) node);
                if (((DBNDatabaseNode) node).getObject() == ownTable.getParentObject()) {
                    selectedNode = (DBNDatabaseNode) node;
                }
            }
        }
        if (selectedNode != null) {
            schemaCombo.select(selectedNode);
        }
        schemaCombo.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                // Here is another trick
                // We need to find table container node
                // This node is a child of schema node and has the same meta as our original table parent node
                DBNDatabaseNode newContainerNode = null;
                DBXTreeNode tableContainerMeta = ((DBNDatabaseNode) ownerTableNode.getParentNode()).getMeta();
                DBNDatabaseNode schemaNode = schemaCombo.getSelectedItem();
                if (schemaNode.getMeta() == tableContainerMeta) {
                    newContainerNode = schemaNode;
                } else {
                    try {
                        for (DBNNode child : schemaNode.getChildren(VoidProgressMonitor.INSTANCE)) {
                            if (child instanceof DBNDatabaseNode && ((DBNDatabaseNode) child).getMeta() == tableContainerMeta) {
                                newContainerNode = (DBNDatabaseNode) child;
                                break;
                            }
                        }
                    } catch (DBException e1) {
                        log.debug(e1);
                    // Shouldn't be here
                    }
                }
                if (newContainerNode != null) {
                    tableList.setRootNode(newContainerNode);
                    tableList.loadData();
                }
            }
        });
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBXTreeNode(org.jkiss.dbeaver.model.navigator.meta.DBXTreeNode) CSmartCombo(org.jkiss.dbeaver.ui.controls.CSmartCombo) GridData(org.eclipse.swt.layout.GridData) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 4 with DBXTreeNode

use of org.jkiss.dbeaver.model.navigator.meta.DBXTreeNode in project dbeaver by serge-rider.

the class CompareObjectsHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final IWorkbenchWindow workbenchWindow = HandlerUtil.getActiveWorkbenchWindow(event);
    final ISelection selection = HandlerUtil.getCurrentSelection(event);
    if (!(selection instanceof IStructuredSelection)) {
        return null;
    }
    IStructuredSelection ss = (IStructuredSelection) selection;
    if (ss.size() < 2) {
        log.error("At least 2 objects must be selected to perform compare");
        return null;
    }
    List<DBNDatabaseNode> nodes = new ArrayList<>();
    Class<?> firstType = null;
    DBXTreeNode firstMeta = null;
    for (Iterator<?> iter = ss.iterator(); iter.hasNext(); ) {
        DBNDatabaseNode node = (DBNDatabaseNode) iter.next();
        DBXTreeNode meta = null;
        if (node instanceof DBNDatabaseFolder) {
            meta = node.getMeta();
            if (firstMeta == null) {
                firstMeta = meta;
            }
        }
        if (node.getObject() == null) {
            log.error("Bad node with null object");
            return null;
        }
        Class<?> itemType = node.getObject().getClass();
        if (firstType == null) {
            firstType = itemType;
        } else {
            if (firstType != itemType || firstMeta != meta) {
                UIUtils.showErrorDialog(null, "Different object types", "Objects of different types were selected. You may compare only objects of the same type");
                return null;
            }
        }
        nodes.add(node);
    }
    CompareWizardDialog dialog = new CompareWizardDialog(workbenchWindow, new CompareObjectsWizard(nodes));
    dialog.open();
    return null;
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) DBXTreeNode(org.jkiss.dbeaver.model.navigator.meta.DBXTreeNode) ISelection(org.eclipse.jface.viewers.ISelection) ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) DBNDatabaseFolder(org.jkiss.dbeaver.model.navigator.DBNDatabaseFolder)

Example 5 with DBXTreeNode

use of org.jkiss.dbeaver.model.navigator.meta.DBXTreeNode in project dbeaver by serge-rider.

the class DBNDatabaseNode method getNodeIcon.

@Override
public DBPImage getNodeIcon() {
    final DBSObject object = getObject();
    DBPImage image = DBValueFormatting.getObjectImage(object, false);
    if (image == null) {
        DBXTreeNode meta = getMeta();
        if (meta != null) {
            image = meta.getIcon(this);
        }
    }
    if (image != null && object instanceof DBPStatefulObject) {
        image = DBNModel.getStateOverlayImage(image, ((DBPStatefulObject) object).getObjectState());
    }
    return image;
}
Also used : DBXTreeNode(org.jkiss.dbeaver.model.navigator.meta.DBXTreeNode)

Aggregations

DBXTreeNode (org.jkiss.dbeaver.model.navigator.meta.DBXTreeNode)6 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)4 DBException (org.jkiss.dbeaver.DBException)3 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)2 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)2 DBSObjectContainer (org.jkiss.dbeaver.model.struct.DBSObjectContainer)2 ArrayList (java.util.ArrayList)1 ISelection (org.eclipse.jface.viewers.ISelection)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 GridData (org.eclipse.swt.layout.GridData)1 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)1 DBPObject (org.jkiss.dbeaver.model.DBPObject)1 DBPSystemObject (org.jkiss.dbeaver.model.DBPSystemObject)1 DBNDatabaseFolder (org.jkiss.dbeaver.model.navigator.DBNDatabaseFolder)1 DBXTreeFolder (org.jkiss.dbeaver.model.navigator.meta.DBXTreeFolder)1 DBXTreeItem (org.jkiss.dbeaver.model.navigator.meta.DBXTreeItem)1 DBXTreeObject (org.jkiss.dbeaver.model.navigator.meta.DBXTreeObject)1 CSmartCombo (org.jkiss.dbeaver.ui.controls.CSmartCombo)1 ListContentProvider (org.jkiss.dbeaver.ui.controls.ListContentProvider)1 TreeContentProvider (org.jkiss.dbeaver.ui.controls.TreeContentProvider)1