Search in sources :

Example 1 with DBPDataSourceContainer

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

the class NavigatorHandlerLinkEditor method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final IWorkbenchPage activePage = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
    final IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
    if (activeEditor == null) {
        return null;
    }
    NavigatorViewBase navigatorView = NavigatorUtils.getActiveNavigatorView(event);
    if (navigatorView == null) {
        return null;
    }
    if (navigatorView instanceof ProjectExplorerView) {
        if (activeEditor instanceof SQLEditor) {
            IFile file = EditorUtils.getFileFromInput(activeEditor.getEditorInput());
            if (file != null) {
                showResourceInNavigator(navigatorView, file);
            }
        } else if (activeEditor.getEditorInput() instanceof ProjectFileEditorInput) {
            IFile editorFile = ((ProjectFileEditorInput) activeEditor.getEditorInput()).getFile();
            showResourceInNavigator(navigatorView, editorFile);
        }
    } else if (activeEditor.getEditorInput() instanceof IDatabaseEditorInput) {
        IDatabaseEditorInput editorInput = (IDatabaseEditorInput) activeEditor.getEditorInput();
        DBNNode dbnNode = editorInput.getNavigatorNode();
        if (dbnNode != null) {
            navigatorView.showNode(dbnNode);
        }
    } else if (activeEditor instanceof IDataSourceContainerProvider) {
        DBPDataSourceContainer dsContainer = ((IDataSourceContainerProvider) activeEditor).getDataSourceContainer();
        @NotNull final DBSObject activeObject;
        if (dsContainer != null) {
            DBPDataSource dataSource = dsContainer.getDataSource();
            if (dataSource != null) {
                activeObject = DBUtils.getDefaultOrActiveObject(dataSource);
            } else {
                activeObject = dsContainer;
            }
            final NavigatorViewBase view = navigatorView;
            DBeaverUI.runInUI(activePage.getWorkbenchWindow(), new DBRRunnableWithProgress() {

                @Override
                public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    DBSObject showObject = activeObject;
                    if (showObject instanceof DBPDataSource) {
                        showObject = ((DBPDataSource) showObject).getContainer();
                    }
                    DBNDatabaseNode objectNode = view.getModel().getNodeByObject(monitor, showObject, true);
                    if (objectNode != null) {
                        view.showNode(objectNode);
                    }
                }
            });
        }
    }
    activePage.activate(navigatorView);
    return null;
}
Also used : SQLEditor(org.jkiss.dbeaver.ui.editors.sql.SQLEditor) IDataSourceContainerProvider(org.jkiss.dbeaver.model.IDataSourceContainerProvider) ProjectExplorerView(org.jkiss.dbeaver.ui.navigator.project.ProjectExplorerView) IFile(org.eclipse.core.resources.IFile) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) ProjectFileEditorInput(org.jkiss.dbeaver.ui.editors.ProjectFileEditorInput) IEditorPart(org.eclipse.ui.IEditorPart) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) NotNull(org.jkiss.code.NotNull) InvocationTargetException(java.lang.reflect.InvocationTargetException) IDatabaseEditorInput(org.jkiss.dbeaver.ui.editors.IDatabaseEditorInput) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) NavigatorViewBase(org.jkiss.dbeaver.ui.navigator.database.NavigatorViewBase) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 2 with DBPDataSourceContainer

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

the class NavigatorHandlerAssociateScript method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final Shell activeShell = HandlerUtil.getActiveShell(event);
    List<IFile> scripts = new ArrayList<>();
    final ISelection selection = HandlerUtil.getCurrentSelection(event);
    if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
        for (Iterator iter = ((IStructuredSelection) selection).iterator(); iter.hasNext(); ) {
            final DBNNode node = RuntimeUtils.getObjectAdapter(iter.next(), DBNNode.class);
            if (node instanceof DBNResource) {
                IResource resource = ((DBNResource) node).getResource();
                if (resource instanceof IFile) {
                    scripts.add((IFile) resource);
                }
            }
        }
    }
    if (!scripts.isEmpty()) {
        DBPDataSourceContainer dataSourceDescriptor = SelectDataSourceDialog.selectDataSource(activeShell, scripts.get(0).getProject());
        if (dataSourceDescriptor != null) {
            for (IFile script : scripts) {
                EditorUtils.setFileDataSource(script, dataSourceDescriptor, true);
            }
        }
    }
    return null;
}
Also used : DBNResource(org.jkiss.dbeaver.model.navigator.DBNResource) Shell(org.eclipse.swt.widgets.Shell) IFile(org.eclipse.core.resources.IFile) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) ArrayList(java.util.ArrayList) ISelection(org.eclipse.jface.viewers.ISelection) Iterator(java.util.Iterator) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) IResource(org.eclipse.core.resources.IResource)

Example 3 with DBPDataSourceContainer

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

