提交 b101b82c 编写于 作者: S serge-rider

Debug API refactoring

上级 6bca96e4
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<?eclipse version="3.4"?> <?eclipse version="3.4"?>
<plugin> <plugin>
<extension-point id="procedureControllers" name="%procedureControllers.name" schema="schema/procedureControllers.exsd"/> <extension-point id="org.jkiss.dbeaver.debug.core.controllers" name="%procedureControllers.name" schema="schema/procedureControllers.exsd"/>
<extension <extension
id="databaseBreakpointMarker" id="databaseBreakpointMarker"
......
...@@ -17,98 +17,61 @@ ...@@ -17,98 +17,61 @@
*/ */
package org.jkiss.dbeaver.debug; package org.jkiss.dbeaver.debug;
import java.util.Map;
import org.eclipse.osgi.util.NLS; import org.eclipse.osgi.util.NLS;
import org.jkiss.dbeaver.DBException; import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log; import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.debug.internal.DebugMessages; import org.jkiss.dbeaver.debug.internal.DebugMessages;
import org.jkiss.dbeaver.model.DBPDataSource; import org.jkiss.dbeaver.model.DBPDataSource;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext; import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.exec.DBCExecutionPurpose;
import org.jkiss.dbeaver.model.exec.DBCSession;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.registry.DataSourceDescriptor;
public abstract class DBGBaseController<SID_TYPE, OID_TYPE> implements DBGController { public abstract class DBGBaseController implements DBGController {
private static final Log log = Log.getLog(DBGBaseController.class); private static final Log log = Log.getLog(DBGBaseController.class);
private DataSourceDescriptor dataSourceDescriptor; private DBPDataSourceContainer dataSourceContainer;
private DBCExecutionContext debugContext;
private DBCSession debugSession;
public DBGBaseController() { public DBGBaseController(DBPDataSourceContainer dataSourceContainer) {
this.dataSourceContainer = dataSourceContainer;
} }
@Override public DBPDataSourceContainer getDataSourceContainer() {
public void init(DataSourceDescriptor dataSourceDescriptor, String databaseName, Map<String, Object> attributes) { return dataSourceContainer;
this.dataSourceDescriptor = dataSourceDescriptor;
} }
@Override @Override
public void connect(DBRProgressMonitor monitor) throws DBGException { public DBGSession connect(DBRProgressMonitor monitor) throws DBGException {
DBPDataSource dataSource = dataSourceDescriptor.getDataSource(); DBPDataSource dataSource = dataSourceContainer.getDataSource();
if (!dataSourceDescriptor.isConnected()) { if (!dataSourceContainer.isConnected()) {
throw new DBGException("Not connected to database");
try {
// FIXME: AF: the contract of this call is not clear, we need
// some utility for this
dataSourceDescriptor.connect(monitor, true, true);
} catch (DBException e) {
String message = NLS.bind(DebugMessages.DatabaseDebugController_e_connecting_datasource,
dataSourceDescriptor);
log.error(message, e);
throw new DBGException(message, e);
}
} }
try { try {
this.debugContext = dataSource.openIsolatedContext(monitor, return createSession(monitor, dataSource);
DebugMessages.DatabaseDebugController_debug_context_purpose);
this.debugSession = debugContext.openSession(monitor, DBCExecutionPurpose.UTIL,
DebugMessages.DatabaseDebugController_debug_session_name);
afterSessionOpen(debugSession);
} catch (DBException e) { } catch (DBException e) {
String message = NLS.bind(DebugMessages.DatabaseDebugController_e_opening_debug_context, String message = NLS.bind(DebugMessages.DatabaseDebugController_e_opening_debug_context,
dataSourceDescriptor); dataSourceContainer);
log.error(message, e); log.error(message, e);
throw new DBGException(message, e); throw new DBGException(message, e);
} }
} }
protected void afterSessionOpen(DBCSession session) throws DBGException { protected abstract DBGSession createSession(DBRProgressMonitor monitor, DBPDataSource dataSource) throws DBGException;
//do nothing by default
}
protected void beforeSessionClose(DBCSession session) throws DBGException {
//do nothing by default
}
@Override @Override
public void resume() throws DBGException { public void resume(DBRProgressMonitor monitor, DBGSession session) throws DBGException {
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }
@Override @Override
public void suspend() throws DBGException { public void suspend(DBRProgressMonitor monitor, DBGSession session) throws DBGException {
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }
@Override @Override
public void terminate() throws DBGException { public void terminate(DBRProgressMonitor monitor, DBGSession session) throws DBGException {
beforeSessionClose(this.debugSession);
if (this.debugSession != null) {
this.debugSession.close();
this.debugSession = null;
}
if (this.debugContext != null) {
this.debugContext.close();
this.debugContext = null;
}
} }
@Override @Override
......
...@@ -27,15 +27,13 @@ import org.jkiss.dbeaver.registry.DataSourceDescriptor; ...@@ -27,15 +27,13 @@ import org.jkiss.dbeaver.registry.DataSourceDescriptor;
*/ */
public interface DBGController { public interface DBGController {
void init(DataSourceDescriptor dataSourceDescriptor, String databaseName, Map<String, Object> attributes); DBGSession connect(DBRProgressMonitor monitor) throws DBGException;
void connect(DBRProgressMonitor monitor) throws DBGException; void resume(DBRProgressMonitor monitor, DBGSession session) throws DBGException;
void resume() throws DBGException; void suspend(DBRProgressMonitor monitor, DBGSession session) throws DBGException;
void suspend() throws DBGException; void terminate(DBRProgressMonitor monitor, DBGSession session) throws DBGException;
void terminate() throws DBGException;
void dispose() throws DBGException; void dispose() throws DBGException;
......
package org.jkiss.dbeaver.debug; package org.jkiss.dbeaver.debug;
public interface DBGControllerRegistry<C extends DBGController> { import org.jkiss.dbeaver.model.DBPDataSourceContainer;
C createController(String dataTypeProviderId); public interface DBGControllerRegistry {
DBGController createController(DBPDataSourceContainer dataSource) throws DBGException;
} }
...@@ -18,8 +18,11 @@ ...@@ -18,8 +18,11 @@
package org.jkiss.dbeaver.debug; package org.jkiss.dbeaver.debug;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.DBPDataSource;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class DBGException extends Exception { public class DBGException extends DBException {
public DBGException(String message, Throwable e) { public DBGException(String message, Throwable e) {
super(message, e); super(message, e);
...@@ -29,8 +32,11 @@ public class DBGException extends Exception { ...@@ -29,8 +32,11 @@ public class DBGException extends Exception {
super(message); super(message);
} }
public DBGException(Throwable e) { public DBGException(Throwable cause, DBPDataSource dataSource) {
super(e); super(cause, dataSource);
} }
public DBGException(String message, Throwable cause, DBPDataSource dataSource) {
super(message, cause, dataSource);
}
} }
package org.jkiss.dbeaver.debug;
import org.jkiss.dbeaver.model.exec.DBCSession;
public abstract class DBGProcedureController<SID_TYPE, OID_TYPE> extends DBGBaseController<SID_TYPE, OID_TYPE> {
private SID_TYPE sessionId;
private DBGSessionManager<SID_TYPE, OID_TYPE> dbgSessionManager;
private DBGSession<? extends DBGSessionInfo<SID_TYPE>, ? extends DBGObject<OID_TYPE>, SID_TYPE, OID_TYPE> dbgSession;
public DBGProcedureController() {
super();
}
@Override
protected void afterSessionOpen(DBCSession session) throws DBGException {
super.afterSessionOpen(session);
this.dbgSessionManager = initSessionManager(session);
if (this.dbgSessionManager == null) {
throw new DBGException("Failed to initialize Debug Session Manager");
}
this.dbgSession = createSession(session, dbgSessionManager);
if (this.dbgSession == null) {
throw new DBGException("Failed to initialize Debug Session");
}
setSessionId(dbgSession.getSessionId());
}
protected abstract DBGSessionManager<SID_TYPE, OID_TYPE> initSessionManager(DBCSession session) throws DBGException;
protected abstract DBGSession<? extends DBGSessionInfo<SID_TYPE>, ? extends DBGObject<OID_TYPE>, SID_TYPE, OID_TYPE> createSession(
DBCSession session, DBGSessionManager<SID_TYPE, OID_TYPE> sessionManager) throws DBGException;
@Override
protected void beforeSessionClose(DBCSession session) throws DBGException {
if (this.dbgSessionManager != null) {
this.dbgSessionManager.terminateSession(getSessionId());
this.dbgSessionManager.dispose();
}
this.dbgSessionManager = null;
super.beforeSessionClose(session);
}
public SID_TYPE getSessionId() {
return sessionId;
}
public void setSessionId(SID_TYPE sessionId) {
this.sessionId = sessionId;
}
}
...@@ -18,11 +18,13 @@ ...@@ -18,11 +18,13 @@
package org.jkiss.dbeaver.debug; package org.jkiss.dbeaver.debug;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import java.sql.Connection; import java.sql.Connection;
import java.util.List; import java.util.List;
public interface DBGSessionManager<SESSION_ID_TYPE, OBJECT_ID_TYPE> { public interface DBGSessionManager<SESSION_ID_TYPE, OBJECT_ID_TYPE> {
DBGSessionInfo<SESSION_ID_TYPE> getSessionInfo(Connection connection) throws DBGException; DBGSessionInfo<SESSION_ID_TYPE> getSessionInfo(DBCExecutionContext connection) throws DBGException;
List<? extends DBGSessionInfo<SESSION_ID_TYPE>> getSessions() throws DBGException; List<? extends DBGSessionInfo<SESSION_ID_TYPE>> getSessions() throws DBGException;
...@@ -32,7 +34,7 @@ public interface DBGSessionManager<SESSION_ID_TYPE, OBJECT_ID_TYPE> { ...@@ -32,7 +34,7 @@ public interface DBGSessionManager<SESSION_ID_TYPE, OBJECT_ID_TYPE> {
void terminateSession(SESSION_ID_TYPE id); void terminateSession(SESSION_ID_TYPE id);
DBGSession<? extends DBGSessionInfo<SESSION_ID_TYPE>, ? extends DBGObject<OBJECT_ID_TYPE>, SESSION_ID_TYPE, OBJECT_ID_TYPE> createDebugSession(Connection connection) throws DBGException; DBGSession<? extends DBGSessionInfo<SESSION_ID_TYPE>, ? extends DBGObject<OBJECT_ID_TYPE>, SESSION_ID_TYPE, OBJECT_ID_TYPE> createDebugSession(DBCExecutionContext connection) throws DBGException;
boolean isSessionExists(SESSION_ID_TYPE id); boolean isSessionExists(SESSION_ID_TYPE id);
......
...@@ -30,11 +30,13 @@ import org.jkiss.dbeaver.debug.DBGController; ...@@ -30,11 +30,13 @@ import org.jkiss.dbeaver.debug.DBGController;
import org.jkiss.dbeaver.debug.DBGException; import org.jkiss.dbeaver.debug.DBGException;
import org.jkiss.dbeaver.debug.core.model.DatabaseDebugTarget; import org.jkiss.dbeaver.debug.core.model.DatabaseDebugTarget;
import org.jkiss.dbeaver.debug.core.model.DatabaseProcess; import org.jkiss.dbeaver.debug.core.model.DatabaseProcess;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.runtime.DefaultProgressMonitor; import org.jkiss.dbeaver.model.runtime.DefaultProgressMonitor;
import org.jkiss.dbeaver.registry.DataSourceDescriptor; import org.jkiss.dbeaver.registry.DataSourceDescriptor;
import org.jkiss.dbeaver.registry.DataSourceRegistry; import org.jkiss.dbeaver.registry.DataSourceRegistry;
import org.jkiss.dbeaver.utils.GeneralUtils;
public abstract class DatabaseLaunchDelegate<C extends DBGController> extends LaunchConfigurationDelegate { public abstract class DatabaseLaunchDelegate extends LaunchConfigurationDelegate {
@Override @Override
public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor)
...@@ -47,16 +49,15 @@ public abstract class DatabaseLaunchDelegate<C extends DBGController> extends La ...@@ -47,16 +49,15 @@ public abstract class DatabaseLaunchDelegate<C extends DBGController> extends La
} }
String databaseName = DebugCore.extractDatabaseName(configuration); String databaseName = DebugCore.extractDatabaseName(configuration);
Map<String, Object> attributes = extractAttributes(configuration); Map<String, Object> attributes = extractAttributes(configuration);
C controller = createController(datasourceDescriptor, databaseName, attributes); DBGController controller = createController(datasourceDescriptor, databaseName, attributes);
DatabaseProcess process = createProcess(launch, configuration.getName()); DatabaseProcess process = createProcess(launch, configuration.getName());
DatabaseDebugTarget<C> target = createDebugTarget(launch, controller, process); DatabaseDebugTarget<DBGController> target = createDebugTarget(launch, controller, process);
launch.addDebugTarget(target); launch.addDebugTarget(target);
DefaultProgressMonitor progress = new DefaultProgressMonitor(monitor); DefaultProgressMonitor progress = new DefaultProgressMonitor(monitor);
try { try {
controller.connect(progress); controller.connect(progress);
} catch (DBGException e) { } catch (DBGException e) {
String message = NLS.bind("Unable to connect to {0}", datasourceDescriptor.getName()); throw new CoreException(GeneralUtils.makeExceptionStatus("Error connecting debug controller", e));
throw new CoreException(DebugCore.newErrorStatus(message));
} }
} }
...@@ -65,10 +66,10 @@ public abstract class DatabaseLaunchDelegate<C extends DBGController> extends La ...@@ -65,10 +66,10 @@ public abstract class DatabaseLaunchDelegate<C extends DBGController> extends La
return attributes; return attributes;
} }
protected abstract C createController(DataSourceDescriptor datasourceDescriptor, String databaseName, Map<String, Object> attributes) throws CoreException; protected abstract DBGController createController(DBPDataSourceContainer dataSourceContainer, String databaseName, Map<String, Object> attributes) throws CoreException;
protected abstract DatabaseProcess createProcess(ILaunch launch, String name); protected abstract DatabaseProcess createProcess(ILaunch launch, String name);
protected abstract DatabaseDebugTarget<C> createDebugTarget(ILaunch launch, C controller, DatabaseProcess process); protected abstract DatabaseDebugTarget<DBGController> createDebugTarget(ILaunch launch, DBGController controller, DatabaseProcess process);
} }
/*
* 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.debug.core;
import org.eclipse.core.runtime.IConfigurationElement;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.debug.DBGControllerRegistry;
import org.jkiss.dbeaver.model.DBPDataKind;
import org.jkiss.dbeaver.model.DBPDataSource;
import org.jkiss.dbeaver.model.impl.AbstractDescriptor;
import org.jkiss.dbeaver.model.struct.DBSDataType;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import org.jkiss.dbeaver.model.struct.DBSTypedObjectEx;
import org.jkiss.dbeaver.registry.RegistryConstants;
import org.jkiss.dbeaver.registry.driver.DriverDescriptor;
import org.jkiss.dbeaver.ui.data.IValueManager;
import org.jkiss.utils.CommonUtils;
/**
* ValueManagerDescriptor
*/
public class DebugControllerDescriptor extends AbstractDescriptor
{
private static final Log log = Log.getLog(DebugControllerDescriptor.class);
public static final String EXTENSION_ID = "org.jkiss.dbeaver.debug.core.controllers"; //$NON-NLS-1$
public static final String TAG_CONTROLLER = "controller"; //$NON-NLS-1$
public static final String ATTR_DATASOURCE = "datasource"; //$NON-NLS-1$
private String dataSourceID;
private ObjectType implType;
private DBGControllerRegistry isntance;
public DebugControllerDescriptor(IConfigurationElement config)
{
super(config);
this.dataSourceID = config.getAttribute(ATTR_DATASOURCE);
this.implType = new ObjectType(config.getAttribute(RegistryConstants.ATTR_CLASS));
}
public String getDataSourceID()
{
return dataSourceID;
}
@NotNull
public DBGControllerRegistry getInstance()
{
if (isntance == null ) {
try {
isntance = implType.createInstance(DBGControllerRegistry.class);
} catch (Exception e) {
throw new IllegalStateException("Can't instantiate debug controller '" + this.dataSourceID + "'", e); //$NON-NLS-1$
}
}
return isntance;
}
@Override
public String toString() {
return dataSourceID;
}
}
\ No newline at end of file
/*
* 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.debug.core;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.debug.DBGControllerRegistry;
import org.jkiss.dbeaver.model.DBPDataSource;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.data.DBDContent;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import org.jkiss.dbeaver.ui.data.IStreamValueManager;
import org.jkiss.dbeaver.ui.data.IValueManager;
import org.jkiss.dbeaver.ui.data.managers.DefaultValueManager;
import org.jkiss.dbeaver.ui.data.registry.StreamValueManagerDescriptor;
import org.jkiss.dbeaver.ui.data.registry.ValueManagerDescriptor;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* EntityEditorsRegistry
*/
public class DebugControllersRegistry {
private static DebugControllersRegistry instance = null;
public synchronized static DebugControllersRegistry getInstance() {
if (instance == null) {
instance = new DebugControllersRegistry(Platform.getExtensionRegistry());
}
return instance;
}
private List<DebugControllerDescriptor> controllers = new ArrayList<>();
private DebugControllersRegistry(IExtensionRegistry registry) {
// Load datasource providers from external plugins
IConfigurationElement[] extElements = registry.getConfigurationElementsFor(DebugControllerDescriptor.EXTENSION_ID);
for (IConfigurationElement ext : extElements) {
if (DebugControllerDescriptor.TAG_CONTROLLER.equals(ext.getName())) {
controllers.add(new DebugControllerDescriptor(ext));
}
}
}
@NotNull
public DBGControllerRegistry createControllerRegistry(@NotNull DBPDataSourceContainer dataSource) {
for (DebugControllerDescriptor controllerDescriptor : controllers) {
if (controllerDescriptor.getDataSourceID().equals(dataSource.getDriver().getProviderId())) {
return controllerDescriptor.getInstance();
}
}
return null;
}
}
...@@ -17,9 +17,6 @@ ...@@ -17,9 +17,6 @@
*/ */
package org.jkiss.dbeaver.debug.core; package org.jkiss.dbeaver.debug.core;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.Adapters; import org.eclipse.core.runtime.Adapters;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.IStatus;
...@@ -27,24 +24,21 @@ import org.eclipse.core.runtime.Status; ...@@ -27,24 +24,21 @@ import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.ILaunchConfiguration; import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.osgi.util.NLS; import org.eclipse.osgi.util.NLS;
import org.jkiss.dbeaver.Log; import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.debug.DBGController;
import org.jkiss.dbeaver.debug.DBGControllerRegistry; import org.jkiss.dbeaver.debug.DBGControllerRegistry;
import org.jkiss.dbeaver.debug.DBGProcedureController; import org.jkiss.dbeaver.debug.DBGException;
import org.jkiss.dbeaver.debug.internal.core.DebugCoreActivator; import org.jkiss.dbeaver.debug.internal.core.DebugCoreActivator;
import org.jkiss.dbeaver.debug.internal.core.DebugCoreMessages; import org.jkiss.dbeaver.debug.internal.core.DebugCoreMessages;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.struct.DBSObject; import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.dbeaver.registry.DataSourceDescriptor;
import org.jkiss.dbeaver.registry.DataSourceProviderDescriptor; import java.util.ArrayList;
import org.jkiss.dbeaver.registry.driver.DriverDescriptor; import java.util.List;
public class DebugCore { public class DebugCore {
public static final String BUNDLE_SYMBOLIC_NAME = "org.jkiss.dbeaver.debug.core"; //$NON-NLS-1$ public static final String BUNDLE_SYMBOLIC_NAME = "org.jkiss.dbeaver.debug.core"; //$NON-NLS-1$
public static final String EP_PROCEDURE_CONTROLLERS_ID = "procedureControllers"; //$NON-NLS-1$
public static final String EP_PROCEDURE_CONTROLLERS_CONTROLLER = "controller"; //$NON-NLS-1$
public static final String EP_PROCEDURE_CONTROLLERS_CONTROLLER_PROVIDER_ID = "providerId"; //$NON-NLS-1$
public static final String EP_PROCEDURE_CONTROLLERS_CONTROLLER_CLASS = "class"; //$NON-NLS-1$
// FIXME: AF: revisit, looks like we can live without it // FIXME: AF: revisit, looks like we can live without it
public static final String MODEL_IDENTIFIER_DATABASE = BUNDLE_SYMBOLIC_NAME + '.' + "database"; //$NON-NLS-1$ public static final String MODEL_IDENTIFIER_DATABASE = BUNDLE_SYMBOLIC_NAME + '.' + "database"; //$NON-NLS-1$
public static final String MODEL_IDENTIFIER_PROCEDURE = BUNDLE_SYMBOLIC_NAME + '.' + "procedure"; //$NON-NLS-1$ public static final String MODEL_IDENTIFIER_PROCEDURE = BUNDLE_SYMBOLIC_NAME + '.' + "procedure"; //$NON-NLS-1$
...@@ -136,29 +130,16 @@ public class DebugCore { ...@@ -136,29 +130,16 @@ public class DebugCore {
return newErrorStatus(message, null); return newErrorStatus(message, null);
} }
public static String extractProviderId(DataSourceDescriptor datasourceDescriptor) { public static DBGControllerRegistry getProcedureControllerRegistry(DBPDataSourceContainer dataSourceContainer) {
if (datasourceDescriptor == null) { return DebugControllersRegistry.getInstance().createControllerRegistry(dataSourceContainer);
return null;
}
DriverDescriptor driverDescriptor = datasourceDescriptor.getDriver();
if (driverDescriptor == null) {
return null;
}
DataSourceProviderDescriptor providerDescriptor = driverDescriptor.getProviderDescriptor();
if (providerDescriptor == null) {
return null;
}
return providerDescriptor.getId();
} }
public static DBGControllerRegistry<DBGProcedureController> getProcedureControllerRegistry() { public static DBGController findProcedureController(DBPDataSourceContainer dataSourceContainer) throws DBGException {
return DebugCoreActivator.getDefault().getProcedureControllerRegistry(); DBGControllerRegistry registry = getProcedureControllerRegistry(dataSourceContainer);
} if (registry == null) {
throw new DBGException("Can't find registry for datasource '" + dataSourceContainer.getDriver().getProviderId() + "'");
public static DBGProcedureController findProcedureController(DataSourceDescriptor datasourceDescriptor) { }
String providerId = extractProviderId(datasourceDescriptor); return registry.createController(dataSourceContainer);
DBGControllerRegistry<DBGProcedureController> controllerRegistry = getProcedureControllerRegistry();
return controllerRegistry.createController(providerId);
} }
} }
...@@ -17,33 +17,28 @@ ...@@ -17,33 +17,28 @@
*/ */
package org.jkiss.dbeaver.debug.core; package org.jkiss.dbeaver.debug.core;
import java.util.Map;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunch; import org.eclipse.debug.core.ILaunch;
import org.eclipse.osgi.util.NLS; import org.jkiss.dbeaver.debug.DBGController;
import org.jkiss.dbeaver.debug.DBGProcedureController; import org.jkiss.dbeaver.debug.DBGException;
import org.jkiss.dbeaver.debug.core.model.DatabaseProcess; import org.jkiss.dbeaver.debug.core.model.DatabaseProcess;
import org.jkiss.dbeaver.debug.core.model.ProcedureDebugTarget; import org.jkiss.dbeaver.debug.core.model.ProcedureDebugTarget;
import org.jkiss.dbeaver.registry.DataSourceDescriptor; import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.utils.GeneralUtils;
public class ProcedureLaunchDelegate extends DatabaseLaunchDelegate<DBGProcedureController<?,?>> { import java.util.Map;
public class ProcedureLaunchDelegate extends DatabaseLaunchDelegate {
@Override @Override
protected DBGProcedureController<?,?> createController(DataSourceDescriptor datasourceDescriptor, String databaseName, protected DBGController createController(DBPDataSourceContainer dataSourceContainer, String databaseName,
Map<String, Object> attributes) throws CoreException { Map<String, Object> attributes) throws CoreException
String providerId = DebugCore.extractProviderId(datasourceDescriptor); {
if (providerId == null) { try {
String message = NLS.bind("Unable to setup procedure debug for {0}", datasourceDescriptor.getName()); return DebugCore.findProcedureController(dataSourceContainer);
throw new CoreException(DebugCore.newErrorStatus(message)); } catch (DBGException e) {
} throw new CoreException(GeneralUtils.makeExceptionStatus(e));
DBGProcedureController<?,?> procedureController = DebugCore.findProcedureController(datasourceDescriptor);
if (procedureController == null) {
String message = NLS.bind("Procedure debug is not supported for {0}", datasourceDescriptor.getName());
throw new CoreException(DebugCore.newErrorStatus(message));
} }
procedureController.init(datasourceDescriptor, databaseName, attributes);
return procedureController;
} }
@Override @Override
...@@ -52,8 +47,7 @@ public class ProcedureLaunchDelegate extends DatabaseLaunchDelegate<DBGProcedure ...@@ -52,8 +47,7 @@ public class ProcedureLaunchDelegate extends DatabaseLaunchDelegate<DBGProcedure
} }
@Override @Override
protected ProcedureDebugTarget createDebugTarget(ILaunch launch, DBGProcedureController<?,?> controller, protected ProcedureDebugTarget createDebugTarget(ILaunch launch, DBGController controller, DatabaseProcess process) {
DatabaseProcess process) {
return new ProcedureDebugTarget(launch, process, controller); return new ProcedureDebugTarget(launch, process, controller);
} }
......
...@@ -32,7 +32,9 @@ import org.eclipse.debug.core.model.IThread; ...@@ -32,7 +32,9 @@ import org.eclipse.debug.core.model.IThread;
import org.eclipse.osgi.util.NLS; import org.eclipse.osgi.util.NLS;
import org.jkiss.dbeaver.debug.DBGController; import org.jkiss.dbeaver.debug.DBGController;
import org.jkiss.dbeaver.debug.DBGException; import org.jkiss.dbeaver.debug.DBGException;
import org.jkiss.dbeaver.debug.DBGSession;
import org.jkiss.dbeaver.debug.core.DebugCore; import org.jkiss.dbeaver.debug.core.DebugCore;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
public abstract class DatabaseDebugTarget<C extends DBGController> extends DatabaseDebugElement implements IDatabaseDebugTarget { public abstract class DatabaseDebugTarget<C extends DBGController> extends DatabaseDebugElement implements IDatabaseDebugTarget {
...@@ -44,6 +46,8 @@ public abstract class DatabaseDebugTarget<C extends DBGController> extends Datab ...@@ -44,6 +46,8 @@ public abstract class DatabaseDebugTarget<C extends DBGController> extends Datab
private final DatabaseThread thread; private final DatabaseThread thread;
private final IThread[] threads; private final IThread[] threads;
private DBGSession debugSession;
private String name; private String name;
private boolean suspended = false; private boolean suspended = false;
...@@ -146,7 +150,7 @@ public abstract class DatabaseDebugTarget<C extends DBGController> extends Datab ...@@ -146,7 +150,7 @@ public abstract class DatabaseDebugTarget<C extends DBGController> extends Datab
terminated = true; terminated = true;
suspended = false; suspended = false;
try { try {
controller.terminate(); controller.terminate(getProgressMonitor(), debugSession);
} catch (DBGException e) { } catch (DBGException e) {
String message = NLS.bind("Error terminating {0}", getName()); String message = NLS.bind("Error terminating {0}", getName());
IStatus status = DebugCore.newErrorStatus(message, e); IStatus status = DebugCore.newErrorStatus(message, e);
...@@ -174,7 +178,7 @@ public abstract class DatabaseDebugTarget<C extends DBGController> extends Datab ...@@ -174,7 +178,7 @@ public abstract class DatabaseDebugTarget<C extends DBGController> extends Datab
public void resume() throws DebugException { public void resume() throws DebugException {
suspended = false; suspended = false;
try { try {
controller.resume(); controller.resume(getProgressMonitor(), debugSession);
} catch (DBGException e) { } catch (DBGException e) {
String message = NLS.bind("Error resuming {0}", getName()); String message = NLS.bind("Error resuming {0}", getName());
IStatus status = DebugCore.newErrorStatus(message, e); IStatus status = DebugCore.newErrorStatus(message, e);
...@@ -189,7 +193,7 @@ public abstract class DatabaseDebugTarget<C extends DBGController> extends Datab ...@@ -189,7 +193,7 @@ public abstract class DatabaseDebugTarget<C extends DBGController> extends Datab
@Override @Override
public void suspend() throws DebugException { public void suspend() throws DebugException {
try { try {
controller.suspend(); controller.suspend(getProgressMonitor(), debugSession);
} catch (DBGException e) { } catch (DBGException e) {
String message = NLS.bind("Error suspending {0}", getName()); String message = NLS.bind("Error suspending {0}", getName());
IStatus status = DebugCore.newErrorStatus(message, e); IStatus status = DebugCore.newErrorStatus(message, e);
...@@ -197,6 +201,10 @@ public abstract class DatabaseDebugTarget<C extends DBGController> extends Datab ...@@ -197,6 +201,10 @@ public abstract class DatabaseDebugTarget<C extends DBGController> extends Datab
} }
} }
private VoidProgressMonitor getProgressMonitor() {
return new VoidProgressMonitor();
}
public void suspended(int detail) { public void suspended(int detail) {
suspended = true; suspended = true;
thread.setStepping(false); thread.setStepping(false);
......
...@@ -21,18 +21,18 @@ import org.eclipse.core.runtime.CoreException; ...@@ -21,18 +21,18 @@ import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunch; import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration; import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.model.IProcess; import org.eclipse.debug.core.model.IProcess;
import org.jkiss.dbeaver.debug.DBGProcedureController; import org.jkiss.dbeaver.debug.DBGController;
import org.jkiss.dbeaver.debug.core.DebugCore; import org.jkiss.dbeaver.debug.core.DebugCore;
import org.jkiss.dbeaver.debug.internal.core.DebugCoreMessages; import org.jkiss.dbeaver.debug.internal.core.DebugCoreMessages;
public class ProcedureDebugTarget extends DatabaseDebugTarget<DBGProcedureController<?,?>> { public class ProcedureDebugTarget extends DatabaseDebugTarget<DBGController> {
public ProcedureDebugTarget(ILaunch launch, IProcess process, DBGProcedureController<?,?> controller) { public ProcedureDebugTarget(ILaunch launch, IProcess process, DBGController controller) {
super(DebugCore.MODEL_IDENTIFIER_PROCEDURE, launch, process, controller); super(DebugCore.MODEL_IDENTIFIER_PROCEDURE, launch, process, controller);
} }
@Override @Override
protected DatabaseThread newThread(DBGProcedureController<?,?> controller) { protected DatabaseThread newThread(DBGController controller) {
return new ProcedureThread(this, controller); return new ProcedureThread(this, controller);
} }
......
...@@ -19,21 +19,21 @@ package org.jkiss.dbeaver.debug.core.model; ...@@ -19,21 +19,21 @@ package org.jkiss.dbeaver.debug.core.model;
import org.eclipse.debug.core.DebugException; import org.eclipse.debug.core.DebugException;
import org.eclipse.osgi.util.NLS; import org.eclipse.osgi.util.NLS;
import org.jkiss.dbeaver.debug.DBGProcedureController; import org.jkiss.dbeaver.debug.DBGController;
import org.jkiss.dbeaver.debug.internal.core.DebugCoreMessages; import org.jkiss.dbeaver.debug.internal.core.DebugCoreMessages;
public class ProcedureThread extends DatabaseThread { public class ProcedureThread extends DatabaseThread {
private final DBGProcedureController controller; private final DBGController controller;
public ProcedureThread(IDatabaseDebugTarget target, DBGProcedureController controller) { public ProcedureThread(IDatabaseDebugTarget target, DBGController controller) {
super(target); super(target);
this.controller = controller; this.controller = controller;
} }
@Override @Override
public String getName() throws DebugException { public String getName() throws DebugException {
String name = NLS.bind(DebugCoreMessages.ProcedureThread_name, controller.getSessionId()); String name = NLS.bind(DebugCoreMessages.ProcedureThread_name, controller.getClass().getSimpleName());
return name; return name;
} }
......
...@@ -8,8 +8,6 @@ public class DebugCoreActivator implements BundleActivator { ...@@ -8,8 +8,6 @@ public class DebugCoreActivator implements BundleActivator {
private static DebugCoreActivator activator; private static DebugCoreActivator activator;
private static BundleContext bundleContext; private static BundleContext bundleContext;
private ProcedureDebugControllerRegistry procedureControllerRegistry;
public static DebugCoreActivator getDefault() { public static DebugCoreActivator getDefault() {
return activator; return activator;
} }
...@@ -22,22 +20,13 @@ public class DebugCoreActivator implements BundleActivator { ...@@ -22,22 +20,13 @@ public class DebugCoreActivator implements BundleActivator {
public void start(BundleContext context) throws Exception { public void start(BundleContext context) throws Exception {
bundleContext = context; bundleContext = context;
activator = this; activator = this;
procedureControllerRegistry = new ProcedureDebugControllerRegistry();
procedureControllerRegistry.init();
} }
@Override @Override
public void stop(BundleContext context) throws Exception { public void stop(BundleContext context) throws Exception {
procedureControllerRegistry.dispose();
procedureControllerRegistry = null;
activator = null; activator = null;
bundleContext = null; bundleContext = null;
} }
public ProcedureDebugControllerRegistry getProcedureControllerRegistry() {
return procedureControllerRegistry;
}
} }
package org.jkiss.dbeaver.debug.internal.core;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.RegistryFactory;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.debug.DBGControllerRegistry;
import org.jkiss.dbeaver.debug.DBGProcedureController;
import org.jkiss.dbeaver.debug.core.DebugCore;
public class ProcedureDebugControllerRegistry implements DBGControllerRegistry<DBGProcedureController> {
private static Log log = Log.getLog(ProcedureDebugControllerRegistry.class);
private final Map<String, IConfigurationElement> factories = new HashMap<>();
@Override
public DBGProcedureController createController(String dataTypeProviderId) {
if (dataTypeProviderId == null) {
return null;
}
IConfigurationElement element = factories.get(dataTypeProviderId);
if (element == null) {
return null;
}
try {
Object executable = element.createExecutableExtension(DebugCore.EP_PROCEDURE_CONTROLLERS_CONTROLLER_CLASS);
if (executable instanceof DBGProcedureController) {
DBGProcedureController controller = (DBGProcedureController) executable;
return controller;
}
} catch (CoreException e) {
Log.log(log, e.getStatus());
}
return null;
}
public void init() {
IExtensionRegistry registry = RegistryFactory.getRegistry();
final String namespace = DebugCore.BUNDLE_SYMBOLIC_NAME;
final String epName = DebugCore.EP_PROCEDURE_CONTROLLERS_ID;
final String elementName = DebugCore.EP_PROCEDURE_CONTROLLERS_CONTROLLER;
final String identifierName = DebugCore.EP_PROCEDURE_CONTROLLERS_CONTROLLER_PROVIDER_ID;
IExtensionPoint extensionPoint = registry.getExtensionPoint(namespace, epName);
IConfigurationElement[] configurationElements = extensionPoint.getConfigurationElements();
for (IConfigurationElement element : configurationElements) {
String name = element.getName();
if (elementName.equals(name)) {
String configuredIdentifier = element.getAttribute(identifierName);
factories.put(configuredIdentifier, element);
}
}
}
public void dispose() {
factories.clear();
}
}
...@@ -30,12 +30,9 @@ ...@@ -30,12 +30,9 @@
name="%launchConfigurationTypes.launchConfigurationType.pgSQL.name"> name="%launchConfigurationTypes.launchConfigurationType.pgSQL.name">
</launchConfigurationType> </launchConfigurationType>
</extension> </extension>
<extension
point="org.jkiss.dbeaver.debug.core.procedureControllers"> <extension point="org.jkiss.dbeaver.debug.core.controllers">
<controller <controller datasource="postgresql" class="org.jkiss.dbeaver.ext.postgresql.debug.internal.PostgreDebugControllerRegistry"/>
class="org.jkiss.dbeaver.ext.postgresql.debug.internal.PostgreProcedureController"
providerId="postgresql">
</controller>
</extension> </extension>
</plugin> </plugin>
...@@ -17,56 +17,52 @@ ...@@ -17,56 +17,52 @@
*/ */
package org.jkiss.dbeaver.ext.postgresql.debug.internal; package org.jkiss.dbeaver.ext.postgresql.debug.internal;
import java.sql.Connection; import org.jkiss.dbeaver.DBException;
import java.sql.SQLException; import org.jkiss.dbeaver.debug.DBGBaseController;
import org.jkiss.dbeaver.debug.DBGException; import org.jkiss.dbeaver.debug.DBGException;
import org.jkiss.dbeaver.debug.DBGObject;
import org.jkiss.dbeaver.debug.DBGProcedureController;
import org.jkiss.dbeaver.debug.DBGSession; import org.jkiss.dbeaver.debug.DBGSession;
import org.jkiss.dbeaver.debug.DBGSessionInfo;
import org.jkiss.dbeaver.debug.DBGSessionManager;
import org.jkiss.dbeaver.ext.postgresql.debug.internal.impl.PostgreDebugSessionManager; import org.jkiss.dbeaver.ext.postgresql.debug.internal.impl.PostgreDebugSessionManager;
import org.jkiss.dbeaver.model.exec.DBCSession; import org.jkiss.dbeaver.model.DBPDataSource;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession; import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCExecutionContext;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
public class PostgreDebugController extends DBGBaseController {
public class PostgreProcedureController extends DBGProcedureController<Integer, Integer> { private PostgreDebugSessionManager sessionManager;
public PostgreProcedureController() { public PostgreDebugController(DBPDataSourceContainer dataSourceDescriptor) {
super(); super(dataSourceDescriptor);
} }
@Override private PostgreDebugSessionManager getSessionManager(DBRProgressMonitor monitor) throws DBGException {
protected DBGSessionManager<Integer, Integer> initSessionManager(DBCSession session) throws DBGException { if (sessionManager == null) {
if (session instanceof JDBCSession) {
JDBCSession jdbcSession = (JDBCSession) session;
Connection original;
try { try {
original = jdbcSession.getOriginal(); JDBCExecutionContext controllerContext = (JDBCExecutionContext) getDataSourceContainer().getDataSource().openIsolatedContext(monitor, "Debug controller");
return new PostgreDebugSessionManager(original); sessionManager = new PostgreDebugSessionManager(controllerContext);
} catch (SQLException e) { } catch (Exception e) {
throw new DBGException("Unable to obtain connection"); throw new DBGException("Can't initiate debug session manager", e);
} }
} }
throw new DBGException("Invalid JDBC session handle"); return sessionManager;
} }
@Override @Override
protected DBGSession<? extends DBGSessionInfo<Integer>, ? extends DBGObject<Integer>, Integer, Integer> createSession( protected DBGSession createSession(DBRProgressMonitor monitor, DBPDataSource dataSource) throws DBGException {
DBCSession session, DBGSessionManager<Integer, Integer> sessionManager) throws DBGException { PostgreDebugSessionManager sessionManager = getSessionManager(monitor);
try {
if (session instanceof JDBCSession) { JDBCExecutionContext sessionContext = (JDBCExecutionContext) getDataSourceContainer().getDataSource().openIsolatedContext(monitor, "Debug session");
JDBCSession jdbcSession = (JDBCSession) session; return this.sessionManager.createDebugSession(sessionContext);
Connection original; } catch (DBException e) {
try { throw new DBGException("Can't initiate debug session", e);
original = jdbcSession.getOriginal();
DBGSession<? extends DBGSessionInfo<Integer>, ? extends DBGObject<Integer>, Integer, Integer> created = sessionManager.createDebugSession(original);
return created;
} catch (SQLException e) {
throw new DBGException("Unable to obtain connection");
}
} }
throw new DBGException("Invalid JDBC session handle");
} }
@Override
public void dispose() {
if (sessionManager != null) {
sessionManager.dispose();
sessionManager = null;
}
}
} }
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org)
* Copyright (C) 2017 Alexander Fedorov (alexander.fedorov@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.ext.postgresql.debug.internal;
import org.jkiss.dbeaver.debug.DBGController;
import org.jkiss.dbeaver.debug.DBGControllerRegistry;
import org.jkiss.dbeaver.debug.DBGException;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
public class PostgreDebugControllerRegistry implements DBGControllerRegistry {
@Override
public DBGController createController(DBPDataSourceContainer dataSource) throws DBGException {
return new PostgreDebugController(dataSource);
}
}
...@@ -287,7 +287,8 @@ public class Debugger { ...@@ -287,7 +287,8 @@ public class Debugger {
return; return;
} }
pgDbgManager = new PostgreDebugSessionManager(conn); // TODO: fix connection
pgDbgManager = new PostgreDebugSessionManager(null);
Scanner sc = new Scanner(System.in); Scanner sc = new Scanner(System.in);
Scanner scArg; Scanner scArg;
...@@ -664,7 +665,8 @@ public class Debugger { ...@@ -664,7 +665,8 @@ public class Debugger {
case COMMAND_NEW: case COMMAND_NEW:
try { try {
Connection debugConn = DriverManager.getConnection(url); Connection debugConn = DriverManager.getConnection(url);
PostgreDebugSession s = pgDbgManager.createDebugSession(debugConn); // TODO: fix connection
PostgreDebugSession s = pgDbgManager.createDebugSession(null);
System.out.println("created"); System.out.println("created");
System.out.println(s); System.out.println(s);
......
...@@ -54,7 +54,7 @@ public class PostgreDebugBreakpoint implements DBGBreakpoint { ...@@ -54,7 +54,7 @@ public class PostgreDebugBreakpoint implements DBGBreakpoint {
: String.valueOf(properties.getTargetId()))); : String.valueOf(properties.getTargetId())));
} catch (SQLException e) { } catch (SQLException e) {
throw new DBGException(e); throw new DBGException("SQL error", e);
} }
} }
...@@ -73,7 +73,7 @@ public class PostgreDebugBreakpoint implements DBGBreakpoint { ...@@ -73,7 +73,7 @@ public class PostgreDebugBreakpoint implements DBGBreakpoint {
.replaceAll("\\?line", properties.isOnStart() ? "-1" : String.valueOf(properties.getLineNo()))); .replaceAll("\\?line", properties.isOnStart() ? "-1" : String.valueOf(properties.getLineNo())));
} catch (SQLException e) { } catch (SQLException e) {
throw new DBGException(e); throw new DBGException("SQL error", e);
} }
} }
......
...@@ -18,26 +18,18 @@ ...@@ -18,26 +18,18 @@
package org.jkiss.dbeaver.ext.postgresql.debug.internal.impl; package org.jkiss.dbeaver.ext.postgresql.debug.internal.impl;
import java.sql.Connection; import org.jkiss.dbeaver.debug.*;
import java.sql.PreparedStatement; import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import java.sql.ResultSet; import org.jkiss.dbeaver.model.impl.jdbc.JDBCExecutionContext;
import java.sql.SQLException; import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
import java.sql.Statement;
import java.sql.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask; import java.util.concurrent.FutureTask;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.eclipse.debug.core.DebugException;
import org.jkiss.dbeaver.debug.DBGBreakpoint;
import org.jkiss.dbeaver.debug.DBGBreakpointProperties;
import org.jkiss.dbeaver.debug.DBGException;
import org.jkiss.dbeaver.debug.DBGSession;
import org.jkiss.dbeaver.debug.DBGStackFrame;
import org.jkiss.dbeaver.debug.DBGVariable;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement;
@SuppressWarnings("nls") @SuppressWarnings("nls")
public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo, PostgreDebugObject, Integer, Integer> { public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo, PostgreDebugObject, Integer, Integer> {
...@@ -45,7 +37,7 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo, ...@@ -45,7 +37,7 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo,
private final PostgreDebugSessionInfo sessionDebugInfo; private final PostgreDebugSessionInfo sessionDebugInfo;
private final Connection connection; private final JDBCExecutionContext connection;
private final String title; private final String title;
...@@ -83,13 +75,13 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo, ...@@ -83,13 +75,13 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo,
acquireWriteLock(); acquireWriteLock();
try (Statement stmt = connection.createStatement()) { try (Statement stmt = getConnection(connection).createStatement()) {
ResultSet rs = stmt.executeQuery(SQL_LISTEN); ResultSet rs = stmt.executeQuery(SQL_LISTEN);
if (rs.next()) { if (rs.next()) {
connection.setClientInfo("ApplicationName", "Debug Mode : " + String.valueOf(sessionId)); getConnection(connection).setClientInfo("ApplicationName", "Debug Mode : " + String.valueOf(sessionId));
return rs.getInt("sessionid"); return rs.getInt("sessionid");
} else { } else {
...@@ -99,7 +91,7 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo, ...@@ -99,7 +91,7 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo,
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new DBGException(e); throw new DBGException("SQL error", e);
} finally { } finally {
lock.writeLock().unlock(); lock.writeLock().unlock();
} }
...@@ -129,7 +121,7 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo, ...@@ -129,7 +121,7 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo,
} }
public PostgreDebugSession(PostgreDebugSessionInfo sessionManagerInfo, PostgreDebugSessionInfo sessionDebugInfo, public PostgreDebugSession(PostgreDebugSessionInfo sessionManagerInfo, PostgreDebugSessionInfo sessionDebugInfo,
Connection connection) throws DBGException { JDBCExecutionContext connection) throws DBGException {
this.sessionManagerInfo = sessionManagerInfo; this.sessionManagerInfo = sessionManagerInfo;
this.sessionDebugInfo = sessionDebugInfo; this.sessionDebugInfo = sessionDebugInfo;
this.connection = connection; this.connection = connection;
...@@ -137,6 +129,10 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo, ...@@ -137,6 +129,10 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo,
sessionId = listen(); sessionId = listen();
} }
private static Connection getConnection(DBCExecutionContext connectionTarget) throws SQLException {
return ((JDBCExecutionContext) connectionTarget).getConnection(new VoidProgressMonitor());
}
@Override @Override
public PostgreDebugSessionInfo getSessionInfo() { public PostgreDebugSessionInfo getSessionInfo() {
...@@ -227,14 +223,14 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo, ...@@ -227,14 +223,14 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo,
acquireReadLock(); acquireReadLock();
try (Statement stmt = connection.createStatement()) { try (Statement stmt = getConnection(connection).createStatement()) {
stmt.execute(SQL_ABORT.replaceAll("\\?sessionid", String.valueOf(sessionId))); stmt.execute(SQL_ABORT.replaceAll("\\?sessionid", String.valueOf(sessionId)));
task = null; task = null;
} catch (SQLException e) { } catch (SQLException e) {
throw new DBGException(e); throw new DBGException("SQL error", e);
} finally { } finally {
lock.readLock().unlock(); lock.readLock().unlock();
} }
...@@ -257,10 +253,6 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo, ...@@ -257,10 +253,6 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo,
connection.close(); connection.close();
} catch (SQLException e) {
e.printStackTrace();
} finally { } finally {
lock.writeLock().unlock(); lock.writeLock().unlock();
} }
...@@ -274,7 +266,7 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo, ...@@ -274,7 +266,7 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo,
List<DBGVariable<?>> vars = new ArrayList<>(); List<DBGVariable<?>> vars = new ArrayList<>();
try (Statement stmt = connection.createStatement()) { try (Statement stmt = getConnection(connection).createStatement()) {
ResultSet rs = stmt.executeQuery(SQL_GET_VARS.replaceAll("\\?sessionid", String.valueOf(sessionId))); ResultSet rs = stmt.executeQuery(SQL_GET_VARS.replaceAll("\\?sessionid", String.valueOf(sessionId)));
...@@ -288,7 +280,7 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo, ...@@ -288,7 +280,7 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo,
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new DBGException(e); throw new DBGException("SQL error", e);
} finally { } finally {
lock.readLock().unlock(); lock.readLock().unlock();
} }
...@@ -302,7 +294,7 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo, ...@@ -302,7 +294,7 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo,
acquireReadLock(); acquireReadLock();
try (PreparedStatement stmt = connection.prepareStatement(SQL_SET_VAR)) { try (PreparedStatement stmt = getConnection(connection).prepareStatement(SQL_SET_VAR)) {
if (variable instanceof PostgreDebugVariable){ if (variable instanceof PostgreDebugVariable){
...@@ -329,7 +321,7 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo, ...@@ -329,7 +321,7 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo,
} catch (SQLException e) { } catch (SQLException e) {
throw new DBGException(e); throw new DBGException("SQL error", e);
} finally { } finally {
lock.readLock().unlock(); lock.readLock().unlock();
} }
...@@ -342,7 +334,7 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo, ...@@ -342,7 +334,7 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo,
List<DBGStackFrame> stack = new ArrayList<DBGStackFrame>(1); List<DBGStackFrame> stack = new ArrayList<DBGStackFrame>(1);
try (Statement stmt = connection.createStatement()) { try (Statement stmt = getConnection(connection).createStatement()) {
ResultSet rs = stmt.executeQuery(SQL_GET_STACK.replaceAll("\\?sessionid", String.valueOf(sessionId))); ResultSet rs = stmt.executeQuery(SQL_GET_STACK.replaceAll("\\?sessionid", String.valueOf(sessionId)));
...@@ -353,15 +345,19 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo, ...@@ -353,15 +345,19 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo,
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new DBGException(e); throw new DBGException("SQL error", e);
} finally { } finally {
lock.readLock().unlock(); lock.readLock().unlock();
} }
return stack; return stack;
} }
public Connection getConnection() { public Connection getConnection() throws DBGException {
return connection; try {
return getConnection(connection);
} catch (SQLException e) {
throw new DBGException("SQL error", e);
}
} }
@Override @Override
...@@ -389,7 +385,7 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo, ...@@ -389,7 +385,7 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo,
try { try {
task.get(); task.get();
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new DBGException(e); throw new DBGException("SQL error", e);
} catch (ExecutionException e) { } catch (ExecutionException e) {
System.out.println("WARNING " + e.getMessage()); System.out.println("WARNING " + e.getMessage());
return false; return false;
...@@ -404,6 +400,7 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo, ...@@ -404,6 +400,7 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo,
private void runAsync(String commandSQL, String name) throws DBGException { private void runAsync(String commandSQL, String name) throws DBGException {
Connection connection = getConnection();
try (Statement stmt = connection.createStatement()) { try (Statement stmt = connection.createStatement()) {
connection.setAutoCommit(false); connection.setAutoCommit(false);
...@@ -419,7 +416,7 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo, ...@@ -419,7 +416,7 @@ public class PostgreDebugSession implements DBGSession<PostgreDebugSessionInfo,
workerThread.start(); workerThread.start();
} catch (SQLException e) { } catch (SQLException e) {
throw new DBGException(e); throw new DBGException("SQL error", e);
} }
} }
......
...@@ -30,49 +30,56 @@ import java.util.Map; ...@@ -30,49 +30,56 @@ import java.util.Map;
import org.jkiss.dbeaver.debug.DBGException; import org.jkiss.dbeaver.debug.DBGException;
import org.jkiss.dbeaver.debug.DBGSession; import org.jkiss.dbeaver.debug.DBGSession;
import org.jkiss.dbeaver.debug.DBGSessionManager; import org.jkiss.dbeaver.debug.DBGSessionManager;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCExecutionContext;
import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor;
@SuppressWarnings("nls") @SuppressWarnings("nls")
public class PostgreDebugSessionManager implements DBGSessionManager<Integer, Integer> { public class PostgreDebugSessionManager implements DBGSessionManager<Integer, Integer> {
private final Connection connection; private final DBCExecutionContext context;
private static final String SQL_SESSION = "select pid,usename,application_name,state,query from pg_stat_activity"; private static final String SQL_SESSION = "select pid,usename,application_name,state,query from pg_stat_activity";
private static final String SQL_OBJECT = "select p.oid,p.proname,u.usename as owner,n.nspname, l.lanname as lang " private static final String SQL_OBJECT = "select p.oid,p.proname,u.usename as owner,n.nspname, l.lanname as lang "
+ " from " + " pg_catalog.pg_namespace n " + " join pg_catalog.pg_proc p on p.pronamespace = n.oid " + " from " + " pg_catalog.pg_namespace n " + " join pg_catalog.pg_proc p on p.pronamespace = n.oid "
+ " join pg_user u on u.usesysid = p.proowner " + " join pg_language l on l.oid = p. prolang " + " join pg_user u on u.usesysid = p.proowner " + " join pg_language l on l.oid = p. prolang "
+ " where " + " l.lanname = 'plpgsql' " + " and p.proname like '%?nameCtx%' " + " where " + " l.lanname = 'plpgsql' " + " and p.proname like '%?nameCtx%' "
+ " and u.usename like '%?userCtx%' " + " order by " + " n.nspname,p.proname"; + " and u.usename like '%?userCtx%' " + " order by " + " n.nspname,p.proname";
private static final String SQL_CURRENT_SESSION = "select pid,usename,application_name,state,query from pg_stat_activity where pid = pg_backend_pid()"; private static final String SQL_CURRENT_SESSION = "select pid,usename,application_name,state,query from pg_stat_activity where pid = pg_backend_pid()";
private final Map<Integer, PostgreDebugSession> sessions = new HashMap<Integer, PostgreDebugSession>(1); private final Map<Integer, PostgreDebugSession> sessions = new HashMap<Integer, PostgreDebugSession>(1);
@Override @Override
public PostgreDebugSessionInfo getSessionInfo(Connection connectionTarget) throws DBGException { public PostgreDebugSessionInfo getSessionInfo(DBCExecutionContext connectionTarget) throws DBGException {
try (Statement stmt = connectionTarget.createStatement()) { try (Statement stmt = getConnection(connectionTarget).createStatement()) {
ResultSet rs = stmt.executeQuery(SQL_CURRENT_SESSION); ResultSet rs = stmt.executeQuery(SQL_CURRENT_SESSION);
if (rs.next()) { if (rs.next()) {
PostgreDebugSessionInfo res = new PostgreDebugSessionInfo(rs.getInt("pid"), rs.getString("usename"), PostgreDebugSessionInfo res = new PostgreDebugSessionInfo(rs.getInt("pid"), rs.getString("usename"),
rs.getString("application_name"), rs.getString("state"), rs.getString("query")); rs.getString("application_name"), rs.getString("state"), rs.getString("query"));
return res; return res;
} }
throw new DBGException("Error getting session"); throw new DBGException("Error getting session");
} catch (SQLException e) { } catch (SQLException e) {
throw new DBGException(e); throw new DBGException("SQ Lerror", e);
} }
} }
private static Connection getConnection(DBCExecutionContext connectionTarget) throws SQLException {
return ((JDBCExecutionContext) connectionTarget).getConnection(new VoidProgressMonitor());
}
@Override @Override
public List<PostgreDebugSessionInfo> getSessions() throws DBGException { public List<PostgreDebugSessionInfo> getSessions() throws DBGException {
try (Statement stmt = connection.createStatement()) { try (Statement stmt = getConnection(context).createStatement()) {
ResultSet rs = stmt.executeQuery(SQL_SESSION); ResultSet rs = stmt.executeQuery(SQL_SESSION);
...@@ -81,61 +88,60 @@ public class PostgreDebugSessionManager implements DBGSessionManager<Integer, In ...@@ -81,61 +88,60 @@ public class PostgreDebugSessionManager implements DBGSessionManager<Integer, In
while (rs.next()) { while (rs.next()) {
res.add(new PostgreDebugSessionInfo(rs.getInt("pid"), rs.getString("usename"), res.add(new PostgreDebugSessionInfo(rs.getInt("pid"), rs.getString("usename"),
rs.getString("application_name"), rs.getString("state"), rs.getString("query"))); rs.getString("application_name"), rs.getString("state"), rs.getString("query")));
} }
return res; return res;
} catch (SQLException e) { } catch (SQLException e) {
throw new DBGException(e); throw new DBGException("SQL error", e);
} }
} }
/** /**
* @param connection * @param context
*/ */
public PostgreDebugSessionManager(Connection connection) { public PostgreDebugSessionManager(DBCExecutionContext context) {
super(); super();
this.connection = connection; this.context = context;
} }
@Override @Override
public List<PostgreDebugObject> getObjects(String ownerCtx, String nameCtx) throws DBGException { public List<PostgreDebugObject> getObjects(String ownerCtx, String nameCtx) throws DBGException {
try (Statement stmt = connection.createStatement()) { try (Statement stmt = getConnection(context).createStatement()) {
ResultSet rs = stmt.executeQuery( ResultSet rs = stmt.executeQuery(
SQL_OBJECT.replaceAll("\\?nameCtx", nameCtx).replaceAll("\\?userCtx", ownerCtx).toLowerCase()); SQL_OBJECT.replaceAll("\\?nameCtx", nameCtx).replaceAll("\\?userCtx", ownerCtx).toLowerCase());
List<PostgreDebugObject> res = new ArrayList<PostgreDebugObject>(); List<PostgreDebugObject> res = new ArrayList<PostgreDebugObject>();
while (rs.next()) { while (rs.next()) {
res.add(new PostgreDebugObject(rs.getInt("oid"), rs.getString("proname"), rs.getString("owner"), res.add(new PostgreDebugObject(rs.getInt("oid"), rs.getString("proname"), rs.getString("owner"),
rs.getString("nspname"), rs.getString("lang"))); rs.getString("nspname"), rs.getString("lang")));
} }
return res; return res;
} catch (SQLException e) { } catch (SQLException e) {
throw new DBGException(e); throw new DBGException("SQL error", e);
} }
} }
@Override @Override
public DBGSession<PostgreDebugSessionInfo, PostgreDebugObject, Integer, Integer> getDebugSession(Integer id) public DBGSession<PostgreDebugSessionInfo, PostgreDebugObject, Integer, Integer> getDebugSession(Integer id)
throws DBGException { throws DBGException {
return sessions.get(id); return sessions.get(id);
} }
@Override @Override
public PostgreDebugSession createDebugSession(Connection connectionTarget) throws DBGException { public PostgreDebugSession createDebugSession(DBCExecutionContext connectionTarget) throws DBGException {
PostgreDebugSessionInfo targetInfo = getSessionInfo(connectionTarget); PostgreDebugSessionInfo targetInfo = getSessionInfo(connectionTarget);
PostgreDebugSession debugSession = new PostgreDebugSession(getSessionInfo(this.connection), targetInfo, PostgreDebugSession debugSession = new PostgreDebugSession(getSessionInfo(this.context), targetInfo, (JDBCExecutionContext) connectionTarget);
connectionTarget);
sessions.put(targetInfo.getPid(), debugSession); sessions.put(targetInfo.getPid(), debugSession);
...@@ -170,6 +176,7 @@ public class PostgreDebugSessionManager implements DBGSessionManager<Integer, In ...@@ -170,6 +176,7 @@ public class PostgreDebugSessionManager implements DBGSessionManager<Integer, In
@Override @Override
public void dispose() { public void dispose() {
context.close();
//FIXME: AF: perform cleanup for everything cached //FIXME: AF: perform cleanup for everything cached
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册