Search in sources :

Example 76 with DBPConnectionConfiguration

use of org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration in project dbeaver by dbeaver.

the class GenericDataSource method shutdown.

@Override
public void shutdown(DBRProgressMonitor monitor) {
    String queryShutdown = CommonUtils.toString(getContainer().getDriver().getDriverParameter(GenericConstants.PARAM_QUERY_SHUTDOWN));
    if (!CommonUtils.isEmpty(queryShutdown)) {
        for (JDBCRemoteInstance instance : getAvailableInstances()) {
            for (JDBCExecutionContext context : instance.getAllContexts()) {
                try (JDBCSession session = context.openSession(monitor, DBCExecutionPurpose.UTIL, "Shutdown database")) {
                    JDBCUtils.executeStatement(session, queryShutdown);
                } catch (Throwable e) {
                    log.error("Error shutting down database", e);
                }
            }
        }
    }
    super.shutdown(monitor);
    String paramShutdown = CommonUtils.toString(getContainer().getDriver().getDriverParameter(GenericConstants.PARAM_SHUTDOWN_URL_PARAM));
    if (!CommonUtils.isEmpty(paramShutdown)) {
        monitor.subTask("Shutdown embedded database");
        try {
            Properties shutdownProps = new Properties();
            DBPConnectionConfiguration connectionInfo = getContainer().getActualConnectionConfiguration();
            if (!CommonUtils.isEmpty(connectionInfo.getUserName())) {
                shutdownProps.put(DBConstants.DATA_SOURCE_PROPERTY_USER, connectionInfo.getUserName());
            }
            if (!CommonUtils.isEmpty(connectionInfo.getUserPassword())) {
                shutdownProps.put(DBConstants.DATA_SOURCE_PROPERTY_PASSWORD, connectionInfo.getUserPassword());
            }
            // Use void monitor - driver already loaded
            final Driver driver = getDriverInstance(new VoidProgressMonitor());
            if (driver != null) {
                driver.connect(getContainer().getActualConnectionConfiguration().getUrl() + paramShutdown, shutdownProps.isEmpty() ? null : shutdownProps);
            }
        } catch (Exception e) {
            log.debug("Shutdown finished: :" + e.getMessage());
        }
        monitor.worked(1);
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) DBPDriver(org.jkiss.dbeaver.model.connection.DBPDriver) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) Properties(java.util.Properties) DBException(org.jkiss.dbeaver.DBException)

Example 77 with DBPConnectionConfiguration

use of org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration in project dbeaver by dbeaver.

the class MySQLNativeToolHandler method getCommandLine.

@Override
protected List<String> getCommandLine(SETTINGS settings, PROCESS_ARG arg) throws IOException {
    List<String> cmd = new ArrayList<>();
    fillProcessParameters(settings, arg, cmd);
    String toolUserName = settings.getToolUserName();
    String toolUserPassword = settings.getToolUserPassword();
    /*
         * Use credentials derived from connection configuration
         * if no username was specified by export configuration itself.
         * This is needed to avoid overriding empty password.
         */
    if (CommonUtils.isEmpty(toolUserName)) {
        toolUserName = settings.getDataSourceContainer().getActualConnectionConfiguration().getUserName();
        toolUserPassword = settings.getDataSourceContainer().getActualConnectionConfiguration().getUserPassword();
    }
    if (isOverrideCredentials(settings)) {
        config = createCredentialsFile(toolUserName, toolUserPassword);
        cmd.add(1, "--defaults-file=" + config.getAbsolutePath());
    } else {
        cmd.add("-u");
        cmd.add(toolUserName);
    }
    DBPConnectionConfiguration connectionInfo = settings.getDataSourceContainer().getActualConnectionConfiguration();
    cmd.add("--host=" + connectionInfo.getHostName());
    if (!CommonUtils.isEmpty(connectionInfo.getHostPort())) {
        cmd.add("--port=" + connectionInfo.getHostPort());
    }
    return cmd;
}
Also used : DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) ArrayList(java.util.ArrayList)

