diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/core/WorkbenchContextListener.java b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/core/WorkbenchContextListener.java index b6a00b3c572f986200a97d2549dcc20c3c48f7b7..36fc748250384f688967a61aff358694d5be4a50 100644 --- a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/core/WorkbenchContextListener.java +++ b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/core/WorkbenchContextListener.java @@ -33,6 +33,7 @@ import org.jkiss.dbeaver.ui.controls.resultset.ResultSetViewer; 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; @@ -222,6 +223,8 @@ class WorkbenchContextListener implements IWindowListener, IPageListener, IPartL } // 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); diff --git a/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/impl/AbstractExecutionContext.java b/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/impl/AbstractExecutionContext.java index 3039008b81b7333dc46dbb24245ae835581b0195..d51686af24fd115e41ebf5bd484b4abbd3dc1f7f 100644 --- a/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/impl/AbstractExecutionContext.java +++ b/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/impl/AbstractExecutionContext.java @@ -49,6 +49,8 @@ public abstract class AbstractExecutionContext this.dataSource = dataSource; this.purpose = purpose; this.id = generateContextId(); + + log.debug("Execution context opened (" + dataSource.getName() + ", " + this.id + ")"); } public static synchronized long generateContextId() { @@ -122,6 +124,8 @@ public abstract class AbstractExecutionContext protected void closeContext() { QMUtils.getDefaultHandler().handleContextClose(this); + + log.debug("Execution context closed (" + dataSource.getName() + ", " + this.id + ")"); } @Override diff --git a/plugins/org.jkiss.dbeaver.ui.editors.sql/OSGI-INF/l10n/bundle.properties b/plugins/org.jkiss.dbeaver.ui.editors.sql/OSGI-INF/l10n/bundle.properties index 711b9a1b14019f374e77f342cd81581306813ff6..cb19c92299dcc05cc3e65e8ee1a40064c9245d65 100644 --- a/plugins/org.jkiss.dbeaver.ui.editors.sql/OSGI-INF/l10n/bundle.properties +++ b/plugins/org.jkiss.dbeaver.ui.editors.sql/OSGI-INF/l10n/bundle.properties @@ -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 diff --git a/plugins/org.jkiss.dbeaver.ui.editors.sql/plugin.xml b/plugins/org.jkiss.dbeaver.ui.editors.sql/plugin.xml index 1d1ac962cb822c61c04dcfb8dc551ee685fda81c..8f029c64452623175a62616e8ed04ae22c80e5d4 100644 --- a/plugins/org.jkiss.dbeaver.ui.editors.sql/plugin.xml +++ b/plugins/org.jkiss.dbeaver.ui.editors.sql/plugin.xml @@ -246,6 +246,7 @@ + @@ -485,6 +486,7 @@ + @@ -598,7 +600,9 @@ + + @@ -648,8 +652,11 @@ + + + @@ -664,7 +671,9 @@ + + @@ -679,17 +688,22 @@ - + + - + + + + + diff --git a/plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/SQLEditor.java b/plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/SQLEditor.java index 5e647fc76add27ba236b1de557251dd2352ecea1..7a32b09ae2611d803aab4990b298731bf07fe280 100644 --- a/plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/SQLEditor.java +++ b/plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/SQLEditor.java @@ -391,7 +391,7 @@ public class SQLEditor extends SQLEditorBase implements } } - 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) { @@ -1985,6 +1985,18 @@ public class SQLEditor extends SQLEditorBase implements 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 { diff --git a/plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/SQLEditorCommands.java b/plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/SQLEditorCommands.java index dde58543e5d7fc4a400c6791b3fa7da865c10ba1..32300eef9349df184d7f76b306df009298d1f9b7 100644 --- a/plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/SQLEditorCommands.java +++ b/plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/SQLEditorCommands.java @@ -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"; } diff --git a/plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/handlers/SQLEditorHandlerSeparateConnectionOption.java b/plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/handlers/SQLEditorHandlerSeparateConnectionOption.java new file mode 100644 index 0000000000000000000000000000000000000000..7b8d08ecd1e8a26d3b75d28e0c8acce85eeaf4c8 --- /dev/null +++ b/plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/handlers/SQLEditorHandlerSeparateConnectionOption.java @@ -0,0 +1,82 @@ +/* + * DBeaver - Universal Database Manager + * Copyright (C) 2010-2019 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.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)); + } + +} diff --git a/plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/handlers/SQLEditorHandlerSyncConnectionAuto.java b/plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/handlers/SQLEditorHandlerSyncConnectionAuto.java index b39e431ee2294547d38864a5319c5d22861b77c3..0886fb07180dd5559007d3324cf0a7964b6380e9 100644 --- a/plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/handlers/SQLEditorHandlerSyncConnectionAuto.java +++ b/plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/handlers/SQLEditorHandlerSyncConnectionAuto.java @@ -25,6 +25,7 @@ import org.jkiss.dbeaver.model.preferences.DBPPreferenceStore; 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 { @@ -39,6 +40,13 @@ public class SQLEditorHandlerSyncConnectionAuto extends AbstractHandler implemen 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; } diff --git a/plugins/org.jkiss.dbeaver.ui.navigator/src/org/jkiss/dbeaver/ui/actions/AbstractDataSourceHandler.java b/plugins/org.jkiss.dbeaver.ui.navigator/src/org/jkiss/dbeaver/ui/actions/AbstractDataSourceHandler.java index a4c7598b57b97a7e37ab92a28f4078b6246d3ea1..d8240ed9bdb0c89c1eca244b583718b2d3844f93 100644 --- a/plugins/org.jkiss.dbeaver.ui.navigator/src/org/jkiss/dbeaver/ui/actions/AbstractDataSourceHandler.java +++ b/plugins/org.jkiss.dbeaver.ui.navigator/src/org/jkiss/dbeaver/ui/actions/AbstractDataSourceHandler.java @@ -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) { @@ -63,8 +62,7 @@ public abstract class AbstractDataSourceHandler extends AbstractHandler { return null; } - public static DBSObject getActiveObject(ExecutionEvent event) - { + public static DBSObject getActiveObject(ExecutionEvent event) { IWorkbenchPart activePart = HandlerUtil.getActivePart(event); IStructuredSelection navSelection = NavigatorUtils.getSelectionFromPart(activePart); @@ -78,16 +76,14 @@ public abstract class AbstractDataSourceHandler extends AbstractHandler { 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) { @@ -107,7 +103,7 @@ public abstract class AbstractDataSourceHandler extends AbstractHandler { 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(); @@ -116,8 +112,7 @@ public abstract class AbstractDataSourceHandler extends AbstractHandler { return null; } - public static DBPDataSourceContainer getDataSourceContainerFromPart(IWorkbenchPart activePart) - { + public static DBPDataSourceContainer getDataSourceContainerFromPart(IWorkbenchPart activePart) { if (activePart instanceof IDataSourceContainerProvider) { return ((IDataSourceContainerProvider) activePart).getDataSourceContainer(); }