Search in sources :

Example 16 with DBSObjectContainer

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

the class SQLCompletionProposal method apply.

@Override
public void apply(IDocument document) {
    try {
        if (dataSource != null) {
            if (dataSource.getContainer().getPreferenceStore().getBoolean(SQLPreferenceConstants.INSERT_SPACE_AFTER_PROPOSALS)) {
                boolean insertTrailingSpace = true;
                if (object instanceof DBSObjectContainer) {
                // Do not append trailing space after schemas/catalogs/etc.
                } else {
                    int docLen = document.getLength();
                    if (docLen <= replacementOffset + replacementLength + 2) {
                        insertTrailingSpace = false;
                    } else {
                        insertTrailingSpace = document.getChar(replacementOffset + replacementLength) != ' ';
                    }
                    if (insertTrailingSpace) {
                        replacementString += " ";
                    }
                    cursorPosition++;
                }
            }
        }
        document.replace(replacementOffset, replacementLength, replacementString);
    } catch (BadLocationException e) {
        // ignore
        log.debug(e);
    }
}
Also used : DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) Point(org.eclipse.swt.graphics.Point) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 17 with DBSObjectContainer

use of org.jkiss.dbeaver.model.struct.DBSObjectContainer in project dbeaver by dbeaver.

the class NavigatorHandlerObjectOpen method openEntityEditor.

public static IEditorPart openEntityEditor(@NotNull DBNNode selectedNode, @Nullable String defaultPageId, @Nullable Map<String, Object> attributes, IWorkbenchWindow workbenchWindow) {
    if (selectedNode instanceof DBNDataSource) {
        final DataSourceDescriptor dataSourceContainer = (DataSourceDescriptor) ((DBNDataSource) selectedNode).getDataSourceContainer();
        openConnectionEditor(workbenchWindow, dataSourceContainer);
        return null;
    }
    try {
        String defaultFolderId = null;
        if (selectedNode instanceof DBNDatabaseFolder && !(selectedNode.getParentNode() instanceof DBNDatabaseFolder) && selectedNode.getParentNode() instanceof DBNDatabaseNode) {
            defaultFolderId = selectedNode.getNodeType();
            selectedNode = selectedNode.getParentNode();
        }
        IEditorPart editor = findEntityEditor(workbenchWindow, selectedNode);
        if (editor != null) {
            if (editor instanceof ITabbedFolderContainer && defaultFolderId != null) {
                // Activate default folder
                ((ITabbedFolderContainer) editor).switchFolder(defaultFolderId);
            }
            workbenchWindow.getActivePage().activate(editor);
            return editor;
        }
        if (selectedNode instanceof DBNDatabaseObject) {
            DBNDatabaseObject objectNode = (DBNDatabaseObject) selectedNode;
            ObjectEditorInput objectInput = new ObjectEditorInput(objectNode);
            setInputAttributes(objectInput, defaultPageId, defaultFolderId, attributes);
            return workbenchWindow.getActivePage().openEditor(objectInput, objectNode.getMeta().getEditorId());
        } else if (selectedNode instanceof DBNDatabaseNode) {
            DBNDatabaseNode dnNode = (DBNDatabaseNode) selectedNode;
            if (dnNode.getObject() != null) {
                EntityEditorInput editorInput = new EntityEditorInput(dnNode);
                if (DBeaverCore.getGlobalPreferenceStore().getBoolean(DBeaverPreferences.NAVIGATOR_REFRESH_EDITORS_ON_OPEN)) {
                    if (dnNode.getObject() instanceof DBSObjectContainer) {
                    // do not auto-refresh object containers (too expensive)
                    } else {
                        refreshDatabaseNode(dnNode);
                    }
                }
                setInputAttributes(editorInput, defaultPageId, defaultFolderId, attributes);
                return workbenchWindow.getActivePage().openEditor(editorInput, EntityEditor.class.getName());
            } else {
                DBUserInterface.getInstance().showError("No object", "Node do not has associated database object");
                return null;
            }
        } else {
            NodeEditorInput folderInput = new NodeEditorInput(selectedNode);
            return workbenchWindow.getActivePage().openEditor(folderInput, FolderEditor.class.getName());
        }
    } catch (Exception ex) {
        DBUserInterface.getInstance().showError(CoreMessages.actions_navigator_error_dialog_open_entity_title, "Can't open entity '" + selectedNode.getNodeName() + "'", ex);
        return null;
    }
}
Also used : EntityEditorInput(org.jkiss.dbeaver.ui.editors.entity.EntityEditorInput) IEditorPart(org.eclipse.ui.IEditorPart) ExecutionException(org.eclipse.core.commands.ExecutionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) ITabbedFolderContainer(org.jkiss.dbeaver.ui.controls.folders.ITabbedFolderContainer) ObjectEditorInput(org.jkiss.dbeaver.ui.editors.object.ObjectEditorInput) NodeEditorInput(org.jkiss.dbeaver.ui.editors.entity.NodeEditorInput) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) DataSourceDescriptor(org.jkiss.dbeaver.registry.DataSourceDescriptor)

