提交 9134d13d 编写于 作者: A Andrew Khitrin

140 Save load plan

上级 3d1bb9ba
......@@ -70,6 +70,8 @@ command.org.jkiss.dbeaver.ui.editors.sql.run.expression.name=Evaluate SQL expres
command.org.jkiss.dbeaver.ui.editors.sql.run.expression.description=Select value of selected SQL expression
command.org.jkiss.dbeaver.ui.editors.sql.run.explain.name=Explain Execution Plan
command.org.jkiss.dbeaver.ui.editors.sql.run.explain.description=Explain execution plan
command.org.jkiss.dbeaver.ui.editors.sql.load.plan.name=Load Execution Plan
command.org.jkiss.dbeaver.ui.editors.sql.load.plan.description=Load execution plan from file
command.org.jkiss.dbeaver.ui.editors.sql.query.next.name=Next query
command.org.jkiss.dbeaver.ui.editors.sql.query.next.description=Switch to the next query
command.org.jkiss.dbeaver.ui.editors.sql.query.prev.name=Previous query
......
......@@ -44,6 +44,8 @@ command.org.jkiss.dbeaver.ui.editors.sql.run.count.description = \u
command.org.jkiss.dbeaver.ui.editors.sql.run.count.name = \u041F\u043E\u043A\u0430\u0437\u0430\u0442\u044C \u0447\u0438\u0441\u043B\u043E \u0441\u0442\u0440\u043E\u043A
command.org.jkiss.dbeaver.ui.editors.sql.run.explain.description = \u041F\u043E\u043B\u0443\u0447\u0438\u0442\u044C \u043F\u043B\u0430\u043D \u0432\u044B\u043F\u043E\u043B\u043D\u0435\u043D\u0438\u044F
command.org.jkiss.dbeaver.ui.editors.sql.run.explain.name = \u041F\u043E\u043B\u0443\u0447\u0438\u0442\u044C \u043F\u043B\u0430\u043D \u0432\u044B\u043F\u043E\u043B\u043D\u0435\u043D\u0438\u044F
command.org.jkiss.dbeaver.ui.editors.sql.load.plan.name = \u0417\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044C \u043F\u043B\u0430\u043D
command.org.jkiss.dbeaver.ui.editors.sql.load.plan.description = \u0417\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044C \u043F\u043B\u0430\u043D \u0432\u044B\u043F\u043E\u043B\u043D\u0435\u043D\u0438\u044F \u0438\u0437 \u0444\u0430\u0439\u043B\u0430
command.org.jkiss.dbeaver.ui.editors.sql.run.expression.description = \u0412\u044B\u043F\u043E\u043B\u043D\u0438\u0442\u044C \u0442\u0435\u043A\u0443\u0449\u0435\u0435 SQL \u0432\u044B\u0440\u0430\u0436\u0435\u043D\u0438 \u0438 \u043F\u043E\u043A\u0430\u0437\u0430\u0442\u044C \u0440\u0435\u0437\u0443\u043B\u044C\u0442\u0430\u0442
command.org.jkiss.dbeaver.ui.editors.sql.run.expression.name = \u0412\u044B\u043F\u043E\u043B\u043D\u0438\u0442\u044C SQL \u0432\u044B\u0440\u0430\u0436\u0435\u043D\u0438\u0435
command.org.jkiss.dbeaver.ui.editors.sql.run.script.description = \u0412\u044B\u043F\u043E\u043B\u043D\u0438\u0442\u044C \u0441\u043A\u0440\u0438\u043F\u0442
......
......@@ -233,6 +233,7 @@
<command id="org.jkiss.dbeaver.ui.editors.sql.run.script" name="%command.org.jkiss.dbeaver.ui.editors.sql.run.script.name" description="%command.org.jkiss.dbeaver.ui.editors.sql.run.script.description" categoryId="org.jkiss.dbeaver.core.sql"/>
<command id="org.jkiss.dbeaver.ui.editors.sql.run.scriptNew" name="%command.org.jkiss.dbeaver.ui.editors.sql.run.scriptNew.name" description="%command.org.jkiss.dbeaver.ui.editors.sql.run.scriptNew.description" categoryId="org.jkiss.dbeaver.core.sql"/>
<command id="org.jkiss.dbeaver.ui.editors.sql.run.explain" name="%command.org.jkiss.dbeaver.ui.editors.sql.run.explain.name" description="%command.org.jkiss.dbeaver.ui.editors.sql.run.explain.description" categoryId="org.jkiss.dbeaver.core.sql"/>
<command id="org.jkiss.dbeaver.ui.editors.sql.load.plan" name="%command.org.jkiss.dbeaver.ui.editors.sql.load.plan.name" description="%command.org.jkiss.dbeaver.ui.editors.sql.load.plan.description" categoryId="org.jkiss.dbeaver.core.sql"/>
<command id="org.jkiss.dbeaver.ui.editors.sql.run.count" name="%command.org.jkiss.dbeaver.ui.editors.sql.run.count.name" description="%command.org.jkiss.dbeaver.ui.editors.sql.run.count.description" categoryId="org.jkiss.dbeaver.core.sql"/>
<command id="org.jkiss.dbeaver.ui.editors.sql.run.all.rows" name="%command.org.jkiss.dbeaver.ui.editors.sql.run.all.rows.name" description="%command.org.jkiss.dbeaver.ui.editors.sql.run.all.rows.description" categoryId="org.jkiss.dbeaver.core.sql"/>
<command id="org.jkiss.dbeaver.ui.editors.sql.run.expression" name="%command.org.jkiss.dbeaver.ui.editors.sql.run.expression.name" description="%command.org.jkiss.dbeaver.ui.editors.sql.run.expression.description" categoryId="org.jkiss.dbeaver.core.sql"/>
......@@ -273,6 +274,7 @@
<image commandId="org.jkiss.dbeaver.ui.editors.sql.run.script" icon="platform:/plugin/org.jkiss.dbeaver.ui/icons/sql/sql_script_exec.png"/>
<image commandId="org.jkiss.dbeaver.ui.editors.sql.run.scriptNew" icon="platform:/plugin/org.jkiss.dbeaver.ui/icons/sql/sql_script_exec_new.png"/>
<image commandId="org.jkiss.dbeaver.ui.editors.sql.run.explain" icon="platform:/plugin/org.jkiss.dbeaver.ui/icons/sql/sql_plan.png"/>
<image commandId="org.jkiss.dbeaver.ui.editors.sql.load.plan" icon="platform:/plugin/org.jkiss.dbeaver.ui/icons/file/load.png"/>
<image commandId="org.jkiss.dbeaver.ui.editors.sql.sync.connection" icon="platform:/plugin/org.jkiss.dbeaver.ui/icons/sync_connection.png"/>
<image commandId="org.jkiss.dbeaver.ui.editors.sql.export.data" icon="platform:/plugin/org.jkiss.dbeaver.ui/icons/sql/table_export.png"/>
......@@ -450,6 +452,15 @@
</with>
</enabledWhen>
</handler>
<handler commandId="org.jkiss.dbeaver.ui.editors.sql.load.plan" class="org.jkiss.dbeaver.ui.editors.sql.handlers.ExecuteHandler">
<enabledWhen>
<with variable="activeEditor">
<adapt type="org.jkiss.dbeaver.ui.editors.sql.SQLEditor">
<test property="org.jkiss.dbeaver.ui.editors.sql.canExplain"/>
</adapt>
</with>
</enabledWhen>
</handler>
<handler commandId="org.jkiss.dbeaver.ui.editors.sql.close.tab" class="org.jkiss.dbeaver.ui.editors.sql.handlers.CloseTabHandler">
<enabledWhen><reference definitionId="org.jkiss.dbeaver.core.ui.sql.editor"/></enabledWhen>
</handler>
......@@ -551,6 +562,7 @@
<key commandId="org.jkiss.dbeaver.ui.editors.sql.run.all.rows" contextId="org.jkiss.dbeaver.ui.editors.sql.script.focused" schemeId="org.eclipse.ui.defaultAcceleratorConfiguration" sequence="CTRL+ALT+SHIFT+A"/>
<key commandId="org.jkiss.dbeaver.ui.editors.sql.run.expression" contextId="org.jkiss.dbeaver.ui.editors.sql.script.focused" schemeId="org.eclipse.ui.defaultAcceleratorConfiguration" sequence="CTRL+ALT+'"/>
<key commandId="org.jkiss.dbeaver.ui.editors.sql.run.explain" contextId="org.jkiss.dbeaver.ui.editors.sql.script.focused" schemeId="org.eclipse.ui.defaultAcceleratorConfiguration" sequence="CTRL+SHIFT+E"/>
<key commandId="org.jkiss.dbeaver.ui.editors.sql.close.tab" contextId="org.jkiss.dbeaver.ui.editors.sql.script" schemeId="org.eclipse.ui.defaultAcceleratorConfiguration" sequence="CTRL+SHIFT+\"/>
<key commandId="org.jkiss.dbeaver.ui.editors.sql.navigate.object" contextId="org.jkiss.dbeaver.ui.editors.sql.script.focused" schemeId="org.eclipse.ui.defaultAcceleratorConfiguration" sequence="F4"/>
......@@ -647,6 +659,13 @@
</with>
</visibleWhen>
</command>
<command commandId="org.jkiss.dbeaver.ui.editors.sql.load.plan">
<visibleWhen>
<with variable="activeEditor">
<instanceof value="org.jkiss.dbeaver.ui.editors.sql.SQLEditor"/>
</with>
</visibleWhen>
</command>
<separator name="files" visible="true"/>
<command commandId="org.jkiss.dbeaver.ui.editors.sql.open.file">
<visibleWhen>
......@@ -681,8 +700,10 @@
<command commandId="org.jkiss.dbeaver.ui.editors.sql.run.count"/>
<command commandId="org.jkiss.dbeaver.ui.editors.sql.run.all.rows"/>
<command commandId="org.jkiss.dbeaver.ui.editors.sql.run.expression"/>
<command commandId="org.jkiss.dbeaver.ui.editors.sql.run.explain"/>
<command commandId="org.jkiss.dbeaver.ui.editors.sql.export.data"/>
<separator name="plans" visible="true"/>
<command commandId="org.jkiss.dbeaver.ui.editors.sql.run.explain"/>
<command commandId="org.jkiss.dbeaver.ui.editors.sql.load.plan"/>
<separator name="additions_active_object" visible="true"/>
<command commandId="org.jkiss.dbeaver.ui.tools.select.connection"/>
<command commandId="org.jkiss.dbeaver.ui.tools.select.schema"/>
......
......@@ -771,6 +771,7 @@ public class SQLEditor extends SQLEditorBase implements
VerticalButton.create(sideToolBar, SWT.LEFT | SWT.PUSH, getSite(), SQLEditorCommands.CMD_EXECUTE_SCRIPT, false);
VerticalButton.create(sideToolBar, SWT.LEFT | SWT.PUSH, getSite(), SQLEditorCommands.CMD_EXECUTE_SCRIPT_NEW, false);
VerticalButton.create(sideToolBar, SWT.LEFT | SWT.PUSH, getSite(), SQLEditorCommands.CMD_EXPLAIN_PLAN, false);
VerticalButton.create(sideToolBar, SWT.LEFT | SWT.PUSH, getSite(), SQLEditorCommands.CMD_LOAD_PLAN, false);
UIUtils.createEmptyLabel(sideToolBar, 1, 1).setLayoutData(new GridData(GridData.FILL_VERTICAL));
......@@ -1504,6 +1505,36 @@ public class SQLEditor extends SQLEditorBase implements
super.setFocus();
}
public void loadQueryPlan()
{
DBCQueryPlanner planner = GeneralUtils.adapt(getDataSource(), DBCQueryPlanner.class);
if (planner == null) {
DBWorkbench.getPlatformUI().showError("Execution plan", "Execution plan load isn't supported by current datasource");
return;
}
DBCPlanStyle planStyle = planner.getPlanStyle();
if (planStyle == DBCPlanStyle.QUERY) {
DBWorkbench.getPlatformUI().showError("Execution plan", "Execution plan load isn't supported by current datasource");
return;
}
ExplainPlanViewer planView = new ExplainPlanViewer(this, this, resultTabs);
final CTabItem item = new CTabItem(resultTabs, SWT.CLOSE);
item.setControl(planView.getControl());
item.setText(SQLEditorMessages.editors_sql_error_execution_plan_title);
item.setImage(IMG_EXPLAIN_PLAN);
item.setData(planView);
item.addDisposeListener(resultTabDisposeListener);
UIUtils.disposeControlOnItemDispose(item);
resultTabs.setSelection(item);
planView.loadQueryPlan(planner, planView);
}
public void explainQueryPlan()
{
// Notify listeners
......@@ -1523,6 +1554,8 @@ public class SQLEditor extends SQLEditorBase implements
return;
}
explainQueryPlan((SQLQuery) scriptElement);
}
private void explainQueryPlan(SQLQuery sqlQuery)
......
......@@ -29,6 +29,7 @@ public interface SQLEditorCommands
String CMD_EXECUTE_EXPRESSION = "org.jkiss.dbeaver.ui.editors.sql.run.expression"; //$NON-NLS-1$
String CMD_EXECUTE_ALL_ROWS = "org.jkiss.dbeaver.ui.editors.sql.run.all.rows"; //$NON-NLS-1$
String CMD_EXPLAIN_PLAN = "org.jkiss.dbeaver.ui.editors.sql.run.explain"; //$NON-NLS-1$
String CMD_LOAD_PLAN = "org.jkiss.dbeaver.ui.editors.sql.load.plan"; //$NON-NLS-1$
String CMD_SQL_EDITOR_OPEN = "org.jkiss.dbeaver.core.sql.editor.open";
String CMD_SQL_EDITOR_NEW = "org.jkiss.dbeaver.core.sql.editor.create";
String CMD_SQL_EDITOR_RECENT = "org.jkiss.dbeaver.core.sql.editor.recent";
......
......@@ -28,6 +28,7 @@ public interface SQLEditorFeatures {
DBRFeature SQL_EDITOR = DBRFeature.createCategory("SQL Editor", "SQL Editor features");
DBRFeature SQL_EDITOR_QUERY_PARAMS = DBRFeature.createFeature(SQL_EDITOR, "Use SQL query parameters");
DBRFeature SQL_EDITOR_EXPLAIN_PLAN = DBRFeature.createCommandFeature(SQL_EDITOR, SQLEditorCommands.CMD_EXPLAIN_PLAN);
DBRFeature SQL_EDITOR_LOAD_PLAN = DBRFeature.createCommandFeature(SQL_EDITOR, SQLEditorCommands.CMD_LOAD_PLAN);
DBRFeature SQL_EDITOR_EXECUTE_SCRIPT = DBRFeature.createFeature(SQL_EDITOR, "Execute SQL script");
DBRFeature SQL_EDITOR_EXECUTE_QUERY_NEW = DBRFeature.createCommandFeature(SQL_EDITOR, SQLEditorCommands.CMD_EXECUTE_STATEMENT_NEW);
DBRFeature SQL_EDITOR_EXECUTE_QUERY = DBRFeature.createCommandFeature(SQL_EDITOR, SQLEditorCommands.CMD_EXECUTE_STATEMENT);
......
package org.jkiss.dbeaver.ui.editors.sql;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.InvocationTargetException;
......@@ -23,11 +21,10 @@ import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.ui.DBeaverIcons;
import org.jkiss.dbeaver.ui.UIIcon;
import org.jkiss.dbeaver.ui.dialogs.DialogUtils;
import org.jkiss.dbeaver.ui.internal.UIMessages;
public abstract class SQLPlanSaveProvider implements SQLPlanViewProvider {
private static final String[] EXT = {"*.dbplan", "*"}; //$NON-NLS-1$ //$NON-NLS-2$;
public static final String[] EXT = {"*.dbplan", "*"}; //$NON-NLS-1$ //$NON-NLS-2$;
private static final String[] NAMES = {"DBeaver Plan File", "All files"}; //$NON-NLS-1$ //$NON-NLS-2$;
private Viewer viewer;
......@@ -35,9 +32,7 @@ public abstract class SQLPlanSaveProvider implements SQLPlanViewProvider {
private DBCPlan plan;
private SaveAction saveAction = new SaveAction("Save plan", DBeaverIcons.getImageDescriptor(UIIcon.SAVE_AS), this);
private LoadAction loadAction = new LoadAction("Load plan", DBeaverIcons.getImageDescriptor(UIIcon.LOAD), this);
private String curFolder;
private DBCQueryPlanner planner;
protected abstract void showPlan(Viewer viewer, SQLQuery query, DBCPlan plan);
......@@ -74,44 +69,6 @@ public abstract class SQLPlanSaveProvider implements SQLPlanViewProvider {
}
}
protected void doLoad() {
if (query != null) {
DBCQueryPlanner planner = DBUtils.getAdapter(DBCQueryPlanner.class, query.getDataSource());
if (planner instanceof DBCQueryPlannerSerializable) {
FileDialog fd = new FileDialog(viewer.getControl().getShell(), SWT.OPEN | SWT.SINGLE);
fd.setText(UIMessages.save_execution_plan);
fd.setFilterPath(curFolder);
fd.setFilterExtensions(EXT);
String selected = fd.open();
if (selected != null) {
curFolder = fd.getFilterPath();
try (Reader r = new FileReader(selected)) {
plan = ((DBCQueryPlannerSerializable) planner).deserialize(r);
showPlan(viewer, query, plan);
} catch (IOException | InvocationTargetException e) {
DBWorkbench.getPlatformUI().showError("Load plan", "Error loading plan", e);
}
}
} else {
loadAction.setEnabled(false);
}
}
}
protected void fillPlan(SQLQuery query, DBCPlan plan) {
this.query = query;
this.plan = plan;
......@@ -124,7 +81,6 @@ public abstract class SQLPlanSaveProvider implements SQLPlanViewProvider {
if (saveAction.isEnabled()) {
contributionManager.add(saveAction);
contributionManager.add(loadAction);
contributionManager.add(new Separator());
}
}
......@@ -148,25 +104,5 @@ public abstract class SQLPlanSaveProvider implements SQLPlanViewProvider {
provider.doSave();
}
}
class LoadAction extends Action {
SQLPlanSaveProvider provider;
public LoadAction(String text, ImageDescriptor image, SQLPlanSaveProvider provider) {
super(text, image);
this.provider = provider;
}
@Override
public boolean isEnabled() {
return planner instanceof DBCQueryPlannerSerializable;
}
@Override
public void run() {
provider.doLoad();
}
}
}
......@@ -67,6 +67,9 @@ public class ExecuteHandler extends AbstractHandler
case SQLEditorCommands.CMD_EXPLAIN_PLAN:
editor.explainQueryPlan();
break;
case SQLEditorCommands.CMD_LOAD_PLAN:
editor.loadQueryPlan();
break;
default:
log.error("Unsupported SQL editor command: " + actionId);
break;
......
......@@ -28,18 +28,19 @@ import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IWorkbenchPart;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBPContextProvider;
import org.jkiss.dbeaver.model.DBPDataSource;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.model.exec.DBCExecutionPurpose;
import org.jkiss.dbeaver.model.exec.DBCSession;
import org.jkiss.dbeaver.model.exec.DBExecUtils;
import org.jkiss.dbeaver.model.exec.plan.DBCPlan;
import org.jkiss.dbeaver.model.exec.plan.DBCQueryPlanner;
import org.jkiss.dbeaver.model.exec.plan.DBCQueryPlannerSerializable;
import org.jkiss.dbeaver.model.exec.plan.DBCSavedQueryPlanner;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.load.DatabaseLoadService;
......@@ -52,14 +53,19 @@ import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.controls.ProgressPageControl;
import org.jkiss.dbeaver.ui.controls.VerticalButton;
import org.jkiss.dbeaver.ui.controls.VerticalFolder;
import org.jkiss.dbeaver.ui.editors.sql.SQLPlanSaveProvider;
import org.jkiss.dbeaver.ui.editors.sql.SQLPlanViewProvider;
import org.jkiss.dbeaver.ui.editors.sql.internal.SQLEditorActivator;
import org.jkiss.dbeaver.ui.editors.sql.internal.SQLEditorMessages;
import org.jkiss.dbeaver.ui.editors.sql.plan.registry.SQLPlanViewDescriptor;
import org.jkiss.dbeaver.ui.editors.sql.plan.registry.SQLPlanViewRegistry;
import org.jkiss.dbeaver.ui.internal.UIMessages;
import org.jkiss.dbeaver.utils.GeneralUtils;
import org.jkiss.utils.CommonUtils;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.InvocationTargetException;
/**
......@@ -92,6 +98,8 @@ public class ExplainPlanViewer extends Viewer implements IAdaptable
private DBCPlan lastPlan;
private RefreshPlanAction refreshPlanAction;
private String curFolder;
public ExplainPlanViewer(final IWorkbenchPart workbenchPart, DBPContextProvider contextProvider, Composite parent)
{
......@@ -162,6 +170,40 @@ public class ExplainPlanViewer extends Viewer implements IAdaptable
public SQLQuery getQuery() {
return lastQuery;
}
public void loadQueryPlan(DBCQueryPlanner planner, Viewer viewer) {
if (planner instanceof DBCQueryPlannerSerializable) {
FileDialog fd = new FileDialog(viewer.getControl().getShell(), SWT.OPEN | SWT.SINGLE);
fd.setText(UIMessages.save_execution_plan);
fd.setFilterPath(curFolder);
fd.setFilterExtensions(SQLPlanSaveProvider.EXT);
String selected = fd.open();
if (selected != null) {
curFolder = fd.getFilterPath();
try (Reader r = new FileReader(selected)) {
lastPlan = ((DBCQueryPlannerSerializable) planner).deserialize(r);
lastQuery = new SQLQuery(contextProvider.getExecutionContext().getDataSource(), lastPlan.getQueryString());
lastQueryId = lastPlan.getQueryString();
refresh();
} catch (IOException | InvocationTargetException e) {
DBWorkbench.getPlatformUI().showError("Load plan", "Error loading plan ", (e.getCause() != null) ? e.getCause().getCause() : e);
}
}
}
}
public void explainQueryPlan(SQLQuery query, Object queryId) {
this.lastQuery = query;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册