the class AbstractDataSourceHandler method getDataSourceContainer.

protected static DBPDataSourceContainer getDataSourceContainer(ExecutionEvent event, boolean useEditor) {
    if (useEditor) {
        IEditorPart editor = HandlerUtil.getActiveEditor(event);
        if (editor != null) {
            DBPDataSourceContainer container = getDataSourceContainer(editor);
            if (container != null) {
                return container;
            }
        }
        return null;
    }
    IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
    DBPDataSourceContainer container = getDataSourceContainer(activePart);
    if (container != null) {
        return container;
    }
    ISelection selection = HandlerUtil.getCurrentSelection(event);
    if (selection instanceof IStructuredSelection) {
        DBSObject selectedObject = NavigatorUtils.getSelectedObject((IStructuredSelection) selection);
        if (selectedObject instanceof DBPDataSourceContainer) {
            return (DBPDataSourceContainer) selectedObject;
        } else if (selectedObject != null) {
            DBPDataSource dataSource = selectedObject.getDataSource();
            return dataSource == null ? null : dataSource.getContainer();
        }
    }
    return null;
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) ISelection(org.eclipse.jface.viewers.ISelection) IEditorPart(org.eclipse.ui.IEditorPart) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Example 4 with DBPDataSourceContainer

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

the class GotoObjectDialog method fillContentProvider.

