forked from treejames/OneSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunMain.pas
More file actions
3167 lines (3038 loc) · 93.2 KB
/
unMain.pas
File metadata and controls
3167 lines (3038 loc) · 93.2 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
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit unMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBAccess, MemDS, ActnList, ImgList, OleCtrls, ComCtrls, DateUtils,
cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxContainer,
cxEdit, cxStyles, cxCustomData, cxFilter, cxData, cxDataStorage, cxDBData,
cxGridLevel, cxClasses, IniFiles, cxGridCustomView, cxGridCustomTableView,
cxGridTableView, cxGridDBTableView, cxGrid, cxMemo, Menus, StdCtrls,
cxButtons, cxGridDBDataDefinitions,
ExtCtrls, cxPC, cxSplitter, cxGridCustomPopupMenu, cxGridPopupMenu,
cxPCdxBarPopupMenu, cxTreeView, cxVGrid, cxDBVGrid, cxInplaceContainer,
cxGridExportLink, cxExport, cxNavigator, cxTextEdit, cxMaskEdit,
cxDropDownEdit, cxImageComboBox, ioutils, unEncrypt,
ScSshClient, //AsyncCalls,
System.UITypes, System.Types,
SynEdit, SynEditHighlighter, SynHighlighterSQL, StrUtils, ShellApi, SHFolder,
SynEditMiscClasses, SynEditSearch, SQLMemMain, cxBlobEdit, dxBarBuiltInMenu,
System.Actions, System.Character, SynDBEdit,
FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf,
FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool, FireDAC.Stan.Async,
FireDAC.Phys, FireDAC.Comp.Client, FireDAC.Stan.Param, FireDAC.DatS,
FireDAC.DApt.Intf, FireDAC.DApt, FireDAC.Comp.DataSet, FireDAC.Phys.Oracle,
FireDAC.Phys.OracleDef, FireDAC.VCLUI.Wait, FireDAC.Comp.UI,
FireDAC.Phys.MySQL, FireDAC.Phys.MySQLDef,
FireDAC.Phys.SQLiteDef, FireDAC.Stan.ExprFuncs, FireDAC.VCLUI.Async,
FireDAC.Phys.SQLite, FireDAC.Phys.PG, FireDAC.Phys.PGDef, FireDAC.Phys.ODBC,
FireDAC.Phys.ODBCDef, FireDAC.Phys.MSSQL, FireDAC.Phys.MSSQLDef;
const
maxParams = 20;
maxEditors = 30;
type
TParam = record
Active: String;
ParamName: String;
ParamType: String;
ParamValue: Variant;
end;
TParams = array [1 .. maxParams] of TParam;
TEditorParam = record
Editor: TSynEdit;
FileName: TFileName;
Params: TParams;
end;
TEditorParams = array [1 .. maxEditors] of TEditorParam;
TQueryThread = Class(TThread)
private
fQuery : TFDQuery;
fCommand: TFDCommand;
fError : String;
fDuration: Integer;
protected
procedure Execute; override;
public
constructor Create(aQuery : TFDQuery; aCommand: TFDCommand); reintroduce;
property Query: TFDQuery read fQuery;
property Command: TFDCommand read fCommand;
property Error: String read fError;
property Duration: Integer read fDuration;
End;
Tmain = class(TForm)
paTop: TPanel;
ac: TActionList;
acNewSession: TAction;
acSessionConnect: TAction;
acSessionDisconnect: TAction;
buNewSession: TcxButton;
paSessions: TPanel;
pcSessions: TcxPageControl;
buSessionManager: TcxButton;
acSessionManager: TAction;
buNewEditor: TcxButton;
acNewEditor: TAction;
pmTab: TPopupMenu;
pmClose: TMenuItem;
pmSaveAs: TMenuItem;
buOpenSQL: TcxButton;
acOpenSQL: TAction;
odSQL: TOpenDialog;
sdSQL: TSaveDialog;
acSaveSQL: TAction;
buSaveSQL: TcxButton;
buExportXLS: TcxButton;
buExportCSV: TcxButton;
sdExport: TSaveDialog;
buExportHTML: TcxButton;
buCommit: TcxButton;
buRollback: TcxButton;
tiOpen: TTimer;
buPreferences: TcxButton;
tiSearch: TTimer;
FindDialog: TFindDialog;
SynSearch: TSynEditSearch;
pmEditor: TPopupMenu;
miChangeQuote: TMenuItem;
buExportJSON: TcxButton;
buExit: TcxButton;
N1: TMenuItem;
miSelectForUpdate: TMenuItem;
buHistory: TcxButton;
buCancelExec: TcxButton;
guiCursor: TFDGUIxWaitCursor;
SQLiteLogCon: TFDConnection;
qLog: TFDCommand;
execDialog: TFDGUIxAsyncExecuteDialog;
FDConnection1: TFDConnection;
FDQuery1: TFDQuery;
procedure FormCreate(Sender: TObject);
procedure acNewSessionExecute(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure acSessionManagerExecute(Sender: TObject);
procedure acNewEditorExecute(Sender: TObject);
procedure pmCloseClick(Sender: TObject);
procedure pmSaveAsClick(Sender: TObject);
procedure acOpenSQLExecute(Sender: TObject);
procedure pcSessionsCanCloseEx(Sender: TObject; ATabIndex: Integer; var ACanClose: Boolean);
procedure acSaveSQLExecute(Sender: TObject);
procedure pcSessionsChange(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure buExportXLSClick(Sender: TObject);
procedure buExportCSVClick(Sender: TObject);
procedure buExportHTMLClick(Sender: TObject);
procedure buCommitClick(Sender: TObject);
procedure buRollbackClick(Sender: TObject);
procedure tiOpenTimer(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure buPreferencesClick(Sender: TObject);
procedure tiSearchTimer(Sender: TObject);
procedure miChangeQuoteClick(Sender: TObject);
procedure buExportJSONClick(Sender: TObject);
procedure buExitClick(Sender: TObject);
procedure miSelectForUpdateClick(Sender: TObject);
procedure pmEditorPopup(Sender: TObject);
procedure buHistoryClick(Sender: TObject);
procedure cxButton1Click(Sender: TObject);
private
EditorParams: TEditorParams;
cNullString: String;
lFitSmallColumnsToCaption: Boolean;
procedure InitParams(var P: TParams);
procedure InitEditorParams(var EP: TEditorParams);
procedure SaveParams(Editor: TSynEdit; FileName: TFileName; Params: TParams);
procedure FreeParams(Editor: TSynEdit);
function LoadParams(Editor: TSynEdit): TParams;
function ParamValue(Param: TParam): Variant;
function IsTextFile(const sFile: TFileName): Boolean;
procedure GetSessionObjects(Conn: TFDConnection);
procedure GetForeignKeys(Meta: TFDMetaInfoQuery; Root: TTreeNode; lCallback: Boolean = False);
function GetActiveEditorParamCount: Integer;
function GetDataType(ParamType: String): TFieldType;
function GetActiveEditorPC: TcxPageControl;
function GetObjectMemTable(Tab: TcxTabSheet): TSQLMemTable;
function GetSQLEditor(Control: TWinControl): TSynEdit;
function GetDataGridView(Control: TWinControl): TcxGridDBTableView;
function GetStatusGridView(Control: TWinControl): TcxGridTableView;
function GetSessionTab(SessionName: String): TcxTabSheet;
function GetSession(SessionName: String): TFDConnection;
function GetObjectInspector(Conn: TFDConnection): TcxTreeView;
function GetMetaData: TFDMetaInfoQuery;
function GetMetaDataInfoKind(Node: TTreeNode): TFDPhysMetaInfoKind;
procedure SetMetaData(Meta: TFDMetaInfoQuery);
function GetObjectInfoGrid(Control: TWinControl): TcxDBVerticalGrid;
function GetSyntaxType(Database: String): TSQLDialect;
function GetSyntaxSQL(Tab: TcxTabSheet): TSynSQLSyn;
function GetCommandSQL(Control: TWinControl): TFDCommand;
function GetCursorSQL(Text: String; CursorPos: Integer): String;
function GetSearchEdit(Conn: TFDConnection): TEdit;
function GetSpecialFolderPath(folder: Integer): String;
function GetCancelButton(Control: TWinControl): TcxButton;
function CreateSessionTab(SessionName: String): TcxTabSheet;
function CreateSession(SessionName: String): TFDConnection;
function AddSqlEditor(pcEditors: TcxPageControl; sCaption: String = ''): TcxTabSheet;
procedure ExecuteSQL(Tab: TcxTabSheet);
procedure OpenQuery(aTab: TcxTabSheet; aQuery: TFDQuery; aCommand: TFDCommand; aGridView: TcxGridDBTableView);
procedure DisconnectSSH(Tab: TcxTabSheet = nil);
procedure SaveSQL(Tab: TcxTabSheet);
procedure EditorButtonState;
procedure LoadEditorOptions;
procedure LoadGenericOptions;
procedure InitLogDB;
procedure ExportToFile(Format: String; DisableStyle: Boolean = False);
procedure ExportToJson(FileName: TFileName; GridView: TcxGridDBTableView);
procedure StyleComponents(Component: TComponent);
procedure DisplaySessionObjects;
function Log(Sess: String; Statement: String): Int64;
procedure AddStatus(EditorTab: TcxTabSheet; Status: String = ''; Error: String = '';
HistoryID: Int64 = 0);
procedure ObjectInspectorExpanding(Sender: TObject; Node: TTreeNode;
var AllowExpansion: Boolean);
procedure ObjectInspectorChange(Sender: TObject; Node: TTreeNode);
procedure OnEditorClose(Sender: TObject; ATabIndex: Integer; var ACanClose: Boolean);
procedure PageControlMouseClick(Sender: TObject; Button: TMouseButton; Shift: TShiftState;
X, Y: Integer);
procedure PageControlContexPopup(Sender: TObject; MousePos: TPoint; var Handled: Boolean);
procedure pcEditorOnChange(Sender: TObject);
procedure EditorOnChange(Sender: TObject);
procedure EditorOnKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure SearchKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure OnAutoCommitChange(Sender: TObject);
procedure CancelExecute(Sender: TObject);
procedure onGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
AItem: TcxCustomGridTableItem; var AStyle: TcxStyle);
procedure onGetDisplayText(Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
var AText: string);
procedure onStatusGridDblClick(Sender: TcxCustomGridTableView;
ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState;
var AHandled: Boolean);
function GetEditorFileName(Editor: TSynEdit): TFileName;
procedure OnBeforeQueryPost(DataSet: TDataSet);
procedure ThreadedMetaAfterOpen(DataSet: TDataSet);
function GetNodeByText(ATree: TcxTreeView; AValue: String;
AVisible: Boolean): TTreeNode;
public
procedure ShowError(sMessage: String);
procedure ExecuteSQLCallback(EditorTab: TcxTabSheet; Query: TFDQuery; SQL: TFDCommand;
GridView: TcxGridDBTableView; Error: String);
end;
var
main: Tmain;
implementation
uses unParamForm, unSessionForm, unDm, unSessionManager, unPreferences,
unHistory;
{$R *.dfm}
type
TcxPageControlPropertiesAccess = class(TcxPageControlProperties);
constructor TQueryThread.Create(aQuery: TFDQuery; aCommand: TFDCommand);
begin
inherited Create(true);
fQuery := aQuery;
fCommand := aCommand;
fError := '';
fDuration := 0;
end;
procedure TQueryThread.Execute;
var
iTime: TDateTime;
begin
try
iTime := now;
if (fCommand <> nil) then
fCommand.Execute
else
fQuery.Open;
fDuration := MilliSecondsBetween(now, iTime);
except on E: Exception do
begin
fError := E.Message;
end;
end;
end;
procedure Tmain.OpenQuery(aTab: TcxTabSheet; aQuery: TFDQuery; aCommand: TFDCommand; aGridView: TcxGridDBTableView);
var
t: array of THandle;
n: Cardinal;
worker: TQueryThread;
begin
worker := TQueryThread.Create(aQuery, aCommand);
try
worker.Start;
setLength(t, 1);
t[0] := worker.Handle;
while true do
begin
n := MsgWaitForMultipleObjects(length(t), t[0], false, INFINITE, QS_ALLINPUT);
case n of
WAIT_OBJECT_0: break;
else
Application.ProcessMessages;
end;
end;
finally
aTab.Hint := FormatFloat('0.000', worker.Duration /1000) + ' sec';
ExecuteSQLCallback(aTab, aQuery, aCommand, aGridView, worker.Error);
worker.Terminate;
worker.WaitFor;
FreeAndNil(worker);
end;
end;
procedure Tmain.pmCloseClick(Sender: TObject);
var
PC: TcxPageControl;
begin
PC := GetActiveEditorPC;
if PC = nil then
Exit;
FreeParams(GetSQLEditor(PC.ActivePage));
PC.ActivePage.Free;
EditorButtonState;
end;
procedure Tmain.pmEditorPopup(Sender: TObject);
begin
miSelectForUpdate.Checked :=
GetDataGridView(TWinControl(TPopupMenu(Sender)
.PopupComponent.GetParentComponent.GetParentComponent)).DataController.DataSource.AutoEdit;
if miSelectForUpdate.Checked then
miSelectForUpdate.ImageIndex := 0
else
miSelectForUpdate.ImageIndex := -1;
end;
procedure Tmain.buRollbackClick(Sender: TObject);
var
PC: TcxPageControl;
Conn: TFDConnection;
begin
PC := GetActiveEditorPC;
if PC = nil then
Exit;
Conn := GetSession(pcSessions.ActivePage.Caption);
if not Conn.InTransaction then
begin
AddStatus(PC.ActivePage, 'Nothing to rollback');
Exit;
end;
Conn.Rollback;
AddStatus(PC.ActivePage, 'Rollback executed at ' + DateTimeToStr(now));
end;
procedure Tmain.buCommitClick(Sender: TObject);
var
PC: TcxPageControl;
Conn: TFDConnection;
begin
PC := GetActiveEditorPC;
if PC = nil then
Exit;
Conn := GetSession(pcSessions.ActivePage.Caption);
if not Conn.InTransaction then
begin
AddStatus(PC.ActivePage, 'Nothing to commit');
Exit;
end;
Conn.Commit;
AddStatus(PC.ActivePage, 'Commit executed at ' + DateTimeToStr(now));
end;
procedure Tmain.buExportXLSClick(Sender: TObject);
begin
ExportToFile('xls', True);
end;
procedure Tmain.buHistoryClick(Sender: TObject);
var
i: Integer;
SynEdit: TSynEdit;
sHistorySQL: String;
begin
StyleComponents(history);
with history.qHistory do
begin
Close;
for i := 0 to ParamCount - 1 do
Params[i].Value := null;
ParamByName('P_SESSION').Value := pcSessions.ActivePage.Caption;
end;
history.ShowModal;
if history.ModalResult = mrOK then
begin
SynEdit := GetSQLEditor(GetActiveEditorPC.ActivePage);
sHistorySQL := history.qHistory.FieldByName('statement').AsString;
if Length(SynEdit.Text) = 0 then
SynEdit.Text := SynEdit.Text + sHistorySQL
else
SynEdit.Text := SynEdit.Text + #13#10 + #13#10 + sHistorySQL;
SynEdit.SelStart := Length(SynEdit.Text) - Length(sHistorySQL);
end;
end;
procedure Tmain.buPreferencesClick(Sender: TObject);
var
Pref: TPreferences;
begin
Pref := TPreferences.Create(Self);
try
if Pref.ShowModal = mrOK then
begin
StyleComponents(main);
LoadGenericOptions;
end;
finally
Pref.Free;
end;
end;
procedure Tmain.StyleComponents(Component: TComponent);
var
i: Integer;
begin
for i := 0 to Component.ComponentCount - 1 do
begin
if (Component.Components[i] is TSynSQLSyn) or (Component.Components[i] is TDBSynEdit) or
(Component.Components[i] is TSynEdit) or (Component.Components[i] is TcxGrid) then
dm.Style(Component.Components[i]);
StyleComponents(Component.Components[i]);
end;
end;
procedure Tmain.buExitClick(Sender: TObject);
begin
Close;
end;
procedure Tmain.buExportCSVClick(Sender: TObject);
begin
ExportToFile('csv');
end;
procedure Tmain.buExportHTMLClick(Sender: TObject);
begin
ExportToFile('html');
end;
procedure Tmain.buExportJSONClick(Sender: TObject);
begin
ExportToFile('json');
end;
procedure Tmain.CancelExecute(Sender: TObject);
var
Grid: TcxGridDBTableView;
DataSet: TFDQuery;
begin
Grid := GetDataGridView(GetActiveEditorPC.ActivePage);
if Grid = nil then
Exit;
TcxTabSheet(GetStatusGridView(GetActiveEditorPC.ActivePage).Control.Parent).Caption := 'Canceling...';
TcxTabSheet(GetStatusGridView(GetActiveEditorPC.ActivePage).Control.Parent).ImageIndex := 28;
TcxButton(Sender).Enabled := False;
DataSet := TFDQuery(Grid.DataController.DataSource.DataSet);
DataSet.Tag := 1;
DataSet.Connection.AbortJob;
end;
procedure Tmain.cxButton1Click(Sender: TObject);
begin
GetSessionObjects(GetSession(pcSessions.ActivePage.Caption));
end;
procedure Tmain.onStatusGridDblClick(Sender: TcxCustomGridTableView;
ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState;
var AHandled: Boolean);
begin
history.iLookupId := TcxGridTableView(Sender).Items[0].EditValue;
if history.iLookupId <> 0 then
buHistory.Click;
end;
procedure Tmain.onGetDisplayText(Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
var AText: string);
begin
if ARecord.Values[Sender.Index] = null then
AText := cNullString;
end;
procedure Tmain.onGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
AItem: TcxCustomGridTableItem; var AStyle: TcxStyle);
begin
try
if (ARecord.IsData) and (Assigned(AItem)) and (ARecord.Values[AItem.Index] = null) then
AStyle := dm.NullString;
except
end;
end;
procedure Tmain.OnAutoCommitChange(Sender: TObject);
var
Conn: TFDConnection;
iCommit: Integer;
begin
if TcxImageComboBox(Sender).Tag = 1 then
begin
TcxImageComboBox(Sender).Tag := 0;
Exit;
end;
Conn := GetSession(pcSessions.ActivePage.Caption);
if Conn = nil then
Exit;
if (not Conn.TxOptions.AutoCommit) and (Conn.InTransaction) then
begin
iCommit := messagedlg('Non-commited changes found! Commit?', mtCustom, [mbYes,mbNo,mbCancel], 0);
if iCommit = mrYes then Conn.Commit;
if iCommit = mrNo then Conn.Rollback;
if iCommit = mrCancel then
begin
TcxImageComboBox(Sender).Tag := 1;
TcxImageComboBox(Sender).ItemIndex := 1;
Exit;
end;
end;
Conn.TxOptions.AutoCommit := TcxImageComboBox(Sender).ItemIndex = 0;
EditorButtonState;
end;
procedure Tmain.ObjectInspectorChange(Sender: TObject; Node: TTreeNode);
var
Meta: TFDMetaInfoQuery;
VGrid: TcxDBVerticalGrid;
begin
VGrid := GetObjectInfoGrid(pcSessions.ActivePage);
VGrid.ClearRows;
if (Node.Text = 'Columns') or (Node.Text = 'Constraints') or (Node.Text = 'Indexes') or
(pos('Tables', Node.Text) > 0) or (pos('Procedures', Node.Text) > 0) or
(pos('Functions', Node.Text) > 0) or (pos('Packages', Node.Text) > 0) then
begin
Exit;
end;
Meta := GetMetaData;
with Meta do
begin
ObjectName := '';
BaseObjectName := '';
Active := False;
Filter := '';
Filtered := False;
FilterOptions := [foCaseInsensitive];
MetaInfoKind := GetMetaDataInfoKind(Node);
case MetaInfoKind of
mkTables: begin
ObjectName := Node.Text;
Filter := 'table_name=''' + Node.Text + '''';
Filtered := True;
end;
mkTableFields: begin
ObjectName := Node.Parent.Parent.Text;
Filter := 'column_name=''' + Node.Text + '''';
Filtered := True;
end;
mkProcs: begin
BaseObjectName := Node.Text;
end;
mkProcArgs: begin
ObjectName := Node.Text;
{ Set Package as BaseObjectName if proc/func is within the package }
if (Node.Parent.Parent <> nil) and (pos('Packages', Node.Parent.Parent.Text) > 0) then
BaseObjectName := Node.Parent.Text;
end;
mkPrimaryKeyFields, mkForeignKeyFields, mkIndexFields: begin
ObjectName := Node.Text;
BaseObjectName := Node.Parent.Parent.Text;
end;
end;
Meta.Active := True;
{ Refresh Vertical grid }
VGrid.DataController.CreateAllItems;
end;
end;
procedure Tmain.ObjectInspectorExpanding(Sender: TObject; Node: TTreeNode;
var AllowExpansion: Boolean);
var
Conn: TFDConnection;
Meta: TFDMetaInfoQuery;
TypeNode, Item, ConstraintNode: TTreeNode;
begin
if Node.Level <> 1 then
Exit;
if Node.Count > 0 then
Exit;
Conn := GetSession(pcSessions.ActivePage.Caption);
if Conn = nil then
Exit;
Meta := GetMetaData;
Meta.Connection := Conn;
Meta.Active := False;
try
{ Package procedures/functions}
if pos('Packages', Node.Parent.Text) > 0 then
begin
Meta.MetaInfoKind := mkProcs;
Meta.BaseObjectName := Node.Text;
Meta.Active := True;
while not Meta.Eof do
begin
Item := TcxTreeView(Node.TreeView.Parent).Items.AddChild(Node, Meta.FieldByName('PROC_NAME').AsString);
if (Meta.FieldByName('PROC_TYPE').AsInteger = 1) then
Item.ImageIndex := 24
else
Item.ImageIndex := 16;
Item.SelectedIndex := Item.ImageIndex;
Meta.Next;
end;
end;
if pos('Tables', Node.Parent.Text) > 0 then
begin
{ Colums }
TypeNode := TcxTreeView(Node.TreeView.Parent).Items.AddChild(Node, 'Columns');
TypeNode.ImageIndex := 11;
TypeNode.SelectedIndex := TypeNode.ImageIndex;
Meta.ObjectName := Node.Text;
Meta.MetaInfoKind := mkTableFields;
Meta.Active := True;
while not Meta.Eof do
begin
Item := TcxTreeView(Node.TreeView.Parent).Items.AddChild(TypeNode,
Meta.FieldByName('COLUMN_NAME').AsString);
Item.ImageIndex := 11;
Item.SelectedIndex := Item.ImageIndex;
Meta.Next;
end;
Meta.Active := False;
{ Constraints }
ConstraintNode := TcxTreeView(Node.TreeView.Parent).Items.AddChild(Node, 'Constraints');
ConstraintNode.ImageIndex := 13;
ConstraintNode.SelectedIndex := ConstraintNode.ImageIndex;
{ Primary keys }
Meta.MetaInfoKind := mkPrimaryKey;
Meta.Active := True;
while not Meta.Eof do
begin
Item := TcxTreeView(Node.TreeView.Parent).Items.AddChild(ConstraintNode,
Meta.FieldByName('PKEY_NAME').AsString);
Item.Data := Pointer(1);
Item.ImageIndex := 12;
Item.SelectedIndex := Item.ImageIndex;
Meta.Next;
end;
Meta.Active := False;
Meta.Filtered := False;
Meta.Filter := '';
{ Indexes }
TypeNode := TcxTreeView(Node.TreeView.Parent).Items.AddChild(Node, 'Indexes');
TypeNode.ImageIndex := 25;
TypeNode.SelectedIndex := TypeNode.ImageIndex;
Meta.MetaInfoKind := mkIndexes;
Meta.Active := True;
while not Meta.Eof do
begin
Item := TcxTreeView(Node.TreeView.Parent).Items.AddChild(TypeNode,
Meta.FieldByName('INDEX_NAME').AsString);
Item.ImageIndex := 15;
Item.SelectedIndex := Item.ImageIndex;
Meta.Next;
end;
Meta.Active := False;
{ Slow Meta data are fetched in separate thread }
{ Foreign Keys }
GetForeignKeys(Meta, ConstraintNode);
end;
finally
Meta.Active := False;
end;
end;
procedure Tmain.GetForeignKeys(Meta: TFDMetaInfoQuery; Root: TTreeNode; lCallback: Boolean = False);
var
MetaTemplate: TFDMetaInfoQuery;
sLastAdded: String;
Item: TTreeNode;
begin
if (not lCallback) then
begin
MetaTemplate := Meta;
Meta := TFDMetaInfoQuery.Create(MetaTemplate.Owner);
Meta.Name := MetaTemplate.Name + IntToStr(MilliSecondsBetween(Now, 0));
Meta.Connection := MetaTemplate.Connection;
Meta.CatalogName := MetaTemplate.CatalogName;
Meta.SchemaName := MetaTemplate.SchemaName;
Meta.ObjectName := MetaTemplate.ObjectName;
Meta.BaseObjectName := MetaTemplate.BaseObjectName;
Meta.ResourceOptions.CmdExecMode := amAsync;
Meta.Tag := 1;
Meta.Active := False;
Meta.MetaInfoKind := mkForeignKeys;
if Meta.CatalogName <> '' then
Meta.Filter := 'pkey_catalog_name=''' + Meta.CatalogName + '''';
if Meta.SchemaName <> '' then
begin
if Meta.Filter <> '' then
Meta.Filter := Meta.Filter + ' and pkey_schema_name=''' + Meta.SchemaName + ''''
else
Meta.Filter := 'pkey_schema_name=''' + Meta.SchemaName + '''';
end;
if Meta.Filter <> '' then
Meta.Filtered := True;
Meta.AfterOpen := ThreadedMetaAfterOpen;
Meta.Open;
end
else
begin
sLastAdded := '';
while not Meta.Eof do
begin
if sLastAdded = Meta.FieldByName('FKEY_NAME').AsString then
begin
Meta.Next;
Continue;
end;
Item := TcxTreeView(Root.TreeView.Parent).Items.AddChild(Root, Meta.FieldByName('FKEY_NAME').AsString);
sLastAdded := Meta.FieldByName('FKEY_NAME').AsString;
Item.ImageIndex := 13;
Item.SelectedIndex := Item.ImageIndex;
Meta.Next;
end;
Meta.Active := False;
FreeAndNil(Meta);
end;
end;
procedure Tmain.PageControlContexPopup(Sender: TObject; MousePos: TPoint; var Handled: Boolean);
var
i: Integer;
begin
with Sender as TcxPageControl do
begin
PopupMenu := nil;
i := IndexOfTabAt(MousePos.X, MousePos.Y);
if i > -1 then
begin
PopupMenu := pmTab;
end;
end;
end;
procedure Tmain.PageControlMouseClick(Sender: TObject; Button: TMouseButton; Shift: TShiftState;
X, Y: Integer);
var
i: Integer;
begin
if Button = mbRight then
begin
i := TcxPageControl(Sender).IndexOfTabAt(X, Y);
if i > -1 then
TcxPageControl(Sender).ActivePageIndex := i;
end;
end;
procedure Tmain.acNewSessionExecute(Sender: TObject);
var
SessionF: TsessionForm;
mr: Integer;
begin
SessionF := TsessionForm.Create(Self);
dm.connection_name := '';
mr := SessionF.ShowModal;
SessionF.Destroy;
if mr = mrOK then
buSessionManager.Click;
end;
procedure Tmain.acOpenSQLExecute(Sender: TObject);
var
Tab: TcxTabSheet;
Memo: TSynEdit;
begin
if pcSessions.PageCount = 0 then
Exit;
if odSQL.Execute then
begin
if not IsTextFile(odSQL.FileName) then
begin
ShowMessage('Not a text file!');
Exit;
end;
Tab := AddSqlEditor(GetActiveEditorPC, ExtractFileName(odSQL.FileName));
if Tab = nil then
Exit;
Tab.Tag := 1;
Memo := GetSQLEditor(Tab);
SaveParams(Memo, odSQL.FileName, LoadParams(Memo));
Memo.Lines.LoadFromFile(odSQL.FileName);
end;
end;
procedure Tmain.acSaveSQLExecute(Sender: TObject);
var
PC: TcxPageControl;
begin
if pcSessions.PageCount = 0 then
Exit;
PC := GetActiveEditorPC;
if PC = nil then
Exit;
SaveSQL(PC.ActivePage);
end;
procedure Tmain.SaveSQL(Tab: TcxTabSheet);
var
Memo: TSynEdit;
sSQL: String;
begin
Memo := GetSQLEditor(Tab);
sdSQL.FileName := GetEditorFileName(Memo);
if sdSQL.FileName = '' then
if pos('*', Tab.Caption) > 0 then
sdSQL.FileName := copy(Tab.Caption, 1, Length(Tab.Caption) - 2)
else
sdSQL.FileName := Tab.Caption;
if Tab.Tag = 1 then
begin
sSQL := Memo.Lines.Text;
if dm.LineEnding = 1 then
sSQL := StringReplace(sSQL, #13#10, #10, [rfReplaceAll])
else if dm.LineEnding = 2 then
sSQL := StringReplace(sSQL, #13#10, #13, [rfReplaceAll]);
TFile.WriteAllText(sdSQL.FileName, sSQL, TEncoding.ANSI);
Tab.Caption := ExtractFileName(sdSQL.FileName);
Tab.Tag := 1;
end
else
begin
if sdSQL.Execute then
begin
if TFile.Exists(sdSQL.FileName) then
TFile.Delete(sdSQL.FileName);
sSQL := Memo.Lines.Text;
if dm.LineEnding = 1 then
sSQL := StringReplace(sSQL, #13#10, #10, [rfReplaceAll])
else if dm.LineEnding = 2 then
sSQL := StringReplace(sSQL, #13#10, #13, [rfReplaceAll]);
TFile.AppendAllText(sdSQL.FileName, sSQL, TEncoding.ANSI);
Tab.Caption := ExtractFileName(sdSQL.FileName);
SaveParams(Memo, sdSQL.FileName, LoadParams(Memo));
Tab.Tag := 1;
end;
end;
EditorButtonState;
end;
procedure Tmain.ShowError(sMessage: String);
begin
ShowMessage(sMessage);
end;
procedure Tmain.tiOpenTimer(Sender: TObject);
var
IniFile: TIniFile;
Sections: TStringList;
begin
tiOpen.Enabled := False;
IniFile := dm.GetSessionIniFile;
Sections := TStringList.Create;
try
IniFile.ReadSections(Sections);
if Sections.Count = 0 then
buNewSession.Click
else
buSessionManager.Click;
finally
IniFile.Free;
Sections.Free;
end;
end;
procedure Tmain.tiSearchTimer(Sender: TObject);
begin
DisplaySessionObjects;
tiSearch.Enabled := False;
end;
procedure Tmain.OnBeforeQueryPost(DataSet: TDataSet);
begin
if (not TFDQuery(DataSet).Connection.TxOptions.AutoCommit) and
(not TFDQuery(DataSet).Connection.InTransaction) then
TFDQuery(DataSet).Connection.StartTransaction;
end;
procedure Tmain.acSessionManagerExecute(Sender: TObject);
var
SessionF: TsessionForm;
mr: Integer;
Tab: TcxTabSheet;
Sess: String;
begin
sessionManager.ShowModal;
if dm.connection_name <> '' then
begin
SessionF := TsessionForm.Create(Self);
mr := SessionF.ShowModal;
SessionF.Destroy;
if mr = mrOK then
buSessionManager.Click;
end
else if dm.new_session <> '' then
begin
try
Tab := CreateSessionTab(dm.new_session);
if Tab = nil then
begin
Tab := GetSessionTab(dm.new_session);
if Tab <> nil then
begin
DisconnectSSH(Tab);
Tab.Free;
end;
end;
Sess := dm.new_session;
finally
dm.new_session := '';
end;
end;
end;
function Tmain.GetActiveEditorParamCount: Integer;
var
i: Integer;
begin
Result := 0;
for i := 1 to maxEditors do
if EditorParams[i].Editor <> nil then
Inc(Result);
end;
function Tmain.AddSqlEditor(pcEditors: TcxPageControl; sCaption: String): TcxTabSheet;
var
Tab, tsGrid, tsStatus: TcxTabSheet;
pcResults: TcxPageControl;
Conn: TFDConnection;
paEditor, paGrids: TPanel;
cxDataGrid, cxStatusGrid: TcxGrid;
cxDataGridLevel, cxStatusGridLevel: TcxGridLevel;
cxDataGridView: TcxGridDBTableView;
cxStatusGridView: TcxGridTableView;
Col: TcxGridColumn;
colICB: TcxImageComboBoxItem;
Query: TFDQuery;
SQL: TFDCommand;
DataSource: TDataSource;
Memo: TSynEdit;
Cap: String;
i: Integer;
Splitter: TSplitter;
begin
Result := nil;
if pcEditors = nil then
Exit;
if GetActiveEditorParamCount >= maxEditors then
begin
ShowMessage
('Max number of SQL editors reached! Please close some of unused SQL editors before creating a new one!');
Exit;
end;
Conn := GetSession(TcxTabSheet(pcEditors.Parent).Caption);
{ Create Editor Tab }
Tab := TcxTabSheet.Create(pcEditors);
with Tab do
begin
PageControl := pcEditors;
pcEditors.Tag := pcEditors.Tag + 1;
Name := pcEditors.Name + 'Editor' + IntToStr(pcEditors.Tag);
if sCaption <> '' then
Cap := sCaption
else
Cap := 'Editor ' + IntToStr(pcEditors.Tag);
Caption := Cap;
BorderWidth := 4;
end;
pcEditors.ActivePage := Tab;
{ Create SQL editor panel - for TSynEdit }
paEditor := TPanel.Create(Tab);
with paEditor do
begin
Name := 'paMemo' + pcEditors.Name + IntToStr(pcEditors.Tag);
BevelOuter := bvNone;
Align := alClient;
AlignWithMargins := True;
Margins.Top := 0;
Margins.Right := 0;
Margins.Bottom := 4;
Margins.Left := 0;
Caption := '';
Parent := Tab;
end;
{ Create SQL editor - TSynEdit }
Memo := TSynEdit.Create(paEditor);
with Memo do
begin
Name := 'seSQL' + pcEditors.Name + IntToStr(pcEditors.Tag);
Align := alClient;
Lines.Clear;
WantTabs := True;
TabWidth := 2;
RightEdge := 0;
Options := Options - [eoSmartTabs, eoTabsToSpaces, eoScrollPastEol] +
[eoAltSetsColumnMode, eoTabIndent];
Gutter.ShowLineNumbers := True;
Gutter.LeftOffset := 0;