Search in sources :

Example 51 with DBCExecutionContext

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

the class SQLEditorBase method scrollCursorToError.

/**
 * Error handling
 */
boolean scrollCursorToError(@NotNull DBRProgressMonitor monitor, @NotNull SQLQuery query, @NotNull Throwable error) {
    try {
        DBCExecutionContext context = getExecutionContext();
        if (context == null) {
            return false;
        }
        boolean scrolled = false;
        DBPErrorAssistant errorAssistant = DBUtils.getAdapter(DBPErrorAssistant.class, context.getDataSource());
        if (errorAssistant != null) {
            DBPErrorAssistant.ErrorPosition[] positions = errorAssistant.getErrorPosition(monitor, context, query.getText(), error);
            if (positions != null && positions.length > 0) {
                int queryStartOffset = query.getOffset();
                int queryLength = query.getLength();
                DBPErrorAssistant.ErrorPosition pos = positions[0];
                if (pos.line < 0) {
                    if (pos.position >= 0) {
                        // Only position
                        getSelectionProvider().setSelection(new TextSelection(queryStartOffset + pos.position, 0));
                        scrolled = true;
                    }
                } else {
                    // Line + position
                    IDocument document = getDocument();
                    if (document != null) {
                        int startLine = document.getLineOfOffset(queryStartOffset);
                        int errorOffset = document.getLineOffset(startLine + pos.line);
                        int errorLength;
                        if (pos.position >= 0) {
                            errorOffset += pos.position;
                            errorLength = 1;
                        } else {
                            errorLength = document.getLineLength(startLine + pos.line);
                        }
                        if (errorOffset < queryStartOffset)
                            errorOffset = queryStartOffset;
                        if (errorLength > queryLength)
                            errorLength = queryLength;
                        if (errorOffset >= queryStartOffset + queryLength) {
                            // This may happen if error position was incorrectly detected.
                            // E.g. in SQL Server when actual error happened in some stored procedure.
                            errorOffset = queryStartOffset + queryLength - 1;
                        }
                        getSelectionProvider().setSelection(new TextSelection(errorOffset, errorLength));
                        scrolled = true;
                    }
                }
            }
        }
        return scrolled;
    // if (!scrolled) {
    // // Can't position on error - let's just select entire problem query
    // showStatementInEditor(result.getStatement(), true);
    // }
    } catch (Exception e) {
        log.warn("Error positioning on query error", e);
        return false;
    }
}
Also used : DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) CoreException(org.eclipse.core.runtime.CoreException)

Example 52 with DBCExecutionContext

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

the class NavigatorHandlerObjectGoto method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    DBCExecutionContext context = null;
    DBSObject container = null;
    IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
    if (activePart instanceof DBPContextProvider) {
        context = ((DBPContextProvider) activePart).getExecutionContext();
    } else if (activePart instanceof INavigatorModelView) {
        final ISelection selection = HandlerUtil.getCurrentSelection(event);
        if (selection instanceof IStructuredSelection) {
            Object element = ((IStructuredSelection) selection).getFirstElement();
            if (element instanceof DBSWrapper) {
                DBSObject object = ((DBSWrapper) element).getObject();
                if (object != null) {
                    container = object;
                    while (container instanceof DBSFolder) {
                        container = container.getParentObject();
                    }
                    DBPDataSource dataSource = object.getDataSource();
                    if (dataSource != null) {
                        context = dataSource.getDefaultContext(true);
                    }
                }
            }
        }
    }
    if (context == null) {
        DBUserInterface.getInstance().showError("Go to object", "No active datasource");
        return null;
    }
    IWorkbenchWindow workbenchWindow = HandlerUtil.getActiveWorkbenchWindow(event);
    GotoObjectDialog dialog = new GotoObjectDialog(HandlerUtil.getActiveShell(event), context, container);
    dialog.open();
    Object[] objectsToOpen = dialog.getResult();
    if (!ArrayUtils.isEmpty(objectsToOpen)) {
        Collection<DBNDatabaseNode> nodes = NavigatorHandlerObjectBase.getNodesByObjects(Arrays.asList(objectsToOpen));
        for (DBNDatabaseNode node : nodes) {
            NavigatorUtils.openNavigatorNode(node, workbenchWindow);
        }
    }
    return null;
}
Also used : DBSFolder(org.jkiss.dbeaver.model.struct.DBSFolder) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBSWrapper(org.jkiss.dbeaver.model.struct.DBSWrapper) DBPContextProvider(org.jkiss.dbeaver.model.DBPContextProvider) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) ISelection(org.eclipse.jface.viewers.ISelection) GotoObjectDialog(org.jkiss.dbeaver.ui.dialogs.GotoObjectDialog) INavigatorModelView(org.jkiss.dbeaver.ui.navigator.INavigatorModelView) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 53 with DBCExecutionContext

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