Example 78 with DBPConnectionConfiguration

use of org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration in project dbeaver by dbeaver.

the class SpannerConnectionPage method saveSettings.

@Override
public void saveSettings(DBPDataSourceContainer dataSource) {
    DBPConnectionConfiguration connectionInfo = dataSource.getConnectionConfiguration();
    if (projectText != null) {
        connectionInfo.setServerName(projectText.getText().trim());
    }
    if (instanceText != null) {
        connectionInfo.setHostName(instanceText.getText().trim());
    }
    if (databaseText != null) {
        connectionInfo.setDatabaseName(databaseText.getText().trim());
    }
    if (privateKeyFile != null) {
        connectionInfo.setProviderProperty(SpannerConstants.DRIVER_PROP_PVTKEYPATH, privateKeyFile.getText().trim());
    }
    super.saveSettings(dataSource);
}
Also used : DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration)

Example 79 with DBPConnectionConfiguration

use of org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration in project dbeaver by dbeaver.

the class DataSourceUtils method getDataSourceBySpec.

public static DBPDataSourceContainer getDataSourceBySpec(@NotNull DBPProject project, @NotNull String connectionSpec, @Nullable GeneralUtils.IParameterHandler parameterHandler, boolean searchByParameters, boolean createNewDataSource) {
    String driverName = null, url = null, host = null, port = null, server = null, database = null, user = null, password = null, authModelId = null;
    boolean showSystemObjects = false, showUtilityObjects = false, showOnlyEntities = false, hideFolders = false, hideSchemas = false, mergeEntities = false, savePassword = true;
    Boolean autoCommit = null;
    Map<String, String> conProperties = new HashMap<>();
    Map<String, Map<String, String>> handlerProps = new HashMap<>();
    Map<String, String> authProperties = new HashMap<>();
    DBPDataSourceFolder folder = null;
    String dsId = null, dsName = null;
    DBPDataSourceRegistry dsRegistry = project == null ? null : project.getDataSourceRegistry();
    if (dsRegistry == null) {
        log.debug("No datasource registry for project '" + project.getName() + "'");
        return null;
    }
    String[] conParams = connectionSpec.split("\\|");
    for (String cp : conParams) {
        int divPos = cp.indexOf('=');
        if (divPos == -1) {
            continue;
        }
        String paramName = cp.substring(0, divPos);
        String paramValue = cp.substring(divPos + 1);
        switch(paramName) {
            case PARAM_ID:
                dsId = paramValue;
                break;
            case PARAM_DRIVER:
                driverName = paramValue;
                break;
            case PARAM_NAME:
                dsName = paramValue;
                break;
            case PARAM_URL:
                url = paramValue;
                break;
            case PARAM_HOST:
                host = paramValue;
                break;
            case PARAM_PORT:
                port = paramValue;
                break;
            case PARAM_SERVER:
                server = paramValue;
                break;
            case PARAM_DATABASE:
                database = paramValue;
                break;
            case PARAM_USER:
                user = paramValue;
                break;
            case PARAM_PASSWORD:
                password = paramValue;
                break;
            case PARAM_AUTH_MODEL:
                authModelId = paramValue;
                break;
            case PARAM_SAVE_PASSWORD:
                savePassword = CommonUtils.toBoolean(paramValue);
                break;
            case PARAM_SHOW_SYSTEM_OBJECTS:
                showSystemObjects = CommonUtils.toBoolean(paramValue);
                break;
            case PARAM_SHOW_UTILITY_OBJECTS:
                showUtilityObjects = CommonUtils.toBoolean(paramValue);
                break;
            case PARAM_SHOW_ONLY_ENTITIES:
                showOnlyEntities = CommonUtils.toBoolean(paramValue);
                break;
            case PARAM_HIDE_FOLDERS:
                hideFolders = CommonUtils.toBoolean(paramValue);
                break;
            case PARAM_HIDE_SCHEMAS:
                hideSchemas = CommonUtils.toBoolean(paramValue);
                break;
            case PARAM_MERGE_ENTITIES:
                mergeEntities = CommonUtils.toBoolean(paramValue);
                break;
            case PARAM_FOLDER:
                folder = dsRegistry.getFolder(paramValue);
                break;
            case PARAM_AUTO_COMMIT:
                autoCommit = CommonUtils.toBoolean(paramValue);
                break;
            default:
                boolean handled = false;
                if (paramName.length() > PREFIX_PROP.length() && paramName.startsWith(PREFIX_PROP)) {
                    paramName = paramName.substring(PREFIX_PROP.length());
                    conProperties.put(paramName, paramValue);
                    handled = true;
                } else if (paramName.length() > PREFIX_AUTH_PROP.length() && paramName.startsWith(PREFIX_AUTH_PROP)) {
                    paramName = paramName.substring(PREFIX_AUTH_PROP.length());
                    authProperties.put(paramName, paramValue);
                    handled = true;
                } else if (paramName.length() > PREFIX_HANDLER.length() && paramName.startsWith(PREFIX_HANDLER)) {
                    // network handler prop
                    paramName = paramName.substring(PREFIX_HANDLER.length());
                    divPos = paramName.indexOf('.');
                    if (divPos == -1) {
                        log.debug("Wrong handler parameter: '" + paramName + "'");
                        continue;
                    }
                    String handlerId = paramName.substring(0, divPos);
                    paramName = paramName.substring(divPos + 1);
                    Map<String, String> handlerPopMap = handlerProps.computeIfAbsent(handlerId, k -> new HashMap<>());
                    handlerPopMap.put(paramName, paramValue);
                    handled = true;
                } else if (parameterHandler != null) {
                    handled = parameterHandler.setParameter(paramName, paramValue);
                }
                if (!handled) {
                    log.debug("Unknown connection parameter '" + paramName + "'");
                }
        }
    }
    DBPDataSourceContainer dataSource = null;
    if (dsId != null) {
        dataSource = dsRegistry.getDataSource(dsId);
    }
    if (dsName != null) {
        dataSource = dsRegistry.findDataSourceByName(dsName);
    }
    if (dataSource != null) {
        DBPConnectionConfiguration connConfig = dataSource.getConnectionConfiguration();
        if (!CommonUtils.isEmpty(database))
            connConfig.setDatabaseName(database);
        if (!CommonUtils.isEmpty(user))
            connConfig.setUserName(user);
        if (!CommonUtils.isEmpty(password))
            connConfig.setUserPassword(password);
        if (!CommonUtils.isEmpty(conProperties))
            connConfig.setProperties(conProperties);
        if (!CommonUtils.isEmpty(authProperties))
            connConfig.setAuthProperties(authProperties);
        if (!CommonUtils.isEmpty(authModelId))
            connConfig.setAuthModelId(authModelId);
        return dataSource;
    }
    if (searchByParameters) {
        // Try to find by parameters / handler props
        if (url != null) {
            for (DBPDataSourceContainer ds : dsRegistry.getDataSources()) {
                if (url.equals(ds.getConnectionConfiguration().getUrl())) {
                    if (user == null || user.equals(ds.getConnectionConfiguration().getUserName())) {
                        return ds;
                    }
                }
            }
        } else {
            for (DBPDataSourceContainer ds : dsRegistry.getDataSources()) {
                DBPConnectionConfiguration cfg = ds.getConnectionConfiguration();
                if (server != null && !server.equals(cfg.getServerName()) || host != null && !host.equals(cfg.getHostName()) || port != null && !port.equals(cfg.getHostPort()) || database != null && !database.equals(cfg.getDatabaseName()) || user != null && !user.equals(cfg.getUserName())) {
                    continue;
                }
                boolean matched = true;
                if (!conProperties.isEmpty()) {
                    for (Map.Entry<String, String> prop : conProperties.entrySet()) {
                        if (!CommonUtils.equalObjects(cfg.getProperty(prop.getKey()), prop.getValue())) {
                            matched = false;
                            break;
                        }
                    }
                    if (!matched) {
                        continue;
                    }
                }
                if (!handlerProps.isEmpty()) {
                    for (Map.Entry<String, Map<String, String>> handlerProp : handlerProps.entrySet()) {
                        DBWHandlerConfiguration handler = cfg.getHandler(handlerProp.getKey());
                        if (handler == null) {
                            matched = false;
                            break;
                        }
                        for (Map.Entry<String, String> prop : handlerProp.getValue().entrySet()) {
                            if (!CommonUtils.equalObjects(handler.getProperty(prop.getKey()), prop.getValue())) {
                                matched = false;
                                break;
                            }
                        }
                        if (!matched) {
                            break;
                        }
                    }
                    if (!matched) {
                        continue;
                    }
                }
                return ds;
            }
        }
    }
    if (!createNewDataSource) {
        return null;
    }
    if (driverName == null) {
        log.error("Driver name not specified - can't create new datasource");
        return null;
    }
    DBPDriver driver = DBWorkbench.getPlatform().getDataSourceProviderRegistry().findDriver(driverName);
    if (driver == null) {
        log.error("Driver '" + driverName + "' not found");
        return null;
    }
    // Create new datasource with specified parameters
    if (dsName == null) {
        dsName = "Ext: " + driver.getName();
        if (database != null) {
            dsName += " - " + database;
        } else if (server != null) {
            dsName += " - " + server;
        }
    }
    DBPConnectionConfiguration connConfig = new DBPConnectionConfiguration();
    connConfig.setUrl(url);
    connConfig.setHostName(host);
    connConfig.setHostPort(port);
    connConfig.setServerName(server);
    connConfig.setDatabaseName(database);
    connConfig.setUserName(user);
    connConfig.setUserPassword(password);
    connConfig.setProperties(conProperties);
    if (!CommonUtils.isEmpty(authProperties)) {
        connConfig.setAuthProperties(authProperties);
    }
    if (!CommonUtils.isEmpty(authModelId)) {
        connConfig.setAuthModelId(authModelId);
    }
    if (autoCommit != null) {
        connConfig.getBootstrap().setDefaultAutoCommit(autoCommit);
    }
    DBPDataSourceContainer newDS = dsRegistry.createDataSource(driver, connConfig);
    newDS.setName(dsName);
    ((DataSourceDescriptor) newDS).setTemporary(true);
    if (savePassword) {
        newDS.setSavePassword(true);
    }
    if (folder != null) {
        newDS.setFolder(folder);
    }
    DataSourceNavigatorSettings navSettings = ((DataSourceDescriptor) newDS).getNavigatorSettings();
    navSettings.setShowSystemObjects(showSystemObjects);
    navSettings.setShowUtilityObjects(showUtilityObjects);
    navSettings.setShowOnlyEntities(showOnlyEntities);
    navSettings.setHideSchemas(hideSchemas);
    navSettings.setHideFolders(hideFolders);
    navSettings.setMergeEntities(mergeEntities);
    // ds.set
    dsRegistry.addDataSource(newDS);
    return newDS;
}
Also used : HashMap(java.util.HashMap) DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) DBWHandlerConfiguration(org.jkiss.dbeaver.model.net.DBWHandlerConfiguration) DBPDriver(org.jkiss.dbeaver.model.connection.DBPDriver) DBPDataSourceFolder(org.jkiss.dbeaver.model.DBPDataSourceFolder) DBPDataSourceRegistry(org.jkiss.dbeaver.model.app.DBPDataSourceRegistry) HashMap(java.util.HashMap) Map(java.util.Map) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Example 80 with DBPConnectionConfiguration

