Search in sources :

Example 16 with SQLEditor

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

the class ExecuteHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    SQLEditor editor = RuntimeUtils.getObjectAdapter(HandlerUtil.getActiveEditor(event), SQLEditor.class);
    if (editor == null) {
        log.error("No active SQL editor found");
        return null;
    }
    String actionId = event.getCommand().getId();
    switch(actionId) {
        case CoreCommands.CMD_EXECUTE_STATEMENT:
            editor.processSQL(false, false);
            break;
        case CoreCommands.CMD_EXECUTE_STATEMENT_NEW:
            editor.processSQL(true, false);
            break;
        case CoreCommands.CMD_EXECUTE_SCRIPT:
            editor.processSQL(false, true);
            break;
        case CoreCommands.CMD_EXECUTE_SCRIPT_NEW:
            editor.processSQL(true, true);
            break;
        case CoreCommands.CMD_EXECUTE_ROW_COUNT:
            editor.processSQL(false, false, new SQLQueryTransformerCount());
            break;
        case CoreCommands.CMD_EXECUTE_EXPRESSION:
            editor.processSQL(false, false, new SQLQueryTransformerExpression());
            break;
        case CoreCommands.CMD_EXPLAIN_PLAN:
            editor.explainQueryPlan();
            break;
        default:
            log.error("Unsupported SQL editor command: " + actionId);
            break;
    }
    return null;
}
Also used : SQLEditor(org.jkiss.dbeaver.ui.editors.sql.SQLEditor) SQLQueryTransformerCount(org.jkiss.dbeaver.model.impl.sql.SQLQueryTransformerCount) SQLQueryTransformerExpression(org.jkiss.dbeaver.model.impl.sql.SQLQueryTransformerExpression)

Example 17 with SQLEditor

use of org.jkiss.dbeaver.ui.editors.sql.SQLEditor 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, IDEEncoding.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];
    DBeaverUI.syncExec(new Runnable() {

        @Override
        public void run() {
            final IWorkbenchWindow workbenchWindow = DBeaverUI.getActiveWorkbenchWindow();
            final IncludeEditorInput input = new IncludeEditorInput(finalIncFile, fileContents);
            SQLEditor sqlEditor = OpenHandler.openSQLConsole(workbenchWindow, scriptContext.getExecutionContext().getDataSource().getContainer(), input);
            final IncludeScriptListener scriptListener = new IncludeScriptListener(workbenchWindow, sqlEditor, statusFlag);
            sqlEditor.processSQL(false, true, null, scriptListener);
        }
    });
    // 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) ScriptVariablesResolver(org.jkiss.dbeaver.model.sql.eval.ScriptVariablesResolver)

Example 18 with SQLEditor

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

the class DBeaverStackRenderer method populateFileMenu.

private void populateFileMenu(@NotNull final Menu menu, @NotNull final IWorkbenchPart workbenchPart, @Nullable final IFile inputFile, @NotNull final File file) {
    new MenuItem(menu, SWT.SEPARATOR);
    {
        MenuItem menuItemOpenFolder = new MenuItem(menu, SWT.NONE);
        menuItemOpenFolder.setText("Open Folder in Explorer");
        menuItemOpenFolder.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                if (file.getParentFile().isDirectory()) {
                    UIUtils.launchProgram(file.getParentFile().getAbsolutePath());
                }
            }
        });
    }
    {
        MenuItem menuItemOthers = new MenuItem(menu, SWT.NONE);
        menuItemOthers.setText("Copy File Path");
        menuItemOthers.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                String filePath = file.getAbsolutePath();
                UIUtils.setClipboardContents(Display.getCurrent(), TextTransfer.getInstance(), filePath);
            }
        });
    }
    if (inputFile != null) {
        MenuItem menuItemOthers = new MenuItem(menu, SWT.NONE);
        String renameText = "Rename File";
        if (workbenchPart instanceof SQLEditor) {
            renameText += "\t" + ActionUtils.findCommandDescription(CoreCommands.CMD_SQL_RENAME, workbenchPart.getSite(), true);
        }
        menuItemOthers.setText(renameText);
        menuItemOthers.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                RenameHandler.renameFile(workbenchPart, inputFile, "file");
            }
        });
    }
}
Also used : SQLEditor(org.jkiss.dbeaver.ui.editors.sql.SQLEditor) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) SelectionEvent(org.eclipse.swt.events.SelectionEvent) MenuItem(org.eclipse.swt.widgets.MenuItem)