Example 18 with DBSObjectContainer

use of org.jkiss.dbeaver.model.struct.DBSObjectContainer in project dbeaver by dbeaver.

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 19 with DBSObjectContainer

use of org.jkiss.dbeaver.model.struct.DBSObjectContainer in project dbeaver by dbeaver.

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(new VoidProgressMonitor())) {
            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(new VoidProgressMonitor())) {
                            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) DBPDataSourceInfo(org.jkiss.dbeaver.model.DBPDataSourceInfo) 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) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 20 with DBSObjectContainer

use of org.jkiss.dbeaver.model.struct.DBSObjectContainer in project dbeaver by dbeaver.

the class SQLCompletionProcessor method filterProposals.

private void filterProposals(SQLCompletionAnalyzer.CompletionRequest request, DBPDataSource dataSource) {
    // Remove duplications
    final Set<String> proposalMap = new HashSet<>(request.proposals.size());
    for (int i = 0; i < request.proposals.size(); ) {
        SQLCompletionProposal proposal = request.proposals.get(i);
        if (proposalMap.contains(proposal.getDisplayString())) {
            request.proposals.remove(i);
            continue;
        }
        proposalMap.add(proposal.getDisplayString());
        i++;
    }
    DBSObject selectedObject = dataSource == null ? null : DBUtils.getActiveInstanceObject(dataSource);
    boolean hideDups = editor.getActivePreferenceStore().getBoolean(SQLPreferenceConstants.HIDE_DUPLICATE_PROPOSALS) && selectedObject != null;
    if (hideDups) {
        for (int i = 0; i < request.proposals.size(); i++) {
            SQLCompletionProposal proposal = request.proposals.get(i);
            for (int j = 0; j < request.proposals.size(); ) {
                SQLCompletionProposal proposal2 = request.proposals.get(j);
                if (i != j && proposal.hasStructObject() && proposal2.hasStructObject() && CommonUtils.equalObjects(proposal.getObject().getName(), proposal2.getObject().getName()) && proposal.getObjectContainer() == selectedObject) {
                    request.proposals.remove(j);
                } else {
                    j++;
                }
            }
        }
    }
    if (hideDups) {
        if (selectedObject instanceof DBSObjectContainer) {
        }
    }
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer)

Aggregations

DBSObjectContainer (org.jkiss.dbeaver.model.struct.DBSObjectContainer)21 DBException (org.jkiss.dbeaver.DBException)9 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)9 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)5 DBXTreeNode (org.jkiss.dbeaver.model.navigator.meta.DBXTreeNode)5 DBSObjectSelector (org.jkiss.dbeaver.model.struct.DBSObjectSelector)4 DataSourceDescriptor (org.jkiss.dbeaver.registry.DataSourceDescriptor)4 GridData (org.eclipse.swt.layout.GridData)3 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)3 DBSSchema (org.jkiss.dbeaver.model.struct.rdb.DBSSchema)3 CSmartCombo (org.jkiss.dbeaver.ui.controls.CSmartCombo)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ExecutionException (org.eclipse.core.commands.ExecutionException)2 IStatus (org.eclipse.core.runtime.IStatus)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)2 Point (org.eclipse.swt.graphics.Point)2 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)2 DBPDataSourceInfo (org.jkiss.dbeaver.model.DBPDataSourceInfo)2 DBPSystemObject (org.jkiss.dbeaver.model.DBPSystemObject)2