Search in sources :

Example 1 with DBPDataSourceFolder

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

the class NavigatorHandlerLocalFolderCreate method createFolder.

public static boolean createFolder(IWorkbenchWindow workbenchWindow, IWorkbenchPart activePart, DBNProjectDatabases databases, final DBNLocalFolder parentFolder, final Collection<DBNDataSource> nodes, String newName) {
    if (newName == null) {
        newName = EnterNameDialog.chooseName(workbenchWindow.getShell(), "Folder name");
    }
    if (CommonUtils.isEmpty(newName)) {
        return false;
    }
    // Create folder and refresh databases root
    // DS container will reload folders on refresh
    final DBPDataSourceRegistry dsRegistry = databases.getDataSourceRegistry();
    DBPDataSourceFolder folder = dsRegistry.addFolder(parentFolder == null ? null : parentFolder.getFolder(), newName);
    for (DBNDataSource node : nodes) {
        node.setFolder(folder);
    }
    if (parentFolder != null && activePart instanceof NavigatorViewBase) {
        final TreeViewer viewer = ((NavigatorViewBase) activePart).getNavigatorViewer();
        if (viewer != null) {
            DBeaverUI.asyncExec(new Runnable() {

                @Override
                public void run() {
                    viewer.expandToLevel(parentFolder, 1);
                }
            });
        }
    }
    DBNModel.updateConfigAndRefreshDatabases(databases);
    return true;
}
Also used : DBNDataSource(org.jkiss.dbeaver.model.navigator.DBNDataSource) TreeViewer(org.eclipse.jface.viewers.TreeViewer) DBPDataSourceFolder(org.jkiss.dbeaver.model.DBPDataSourceFolder) NavigatorViewBase(org.jkiss.dbeaver.ui.navigator.database.NavigatorViewBase) DBPDataSourceRegistry(org.jkiss.dbeaver.model.app.DBPDataSourceRegistry)

Example 2 with DBPDataSourceFolder

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

the class DataSourceDescriptorManager method createNewObject.

@Override
public DataSourceDescriptor createNewObject(DBRProgressMonitor monitor, DBECommandContext commandContext, DBPObject parent, Object copyFrom) {
    if (copyFrom != null) {
        DataSourceDescriptor dsTpl = (DataSourceDescriptor) copyFrom;
        DBPDataSourceRegistry registry;
        DBPDataSourceFolder folder = null;
        if (parent instanceof DataSourceRegistry) {
            registry = (DBPDataSourceRegistry) parent;
        } else if (parent instanceof DBPDataSourceFolder) {
            folder = (DBPDataSourceFolder) parent;
            registry = folder.getDataSourceRegistry();
        } else {
            registry = dsTpl.getRegistry();
        }
        DataSourceDescriptor dataSource = new DataSourceDescriptor(registry, DataSourceDescriptor.generateNewId(dsTpl.getDriver()), dsTpl.getDriver(), new DBPConnectionConfiguration(dsTpl.getConnectionConfiguration()));
        dataSource.copyFrom(dsTpl);
        if (folder != null) {
            dataSource.setFolder(folder);
        }
        // Generate new name
        String origName = dsTpl.getName();
        String newName = origName;
        for (int i = 0; ; i++) {
            if (registry.findDataSourceByName(newName) == null) {
                break;
            }
            newName = origName + " " + (i + 1);
        }
        dataSource.setName(newName);
        registry.addDataSource(dataSource);
    } else {
        DBeaverUI.asyncExec(new Runnable() {

            @Override
            public void run() {
                CreateConnectionDialog dialog = new CreateConnectionDialog(DBeaverUI.getActiveWorkbenchWindow(), new NewConnectionWizard());
                dialog.open();
            }
        });
    }
    return null;
}
Also used : DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) DBPDataSourceRegistry(org.jkiss.dbeaver.model.app.DBPDataSourceRegistry) DBPDataSourceFolder(org.jkiss.dbeaver.model.DBPDataSourceFolder) NewConnectionWizard(org.jkiss.dbeaver.ui.dialogs.connection.NewConnectionWizard) DBPDataSourceRegistry(org.jkiss.dbeaver.model.app.DBPDataSourceRegistry) CreateConnectionDialog(org.jkiss.dbeaver.ui.dialogs.connection.CreateConnectionDialog)

Example 3 with DBPDataSourceFolder

use of org.jkiss.dbeaver.model.DBPDataSourceFolder in project dbeaver by dbeaver.

the class DBeaverInstanceServer method openDatabaseConnection.

