Search in sources :

Example 56 with DBCExecutionContext

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

the class PostgreDebugController method getSessionDescriptors.

@Override
public List<PostgreDebugSessionInfo> getSessionDescriptors() throws DBGException {
    DBCExecutionContext executionContext = getExecutionContext();
    try (Statement stmt = getConnection(executionContext).createStatement();
        ResultSet rs = stmt.executeQuery(SQL_SESSION)) {
        List<PostgreDebugSessionInfo> res = new ArrayList<PostgreDebugSessionInfo>();
        while (rs.next()) {
            int pid = rs.getInt("pid");
            String usename = rs.getString("usename");
            String state = rs.getString("state");
            String applicationName = rs.getString("application_name");
            String query = rs.getString("query");
            PostgreDebugSessionInfo info = new PostgreDebugSessionInfo(pid, usename, applicationName, state, query);
            res.add(info);
        }
        return res;
    } catch (SQLException e) {
        throw new DBGException("SQL error", e);
    }
}
Also used : DBGException(org.jkiss.dbeaver.debug.DBGException) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) JDBCExecutionContext(org.jkiss.dbeaver.model.impl.jdbc.JDBCExecutionContext) SQLException(java.sql.SQLException) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList)

Example 57 with DBCExecutionContext

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

the class InvalidateJob method invalidateDataSource.

public static List<ContextInvalidateResult> invalidateDataSource(DBRProgressMonitor monitor, DBPDataSource dataSource, boolean disconnectOnFailure) {
    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, disconnectOnFailure);
                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 && disconnectOnFailure) {
        // 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());
            }
        }
        DBUserInterface.getInstance().showError("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 58 with DBCExecutionContext

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

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)

Example 59 with DBCExecutionContext

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

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
        public 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) BasicSQLDialect(org.jkiss.dbeaver.model.impl.sql.BasicSQLDialect) 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 60 with DBCExecutionContext

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

the class DBGBaseController method attach.

@Override
public Object attach(DBRProgressMonitor monitor) throws DBGException {
    DBPDataSource dataSource = dataSourceContainer.getDataSource();
    if (!dataSourceContainer.isConnected()) {
        throw new DBGException("Not connected to database");
    }
    try {
        this.executionContext = dataSource.openIsolatedContext(monitor, "Debug controller");
        DBGSessionInfo targetInfo = getSessionDescriptor(getExecutionContext());
        DBCExecutionContext sessionContext = dataSource.openIsolatedContext(monitor, "Debug session");
        DBGBaseSession debugSession = createSession(targetInfo, sessionContext);
        Object id = targetInfo.getID();
        sessions.put(id, debugSession);
        attachSession(debugSession, sessionContext, configuration, monitor);
        return id;
    } catch (DBException e) {
        String message = NLS.bind(DebugMessages.DatabaseDebugController_e_opening_debug_context, dataSourceContainer);
        log.error(message, e);
        throw new DBGException(message, e);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource)

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