Skip to content

Commit

Permalink
dbeaver#7822 Add "Open separate connection" option to the main menu. …
Browse files Browse the repository at this point in the history
…Extra logging.
  • Loading branch information
serge-rider committed Feb 10, 2020
1 parent 7977190 commit 6fed550
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.jkiss.dbeaver.ui.editors.entity.EntityEditor;
import org.jkiss.dbeaver.ui.editors.sql.SQLEditor;
import org.jkiss.dbeaver.ui.editors.sql.SQLEditorBase;
import org.jkiss.dbeaver.ui.editors.sql.SQLEditorCommands;
import org.jkiss.dbeaver.ui.editors.sql.SQLEditorContributions;
import org.jkiss.dbeaver.ui.navigator.INavigatorModelView;
import org.jkiss.dbeaver.ui.perspective.DBeaverPerspective;
Expand Down Expand Up @@ -222,6 +223,8 @@ void activatePartContexts(IWorkbenchPart part) {
}
// Refresh auto-commit element state (#3315)
ActionUtils.fireCommandRefresh(CoreCommands.CMD_TOGGLE_AUTOCOMMIT);
// Refresh OpenSeparateConnection
ActionUtils.fireCommandRefresh(SQLEditorCommands.CMD_TOGGLE_SEPARATE_CONNECTION);
}
finally {
contextService.deferUpdates(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public AbstractExecutionContext(@NotNull DATASOURCE dataSource, String purpose)
this.dataSource = dataSource;
this.purpose = purpose;
this.id = generateContextId();

log.debug("Execution context opened (" + dataSource.getName() + ", " + this.id + ")");
}

public static synchronized long generateContextId() {
Expand Down Expand Up @@ -122,6 +124,8 @@ protected void initContextBootstrap(@NotNull DBRProgressMonitor monitor, boolean
protected void closeContext()
{
QMUtils.getDefaultHandler().handleContextClose(this);

log.debug("Execution context closed (" + dataSource.getName() + ", " + this.id + ")");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ command.org.jkiss.dbeaver.ui.editors.sql.sync.connection.name=Set connection fro
command.org.jkiss.dbeaver.ui.editors.sql.sync.connection.description=Set active connection from database navigator selection
command.org.jkiss.dbeaver.ui.editors.sql.sync.auto.name=Auto-sync connection with navigator
command.org.jkiss.dbeaver.ui.editors.sql.sync.auto.description=Auto-sync active connection with database navigator selection
command.org.jkiss.dbeaver.ui.editors.sql.connection.separate.name=Open separate connection
command.org.jkiss.dbeaver.ui.editors.sql.connection.separate.description=Open separate connection
command.org.jkiss.dbeaver.ui.editors.sql.rename.name=Rename SQL Script
command.org.jkiss.dbeaver.ui.editors.sql.rename.description=Rename current SQL script
command.org.jkiss.dbeaver.ui.editors.sql.deleteThisScript.name=Delete this script
Expand Down
18 changes: 16 additions & 2 deletions plugins/org.jkiss.dbeaver.ui.editors.sql/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@
<command id="org.jkiss.dbeaver.ui.editors.sql.assist.templates" name="%command.org.jkiss.dbeaver.ui.editors.sql.assist.templates.name" description="%command.org.jkiss.dbeaver.ui.editors.sql.assist.templates.description" categoryId="org.jkiss.dbeaver.core.sql"/>
<command id="org.jkiss.dbeaver.ui.editors.sql.sync.connection" name="%command.org.jkiss.dbeaver.ui.editors.sql.sync.connection.name" description="%command.org.jkiss.dbeaver.ui.editors.sql.sync.connection.description" categoryId="org.jkiss.dbeaver.core.sql"/>
<command id="org.jkiss.dbeaver.ui.editors.sql.sync.auto" name="%command.org.jkiss.dbeaver.ui.editors.sql.sync.auto.name" description="%command.org.jkiss.dbeaver.ui.editors.sql.sync.auto.description" categoryId="org.jkiss.dbeaver.core.sql"/>
<command id="org.jkiss.dbeaver.ui.editors.sql.connection.separate" name="%command.org.jkiss.dbeaver.ui.editors.sql.connection.separate.name" description="%command.org.jkiss.dbeaver.ui.editors.sql.connection.separate.description" categoryId="org.jkiss.dbeaver.core.sql"/>
<command id="org.jkiss.dbeaver.ui.editors.sql.rename" name="%command.org.jkiss.dbeaver.ui.editors.sql.rename.name" description="%command.org.jkiss.dbeaver.ui.editors.sql.rename.description" categoryId="org.jkiss.dbeaver.core.sql"/>
<command id="org.jkiss.dbeaver.ui.editors.sql.deleteThisScript" name="%command.org.jkiss.dbeaver.ui.editors.sql.deleteThisScript.name" description="%command.org.jkiss.dbeaver.ui.editors.sql.deleteThisScript.description" categoryId="org.jkiss.dbeaver.core.sql"/>
<command id="org.jkiss.dbeaver.ui.editors.sql.close.tab" name="%command.org.jkiss.dbeaver.ui.editors.sql.close.tab.name" description="%command.org.jkiss.dbeaver.ui.editors.sql.close.tab.description" categoryId="org.jkiss.dbeaver.core.sql"/>
Expand Down Expand Up @@ -485,6 +486,7 @@
</enabledWhen>
</handler>
<handler commandId="org.jkiss.dbeaver.ui.editors.sql.sync.auto" class="org.jkiss.dbeaver.ui.editors.sql.handlers.SQLEditorHandlerSyncConnectionAuto"/>
<handler commandId="org.jkiss.dbeaver.ui.editors.sql.connection.separate" class="org.jkiss.dbeaver.ui.editors.sql.handlers.SQLEditorHandlerSeparateConnectionOption"/>
<handler commandId="org.jkiss.dbeaver.ui.editors.sql.export.data" class="org.jkiss.dbeaver.ui.editors.sql.handlers.SQLEditorHandlerExportData">
<enabledWhen>
<with variable="activeEditor">
Expand Down Expand Up @@ -598,7 +600,9 @@
<command commandId="org.jkiss.dbeaver.core.sql.editor.open"/>
<command commandId="org.jkiss.dbeaver.core.sql.editor.recent"/>
<command commandId="org.jkiss.dbeaver.core.sql.editor.create"/>

<separator name="execute" visible="true"/>

<command commandId="org.jkiss.dbeaver.ui.editors.sql.run.statement">
<visibleWhen>
<with variable="activeEditor">
Expand Down Expand Up @@ -648,8 +652,11 @@
</with>
</visibleWhen>
</command>

<separator name="query" visible="true"/>

<separator name="plan" visible="true"/>

<command commandId="org.jkiss.dbeaver.ui.editors.sql.run.explain">
<visibleWhen>
<with variable="activeEditor">
Expand All @@ -664,7 +671,9 @@
</with>
</visibleWhen>
</command>

<separator name="files" visible="true"/>

<command commandId="org.jkiss.dbeaver.ui.editors.sql.open.file">
<visibleWhen>
<with variable="activeEditor">
Expand All @@ -679,17 +688,22 @@
</with>
</visibleWhen>
</command>
<separator name="misc" visible="true"/>
<separator name="layout" visible="true"/>

<command commandId="org.jkiss.dbeaver.ui.editors.sql.toggle.result.panel"/>
<command commandId="org.jkiss.dbeaver.ui.editors.sql.maximize.result.panel" style="push"/>
<command commandId="org.jkiss.dbeaver.ui.editors.sql.switch.panel"/>

<separator name="sync" visible="true"/>
<separator name="context" visible="true"/>

<command commandId="org.jkiss.dbeaver.ui.tools.select.connection"><parameter name="noCustomLabel" value="true"/></command>
<command commandId="org.jkiss.dbeaver.ui.tools.select.schema"><parameter name="noCustomLabel" value="true"/></command>
<command commandId="org.jkiss.dbeaver.ui.editors.sql.sync.connection"/>

<separator name="options" visible="true"/>

<command commandId="org.jkiss.dbeaver.ui.editors.sql.sync.auto" style="toggle"/>
<command commandId="org.jkiss.dbeaver.ui.editors.sql.connection.separate" style="toggle"/>

</menu>
</menuContribution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ private void updateExecutionContext(Runnable onSuccess) {
}
}

private void initSeparateConnection(DBPDataSource dataSource, Runnable onSuccess) {
private void initSeparateConnection(@NotNull DBPDataSource dataSource, Runnable onSuccess) {
DBSInstance dsInstance = dataSource.getDefaultInstance();
String[] contextDefaults = EditorUtils.getInputContextDefaults(getEditorInput());
if (contextDefaults.length > 0 && contextDefaults[0] != null) {
Expand Down Expand Up @@ -1985,6 +1985,18 @@ public boolean transformQueryWithParameters(SQLQuery query) {
return createScriptContext().fillQueryParameters(query, false);
}

// Called on OPEN_SEPARATE_CONNECTION optio nchange
public void updateExecutionContextState() {
// Save current datasource (we want to keep it here)
DBPDataSource dataSource = curDataSource;
releaseExecutionContext();
// Restore cur data source (as it is reset in releaseExecutionContext)
curDataSource = dataSource;
if (dataSource != null && SQLEditorUtils.isOpenSeparateConnection(dataSource.getContainer())) {
initSeparateConnection(dataSource, null);
}
}

private boolean checkSession(DBRProgressListener onFinish)
throws DBException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ public interface SQLEditorCommands

String CMD_NAVIGATE_OBJECT = "org.jkiss.dbeaver.ui.editors.sql.navigate.object";
String CMD_TOGGLE_LAYOUT = "org.jkiss.dbeaver.ui.editors.sql.toggleLayout";
String CMD_TOGGLE_SEPARATE_CONNECTION = "org.jkiss.dbeaver.ui.editors.sql.connection.separate";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2019 Serge Rider ([email protected])
*
* 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.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.commands.IElementUpdater;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.menus.UIElement;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.preferences.DBPPreferenceStore;
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.actions.AbstractDataSourceHandler;
import org.jkiss.dbeaver.ui.editors.sql.SQLEditor;
import org.jkiss.dbeaver.ui.editors.sql.SQLPreferenceConstants;
import org.jkiss.dbeaver.ui.editors.sql.internal.SQLEditorMessages;

import java.io.IOException;
import java.util.Map;

public class SQLEditorHandlerSeparateConnectionOption extends AbstractDataSourceHandler implements IElementUpdater {

public SQLEditorHandlerSeparateConnectionOption()
{
}

@Override
public Object execute(ExecutionEvent event) throws ExecutionException
{
final DBPPreferenceStore prefs = getPreferenceStore(event);
prefs.setValue(SQLPreferenceConstants.EDITOR_SEPARATE_CONNECTION,
!prefs.getBoolean(SQLPreferenceConstants.EDITOR_SEPARATE_CONNECTION));
try {
prefs.save();
} catch (IOException e) {
throw new ExecutionException("Error saving configuration", e);
}

IEditorPart editor = HandlerUtil.getActiveEditor(event);
if (editor instanceof SQLEditor) {
((SQLEditor) editor).updateExecutionContextState();
}

return null;
}

@NotNull
private DBPPreferenceStore getPreferenceStore(ExecutionEvent event) {
DBPDataSourceContainer dsContainer = getActiveDataSourceContainer(event, false);
return dsContainer == null ? DBWorkbench.getPlatform().getPreferenceStore() : dsContainer.getPreferenceStore();
}

@Override
public void updateElement(UIElement element, Map parameters) {
element.setText(SQLEditorMessages.pref_page_sql_editor_label_separate_connection_each_editor);
element.setTooltip(SQLEditorMessages.pref_page_sql_editor_label_separate_connection_each_editor);

IEditorPart activeEditor = UIUtils.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
DBPDataSourceContainer dsContainer = activeEditor == null ? null : getDataSourceContainerFromPart(activeEditor);
DBPPreferenceStore prefStore = dsContainer == null ? DBWorkbench.getPlatform().getPreferenceStore() : dsContainer.getPreferenceStore();

element.setChecked(prefStore.getBoolean(SQLPreferenceConstants.EDITOR_SEPARATE_CONNECTION));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.ui.navigator.NavigatorPreferences;

import java.io.IOException;
import java.util.Map;

public class SQLEditorHandlerSyncConnectionAuto extends AbstractHandler implements IElementUpdater {
Expand All @@ -39,6 +40,13 @@ public Object execute(ExecutionEvent event) throws ExecutionException
final DBPPreferenceStore prefs = DBWorkbench.getPlatform().getPreferenceStore();
prefs.setValue(NavigatorPreferences.NAVIGATOR_SYNC_EDITOR_DATASOURCE,
!prefs.getBoolean(NavigatorPreferences.NAVIGATOR_SYNC_EDITOR_DATASOURCE));

try {
prefs.save();
} catch (IOException e) {
throw new ExecutionException("Error saving configuration", e);
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public abstract class AbstractDataSourceHandler extends AbstractHandler {
/**
* Get execution context from active editor or active selection
*/
public static DBCExecutionContext getActiveExecutionContext(ExecutionEvent event, boolean useEditor)
{
public static DBCExecutionContext getActiveExecutionContext(ExecutionEvent event, boolean useEditor) {
if (useEditor) {
IEditorPart editor = HandlerUtil.getActiveEditor(event);
if (editor instanceof DBPContextProvider) {
Expand All @@ -63,8 +62,7 @@ public static DBCExecutionContext getActiveExecutionContext(ExecutionEvent event
return null;
}

public static DBSObject getActiveObject(ExecutionEvent event)
{
public static DBSObject getActiveObject(ExecutionEvent event) {
IWorkbenchPart activePart = HandlerUtil.getActivePart(event);

IStructuredSelection navSelection = NavigatorUtils.getSelectionFromPart(activePart);
Expand All @@ -78,16 +76,14 @@ public static DBSObject getActiveObject(ExecutionEvent event)
return null;
}

public static DBCExecutionContext getExecutionContextFromPart(IWorkbenchPart activePart)
{
public static DBCExecutionContext getExecutionContextFromPart(IWorkbenchPart activePart) {
if (activePart instanceof DBPContextProvider) {
return ((DBPContextProvider) activePart).getExecutionContext();
}
return null;
}

public static DBPDataSourceContainer getActiveDataSourceContainer(ExecutionEvent event, boolean useEditor)
{
public static DBPDataSourceContainer getActiveDataSourceContainer(ExecutionEvent event, boolean useEditor) {
if (useEditor) {
IEditorPart editor = HandlerUtil.getActiveEditor(event);
if (editor != null) {
Expand All @@ -107,7 +103,7 @@ public static DBPDataSourceContainer getActiveDataSourceContainer(ExecutionEvent

DBSObject selectedObject = NavigatorUtils.getSelectedObject(selection);
if (selectedObject instanceof DBPDataSourceContainer) {
return (DBPDataSourceContainer)selectedObject;
return (DBPDataSourceContainer) selectedObject;
} else if (selectedObject != null) {
DBPDataSource dataSource = selectedObject.getDataSource();
return dataSource == null ? null : dataSource.getContainer();
Expand All @@ -116,8 +112,7 @@ public static DBPDataSourceContainer getActiveDataSourceContainer(ExecutionEvent
return null;
}

public static DBPDataSourceContainer getDataSourceContainerFromPart(IWorkbenchPart activePart)
{
public static DBPDataSourceContainer getDataSourceContainerFromPart(IWorkbenchPart activePart) {
if (activePart instanceof IDataSourceContainerProvider) {
return ((IDataSourceContainerProvider) activePart).getDataSourceContainer();
}
Expand Down

0 comments on commit 6fed550

Please sign in to comment.