Search in sources :

Example 41 with DBPDataSourceContainer

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

the class SQLScriptTaskScriptSelectorDialog method createScriptColumns.

static void createScriptColumns(ColumnViewer viewer) {
    final ILabelProvider mainLabelProvider = (ILabelProvider) viewer.getLabelProvider();
    ViewerColumnController columnController = new ViewerColumnController("sqlTaskScriptViewer", viewer);
    columnController.setForceAutoSize(true);
    columnController.addColumn(ModelMessages.model_navigator_Name, DTUIMessages.sql_script_task_selector_dialog_column_description_script, SWT.LEFT, true, true, new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            return mainLabelProvider.getText(element);
        }

        @Override
        public Image getImage(Object element) {
            return mainLabelProvider.getImage(element);
        }

        @Override
        public String getToolTipText(Object element) {
            if (mainLabelProvider instanceof IToolTipProvider) {
                return ((IToolTipProvider) mainLabelProvider).getToolTipText(element);
            }
            return null;
        }
    });
    columnController.addColumn(ModelMessages.model_navigator_Connection, DTUIMessages.sql_script_task_selector_dialog_column_description_script_data_source, SWT.LEFT, true, true, new ColumnLabelProvider() {

        @Override
        public String getText(Object element) {
            if (element instanceof DBNResource) {
                Collection<DBPDataSourceContainer> containers = ((DBNResource) element).getAssociatedDataSources();
                if (!CommonUtils.isEmpty(containers)) {
                    StringBuilder text = new StringBuilder();
                    for (DBPDataSourceContainer container : containers) {
                        if (text.length() > 0) {
                            text.append(", ");
                        }
                        text.append(container.getName());
                    }
                    return text.toString();
                }
            }
            return "";
        }

        @Override
        public Image getImage(Object element) {
            return null;
        }
    });
    columnController.createColumns(true);
}
Also used : DBNResource(org.jkiss.dbeaver.model.navigator.DBNResource) ViewerColumnController(org.jkiss.dbeaver.ui.controls.ViewerColumnController) Collection(java.util.Collection) Image(org.eclipse.swt.graphics.Image) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Example 42 with DBPDataSourceContainer

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

the class SQLScriptTaskPageSettings method loadSettings.

public void loadSettings() {
    SQLScriptExecuteSettings settings = sqlWizard.getSettings();
    List<String> scriptFiles = settings.getScriptFiles();
    for (String filePath : scriptFiles) {
        IFile file = SQLScriptExecuteSettings.getWorkspaceFile(filePath);
        if (file == null) {
            log.debug("Script file '" + filePath + "' not found");
            continue;
        }
        DBPProject currentProject = DBWorkbench.getPlatform().getWorkspace().getProject(file.getProject());
        if (currentProject == null) {
            log.debug("Project '" + file.getProject().getName() + "' not found");
            continue;
        }
        DBNProject projectNode = DBWorkbench.getPlatform().getNavigatorModel().getRoot().getProjectNode(currentProject);
        if (projectNode != null) {
            DBNResource resource = projectNode.findResource(file);
            if (resource != null) {
                selectedScripts.add(resource);
            }
        }
    }
    scriptsViewer.setInput(selectedScripts);
    for (DBPDataSourceContainer dataSource : settings.getDataSources()) {
        DBNProject projectNode = DBWorkbench.getPlatform().getNavigatorModel().getRoot().getProjectNode(dataSource.getProject());
        DBNDataSource dsNode = projectNode.getDatabases().getDataSource(dataSource);
        if (dsNode != null) {
            selectedDataSources.add(dsNode);
        }
    }
    dataSourceViewer.setInput(selectedDataSources);
// if (!selectedDataSources.isEmpty()) {
// dataSourceTree.getCheckboxViewer().setCheckedElements(selectedDataSources.toArray());
// dataSourceTree.getCheckboxViewer().reveal(selectedDataSources.get(0));
// }
}
Also used : DBNResource(org.jkiss.dbeaver.model.navigator.DBNResource) DBNProject(org.jkiss.dbeaver.model.navigator.DBNProject) IFile(org.eclipse.core.resources.IFile) SQLScriptExecuteSettings(org.jkiss.dbeaver.tools.sql.SQLScriptExecuteSettings) DBNDataSource(org.jkiss.dbeaver.model.navigator.DBNDataSource) DBPProject(org.jkiss.dbeaver.model.app.DBPProject) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Example 43 with DBPDataSourceContainer

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

the class SQLScriptExecuteSettings method saveConfiguration.

public void saveConfiguration(Map<String, Object> config) {
    config.put("scriptFiles", scriptFiles);
    List<Map<String, Object>> dsConfig = new ArrayList<>();
    config.put("dataSources", dsConfig);
    for (DBPDataSourceContainer ds : dataSources) {
        Map<String, Object> dsInfo = new LinkedHashMap<>();
        dsInfo.put("project", ds.getProject().getName());
        dsInfo.put("dataSource", ds.getId());
        dsConfig.add(dsInfo);
    }
    config.put("ignoreErrors", ignoreErrors);
    config.put("dumpQueryResultsToLog", dumpQueryResultsToLog);
    config.put("autoCommit", autoCommit);
}
Also used : ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) LinkedHashMap(java.util.LinkedHashMap)

Example 44 with DBPDataSourceContainer

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

the class SQLScriptExecuteSettings method loadConfiguration.