@Override
public void openDatabaseConnection(String connectionSpec) throws RemoteException {
    log.debug("Open external database connection [" + connectionSpec + "]");
    final IWorkbenchWindow workbenchWindow = DBeaverUI.getActiveWorkbenchWindow();
    DataSourceRegistry dsRegistry = DBeaverCore.getInstance().getProjectRegistry().getActiveDataSourceRegistry();
    String driverName = null, url = null, host = null, port = null, server = null, database = null, user = null, password = null;
    boolean makeConnect = true, openConsole = false, savePassword = true;
    Boolean autoCommit = null;
    Map<String, String> conProperties = new HashMap<>();
    DBPDataSourceFolder folder = null;
    String dsName = 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 "driver":
                driverName = paramValue;
                break;
            case "name":
                dsName = paramValue;
                break;
            case "url":
                url = paramValue;
                break;
            case "host":
                host = paramValue;
                break;
            case "port":
                port = paramValue;
                break;
            case "server":
                server = paramValue;
                break;
            case "database":
                database = paramValue;
                break;
            case "user":
                user = paramValue;
                break;
            case "password":
                password = paramValue;
                break;
            case "savePassword":
                savePassword = CommonUtils.toBoolean(paramValue);
                break;
            case "connect":
                makeConnect = CommonUtils.toBoolean(paramValue);
                break;
            case "openConsole":
                openConsole = CommonUtils.toBoolean(paramValue);
                break;
            case "folder":
                folder = dsRegistry.getFolder(paramValue);
                break;
            case "autoCommit":
                autoCommit = CommonUtils.toBoolean(paramValue);
                break;
            default:
                if (paramName.length() > 5 && paramName.startsWith("prop.")) {
                    paramName = paramName.substring(5);
                    conProperties.put(paramName, paramValue);
                }
        }
    }
    if (driverName == null) {
        log.error("Driver name not specified");
        return;
    }
    DriverDescriptor driver = DataSourceProviderRegistry.getInstance().findDriver(driverName);
    if (driver == null) {
        log.error("Driver '" + driverName + "' not found");
        return;
    }
    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 (autoCommit != null) {
        connConfig.getBootstrap().setDefaultAutoCommit(autoCommit);
    }
    final DataSourceDescriptor ds = new DataSourceDescriptor(dsRegistry, DataSourceDescriptor.generateNewId(driver), driver, connConfig);
    ds.setName(dsName);
    ds.setTemporary(true);
    if (savePassword) {
        ds.setSavePassword(true);
    }
    if (folder != null) {
        ds.setFolder(folder);
    }
    // ds.set
    dsRegistry.addDataSource(ds);
    if (openConsole) {
        DBeaverUI.syncExec(() -> {
            OpenHandler.openSQLConsole(workbenchWindow, ds, ds.getName(), "");
            workbenchWindow.getShell().forceActive();
        });
    } else if (makeConnect) {
        DataSourceHandler.connectToDataSource(null, ds, null);
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) DriverDescriptor(org.jkiss.dbeaver.registry.driver.DriverDescriptor) HashMap(java.util.HashMap) DBPConnectionConfiguration(org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration) DataSourceRegistry(org.jkiss.dbeaver.registry.DataSourceRegistry) DBPDataSourceFolder(org.jkiss.dbeaver.model.DBPDataSourceFolder) DataSourceDescriptor(org.jkiss.dbeaver.registry.DataSourceDescriptor)

Example 4 with DBPDataSourceFolder

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

the class NavigatorHandlerLocalFolderCreate method createFolder.

public static boolean createFolder(IWorkbenchWindow workbenchWindow, IWorkbenchPart activePart, DBNProjectDatabases databases, final DBNLocalFolder parentFolder, final Collection<DBNDataSource> nodes, String newName) {
    if (newName == null) {
        newName = EnterNameDialog.chooseName(workbenchWindow.getShell(), "Folder name");
    }
    if (CommonUtils.isEmpty(newName)) {
        return false;
    }
    // Create folder and refresh databases root
    // DS container will reload folders on refresh
    final DBPDataSourceRegistry dsRegistry = databases.getDataSourceRegistry();
    DBPDataSourceFolder folder = dsRegistry.addFolder(parentFolder == null ? null : parentFolder.getFolder(), newName);
    for (DBNDataSource node : nodes) {
        node.setFolder(folder);
    }
    if (parentFolder != null && activePart instanceof NavigatorViewBase) {
        final TreeViewer viewer = ((NavigatorViewBase) activePart).getNavigatorViewer();
        if (viewer != null) {
            UIUtils.asyncExec(() -> viewer.expandToLevel(parentFolder, 1));
        }
    }
    DBNModel.updateConfigAndRefreshDatabases(databases);
    return true;
}
Also used : DBNDataSource(org.jkiss.dbeaver.model.navigator.DBNDataSource) TreeViewer(org.eclipse.jface.viewers.TreeViewer) DBPDataSourceFolder(org.jkiss.dbeaver.model.DBPDataSourceFolder) NavigatorViewBase(org.jkiss.dbeaver.ui.navigator.database.NavigatorViewBase) DBPDataSourceRegistry(org.jkiss.dbeaver.model.app.DBPDataSourceRegistry)