use of org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration in project dbeaver by dbeaver.

the class SQLServerConnectionPage method saveSettings.

@Override
public void saveSettings(DBPDataSourceContainer dataSource) {
    DBPConnectionConfiguration connectionInfo = dataSource.getConnectionConfiguration();
    if (hostText != null) {
        connectionInfo.setHostName(hostText.getText().trim());
    }
    if (portText != null) {
        connectionInfo.setHostPort(portText.getText().trim());
    }
    if (dbText != null) {
        connectionInfo.setDatabaseName(dbText.getText().trim());
    }
    if (userNameText != null) {
        connectionInfo.setUserName(userNameText.getText().trim());
    }
    if (passwordText != null) {
        connectionInfo.setUserPassword(passwordText.getText());
    }
    if (authCombo != null) {
        SQLServerAuthentication authSchema = authSchemas[authCombo.getSelectionIndex()];
        connectionInfo.setProviderProperty(SQLServerConstants.PROP_AUTHENTICATION, authSchema.name());
        if (SQLServerConstants.PROVIDER_GENERIC.equals(getSite().getDriver().getProviderId())) {
            if (authSchema == SQLServerAuthentication.WINDOWS_INTEGRATED) {
                connectionInfo.getProperties().put(SQLServerConstants.PROP_CONNECTION_INTEGRATED_SECURITY, String.valueOf(true));
            } else {
                connectionInfo.getProperties().remove(SQLServerConstants.PROP_CONNECTION_INTEGRATED_SECURITY);
            }
        }
    }
    /*
        if (windowsAuthenticationButton != null) {
            connectionInfo.getProperties().put(SQLServerConstants.PROP_CONNECTION_INTEGRATED_SECURITY,
                    String.valueOf(windowsAuthenticationButton.getSelection()));
        }
        if (adpAuthenticationButton != null) {
            if (adpAuthenticationButton.getSelection()) {
                connectionInfo.getProperties().put(SQLServerConstants.PROP_CONNECTION_AUTHENTICATION, SQLServerConstants.AUTH_ACTIVE_DIRECTORY_PASSWORD);
            } else {
                connectionInfo.getProperties().remove(SQLServerConstants.PROP_CONNECTION_AUTHENTICATION);
            }
        }
*/
    if (showAllSchemas != null) {
        connectionInfo.setProviderProperty(SQLServerConstants.PROP_SHOW_ALL_SCHEMAS, String.valueOf(showAllSchemas.getSelection()));
    }
    super.saveSettings(dataSource);
}
Also used : DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) SQLServerAuthentication(org.jkiss.dbeaver.ext.mssql.model.SQLServerAuthentication)

Aggregations

DBPConnectionConfiguration (org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration)172 DBException (org.jkiss.dbeaver.DBException)25 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)18 DataSourceDescriptor (org.jkiss.dbeaver.registry.DataSourceDescriptor)16 DBPDriver (org.jkiss.dbeaver.model.connection.DBPDriver)14 DBWHandlerConfiguration (org.jkiss.dbeaver.model.net.DBWHandlerConfiguration)12 ArrayList (java.util.ArrayList)10 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)10 SelectionEvent (org.eclipse.swt.events.SelectionEvent)10 GridData (org.eclipse.swt.layout.GridData)10 DBPDataSourceRegistry (org.jkiss.dbeaver.model.app.DBPDataSourceRegistry)10 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 Button (org.eclipse.swt.widgets.Button)8 Group (org.eclipse.swt.widgets.Group)8 Label (org.eclipse.swt.widgets.Label)8 DBPDataSourceFolder (org.jkiss.dbeaver.model.DBPDataSourceFolder)7 Map (java.util.Map)6 Properties (java.util.Properties)6 DriverDescriptor (org.jkiss.dbeaver.registry.driver.DriverDescriptor)6 File (java.io.File)5