diff --git a/plugins/org.jkiss.dbeaver.debug.core/src/org/jkiss/dbeaver/debug/DBGController.java b/plugins/org.jkiss.dbeaver.debug.core/src/org/jkiss/dbeaver/debug/DBGController.java index 86bea9a95bd0d368eb1a93605c7bd4f483dea207..91f294f3f50e5d0c86684fd1af48cd957aec697b 100644 --- a/plugins/org.jkiss.dbeaver.debug.core/src/org/jkiss/dbeaver/debug/DBGController.java +++ b/plugins/org.jkiss.dbeaver.debug.core/src/org/jkiss/dbeaver/debug/DBGController.java @@ -17,6 +17,9 @@ */ package org.jkiss.dbeaver.debug; +import java.util.List; + +import org.jkiss.dbeaver.model.exec.DBCExecutionContext; import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; /** @@ -34,4 +37,20 @@ public interface DBGController { void dispose() throws DBGException; + DBGSessionInfo getSessionInfo(DBCExecutionContext connection) throws DBGException; + + List getSessions() throws DBGException; + + DBGSession getDebugSession(Object id) throws DBGException; + + List getDebugSessions() throws DBGException; + + void terminateSession(Object id); + + DBGSession createDebugSession(DBCExecutionContext connection) throws DBGException; + + boolean isSessionExists(Object id); + + List getObjects(String ownerCtx, String nameCtx) throws DBGException; + } diff --git a/plugins/org.jkiss.dbeaver.debug.core/src/org/jkiss/dbeaver/debug/DBGSessionManager.java b/plugins/org.jkiss.dbeaver.debug.core/src/org/jkiss/dbeaver/debug/DBGSessionManager.java deleted file mode 100644 index f8880600088e27e7f6aed979003a8da7c3843c72..0000000000000000000000000000000000000000 --- a/plugins/org.jkiss.dbeaver.debug.core/src/org/jkiss/dbeaver/debug/DBGSessionManager.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * DBeaver - Universal Database Manager - * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) - * Copyright (C) 2017 Andrew Khitrin (ahitrin@gmail.com) - * - * 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; - -import java.util.List; - -import org.jkiss.dbeaver.model.exec.DBCExecutionContext; - -public interface DBGSessionManager { - DBGSessionInfo getSessionInfo(DBCExecutionContext connection) throws DBGException; - - List getSessions() throws DBGException; - - DBGSession getDebugSession(Object id) throws DBGException; - - List getDebugSessions() throws DBGException; - - void terminateSession(Object id); - - DBGSession createDebugSession(DBCExecutionContext connection) throws DBGException; - - boolean isSessionExists(Object id); - - List getObjects(String ownerCtx, String nameCtx) throws DBGException; - - void dispose(); -} diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/debug/internal/console/Debugger.java b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/debug/internal/console/Debugger.java index 01160759878c713c8cb0bbec9508dafd2437946e..ce7314e52d27934517258e576651736bfa3e69bd 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/debug/internal/console/Debugger.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/debug/internal/console/Debugger.java @@ -25,15 +25,16 @@ import java.util.Scanner; import org.jkiss.dbeaver.debug.DBGBreakpoint; import org.jkiss.dbeaver.debug.DBGBreakpointProperties; +import org.jkiss.dbeaver.debug.DBGController; import org.jkiss.dbeaver.debug.DBGException; import org.jkiss.dbeaver.debug.DBGObject; import org.jkiss.dbeaver.debug.DBGSession; import org.jkiss.dbeaver.debug.DBGSessionInfo; -import org.jkiss.dbeaver.debug.DBGSessionManager; import org.jkiss.dbeaver.debug.DBGStackFrame; import org.jkiss.dbeaver.debug.DBGVariable; import org.jkiss.dbeaver.ext.postgresql.debug.internal.impl.PostgreDebugBreakpointProperties; -import org.jkiss.dbeaver.ext.postgresql.debug.internal.impl.PostgreDebugSessionManager; +import org.jkiss.dbeaver.ext.postgresql.debug.internal.impl.PostgreDebugController; +import org.jkiss.dbeaver.model.DBPDataSourceContainer; @SuppressWarnings("nls") public class Debugger { @@ -62,7 +63,7 @@ public class Debugger { public static final String ANY_ARG = "*"; - public static DBGVariable chooseVariable(Scanner sc, DBGSessionManager pgDbgManager, + public static DBGVariable chooseVariable(Scanner sc, DBGController controller, DBGSession session) throws DBGException { DBGVariable v = null; @@ -134,8 +135,8 @@ public class Debugger { } - public static DBGBreakpoint chooseBreakpoint(Scanner sc, DBGSessionManager pgDbgManager, - DBGSession session) throws DBGException { + public static DBGBreakpoint chooseBreakpoint(Scanner sc, DBGController controller, DBGSession session) + throws DBGException { DBGBreakpoint bp = null; @@ -206,12 +207,12 @@ public class Debugger { } - public static DBGSession chooseSession(Scanner sc, DBGSessionManager pgDbgManager) + public static DBGSession chooseSession(Scanner sc, DBGController controller) throws DBGException { DBGSession debugSession = null; - List sessions = pgDbgManager.getDebugSessions(); + List sessions = controller.getDebugSessions(); Scanner scArg; @@ -281,9 +282,10 @@ public class Debugger { public static void main(String[] args) throws DBGException { String url = "jdbc:postgresql://192.168.229.133/postgres?user=postgres&password=postgres&ssl=false"; // "jdbc:postgresql://localhost/postgres?user=postgres&password=postgres&ssl=false"; + DBPDataSourceContainer dataSource = null; Connection conn; - DBGSessionManager pgDbgManager; + DBGController controller; try { conn = DriverManager.getConnection(url); @@ -294,7 +296,7 @@ public class Debugger { } // TODO: fix connection - pgDbgManager = new PostgreDebugSessionManager(null); + controller = new PostgreDebugController(dataSource); Scanner sc = new Scanner(System.in); Scanner scArg; @@ -327,18 +329,18 @@ public class Debugger { case COMMAND_CLOSE: - if (pgDbgManager.getDebugSessions().size() == 0) { + if (controller.getDebugSessions().size() == 0) { System.out.println("Debug sessions not found"); break; } - DBGSession debugSessionC = chooseSession(sc, pgDbgManager); + DBGSession debugSessionC = chooseSession(sc, controller); if (debugSessionC == null) { break; } - pgDbgManager.terminateSession(debugSessionC.getSessionId()); + controller.terminateSession(debugSessionC.getSessionId()); System.out.println("Session closed"); @@ -346,12 +348,12 @@ public class Debugger { case COMMAND_ABORT: - if (pgDbgManager.getDebugSessions().size() == 0) { + if (controller.getDebugSessions().size() == 0) { System.out.println("Debug sessions not found"); break; } - DBGSession debugSessionAB = chooseSession(sc, pgDbgManager); + DBGSession debugSessionAB = chooseSession(sc, controller); if (debugSessionAB == null) { break; @@ -365,12 +367,12 @@ public class Debugger { case COMMAND_STACK: - if (pgDbgManager.getDebugSessions().size() == 0) { + if (controller.getDebugSessions().size() == 0) { System.out.println("Debug sessions not found"); break; } - DBGSession debugSessionSL = chooseSession(sc, pgDbgManager); + DBGSession debugSessionSL = chooseSession(sc, controller); if (debugSessionSL == null) { break; @@ -394,12 +396,12 @@ public class Debugger { case COMMAND_VARIABLES: - if (pgDbgManager.getDebugSessions().size() == 0) { + if (controller.getDebugSessions().size() == 0) { System.out.println("Debug sessions not found"); break; } - DBGSession debugSessionVL = chooseSession(sc, pgDbgManager); + DBGSession debugSessionVL = chooseSession(sc, controller); if (debugSessionVL == null) { break; @@ -436,19 +438,19 @@ public class Debugger { } - if (pgDbgManager.getDebugSessions().size() == 0) { + if (controller.getDebugSessions().size() == 0) { System.out.println("Debug sessions not found"); break; } - DBGSession debugSessionVS = chooseSession(sc, pgDbgManager); + DBGSession debugSessionVS = chooseSession(sc, controller); if (debugSessionVS == null) { break; } - DBGVariable var = chooseVariable(sc, pgDbgManager,debugSessionVS); + DBGVariable var = chooseVariable(sc, controller,debugSessionVS); if (var == null) { break; @@ -517,7 +519,7 @@ public class Debugger { DBGObject debugObject = null; - for (DBGObject o : pgDbgManager.getObjects("_", "_")) { + for (DBGObject o : controller.getObjects("_", "_")) { if (objId.equals(o.getID())) { debugObject = o; } @@ -528,12 +530,12 @@ public class Debugger { break; } - if (pgDbgManager.getDebugSessions().size() == 0) { + if (controller.getDebugSessions().size() == 0) { System.out.println("Debug sessions not found"); break; } - DBGSession debugSession = chooseSession(sc, pgDbgManager); + DBGSession debugSession = chooseSession(sc, controller); if (debugSession == null) { break; @@ -551,12 +553,12 @@ public class Debugger { break; case COMMAND_BREAKPOINT_LIST: - if (pgDbgManager.getDebugSessions().size() == 0) { + if (controller.getDebugSessions().size() == 0) { System.out.println("Debug sessions not found"); break; } - DBGSession debugSessionBL = chooseSession(sc, pgDbgManager); + DBGSession debugSessionBL = chooseSession(sc, controller); if (debugSessionBL == null) { break; @@ -573,12 +575,12 @@ public class Debugger { break; case COMMAND_BREAKPOINT_REMOVE: - if (pgDbgManager.getDebugSessions().size() == 0) { + if (controller.getDebugSessions().size() == 0) { System.out.println("Debug sessions not found"); break; } - DBGSession debugSessionBR = chooseSession(sc, pgDbgManager); + DBGSession debugSessionBR = chooseSession(sc, controller); if (debugSessionBR == null) { break; @@ -588,7 +590,7 @@ public class Debugger { System.out.println("No breakpoints defined"); } - DBGBreakpoint bpr = chooseBreakpoint(sc, pgDbgManager, debugSessionBR); + DBGBreakpoint bpr = chooseBreakpoint(sc, controller, debugSessionBR); debugSessionBR.removeBreakpoint(bpr); @@ -597,12 +599,12 @@ public class Debugger { break; case COMMAND_CONTINUE: - if (pgDbgManager.getDebugSessions().size() == 0) { + if (controller.getDebugSessions().size() == 0) { System.out.println("Debug sessions not found"); break; } - DBGSession debugSessionSC = chooseSession(sc, pgDbgManager); + DBGSession debugSessionSC = chooseSession(sc, controller); if (debugSessionSC == null) { break; @@ -615,12 +617,12 @@ public class Debugger { break; case COMMAND_INTO: - if (pgDbgManager.getDebugSessions().size() == 0) { + if (controller.getDebugSessions().size() == 0) { System.out.println("Debug sessions not found"); break; } - DBGSession debugSessionSI = chooseSession(sc, pgDbgManager); + DBGSession debugSessionSI = chooseSession(sc, controller); if (debugSessionSI == null) { break; @@ -634,12 +636,12 @@ public class Debugger { case COMMAND_OVER: - if (pgDbgManager.getDebugSessions().size() == 0) { + if (controller.getDebugSessions().size() == 0) { System.out.println("Debug sessions not found"); break; } - DBGSession debugSessionSO = chooseSession(sc, pgDbgManager); + DBGSession debugSessionSO = chooseSession(sc, controller); if (debugSessionSO == null) { break; @@ -652,17 +654,17 @@ public class Debugger { break; case COMMAND_SESSIONS: - for (DBGSessionInfo s : pgDbgManager.getSessions()) { + for (DBGSessionInfo s : controller.getSessions()) { System.out.println(s); } break; case COMMAND_DEBUG_LIST: - if (pgDbgManager.getDebugSessions().size() == 0) { + if (controller.getDebugSessions().size() == 0) { System.out.println("no debug sessions"); break; } - for (DBGSession s : pgDbgManager.getDebugSessions()) { + for (DBGSession s : controller.getDebugSessions()) { System.out.println(s); } @@ -672,7 +674,7 @@ public class Debugger { try { Connection debugConn = DriverManager.getConnection(url); // TODO: fix connection - DBGSession s = pgDbgManager.createDebugSession(null); + DBGSession s = controller.createDebugSession(null); System.out.println("created"); System.out.println(s); @@ -711,7 +713,7 @@ public class Debugger { } - for (DBGObject o : pgDbgManager.getObjects(owner.equals(ANY_ARG) ? "_" : owner, + for (DBGObject o : controller.getObjects(owner.equals(ANY_ARG) ? "_" : owner, proc.equals(ANY_ARG) ? "_" : proc)) { System.out.println(o); } @@ -719,12 +721,12 @@ public class Debugger { break; case COMMAND_ATTACH: - if (pgDbgManager.getDebugSessions().size() == 0) { + if (controller.getDebugSessions().size() == 0) { System.out.println("Debug sessions not found"); break; } - DBGSession debugSessionA = chooseSession(sc, pgDbgManager); + DBGSession debugSessionA = chooseSession(sc, controller); if (debugSessionA == null) { break; diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/debug/internal/impl/PostgreDebugController.java b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/debug/internal/impl/PostgreDebugController.java index 63d53808e81d8304f913115b6f976560c8c84b6f..6b990e51aeda6b6b6d072759c6768980d89e4433 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/debug/internal/impl/PostgreDebugController.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/debug/internal/impl/PostgreDebugController.java @@ -17,52 +17,184 @@ */ package org.jkiss.dbeaver.ext.postgresql.debug.internal.impl; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import org.jkiss.dbeaver.DBException; import org.jkiss.dbeaver.debug.DBGBaseController; import org.jkiss.dbeaver.debug.DBGException; import org.jkiss.dbeaver.debug.DBGSession; -import org.jkiss.dbeaver.debug.DBGSessionManager; import org.jkiss.dbeaver.model.DBPDataSource; import org.jkiss.dbeaver.model.DBPDataSourceContainer; +import org.jkiss.dbeaver.model.exec.DBCExecutionContext; import org.jkiss.dbeaver.model.impl.jdbc.JDBCExecutionContext; import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; +import org.jkiss.dbeaver.model.runtime.VoidProgressMonitor; public class PostgreDebugController extends DBGBaseController { - private DBGSessionManager sessionManager; + private static final String SQL_SESSION = "select pid,usename,application_name,state,query from pg_stat_activity"; //$NON-NLS-1$ + + private static final String SQL_OBJECT = "select p.oid,p.proname,u.usename as owner,n.nspname, l.lanname as lang " //$NON-NLS-1$ + + " from " + " pg_catalog.pg_namespace n " + " join pg_catalog.pg_proc p on p.pronamespace = n.oid " //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ + + " join pg_user u on u.usesysid = p.proowner " + " join pg_language l on l.oid = p. prolang " //$NON-NLS-1$ //$NON-NLS-2$ + + " where " + " l.lanname = 'plpgsql' " + " and p.proname like '%?nameCtx%' " //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ + + " and u.usename like '%?userCtx%' " + " order by " + " n.nspname,p.proname"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + + private static final String SQL_CURRENT_SESSION = "select pid,usename,application_name,state,query from pg_stat_activity where pid = pg_backend_pid()"; //$NON-NLS-1$ + + private final Map sessions = new HashMap(1); + + private DBCExecutionContext context; public PostgreDebugController(DBPDataSourceContainer dataSourceDescriptor) { super(dataSourceDescriptor); } + + @Override + protected DBGSession createSession(DBRProgressMonitor monitor, DBPDataSource dataSource) throws DBGException { + try { + this.context = dataSource.openIsolatedContext(monitor, "Debug controller"); + DBCExecutionContext sessionContext = dataSource.openIsolatedContext(monitor, "Debug session"); + return createDebugSession(sessionContext); + } catch (DBException e) { + throw new DBGException("Can't initiate debug session", e); + } + } + + @Override + public PostgreDebugSessionInfo getSessionInfo(DBCExecutionContext connectionTarget) throws DBGException { + try (Statement stmt = getConnection(connectionTarget).createStatement(); + ResultSet rs = stmt.executeQuery(SQL_CURRENT_SESSION)) { - private DBGSessionManager getSessionManager(DBRProgressMonitor monitor) throws DBGException { - if (sessionManager == null) { - try { - JDBCExecutionContext controllerContext = (JDBCExecutionContext) getDataSourceContainer().getDataSource().openIsolatedContext(monitor, "Debug controller"); - sessionManager = new PostgreDebugSessionManager(controllerContext); - } catch (Exception e) { - throw new DBGException("Can't initiate debug session manager", e); + if (rs.next()) { + int pid = rs.getInt("pid"); + String usename = rs.getString("usename"); + String applicationName = rs.getString("application_name"); + String state = rs.getString("state"); + String query = rs.getString("query"); + PostgreDebugSessionInfo res = new PostgreDebugSessionInfo(pid, usename, applicationName, state, query); + return res; } + + throw new DBGException("Error getting session"); + + } catch (SQLException e) { + throw new DBGException("SQ Lerror", e); } - return sessionManager; + + } + + private static Connection getConnection(DBCExecutionContext connectionTarget) throws SQLException { + return ((JDBCExecutionContext) connectionTarget).getConnection(new VoidProgressMonitor()); } @Override - protected DBGSession createSession(DBRProgressMonitor monitor, DBPDataSource dataSource) throws DBGException { - DBGSessionManager sessionManager = getSessionManager(monitor); - try { - JDBCExecutionContext sessionContext = (JDBCExecutionContext) getDataSourceContainer().getDataSource().openIsolatedContext(monitor, "Debug session"); - return sessionManager.createDebugSession(sessionContext); - } catch (DBException e) { - throw new DBGException("Can't initiate debug session", e); + public List getSessions() throws DBGException { + + try (Statement stmt = getConnection(context).createStatement(); ResultSet rs = stmt.executeQuery(SQL_SESSION)) { + List res = new ArrayList(); + + while (rs.next()) { + int pid = rs.getInt("pid"); + String usename = rs.getString("usename"); + String state = rs.getString("state"); + String applicationName = rs.getString("application_name"); + String query = rs.getString("query"); + PostgreDebugSessionInfo info = new PostgreDebugSessionInfo(pid, usename, applicationName, state, query); + res.add(info); + } + + return res; + + } catch (SQLException e) { + throw new DBGException("SQL error", e); } + } @Override - public void dispose() { - if (sessionManager != null) { - sessionManager.dispose(); - sessionManager = null; + public List getObjects(String ownerCtx, String nameCtx) throws DBGException { + String sql = SQL_OBJECT.replaceAll("\\?nameCtx", nameCtx).replaceAll("\\?userCtx", ownerCtx).toLowerCase(); + try (Statement stmt = getConnection(context).createStatement(); ResultSet rs = stmt.executeQuery(sql)) { + + List res = new ArrayList(); + + while (rs.next()) { + int oid = rs.getInt("oid"); + String proname = rs.getString("proname"); + String owner = rs.getString("owner"); + String nspname = rs.getString("nspname"); + String lang = rs.getString("lang"); + PostgreDebugObject object = new PostgreDebugObject(oid, proname, owner, nspname, lang); + res.add(object); + } + + return res; + + } catch (SQLException e) { + throw new DBGException("SQL error", e); } } + + @Override + public DBGSession getDebugSession(Object id) + throws DBGException { + return sessions.get(id); + } + + @Override + public PostgreDebugSession createDebugSession(DBCExecutionContext connectionTarget) throws DBGException { + + PostgreDebugSessionInfo targetInfo = getSessionInfo(connectionTarget); + + PostgreDebugSession debugSession = new PostgreDebugSession(getSessionInfo(this.context), targetInfo); + + debugSession.attach((JDBCExecutionContext) connectionTarget, 16749, -1); + //FIXME 16749 - OID for debug proc + //FIXME -1 - target PID (-1 for ANY PID) + + sessions.put(targetInfo.getPid(), debugSession); + + return debugSession; + + } + + @Override + public boolean isSessionExists(Object id) { + return sessions.containsKey(id); + } + + @Override + public void terminateSession(Object id) { + + PostgreDebugSession session = sessions.get(id); + + if (session != null) { + + session.close(); + + sessions.remove(id); + + } + + } + + @Override + public List getDebugSessions() throws DBGException { + return new ArrayList(sessions.values()); + } + + @Override + public void dispose() { + context.close(); + //FIXME: AF: perform cleanup for everything cached + } + } diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/debug/internal/impl/PostgreDebugSessionManager.java b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/debug/internal/impl/PostgreDebugSessionManager.java deleted file mode 100644 index 3315181c585f472ea307f2b78e18b803c924d2ec..0000000000000000000000000000000000000000 --- a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/debug/internal/impl/PostgreDebugSessionManager.java +++ /dev/null @@ -1,191 +0,0 @@ -/* - * DBeaver - Universal Database Manager - * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) - * Copyright (C) 2017 Andrew Khitrin (ahitrin@gmail.com) - * - * 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.impl; - -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.jkiss.dbeaver.debug.DBGException; -import org.jkiss.dbeaver.debug.DBGSession; -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") -public class PostgreDebugSessionManager implements DBGSessionManager { - - 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_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 " - + " 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%' " - + " 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 final Map sessions = new HashMap(1); - - @Override - public PostgreDebugSessionInfo getSessionInfo(DBCExecutionContext connectionTarget) throws DBGException { - try (Statement stmt = getConnection(connectionTarget).createStatement(); - ResultSet rs = stmt.executeQuery(SQL_CURRENT_SESSION)) { - - if (rs.next()) { - int pid = rs.getInt("pid"); - String usename = rs.getString("usename"); - String applicationName = rs.getString("application_name"); - String state = rs.getString("state"); - String query = rs.getString("query"); - PostgreDebugSessionInfo res = new PostgreDebugSessionInfo(pid, usename, applicationName, state, query); - return res; - } - - throw new DBGException("Error getting session"); - - } catch (SQLException e) { - throw new DBGException("SQ Lerror", e); - } - - } - - private static Connection getConnection(DBCExecutionContext connectionTarget) throws SQLException { - return ((JDBCExecutionContext) connectionTarget).getConnection(new VoidProgressMonitor()); - } - - @Override - public List getSessions() throws DBGException { - - try (Statement stmt = getConnection(context).createStatement(); ResultSet rs = stmt.executeQuery(SQL_SESSION)) { - List res = new ArrayList(); - - while (rs.next()) { - int pid = rs.getInt("pid"); - String usename = rs.getString("usename"); - String state = rs.getString("state"); - String applicationName = rs.getString("application_name"); - String query = rs.getString("query"); - PostgreDebugSessionInfo info = new PostgreDebugSessionInfo(pid, usename, applicationName, state, query); - res.add(info); - } - - return res; - - } catch (SQLException e) { - throw new DBGException("SQL error", e); - } - - } - - /** - * @param context - */ - public PostgreDebugSessionManager(DBCExecutionContext context) { - super(); - this.context = context; - } - - @Override - public List getObjects(String ownerCtx, String nameCtx) throws DBGException { - String sql = SQL_OBJECT.replaceAll("\\?nameCtx", nameCtx).replaceAll("\\?userCtx", ownerCtx).toLowerCase(); - try (Statement stmt = getConnection(context).createStatement(); ResultSet rs = stmt.executeQuery(sql)) { - - List res = new ArrayList(); - - while (rs.next()) { - int oid = rs.getInt("oid"); - String proname = rs.getString("proname"); - String owner = rs.getString("owner"); - String nspname = rs.getString("nspname"); - String lang = rs.getString("lang"); - PostgreDebugObject object = new PostgreDebugObject(oid, proname, owner, nspname, lang); - res.add(object); - } - - return res; - - } catch (SQLException e) { - throw new DBGException("SQL error", e); - } - } - - @Override - public DBGSession getDebugSession(Object id) - throws DBGException { - return sessions.get(id); - } - - @Override - public PostgreDebugSession createDebugSession(DBCExecutionContext connectionTarget) throws DBGException { - - PostgreDebugSessionInfo targetInfo = getSessionInfo(connectionTarget); - - PostgreDebugSession debugSession = new PostgreDebugSession(getSessionInfo(this.context), targetInfo); - - debugSession.attach((JDBCExecutionContext) connectionTarget, 16749, -1); - //FIXME 16749 - OID for debug proc - //FIXME -1 - target PID (-1 for ANY PID) - - sessions.put(targetInfo.getPid(), debugSession); - - return debugSession; - - } - - @Override - public boolean isSessionExists(Object id) { - return sessions.containsKey(id); - } - - @Override - public void terminateSession(Object id) { - - PostgreDebugSession session = sessions.get(id); - - if (session != null) { - - session.close(); - - sessions.remove(id); - - } - - } - - @Override - public List getDebugSessions() throws DBGException { - return new ArrayList(sessions.values()); - } - - @Override - public void dispose() { - context.close(); - //FIXME: AF: perform cleanup for everything cached - } - -}