21
21
22
22
import javax .swing .JSplitPane ;
23
23
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 ;
24
28
import org .utplsql .sqldev .exception .GenericDatabaseAccessException ;
29
+ import org .utplsql .sqldev .exception .GenericRuntimeException ;
25
30
import org .utplsql .sqldev .model .DatabaseTools ;
26
31
import org .utplsql .sqldev .model .StringTools ;
27
32
import org .utplsql .sqldev .model .SystemTools ;
39
44
public class UtplsqlWorksheetRunner {
40
45
private static final Logger logger = Logger .getLogger (UtplsqlWorksheetRunner .class .getName ());
41
46
42
- private PreferenceModel preferences ;
47
+ private final PreferenceModel preferences ;
48
+ private final List <String > pathList ;
43
49
private String connectionName ;
44
- private List < String > pathList ;
50
+ private boolean debug = false ;
45
51
46
52
public UtplsqlWorksheetRunner (final List <String > pathList , final String connectionName ) {
47
53
this .pathList = pathList ;
48
54
preferences = PreferenceModel .getInstance (Preferences .getPreferences ());
49
55
setConnection (connectionName );
50
56
}
51
57
58
+ public void enableDebugging () {
59
+ this .debug = true ;
60
+ }
61
+
52
62
private void setConnection (final String connectionName ) {
53
63
if (connectionName != null && preferences .isUnsharedWorksheet ()) {
54
64
try {
@@ -65,23 +75,32 @@ private void setConnection(final String connectionName) {
65
75
66
76
private CharSequence getCode () {
67
77
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
8000
+ 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
+ }
80
96
} 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 " );
85
104
}
86
105
return sb ;
87
106
}
@@ -115,17 +134,37 @@ private void resizeResultPanel(final Worksheet worksheet) {
115
134
}
116
135
}
117
136
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
+
118
151
private void runScript (final Worksheet worksheet ) {
119
152
if (preferences .isAutoExecute ()) {
120
153
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
+ }
127
167
}
128
- resizeResultPanel (worksheet );
129
168
}
130
169
}
131
170
}
@@ -137,7 +176,7 @@ private void runTest() {
137
176
}
138
177
139
178
public void runTestAsync () {
140
- final Thread thread = new Thread (() -> runTest () );
179
+ final Thread thread = new Thread (this :: runTest );
141
180
thread .setName ("utPLSQL run test" );
142
181
thread .start ();
143
182
}
0 commit comments