@Override
protected void fillContentProvider(AbstractContentProvider contentProvider, ItemsFilter itemsFilter, IProgressMonitor progressMonitor) throws CoreException {
    DBSStructureAssistant structureAssistant = DBUtils.getAdapter(DBSStructureAssistant.class, context.getDataSource());
    if (structureAssistant == null) {
        return;
    }
    String nameMask = ((ObjectFilter) itemsFilter).getNameMask();
    DBRProgressMonitor monitor = RuntimeUtils.makeMonitor(progressMonitor);
    try {
        monitor.beginTask("Search for '" + nameMask + "'", 100);
        List<DBSObjectType> typesToSearch = new ArrayList<>();
        for (DBSObjectType type : structureAssistant.getSupportedObjectTypes()) {
            Class<? extends DBSObject> typeClass = type.getTypeClass();
            if (DBSEntityElement.class.isAssignableFrom(typeClass)) {
                // Skipp attributes (columns), methods, etc
                continue;
            }
            typesToSearch.add(type);
        }
        Collection<DBSObjectReference> result = structureAssistant.findObjectsByMask(monitor, container, typesToSearch.toArray(new DBSObjectType[typesToSearch.size()]), nameMask, false, true, 1000);
        DBPDataSourceContainer dsContainer = context.getDataSource().getContainer();
        for (DBSObjectReference ref : result) {
            DBSObjectFilter filter = dsContainer.getObjectFilter(ref.getObjectClass(), ref.getContainer(), true);
            if (filter == null || !filter.isEnabled() || filter.matches(ref.getName())) {
                contentProvider.add(ref, itemsFilter);
            }
        }
    } catch (DBException e) {
        throw new CoreException(GeneralUtils.makeExceptionStatus(e));
    } finally {
        monitor.done();
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) ArrayList(java.util.ArrayList) StyledString(org.eclipse.jface.viewers.StyledString) CoreException(org.eclipse.core.runtime.CoreException) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Example 5 with DBPDataSourceContainer

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

the class PrefPageSQLFormat method createPreferenceContent.

@Override
protected Control createPreferenceContent(Composite parent) {
    Composite composite = UIUtils.createPlaceholder(parent, 2, 5);
    // Autoclose
    {
        Composite acGroup = UIUtils.createControlGroup(composite, "Auto close", 1, GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING, 0);
        acSingleQuotesCheck = UIUtils.createCheckbox(acGroup, "Single quotes", false);
        acDoubleQuotesCheck = UIUtils.createCheckbox(acGroup, "Double quotes", false);
        acBracketsCheck = UIUtils.createCheckbox(acGroup, "Brackets", false);
    }
    {
        // Formatting
        Composite afGroup = UIUtils.createControlGroup(composite, "Auto format", 1, GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING, 0);
        afKeywordCase = UIUtils.createCheckbox(afGroup, "Convert keyword case", "Auto-convert keywords to upper/lower case on enter", false, 1);
        afExtractFromSource = UIUtils.createCheckbox(afGroup, "Extract SQL from source code", "On source code paste will remove all source language elements like quotes, +, \\n, etc", false, 1);
    }
    Composite formatterGroup = UIUtils.createControlGroup(composite, "Formatter", 1, GridData.FILL_BOTH, 0);
    ((GridData) formatterGroup.getLayoutData()).horizontalSpan = 2;
    {
        Composite formatterPanel = UIUtils.createPlaceholder(formatterGroup, 2);
        formatterPanel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        formatterSelector = UIUtils.createLabelCombo(formatterPanel, "Formatter", SWT.DROP_DOWN | SWT.READ_ONLY);
        formatterSelector.add(capitalizeCaseName(SQLTokenizedFormatter.FORMATTER_ID));
        formatterSelector.add(capitalizeCaseName(SQLExternalFormatter.FORMATTER_ID));
        formatterSelector.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                showFormatterSettings();
            }
        });
        formatterSelector.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
    }
    // Default formatter settings
    {
        defaultGroup = UIUtils.createPlaceholder(formatterGroup, 2, 0);
        defaultGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
        keywordCaseCombo = UIUtils.createLabelCombo(defaultGroup, "Keyword case", SWT.DROP_DOWN | SWT.READ_ONLY);
        keywordCaseCombo.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
        keywordCaseCombo.add("Database");
        for (DBPIdentifierCase c : DBPIdentifierCase.values()) {
            keywordCaseCombo.add(capitalizeCaseName(c.name()));
        }
        keywordCaseCombo.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                performApply();
            }
        });
    }
    // External formatter
    {
        externalGroup = UIUtils.createPlaceholder(formatterGroup, 2);
        externalGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
        externalCmdText = UIUtils.createLabelText(externalGroup, "Command line", "");
        externalCmdText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        externalUseFile = UIUtils.createLabelCheckbox(externalGroup, "Use temp file", "Use temporary file to pass SQL text.\nTo pass file name in command line use parameter ${file}", false);
        externalTimeout = UIUtils.createLabelSpinner(externalGroup, "Exec timeout", "Time to wait until formatter process finish (ms)", 100, 100, 10000);
    }
    {
        // SQL preview
        Composite previewGroup = new Composite(formatterGroup, SWT.BORDER);
        previewGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
        previewGroup.setLayout(new FillLayout());
        sqlViewer = new SQLEditorBase() {

            @Override
            public DBCExecutionContext getExecutionContext() {
                final DBPDataSourceContainer container = getDataSourceContainer();
                if (container != null) {
                    final DBPDataSource dataSource = container.getDataSource();
                    if (dataSource != null) {
                        return dataSource.getDefaultContext(false);
                    }
                }
                return null;
            }
        };
        try {
            try (final InputStream sqlStream = getClass().getResourceAsStream(FORMAT_FILE_NAME)) {
                final String sqlText = ContentUtils.readToString(sqlStream, GeneralUtils.DEFAULT_ENCODING);
                IEditorSite subSite = new SubEditorSite(DBeaverUI.getActiveWorkbenchWindow().getActivePage().getActivePart().getSite());
                StringEditorInput sqlInput = new StringEditorInput("SQL preview", sqlText, true, GeneralUtils.getDefaultFileEncoding());
                sqlViewer.init(subSite, sqlInput);
            }
        } catch (Exception e) {
            log.error(e);
        }
        sqlViewer.createPartControl(previewGroup);
        Object text = sqlViewer.getAdapter(Control.class);
        if (text instanceof StyledText) {
            ((StyledText) text).setWordWrap(true);
        }
        sqlViewer.reloadSyntaxRules();
        previewGroup.addDisposeListener(new DisposeListener() {

            @Override
            public void widgetDisposed(DisposeEvent e) {
                sqlViewer.dispose();
            }
        });
    }
    return composite;
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) StyledText(org.eclipse.swt.custom.StyledText) InputStream(java.io.InputStream) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) FillLayout(org.eclipse.swt.layout.FillLayout) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DisposeEvent(org.eclipse.swt.events.DisposeEvent) StringEditorInput(org.jkiss.dbeaver.ui.editors.StringEditorInput) SubEditorSite(org.jkiss.dbeaver.ui.editors.SubEditorSite) SQLEditorBase(org.jkiss.dbeaver.ui.editors.sql.SQLEditorBase) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) DBPIdentifierCase(org.jkiss.dbeaver.model.DBPIdentifierCase) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) IEditorSite(org.eclipse.ui.IEditorSite)

Aggregations

DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)230 DBException (org.jkiss.dbeaver.DBException)32 ArrayList (java.util.ArrayList)31 IFile (org.eclipse.core.resources.IFile)30 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)27 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)27 DBNDataSource (org.jkiss.dbeaver.model.navigator.DBNDataSource)24 InvocationTargetException (java.lang.reflect.InvocationTargetException)22 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)19 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)19 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)18 SelectionEvent (org.eclipse.swt.events.SelectionEvent)18 DBPProject (org.jkiss.dbeaver.model.app.DBPProject)18 DBPConnectionConfiguration (org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration)18 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)17 GridData (org.eclipse.swt.layout.GridData)16 IEditorPart (org.eclipse.ui.IEditorPart)16 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)16 DBNResource (org.jkiss.dbeaver.model.navigator.DBNResource)16 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)13