Example 5 with DBPDataSourceFolder

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

the class SQLEditorUtils method createNewScript.

public static IFile createNewScript(DBPProject project, @Nullable IFolder folder, @NotNull SQLNavigatorContext navigatorContext) throws CoreException {
    final IProgressMonitor progressMonitor = new NullProgressMonitor();
    // Get folder
    final IFolder scriptsRootFolder = getScriptsFolder(project, true);
    IFolder scriptsFolder = folder;
    if (scriptsFolder == null) {
        scriptsFolder = scriptsRootFolder;
    }
    if (!scriptsFolder.exists()) {
        scriptsFolder.create(true, true, new NullProgressMonitor());
    }
    if (CommonUtils.equalObjects(scriptsRootFolder, scriptsFolder)) {
        // We are in the root folder
        DBPDataSourceContainer dataSourceContainer = navigatorContext.getDataSourceContainer();
        if (dataSourceContainer != null) {
            if (dataSourceContainer.getPreferenceStore().getBoolean(SQLPreferenceConstants.SCRIPT_CREATE_CONNECTION_FOLDERS)) {
                // Create script folders according to connection folders
                DBPDataSourceFolder conFolder = dataSourceContainer.getFolder();
                if (conFolder != null) {
                    List<DBPDataSourceFolder> conFolders = new ArrayList<>();
                    for (DBPDataSourceFolder f = conFolder; f != null; f = f.getParent()) {
                        conFolders.add(0, f);
                    }
                    for (DBPDataSourceFolder f : conFolders) {
                        IFolder dbFolder = scriptsFolder.getFolder(CommonUtils.escapeFileName(f.getName()));
                        if (dbFolder != null) {
                            if (!dbFolder.exists()) {
                                dbFolder.create(true, true, progressMonitor);
                            }
                            scriptsFolder = dbFolder;
                        }
                    }
                }
            }
            if (dataSourceContainer.getPreferenceStore().getBoolean(SQLPreferenceConstants.SCRIPT_AUTO_FOLDERS)) {
                // Create special folder for connection
                IFolder dbFolder = scriptsFolder.getFolder(CommonUtils.escapeFileName(dataSourceContainer.getName()));
                if (dbFolder != null) {
                    if (!dbFolder.exists()) {
                        dbFolder.create(true, true, progressMonitor);
                    }
                    scriptsFolder = dbFolder;
                }
            }
        }
    }
    // Make new script file
    IFile tempFile = ContentUtils.getUniqueFile(scriptsFolder, "Script", SCRIPT_FILE_EXTENSION);
    tempFile.create(new ByteArrayInputStream(new byte[] {}), true, progressMonitor);
    // Save ds container reference
    if (navigatorContext.getDataSourceContainer() != null) {
        EditorUtils.setFileDataSource(tempFile, navigatorContext);
    }
    return tempFile;
}
Also used : IFile(org.eclipse.core.resources.IFile) ByteArrayInputStream(java.io.ByteArrayInputStream) DBPDataSourceFolder(org.jkiss.dbeaver.model.DBPDataSourceFolder) ArrayList(java.util.ArrayList) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) IFolder(org.eclipse.core.resources.IFolder)

Aggregations

DBPDataSourceFolder (org.jkiss.dbeaver.model.DBPDataSourceFolder)16 DBPDataSourceRegistry (org.jkiss.dbeaver.model.app.DBPDataSourceRegistry)10 DBPConnectionConfiguration (org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration)7 TreeViewer (org.eclipse.jface.viewers.TreeViewer)4 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)4 DBNDataSource (org.jkiss.dbeaver.model.navigator.DBNDataSource)4 NavigatorViewBase (org.jkiss.dbeaver.ui.navigator.database.NavigatorViewBase)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 IFile (org.eclipse.core.resources.IFile)3 IFolder (org.eclipse.core.resources.IFolder)3 Map (java.util.Map)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 Point (org.eclipse.swt.graphics.Point)2 GridData (org.eclipse.swt.layout.GridData)2 DBPProject (org.jkiss.dbeaver.model.app.DBPProject)2 DBPDriver (org.jkiss.dbeaver.model.connection.DBPDriver)2 DBWHandlerConfiguration (org.jkiss.dbeaver.model.net.DBWHandlerConfiguration)2