diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/Breakpoint.java b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/Breakpoint.java index 22e8acb4b737e069692d89ca8b10b097a3f43b1d..b1d8c035e90a908c70c18aaabcb8fd59f5e1f5d5 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/Breakpoint.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/Breakpoint.java @@ -1,9 +1,28 @@ +/* + * DBeaver - Universal Database Manager + * 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.pldbg; public interface Breakpoint { - - DebugObject getObj(); - BreakpointProperties getProperties(); - void drop() throws DebugException; + + DebugObject getObj(); + + BreakpointProperties getProperties(); + + void drop() throws DebugException; } diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/BreakpointProperties.java b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/BreakpointProperties.java index 6af633a914360ee7f24842660e198e51edf13d5a..2f2a6f46671c98c8940180ea26cc7ec10a252a0e 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/BreakpointProperties.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/BreakpointProperties.java @@ -1,3 +1,20 @@ +/* + * DBeaver - Universal Database Manager + * 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.pldbg; public interface BreakpointProperties { diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/DebugException.java b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/DebugException.java index 57a1f431fdd3bf1e4cb681cadd68b0fcb929b52c..7392678f06f7393f21adea78b090895ae6348fd6 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/DebugException.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/DebugException.java @@ -14,27 +14,22 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.jkiss.dbeaver.ext.postgresql.pldbg; +package org.jkiss.dbeaver.ext.postgresql.pldbg; @SuppressWarnings("serial") public class DebugException extends Exception { - public DebugException(String message, Throwable e) - { - super(message, e); - } + public DebugException(String message, Throwable e) { + super(message, e); + } - public DebugException(String message) - { - super(message); - } + public DebugException(String message) { + super(message); + } - public DebugException(Throwable e) - { - super(e); - } + public DebugException(Throwable e) { + super(e); + } - - } diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/DebugObject.java b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/DebugObject.java index c97dcf1d08e05a200a6fc386af575c65bce63848..983112dd6a72058c2b60f141452176718f93dedd 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/DebugObject.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/DebugObject.java @@ -1,8 +1,26 @@ +/* + * DBeaver - Universal Database Manager + * 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.pldbg; public interface DebugObject { - - OBJECTID getID(); - String getName(); + + OBJECTID getID(); + + String getName(); } diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/DebugSession.java b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/DebugSession.java index 1468cef80dd8b241dffcf3f0ce52dc126add9e1c..fda84e3f9f7acabe56c3059f9e23b814d00ba7db 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/DebugSession.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/DebugSession.java @@ -1,21 +1,52 @@ +/* + * DBeaver - Universal Database Manager + * 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.pldbg; -import java.sql.Connection; import java.util.List; -public interface DebugSession,DEBUGOBJECT extends DebugObject,SESSIONID> { +public interface DebugSession, DEBUGOBJECT extends DebugObject, SESSIONID> { SESSIONINFO getSessionInfo(); + String getTitle(); + List getBreakpoints(); - Breakpoint setBreakpoint(DEBUGOBJECT obj,BreakpointProperties properties) throws DebugException; - void removeBreakpoint(Breakpoint bp); + + Breakpoint setBreakpoint(DEBUGOBJECT obj, BreakpointProperties properties) throws DebugException; + + void removeBreakpoint(Breakpoint bp) throws DebugException; + void execContinue() throws DebugException; + void execStepInto() throws DebugException; + void execStepOver() throws DebugException; - void abort(); - List> getVarables(String ctx); - void setVariableVal(Variable variable, Object value); - List getStack() throws DebugException; + + void abort() throws DebugException; + + void close(); + + List> getVarables() throws DebugException; + + void setVariableVal(Variable variable, Object value) throws DebugException; + + List getStack() throws DebugException; + SESSIONID getSessionId(); - //move Stack + + // move Stack } diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/SessionInfo.java b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/SessionInfo.java index fe0c80fd701bb62f994b0d343ebe622be6130a86..157af1b5bd829f39803879599e73b5817a82de76 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/SessionInfo.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/SessionInfo.java @@ -1,7 +1,24 @@ +/* + * DBeaver - Universal Database Manager + * 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.pldbg; public interface SessionInfo { - + SESSIONID getID(); } diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/StackFrame.java b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/StackFrame.java index e1494f825d85dd0f4c43fdfc854b4f43225192c7..7af47111f9c49b3634fd9bf978dd8e102a625f02 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/StackFrame.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/StackFrame.java @@ -1,5 +1,24 @@ +/* + * DBeaver - Universal Database Manager + * 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.pldbg; public interface StackFrame { - String getName(); + String getName(); + int getLevel(); + int getLine(); } diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/Variable.java b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/Variable.java index 538c92df1224baeeaf6a046d4f2529931fc1ddb1..b0db5f7ff3217a76b389beee638b9f8e2e63d3c5 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/Variable.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/Variable.java @@ -1,9 +1,28 @@ +/* + * DBeaver - Universal Database Manager + * 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.pldbg; public interface Variable { - - T getVal(); - String getName(); - VariableType getType(); + + T getVal(); + + String getName(); + + VariableType getType(); } diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/VariableType.java b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/VariableType.java index 0fbed834097983acfc5eaa2dedf0ae4dc122ffc1..5fa2679952bdaab87175336b0383dda11677aece 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/VariableType.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/VariableType.java @@ -1,3 +1,22 @@ +/* + * DBeaver - Universal Database Manager + * 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.pldbg; -public enum VariableType { NUMBER, TEXT, DATE, BLOB } +public enum VariableType { + NUMBER, TEXT, DATE, BLOB +} diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/console/Debugger.java b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/console/Debugger.java index 1234fc4f197fb051c5747950f89380e7c1ad704e..71c9c1164cf00e61497420eaaa00685d05ebafe5 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/console/Debugger.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/console/Debugger.java @@ -1,3 +1,20 @@ +/* + * DBeaver - Universal Database Manager + * 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.pldbg.console; import java.sql.Connection; @@ -6,9 +23,11 @@ import java.sql.SQLException; import java.util.List; import java.util.Scanner; +import org.jkiss.dbeaver.ext.postgresql.pldbg.Breakpoint; import org.jkiss.dbeaver.ext.postgresql.pldbg.DebugException; import org.jkiss.dbeaver.ext.postgresql.pldbg.DebugSession; import org.jkiss.dbeaver.ext.postgresql.pldbg.StackFrame; +import org.jkiss.dbeaver.ext.postgresql.pldbg.Variable; import org.jkiss.dbeaver.ext.postgresql.pldbg.impl.BreakpointPropertiesPostgres; import org.jkiss.dbeaver.ext.postgresql.pldbg.impl.DebugManagerPostgres; import org.jkiss.dbeaver.ext.postgresql.pldbg.impl.DebugObjectPostgres; @@ -16,465 +35,605 @@ import org.jkiss.dbeaver.ext.postgresql.pldbg.impl.DebugSessionPostgres; import org.jkiss.dbeaver.ext.postgresql.pldbg.impl.PostgresBreakpoint; import org.jkiss.dbeaver.ext.postgresql.pldbg.impl.SessionInfoPostgres; - @SuppressWarnings("nls") public class Debugger { - - - /* - * * проверить стек вызова (pldbg_get_stack), -* перемещаться по функциям в стеке (pldbg_select_frame), -* смотреть значения параметров и локальных переменных текущей функции в стеке - (pldbg_get_variables), -* изменить значения переменных (pldbg_deposit_value), -* установить/удалить точки останова - (pldbg_set_breakpoint/pldbg_drop_breakpoint), -* продолжить выполнение до следующей точки (pldbg_continue), -* выполнять пошаговую отладку с заходом внутрь функций (pldbg_step_into), -* или выполняя строку целиком (pldbg_step_over), -* в конечном итоге, прекратить сеанс отладки (pldbg_abort_target). - */ - - public static final String PROMPT = ">"; - public static final String COMMAND_ATTACH = "A"; - public static final String COMMAND_STACK = "S"; - public static final String COMMAND_FRAME = "F"; - public static final String COMMAND_VARIABLES = "V"; - public static final String COMMAND_VARIABLE_SET = "="; - public static final String COMMAND_BREAKPOINT = "B"; - public static final String COMMAND_BREAKPOINT_LIST = "L"; - public static final String COMMAND_CONTINUE = "C"; - public static final String COMMAND_INTO = "I"; - public static final String COMMAND_OVER = "O"; - public static final String COMMAND_TERMINATE = "E"; - public static final String COMMAND_SESSIONS = "W"; - public static final String COMMAND_OBJ = "D"; - public static final String COMMAND_NEW = "N"; - public static final String COMMAND_DEBUG_LIST = "Q"; - public static final String COMMAND_HELP = "?"; - - public static final String ANY_ARG = "*"; - - public static DebugSessionPostgres chooseSession(Scanner sc,DebugManagerPostgres pgDbgManager) throws DebugException{ - - DebugSessionPostgres debugSession = null; - - List> sessions = pgDbgManager.getDebugSessions(); - - Scanner scArg; - - if (sessions.size() == 1) { - - debugSession = (DebugSessionPostgres) sessions.get(0); - - } else { - - System.out.println("Choose debug session (0 for quit) :"); - - int sessNo = 1; - - for (DebugSession s : pgDbgManager.getDebugSessions()) { - System.out.println(String.format(" (%d) %s", sessNo++,s.toString())); - } - - - int sessionId = -1; - - - - while (sessionId <0) { - - String argc =sc.nextLine(); - - String strSessionid = ""; - - scArg = new Scanner(argc); - - if (scArg.hasNext()) { - - strSessionid = scArg.next(); - - if (strSessionid.trim().length() > 0) { - - try { - - sessionId = Integer.valueOf(strSessionid); - - } catch (Exception e) { - System.out.println(String.format("Incorrect session ID %s", strSessionid)); - sessionId = -1; - } - - if (sessionId ==0) { - break; - } - - if (sessionId > sessions.size()) { - System.out.println(String.format("Incorrect session ID %s", strSessionid)); - sessionId = -1; - } else { - debugSession = (DebugSessionPostgres) sessions.get(sessionId -1); - break; - } - } - - } - scArg.close(); - - } - - } - - return debugSession; - - } - - public static void main(String[] args) throws DebugException { - - 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"; - - Connection conn; - DebugManagerPostgres pgDbgManager; - try { - - conn = DriverManager.getConnection(url); - - } catch (SQLException e) { - e.printStackTrace(); - return; - } - - pgDbgManager = new DebugManagerPostgres(conn); - - Scanner sc = new Scanner(System.in); - Scanner scArg; - String command; - while(true) { - System.out.print(PROMPT); - command = sc.next(); - switch (command.toUpperCase()) { - - case COMMAND_HELP: - System.out.println("W Show sessions"); - System.out.println("N New session"); - System.out.println("D Show debug objects"); - System.out.println("S Stack"); - System.out.println("F Frame"); - System.out.println("V Variables"); - System.out.println("= Set Variables"); - System.out.println("L List breakpoint(s)"); - System.out.println("Q List debug session(s)"); - System.out.println("B Set breakpoint"); - System.out.println("C Continue execution"); - System.out.println("I Step into"); - System.out.println("O Step over"); - System.out.println("E Exit debugger"); - System.out.println("? This help"); - break; - - case COMMAND_STACK: - - - if ( pgDbgManager.getDebugSessions().size() == 0) { - System.out.println("Debug sessions not found"); - break; - } - - DebugSessionPostgres debugSessionSL = chooseSession(sc,pgDbgManager); - - if (debugSessionSL == null) { - break; - } - - List stack = debugSessionSL.getStack(); - - if (stack.size() == 0) { - System.out.println("No stack defined"); - } - - for(StackFrame s : stack) { - System.out.println(s.toString()); - } - - break; - - case COMMAND_FRAME: - System.out.println("FRAME!!!"); - break; - - case COMMAND_VARIABLES: - System.out.println("VARIABLES!!!"); - break; - - case COMMAND_VARIABLE_SET: - System.out.println("VARIABLE_SET!!!"); - break; - - case COMMAND_BREAKPOINT: - - String strObjId = ANY_ARG; - String strLineNo= ANY_ARG; - - String argc =sc.nextLine(); - - if (argc.length() > 0) { - - scArg = new Scanner(argc); - - if (scArg.hasNext()) { - - strObjId = scArg.next(); - - if (scArg.hasNext()) { - - argc = scArg.nextLine(); - - if (argc.length() > 0) { - strLineNo = argc; - } - - } - - } - scArg.close(); - - } - - int objId = -1; - - try { - - objId = Integer.valueOf(strObjId.trim()); - - } catch (Exception e) { - System.out.println(String.format("Incorrect object ID '%s'", strObjId)); - break; - } - - int lineNo = -1; - - if (strLineNo.trim().length() > 0) { - - try { - - lineNo = Integer.valueOf(strLineNo.trim()); - - } catch (Exception e) { - System.out.println(String.format("Incorrect line number '%s'", strLineNo)); - break; - } - - } - - DebugObjectPostgres debugObject = null; - - for (DebugObjectPostgres o : pgDbgManager.getObjects("_","_")) { - if (o.getID() == objId) { - debugObject = o; - } - } - - if (debugObject == null) { - System.out.println(String.format("Object ID '%s' no found", strObjId)); - break; - } - - if ( pgDbgManager.getDebugSessions().size() == 0) { - System.out.println("Debug sessions not found"); - break; - } - - DebugSessionPostgres debugSession = chooseSession(sc,pgDbgManager); - - if (debugSession == null) { - break; - } - - BreakpointPropertiesPostgres bpp = lineNo > 0 ? new BreakpointPropertiesPostgres(lineNo,true) : new BreakpointPropertiesPostgres(true); - - PostgresBreakpoint bp = (PostgresBreakpoint) debugSession.setBreakpoint(debugObject, bpp); - - System.out.println("Breakpoint set"); - - System.out.println(bp.toString()); - - break; - - case COMMAND_BREAKPOINT_LIST: - if ( pgDbgManager.getDebugSessions().size() == 0) { - System.out.println("Debug sessions not found"); - break; - } - - DebugSessionPostgres debugSessionBL = chooseSession(sc,pgDbgManager); - - if (debugSessionBL == null) { - break; - } - - if (debugSessionBL.getBreakpoints().size() == 0) { - System.out.println("No breakpoints defined"); - } - - for(PostgresBreakpoint bpl : debugSessionBL.getBreakpoints()) { - System.out.println(bpl.toString()); - } - - break; - - case COMMAND_CONTINUE: - if ( pgDbgManager.getDebugSessions().size() == 0) { - System.out.println("Debug sessions not found"); - break; - } - - DebugSessionPostgres debugSessionSC = chooseSession(sc,pgDbgManager); - - if (debugSessionSC == null) { - break; - } - - debugSessionSC.execContinue(); - - System.out.println("Continue ..."); - - break; - - case COMMAND_INTO: - if ( pgDbgManager.getDebugSessions().size() == 0) { - System.out.println("Debug sessions not found"); - break; - } - - DebugSessionPostgres debugSessionSI = chooseSession(sc,pgDbgManager); - - if (debugSessionSI == null) { - break; - } - - debugSessionSI.execStepInto(); - - System.out.println("Step Into ..."); - - break; - - case COMMAND_OVER: - - if ( pgDbgManager.getDebugSessions().size() == 0) { - System.out.println("Debug sessions not found"); - break; - } - - DebugSessionPostgres debugSessionSO = chooseSession(sc,pgDbgManager); - - if (debugSessionSO == null) { - break; - } - - debugSessionSO.execStepOver(); - - System.out.println("Step over ..."); - - break; - - case COMMAND_SESSIONS: - for (SessionInfoPostgres s : pgDbgManager.getSessions()) { - System.out.println(s); - } - break; - - case COMMAND_DEBUG_LIST: - if (pgDbgManager.getDebugSessions().size()==0){ - System.out.println("no debug sessions"); - break; - } - for (DebugSession s : pgDbgManager.getDebugSessions()) { - System.out.println(s); - } - - break; - - case COMMAND_NEW: - try { - Connection debugConn = DriverManager.getConnection(url); - DebugSessionPostgres s = pgDbgManager.createDebugSession(debugConn); - System.out.println("created"); - System.out.println(s); - - } catch (SQLException e) { - e.printStackTrace(); - break; - } - break; - - case COMMAND_OBJ: - String proc = ANY_ARG; - String owner= ANY_ARG; - - String arg =sc.nextLine(); - - if (arg.length() > 0) { - - scArg = new Scanner(arg); - - if (scArg.hasNext()) { - - proc = scArg.next(); - - if (scArg.hasNext()) { - - arg = scArg.nextLine(); - - if (arg.length() > 0) { - owner = arg; - } - - } - - } - scArg.close(); - - } - - - for (DebugObjectPostgres o : pgDbgManager.getObjects(owner.equals(ANY_ARG) ? "_" : owner, proc.equals(ANY_ARG) ? "_" : proc)) { - System.out.println(o); - } - - break; - - case COMMAND_ATTACH: - if ( pgDbgManager.getDebugSessions().size() == 0) { - System.out.println("Debug sessions not found"); - break; - } - - DebugSessionPostgres debugSessionA = chooseSession(sc,pgDbgManager); - - if (debugSessionA == null) { - break; - } - - - System.out.println("Waiting for target session ..."); - - debugSessionA.attach(false); - - break; - - case COMMAND_TERMINATE: - System.out.println("EXIT....."); - return; - - default: - System.out.println(String.format("Unnown command '%s' for command list type ?", command)); - break; - } - /*while(sc.hasNext()) { - - }*/ - } - - } + + public static final String PROMPT = ">"; + public static final String COMMAND_ATTACH = "A"; + public static final String COMMAND_ABORT = "T"; + public static final String COMMAND_CLOSE = "X"; + public static final String COMMAND_STACK = "S"; + public static final String COMMAND_FRAME = "F"; + public static final String COMMAND_VARIABLES = "V"; + public static final String COMMAND_VARIABLE_SET = "="; + public static final String COMMAND_BREAKPOINT = "B"; + public static final String COMMAND_BREAKPOINT_REMOVE = "R"; + public static final String COMMAND_BREAKPOINT_LIST = "L"; + public static final String COMMAND_CONTINUE = "C"; + public static final String COMMAND_INTO = "I"; + public static final String COMMAND_OVER = "O"; + public static final String COMMAND_TERMINATE = "E"; + public static final String COMMAND_SESSIONS = "W"; + public static final String COMMAND_OBJ = "D"; + public static final String COMMAND_NEW = "N"; + public static final String COMMAND_DEBUG_LIST = "Q"; + public static final String COMMAND_HELP = "?"; + + public static final String ANY_ARG = "*"; + + public static PostgresBreakpoint chooseBreakpoint(Scanner sc, DebugManagerPostgres pgDbgManager, + DebugSessionPostgres session) throws DebugException { + + PostgresBreakpoint bp = null; + + List bps = session.getBreakpoints(); + + Scanner scArg; + + if (bps.size() == 1) { + + bp = (PostgresBreakpoint) bps.get(0); + + } else { + + System.out.println("Choose breakpoint (0 for quit) :"); + + int bpNo = 1; + + for (PostgresBreakpoint b : bps) { + System.out.println(String.format(" (%d) %s", bpNo++, b.toString())); + } + + int bpId = -1; + + while (bpId < 0) { + + String argc = sc.nextLine(); + + String strBpid = ""; + + scArg = new Scanner(argc); + + if (scArg.hasNext()) { + + strBpid = scArg.next(); + + if (strBpid.trim().length() > 0) { + + try { + + bpId = Integer.valueOf(strBpid); + + } catch (Exception e) { + System.out.println(String.format("Incorrect session ID %s", strBpid)); + bpId = -1; + } + + if (bpId == 0) { + break; + } + + if (bpId > bps.size()) { + System.out.println(String.format("Incorrect breakpoint ID %s", strBpid)); + bpId = -1; + } else { + bp = bps.get(bpId - 1); + break; + } + } + + } + scArg.close(); + + } + + } + + return bp; + + } + + public static DebugSessionPostgres chooseSession(Scanner sc, DebugManagerPostgres pgDbgManager) + throws DebugException { + + DebugSessionPostgres debugSession = null; + + List> sessions = pgDbgManager.getDebugSessions(); + + Scanner scArg; + + if (sessions.size() == 1) { + + debugSession = (DebugSessionPostgres) sessions.get(0); + + } else { + + System.out.println("Choose debug session (0 for quit) :"); + + int sessNo = 1; + + for (DebugSession s : sessions) { + System.out.println(String.format(" (%d) %s", sessNo++, s.toString())); + } + + int sessionId = -1; + + while (sessionId < 0) { + + String argc = sc.nextLine(); + + String strSessionid = ""; + + scArg = new Scanner(argc); + + if (scArg.hasNext()) { + + strSessionid = scArg.next(); + + if (strSessionid.trim().length() > 0) { + + try { + + sessionId = Integer.valueOf(strSessionid); + + } catch (Exception e) { + System.out.println(String.format("Incorrect session ID %s", strSessionid)); + sessionId = -1; + } + + if (sessionId == 0) { + break; + } + + if (sessionId > sessions.size()) { + System.out.println(String.format("Incorrect session ID %s", strSessionid)); + sessionId = -1; + } else { + debugSession = (DebugSessionPostgres) sessions.get(sessionId - 1); + break; + } + } + + } + scArg.close(); + + } + + } + + return debugSession; + + } + + public static void main(String[] args) throws DebugException { + + 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"; + + Connection conn; + DebugManagerPostgres pgDbgManager; + try { + + conn = DriverManager.getConnection(url); + + } catch (SQLException e) { + e.printStackTrace(); + return; + } + + pgDbgManager = new DebugManagerPostgres(conn); + + Scanner sc = new Scanner(System.in); + Scanner scArg; + String command; + while (true) { + System.out.print(PROMPT); + command = sc.next(); + switch (command.toUpperCase()) { + + case COMMAND_HELP: + System.out.println("W Show sessions"); + System.out.println("N New session"); + System.out.println("D Show debug objects"); + System.out.println("S Stack"); + System.out.println("F Frame"); + System.out.println("V Variables"); + System.out.println("= Set Variables"); + System.out.println("L List breakpoint(s)"); + System.out.println("Q List debug session(s)"); + System.out.println("B Set breakpoint"); + System.out.println("R Remove breakpoint"); + System.out.println("C Continue execution"); + System.out.println("I Step into"); + System.out.println("O Step over"); + System.out.println("X Close session"); + System.out.println("T Abort session"); + System.out.println("E Exit debugger"); + System.out.println("? This help"); + break; + + case COMMAND_CLOSE: + + if (pgDbgManager.getDebugSessions().size() == 0) { + System.out.println("Debug sessions not found"); + break; + } + + DebugSessionPostgres debugSessionC = chooseSession(sc, pgDbgManager); + + if (debugSessionC == null) { + break; + } + + pgDbgManager.terminateSession(debugSessionC.getSessionInfo().getPid()); + + System.out.println("Session closed"); + + break; + + case COMMAND_ABORT: + + if (pgDbgManager.getDebugSessions().size() == 0) { + System.out.println("Debug sessions not found"); + break; + } + + DebugSessionPostgres debugSessionAB = chooseSession(sc, pgDbgManager); + + if (debugSessionAB == null) { + break; + } + + debugSessionAB.abort(); + + System.out.println("Aborted."); + + break; + + case COMMAND_STACK: + + if (pgDbgManager.getDebugSessions().size() == 0) { + System.out.println("Debug sessions not found"); + break; + } + + DebugSessionPostgres debugSessionSL = chooseSession(sc, pgDbgManager); + + if (debugSessionSL == null) { + break; + } + + List stack = debugSessionSL.getStack(); + + if (stack.size() == 0) { + System.out.println("No stack defined"); + } + + for (StackFrame s : stack) { + System.out.println(s.toString()); + } + + break; + + case COMMAND_FRAME: + System.out.println("FRAME!!!"); + break; + + case COMMAND_VARIABLES: + + if (pgDbgManager.getDebugSessions().size() == 0) { + System.out.println("Debug sessions not found"); + break; + } + + DebugSessionPostgres debugSessionVL = chooseSession(sc, pgDbgManager); + + if (debugSessionVL == null) { + break; + } + + List> vars = debugSessionVL.getVarables(); + + if (vars.size() == 0) { + System.out.println("No vars defined"); + } + + for (Variable v : vars) { + System.out.println(v.toString()); + } + + break; + + case COMMAND_VARIABLE_SET: + System.out.println("VARIABLE_SET!!!"); + break; + + case COMMAND_BREAKPOINT: + + String strObjId = ANY_ARG; + String strLineNo = ANY_ARG; + + String argc = sc.nextLine(); + + if (argc.length() > 0) { + + scArg = new Scanner(argc); + + if (scArg.hasNext()) { + + strObjId = scArg.next(); + + if (scArg.hasNext()) { + + argc = scArg.nextLine(); + + if (argc.length() > 0) { + strLineNo = argc; + } + + } + + } + scArg.close(); + + } + + int objId = -1; + + try { + + objId = Integer.valueOf(strObjId.trim()); + + } catch (Exception e) { + System.out.println(String.format("Incorrect object ID '%s'", strObjId)); + break; + } + + int lineNo = -1; + + if (strLineNo.trim().length() > 0) { + + try { + + lineNo = Integer.valueOf(strLineNo.trim()); + + } catch (Exception e) { + System.out.println(String.format("Incorrect line number '%s'", strLineNo)); + break; + } + + } + + DebugObjectPostgres debugObject = null; + + for (DebugObjectPostgres o : pgDbgManager.getObjects("_", "_")) { + if (o.getID() == objId) { + debugObject = o; + } + } + + if (debugObject == null) { + System.out.println(String.format("Object ID '%s' no found", strObjId)); + break; + } + + if (pgDbgManager.getDebugSessions().size() == 0) { + System.out.println("Debug sessions not found"); + break; + } + + DebugSessionPostgres debugSession = chooseSession(sc, pgDbgManager); + + if (debugSession == null) { + break; + } + + BreakpointPropertiesPostgres bpp = lineNo > 0 ? new BreakpointPropertiesPostgres(lineNo, true) + : new BreakpointPropertiesPostgres(true); + + PostgresBreakpoint bp = (PostgresBreakpoint) debugSession.setBreakpoint(debugObject, bpp); + + System.out.println("Breakpoint set"); + + System.out.println(bp.toString()); + + break; + + case COMMAND_BREAKPOINT_LIST: + if (pgDbgManager.getDebugSessions().size() == 0) { + System.out.println("Debug sessions not found"); + break; + } + + DebugSessionPostgres debugSessionBL = chooseSession(sc, pgDbgManager); + + if (debugSessionBL == null) { + break; + } + + if (debugSessionBL.getBreakpoints().size() == 0) { + System.out.println("No breakpoints defined"); + } + + for (PostgresBreakpoint bpl : debugSessionBL.getBreakpoints()) { + System.out.println(bpl.toString()); + } + + break; + + case COMMAND_BREAKPOINT_REMOVE: + if (pgDbgManager.getDebugSessions().size() == 0) { + System.out.println("Debug sessions not found"); + break; + } + + DebugSessionPostgres debugSessionBR = chooseSession(sc, pgDbgManager); + + if (debugSessionBR == null) { + break; + } + + if (debugSessionBR.getBreakpoints().size() == 0) { + System.out.println("No breakpoints defined"); + } + + PostgresBreakpoint bpr = chooseBreakpoint(sc, pgDbgManager, debugSessionBR); + + debugSessionBR.removeBreakpoint(bpr); + + System.out.println("Breakpoint removed ..."); + + break; + + case COMMAND_CONTINUE: + if (pgDbgManager.getDebugSessions().size() == 0) { + System.out.println("Debug sessions not found"); + break; + } + + DebugSessionPostgres debugSessionSC = chooseSession(sc, pgDbgManager); + + if (debugSessionSC == null) { + break; + } + + debugSessionSC.execContinue(); + + System.out.println("Continue ..."); + + break; + + case COMMAND_INTO: + if (pgDbgManager.getDebugSessions().size() == 0) { + System.out.println("Debug sessions not found"); + break; + } + + DebugSessionPostgres debugSessionSI = chooseSession(sc, pgDbgManager); + + if (debugSessionSI == null) { + break; + } + + debugSessionSI.execStepInto(); + + System.out.println("Step Into ..."); + + break; + + case COMMAND_OVER: + + if (pgDbgManager.getDebugSessions().size() == 0) { + System.out.println("Debug sessions not found"); + break; + } + + DebugSessionPostgres debugSessionSO = chooseSession(sc, pgDbgManager); + + if (debugSessionSO == null) { + break; + } + + debugSessionSO.execStepOver(); + + System.out.println("Step over ..."); + + break; + + case COMMAND_SESSIONS: + for (SessionInfoPostgres s : pgDbgManager.getSessions()) { + System.out.println(s); + } + break; + + case COMMAND_DEBUG_LIST: + if (pgDbgManager.getDebugSessions().size() == 0) { + System.out.println("no debug sessions"); + break; + } + for (DebugSession s : pgDbgManager.getDebugSessions()) { + System.out.println(s); + } + + break; + + case COMMAND_NEW: + try { + Connection debugConn = DriverManager.getConnection(url); + DebugSessionPostgres s = pgDbgManager.createDebugSession(debugConn); + System.out.println("created"); + System.out.println(s); + + } catch (SQLException e) { + e.printStackTrace(); + break; + } + break; + + case COMMAND_OBJ: + String proc = ANY_ARG; + String owner = ANY_ARG; + + String arg = sc.nextLine(); + + if (arg.length() > 0) { + + scArg = new Scanner(arg); + + if (scArg.hasNext()) { + + proc = scArg.next(); + + if (scArg.hasNext()) { + + arg = scArg.nextLine(); + + if (arg.length() > 0) { + owner = arg; + } + + } + + } + scArg.close(); + + } + + for (DebugObjectPostgres o : pgDbgManager.getObjects(owner.equals(ANY_ARG) ? "_" : owner, + proc.equals(ANY_ARG) ? "_" : proc)) { + System.out.println(o); + } + + break; + + case COMMAND_ATTACH: + if (pgDbgManager.getDebugSessions().size() == 0) { + System.out.println("Debug sessions not found"); + break; + } + + DebugSessionPostgres debugSessionA = chooseSession(sc, pgDbgManager); + + if (debugSessionA == null) { + break; + } + + System.out.println("Waiting for target session ..."); + + debugSessionA.attach(false); + + break; + + case COMMAND_TERMINATE: + System.out.println("EXIT....."); + return; + + default: + System.out.println(String.format("Unnown command '%s' for command list type ?", command)); + break; + } + + } + + } } diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/BreakpointPropertiesPostgres.java b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/BreakpointPropertiesPostgres.java index 7793b9b438c393b4972a92792f8aee751af349c7..192c2acc82dc08ce684ef6bfed103ec08a030de2 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/BreakpointPropertiesPostgres.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/BreakpointPropertiesPostgres.java @@ -1,75 +1,80 @@ +/* + * DBeaver - Universal Database Manager + * 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.pldbg.impl; import org.jkiss.dbeaver.ext.postgresql.pldbg.BreakpointProperties; public class BreakpointPropertiesPostgres implements BreakpointProperties { - - private final long lineNo; - private final boolean onStart; - private final long targetId; - private final boolean all; - private final boolean global; - - - public long getLineNo() { - return lineNo; - } - - - - public boolean isOnStart() { - return onStart; - } - - - - public long getTargetId() { - return targetId; - } - - - public boolean isAll() { - return all; - } + private final long lineNo; + private final boolean onStart; + private final long targetId; + private final boolean all; + private final boolean global; + + public long getLineNo() { + return lineNo; + } + + public boolean isOnStart() { + return onStart; + } + + public long getTargetId() { + return targetId; + } + + public boolean isAll() { + return all; + } + + public boolean isGlobal() { + return global; + } + + public BreakpointPropertiesPostgres(long lineNo, long targetId, boolean global) { + this.lineNo = lineNo; + this.onStart = lineNo < 0; + this.targetId = targetId; + this.all = targetId < 0; + this.global = global; + } + + public BreakpointPropertiesPostgres(long lineNo, boolean global) { + this.lineNo = lineNo; + this.onStart = lineNo < 0; + this.targetId = -1; + this.all = true; + this.global = global; + } + + public BreakpointPropertiesPostgres(boolean global) { + this.lineNo = -1; + this.onStart = true; + this.targetId = -1; + this.all = true; + this.global = global; + } + + @Override + public String toString() { + return "BreakpointPropertiesPostgres [lineNo=" + lineNo + ", onStart=" + onStart + ", targetId=" + targetId + + ", all=" + all + ", global=" + global + "]"; + } - public boolean isGlobal() { - return global; - } - - public BreakpointPropertiesPostgres(long lineNo, long targetId,boolean global) { - this.lineNo = lineNo; - this.onStart = lineNo < 0; - this.targetId = targetId; - this.all = targetId < 0; - this.global = global; - } - - public BreakpointPropertiesPostgres(long lineNo,boolean global) { - this.lineNo = lineNo; - this.onStart = lineNo < 0; - this.targetId = -1; - this.all = true; - this.global = global; - } - - public BreakpointPropertiesPostgres(boolean global) { - this.lineNo = -1; - this.onStart = true; - this.targetId = -1; - this.all = true; - this.global = global; - } - - - - @Override - public String toString() { - return "BreakpointPropertiesPostgres [lineNo=" + lineNo + ", onStart=" + onStart + ", targetId=" + targetId - + ", all=" + all + ", global=" + global + "]"; - } - - - } - diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/DebugManagerPostgres.java b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/DebugManagerPostgres.java index 25931501919aca6e339313ace28fc6aa17f5dd85..5384e499a34b1843278ffdab3fe5ea1d042716a0 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/DebugManagerPostgres.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/DebugManagerPostgres.java @@ -27,179 +27,144 @@ import java.util.List; import java.util.Map; import org.jkiss.dbeaver.ext.postgresql.pldbg.DebugException; -import org.jkiss.dbeaver.ext.postgresql.pldbg.DebugObject; import org.jkiss.dbeaver.ext.postgresql.pldbg.DebugSession; -import org.jkiss.dbeaver.ext.postgresql.pldbg.SessionInfo; import org.jkiss.dbeaver.postgresql.pldbg.control.DebugManager; -/** - * @author Andrey.Hitrin - * - */ @SuppressWarnings("nls") -public class DebugManagerPostgres implements DebugManager { - - private final Connection connection; - - 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 SessionInfoPostgres getSessionInfo(Connection connectionTarget) throws DebugException - { - try (Statement stmt = connectionTarget.createStatement()) { - - ResultSet rs = stmt.executeQuery(SQL_CURRENT_SESSION); - - if (rs.next()) { - - SessionInfoPostgres res = new SessionInfoPostgres( - rs.getInt("pid") , - rs.getString("usename"), - rs.getString("application_name"), - rs.getString("state"), - rs.getString("query")); - return res; - } - - throw new DebugException("Error getting session"); - - - } catch (SQLException e) { - throw new DebugException(e); - } - - } - - - - @Override - public List getSessions() throws DebugException - { - - try (Statement stmt = connection.createStatement()) { - - ResultSet rs = stmt.executeQuery(SQL_SESSION); - - List res = new ArrayList(); - - while (rs.next()) { - - res.add(new SessionInfoPostgres( - rs.getInt("pid") , - rs.getString("usename"), - rs.getString("application_name"), - rs.getString("state"), - rs.getString("query"))); - } - - return res; - - } catch (SQLException e) { - throw new DebugException(e); - } - - } - - - - /** - * @param connection - */ - public DebugManagerPostgres(Connection connection) - { - super(); - this.connection = connection; - } - - @Override - public List getObjects(String ownerCtx, - String nameCtx) throws DebugException - { - try (Statement stmt = connection.createStatement()) { - - - ResultSet rs = stmt.executeQuery(SQL_OBJECT.replaceAll("\\?nameCtx", nameCtx).replaceAll("\\?userCtx", ownerCtx).toLowerCase()); - - List res = new ArrayList(); - - while (rs.next()) { - - res.add(new DebugObjectPostgres( - rs.getInt("oid") , - rs.getString("proname"), - rs.getString("owner"), - rs.getString("nspname"), - rs.getString("lang"))); - - } - - return res; - - } catch (SQLException e) { - throw new DebugException(e); - } - } - - @Override - public DebugSession getDebugSession( - Integer id) throws DebugException - { - return sessions.get(id); - } - - @Override - public DebugSessionPostgres createDebugSession(Connection connectionTarget) throws DebugException - { - - SessionInfoPostgres targetInfo = getSessionInfo(connectionTarget); - - DebugSessionPostgres debugSession = new DebugSessionPostgres(getSessionInfo(this.connection),targetInfo ,connectionTarget); - - sessions.put(targetInfo.getPid(), debugSession); - - return debugSession; - - } - - @Override - public boolean isSessionExists(Integer id) - { - return sessions.containsKey(id); - } - - - - @Override - public void terminateSession(Integer id) { - // TODO Auto-generated method stub - - } - - - - @Override - public List> getDebugSessions() throws DebugException { - return new ArrayList>(sessions.values()); - } - - - +public class DebugManagerPostgres implements DebugManager { + + private final Connection connection; + + 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 SessionInfoPostgres getSessionInfo(Connection connectionTarget) throws DebugException { + try (Statement stmt = connectionTarget.createStatement()) { + + ResultSet rs = stmt.executeQuery(SQL_CURRENT_SESSION); + + if (rs.next()) { + + SessionInfoPostgres res = new SessionInfoPostgres(rs.getInt("pid"), rs.getString("usename"), + rs.getString("application_name"), rs.getString("state"), rs.getString("query")); + return res; + } + + throw new DebugException("Error getting session"); + + } catch (SQLException e) { + throw new DebugException(e); + } + + } + + @Override + public List getSessions() throws DebugException { + + try (Statement stmt = connection.createStatement()) { + + ResultSet rs = stmt.executeQuery(SQL_SESSION); + + List res = new ArrayList(); + + while (rs.next()) { + + res.add(new SessionInfoPostgres(rs.getInt("pid"), rs.getString("usename"), + rs.getString("application_name"), rs.getString("state"), rs.getString("query"))); + } + + return res; + + } catch (SQLException e) { + throw new DebugException(e); + } + + } + + /** + * @param connection + */ + public DebugManagerPostgres(Connection connection) { + super(); + this.connection = connection; + } + + @Override + public List getObjects(String ownerCtx, String nameCtx) throws DebugException { + try (Statement stmt = connection.createStatement()) { + + ResultSet rs = stmt.executeQuery( + SQL_OBJECT.replaceAll("\\?nameCtx", nameCtx).replaceAll("\\?userCtx", ownerCtx).toLowerCase()); + + List res = new ArrayList(); + + while (rs.next()) { + + res.add(new DebugObjectPostgres(rs.getInt("oid"), rs.getString("proname"), rs.getString("owner"), + rs.getString("nspname"), rs.getString("lang"))); + + } + + return res; + + } catch (SQLException e) { + throw new DebugException(e); + } + } + + @Override + public DebugSession getDebugSession(Integer id) + throws DebugException { + return sessions.get(id); + } + + @Override + public DebugSessionPostgres createDebugSession(Connection connectionTarget) throws DebugException { + + SessionInfoPostgres targetInfo = getSessionInfo(connectionTarget); + + DebugSessionPostgres debugSession = new DebugSessionPostgres(getSessionInfo(this.connection), targetInfo, + connectionTarget); + + sessions.put(targetInfo.getPid(), debugSession); + + return debugSession; + + } + + @Override + public boolean isSessionExists(Integer id) { + return sessions.containsKey(id); + } + + @Override + public void terminateSession(Integer id) { + + DebugSessionPostgres session = sessions.get(id); + + if (session != null) { + + session.close(); + + sessions.remove(id); + + } + + } + + @Override + public List> getDebugSessions() throws DebugException { + return new ArrayList>(sessions.values()); + } } diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/DebugObjectPostgres.java b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/DebugObjectPostgres.java index fcbab0055604c73396a80ac42090c76f56af8aa7..12a95f67b84a2759f590ab5140d9bdbc7cdcdee1 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/DebugObjectPostgres.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/DebugObjectPostgres.java @@ -14,74 +14,59 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.jkiss.dbeaver.ext.postgresql.pldbg.impl; import org.jkiss.dbeaver.ext.postgresql.pldbg.DebugObject; @SuppressWarnings("nls") public class DebugObjectPostgres implements DebugObject { - - private final Integer oid; - - private final String proname; - - private final String owner; - - private final String schema; - - private final String lang; - - /** - * @param oid - * @param proname - * @param owner - * @param schema - * @param lang - */ - public DebugObjectPostgres(Integer oid, String proname, String owner, - String schema, String lang) - { - super(); - this.oid = oid; - this.proname = proname; - this.owner = owner; - this.schema = schema; - this.lang = lang; - } - - public String getOwner() - { - return owner; - } - - public String getSchema() - { - return schema; - } - - public String getLang() - { - return lang; - } - - @Override - public Integer getID() - { - return oid; - } - - @Override - public String getName() - { - return proname; - } - - @Override - public String toString() - { - return "id:" + String.valueOf(oid) + ", name: `"+ proname + "("+lang+")" + ", user: " + owner +"(" + schema + ")"; - } - - - + + private final Integer oid; + + private final String proname; + + private final String owner; + + private final String schema; + + private final String lang; + + public DebugObjectPostgres(Integer oid, String proname, String owner, String schema, String lang) { + super(); + this.oid = oid; + this.proname = proname; + this.owner = owner; + this.schema = schema; + this.lang = lang; + } + + public String getOwner() { + return owner; + } + + public String getSchema() { + return schema; + } + + public String getLang() { + return lang; + } + + @Override + public Integer getID() { + return oid; + } + + @Override + public String getName() { + return proname; + } + + @Override + public String toString() { + return "id:" + String.valueOf(oid) + ", name: `" + proname + "(" + lang + ")" + ", user: " + owner + "(" + + schema + ")"; + } + } diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/DebugSessionPostgres.java b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/DebugSessionPostgres.java index e0b29893e091db80793f0715354cd4b657bc4c90..280a487a4495a7a19453418ca12ba3ac11f4ea5e 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/DebugSessionPostgres.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/DebugSessionPostgres.java @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.jkiss.dbeaver.ext.postgresql.pldbg.impl; import java.sql.Connection; @@ -33,388 +34,409 @@ import org.jkiss.dbeaver.ext.postgresql.pldbg.DebugSession; import org.jkiss.dbeaver.ext.postgresql.pldbg.StackFrame; import org.jkiss.dbeaver.ext.postgresql.pldbg.Variable; - @SuppressWarnings("nls") -public class DebugSessionPostgres implements DebugSession { - - private final SessionInfoPostgres sessionManagerInfo; - - private final SessionInfoPostgres sessionTargetInfo; - - private final Connection connection; - - private final String title; - - private final int sessionId; - - private static final String SQL_ATTACH = "select pldbg_wait_for_target(?sessionid)"; - - private static final String SQL_ATTACH_BREAKPOINT = "select pldbg_wait_for_breakpoint(?sessionid)"; - - private static final String SQL_LISTEN = "select pldbg_create_listener() as sessionid"; - - private static final String SQL_GET_VARS = "select * from pldbg_get_variables()"; - - private static final String SQL_GET_STACK = "select * from pldbg_get_stack(?sessionid)"; - - private static final String SQL_STEP_OVER = "select pldbg_step_over(?sessionid)"; - - private static final String SQL_STEP_INTO = "select pldbg_step_into(?sessionid)"; - - private static final String SQL_CONTINUE = "select pldbg_continue(?sessionid)"; - - //private static final int ATTACH_TIMEOUT = 300; // seconds TODO - - private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); - - private List breakpoints = new ArrayList(1); - - private DebugSessionWorker worker; - - private FutureTask task; - - private Thread workerThread = null; - - private int listen() throws DebugException{ - - try { - - lock.writeLock().lockInterruptibly(); - - } catch (InterruptedException e1) { - throw new DebugException(e1.getMessage()); - } - - if (isWaiting()) { - lock.writeLock().unlock(); - throw new DebugException("Debug session in waiting state"); - } - - if (!isDone()) { - throw new DebugException("Debug session in incorrect state"); - } - - try (Statement stmt = connection.createStatement()) { - - ResultSet rs = stmt.executeQuery(SQL_LISTEN); - - if (rs.next()) { - - connection.setClientInfo("ApplicationName", "Debug Mode : " + String.valueOf(sessionId)); - return rs.getInt("sessionid"); - - - } else { - - throw new DebugException("Unable to create debug instance"); - - } - - } catch (SQLException e) { - throw new DebugException(e); - } finally { - lock.writeLock().unlock(); - } - - } - - - public void attach(boolean breakpoint) throws DebugException{ - - try { - - lock.writeLock().lockInterruptibly(); - - } catch (InterruptedException e1) { - - throw new DebugException(e1.getMessage()); - - } - - if (isWaiting()) { - throw new DebugException("Debug session in waiting state"); - } - - if (!isDone()) { - throw new DebugException("Debug session in incorrect state"); - } - - try { - - if (breakpoint) { - runAsync(SQL_ATTACH_BREAKPOINT.replaceAll("\\?sessionid",String.valueOf(sessionId)), - String.valueOf(sessionId) + " breakpoint attached to " + String.valueOf(sessionManagerInfo.pid)); - - } else { - runAsync(SQL_ATTACH.replaceAll("\\?sessionid",String.valueOf(sessionId)), - String.valueOf(sessionId) + " global attached to " + String.valueOf(sessionManagerInfo.pid)); - } - - } finally { - lock.writeLock().unlock(); - } - - } - - public DebugSessionPostgres(SessionInfoPostgres sessionManagerInfo,SessionInfoPostgres sessionTargetInfo,Connection connection) throws DebugException - { - this.sessionManagerInfo = sessionManagerInfo; - this.sessionTargetInfo = sessionTargetInfo; - this.connection = connection; - this.title = sessionManagerInfo.application; - this.worker = new DebugSessionWorker(this.connection); - sessionId = listen(); - } - - @Override - public SessionInfoPostgres getSessionInfo() - { - - return sessionManagerInfo; - } - - @Override - public String getTitle() - { - return title; - } - - @Override - public List getBreakpoints() - { - - return breakpoints; - } - - @Override - public Breakpoint setBreakpoint(DebugObjectPostgres obj,BreakpointProperties properties) throws DebugException { - - try { - - lock.readLock().lockInterruptibly(); - - } catch (InterruptedException e1) { - - throw new DebugException(e1.getMessage()); - - } - - if (isWaiting()) { - throw new DebugException("Debug session in waiting state"); - } - - if (!isDone()) { - throw new DebugException("Debug session in incorrect state"); - } - - PostgresBreakpoint bp = null; - - try { - bp = new PostgresBreakpoint(this, obj, (BreakpointPropertiesPostgres) properties); - breakpoints.add(bp); - - } finally { - lock.readLock().unlock(); - } - - return bp; - } - - @Override - public void removeBreakpoint(Breakpoint bp) - { - // TODO Auto-generated method stub - - } - - @Override - public void execContinue() throws DebugException - { - execStep(SQL_CONTINUE," continue for "); - - } - - @Override - public void execStepInto() throws DebugException - { - execStep(SQL_STEP_INTO," step into for "); - - } - - @Override - public void execStepOver() throws DebugException - { - execStep(SQL_STEP_OVER," step over for "); - - } - - public void execStep(String commandSQL,String name) throws DebugException - { - - try { - - lock.writeLock().lockInterruptibly(); - - } catch (InterruptedException e1) { - - throw new DebugException(e1.getMessage()); - - } - - if (isWaiting()) { - throw new DebugException("Debug session in waiting state"); - } - - if (!isDone()) { - throw new DebugException("Debug session in incorrect state"); - } - - try { - - runAsync(commandSQL.replaceAll("\\?sessionid",String.valueOf(sessionId)), - String.valueOf(sessionId) + name + String.valueOf(sessionManagerInfo.pid)); - - } finally { - lock.writeLock().unlock(); - } - - } - - @Override - public void abort() - { - // TODO Auto-generated method stub - - } - - @Override - public List> getVarables(String ctx) - { - // TODO Auto-generated method stub - return null; - } - - @Override - public void setVariableVal(Variable variable, Object value) - { - // TODO Auto-generated method stub - - } - - @Override - public List getStack() throws DebugException - { - List stack = new ArrayList(1); - - try { - lock.readLock().lockInterruptibly(); - } catch (InterruptedException e) { - throw new DebugException(e.getMessage()); - } - - if (isWaiting()) { - lock.readLock().unlock(); - throw new DebugException("Debug session in waiting state"); - } - - if (!isDone()) { - throw new DebugException("Debug session in incorrect state"); - } - - - try (Statement stmt = connection.createStatement()) { - - ResultSet rs = stmt.executeQuery(SQL_GET_STACK.replaceAll("\\?sessionid",String.valueOf(sessionId))); - - while(rs.next()){ - PostgresStackFrame frame = new PostgresStackFrame ( - rs.getInt("level"), - rs.getString("targetname"), - rs.getString("func"), - rs.getInt("linenumber"), - rs.getString("args") - ); - stack.add(frame); - } - - } catch (SQLException e) { - throw new DebugException(e); - } finally { - lock.readLock().unlock(); - } - return stack; - } - - - - public Connection getConnection() { - return connection; - } - - - @Override - public String toString() { - return "DebugSessionPostgres "+(isWaiting() ? "WAITING" : "READY")+" [connection=" + connection + ", title=" + title - + ", sessionId=" + sessionId + ", breakpoints=" + breakpoints + "ManagerSession=("+ sessionManagerInfo.toString() +") Session=("+ sessionTargetInfo.toString()+") "+"]"; - } - - - @Override - public Integer getSessionId() { - return sessionId; - } - - public boolean isWaiting() { - return (task == null ? false : !task.isDone()) - && (workerThread == null ? false : workerThread.isAlive()); - } - - public boolean isDone() throws DebugException { - - if (task == null) return true; - - if (task.isDone()) { - - DebugSessionResult res; - - try { - res = task.get(); - } catch (InterruptedException | ExecutionException e1) { - throw new DebugException(e1); - } - - if (res.getException() != null) { - System.out.println("WARNING " + res.getException().getMessage()); - } - - return res.isResult(); - - } - - return false; - - } - - private void runAsync(String commandSQL,String name) throws DebugException { - - try (Statement stmt = connection.createStatement()) { - - connection.setAutoCommit(false); - - worker.execSQL(commandSQL); - - task = new FutureTask(worker); - - workerThread = new Thread(task); - - workerThread.setName(name); - - workerThread.start(); - - } catch (SQLException e) { - throw new DebugException(e); - - } - } +public class DebugSessionPostgres implements DebugSession { + + private final SessionInfoPostgres sessionManagerInfo; + + private final SessionInfoPostgres sessionDebugInfo; + + private final Connection connection; + + private final String title; + + private final int sessionId; + + private static final String SQL_ATTACH = "select pldbg_wait_for_target(?sessionid)"; + + private static final String SQL_ATTACH_BREAKPOINT = "select pldbg_wait_for_breakpoint(?sessionid)"; + + private static final String SQL_LISTEN = "select pldbg_create_listener() as sessionid"; + + private static final String SQL_GET_VARS = "select * from pldbg_get_variables(?sessionid)"; + + private static final String SQL_GET_STACK = "select * from pldbg_get_stack(?sessionid)"; + + private static final String SQL_STEP_OVER = "select pldbg_step_over(?sessionid)"; + + private static final String SQL_STEP_INTO = "select pldbg_step_into(?sessionid)"; + + private static final String SQL_CONTINUE = "select pldbg_continue(?sessionid)"; + + private static final String SQL_ABORT = "select pldbg_abort_target(?sessionid)"; + + private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); + + private List breakpoints = new ArrayList(1); + + private DebugSessionWorker worker; + + private FutureTask task; + + private Thread workerThread = null; + + private int listen() throws DebugException { + + acquireWriteLock(); + + try (Statement stmt = connection.createStatement()) { + + ResultSet rs = stmt.executeQuery(SQL_LISTEN); + + if (rs.next()) { + + connection.setClientInfo("ApplicationName", "Debug Mode : " + String.valueOf(sessionId)); + return rs.getInt("sessionid"); + + } else { + + throw new DebugException("Unable to create debug instance"); + + } + + } catch (SQLException e) { + throw new DebugException(e); + } finally { + lock.writeLock().unlock(); + } + + } + + public void attach(boolean breakpoint) throws DebugException { + + acquireWriteLock(); + + try { + + if (breakpoint) { + runAsync(SQL_ATTACH_BREAKPOINT.replaceAll("\\?sessionid", String.valueOf(sessionId)), + String.valueOf(sessionId) + " breakpoint attached to " + + String.valueOf(sessionManagerInfo.pid)); + + } else { + runAsync(SQL_ATTACH.replaceAll("\\?sessionid", String.valueOf(sessionId)), + String.valueOf(sessionId) + " global attached to " + String.valueOf(sessionManagerInfo.pid)); + } + + } finally { + lock.writeLock().unlock(); + } + + } + + public DebugSessionPostgres(SessionInfoPostgres sessionManagerInfo, SessionInfoPostgres sessionDebugInfo, + Connection connection) throws DebugException { + this.sessionManagerInfo = sessionManagerInfo; + this.sessionDebugInfo = sessionDebugInfo; + this.connection = connection; + this.title = sessionManagerInfo.application; + this.worker = new DebugSessionWorker(this.connection); + sessionId = listen(); + } + + @Override + public SessionInfoPostgres getSessionInfo() { + + return sessionDebugInfo; + } + + @Override + public String getTitle() { + return title; + } + + @Override + public List getBreakpoints() { + + return breakpoints; + } + + @Override + public Breakpoint setBreakpoint(DebugObjectPostgres obj, BreakpointProperties properties) throws DebugException { + + acquireReadLock(); + + PostgresBreakpoint bp = null; + + try { + bp = new PostgresBreakpoint(this, obj, (BreakpointPropertiesPostgres) properties); + breakpoints.add(bp); + + } finally { + lock.readLock().unlock(); + } + + return bp; + } + + @Override + public void removeBreakpoint(Breakpoint bp) throws DebugException { + + acquireReadLock(); + + try { + + bp.drop(); + + breakpoints.remove(bp); + + } finally { + lock.readLock().unlock(); + } + + } + + @Override + public void execContinue() throws DebugException { + execStep(SQL_CONTINUE, " continue for "); + + } + + @Override + public void execStepInto() throws DebugException { + execStep(SQL_STEP_INTO, " step into for "); + + } + + @Override + public void execStepOver() throws DebugException { + execStep(SQL_STEP_OVER, " step over for "); + + } + + public void execStep(String commandSQL, String name) throws DebugException { + + acquireWriteLock(); + + try { + + runAsync(commandSQL.replaceAll("\\?sessionid", String.valueOf(sessionId)), + String.valueOf(sessionId) + name + String.valueOf(sessionManagerInfo.pid)); + + } finally { + lock.writeLock().unlock(); + } + + } + + @Override + public void abort() throws DebugException { + + acquireReadLock(); + + try (Statement stmt = connection.createStatement()) { + + stmt.execute(SQL_ABORT.replaceAll("\\?sessionid", String.valueOf(sessionId))); + + task = null; + + } catch (SQLException e) { + throw new DebugException(e); + } finally { + lock.readLock().unlock(); + } + + } + + public void close() { + + lock.writeLock().lock(); + + try { + + try { + if (!isDone()) { + task.cancel(true); + } + } catch (DebugException e1) { + e1.printStackTrace(); + } + + connection.close(); + + } catch (SQLException e) { + + e.printStackTrace(); + + } finally { + lock.writeLock().unlock(); + } + + } + + @Override + public List> getVarables() throws DebugException { + + acquireReadLock(); + + List> vars = new ArrayList<>(); + + try (Statement stmt = connection.createStatement()) { + + ResultSet rs = stmt.executeQuery(SQL_GET_VARS.replaceAll("\\?sessionid", String.valueOf(sessionId))); + + while (rs.next()) { + + PostgresVariable var = new PostgresVariable(rs.getString("name"), rs.getString("varclass"), + rs.getInt("linenumber"), rs.getBoolean("isunique"), rs.getBoolean("isconst"), + rs.getBoolean("isnotnull"), rs.getInt("dtype"), rs.getString("value")); + + vars.add(var); + } + + } catch (SQLException e) { + throw new DebugException(e); + } finally { + lock.readLock().unlock(); + } + + return vars; + + } + + @Override + public void setVariableVal(Variable variable, Object value) throws DebugException { + // TODO Auto-generated method stub + + } + + @Override + public List getStack() throws DebugException { + acquireReadLock(); + + List stack = new ArrayList(1); + + try (Statement stmt = connection.createStatement()) { + + ResultSet rs = stmt.executeQuery(SQL_GET_STACK.replaceAll("\\?sessionid", String.valueOf(sessionId))); + + while (rs.next()) { + PostgresStackFrame frame = new PostgresStackFrame(rs.getInt("level"), rs.getString("targetname"), + rs.getInt("func"), rs.getInt("linenumber"), rs.getString("args")); + stack.add(frame); + } + + } catch (SQLException e) { + throw new DebugException(e); + } finally { + lock.readLock().unlock(); + } + return stack; + } + + public Connection getConnection() { + return connection; + } + + @Override + public String toString() { + return "DebugSessionPostgres " + (isWaiting() ? "WAITING" : "READY") + " [connection=" + connection + ", title=" + + title + ", sessionId=" + sessionId + ", breakpoints=" + breakpoints + "ManagerSession=(" + + sessionManagerInfo.toString() + ") Session=(" + sessionDebugInfo.toString() + ") " + "]"; + } + + @Override + public Integer getSessionId() { + return sessionId; + } + + public boolean isWaiting() { + return (task == null ? false : !task.isDone()) && (workerThread == null ? false : workerThread.isAlive()); + } + + public boolean isDone() throws DebugException { + + if (task == null) + return true; + + if (task.isDone()) { + + DebugSessionResult res; + + try { + res = task.get(); + } catch (InterruptedException | ExecutionException e1) { + throw new DebugException(e1); + } + + if (res.getException() != null) { + System.out.println("WARNING " + res.getException().getMessage()); + } + + return res.isResult(); + + } + + return false; + + } + + private void runAsync(String commandSQL, String name) throws DebugException { + + try (Statement stmt = connection.createStatement()) { + + connection.setAutoCommit(false); + + worker.execSQL(commandSQL); + + task = new FutureTask(worker); + + workerThread = new Thread(task); + + workerThread.setName(name); + + workerThread.start(); + + } catch (SQLException e) { + throw new DebugException(e); + + } + } + + private void acquireReadLock() throws DebugException { + + try { + + lock.readLock().lockInterruptibly(); + + } catch (InterruptedException e1) { + + throw new DebugException(e1.getMessage()); + + } + + if (isWaiting()) { + lock.readLock().unlock(); + throw new DebugException("Debug session in waiting state"); + } + + if (!isDone()) { + lock.readLock().unlock(); + throw new DebugException("Debug session in incorrect state"); + } + + } + + private void acquireWriteLock() throws DebugException { + + try { + + lock.writeLock().lockInterruptibly(); + + } catch (InterruptedException e1) { + throw new DebugException(e1.getMessage()); + } + + if (isWaiting()) { + lock.writeLock().unlock(); + throw new DebugException("Debug session in waiting state"); + } + + if (!isDone()) { + lock.writeLock().unlock(); + throw new DebugException("Debug session in incorrect state"); + } + + } } diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/DebugSessionResult.java b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/DebugSessionResult.java index bd71f94c58abe4e22355238323318f90f42050b5..d9894b2acc0a204118a1c930c9bef7f6803b13bc 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/DebugSessionResult.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/DebugSessionResult.java @@ -1,25 +1,40 @@ +/* + * DBeaver - Universal Database Manager + * 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.pldbg.impl; public class DebugSessionResult { - private final boolean result; - - private final Exception exception; - - public DebugSessionResult(boolean result, Exception exception) { - super(); - this.result = result; - this.exception = exception; - } - - public boolean isResult() { - return result; - } - - public Exception getException() { - return exception; - } - - - + private final boolean result; + + private final Exception exception; + + public DebugSessionResult(boolean result, Exception exception) { + super(); + this.result = result; + this.exception = exception; + } + + public boolean isResult() { + return result; + } + + public Exception getException() { + return exception; + } + } diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/DebugSessionWorker.java b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/DebugSessionWorker.java index 1c9707b58587d66826ba3fcdf06b9b88ca4ad6fe..50c74b3837a4683f0cb47e545eb7f4e33dc6094a 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/DebugSessionWorker.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/DebugSessionWorker.java @@ -1,3 +1,20 @@ +/* + * DBeaver - Universal Database Manager + * 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.pldbg.impl; import java.sql.Connection; @@ -7,27 +24,30 @@ import java.util.concurrent.Callable; public class DebugSessionWorker implements Callable { - private final Connection conn; - private String sql =""; - - public void execSQL(String sqlCommand) { - this.sql = sqlCommand; - } - - public DebugSessionWorker(Connection conn) { - this.conn = conn; - } - - @Override - public DebugSessionResult call() throws Exception { - - try (Statement stmt = conn.createStatement()) { - stmt.executeQuery(sql); - return new DebugSessionResult(true,null); - - } catch (SQLException e) { - return new DebugSessionResult(false,e); - } - } - + private final Connection conn; + private String sql = ""; + + public void execSQL(String sqlCommand) + { + this.sql = sqlCommand; + } + + public DebugSessionWorker(Connection conn) + { + this.conn = conn; + } + + @Override + public DebugSessionResult call() throws Exception + { + + try (Statement stmt = conn.createStatement()) { + stmt.executeQuery(sql); + return new DebugSessionResult(true, null); + + } catch (SQLException e) { + return new DebugSessionResult(false, e); + } + } + } diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/PostgresBreakpoint.java b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/PostgresBreakpoint.java index b2c601682789f9969d08903ffe153fdd127767a1..7bfa1a69e49ab1b766c228ea8bf58f9b490e2329 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/PostgresBreakpoint.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/PostgresBreakpoint.java @@ -14,74 +14,78 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.jkiss.dbeaver.ext.postgresql.pldbg.impl; -import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import org.jkiss.dbeaver.ext.postgresql.pldbg.Breakpoint; import org.jkiss.dbeaver.ext.postgresql.pldbg.DebugException; - @SuppressWarnings("nls") public class PostgresBreakpoint implements Breakpoint { - - private final DebugObjectPostgres obj; - - private final DebugSessionPostgres session; - - private final BreakpointPropertiesPostgres properties; - - private static final String SQL_SET_GLOBAL = "select pldbg_set_global_breakpoint(?sessionid, ?obj, ?line, ?target)"; - private static final String SQL_SET = "select pldbg_set_breakpoint(?sessionid, ?obj, ?line, ?target)"; - - public PostgresBreakpoint(DebugSessionPostgres session,DebugObjectPostgres obj, BreakpointPropertiesPostgres properties) throws DebugException - { - - this.session = session; - this.obj = obj; - this.properties = properties; - try (Statement stmt = session.getConnection().createStatement()) { - - String sqlCommand = properties.isGlobal() ? SQL_SET_GLOBAL : SQL_SET; - - stmt.executeQuery(sqlCommand.replaceAll("\\?sessionid",String.valueOf(session.getSessionId())) - .replaceAll("\\?obj",String.valueOf(obj.getID())) - .replaceAll("\\?line",properties.isOnStart() ? "-1" : String.valueOf(properties.getLineNo())) - .replaceAll("\\?target",properties.isAll() ? "null" : String.valueOf(properties.getTargetId()))); - - - } catch (SQLException e) { - throw new DebugException(e); - } - - } - - @Override - public DebugObjectPostgres getObj() - { - return obj; - } - - - @Override - public void drop() throws DebugException - { - // TODO Auto-generated method stub - - } - - @Override - public BreakpointPropertiesPostgres getProperties() { - return properties; - } - - @Override - public String toString() { - return "PostgresBreakpoint [obj=" + obj + ", session id =" + session.getSessionId() + ", properties=" + properties + "]"; - } - - + + private final DebugObjectPostgres obj; + + private final DebugSessionPostgres session; + + private final BreakpointPropertiesPostgres properties; + + private static final String SQL_SET_GLOBAL = "select pldbg_set_global_breakpoint(?sessionid, ?obj, ?line, ?target)"; + private static final String SQL_SET = "select pldbg_set_breakpoint(?sessionid, ?obj, ?line)"; + private static final String SQL_DROP = "select pldbg_drop_breakpoint(?sessionid, ?obj, ?line)"; + + public PostgresBreakpoint(DebugSessionPostgres session, DebugObjectPostgres obj, + BreakpointPropertiesPostgres properties) throws DebugException { + + this.session = session; + this.obj = obj; + this.properties = properties; + try (Statement stmt = session.getConnection().createStatement()) { + + String sqlCommand = properties.isGlobal() ? SQL_SET_GLOBAL : SQL_SET; + + stmt.executeQuery(sqlCommand.replaceAll("\\?sessionid", String.valueOf(session.getSessionId())) + .replaceAll("\\?obj", String.valueOf(obj.getID())) + .replaceAll("\\?line", properties.isOnStart() ? "-1" : String.valueOf(properties.getLineNo())) + .replaceAll("\\?target", properties.isAll() ? "null" + : String.valueOf(properties.getTargetId()))); + + } catch (SQLException e) { + throw new DebugException(e); + } + + } + + @Override + public DebugObjectPostgres getObj() { + return obj; + } + + @Override + public void drop() throws DebugException { + try (Statement stmt = session.getConnection().createStatement()) { + + stmt.executeQuery(SQL_DROP.replaceAll("\\?sessionid", String.valueOf(session.getSessionId())) + .replaceAll("\\?obj", String.valueOf(obj.getID())) + .replaceAll("\\?line", properties.isOnStart() ? "-1" : String.valueOf(properties.getLineNo()))); + + } catch (SQLException e) { + throw new DebugException(e); + } + + } + + @Override + public BreakpointPropertiesPostgres getProperties() { + return properties; + } + + @Override + public String toString() { + return "PostgresBreakpoint [obj=" + obj + ", session id =" + session.getSessionId() + ", properties=" + + properties + "]"; + } } diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/PostgresStackFrame.java b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/PostgresStackFrame.java index f79482951e89aa84bfbb2eab70dcafa0a0e1b3e6..2f8abd7c4c087f4ab1f6855e5fbd82b2183c6c9d 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/PostgresStackFrame.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/PostgresStackFrame.java @@ -1,63 +1,69 @@ +/* + * DBeaver - Universal Database Manager + * 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.pldbg.impl; import org.jkiss.dbeaver.ext.postgresql.pldbg.StackFrame; public class PostgresStackFrame implements StackFrame { - - private final int level; - private final String name; - private final String oid; - private final int lineNo; - private final String args; - - public PostgresStackFrame(int level, String name, String oid, int lineNo, String args) { - super(); - this.level = level; - this.name = name; - this.oid = oid; - this.lineNo = lineNo; - this.args = args; - } - - - - public int getLevel() { - return level; - } - - - - public String getOid() { - return oid; - } - - - public int getLineNo() { - return lineNo; - } + private final int level; + private final String name; + private final int oid; + private final int lineNo; + private final String args; + public PostgresStackFrame(int level, String name, int oid, int lineNo, String args) { + super(); + this.level = level; + this.name = name; + this.oid = oid; + this.lineNo = lineNo; + this.args = args; + } + public int getLevel() { + return level; + } - public String getArgs() { - return args; - } + public int getOid() { + return oid; + } + @Override + public int getLine() { + return lineNo; + } + public String getArgs() { + return args; + } - @Override - public String getName() { - return name; - } + @Override + public String getName() { + return name; + } + @Override + public String toString() { + return "PostgresStackFrame [level=" + level + ", name=" + name + ", oid=" + oid + ", lineNo=" + lineNo + + ", args=" + args + "]"; + } - @Override - public String toString() { - return "PostgresStackFrame [level=" + level + ", name=" + name + ", oid=" + oid + ", lineNo=" + lineNo - + ", args=" + args + "]"; - } - - } diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/PostgresVariable.java b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/PostgresVariable.java new file mode 100644 index 0000000000000000000000000000000000000000..e1d2e8f66babcc6ec3269398bb0809348f83db73 --- /dev/null +++ b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/PostgresVariable.java @@ -0,0 +1,103 @@ +/* + * DBeaver - Universal Database Manager + * 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.pldbg.impl; + +import org.jkiss.dbeaver.ext.postgresql.pldbg.Variable; +import org.jkiss.dbeaver.ext.postgresql.pldbg.VariableType; + +public class PostgresVariable implements Variable { + + private final String name; + + private final String varclass; + + private final int linenumber; + + private final boolean unique; + + private final boolean constant; + + private final boolean notnull; + + private final int oid; + + private final String val; + + @Override + public String getVal() { + + return val; + } + + @Override + public String getName() { + + return name; + } + + @Override + public VariableType getType() { + // TODO Auto-generated method stub + return VariableType.TEXT; + } + + public PostgresVariable(String name, String varclass, int linenumber, boolean unique, boolean constant, + boolean notnull, int oid, String val) { + super(); + this.name = name; + this.varclass = varclass; + this.linenumber = linenumber; + this.unique = unique; + this.constant = constant; + this.notnull = notnull; + this.oid = oid; + this.val = val; + } + + public String getVarclass() { + return varclass; + } + + public int getLinenumber() { + return linenumber; + } + + public boolean isUnique() { + return unique; + } + + public boolean isConstant() { + return constant; + } + + public boolean isNotnull() { + return notnull; + } + + public int getOid() { + return oid; + } + + @Override + public String toString() { + return "PostgresVariable [name=" + name + ", val=" + val + ", varclass=" + varclass + ", linenumber=" + + linenumber + ", unique=" + unique + ", constant=" + constant + ", notnull=" + notnull + ", oid=" + oid + + "]"; + } + +} diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/SessionInfoPostgres.java b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/SessionInfoPostgres.java index d64cd5b2dd3b7d3c18e445e44d479d11bebe09da..38ca71d087716b432721718c95867f4e3fb73788 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/SessionInfoPostgres.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql.debug.core/src/org/jkiss/dbeaver/ext/postgresql/pldbg/impl/SessionInfoPostgres.java @@ -19,70 +19,56 @@ package org.jkiss.dbeaver.ext.postgresql.pldbg.impl; import org.jkiss.dbeaver.ext.postgresql.pldbg.SessionInfo; -/** - * @author Andrey.Hitrin - * - */ @SuppressWarnings("nls") public class SessionInfoPostgres implements SessionInfo { - - public static final String CREATE_LISTEN = "CREATE LISTEN"; - - final int pid; - final String user; - final String application; - final String state; - final String query; - - @Override - public Integer getID() - { - return pid; - } - public SessionInfoPostgres(int pid, String user, String application, - String state, String query) - { - super(); - this.pid = pid; - this.user = user; - this.application = application; - this.state = state; - this.query = query; - } + public static final String CREATE_LISTEN = "CREATE LISTEN"; + + final int pid; + final String user; + final String application; + final String state; + final String query; + + @Override + public Integer getID() { + return pid; + } + + public SessionInfoPostgres(int pid, String user, String application, String state, String query) { + super(); + this.pid = pid; + this.user = user; + this.application = application; + this.state = state; + this.query = query; + } + + public int getPid() { + return pid; + } - public int getPid() - { - return pid; - } + public String getUser() { + return user; + } - public String getUser() - { - return user; - } + public String getApplication() { + return application; + } - public String getApplication() - { - return application; - } + public String getState() { + return state; + } - public String getState() - { - return state; - } + public String getQuery() { + return query; + } - public String getQuery() - { - return query; - } + @Override + public String toString() { - @Override - public String toString() - { - - return "pid:" + String.valueOf(pid) + ", user: " + user + ", application: `" + application + "`, state: " +state+ ", query: " + query.replace('\n', '\\') ; - } - - + return "pid:" + String.valueOf(pid) + ", user: " + user + ", application: `" + application + "`, state: " + + state + ", query: " + query.replace('\n', '\\'); + } }