Search in sources :

Example 6 with DBPDataSource

use of org.jkiss.dbeaver.model.DBPDataSource 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 7 with DBPDataSource

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

the class NavigatorHandlerObjectBase method getCommandTarget.

protected static CommandTarget getCommandTarget(IWorkbenchWindow workbenchWindow, DBNContainer container, Class<?> childType, boolean openEditor) throws DBException {
    final Object parentObject = container.getValueObject();
    DBSObject objectToSeek = null;
    if (parentObject instanceof DBSObject) {
        final DBEStructEditor parentStructEditor = EntityEditorsRegistry.getInstance().getObjectManager(parentObject.getClass(), DBEStructEditor.class);
        if (parentStructEditor != null && RuntimeUtils.isTypeSupported(childType, parentStructEditor.getChildTypes())) {
            objectToSeek = (DBSObject) parentObject;
        }
    }
    if (objectToSeek != null) {
        for (final IEditorReference editorRef : workbenchWindow.getActivePage().getEditorReferences()) {
            final IEditorPart editor = editorRef.getEditor(false);
            if (editor instanceof IDatabaseEditor) {
                final IDatabaseEditorInput editorInput = ((IDatabaseEditor) editor).getEditorInput();
                if (editorInput.getDatabaseObject() == objectToSeek) {
                    workbenchWindow.getActivePage().activate(editor);
                    switchEditorFolder(container, editor);
                    return new CommandTarget((IDatabaseEditor) editor);
                }
            }
        }
        if (openEditor && container instanceof DBNDatabaseNode) {
            final IDatabaseEditor editor = (IDatabaseEditor) NavigatorHandlerObjectOpen.openEntityEditor((DBNDatabaseNode) container, null, workbenchWindow);
            if (editor != null) {
                switchEditorFolder(container, editor);
                return new CommandTarget(editor);
            }
        }
    }
    if (container instanceof DBNDatabaseNode) {
        // No editor found - create new command context
        DBPDataSource dataSource = ((DBNDatabaseNode) container).getObject().getDataSource();
        if (dataSource != null) {
            return new CommandTarget(new SimpleCommandContext(dataSource.getDefaultContext(true), !openEditor));
        }
    }
    return new CommandTarget();
}
Also used : IDatabaseEditorInput(org.jkiss.dbeaver.ui.editors.IDatabaseEditorInput) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) IEditorReference(org.eclipse.ui.IEditorReference) SimpleCommandContext(org.jkiss.dbeaver.ui.SimpleCommandContext) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBEStructEditor(org.jkiss.dbeaver.model.edit.DBEStructEditor) IEditorPart(org.eclipse.ui.IEditorPart) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) IDatabaseEditor(org.jkiss.dbeaver.ui.editors.IDatabaseEditor)

Example 8 with DBPDataSource

use of org.jkiss.dbeaver.model.DBPDataSource 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 9 with DBPDataSource

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

the class ResultSetUtils method getEntityFromMetaData.

private static DBSEntity getEntityFromMetaData(DBRProgressMonitor monitor, DBSObjectContainer objectContainer, DBCEntityMetaData entityMeta, boolean transformName) throws DBException {
    final DBPDataSource dataSource = objectContainer.getDataSource();
    String catalogName = entityMeta.getCatalogName();
    String schemaName = entityMeta.getSchemaName();
    String entityName = entityMeta.getEntityName();
    if (transformName) {
        catalogName = DBObjectNameCaseTransformer.transformName(dataSource, catalogName);
        schemaName = DBObjectNameCaseTransformer.transformName(dataSource, schemaName);
        entityName = DBObjectNameCaseTransformer.transformName(dataSource, entityName);
    }
    DBSObject entityObject = DBUtils.getObjectByPath(monitor, objectContainer, catalogName, schemaName, entityName);
    if (entityObject == null) {
        return null;
    } else if (entityObject instanceof DBSEntity) {
        return (DBSEntity) entityObject;
    } else {
        log.debug("Unsupported table class: " + entityObject.getClass().getName());
        return null;
    }
}
Also used : DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource)

Example 10 with DBPDataSource

use of org.jkiss.dbeaver.model.DBPDataSource 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

DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)118 DBException (org.jkiss.dbeaver.DBException)43 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)22 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)14 JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)12 SQLException (java.sql.SQLException)10 GridData (org.eclipse.swt.layout.GridData)10 IEditorPart (org.eclipse.ui.IEditorPart)10 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)10 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)10 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)10 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)9 ArrayList (java.util.ArrayList)8 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)8 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)7 SQLDataSource (org.jkiss.dbeaver.model.sql.SQLDataSource)7 ISelection (org.eclipse.jface.viewers.ISelection)6 Composite (org.eclipse.swt.widgets.Composite)5 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)5