Example 19 with SQLEditor

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

the class SQLEditorHandlerToggleEditorLayout method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    SQLEditor editor = RuntimeUtils.getObjectAdapter(HandlerUtil.getActiveEditor(event), SQLEditor.class);
    if (editor != null) {
        String curPresentationName = DBWorkbench.getPlatform().getPreferenceStore().getString(SQLPreferenceConstants.RESULT_SET_ORIENTATION);
        SQLEditor.ResultSetOrientation curOrientation = CommonUtils.valueOf(SQLEditor.ResultSetOrientation.class, curPresentationName, SQLEditor.ResultSetOrientation.HORIZONTAL);
        if (curOrientation == SQLEditor.ResultSetOrientation.HORIZONTAL) {
            curOrientation = SQLEditor.ResultSetOrientation.VERTICAL;
        } else {
            curOrientation = SQLEditor.ResultSetOrientation.HORIZONTAL;
        }
        DBWorkbench.getPlatform().getPreferenceStore().setValue(SQLPreferenceConstants.RESULT_SET_ORIENTATION, curOrientation.name());
        try {
            editor.getActivePreferenceStore().save();
        } catch (IOException e) {
            log.error("Error saving editor preferences", e);
        }
    }
    return null;
}
Also used : SQLEditor(org.jkiss.dbeaver.ui.editors.sql.SQLEditor) IOException(java.io.IOException)

Example 20 with SQLEditor

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

the class SQLEditorHandlerOpenObjectConsole method openConsole.

void openConsole(IWorkbenchWindow workbenchWindow, DBRRunnableWithResult<String> generator, SQLNavigatorContext navigatorContext, String title, boolean doRun, ISelection currentSelection) throws Exception {
    UIUtils.runInUI(workbenchWindow, generator);
    String sql = CommonUtils.notEmpty(generator.getResult());
    DBPProject project = navigatorContext.getProject();
    SQLEditorHandlerOpenEditor.checkProjectIsOpen(project);
    IFolder folder = SQLEditorHandlerOpenEditor.getCurrentScriptFolder(currentSelection);
    IFile scriptFile = SQLEditorUtils.createNewScript(project, folder, navigatorContext);
    FileEditorInput sqlInput = new FileEditorInput(scriptFile);
    SQLEditor editor = (SQLEditor) workbenchWindow.getActivePage().openEditor(sqlInput, SQLEditor.class.getName());
    if (editor != null) {
        editor.getDocument().set(sql);
        AbstractJob execJob = new AbstractJob("Execute SQL in console") {

            @Override
            protected IStatus run(DBRProgressMonitor monitor) {
                // So let's give it a chance and wait for 10 seconds
                for (int i = 0; i < 100; i++) {
                    if (editor.getExecutionContext() != null) {
                        break;
                    }
                    RuntimeUtils.pause(100);
                }
                return Status.OK_STATUS;
            }
        };
        if (doRun) {
            execJob.addJobChangeListener(new JobChangeAdapter() {

                @Override
                public void done(IJobChangeEvent event) {
                    UIUtils.syncExec(() -> editor.processSQL(false, false));
                }
            });
        }
        execJob.schedule();
    }
}
Also used : AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) SQLEditor(org.jkiss.dbeaver.ui.editors.sql.SQLEditor) IFile(org.eclipse.core.resources.IFile) FileEditorInput(org.eclipse.ui.part.FileEditorInput) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) DBPProject(org.jkiss.dbeaver.model.app.DBPProject) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) IFolder(org.eclipse.core.resources.IFolder)

Aggregations

SQLEditor (org.jkiss.dbeaver.ui.editors.sql.SQLEditor)23 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)5 IFile (org.eclipse.core.resources.IFile)4 IEditorPart (org.eclipse.ui.IEditorPart)4 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)4 TextSelection (org.eclipse.jface.text.TextSelection)3 ISelection (org.eclipse.jface.viewers.ISelection)3 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)3 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)3 SQLQueryTransformerCount (org.jkiss.dbeaver.model.impl.sql.SQLQueryTransformerCount)3 SQLQueryTransformerExpression (org.jkiss.dbeaver.model.impl.sql.SQLQueryTransformerExpression)3 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)2 JobChangeAdapter (org.eclipse.core.runtime.jobs.JobChangeAdapter)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 MenuItem (org.eclipse.swt.widgets.MenuItem)2 NotNull (org.jkiss.code.NotNull)2 DBException (org.jkiss.dbeaver.DBException)2