the class BaseSQLDialog method createSQLPanel.

protected Composite createSQLPanel(Composite parent) {
    Composite panel = UIUtils.createPlaceholder(parent, 1);
    panel.setLayoutData(new GridData(GridData.FILL_BOTH));
    if (isLabelVisible()) {
        UIUtils.createControlLabel(panel, "SQL Preview");
    }
    //        new Label(panel, SWT.SEPARATOR | SWT.HORIZONTAL).setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    Composite editorPH = new Composite(panel, SWT.BORDER);
    GridData gd = new GridData(GridData.FILL_BOTH);
    gd.verticalIndent = 3;
    gd.horizontalSpan = 1;
    gd.minimumHeight = 100;
    gd.minimumWidth = 100;
    editorPH.setLayoutData(gd);
    editorPH.setLayout(new FillLayout());
    sqlViewer = new SQLEditorBase() {

        @NotNull
        @Override
        protected SQLDialect getSQLDialect() {
            return BaseSQLDialog.this.getSQLDialect();
        }

        @Override
        public DBCExecutionContext getExecutionContext() {
            return BaseSQLDialog.this.getExecutionContext();
        }
    };
    updateSQL();
    sqlViewer.createPartControl(editorPH);
    if (isWordWrap()) {
        Object text = sqlViewer.getAdapter(Control.class);
        if (text instanceof StyledText) {
            ((StyledText) text).setWordWrap(true);
        }
    }
    sqlViewer.reloadSyntaxRules();
    parent.addDisposeListener(new DisposeListener() {

        @Override
        public void widgetDisposed(DisposeEvent e) {
            sqlViewer.dispose();
        }
    });
    return panel;
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) StyledText(org.eclipse.swt.custom.StyledText) Composite(org.eclipse.swt.widgets.Composite) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) SQLEditorBase(org.jkiss.dbeaver.ui.editors.sql.SQLEditorBase) SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect) GridData(org.eclipse.swt.layout.GridData) FillLayout(org.eclipse.swt.layout.FillLayout) DisposeEvent(org.eclipse.swt.events.DisposeEvent) NotNull(org.jkiss.code.NotNull)

Example 54 with DBCExecutionContext

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

the class InvalidateJob method invalidateDataSource.

public static List<ContextInvalidateResult> invalidateDataSource(DBRProgressMonitor monitor, DBPDataSource dataSource) {
    long timeSpent = 0;
    List<ContextInvalidateResult> invalidateResults = new ArrayList<>();
    DBPDataSourceContainer container = dataSource.getContainer();
    DBWNetworkHandler[] activeHandlers = container.getActiveNetworkHandlers();
    boolean networkOK = true;
    boolean hasGoodContexts = false;
    if (activeHandlers != null && activeHandlers.length > 0) {
        for (DBWNetworkHandler nh : activeHandlers) {
            monitor.subTask("Invalidate network [" + container.getName() + "]");
            try {
                nh.invalidateHandler(monitor);
            } catch (Exception e) {
                invalidateResults.add(new ContextInvalidateResult(DBCExecutionContext.InvalidateResult.ERROR, e));
                networkOK = false;
                break;
            }
        }
    }
    if (networkOK) {
        // Invalidate datasource
        monitor.subTask("Invalidate connections of [" + container.getName() + "]");
        DBCExecutionContext[] allContexts = dataSource.getAllContexts();
        for (DBCExecutionContext context : allContexts) {
            long startTime = System.currentTimeMillis();
            try {
                final DBCExecutionContext.InvalidateResult result = context.invalidateContext(monitor);
                if (result != DBCExecutionContext.InvalidateResult.ERROR) {
                    hasGoodContexts = true;
                }
                invalidateResults.add(new ContextInvalidateResult(result, null));
            } catch (Exception e) {
                invalidateResults.add(new ContextInvalidateResult(DBCExecutionContext.InvalidateResult.ERROR, e));
            } finally {
                timeSpent += (System.currentTimeMillis() - startTime);
            }
        }
    }
    if (!hasGoodContexts) {
        // Close whole datasource. Target host seems to be unavailable
        try {
            container.disconnect(monitor);
        } catch (Exception e) {
            log.error("Error closing unaccessible datasource", e);
        }
        StringBuilder msg = new StringBuilder();
        for (ContextInvalidateResult result : invalidateResults) {
            if (result.error != null) {
                if (msg.length() > 0)
                    msg.append("\n");
                msg.append(result.error.getMessage());
            }
        }
        UIUtils.showErrorDialog(null, "Forced disconnect", "Datasource '" + container.getName() + "' was disconnected: destination database unreachable.\n" + msg);
    }
    return invalidateResults;
}
Also used : DBWNetworkHandler(org.jkiss.dbeaver.model.net.DBWNetworkHandler) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) ArrayList(java.util.ArrayList) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Example 55 with DBCExecutionContext

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

