Search in sources :

Example 1 with SQLNavigatorContext

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

the class SQLEditor method afterSaveToFile.

protected void afterSaveToFile(File saveFile) {
    try {
        IFileStore fileStore = EFS.getStore(saveFile.toURI());
        IEditorInput input = new FileStoreEditorInput(fileStore);
        EditorUtils.setInputDataSource(input, new SQLNavigatorContext(getDataSourceContainer(), getExecutionContext()));
        setInput(input);
    } catch (CoreException e) {
        DBWorkbench.getPlatformUI().showError("File save", "Can't open SQL editor from external file", e);
    }
}
Also used : IFileStore(org.eclipse.core.filesystem.IFileStore) SQLNavigatorContext(org.jkiss.dbeaver.ui.editors.sql.handlers.SQLNavigatorContext) FileStoreEditorInput(org.eclipse.ui.ide.FileStoreEditorInput)

Example 2 with SQLNavigatorContext

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

the class SQLEditor method onDataSourceChange.

private void onDataSourceChange() {
    if (resultsSash == null || resultsSash.isDisposed()) {
        reloadSyntaxRules();
        return;
    }
    DatabaseEditorUtils.setPartBackground(this, resultTabs);
    if (getSourceViewerConfiguration() instanceof SQLEditorSourceViewerConfiguration) {
        ((SQLEditorSourceViewerConfiguration) getSourceViewerConfiguration()).onDataSourceChange();
    }
    DBCExecutionContext executionContext = getExecutionContext();
    if (executionContext != null) {
        EditorUtils.setInputDataSource(getEditorInput(), new SQLNavigatorContext(executionContext));
    }
    refreshActions();
    if (syntaxLoaded && lastExecutionContext == executionContext) {
        return;
    }
    if (curResultsContainer != null) {
        ResultSetViewer rsv = curResultsContainer.getResultSetController();
        if (rsv != null) {
            if (executionContext == null) {
                rsv.setStatus(ModelMessages.error_not_connected_to_database);
            } else {
                // $NON-NLS-2$
                rsv.setStatus(SQLEditorMessages.editors_sql_staus_connected_to + executionContext.getDataSource().getContainer().getName() + "'");
            }
        }
    }
    if (lastExecutionContext == null || executionContext == null || lastExecutionContext.getDataSource() != executionContext.getDataSource()) {
        // Update command states
        SQLEditorPropertyTester.firePropertyChange(SQLEditorPropertyTester.PROP_CAN_EXECUTE);
        SQLEditorPropertyTester.firePropertyChange(SQLEditorPropertyTester.PROP_CAN_EXPLAIN);
        reloadSyntaxRules();
    }
    if (getDataSourceContainer() == null) {
        resultsSash.setMaximizedControl(sqlEditorPanel);
    } else {
        resultsSash.setMaximizedControl(null);
    }
    lastExecutionContext = executionContext;
    syntaxLoaded = true;
    loadActivePreferenceSettings();
}
Also used : SQLNavigatorContext(org.jkiss.dbeaver.ui.editors.sql.handlers.SQLNavigatorContext)

Example 3 with SQLNavigatorContext

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

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 4 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 5 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)

Aggregations

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