forked from motaz/turbobird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.pas
4243 lines (3792 loc) · 127 KB
/
main.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
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 main;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, IBConnection, sqldb, memds, FileUtil, LResources, Forms,
Controls, Graphics, Dialogs, Menus, ComCtrls, Reg, QueryWindow, Grids,
ExtCtrls, Buttons, StdCtrls, TableManage, dbugintf, turbocommon;
{$i turbocommon.inc}
type
TDatabaseRec = record
Index: Integer;
RegRec: TRegisteredDatabase;
OrigRegRec: TRegisteredDatabase;
IBConnection: TIBConnection;
SQLTrans: TSQLTransaction;
end;
{ TfmMain }
TfmMain = class(TForm)
bbCreateNewDB: TBitBtn;
bbRegisterDB: TBitBtn;
bbRestoreDB: TBitBtn;
ImageList1: TImageList;
MainMenu1: TMainMenu;
mdsHistory: TMemDataset;
MenuItem1: TMenuItem;
lmDisplayView: TMenuItem;
lmViewTrigger: TMenuItem;
lmCreateDB: TMenuItem;
lmRegdb: TMenuItem;
lmRestore: TMenuItem;
lmSweep: TMenuItem;
lmAddUser: TMenuItem;
lmChangePassword: TMenuItem;
lmUserPermManagement: TMenuItem;
lmRolePerManagement: TMenuItem;
lmSetGen: TMenuItem;
lmDisconnect: TMenuItem;
lmCopyTable: TMenuItem;
lmCopyUserPermission: TMenuItem;
lmViewFields: TMenuItem;
lmEditField: TMenuItem;
lmDBInfo: TMenuItem;
lmCopyRolePermission: TMenuItem;
lmCompare: TMenuItem;
lmGetIncrementGen: TMenuItem;
lmDropTable: TMenuItem;
lmRecalculateStatistics: TMenuItem;
mnExit: TMenuItem;
mnCreateDB: TMenuItem;
mnRegDB: TMenuItem;
MenuItem5: TMenuItem;
MenuItem6: TMenuItem;
lmEditReg: TMenuItem;
lmUnregisterDatabase: TMenuItem;
lmViewFirst1000: TMenuItem;
lmViewStoredProcedure: TMenuItem;
lmViewGen: TMenuItem;
lmNewTable: TMenuItem;
lmNewGen: TMenuItem;
lmCreateAutoInc: TMenuItem;
lmCreateStoredProc: TMenuItem;
lmEditProc: TMenuItem;
lmCreateView: TMenuItem;
lmDisplay1000V: TMenuItem;
lmEditView: TMenuItem;
lmCreateTrigger: TMenuItem;
lmEditTrigger: TMenuItem;
lmActivateTrig: TMenuItem;
lmDeactiveTrig: TMenuItem;
lmScriptTable: TMenuItem;
lmScriptTableCreate: TMenuItem;
lmScriptInsert: TMenuItem;
lmScriptUpdate: TMenuItem;
lmEditTable: TMenuItem;
lmCallStoreProc: TMenuItem;
lmEditDataForm: TMenuItem;
lmNewUDF: TMenuItem;
lmViewUDF: TMenuItem;
lmOpenSystemTable: TMenuItem;
lmViewDomain: TMenuItem;
lmNewDomain: TMenuItem;
lmNewRole: TMenuItem;
MenuItem7: TMenuItem;
lmOpenQuery: TMenuItem;
lmNewException: TMenuItem;
lmRefresh: TMenuItem;
lmDropException: TMenuItem;
lmScriptException: TMenuItem;
lmScriptDatabase: TMenuItem;
lmConnectAs: TMenuItem;
lmPermissions: TMenuItem;
lmRolePermissions: TMenuItem;
lmTableManage: TMenuItem;
MenuItem8: TMenuItem;
lmBackup: TMenuItem;
mnRestore: TMenuItem;
PageControl1: TPageControl;
pmDatabase: TPopupMenu;
Splitter1: TSplitter;
SQLQuery1: TSQLQuery;
StatusBar1: TStatusBar;
TabSheet1: TTabSheet;
tvMain: TTreeView;
procedure FormActivate(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure lmAddUserClick(Sender: TObject);
procedure lmBackupClick(Sender: TObject);
procedure lmChangePasswordClick(Sender: TObject);
procedure lmCompareClick(Sender: TObject);
procedure lmCopyRolePermissionClick(Sender: TObject);
procedure lmCopyUserPermissionClick(Sender: TObject);
procedure lmCopyTableClick(Sender: TObject);
procedure lmCreateDBClick(Sender: TObject);
procedure lmDBInfoClick(Sender: TObject);
procedure lmDisconnectClick(Sender: TObject);
procedure lmEditFieldClick(Sender: TObject);
procedure lmGetIncrementGenClick(Sender: TObject);
// Show all records in table
procedure lmOpenSystemTableClick(Sender: TObject);
procedure lmActivateTrigClick(Sender: TObject);
procedure lmCallStoreProcClick(Sender: TObject);
procedure lmConnectAsClick(Sender: TObject);
procedure lmCreateAutoIncClick(Sender: TObject);
procedure lmCreateStoredProcClick(Sender: TObject);
procedure lmCreateTriggerClick(Sender: TObject);
procedure lmCreateViewClick(Sender: TObject);
procedure lmDeactiveTrigClick(Sender: TObject);
procedure lmDisplay1000VClick(Sender: TObject);
procedure lmDropExceptionClick(Sender: TObject);
procedure lmEditDataFormClick(Sender: TObject);
procedure lmEditProcClick(Sender: TObject);
procedure lmEditTableClick(Sender: TObject);
procedure lmEditTriggerClick(Sender: TObject);
procedure lmEditViewClick(Sender: TObject);
procedure lmNewDomainClick(Sender: TObject);
procedure lmNewExceptionClick(Sender: TObject);
procedure lmNewGenClick(Sender: TObject);
procedure lmNewTableClick(Sender: TObject);
procedure lmNewUDFClick(Sender: TObject);
procedure lmOpenQueryClick(Sender: TObject);
procedure lmPermissionsClick(Sender: TObject);
procedure lmRefreshClick(Sender: TObject);
procedure lmRegdbClick(Sender: TObject);
procedure lmRestoreClick(Sender: TObject);
procedure lmRolePerManagementClick(Sender: TObject);
procedure lmRolePermissionsClick(Sender: TObject);
procedure lmScriptDatabaseClick(Sender: TObject);
procedure lmScriptExceptionClick(Sender: TObject);
procedure lmScriptInsertClick(Sender: TObject);
procedure lmScriptTableCreateClick(Sender: TObject);
procedure lmScriptUpdateClick(Sender: TObject);
procedure lmSetGenClick(Sender: TObject);
procedure lmSweepClick(Sender: TObject);
procedure lmTableManageClick(Sender: TObject);
procedure lmUserPermManagementClick(Sender: TObject);
procedure lmViewDomainClick(Sender: TObject);
procedure lmDisplayViewClick(Sender: TObject);
// Expand table field nodes
procedure lmViewFieldsClick(Sender: TObject);
procedure lmViewGenClick(Sender: TObject);
procedure lmViewStoredProcedureClick(Sender: TObject);
procedure lmViewTriggerClick(Sender: TObject);
procedure lmViewUDFClick(Sender: TObject);
procedure lmDropTableClick(Sender: TObject);
procedure lmRecalculateStatisticsClick(Sender: TObject);
procedure mnExitClick(Sender: TObject);
procedure mnCreateDBClick(Sender: TObject);
procedure mnRegDBClick(Sender: TObject);
procedure MenuItem6Click(Sender: TObject);
procedure lmEditRegClick(Sender: TObject);
procedure lmUnregisterDatabaseClick(Sender: TObject);
procedure lmViewFirst1000Click(Sender: TObject);
procedure lmNewRoleClick(Sender: TObject);
procedure mnRestoreClick(Sender: TObject);
procedure PageControl1CloseTabClicked(Sender: TObject);
procedure pmDatabasePopup(Sender: TObject);
procedure tvMainDblClick(Sender: TObject);
procedure tvMainExpanded(Sender: TObject; Node: TTreeNode);
procedure GlobalException(Sender: TObject; E : Exception);
private
FIBConnection: TIBConnection;
FSQLTransaction: TSQLTransaction;
FCurrentHistoryFile: string;
FActivated: Boolean;
function FindCustomForm(ATitle: string; AClass: TClass): TComponent;
// Show new generator form
procedure InitNewGen(DatabaseIndex: Integer);
function GetServerNameNode(ServerName: string): TTreeNode;
// Remove RegisteredDatabases and clean up memory held by its objects
procedure ReleaseRegisteredDatabases;
// Set connection for SQLQuery1 to selected registered database
procedure SetConnection(Index: Integer);
procedure SetFocus; override; // solve a bug in Lazarus
protected
// This procedure will receive the events that are logged by the connection:
procedure GetLogEvent(Sender: TSQLConnection; EventType: TDBEventType; Const Msg : String);
public
// Array of database connection details as stored in turbobird.reg file
RegisteredDatabases: array of TDatabaseRec;
Version: string;
VersionDate: string;
Major, Minor, ReleaseVersion: word;
function GetServerName(DBName: string): string;
function RetrieveInputParamFromSP(Body: string): string;
// Load registered databases from file and show them in treeview
function LoadRegisteredDatabases: Boolean;
function FindQueryWindow(ATitle: string): TComponent;
function DeleteRegistration(Index: Integer): Boolean;
// Returns BLOB subtype clause depending on subtype
function GetBlobSubTypeName(SubType: integer): string;
// Get name of index used for primary key
// Also returns name of constraint used
function GetPrimaryKeyIndexName(DatabaseIndex: Integer; ATableName: string; var ConstraintName: string): string;
// Get primary key field(s) names into KeyFields
function GetPrimaryKeyFields(DatabaseIndex: Integer; ATableName: string; var KeyFields: TStringList): boolean;
function GetConstraintFields(ATableName, AIndexName: string; var List: TStringList): Boolean;
// Get fields information for specified table
// Fills SQLQuery1 with details
procedure GetFields(DatabaseIndex: Integer; ATableName: string; FieldsList: TStringList);
// Get body of a stored procedure (without SET TERM... clauses)
// Fills SQLQuery1 with details
function GetStoredProcBody(DatabaseIndex: Integer; AProcName: string; var SPOwner: string): string;
// Get body and output parameters of a view
// Does *not* fill SQLQuery1 with details
function GetViewInfo(DatabaseIndex: Integer; AViewName: string; var Columns, Body: string): Boolean;
function ChangeTriggerActivity(DatabaseIndex: Integer; ATriggerName: string; ActiveState: Boolean): Boolean;
function GetIndices(ATableName: string; AQuery: TSQLQuery): Boolean;
function GetIndexFields(ATableName, AIndexName: string; AQuery: TSQLQuery; var FieldsList: TStringList): Boolean;
function GetUDFInfo(DatabaseIndex: Integer; UDFName: string; var ModuleName, EntryPoint, Params: string): Boolean;
function ShowQueryWindow(DatabaseIndex: Integer; ATitle: string): TfmQueryWindow;
procedure FillObjectRoot(Node: TTreeNode);
procedure FillAndShowConstraintsForm(Form: TfmTableManage; ATableName: string; dbIndex: Integer);
procedure ShowCompleteQueryWindow(DatabaseIndex: Integer; ATitle, AQueryText: string;
OnCommitProcedure: TNotifyEvent = nil);
// Gets fields info and fills TableManage form(!) grids with info
procedure ViewTableFields(ATableName: string; dbIndex: Integer; AStringGrid: TStringGrid);
procedure ShowIndicesManagement(AForm: TForm; DatabaseIndex: Integer; ATableName: string);
function GetTableNames(dbIndex: Integer): string;
function CreateNewTrigger(dbIndex: Integer; ATableName: string; OnCommitProcedure: TNotifyEvent = nil): Boolean;
function AddToSQLHistory(DatabaseTitle: string; SQLType, SQLStatement: string): Boolean;
function SaveAndCloseSQLHistory: Boolean;
function OpenSQLHistory(DatabaseTitle: string): Boolean;
// Connects to database.
// If not succesful (or if ForceConnectDialog is true), ask user for credentials and try again
function ConnectToDBAs(dbIndex: Integer; ForceConnectDialog: boolean=false): Boolean;
function IsLinux: Boolean;
function IsWindows: Boolean;
function IsUnix: Boolean;
function Is64bit: Boolean;
function Is32bit: Boolean;
function getConfigurationDirectory: string;
end;
var
fmMain: TfmMain;
implementation
{ TfmMain }
uses CreateDb, ViewView, ViewTrigger, ViewSProc, ViewGen, NewTable, NewGen,
EnterPass, CreateTrigger, EditTable, CallProc, EditDataFullRec, UDFInfo, ViewDomain,
NewDomain, SysTables, Scriptdb, UserPermissions, BackupRestore, UnitFirebirdServices, CreateUser, ChangePass,
PermissionManage, CopyTable, About, NewEditField, dbInfo, Comparison;
procedure TfmMain.mnExitClick(Sender: TObject);
begin
Close;
end;
procedure TfmMain.FormCreate(Sender: TObject);
begin
{$IFNDEF DEBUG}
// Do not log to debug server if built as release instead of debug
SetDebuggingEnabled(false);
{$ENDIF}
Application.OnException:= @GlobalException;
FActivated:= False;
LoadRegisteredDatabases;
StatusBar1.Panels[0].Text:= 'TurboBird for ' + Target + '-' + Arch;
end;
procedure TfmMain.FormDestroy(Sender: TObject);
begin
// Clean up objects in registered database records
try
ReleaseRegisteredDatabases;
except
// Ignore exceptions/errors; just close
end;
end;
(***************** Add New user ***********************)
procedure TfmMain.lmAddUserClick(Sender: TObject);
var
SelNode: TTreeNode;
dbIndex: Integer;
begin
with fmCreateUser do
try
SelNode:= tvMain.Selected;
dbIndex:= PtrInt(SelNode.Parent.Data);
Init(dbIndex);
edUserName.Clear;
edPassword.Clear;
if ShowModal = mrOK then
begin
// Create user
dmSysTables.Init(dbIndex);
dmSysTables.sqQuery.Close;
dmSysTables.sqQuery.SQL.Text:= 'create user ' + edUserName.Text + ' password ' + QuotedStr(edPassword.Text);
dmSysTables.sqQuery.ExecSQL;
// Grant rule
if cxGrantRole.Checked then
begin
dmSysTables.sqQuery.SQL.Text:= 'grant ' + cbRoles.Text + ' to ' + edUserName.Text;
dmSysTables.sqQuery.ExecSQL;
end;
dmSysTables.stTrans.Commit;
MessageDlg('New user (' + edUserName.Text + ') has been created successfully', mtInformation, [mbOk], 0);
if not cxGrantRole.Checked then
ShowMessage('User (' + edUserName.Text + ') will not appear in users list unless you grant it a permission');
lmRefresh.Click;
end;
except
on E: Exception do
begin
MessageDlg('Error while creating new user: ' + e.Message, mtError, [mbOk], 0);
end;
end;
end;
(*********** Backup / Restore database ************)
procedure TfmMain.lmBackupClick(Sender: TObject);
var
SelNode: TTreeNode;
begin
SelNode:= tvMain.Selected;
with RegisteredDatabases[PtrInt(tvMain.Selected.Data)].RegRec do
fmBackupRestore.Init(SelNode.Text, DatabaseName, UserName, Password);
fmBackupRestore.cbOperation.Enabled:= True;
fmBackupRestore.Show;
end;
(********** change user password **********)
procedure TfmMain.lmChangePasswordClick(Sender: TObject);
begin
fmChangePass.Caption:= 'Change password for user: ' + tvMain.Selected.Text;
fmChangePass.edPassword.Clear;
fmChangePass.edConfirm.Clear;
if fmChangePass.ShowModal = mrOK then
begin
try
dmSysTables.Init(PtrInt(tvMain.Selected.Parent.Parent.Data));
dmSysTables.sqQuery.Close;
dmSysTables.sqQuery.SQL.Text:= 'alter user ' + tvMain.Selected.Text +
' password ' + QuotedStr(fmChangePass.edPassword.Text);
dmSysTables.sqQuery.ExecSQL;
dmSysTables.stTrans.Commit;
MessageDlg('Password has been changed', mtInformation, [mbOk], 0);
except
on E: Exception do
ShowMessage('Error while changing password: ' + e.Message);
end;
end;
end;
procedure TfmMain.lmCompareClick(Sender: TObject);
var
dbIndex: Integer;
Title: string;
ATab: TTabSheet;
begin
dbIndex:= PtrInt(tvMain.Selected.Data);
// Check if password is saved - it may be empty, which can be valid for
// e.g. embedded databases
if (RegisteredDatabases[dbIndex].RegRec.SavePassword) or
ConnectToDBAs(dbIndex) then
begin
Title:= RegisteredDatabases[dbIndex].RegRec.Title + ': Database Comparison';
fmComparison:= FindCustomForm(Title, TfmComparison) as TfmComparison;
if fmComparison = nil then
begin
fmComparison:= TfmComparison.Create(Application);
ATab:= TTabSheet.Create(self);
ATab.Parent:= PageControl1;
fmComparison.Parent:= ATab;
fmComparison.Left:= 0;
fmComparison.Top:= 0;
fmComparison.BorderStyle:= bsNone;
fmComparison.Align:= alClient;
fmComparison.Caption:= Title;
end
else
ATab:= fmComparison.Parent as TTabSheet;
PageControl1.ActivePage:= ATab;
ATab.Tag:= dbIndex;
ATab.Caption:= Title;
fmComparison.Init(dbIndex);
fmComparison.Show;
end;
end;
procedure TfmMain.lmCopyRolePermissionClick(Sender: TObject);
begin
lmCopyUserPermissionClick(nil);
end;
procedure TfmMain.lmCopyUserPermissionClick(Sender: TObject);
var
List: TStringList;
dbIndex: Integer;
UserName: string;
NewUser: string;
begin
if InputQuery('Permission', 'Please type a User/Role name to copy perissions to', NewUser) then
begin
UserName:= tvMain.Selected.Text;
dbIndex:= PtrInt(tvMain.Selected.Parent.Parent.Data);
List:= TStringList.Create;
try
Scriptdb.ScriptUserAllPermissions(dbIndex, UserName, List, NewUser);
ShowCompleteQueryWindow(dbIndex, 'Script permissions for : ' + UserName, List.Text);
finally
List.Free;
end;
end;
end;
procedure TfmMain.lmCopyTableClick(Sender: TObject);
begin
fmCopyTable.Init(PtrInt(tvMain.Selected.Parent.Parent.Data), tvMain.Selected.Text);
fmCopyTable.Show;
end;
procedure TfmMain.lmCreateDBClick(Sender: TObject);
begin
fmCreateDB.edNewDatabase.Text:= tvMain.Selected.Text + ':';
mnCreateDBClick(nil);
end;
procedure TfmMain.lmDBInfoClick(Sender: TObject);
var
ATab: TTabSheet;
Title: string;
dbIndex: Integer;
begin
Title:= 'Database information for: ' + tvMain.Selected.Text;
dbIndex:= PtrInt(tvMain.Selected.Data);
fmDBInfo:= FindCustomForm(Title, TfmDBInfo) as TfmDBInfo;
if fmDBInfo = nil then
begin
fmDBInfo:= TfmDBInfo.Create(Application);
ATab:= TTabSheet.Create(self);
ATab.Parent:= PageControl1;
fmDBInfo.Parent:= ATab;
fmDBInfo.Left:= 0;
fmDBInfo.Top:= 0;
fmDBInfo.BorderStyle:= bsNone;
fmDBInfo.Align:= alClient;
Caption:= Title;
end
else
ATab:= fmDBInfo.Parent as TTabSheet;
PageControl1.ActivePage:= ATab;
ATab.Tag:= dbIndex;
ATab.Caption:= Title;
fmDBInfo.Init(dbIndex);
end;
procedure TfmMain.lmDisconnectClick(Sender: TObject);
var
dbIndex: Integer;
i: Integer;
j: Integer;
TabSheet: TTabSheet;
begin
dbIndex:= PtrInt(tvMain.Selected.Data);
RegisteredDatabases[dbIndex].IBConnection.Close;
for i:= PageControl1.PageCount - 1 downto 0 do
if (PageControl1.Pages[i] as TComponent).Tag = dbIndex then
begin
TabSheet:= PageControl1.Page[i] as TTabSheet;
for j:= 0 to TabSheet.ControlCount - 1 do
if TabSheet.Controls[j] is TForm then
begin
(TabSheet.Controls[j] as TForm).Close;
TabSheet.Free;
Break;
end;
end;
tvMain.Selected.Collapse(True);
end;
procedure TfmMain.lmEditFieldClick(Sender: TObject);
var
SelNode: TTreeNode;
dbIndex: Integer;
FieldName: string;
FieldType, DefaultValue: string;
Size, Scale: Integer;
Description, Characterset, Collation: string;
NotNull: Boolean;
begin
SelNode:= tvMain.Selected;
dbIndex:= PtrInt(SelNode.Parent.Parent.Parent.Data);
FieldName:= Copy(SelNode.Text, 1, Pos(' ', SelNode.Text) - 1);
if dmSysTables.GetFieldInfo(dbIndex, SelNode.Parent.Text, FieldName,
FieldType, Size, Scale, NotNull,
DefaultValue, Characterset, Collation, Description) then
begin
fmNewEditField:= TfmNewEditField.Create(nil);
fmNewEditField.Init(dbIndex, SelNode.Parent.Text, foEdit,
FieldName, FieldType,
CharacterSet, Collation,
DefaultValue, Description,
Size, Scale,
PtrInt(SelNode.Data), not NotNull,
nil);
fmNewEditField.Show;
end
else
ShowMessage('Unable to locate the field: ' + SelNode.Text);
end;
procedure TfmMain.lmGetIncrementGenClick(Sender: TObject);
var
SelNode: TTreeNode;
AGenName: string;
dbIndex: Integer;
begin
SelNode:= tvMain.Selected;
if (SelNode <> nil) and (SelNode.Parent <> nil) then
begin
dbIndex:= PtrInt(SelNode.Parent.Parent.Data);
AGenName:= SelNode.Text;
ShowCompleteQueryWindow(dbIndex, 'get increment generator SQL for:' + AGenName,
'select GEN_ID(' + AGenName + ', 1) from RDB$Database;');
end;
end;
procedure TfmMain.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
if Length(RegisteredDatabases) > 0 then
fmReg.SaveRegistrations;
SaveAndCloseSQLHistory;
end;
procedure TfmMain.FormActivate(Sender: TObject);
begin
FActivated:= True;
end;
(*************** Open System table **************)
procedure TfmMain.lmOpenSystemTableClick(Sender: TObject);
var
SelNode: TTreeNode;
QWindow: TfmQueryWindow;
begin
SelNode:= tvMain.Selected;
if (SelNode <> nil) and (SelNode.Parent <> nil) then
begin
QWindow:= ShowQueryWindow(PtrInt(SelNode.Parent.Parent.Data), SelNode.Text);
QWindow.meQuery.Lines.Text:= 'select * from ' + SelNode.Text;
QWindow.bbRunClick(nil);
QWindow.Show;
end;
end;
procedure TfmMain.lmActivateTrigClick(Sender: TObject);
var
SelNode: TTreeNode;
begin
SelNode:= tvMain.Selected;
if ChangeTriggerActivity(PtrInt(SelNode.Parent.Parent.Data), SelNode.Text, True) then
MessageDlg('Trigger has been activated', mtInformation, [mbOk], 0);
end;
(******************* Call stored procedure *****************)
procedure TfmMain.lmCallStoreProcClick(Sender: TObject);
var
SelNode: TTreeNode;
Body: string;
AProcName: string;
SPOwner: string;
Params: string;
OneParam: string;
i: Integer;
QWindow: TfmQueryWindow;
Line: string;
Called: Boolean;
WithParams: Boolean;
begin
SelNode:= tvMain.Selected;
if (SelNode <> nil) and (SelNode.Parent <> nil) then
begin
AProcName:= SelNode.Text;
Body:= GetStoredProcBody(PtrInt(SelNode.Parent.Parent.Data), AProcName, SPOwner);
Params:= RetrieveInputParamFromSP(Body);
withParams:= Params <> '';
with fmCallProc do
begin
StringGrid1.RowCount:= 1;
if WithParams then
Params:= Params + ',';
i:= 1;
if WithParams then
while Params <> '' do
begin
OneParam:= Copy(Params, 1, Pos(',', Params) - 1);
Delete(Params, 1, Pos(',', Params));
Params:= Trim(Params);
StringGrid1.RowCount:= i + 1;
StringGrid1.Cells[1, i]:= Trim(Copy(OneParam, 1, Pos(' ', OneParam)));
StringGrid1.Cells[2, i]:= Trim(Copy(OneParam, Pos(' ', OneParam), Length(OneParam)));
Inc(i);
end;
Caption:= 'Call stored procedure: ' + AProcName;
if WithParams then
Called:= ShowModal = mrOK; // View parameters form
if (Called) or (not WithParams) then
begin
QWindow:= ShowQueryWindow(PtrInt(SelNode.Parent.Parent.Data), 'Call procedure: ' + AProcName);
QWindow.meQuery.lines.Clear;
if Pos('suspend', LowerCase(Body)) > 0 then
Line:= 'select * from ' + AProcName
else
Line:= 'execute procedure ' + AProcName;
if WithParams then
Line:= Line + '(';
for i:= 1 to StringGrid1.RowCount - 1 do
if Pos('CHAR', StringGrid1.Cells[2, i]) > 0 then
Line:= Line + QuotedStr(StringGrid1.Cells[0, i]) + ', '
else
Line:= Line + StringGrid1.Cells[0, i] + ', ';
if WithParams then
begin
Delete(Line, Length(Line) - 1, 2);
Line:= Line + ')';
end;
Line:= Line + ';';
QWindow.meQuery.Lines.Add(Line);
QWindow.Show;
end;
end;
end;
end;
(**************** Connect As *****************)
procedure TfmMain.lmConnectAsClick(Sender: TObject);
begin
if ConnectToDBAs(PtrInt(tvMain.Selected.Data), True) then
tvMain.Selected.Expand(False)
else
tvMain.Selected.Collapse(False);
end;
function TfmMain.ConnectToDBAs(dbIndex: Integer; ForceConnectDialog: boolean=false): Boolean;
var
Rec: TRegisteredDatabase;
Count: Integer;
begin
Result:= False;
Rec:= RegisteredDatabases[dbIndex].RegRec;
fmEnterPass.laDatabase.Caption:= Rec.Title;
fmEnterPass.edUser.Text:= Rec.UserName;
fmEnterPass.edPassword.Clear;
fmEnterPass.cbRole.Clear;
// Use may have saved an empty password, which is valid for embedded dbs
// So check SavePassword instead of Password itself.
if (ForceConnectDialog=false) and Rec.SavePassword then
try
fmEnterPass.cbRole.Items.CommaText:= dmSysTables.GetDBObjectNames(dbIndex, otRoles, Count);
fmEnterPass.cbRole.ItemIndex:= -1;
fmEnterPass.cbRole.Text:= '';
Result:= True; //this works, no need to go through a retry attempt below
except
// We don't particularly care which error occurred; we're trying again below.
Result:= False;
end;
// Only show form if connection failed before
if (ForceConnectDialog or (Result=false)) and
(fmEnterPass.ShowModal = mrOk) then
begin
if fmReg.TestConnection(Rec.DatabaseName, fmEnterPass.edUser.Text, fmEnterPass.edPassword.Text,
Rec.Charset) then
begin
RegisteredDatabases[dbIndex].RegRec.UserName:= fmEnterPass.edUser.Text;
RegisteredDatabases[dbIndex].RegRec.Password:= fmEnterPass.edPassword.Text;
RegisteredDatabases[dbIndex].RegRec.Role:= fmEnterPass.cbRole.Text;
Result:= True;
end;
end;
end;
function TfmMain.IsLinux: Boolean;
begin
Result := Target = 'Linux';
end;
function TfmMain.IsWindows: Boolean;
begin
Result := Target = 'Win';
end;
function TfmMain.IsUnix: Boolean;
begin
end;
function TfmMain.Is64bit: Boolean;
begin
result := Arch = '64';
end;
function TfmMain.Is32bit: Boolean;
begin
result := Arch = '32';
end;
function TfmMain.getConfigurationDirectory: string;
var
ConfigDir: string;
begin
if IsLinux then
begin
ConfigDir:= GetEnvironmentVariable('HOME') + DirectorySeparator + '.turbobird' + DirectorySeparator;
if not DirectoryExists(ConfigDir) then
CreateDir(ConfigDir);
Result:= ConfigDir;
end
else
ExtractFilePath(ParamStr(0));
end;
(**************** Fill and show constraints form ************************)
procedure TfmMain.FillAndShowConstraintsForm(Form: TfmTableManage; ATableName: string; dbIndex: Integer);
begin
dmSysTables.Init(dbIndex);
dmSysTables.GetTableConstraints(ATableName, Form.SQLQuery1);
Form.FillConstraints(dbIndex);
end;
(*********** Show and Fill Query Window *****************)
procedure TfmMain.ShowCompleteQueryWindow(DatabaseIndex: Integer; ATitle,
AQueryText: string; OnCommitProcedure: TNotifyEvent = nil);
var
QWindow: TfmQueryWindow;
Part: string;
begin
QWindow:= ShowQueryWindow(DatabaseIndex, ATitle);
QWindow.meQuery.ClearAll;
QWindow.OnCommit:= OnCommitProcedure;
repeat
if Pos(LineEnding, AQueryText) > 0 then
Part:= Copy(AQueryText, 1, Pos(LineEnding, AQueryText))
else
Part:= AQueryText;
Delete(AQueryText, 1, Length(Part));
Part:= StringReplace(Part, LineEnding, ' ', [rfReplaceAll]);
QWindow.meQuery.Lines.Add(Part);
until AQueryText = '';
end;
(*********************** Constraint Management ********************)
(********** Create Auto Increment Trigger from current generator **********)
procedure TfmMain.lmCreateAutoIncClick(Sender: TObject);
var
SelNode: TTreeNode;
begin
SelNode:= tvMain.Selected;
if (SelNode <> nil) and (SelNode.Parent <> nil) then
begin
InitNewGen(PtrInt(SelNode.Parent.Parent.Data));
fmNewGen.edGenName.Text:= SelNode.Text;
fmNewGen.edGenName.Enabled:= False;
fmNewGen.cxTrigger.Checked:= True;
fmNewGen.ShowModal;
end;
end;
(**************** Create new stored proc *******************)
procedure TfmMain.lmCreateStoredProcClick(Sender: TObject);
var
SelNode: TTreeNode;
QWindow: TfmQueryWindow;
AProcName: string;
begin
SelNode:= tvMain.Selected;
if (SelNode <> nil) and (SelNode.Parent <> nil) then
if InputQuery('Create new stored procedure', 'Please enter new procedure name', AProcName) then
begin
QWindow:= ShowQueryWindow(PtrInt(SelNode.Parent.Data), 'Create new stored procedure');
QWindow.meQuery.Lines.Clear;
// QWindow.meQuery.Lines.Add('SET TERM ^;');
QWindow.meQuery.Lines.Add('CREATE PROCEDURE ' + AProcName);
QWindow.meQuery.Lines.Add('-- Input parameters, you can modify,remove them');
QWindow.meQuery.Lines.Add('( Input1 int, -- You can replace it by your first parameter');
QWindow.meQuery.Lines.Add(' Input2 varchar(20) -- you can replace it by your second parameter');
QWindow.meQuery.Lines.Add(')');
QWindow.meQuery.Lines.Add('RETURNS');
QWindow.meQuery.Lines.Add('( Out1 int -- You can replace it by your first parameter');
QWindow.meQuery.Lines.Add(')');
QWindow.meQuery.Lines.Add('AS');
QWindow.meQuery.Lines.Add('BEGIN');
QWindow.meQuery.Lines.Add(' -- Write your procedure code here');
QWindow.meQuery.Lines.Add('END;');
// QWindow.meQuery.Lines.Add('SET TERM ;^');
QWindow.Show;
end;
end;
(*************** Create new Trigger ****************)
function TfmMain.CreateNewTrigger(dbIndex: Integer; ATableName: string; OnCommitProcedure: TNotifyEvent = nil): Boolean;
var
QWindow: TfmQueryWindow;
TrigType: string;
begin
Result:= False;
if ATableName <> '' then
begin
fmCreateTrigger.cbTables.Clear;
fmCreateTrigger.cbTables.Items.Add(ATableName);
fmCreateTrigger.cbTables.ItemIndex:= 0;
end;
fmCreateTrigger.edTriggerName.Clear;
fmCreateTrigger.cxUpdate.Checked:= False;
fmCreateTrigger.cxInsert.Checked:= False;
fmCreateTrigger.cxDelete.Checked:= False;
if fmCreateTrigger.ShowModal = mrOK then
begin
Result:= True;
QWindow:= ShowQueryWindow(dbIndex, 'Create new Trigger');
if fmCreateTrigger.rbAfter.Checked then
TrigType:= 'After'
else
TrigType:= 'Before';
if fmCreateTrigger.cxInsert.Checked then
TrigType:= TrigType + ' insert or';
if fmCreateTrigger.cxUpdate.Checked then
TrigType:= TrigType + ' update or';
if fmCreateTrigger.cxDelete.Checked then
TrigType:= TrigType + ' delete or';
Delete(TrigType, Length(TrigType) - 2, 3);
QWindow.meQuery.Lines.Clear;
QWindow.meQuery.Lines.Add('CREATE TRIGGER ' + fmCreateTrigger.edTriggerName.Text + ' for ' +
fmCreateTrigger.cbTables.Text);
QWindow.meQuery.Lines.Add('Active');
QWindow.meQuery.Lines.Add(TrigType);
QWindow.meQuery.Lines.Add('Position 0');
QWindow.meQuery.Lines.Add('AS');
QWindow.meQuery.Lines.Add('BEGIN');
QWindow.meQuery.Lines.Add(' -- Your code here');
QWindow.meQuery.Lines.Add(' -- New.FieldName = YourData;');
QWindow.meQuery.Lines.Add('END;');
fmMain.Show;
if OnCommitProcedure <> nil then
QWindow.OnCommit:= OnCommitProcedure;
end;
end;
(******* Create Trigger click ********)
procedure TfmMain.lmCreateTriggerClick(Sender: TObject);
var
SelNode: TTreeNode;
DBIndex: Integer;
TableNames: string;
Count: Integer;
begin
SelNode:= tvMain.Selected;
DBIndex:= PtrInt(SelNode.Parent.Data);
TableNames:= dmSysTables.GetDBObjectNames(DBIndex, otTables, Count);
fmCreateTrigger.cbTables.Items.CommaText:= TableNames;
CreateNewTrigger(DBIndex, '');
end;
(****************** Create New View ***************)
procedure TfmMain.lmCreateViewClick(Sender: TObject);
var
SelNode: TTreeNode;
QWindow: TfmQueryWindow;
AViewName: string;
begin
SelNode:= tvMain.Selected;
if (SelNode <> nil) and (SelNode.Parent <> nil) then
if InputQuery('Create new view', 'Please enter new view name', AViewName) then
begin
QWindow:= ShowQueryWindow(PtrInt(SelNode.Parent.Data), 'Create new view');
QWindow.meQuery.Lines.Clear;
QWindow.meQuery.Lines.Add('CREATE VIEW "' + AViewName + '" (');
QWindow.meQuery.Lines.Add('Field1Name, Field2Name) ');
QWindow.meQuery.Lines.Add('AS');
QWindow.meQuery.Lines.Add('SELECT Field1, Field2 FROM ATableName');
QWindow.meQuery.Lines.Add('-- WHERE condition');
QWindow.Show;
end;
end;
procedure TfmMain.lmDeactiveTrigClick(Sender: TObject);
var
SelNode: TTreeNode;
begin
SelNode:= tvMain.Selected;
if ChangeTriggerActivity(PtrInt(SelNode.Parent.Parent.Data), SelNode.Text, False) then
MessageDlg('Trigger has been DeActivated', mtInformation, [mbOk], 0);
end;
(*************** Display view top 1000 records ************)
procedure TfmMain.lmDisplay1000VClick(Sender: TObject);
var
SelNode: TTreeNode;
QWindow: TfmQueryWindow;
begin
SelNode:= tvMain.Selected;
if (SelNode <> nil) and (SelNode.Parent <> nil) then
begin
QWindow:= ShowQueryWindow(PtrInt(SelNode.Parent.Parent.Data), 'Select first 1000 from ' + SelNode.Text);
QWindow.meQuery.Lines.Text:= 'select first 1000 * from "' + SelNode.Text + '"';
QWindow.bbRunClick(nil);
QWindow.Show;
end;
end;
(********** Drop Exception ********)
procedure TfmMain.lmDropExceptionClick(Sender: TObject);
var
SelNode: TTreeNode;
QWindow: TfmQueryWindow;
begin
SelNode:= tvMain.Selected;
if MessageDlg('Are you sure you want to delete ' + SelNode.Text + ' permanently', mtConfirmation,
[mbYes, mbNo], 0) = mrYes then
begin
QWindow:= ShowQueryWindow(PtrInt(SelNode.Parent.Parent.Data), 'Drop Exception');
QWindow.meQuery.Lines.Clear;
QWindow.meQuery.Lines.Add('DROP EXCEPTION ' + SelNode.Text + ';');
QWindow.Show;
end;
end;
(*********** Edit Data in a form *****************)
procedure TfmMain.lmEditDataFormClick(Sender: TObject);