the class GlobalProxySelector method select.

@Override
public List<Proxy> select(URI uri) {
    String scheme = uri.getScheme();
    if (CommonUtils.isEmpty(scheme)) {
        return parent.select(uri);
    }
    if (scheme.startsWith("http")) {
    // 1. Check for drivers download proxy
    }
    if (SocksConstants.SOCKET_SCHEME.equals(scheme)) {
        // 2. Check for connections' proxy config
        DBCExecutionContext activeContext = DBExecUtils.findConnectionContext(uri.getHost(), uri.getPort(), uri.getPath());
        if (activeContext != null) {
            List<Proxy> proxies = null;
            DBPDataSourceContainer container = activeContext.getDataSource().getContainer();
            for (DBWHandlerConfiguration networkHandler : container.getConnectionConfiguration().getDeclaredHandlers()) {
                if (networkHandler.isEnabled() && networkHandler.getType() == DBWHandlerType.PROXY) {
                    Map<String, String> proxyProps = networkHandler.getProperties();
                    String proxyHost = proxyProps.get(SocksConstants.PROP_HOST);
                    String proxyPort = proxyProps.get(SocksConstants.PROP_PORT);
                    if (!CommonUtils.isEmpty(proxyHost)) {
                        int portNumber = SocksConstants.DEFAULT_SOCKS_PORT;
                        if (!CommonUtils.isEmpty(proxyPort)) {
                            try {
                                portNumber = Integer.parseInt(proxyPort);
                            } catch (NumberFormatException e) {
                                log.warn("Bad proxy port number", e);
                            }
                        }
                        InetSocketAddress proxyAddr = new InetSocketAddress(proxyHost, portNumber);
                        Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxyAddr);
                        if (proxies == null) {
                            proxies = new ArrayList<>();
                        }
                        proxies.add(proxy);
                        log.debug("Use SOCKS proxy [" + proxyAddr + "]");
                    }
                }
            }
            if (proxies != null) {
                return proxies;
            }
        }
    }
    return parent.select(uri);
}
Also used : DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBWHandlerConfiguration(org.jkiss.dbeaver.model.net.DBWHandlerConfiguration) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Aggregations

DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)107 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)27 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)22 DBException (org.jkiss.dbeaver.DBException)21 DBPContextProvider (org.jkiss.dbeaver.model.DBPContextProvider)20 InvocationTargetException (java.lang.reflect.InvocationTargetException)16 DBCExecutionContextDefaults (org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults)16 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)15 GridData (org.eclipse.swt.layout.GridData)12 IEditorPart (org.eclipse.ui.IEditorPart)12 DBCException (org.jkiss.dbeaver.model.exec.DBCException)12 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)12 ArrayList (java.util.ArrayList)10 Composite (org.eclipse.swt.widgets.Composite)10 DBSCatalog (org.jkiss.dbeaver.model.struct.rdb.DBSCatalog)10 DBSSchema (org.jkiss.dbeaver.model.struct.rdb.DBSSchema)10 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)8 SelectionEvent (org.eclipse.swt.events.SelectionEvent)8 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)8 NotNull (org.jkiss.code.NotNull)8