/*
 * DBeaver - Universal Database Manager
 * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jkiss.dbeaver.ui.editors.sql.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import org.jkiss.dbeaver.core.DBeaverUI;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.runtime.DBRRunnableWithResult;
import org.jkiss.dbeaver.model.struct.DBSEntity;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.dbeaver.ui.editors.sql.SQLEditor;
import org.jkiss.dbeaver.ui.editors.sql.generator.GenerateSQLContributor;
import org.jkiss.dbeaver.ui.navigator.NavigatorUtils;

import java.util.ArrayList;
import java.util.List;

public class OpenObjectConsoleHandler extends AbstractHandler {

    public OpenObjectConsoleHandler()
    {
    }

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException
    {
        IWorkbenchWindow workbenchWindow = HandlerUtil.getActiveWorkbenchWindow(event);
        DBPDataSourceContainer ds = null;

        List<DBSObject> selectedObjects = NavigatorUtils.getSelectedObjects(
            HandlerUtil.getCurrentSelection(event));
        List<DBSEntity> entities = new ArrayList<>();
        for (DBSObject object : selectedObjects) {
            if (object instanceof DBSEntity) {
                entities.add((DBSEntity) object);
                ds = object.getDataSource().getContainer();
            }
        }
        DBRRunnableWithResult<String> generator = GenerateSQLContributor.SELECT_GENERATOR(entities, true);
        DBeaverUI.runInUI(workbenchWindow, generator);
        String sql = generator.getResult();
        SQLEditor editor = OpenHandler.openSQLConsole(workbenchWindow, ds, "Query", sql);
        if (editor != null) {
            editor.processSQL(false, false);
        }
        return null;
    }

}

@@ -859,16 +859,206 @@
 eption;%0A
+import org.eclipse.core.runtime.IStatus;%0Aimport org.eclipse.core.runtime.Status;%0Aimport org.eclipse.core.runtime.jobs.IJobChangeEvent;%0Aimport org.eclipse.core.runtime.jobs.JobChangeAdapter;%0A
 import o
@@ -1089,16 +1089,16 @@
 Window;%0A
-
 import o
@@ -1225,24 +1225,135 @@
 eContainer;%0A
+import org.jkiss.dbeaver.model.runtime.AbstractJob;%0Aimport org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;%0A
 import org.j
@@ -1678,16 +1678,61 @@
 orUtils;
+%0Aimport org.jkiss.dbeaver.utils.RuntimeUtils;
 %0A%0Aimport
@@ -2876,38 +2876,1117 @@
     
-editor.processSQL(false, false
+AbstractJob execJob = new AbstractJob(%22Execute SQL in console%22) %7B%0A%0A                @Override%0A                protected IStatus run(DBRProgressMonitor monitor) %7B%0A                    // If we open new connection for each editor it may take some time%0A                    // So let's give it a chance and wait for 10 seconds%0A                    for (int i = 0; i %3C 100; i++) %7B%0A                        if (editor.getExecutionContext() != null) %7B%0A                            break;%0A                        %7D%0A                        RuntimeUtils.pause(100);%0A                    %7D%0A                    return Status.OK_STATUS;%0A                %7D%0A            %7D;%0A            execJob.addJobChangeListener(new JobChangeAdapter() %7B%0A                @Override%0A                public void done(IJobChangeEvent event) %7B%0A                    DBeaverUI.syncExec(new Runnable() %7B%0A                        @Override%0A                        public void run() %7B%0A                            editor.processSQL(false, false);%0A                        %7D%0A                    %7D);%0A                %7D%0A            %7D);%0A            execJob.schedule(
 );%0A 

Read data in console fix (wait for actual connect)