public void loadConfiguration(DBRRunnableContext runnableContext, Map<String, Object> config) {
    // Legacy config support (single datasource
    String projectName = JSONUtils.getString(config, "project");
    DBPProject project = CommonUtils.isEmpty(projectName) ? null : DBWorkbench.getPlatform().getWorkspace().getProject(projectName);
    if (project != null) {
        String dataSourceContainerId = JSONUtils.getString(config, "dataSourceContainer");
        if (!CommonUtils.isEmpty(dataSourceContainerId)) {
            DBPDataSourceContainer dataSource = project.getDataSourceRegistry().getDataSource(dataSourceContainerId);
            if (dataSource != null) {
                dataSources.add(dataSource);
            }
        }
    } else {
        // Modern config (datasource list)
        List<Map<String, Object>> dsConfig = JSONUtils.getObjectList(config, "dataSources");
        for (Map<String, Object> dsInfo : dsConfig) {
            projectName = JSONUtils.getString(dsInfo, "project");
            project = CommonUtils.isEmpty(projectName) ? null : DBWorkbench.getPlatform().getWorkspace().getProject(projectName);
            if (project != null) {
                String dataSourceContainerId = JSONUtils.getString(dsInfo, "dataSource");
                if (!CommonUtils.isEmpty(dataSourceContainerId)) {
                    DBPDataSourceContainer dataSource = project.getDataSourceRegistry().getDataSource(dataSourceContainerId);
                    if (dataSource != null) {
                        dataSources.add(dataSource);
                    }
                }
            }
        }
    }
    scriptFiles = JSONUtils.deserializeStringList(config, "scriptFiles");
    ignoreErrors = JSONUtils.getBoolean(config, "ignoreErrors");
    dumpQueryResultsToLog = JSONUtils.getBoolean(config, "dumpQueryResultsToLog");
    autoCommit = JSONUtils.getBoolean(config, "autoCommit");
}
Also used : DBPProject(org.jkiss.dbeaver.model.app.DBPProject) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 45 with DBPDataSourceContainer

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

the class SQLScriptExecuteHandler method runScripts.

private void runScripts(DBRProgressMonitor monitor, DBTTask task, SQLScriptExecuteSettings settings, Log log, PrintStream logStream) throws DBException {
    List<DBPDataSourceContainer> dataSources = settings.getDataSources();
    for (String filePath : settings.getScriptFiles()) {
        IFile sqlFile = SQLScriptExecuteSettings.getWorkspaceFile(filePath);
        try (InputStream sqlStream = sqlFile.getContents(true)) {
            try (Reader fileReader = new InputStreamReader(sqlStream, sqlFile.getCharset())) {
                String sqlScriptContent = IOUtils.readToString(fileReader);
                try {
                    for (DBPDataSourceContainer dataSourceContainer : dataSources) {
                        if (!dataSourceContainer.isConnected()) {
                            dataSourceContainer.connect(monitor, true, true);
                        }
                        DBPDataSource dataSource = dataSourceContainer.getDataSource();
                        if (dataSource == null) {
                            throw new DBException("Can't obtain data source connection");
                        }
                        DBCExecutionContext executionContext = dataSource.getDefaultInstance().getDefaultContext(monitor, false);
                        log.debug("> Execute script [" + filePath + "] in [" + dataSourceContainer.getName() + "]");
                        DBCExecutionContextDefaults contextDefaults = executionContext.getContextDefaults();
                        if (contextDefaults != null) {
                            DBSCatalog defaultCatalog = contextDefaults.getDefaultCatalog();
                            if (defaultCatalog != null) {
                                log.debug("> Default catalog: " + defaultCatalog.getName());
                            }
                            DBSSchema defaultSchema = contextDefaults.getDefaultSchema();
                            if (defaultSchema != null) {
                                log.debug("> Default schema: " + defaultSchema.getName());
                            }
                        }
                        processScript(monitor, task, settings, executionContext, filePath, sqlScriptContent, log, logStream);
                    }
                } catch (Exception e) {
                    throw new InvocationTargetException(e);
                }
            }
        } catch (Throwable e) {
            Throwable error = e instanceof InvocationTargetException ? ((InvocationTargetException) e).getTargetException() : e;
            throw new DBException("Error executing script '" + filePath + "'", error);
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBCExecutionContextDefaults(org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults) IFile(org.eclipse.core.resources.IFile) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBSCatalog(org.jkiss.dbeaver.model.struct.rdb.DBSCatalog) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBSSchema(org.jkiss.dbeaver.model.struct.rdb.DBSSchema) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Aggregations

DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)148 IFile (org.eclipse.core.resources.IFile)22 ArrayList (java.util.ArrayList)21 DBException (org.jkiss.dbeaver.DBException)19 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)19 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)19 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)14 DBNDataSource (org.jkiss.dbeaver.model.navigator.DBNDataSource)14 InvocationTargetException (java.lang.reflect.InvocationTargetException)13 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)13 DBPConnectionConfiguration (org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration)12 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)12 IFolder (org.eclipse.core.resources.IFolder)11 IEditorPart (org.eclipse.ui.IEditorPart)11 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)10 SelectionEvent (org.eclipse.swt.events.SelectionEvent)10 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)10 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)9 GridData (org.eclipse.swt.layout.GridData)9 IDataSourceContainerProvider (org.jkiss.dbeaver.model.IDataSourceContainerProvider)9