-
Notifications
You must be signed in to change notification settings - Fork 8
/
connectionFormUnit.pas
643 lines (562 loc) · 16.4 KB
/
connectionFormUnit.pas
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
unit connectionFormUnit;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, System.Math, Winapi.ShellApi,
System.Actions, Vcl.ActnList, System.ImageList, Vcl.ImgList, Vcl.OleCtrls,
SHDocVw, Vcl.Grids, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.ToolWin,
Vcl.PlatformDefaultStyleActnCtrls, Vcl.ActnMan, Vcl.ActnCtrls,
StringGridsUnit, ExtScrollingWinControlUnit, NppDockingForms, nppplugin,
Vcl.ExtCtrls, Vcl.WinXCtrls, Vcl.ActnPopup,
plugin, TreeViewExUnit;
type
TconnectionForm = class(TNppDockingForm)
Images: TImageList;
ActionManager1: TActionManager;
SQLAction: TAction;
CopyGridRowAction: TAction;
CopyAllGridAction: TAction;
ConnectAction: TAction;
DisonnectSQLAction: TAction;
AfterSQLAction: TAction;
BasesPanel: TSplitView;
OpenListBDAction: TAction;
TreeImageList: TImageList;
msModeAction: TAction;
sybModeAction: TAction;
ActionToolBar2: TActionToolBar;
AfterAddServerAction: TAction;
invTreeView: TTreeView;
Panel1: TPanel;
TabControl: TTabControl;
sql: TPanel;
invGrid: TStringGrid;
ResultLabel: TPanel;
TreeViewPopupMenu: TPopupMenu;
N8: TMenuItem;
N9: TMenuItem;
GridMenu: TPopupMenu;
N1: TMenuItem;
N2: TMenuItem;
ToolBar1: TToolBar;
ToolButton1: TToolButton;
RefreshButton: TToolButton;
ToolButton3: TToolButton;
RefreshMenu: TPopupMenu;
odbcModeAction: TAction;
tabPopupMenu: TPopupMenu;
DeleteTabAction: TAction;
N6: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure SQLActionExecute(Sender: TObject);
procedure CopyAllGridActionExecute(Sender: TObject);
procedure TabControlChange(Sender: TObject);
procedure OpenListBDActionExecute(Sender: TObject);
procedure ConnectActionExecute(Sender: TObject);
procedure msModeActionExecute(Sender: TObject);
procedure sybModeActionExecute(Sender: TObject);
procedure AfterAddServerActionExecute(Sender: TObject);
procedure msTreeViewMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure DisonnectSQLActionExecute(Sender: TObject);
procedure DisonnectSQLActionUpdate(Sender: TObject);
procedure AfterSQLActionExecute(Sender: TObject);
procedure CopyGridRowActionExecute(Sender: TObject);
procedure odbcModeActionExecute(Sender: TObject);
procedure DeleteTabActionUpdate(Sender: TObject);
procedure DeleteTabActionExecute(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormHide(Sender: TObject);
private
msTreeView: TTreeViewEx;
sybTreeView: TTreeViewEx;
odbcTreeView: TTreeViewEx;
FCurrentNode: TTreeNode;
FMenuItemCheck: TMenuItemCheck;
procedure SetMenuItemCheck(const Value: TMenuItemCheck);
const MaxTabIndex = 10;
function CreateBox: TExScrollBox;
procedure HideAllExcept(const aIndex: Integer);
procedure WMThreadMessage(var Msg: TMessage); message WM_USER_MESSAGE_FROM_THREAD;
procedure FillResultGrid(Results: TObjectStrings);
procedure SetCurrentNode(const Value: TTreeNode);
function GetResultPanel: TExScrollBox;
protected
property CurrentNode: TTreeNode read FCurrentNode write SetCurrentNode;
property ResultPanel: TExScrollBox read GetResultPanel;
public
procedure DoSql(const SqlText: string);
procedure DoConnect;
procedure Carousel;
property MenuItemCheck: TMenuItemCheck read FMenuItemCheck write SetMenuItemCheck;
end;
implementation
uses
Vcl.Clipbrd,
ThreadUnit,StringGridExUnit,
SqlThreadUnit,
BDLoginUnit, connectionFormHelpersUnit;
{$R *.dfm}
procedure TconnectionForm.DeleteTabActionExecute(Sender: TObject);
var
aIndex: Integer;
EBox: TExScrollBox;
begin
aIndex := TabControl.TabIndex;
if aIndex = TabControl.Tabs.Count-2 then
TabControl.TabIndex := TabControl.TabIndex-1
else
TabControl.TabIndex := TabControl.TabIndex+1;
HideAllExcept(TabControl.TabIndex);
EBox := TExScrollBox(TabControl.Tabs.Objects[aIndex]);
TabControl.Tabs.Delete(aIndex);
sql.RemoveComponent(EBox);
Ebox.Free;
end;
procedure TconnectionForm.DeleteTabActionUpdate(Sender: TObject);
begin
inherited;
TAction(Sender).Enabled := (TabControl.Tabs.Count>2)
and (TabControl.TabOrder < (TabControl.Tabs.Count-1));
end;
procedure TconnectionForm.DisonnectSQLActionExecute(Sender: TObject);
var
TreeNode: TTreeNode;
begin
inherited;
TreeNode := BasesPanel.CurrentNode;
if Assigned(TreeNode) then
begin
if Assigned(TreeNode.Parent) then
TreeNode := TreeNode.Parent;
TreeNode.Delete;
end;
end;
procedure TconnectionForm.DisonnectSQLActionUpdate(Sender: TObject);
var
TreeNode: TTreeNode;
begin
inherited;
TreeNode := BasesPanel.CurrentNode;
if Assigned(TreeNode) then
TAction(Sender).Enabled := TreeNode.ItemType in [itServerMS,itServerSYB,itODBC]
else
TAction(Sender).Enabled := False;
end;
procedure TconnectionForm.Carousel;
begin
if MenuItemCheck = miShown then
Hide
else
begin
Show;
MenuItemCheck := miShown;
end;
end;
procedure TconnectionForm.ConnectActionExecute(Sender: TObject);
var
FBDLoginForm: TBDLoginForm;
BDType: TBdType;
TreeView: TTreeViewEx;
Data: string;
Node: TTreeNode;
SqlThread: TSqlQueryObject;
begin
FBDLoginForm := TBDLoginForm.Create(Npp);
BdType := BasesPanel.BdType;
try
FBDLoginForm.BDType := BdType;
if FBDLoginForm.ShowModal = mrOK then
begin
TreeView := BasesPanel.CurrentTreeView;
Data := FBDLoginForm.Data;
Node := TreeView.NodeFoundByDataSource(TreeView,Data);
if Assigned(Node) then
begin
Node.Selected := True;
Node.Expanded := True
end
else
begin
SqlThread := TSqlQueryObject.Create;
SqlThread.Description := Data;
SqlThread.BdType := BdType;
if BdType <> bdODBC then
SqlThread.SQL.Text := FBDLoginForm.constDBList;
SqlThread.ConnectionString := TSplitView.GetConnectionString(Data);
SqlThread.OnAfterAction := AfterAddServerAction;
SqlThread.WinHandle := self.Handle;
SqlThread.Start; //DB List
end;
end;
finally
FBDLoginForm.Free;
end;
end;
procedure TconnectionForm.CopyAllGridActionExecute(Sender: TObject);
begin
if Self.ActiveControl is TStringGridEx then
TStringGridEx(Self.ActiveControl).CopyAllToClipboard;
end;
procedure TconnectionForm.CopyGridRowActionExecute(Sender: TObject);
begin
if Self.ActiveControl is TStringGridEx then
TStringGridEx(Self.ActiveControl).CopyToClipboard;
end;
procedure TconnectionForm.AfterAddServerActionExecute(Sender: TObject);
var
Obj: TSqlQueryObject;
TreeView: TTreeViewEx;
begin
Obj := TSqlQueryObject(TAction(Sender).Tag);
if Obj.ErrMessage = '' then
begin
case Obj.BdType of
bdMSSQL: TreeView := msTreeView;
bdSybase: TreeView := sybTreeView;
else
TreeView := odbcTreeView;
end;
TreeView.AddServer(Obj.Description,Obj.Login,Obj.Query);
end
else
MessageError(Obj.ErrMessage,cnstErroCaption);
end;
procedure TconnectionForm.AfterSQLActionExecute(Sender: TObject);
function GetDots(const S: string): string;
begin
if length(S) > 150 then
Result := '...'
else
Result := '';
end;
procedure SetItem(const Value: string);
var
i: integer;
NewItem: TMenuItem;
S: string;
begin
if Length(Value) > 3 then
begin
for i := 0 to RefreshMenu.Items.Count - 1 do
if RefreshMenu.Items[i].Hint = Value then Exit;
S := Trim(StringReplace(Value,sLineBreak,' ',[rfReplaceAll]));
NewItem := TMenuItem.Create(RefreshMenu);
RefreshMenu.Items.Add(NewItem);
NewItem.Caption := Copy(S,1,150) + GetDots(S);
NewItem.Hint := Value;
NewItem.OnClick := SQLActionExecute;
NewItem.ImageIndex := 0;
end
end;
var
Obj: TSqlExecutorObject;
begin
Obj := TSqlExecutorObject(TAction(Sender).Tag);
if Obj.ErrMessage = '' then
begin
FillResultGrid(Obj.Grids);
SetItem(Obj.Description);//Add menu Item
if MenuItemCheck = miHidden then Carousel;
end
else
MessageError(Obj.ErrMessage,cnstErroCaption);
end;
procedure TconnectionForm.DoConnect;
begin
ConnectAction.Execute;
end;
procedure TconnectionForm.DoSql(const SqlText: string);
var
SqlThread: TSqlExecutorObject;
CString,CName: string;
begin
CString := BasesPanel.ConnectionString;
CName := Format(cnstSqlExec,[
BasesPanel.CurrentServer,
BasesPanel.CurrentBase,
BasesPanel.CurrentUser
]);
if CString = '' then
begin
CString := ResultPanel.ConnectionString;
CName := ResultPanel.Description;
end;
if CString <> '' then
begin
SqlThread := TSqlExecutorObject.Create;
SqlThread.Description := SqlText.Trim;
SqlThread.BdType := BasesPanel.BdType;
SqlThread.SQL.Text := SqlThread.Description;
SqlThread.Name := CName;
SqlThread.ConnectionString := CString;
SqlThread.OnAfterAction := AfterSQLAction;
SqlThread.WinHandle := self.Handle;
SqlThread.Start;
end
else
MessageError(cnstNoBaseSelected,cnstErroCaption);
end;
procedure TconnectionForm.FormCreate(Sender: TObject);
begin
NppDefaultDockingMask := DWS_DF_FLOATING;
FMenuItemCheck := miHidden;
msTreeView := TTreeViewEx.Create(Self);
msTreeView.Parent := BasesPanel;
msTreeView.Images := TreeImageList;
msTreeView.PopupMenu := TreeViewPopupMenu;
msTreeView.BdType := bdMSSQL;
sybTreeView := TTreeViewEx.Create(Self);
sybTreeView.Parent := BasesPanel;
sybTreeView.Images := TreeImageList;
sybTreeView.PopupMenu := TreeViewPopupMenu;
sybTreeView.BdType := bdSybase;
odbcTreeView := TTreeViewEx.Create(Self);
odbcTreeView.Parent := BasesPanel;
odbcTreeView.Images := TreeImageList;
odbcTreeView.PopupMenu := TreeViewPopupMenu;
odbcTreeView.BdType := bdODBC;
TabControl.Tabs.Objects[0] := CreateBox;
TabControl.Tabs.Objects[1] := nil;
msModeAction.Execute;
end;
procedure TconnectionForm.FormDestroy(Sender: TObject);
var
i: Integer;
EBox: TExScrollBox;
begin
for i := 0 to TabControl.Tabs.Count-1 do
if Assigned(TabControl.Tabs.Objects[i]) then
begin
EBox := TExScrollBox(TabControl.Tabs.Objects[i]);
try
sql.RemoveComponent(EBox);
finally
Ebox.Free;
TabControl.Tabs.Objects[i] := nil;
end;
end;
end;
procedure TconnectionForm.FormHide(Sender: TObject);
begin
MenuItemCheck := miHidden;
end;
procedure TconnectionForm.FillResultGrid(Results: TObjectStrings);
var i,j,k,maxWidthGrid,aTop: integer;
Temp: TComponent;
Grid: TStringGridEx;
begin
ResultLabel.Caption := Results.Name;
maxWidthGrid := 0;
Grid := nil;
try
for i := ResultPanel.ControlCount - 1 downto 0 do
begin
Temp := ResultPanel.Controls[i];
if (Temp is TStringGrid) or (Temp is TButton) then
begin
ResultPanel.RemoveComponent(Temp);
Temp.Free;
end;
end;
ResultPanel.HorzScrollBar.Range := MaxInt;
ResultPanel.VertScrollBar.Range:= MaxInt;
ResultPanel.DisableAlign;
ResultPanel.Description := Results.Name;
ResultPanel.ConnectionString := Results.ConnectionString;
Results.OwnsObjects := False;
aTop := 0;
for i := 0 to Results.Count - 1 do
begin
Grid := TStringGridEx(Results[i]);
Grid.Parent := ResultPanel;
ResultPanel.InsertComponent(Grid);
Grid.Align := alTop;
Grid.FixedCols := 0;
if Grid.RowCount > 1 then
Grid.FixedRows := 1
else
Grid.FixedRows := 0;
Grid.Options := Grid.Options + [goFixedVertLine,goFixedHorzLine,goVertLine,goHorzLine,goRangeSelect,goColSizing];
if (Grid.ColCount = 1) and (Grid.RowCount = 1) then Grid.Options := [];
Grid.Font.Assign(invGrid.Font);
Grid.PopupMenu := GridMenu;
k := 0;
for j := 0 to Grid.ColCount - 1 do
begin
Grid.ColWidths[j] := Grid.ColWidths[j] * Grid.Font.Size;
k := k + Grid.ColWidths[j];
end;
Grid.Width := k+50;
k := 0;
for j := 0 to Grid.RowCount - 1 do
begin
k := k + Grid.RowHeights[j]+ Grid.GridLineWidth;
end;
Grid.Height := k+18;
maxWidthGrid := Max(maxWidthGrid,Grid.Width);
aTop := aTop + Grid.Height + 1;
end;
ResultPanel.HorzScrollBar.Position := 0;
ResultPanel.HorzScrollBar.Range := maxWidthGrid;
ResultPanel.VertScrollBar.Position := 0;
ResultPanel.VertScrollBar.Range := aTop;
for k := Results.Count-1 downto 0 do
begin
if k = 0 then
Grid := TStringGridEx(Results[0]);
Results.Delete(k);
end;
if Assigned(Grid) and Grid.CanFocus then Grid.SetFocus;
finally
ResultPanel.EnableAlign;
ResultPanel.ShowMe;
end;
end;
function TconnectionForm.GetResultPanel: TExScrollBox;
begin
Result := TExScrollBox(TabControl.Tabs.Objects[TabControl.TabIndex]);
end;
procedure TconnectionForm.msModeActionExecute(Sender: TObject);
begin
if not TAction(Sender).Checked then
begin
TAction(Sender).Checked := True;
sybModeAction.Checked := False;
odbcModeAction.Checked:= False;
sybTreeView.HideMe;
odbcTreeView.HideMe;
msTreeView.ShowMe;
end;
end;
procedure TconnectionForm.msTreeViewMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
TreeView: TTreeViewEx;
Node: TTreeNode;
begin
TreeView := BasesPanel.CurrentTreeView;
if Assigned(TreeView) then
begin
Node := TreeView.GetNodeAt(X,Y);
if Assigned(Node) then Node.Selected := True;
end;
end;
procedure TconnectionForm.odbcModeActionExecute(Sender: TObject);
begin
if not TAction(Sender).Checked then
begin
TAction(Sender).Checked := True;
msModeAction.Checked := False;
sybModeAction.Checked:= False;
msTreeView.HideMe;
sybTreeView.HideMe;
odbcTreeView.ShowMe;
end;
end;
procedure TconnectionForm.OpenListBDActionExecute(Sender: TObject);
begin
if BasesPanel.Opened then
BasesPanel.Close
else
BasesPanel.Open;
end;
procedure TconnectionForm.SetCurrentNode(const Value: TTreeNode);
begin
FCurrentNode := Value;
end;
procedure TconnectionForm.SetMenuItemCheck(const Value: TMenuItemCheck);
begin
if Value <> FMenuItemCheck then
begin
SendMessage(Npp.NppData.NppHandle, NPPM_SETMENUITEMCHECK, CmdId, LPARAM(Value));
FMenuItemCheck := Value;
end;
end;
procedure TconnectionForm.SQLActionExecute(Sender: TObject);
var
S: string;
begin
if Sender is TMenuItem then
S := TMenuItem(Sender).Hint
else
S := '';
if not S.IsEmpty then
DoSql(S)
else
TdsPlugin(Npp).FuncExecSQL;
end;
procedure TconnectionForm.sybModeActionExecute(Sender: TObject);
begin
if not TAction(Sender).Checked then
begin
TAction(Sender).Checked := True;
msModeAction.Checked := False;
odbcModeAction.Checked:= False;
msTreeView.HideMe;
odbcTreeView.HideMe;
sybTreeView.ShowMe;
end;
end;
function TconnectionForm.CreateBox: TExScrollBox;
var
ResultPanel0: TExScrollBox;
begin
ResultPanel0:= TExScrollBox.Create(sql);
ResultPanel0.Parent := sql;
ResultPanel0.Color := clWhite;
ResultPanel0.DoubleBuffered := True;
ResultPanel0.Align := alClient;
ResultPanel0.BorderStyle := bsNone;
ResultPanel0.Visible := True;
Result := ResultPanel0;
end;
procedure TconnectionForm.HideAllExcept(const aIndex: Integer);
var
i: Integer;
begin
for i := 0 to TabControl.Tabs.Count-1 do
if TabControl.Tabs.Objects[i] <> nil then
begin
if i = aIndex then
begin
TExScrollBox(TabControl.Tabs.Objects[i]).ShowMe;
ResultLabel.Caption := TExScrollBox(TabControl.Tabs.Objects[i]).Description;
end
else
TExScrollBox(TabControl.Tabs.Objects[i]).HideMe
end;
end;
procedure TconnectionForm.TabControlChange(Sender: TObject);
var
i: Integer;
begin
i := TTabControl(Sender).TabIndex;
if not Assigned(TTabControl(Sender).Tabs.Objects[i]) then
begin
if i = MaxTabIndex then
TTabControl(Sender).TabIndex := i-1
else
begin
TTabControl(Sender).Tabs.Objects[i] := CreateBox;
with TTabControl(Sender).Tabs do
begin
Strings[i] := 'SQL';
AddObject('Add',nil);
end;
end;
end;
HideAllExcept(i);
end;
procedure TconnectionForm.WMThreadMessage(var Msg: TMessage);
var
ExecObject: TExecObject;
begin
ExecObject := TExecObject(Msg.LParam);
try
ExecObject.Finish;
finally
ExecObject.Free;
end;
end;
end.