-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPlsqlDeveloperUtPlsqlPlugin.cs
More file actions
353 lines (306 loc) · 14.9 KB
/
Copy pathPlsqlDeveloperUtPlsqlPlugin.cs
File metadata and controls
353 lines (306 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
using RGiesecke.DllExport;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace utPLSQL
{
//*FUNC: 4*/ extern char *(*SYS_OracleHome)();
internal delegate IntPtr SysOracleHome();
//*FUNC: 11*/ BOOL (*IDE_Connected)();
internal delegate bool IdeConnected();
//*FUNC: 12*/ void (*IDE_GetConnectionInfo)(char **Username, char **Password, char **Database);
internal delegate void IdeGetConnectionInfo(out IntPtr username, out IntPtr password, out IntPtr database);
//*FUNC: 20*/ void (*IDE_CreateWindow)(int WindowType, char *Text, BOOL Execute);
internal delegate void IdeCreateWindow(int windowType, string text, bool execute);
//*FUNC: 69*/ void *(*IDE_CreatePopupItem)(int ID, int Index, char *Name, char *ObjectType);
internal delegate void IdeCreatePopupItem(int id, int index, string name, string objectType);
//*FUNC: 74*/ int (*IDE_GetPopupObject)(char **ObjectType, char **ObjectOwner, char **ObjectName, char **SubObject);
internal delegate int IdeGetPopupObject(out IntPtr objectType, out IntPtr objectOwner, out IntPtr objectName, out IntPtr subObject);
//*FUNC: 79*/ char *(*IDE_GetObjectSource)(char *ObjectType, char *ObjectOwner, char *ObjectName);
internal delegate IntPtr IdeGetObjectSource(string objectType, string objectOwner, string objectName);
//*FUNC: 97*/ extern char *(*IDE_GetConnectAs)();
internal delegate IntPtr IdeGetConnectAs();
//*FUNC: 150*/ void (*IDE_CreateToolButton)(int ID, int Index, char *Name, char *BitmapFile, int BitmapHandle);
internal delegate void IdeCreateToolButton(int id, int index, string name, string bitmapFile, long bitmapHandle);
public class PlsqlDeveloperUtPlsqlPlugin
{
private const string PluginName = "utPLSQL Plugin";
private const int PluginMenuIndexAllTests = 3;
private const int PluginMenuIndexAllTestsWithCoverage = 4;
private const int PluginMenuIndexPath = 5;
private const int PluginPopupIndex = 1;
private const int PluginPopupIndexWithCoverage = 2;
private static SysOracleHome sysOracleHome;
private static IdeConnected connected;
private static IdeGetConnectionInfo getConnectionInfo;
private static IdeCreateWindow createWindow;
private static IdeCreatePopupItem createPopupItem;
private static IdeGetPopupObject getPopupObject;
private static IdeGetObjectSource getObjectSource;
private static IdeGetConnectAs getConnectAs;
private static IdeCreateToolButton createToolButton;
private static int pluginId;
private static string username;
private static string password;
private static string database;
private static string connectAs;
private static string oracleHome;
private static PlsqlDeveloperUtPlsqlPlugin _plugin;
private static readonly List<TestRunnerWindow> Windows = new List<TestRunnerWindow>();
#region DLL exported API
[DllExport("IdentifyPlugIn", CallingConvention = CallingConvention.Cdecl)]
public static string IdentifyPlugIn(int id)
{
if (_plugin == null)
{
_plugin = new PlsqlDeveloperUtPlsqlPlugin();
pluginId = id;
}
return PluginName;
}
[DllExport("OnActivate", CallingConvention = CallingConvention.Cdecl)]
public static void OnActivate()
{
try
{
getDatabaseInformation();
// Separate streams are needed!
var assembly = Assembly.GetExecutingAssembly();
using (var stream = assembly.GetManifestResourceStream("utPLSQL.utPLSQL.bmp"))
{
if (stream != null)
{
createToolButton(pluginId, PluginMenuIndexAllTests, "utPLSQL", "utPLSQL.bmp", new Bitmap(stream).GetHbitmap().ToInt64());
}
}
using (var stream = assembly.GetManifestResourceStream("utPLSQL.utPLSQL_coverage.bmp"))
{
if (stream != null)
{
createToolButton(pluginId, PluginMenuIndexAllTestsWithCoverage, "utPLSQL", "utPLSQL_coverage.bmp", new Bitmap(stream).GetHbitmap().ToInt64());
}
}
using (var stream = assembly.GetManifestResourceStream("utPLSQL.utPLSQL.bmp"))
{
if (stream != null)
{
createToolButton(pluginId, PluginMenuIndexPath, "utPLSQL", "utPLSQL.bmp", new Bitmap(stream).GetHbitmap().ToInt64());
}
}
using (var stream = assembly.GetManifestResourceStream("utPLSQL.utPLSQL.bmp"))
{
if (stream != null)
{
createToolButton(pluginId, PluginPopupIndex, "utPLSQL", "utPLSQL.bmp", new Bitmap(stream).GetHbitmap().ToInt64());
}
}
using (var stream = assembly.GetManifestResourceStream("utPLSQL.utPLSQL_coverage.bmp"))
{
if (stream != null)
{
createToolButton(pluginId, PluginPopupIndexWithCoverage, "utPLSQL", "utPLSQL_coverage.bmp", new Bitmap(stream).GetHbitmap().ToInt64());
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
createPopupItem(pluginId, PluginPopupIndex, "Run utPLSQL Test", "USER");
createPopupItem(pluginId, PluginPopupIndexWithCoverage, "Run Code Coverage", "USER");
createPopupItem(pluginId, PluginPopupIndex, "Run utPLSQL Test", "PACKAGE");
createPopupItem(pluginId, PluginPopupIndexWithCoverage, "Run Code Coverage", "PACKAGE");
createPopupItem(pluginId, PluginPopupIndex, "Run utPLSQL Test", "PACKAGE BODY");
createPopupItem(pluginId, PluginPopupIndexWithCoverage, "Run Code Coverage", "PACKAGE BODY");
createPopupItem(pluginId, PluginPopupIndex, "Run utPLSQL Test", "PROCEDURE");
createPopupItem(pluginId, PluginPopupIndexWithCoverage, "Run Code Coverage", "PROCEDURE");
}
[DllExport("CanClose", CallingConvention = CallingConvention.Cdecl)]
public static bool CanClose()
{
if (Windows.Any(window => window.Running))
{
var confirmResult = MessageBox.Show(
"utPLSQL Tests are still running.\r\n\r\nDo you really want to close PL/SQL Developer?",
"Running utPLSQL Tests", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
return confirmResult == DialogResult.Yes;
}
return true;
}
[DllExport("RegisterCallback", CallingConvention = CallingConvention.Cdecl)]
public static void RegisterCallback(int index, IntPtr function)
{
switch (index)
{
case 4:
sysOracleHome = (SysOracleHome)Marshal.GetDelegateForFunctionPointer(function, typeof(SysOracleHome));
break;
case 11:
connected = (IdeConnected)Marshal.GetDelegateForFunctionPointer(function, typeof(IdeConnected));
break;
case 12:
getConnectionInfo = (IdeGetConnectionInfo)Marshal.GetDelegateForFunctionPointer(function,
typeof(IdeGetConnectionInfo));
break;
case 20:
createWindow = (IdeCreateWindow)Marshal.GetDelegateForFunctionPointer(function, typeof(IdeCreateWindow));
break;
case 69:
createPopupItem = (IdeCreatePopupItem)Marshal.GetDelegateForFunctionPointer(function, typeof(IdeCreatePopupItem));
break;
case 74:
getPopupObject = (IdeGetPopupObject)Marshal.GetDelegateForFunctionPointer(function, typeof(IdeGetPopupObject));
break;
case 79:
getObjectSource = (IdeGetObjectSource)Marshal.GetDelegateForFunctionPointer(function, typeof(IdeGetObjectSource));
break;
case 97:
getConnectAs = (IdeGetConnectAs)Marshal.GetDelegateForFunctionPointer(function, typeof(IdeGetConnectAs));
break;
case 150:
createToolButton = (IdeCreateToolButton)Marshal.GetDelegateForFunctionPointer(function, typeof(IdeCreateToolButton));
break;
}
}
[DllExport("OnConnectionChange", CallingConvention = CallingConvention.Cdecl)]
public static void OnConnectionChange()
{
getDatabaseInformation();
}
[DllExport("CreateMenuItem", CallingConvention = CallingConvention.Cdecl)]
public static string CreateMenuItem(int index)
{
switch (index)
{
case 1:
return "TAB=Tools";
case 2:
return "GROUP=utPLSQL";
case PluginMenuIndexAllTests:
return "LARGEITEM=Run all tests of current user";
case PluginMenuIndexAllTestsWithCoverage:
return "LARGEITEM=Run code coverage for current user";
case PluginMenuIndexPath:
return "LARGEITEM=Run tests for specific path";
default:
return "";
}
}
[DllExport("OnMenuClick", CallingConvention = CallingConvention.Cdecl)]
public static void OnMenuClick(int index)
{
try
{
if (index == PluginMenuIndexAllTests)
{
if (isConnected() && !isSydba())
{
var testResultWindow = new TestRunnerWindow(_plugin, username, password, database, connectAs, oracleHome);
Windows.Add(testResultWindow);
testResultWindow.RunTestsAsync("_ALL", username, null, null, false, false);
}
}
else if (index == PluginMenuIndexAllTestsWithCoverage)
{
if (isConnected() && !isSydba())
{
var testResultWindow = new TestRunnerWindow(_plugin, username, password, database, connectAs, oracleHome);
Windows.Add(testResultWindow);
testResultWindow.RunTestsAsync("_ALL", username, null, null, true, false);
}
}
else if (index == PluginMenuIndexPath)
{
if (isConnected() && !isSydba())
{
var testResultWindow = new TestRunnerWindow(_plugin, username, password, database, connectAs, oracleHome);
Windows.Add(testResultWindow);
testResultWindow.RunTestsAsync(null, null, null, null, false, true);
}
}
else if (index == PluginPopupIndex)
{
if (isConnected() && !isSydba())
{
getPopupObject(out IntPtr type, out IntPtr owner, out IntPtr name, out IntPtr subType);
var testResultWindow = new TestRunnerWindow(_plugin, username, password, database, connectAs, oracleHome);
Windows.Add(testResultWindow);
testResultWindow.RunTestsAsync(Marshal.PtrToStringAnsi(type), Marshal.PtrToStringAnsi(owner),
Marshal.PtrToStringAnsi(name), Marshal.PtrToStringAnsi(subType), false, false);
}
}
else if (index == PluginPopupIndexWithCoverage)
{
if (isConnected() && !isSydba())
{
getPopupObject(out IntPtr type, out IntPtr owner, out IntPtr name, out IntPtr subType);
var testResultWindow = new TestRunnerWindow(_plugin, username, password, database, connectAs, oracleHome);
Windows.Add(testResultWindow);
testResultWindow.RunTestsAsync(Marshal.PtrToStringAnsi(type), Marshal.PtrToStringAnsi(owner),
Marshal.PtrToStringAnsi(name), Marshal.PtrToStringAnsi(subType), true, false);
}
}
}
catch (Exception e)
{
MessageBox.Show($"{e.Message}\n\n{e.StackTrace}", "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
[DllExport("About", CallingConvention = CallingConvention.Cdecl)]
public static string About()
{
new AboutDialog().Show();
return "";
}
#endregion
public void OpenPackageBody(string owner, string name)
{
var source = getObjectSource("PACKAGE BODY", owner, name);
createWindow(3, Marshal.PtrToStringAnsi(source), false);
}
private static bool isSydba()
{
if (connectAs.ToLower().Equals("sysdba"))
{
MessageBox.Show("You shouldn't run utPLSQL as SYSDBA.\n\nTest will not run.", "Connected as SYSDBA", MessageBoxButtons.OK, MessageBoxIcon.Error);
return true;
}
return false;
}
private static bool isConnected()
{
if (!connected())
{
MessageBox.Show("Please connect before running tests!", "No connection", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return true;
}
private static void getDatabaseInformation()
{
try
{
if (connected())
{
getConnectionInfo(out IntPtr ptrUsername, out IntPtr ptrPassword, out IntPtr ptrDatabase);
username = Marshal.PtrToStringAnsi(ptrUsername);
password = Marshal.PtrToStringAnsi(ptrPassword);
database = Marshal.PtrToStringAnsi(ptrDatabase);
IntPtr ptrConnectAs = getConnectAs();
connectAs = Marshal.PtrToStringAnsi(ptrConnectAs);
IntPtr ptrOracleHome = sysOracleHome();
oracleHome = Marshal.PtrToStringAnsi(ptrOracleHome);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}
}