2121
2222import javax .swing .JSplitPane ;
2323
24+ import oracle .dbtools .raptor .runner .DBStarterFactory ;
25+ import oracle .ide .Context ;
26+ import oracle .jdevimpl .runner .debug .DebuggingProcess ;
27+ import oracle .jdevimpl .runner .run .JRunner ;
2428import org .utplsql .sqldev .exception .GenericDatabaseAccessException ;
29+ import org .utplsql .sqldev .exception .GenericRuntimeException ;
2530import org .utplsql .sqldev .model .DatabaseTools ;
2631import org .utplsql .sqldev .model .StringTools ;
2732import org .utplsql .sqldev .model .SystemTools ;
3944public class UtplsqlWorksheetRunner {
4045 private static final Logger logger = Logger .getLogger (UtplsqlWorksheetRunner .class .getName ());
4146
42- private PreferenceModel preferences ;
47+ private final PreferenceModel preferences ;
48+ private final List <String > pathList ;
4349 private String connectionName ;
44- private List < String > pathList ;
50+ private boolean debug = false ;
4551
4652 public UtplsqlWorksheetRunner (final List <String > pathList , final String connectionName ) {
4753 this .pathList = pathList ;
4854 preferences = PreferenceModel .getInstance (Preferences .getPreferences ());
4955 setConnection (connectionName );
5056 }
5157
58+ public void enableDebugging () {
59+ this .debug = true ;
60+ }
61+
5262 private void setConnection (final String connectionName ) {
5363 if (connectionName != null && preferences .isUnsharedWorksheet ()) {
5464 try {
@@ -65,23 +75,32 @@ private void setConnection(final String connectionName) {
6575
6676 private CharSequence getCode () {
6777 StringBuilder sb = new StringBuilder ();
68- if (preferences .isResetPackage ()) {
69- sb .append ("EXECUTE dbms_session.reset_package;\n " );
70- }
71- sb .append ("SET SERVEROUTPUT ON SIZE UNLIMITED\n " );
72- if (preferences .isClearScreen ()) {
73- sb .append ("CLEAR SCREEN\n " );
74- }
75- final List <String > paths = pathList ;
76- if (paths .size () == 1 ) {
77- sb .append ("EXECUTE ut.run('" );
78- sb .append (paths .get (0 ));
79- sb .append ("');\n " );
78+ if (!debug ) {
79+ if (preferences .isResetPackage ()) {
80+ sb .append ("EXECUTE dbms_session.reset_package;\n " );
81+ }
82+ sb .append ("SET SERVEROUTPUT ON SIZE UNLIMITED\n " );
83+ if (preferences .isClearScreen ()) {
84+ sb .append ("CLEAR SCREEN\n " );
85+ }
86+ if (pathList .size () == 1 ) {
87+ sb .append ("EXECUTE ut.run('" );
88+ sb .append (pathList .get (0 ));
89+ sb .append ("');\n " );
90+ } else {
91+ // we want a horizontal dense output because we resize the worksheet to fit the command in common cases
92+ sb .append ("EXECUTE ut.run(ut_varchar2_list(" );
93+ sb .append (StringTools .getCSV (pathList , "" ).replace ("\n " , "" ));
94+ sb .append ("));\n " );
95+ }
8096 } else {
81- // we want a horizontal dense output because we resize the worksheet to fit the command in common cases
82- sb .append ("EXECUTE ut.run(ut_varchar2_list(" );
83- sb .append (StringTools .getCSV (pathList , "" ).replace ("\n " ,"" ));
84- sb .append ("));\n " );
97+ sb .append ("BEGIN\n " );
98+ sb .append (" ut.run(\n " );
99+ sb .append (" ut_varchar2_list(\n " );
100+ sb .append (StringTools .getCSV (pathList , 9 ));
101+ sb .append (" )\n " );
102+ sb .append (" );\n " );
103+ sb .append ("END;\n " );
85104 }
86105 return sb ;
87106 }
@@ -115,17 +134,37 @@ private void resizeResultPanel(final Worksheet worksheet) {
115134 }
116135 }
117136
137+ // cannot use IdeAction to run debugger, because text has to be set in inaccessible PLSQLController.updateAction
138+ private void runDebugger (final Worksheet worksheet , final String anonymousPlsqlBlock ) {
139+ try {
140+ Context processContext = JRunner .prepareProcessContext (worksheet .getContext (), false );
141+ DebuggingProcess process = new DebuggingProcess (processContext );
142+ DBStarterFactory .PlSqlStarter starter = new DBStarterFactory .PlSqlStarter (process , anonymousPlsqlBlock , connectionName , worksheet .getContext ());
143+ starter .start ();
144+ } catch (Throwable t ) {
145+ String msg = t .getClass ().getName () + " while debugging utPLSQL test." ;
146+ logger .severe (() -> msg );
147+ throw new GenericRuntimeException (msg );
148+ }
149+ }
150+
118151 private void runScript (final Worksheet worksheet ) {
119152 if (preferences .isAutoExecute ()) {
120153 SystemTools .sleep (100 );
121- final IdeAction action = ((IdeAction ) Ide .getIdeActionMap ().get (Ide .findCmdID ("Worksheet.RunScript" )));
122- if (action != null ) {
123- try {
124- action .performAction (worksheet .getContext ());
125- } catch (Exception e ) {
126- logger .severe (() -> "Could not run script in worksheet due to " + (e != null ? e .getMessage () : "???" ) + "." );
154+ if (debug ) {
155+ runDebugger (worksheet , worksheet .getFocusedEditorPane ().getText ());
156+ } else {
157+ final IdeAction action = ((IdeAction ) Ide .getIdeActionMap ().get (Ide .findCmdID ("Worksheet.RunScript" )));
158+ if (action != null ) {
159+ try {
160+ action .performAction (worksheet .getContext ());
161+ } catch (Exception e ) {
162+ logger .severe (() -> "Could not run script in worksheet due to " + e .getMessage () + "." );
163+ }
164+ if (!debug ) {
165+ resizeResultPanel (worksheet );
166+ }
127167 }
128- resizeResultPanel (worksheet );
129168 }
130169 }
131170 }
@@ -137,7 +176,7 @@ private void runTest() {
137176 }
138177
139178 public void runTestAsync () {
140- final Thread thread = new Thread (() -> runTest () );
179+ final Thread thread = new Thread (this :: runTest );
141180 thread .setName ("utPLSQL run test" );
142181 thread .start ();
143182 }
0 commit comments