Search in sources :

Example 6 with SQLNavigatorContext

use of org.jkiss.dbeaver.ui.editors.sql.handlers.SQLNavigatorContext in project dbeaver by dbeaver.

the class SQLCommandInclude method handleCommand.

@Override
public boolean handleCommand(SQLControlCommand command, final SQLScriptContext scriptContext) throws DBException {
    String fileName = command.getParameter();
    if (CommonUtils.isEmpty(fileName)) {
        throw new DBException("Empty input file");
    }
    fileName = GeneralUtils.replaceVariables(fileName, new ScriptVariablesResolver(scriptContext)).trim();
    fileName = DBUtils.getUnQuotedIdentifier(scriptContext.getExecutionContext().getDataSource(), fileName);
    File curFile = scriptContext.getSourceFile();
    File incFile = curFile == null ? new File(fileName) : new File(curFile.getParent(), fileName);
    if (!incFile.exists()) {
        incFile = new File(fileName);
    }
    if (!incFile.exists()) {
        throw new DBException("File '" + fileName + "' not found");
    }
    final String fileContents;
    try (InputStream is = new FileInputStream(incFile)) {
        Reader reader = new InputStreamReader(is, getResourceEncoding());
        fileContents = IOUtils.readToString(reader);
    } catch (IOException e) {
        throw new DBException("IO error reading file '" + fileName + "'", e);
    }
    final File finalIncFile = incFile;
    final boolean[] statusFlag = new boolean[1];
    UIUtils.syncExec(() -> {
        try {
            final IWorkbenchWindow workbenchWindow = UIUtils.getActiveWorkbenchWindow();
            final IncludeEditorInput input = new IncludeEditorInput(finalIncFile, fileContents);
            SQLEditor sqlEditor = SQLEditorHandlerOpenEditor.openSQLConsole(workbenchWindow, new SQLNavigatorContext(scriptContext.getExecutionContext()), input);
            final IncludeScriptListener scriptListener = new IncludeScriptListener(workbenchWindow, sqlEditor, statusFlag);
            boolean execResult = sqlEditor.processSQL(false, true, null, scriptListener);
            if (!execResult) {
                statusFlag[0] = true;
            }
        } catch (Throwable e) {
            log.error(e);
            statusFlag[0] = true;
        }
    });
    // Wait until script finished
    while (!statusFlag[0]) {
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            break;
        }
    }
    return true;
}
Also used : DBException(org.jkiss.dbeaver.DBException) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) SQLEditor(org.jkiss.dbeaver.ui.editors.sql.SQLEditor) SQLNavigatorContext(org.jkiss.dbeaver.ui.editors.sql.handlers.SQLNavigatorContext) ScriptVariablesResolver(org.jkiss.dbeaver.model.sql.eval.ScriptVariablesResolver)

Example 7 with SQLNavigatorContext

use of org.jkiss.dbeaver.ui.editors.sql.handlers.SQLNavigatorContext in project dbeaver by dbeaver.

the class DBeaverInstanceServer method openDatabaseConnection.

