forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlCommandHandler.cs
More file actions
407 lines (358 loc) · 14.5 KB
/
NpgsqlCommandHandler.cs
File metadata and controls
407 lines (358 loc) · 14.5 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/********************************************************
* ADO.NET 2.0 Data Provider for SQLite Version 3.X
* Written by Robert Simpson (robert@blackcastlesoft.com)
*
* Released to the public domain, use at your own risk!
********************************************************/
namespace Npgsql.Designer
{
using System;
using Microsoft.VisualStudio.Data;
using System.Windows.Forms.Design;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.OLE.Interop;
using System.Data.Common;
using Npgsql.Designer.Editors;
enum cmdid
{
CreateTable = 0x3520,
CreateView = 0x3521,
Alter = 0x3003,
Refresh = 0x3004,
Delete = 17,
Vacuum = 262,
Rekey = 263,
Triggers = 264,
}
internal sealed class NpgsqlCommandHandler : DataViewCommandHandler
{
internal static readonly Guid guidDataCmdSet = new Guid("501822E1-B5AF-11d0-B4DC-00A0C91506EF");
internal static readonly Guid guidSQLiteCmdSet = new Guid("814658EE-A28E-4b97-BC33-4B1BC81EBECB");
internal static readonly Guid guidIFCmdId = new Guid("{74d21311-2aee-11d1-8bfb-00a0c90f26f7}");
internal static readonly Guid guidDavinci = new Guid("{732abe75-cd80-11d0-a2db-00aa00a3efff}");
internal static readonly Guid guidDavinciGrp = new Guid("{732abe74-cd80-11d0-a2db-00aa00a3efff}");
internal static readonly Guid guidQueryGroup = new Guid("5efc7975-14bc-11cf-9b2b-00aa00573819");
internal static Guid guidTableDesignContext = new Guid("4194fee5-6777-419f-a5fc-47a536df1bdb");
internal static Guid guidViewDesignContext = new Guid("b968e165-98e0-41f0-8fbe-a8ed1d246a90");
public NpgsqlCommandHandler()
{
}
public override OleCommandStatus GetCommandStatus(int[] itemIds, OleCommand command, OleCommandTextType textType, OleCommandStatus status)
{
if (command.GroupGuid == guidSQLiteCmdSet)
{
switch ((cmdid)command.CommandId)
{
case cmdid.CreateTable:
case cmdid.Vacuum:
case cmdid.Rekey:
status.Supported = true;
status.Visible = true;
status.Enabled = true;
return status;
}
}
else if (command.GroupGuid == VSConstants.GUID_VSStandardCommandSet97)
{
switch ((VSConstants.VSStd97CmdID)command.CommandId)
{
case VSConstants.VSStd97CmdID.Delete:
status.Supported = true;
status.Visible = true;
status.Enabled = (SystemTableSelected == false && SystemIndexSelected == false);
return status;
}
}
else if (command.GroupGuid == guidDataCmdSet)
{
switch ((cmdid)command.CommandId)
{
case cmdid.Alter:
status.Supported = true;
status.Visible = true;
status.Enabled = (SystemTableSelected == false && SystemIndexSelected == false);
return status;
case cmdid.CreateTable:
case cmdid.CreateView:
status.Supported = true;
status.Visible = true;
status.Enabled = true;
return status;
}
}
base.GetCommandStatus(itemIds, command, textType, status);
return status;
}
private bool SystemTableSelected
{
get
{
int[] items = DataViewHierarchyAccessor.GetSelectedItems();
int n;
object[] parts;
for (n = 0; n < items.Length; n++)
{
parts = DataViewHierarchyAccessor.GetObjectIdentifier(items[n]);
if (parts == null) return true;
if (parts[2].ToString().StartsWith("sqlite_", StringComparison.InvariantCultureIgnoreCase))
return true;
}
return false;
}
}
private bool SystemIndexSelected
{
get
{
int[] items = DataViewHierarchyAccessor.GetSelectedItems();
int n;
object[] parts;
for (n = 0; n < items.Length; n++)
{
parts = DataViewHierarchyAccessor.GetObjectIdentifier(items[n]);
if (parts == null) return true;
if (parts[2].ToString().StartsWith("sqlite_", StringComparison.InvariantCultureIgnoreCase))
return true;
if (parts.Length > 3)
{
if (parts[3].ToString().StartsWith("sqlite_autoindex_", StringComparison.InvariantCultureIgnoreCase)
|| parts[3].ToString().StartsWith("sqlite_master_PK_", StringComparison.InvariantCultureIgnoreCase))
return true;
}
}
return false;
}
}
public override object[] ExecuteCommand(int[] itemIds, OleCommand command, OleCommandExecutionOption executionOption, object arguments)
{
return base.ExecuteCommand(itemIds, command, executionOption, arguments);
}
/// <summary>
/// This method executes a specified command, potentially based
/// on parameters passed in from the data view support XML.
/// </summary>
public override object ExecuteCommand(int itemId, OleCommand command, OleCommandExecutionOption executionOption, object arguments)
{
object returnValue = null;
object[] args = arguments as object[];
if (command.GroupGuid == guidSQLiteCmdSet)
{
switch ((cmdid)command.CommandId)
{
case cmdid.Vacuum:
Vacuum();
break;
case cmdid.Rekey:
ChangePassword(itemId);
break;
default:
returnValue = base.ExecuteCommand(itemId, command, executionOption, arguments);
break;
}
}
else if (command.GroupGuid == VSConstants.GUID_VSStandardCommandSet97)
{
switch ((VSConstants.VSStd97CmdID)command.CommandId)
{
case VSConstants.VSStd97CmdID.Delete:
switch ((string)args[0])
{
case "Table":
DropSelectedTables();
break;
case "Index":
DropSelectedIndexes();
break;
case "View":
DropSelectedViews();
break;
}
break;
}
}
else if (command.GroupGuid == guidDataCmdSet)
{
switch ((cmdid)command.CommandId)
{
case cmdid.CreateTable:
DesignTable(itemId, null);
break;
case cmdid.CreateView:
DesignView(itemId, null);
break;
case cmdid.Alter:
switch ((string)args[0])
{
case "Table":
{
object[] parts;
int[] items = DataViewHierarchyAccessor.GetSelectedItems();
parts = DataViewHierarchyAccessor.GetObjectIdentifier(items[0]);
DesignTable(itemId, parts[2].ToString());
}
break;
case "Index":
break;
case "View":
{
object[] parts;
int[] items = DataViewHierarchyAccessor.GetSelectedItems();
parts = DataViewHierarchyAccessor.GetObjectIdentifier(items[0]);
DesignView(itemId, parts[2].ToString());
}
break;
}
break;
}
}
else
{
returnValue = base.ExecuteCommand(itemId, command, executionOption, arguments);
}
return returnValue;
}
private void DesignTable(int itemId, string tableName)
{
Microsoft.VisualStudio.OLE.Interop.IServiceProvider provider = DataViewHierarchyAccessor.ServiceProvider as Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
IVsUIShell shell = DataViewHierarchyAccessor.ServiceProvider.GetService(typeof(IVsUIShell)) as IVsUIShell;
IVsUIHierarchy hier = DataViewHierarchyAccessor.Hierarchy;
IVsWindowFrame frame;
if (shell != null)
{
TableDesignerDoc form = new TableDesignerDoc(itemId, DataViewHierarchyAccessor, tableName);
IntPtr formptr = System.Runtime.InteropServices.Marshal.GetIUnknownForObject(form);
Guid empty = Guid.Empty;
FakeHierarchy fake = new FakeHierarchy(form, hier);
int code = shell.CreateDocumentWindow(
0, // (uint)(__VSCREATEDOCWIN.CDW_fCreateNewWindow | __VSCREATEDOCWIN.CDW_RDTFLAGS_MASK) | (uint)(_VSRDTFLAGS.RDT_CanBuildFromMemory | _VSRDTFLAGS.RDT_NonCreatable | _VSRDTFLAGS.RDT_VirtualDocument | _VSRDTFLAGS.RDT_DontAddToMRU),
form.Name, fake, (uint)itemId, formptr, formptr, ref empty, null, ref guidTableDesignContext, provider, "", form.Caption, null, out frame);
if (frame != null)
{
object ret;
int prop = (int)__VSFPROPID.VSFPROPID_Caption;
code = frame.GetProperty(prop, out ret);
code = frame.Show();
}
}
}
private void DesignView(int itemId, string viewName)
{
Microsoft.VisualStudio.OLE.Interop.IServiceProvider provider = DataViewHierarchyAccessor.ServiceProvider as Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
IVsUIShell shell = DataViewHierarchyAccessor.ServiceProvider.GetService(typeof(IVsUIShell)) as IVsUIShell;
IVsUIHierarchy hier = DataViewHierarchyAccessor.Hierarchy;
IVsWindowFrame frame;
if (shell != null)
{
ViewDesignerDoc form = new ViewDesignerDoc(itemId, DataViewHierarchyAccessor, viewName);
IntPtr formptr = System.Runtime.InteropServices.Marshal.GetIUnknownForObject(form);
Guid empty = Guid.Empty;
FakeHierarchy fake = new FakeHierarchy(form, hier);
int code = shell.CreateDocumentWindow(
0, // (uint)(__VSCREATEDOCWIN.CDW_fCreateNewWindow | __VSCREATEDOCWIN.CDW_RDTFLAGS_MASK) | (uint)(_VSRDTFLAGS.RDT_CanBuildFromMemory | _VSRDTFLAGS.RDT_NonCreatable | _VSRDTFLAGS.RDT_VirtualDocument | _VSRDTFLAGS.RDT_DontAddToMRU),
form.Name, fake, (uint)itemId, formptr, formptr, ref empty, null, ref guidViewDesignContext, provider, "", form.Caption, null, out frame);
if (frame != null)
{
object ret;
int prop = (int)__VSFPROPID.VSFPROPID_Caption;
code = frame.GetProperty(prop, out ret);
code = frame.Show();
}
}
}
private void DropSelectedTables()
{
int[] items = DataViewHierarchyAccessor.GetSelectedItems();
int n;
object[] parts;
for (n = 0; n < items.Length; n++)
{
parts = DataViewHierarchyAccessor.GetObjectIdentifier(items[n]);
if (parts == null) continue;
if (System.Windows.Forms.MessageBox.Show(String.Format("Drop table {0} ({1}), are you sure?", parts[2], parts[0]), "Confirm delete", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
{
string sql = String.Format("DROP TABLE [{0}].[{1}]", parts[0], parts[2]);
DataViewHierarchyAccessor.Connection.Command.ExecuteWithoutResults(sql, (int)System.Data.CommandType.Text, null, 0);
DataViewHierarchyAccessor.DropObjectNode(items[n]);
}
else throw new OperationCanceledException();
}
}
private void DropSelectedViews()
{
int[] items = DataViewHierarchyAccessor.GetSelectedItems();
int n;
object[] parts;
for (n = 0; n < items.Length; n++)
{
parts = DataViewHierarchyAccessor.GetObjectIdentifier(items[n]);
if (parts == null) continue;
if (System.Windows.Forms.MessageBox.Show(String.Format("Drop view {0} ({1}), are you sure?", parts[2], parts[0]), "Confirm delete", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
{
string sql = String.Format("DROP VIEW [{0}].[{1}]", parts[0], parts[2]);
DataViewHierarchyAccessor.Connection.Command.ExecuteWithoutResults(sql, (int)System.Data.CommandType.Text, null, 0);
DataViewHierarchyAccessor.DropObjectNode(items[n]);
}
else throw new OperationCanceledException();
}
}
private void DropSelectedIndexes()
{
int[] items = DataViewHierarchyAccessor.GetSelectedItems();
int n;
object[] parts;
for (n = 0; n < items.Length; n++)
{
parts = DataViewHierarchyAccessor.GetObjectIdentifier(items[n]);
if (parts == null) continue;
if (System.Windows.Forms.MessageBox.Show(String.Format("Drop index {0} ({1}), are you sure?", parts[3], parts[0]), "Confirm delete", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
{
string sql = String.Format("DROP INDEX [{0}].[{1}]", parts[0], parts[3]);
DataViewHierarchyAccessor.Connection.Command.ExecuteWithoutResults(sql, (int)System.Data.CommandType.Text, null, 0);
DataViewHierarchyAccessor.DropObjectNode(items[n]);
}
else throw new OperationCanceledException();
}
}
private void Vacuum()
{
DataViewHierarchyAccessor.Connection.Command.ExecuteWithoutResults("VACUUM", (int)System.Data.CommandType.Text, null, 0);
}
private void ChangePassword(int itemId)
{
DataConnection dataConn = DataViewHierarchyAccessor.Connection;
DbConnection cnn = DataViewHierarchyAccessor.Connection.ConnectionSupport.ProviderObject as DbConnection;
if (cnn == null) return;
NpgsqlConnectionProperties props = new NpgsqlConnectionProperties(cnn.ConnectionString);
using (ChangePasswordDialog dlg = new ChangePasswordDialog(props))
{
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (String.IsNullOrEmpty(dlg.Password))
props.Remove("Password");
else
props["Password"] = dlg.Password;
System.Reflection.MethodInfo method = cnn.GetType().GetMethod("ChangePassword", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.InvokeMethod, null, new Type[] { typeof(string) }, null);
if (method != null)
{
method.Invoke(cnn, new object[] { dlg.Password });
dataConn.Close();
dataConn.DisplayConnectionString = props.ToDisplayString();
dataConn.Open();
Refresh(itemId);
}
}
}
}
public void Refresh(int itemId)
{
Refresh(DataViewHierarchyAccessor, itemId);
}
public static void Refresh(DataViewHierarchyAccessor accessor, int itemId)
{
IVsUIHierarchy hier = accessor.Hierarchy as IVsUIHierarchy;
Guid g = VSConstants.GUID_VSStandardCommandSet97;
hier.ExecCommand((uint)itemId, ref g, (uint)0xbd, (uint)OleCommandExecutionOption.DoDefault, IntPtr.Zero, IntPtr.Zero);
}
}
}