@Override
public void openDatabaseConnection(String connectionSpec) throws RemoteException {
    // Do not log it (#3788)
    // log.debug("Open external database connection [" + connectionSpec + "]");
    InstanceConnectionParameters instanceConParameters = new InstanceConnectionParameters();
    final DBPDataSourceContainer dataSource = DataSourceUtils.getDataSourceBySpec(DBWorkbench.getPlatform().getWorkspace().getActiveProject(), connectionSpec, instanceConParameters, false, instanceConParameters.createNewConnection);
    if (dataSource == null) {
        return;
    }
    if (instanceConParameters.openConsole) {
        final IWorkbenchWindow workbenchWindow = UIUtils.getActiveWorkbenchWindow();
        UIUtils.syncExec(() -> {
            SQLEditorHandlerOpenEditor.openSQLConsole(workbenchWindow, new SQLNavigatorContext(dataSource), dataSource.getName(), "");
            workbenchWindow.getShell().forceActive();
        });
    } else if (instanceConParameters.makeConnect) {
        DataSourceHandler.connectToDataSource(null, dataSource, null);
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) SQLNavigatorContext(org.jkiss.dbeaver.ui.editors.sql.handlers.SQLNavigatorContext) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Example 8 with SQLNavigatorContext

use of org.jkiss.dbeaver.ui.editors.sql.handlers.SQLNavigatorContext in project dbeaver by serge-rider.

the class DBeaverInstanceServer method openDatabaseConnection.

@Override
public void openDatabaseConnection(String connectionSpec) throws RemoteException {
    // Do not log it (#3788)
    // log.debug("Open external database connection [" + connectionSpec + "]");
    InstanceConnectionParameters instanceConParameters = new InstanceConnectionParameters();
    final DBPDataSourceContainer dataSource = DataSourceUtils.getDataSourceBySpec(DBWorkbench.getPlatform().getWorkspace().getActiveProject(), connectionSpec, instanceConParameters, false, instanceConParameters.createNewConnection);
    if (dataSource == null) {
        return;
    }
    if (instanceConParameters.openConsole) {
        final IWorkbenchWindow workbenchWindow = UIUtils.getActiveWorkbenchWindow();
        UIUtils.syncExec(() -> {
            SQLEditorHandlerOpenEditor.openSQLConsole(workbenchWindow, new SQLNavigatorContext(dataSource), dataSource.getName(), "");
            workbenchWindow.getShell().forceActive();
        });
    } else if (instanceConParameters.makeConnect) {
        DataSourceHandler.connectToDataSource(null, dataSource, null);
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) SQLNavigatorContext(org.jkiss.dbeaver.ui.editors.sql.handlers.SQLNavigatorContext) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Example 9 with SQLNavigatorContext

use of org.jkiss.dbeaver.ui.editors.sql.handlers.SQLNavigatorContext in project dbeaver by serge-rider.

the class QueryLogViewer method openSelectionInEditor.

private void openSelectionInEditor() {
    DBPDataSourceContainer dsContainer = null;
    StringBuilder sql = new StringBuilder();
    TableItem[] items = logTable.getSelection();
    for (TableItem item : items) {
        QMMetaEvent event = (QMMetaEvent) item.getData();
        QMMObject object = event.getObject();
        if (object instanceof QMMStatementExecuteInfo) {
            QMMStatementExecuteInfo stmtExec = (QMMStatementExecuteInfo) object;
            if (dsContainer == null) {
                QMMSessionInfo session = stmtExec.getStatement().getSession();
                DBPProject project = session.getProject();
                String containerId = session.getContainerId();
                if (project != null) {
                    dsContainer = project.getDataSourceRegistry().getDataSource(containerId);
                } else {
                    dsContainer = DBUtils.findDataSource(containerId);
                }
            }
            String queryString = stmtExec.getQueryString();
            if (!CommonUtils.isEmptyTrimmed(queryString)) {
                if (sql.length() > 0) {
                    // $NON-NLS-1$
                    sql.append("\n");
                }
                queryString = queryString.trim();
                sql.append(queryString);
                if (!queryString.endsWith(SQLConstants.DEFAULT_STATEMENT_DELIMITER)) {
                    // $NON-NLS-1$
                    sql.append(SQLConstants.DEFAULT_STATEMENT_DELIMITER).append("\n");
                }
            }
        }
    }
    if (sql.length() > 0) {
        SQLEditorHandlerOpenEditor.openSQLConsole(UIUtils.getActiveWorkbenchWindow(), new SQLNavigatorContext(dsContainer), // $NON-NLS-1$
        "QueryManager", sql.toString());
    }
}
Also used : DBPProject(org.jkiss.dbeaver.model.app.DBPProject) SQLNavigatorContext(org.jkiss.dbeaver.ui.editors.sql.handlers.SQLNavigatorContext) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Example 10 with SQLNavigatorContext

use of org.jkiss.dbeaver.ui.editors.sql.handlers.SQLNavigatorContext in project dbeaver by serge-rider.

the class SQLEditor method setDataSourceContainer.

@Override
public boolean setDataSourceContainer(@Nullable DBPDataSourceContainer container) {
    if (container == dataSourceContainer) {
        return true;
    }
    // Release ds container
    releaseContainer();
    closeAllJobs();
    dataSourceContainer = container;
    if (dataSourceContainer != null) {
        dataSourceContainer.getPreferenceStore().addPropertyChangeListener(this);
        dataSourceContainer.getRegistry().addDataSourceListener(this);
    }
    IEditorInput input = getEditorInput();
    if (input != null) {
        DBPDataSourceContainer savedContainer = EditorUtils.getInputDataSource(input);
        if (savedContainer != container) {
            EditorUtils.setInputDataSource(input, new SQLNavigatorContext(container, getExecutionContext()));
        }
        IFile file = EditorUtils.getFileFromInput(input);
        if (file != null) {
            DBNUtils.refreshNavigatorResource(file, container);
        } else {
            // FIXME: this is a hack. We can't fire event on resource change so editor's state won't be updated in UI.
            // FIXME: To update main toolbar and other controls we hade and show this editor
            IWorkbenchPage page = getSite().getPage();
            for (IEditorReference er : page.getEditorReferences()) {
                if (er.getEditor(false) == this) {
                    page.hideEditor(er);
                    page.showEditor(er);
                    break;
                }
            }
        // page.activate(this);
        }
    }
    checkConnected(false, status -> UIUtils.asyncExec(() -> {
        if (!status.isOK()) {
            DBWorkbench.getPlatformUI().showError("Can't connect to database", "Error connecting to datasource", status);
        }
        setFocus();
    }));
    setPartName(getEditorName());
    fireDataSourceChange();
    if (dataSourceContainer != null) {
        dataSourceContainer.acquire(this);
    }
    if (SQLEditorBase.isWriteEmbeddedBinding()) {
        // Patch connection reference
        UIUtils.syncExec(this::embedDataSourceAssociation);
    }
    return true;
}
Also used : IFile(org.eclipse.core.resources.IFile) SQLNavigatorContext(org.jkiss.dbeaver.ui.editors.sql.handlers.SQLNavigatorContext)

Aggregations

SQLNavigatorContext (org.jkiss.dbeaver.ui.editors.sql.handlers.SQLNavigatorContext)14 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)4 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)4 IFileStore (org.eclipse.core.filesystem.IFileStore)2 IFile (org.eclipse.core.resources.IFile)2 FileStoreEditorInput (org.eclipse.ui.ide.FileStoreEditorInput)2 DBException (org.jkiss.dbeaver.DBException)2 DBPProject (org.jkiss.dbeaver.model.app.DBPProject)2 ScriptVariablesResolver (org.jkiss.dbeaver.model.sql.eval.ScriptVariablesResolver)2 SQLEditor (org.jkiss.dbeaver.ui.editors.sql.SQLEditor)2