-
Notifications
You must be signed in to change notification settings - Fork 3
/
MainFrm.pas
2256 lines (2155 loc) · 82.6 KB
/
MainFrm.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 MainFrm;
interface
uses
SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Grids,
DBGrids, StdCtrls, Buttons, Menus, ComCtrls, DbCtrls, ExtCtrls, ActnList,
UtilsUnit, Types, IniFiles, DB, sqldb, BufDataset, laz2_DOM, laz2_XMLRead,
Base64, PingThreadUnit, DBUnit;
type
{ TMainForm }
TMainForm = class(TForm)
AddColumnBtn: TButton;
AddLinkedColumnBtn: TButton;
AddValueDetailHelpBtn: TSpeedButton;
AddPrimaryTableDetailBtn: TButton;
AddValueBtn: TButton;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
TablesDirectoryMenuItem: TMenuItem;
ViewMenuItem: TMenuItem;
ScriptSQLHeightEdt: TEdit;
FindRecordsBtn: TButton;
DBGrid1: TDBGrid;
FindandReplaceRecordsBtn: TButton;
RecordsFoundMemo: TMemo;
PrefixEdt: TEdit;
PrefixLbl: TLabel;
ReplaceWithEdt: TEdit;
FindEdt: TEdit;
FromPort: TEdit;
FindLbl: TLabel;
ReplaceWithLbl: TLabel;
RecordsFoundStrGrid: TStringGrid;
AddPrimaryDetailHelpBtn: TSpeedButton;
CompareStopRunBtn: TSpeedButton;
CancelFindBtn: TSpeedButton;
BtnCompareRight: TSpeedButton;
TablesDirectoryBtn: TSpeedButton;
TabSheetFindAndReplace: TTabSheet;
ToPort: TEdit;
SQLTypeSelect: TComboBox;
SpeedButton1: TSpeedButton;
ToFieldsEdit: TEdit;
FromFieldsEdit: TEdit;
FromFieldsLbl: TLabel;
ToFieldsLbl: TLabel;
RefreshCSVColumnsBtn: TButton;
LoadPrimaryColumnsBtn: TButton;
ClearProjectBtn: TButton;
ColumnsList: TDBLookupListBox;
CSVFirstLineCheck: TCheckBox;
CSVGrid: TDBGrid;
CSVSearchBtn: TButton;
CSVSearchCombo: TComboBox;
CSVSearchEdt: TEdit;
CSVSearchNextBtn: TButton;
DBNavigator1: TDBNavigator;
DBNavigator2: TDBNavigator;
DeleteSelectedRowBtn: TButton;
ExecuteQueryBtn: TBitBtn;
ExportSQLBtn: TButton;
FieldSelectList: TListBox;
FromDBCombo: TDBLookupComboBox;
CreateQueriesPopup: TPopupMenu;
CompareFromGrid: TDBGrid;
FromRowsCountLabel: TLabel;
ToTableName: TEdit;
FromUniqueField: TEdit;
GenerateInsertsFromAllCSVRecordsMNU: TMenuItem;
GenerateInsertsfromPrimaryCSVFieldNotFoundMNU: TMenuItem;
EditMnu: TMenuItem;
ExportSQLPopup: TPopupMenu;
GenerateInsertfromResultsetMNU: TMenuItem;
GenerateQueriesBtn: TButton;
GenerateUpdatesfromResultsetMNU: TMenuItem;
GenerateUpdatesfromallCSVrecordsMNU: TMenuItem;
GenerateUpdatesfromFoundMNU: TMenuItem;
ImportSaveLogMemoBtn: TButton;
LoadFromandToDataBtn: TBitBtn;
CompareToGrid: TDBGrid;
FromSQLLbl: TLabel;
FromUniqueFieldLbl: TLabel;
Label12: TLabel;
Label14: TLabel;
Label16: TLabel;
ToTableNameLbl: TLabel;
ToDatabaseLbl: TLabel;
ToUserNameLbl: TLabel;
ToPasswordLbl: TLabel;
ToServerNameLbl: TLabel;
LoadCSVBtn: TButton;
LoadTablesBtn: TButton;
LogMemo: TMemo;
ToSQLEdit: TMemo;
OpenProjectBtn: TButton;
OutputLog: TMemo;
PageControl1: TPageControl;
PrimaryColumnEdt: TEdit;
PrimaryColumnsList: TDBLookupListBox;
PrimaryTableNameEdt: TEdit;
ResultsetEditableMnu: TMenuItem;
FixLinkedValueMenuBtn: TMenuItem;
CSVGridMenu: TPopupMenu;
OpenDialog1: TOpenDialog;
FromPassword: TEdit;
FromServerName: TEdit;
FromUserName: TEdit;
ConnecttoServerBtn: TBitBtn;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
ProgressBar1: TProgressBar;
MainMenu1: TMainMenu;
File1: TMenuItem;
RowsCountLabel: TLabel;
SaveDialog1: TSaveDialog;
About1: TMenuItem;
SaveProjectBtn: TButton;
SaveSQLBtn: TButton;
ScriptGrid: TDBGrid;
ScriptSQL: TMemo;
ScriptSQLEdit: TMemo;
ScriptTableName: TEdit;
SetupGrid: TStringGrid;
SQL: TMemo;
FromSQLEdit: TMemo;
TabControl1: TTabControl;
TableList: TDBLookupListBox;
TabSheetCompareDBs: TTabSheet;
TabsheetQueriesAndExport: TTabSheet;
TabSheetImport: TTabSheet;
TestLinkedTableBtn: TButton;
ToDatabase: TEdit;
ToPassword: TEdit;
ToServerName: TEdit;
ToUserName: TEdit;
ScriptSQLHeightUpDown: TUpDown;
ValueEdt: TEdit;
XMLToCSVConvertBtn: TButton;
procedure AddColumnBtnClick(Sender: TObject);
procedure AddPrimaryDetailHelpBtnClick(Sender: TObject);
procedure AddValueBtnClick(Sender: TObject);
procedure AddValueDetailHelpBtnClick(Sender: TObject);
procedure BtnCompareRightClick(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure CancelFindBtnClick(Sender: TObject);
procedure CompareStopRunBtnClick(Sender: TObject);
procedure FindandReplaceRecordsBtnClick(Sender: TObject);
procedure ClearProjectBtnClick(Sender: TObject);
procedure CSVGridEnter(Sender: TObject);
procedure CSVSearchBtnClick(Sender: TObject);
procedure DeleteSelectedRowBtnClick(Sender: TObject);
procedure FindRecordsBtnClick(Sender: TObject);
procedure FixLinkedValueMenuBtnClick(Sender: TObject);
procedure AddLinkedColumnBtnClick(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FromDBComboSelect(Sender: TObject);
procedure GenerateQueriesBtnClick(Sender: TObject);
procedure GenerateInsertfromResultsetMNUClick(Sender: TObject);
procedure GenerateInsertsFromAllCSVRecordsMNUClick(Sender: TObject);
procedure GenerateInsertsfromPrimaryCSVFieldNotFoundMNUClick(Sender: TObject
);
procedure GenerateUpdatesfromallCSVrecordsMNUClick(Sender: TObject);
procedure GenerateUpdatesfromFoundMNUClick(Sender: TObject);
procedure GenerateUpdatesfromResultsetMNUClick(Sender: TObject);
procedure ImportSaveLogMemoBtnClick(Sender: TObject);
procedure LoadPrimaryColumnsBtnClick(Sender: TObject);
procedure LoadTablesBtnClick(Sender: TObject);
procedure AddPrimaryTableDetailBtnClick(Sender: TObject);
procedure ConnecttoServerBtnClick(Sender: TObject);
procedure PageControl1Exit(Sender: TObject);
procedure RefreshCSVColumnsBtnClick(Sender: TObject);
procedure ScriptGridEnter(Sender: TObject);
procedure ScriptSQLEditResize(Sender: TObject);
procedure ScriptSQLHeightEdtChange(Sender: TObject);
procedure ScriptSQLHeightUpDownClick(Sender: TObject; Button: TUDBtnType);
procedure SetupGridDrawCell(Sender: TObject; aCol, aRow: Integer;
aRect: TRect; aState: TGridDrawState);
procedure SpeedButton1Click(Sender: TObject);
procedure TabControl1Change(Sender: TObject);
procedure TablesDirectoryBtnClick(Sender: TObject);
procedure TablesDirectoryMenuItemClick(Sender: TObject);
procedure TabSheetCompareDBsExit(Sender: TObject);
procedure XMLToCSVConvertBtnClick(Sender: TObject);
procedure OpenProjectBtnClick(Sender: TObject);
procedure SaveProjectBtnClick(Sender: TObject);
procedure CSVSearchNextBtnClick(Sender: TObject);
procedure TableListClick(Sender: TObject);
procedure ExportSQLBtnClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure About1Click(Sender: TObject);
procedure LoadFromandToDataBtnClick(Sender: TObject);
procedure ExecuteQueryBtnClick(Sender: TObject);
procedure LoadCSVBtnClick(Sender: TObject);
procedure SaveSQLBtnClick(Sender: TObject);
procedure TestLinkedTableBtnClick(Sender: TObject);
procedure SetupGridClick(Sender: TObject);
private
procedure CloseConnections;
function GenerateQueryLine(typestr: String): String;
function getTablePrimaryKey(TableName: String): String;
function LoadPrimaryTable(): Boolean;
function OpenTableColumnQuery: Boolean;
procedure setQueryConnections;
{ Private declarations }
public
SetupGridSelectedRow: Integer;
CSVGridSelectedField: TField;
SharedSetupGrid: TStringGrid;
ConnectedVia: String;
{ Public declarations }
end;
var
MainForm: TMainForm;
FieldsString, ValuesString: String;
RecordGuid: TGUID;
LastFromDB: Integer;
StopRunBool: Boolean;
PreviousScriptsTab: Integer;
implementation
uses datafrm, AboutFrm, FixLinkedValuesFrm, TablesDirectoryFrm;
{$R *.lfm}
procedure TMainForm.About1Click(Sender: TObject);
begin
Application.CreateForm(TAboutForm, AboutForm);
AboutForm.showmodal;
AboutForm.Free;
end;
procedure TMainForm.Button1Click(Sender: TObject);
begin
SaveDialog1.FilterIndex := 1;
SaveDialog1.DefaultExt := '.sql';
if Savedialog1.Execute then
begin
SQL.Lines.SaveToFile(SaveDialog1.FileName);
end;
end;
procedure TMainForm.Button2Click(Sender: TObject);
begin
SaveDialog1.FilterIndex := 1;
SaveDialog1.DefaultExt := '.txt';
if Savedialog1.Execute then
begin
Outputlog.Lines.SaveToFile(SaveDialog1.FileName);
end;
end;
procedure TMainForm.Button3Click(Sender: TObject);
begin
if MessageDlg('Are you sure you want to Run this it cannot be undone!',
mtConfirmation, [mbYes, mbNo], 0) = mrYes then
begin
Dataform.ToTransaction.Commit;
Dataform.ToTransaction.StartTransaction;
try
Dataform.ToQuery2.SQL.Clear;
Dataform.ToQuery2.SQL.Add(SQL.Lines.Text);
Dataform.ToQuery2.ExecSQL;
Dataform.ToQuery2.SQL.Clear;
Dataform.ToTransaction.Commit;
showmessage('Finished');
finally
if DataForm.ToTransaction.Active then
begin
Dataform.ToTransaction.Rollback;
showmessage('Something went wrong all changes were dropped!');
end
else
begin
Dataform.ToTransaction.StartTransaction;
end;
end;
end;
end;
procedure TMainForm.ExportSQLBtnClick(Sender: TObject);
var
button: TControl;
lowerLeft: TPoint;
begin
if Sender is TControl then
begin
button := TControl(Sender);
lowerLeft := Point(0, button.Height);
lowerLeft := button.ClientToScreen(lowerLeft);
ExportSQLPopup.Popup(lowerLeft.X, lowerLeft.Y);
end;
end;
procedure TMainForm.ConnecttoServerBtnClick(Sender: TObject);
var
s: String;
// PingThread: TPingThread;
begin
ConnectedVia := 'NULL';
If ConnecttoServerBtn.Caption = 'Disconnect' then
begin
// PingThread.Terminate;
// PingThread.WaitFor;
CloseConnections();
ConnecttoServerBtn.Caption := 'Connect';
Dataform.FromConnection.DatabaseName := '';
FromDBCombo.Clear;
end
else
begin
try
if (SQLTypeSelect.ItemIndex = 0) then
begin
Dataform.FromConnection.Close;
Dataform.FromConnection.Params.Clear;
// Dataform.FromConnection.Params.Add('DriverID=MSSQL2005');
Dataform.FromConnection.UserName := FromUserName.Text;
Dataform.FromConnection.Password := FromPassword.Text;
Dataform.FromConnection.HostName := FromServerName.Text;
Dataform.FromTransaction.DataBase := Dataform.FromConnection;
Dataform.ToTransaction.DataBase := Dataform.ToConnection;
Dataform.FromConnection.Open;
ConnectedVia := 'MSSQL';
Dataform.DBQuery1.SQL.Text := 'SELECT name as [Database] FROM sys.databases order by [Database]';
Dataform.DBQuery1.SQLConnection := Dataform.FromConnection;
Dataform.DBQuery1.Open;
// Initialize and start the ping thread
// PingThread := TPingThread.Create(Dataform.FromConnection, 300000); // 5-minute interval
// PingThread.Start; // Start the thread execution
end
else
begin
Dataform.FromMySQL80Connection.Close;
Dataform.FromMySQL80Connection.Params.Clear;
Dataform.FromMySQL80Connection.UserName := FromUserName.Text;
Dataform.FromMySQL80Connection.Password := FromPassword.Text;
Dataform.FromMySQL80Connection.HostName := FromServerName.Text;
Dataform.FromMySQL80Connection.Port := StrtoInt(FromPort.Text);
Dataform.FromMySQL80Connection.DatabaseName := 'information_schema';
Dataform.FromTransaction.DataBase := Dataform.FromMySQL80Connection;
Dataform.ToTransaction.DataBase := Dataform.ToMySQL80Connection;
Dataform.FromMySQL80Connection.Open;
ConnectedVia := 'MySQL';
Dataform.DBQuery1.SQL.Text := 'SELECT table_schema as `Database` FROM information_schema.tables Group by TABLE_SCHEMA Order by TABLE_SCHEMA';
Dataform.DBQuery1.SQLConnection := Dataform.FromMySQL80Connection;
Dataform.DBQuery1.Open;
end;
If (LastFromDB < FromDBCombo.Items.Count) and (LastFromDB >= 0) then
begin
FromDBCombo.ItemIndex := LastFromDB;
FromDBComboSelect(FromDBCombo);
end;
setQueryConnections();
ConnecttoServerBtn.Caption := 'Disconnect';
except
begin
ShowMessage('Unable to connect to SQL Server!');
CloseConnections();
end;
raise;
end;
end;
end;
procedure TMainForm.PageControl1Exit(Sender: TObject);
begin
end;
procedure TMainForm.CloseConnections();
begin
if (Dataform.FromConnection.Connected) then
begin
Dataform.FromConnection.Close;
end;
if (Dataform.ToConnection.Connected) then
begin
Dataform.ToConnection.Close;
end;
if (Dataform.ToMySQL80Connection.Connected) then
begin
Dataform.ToMySQL80Connection.Close;
end;
if (Dataform.FromMySQL80Connection.Connected) then
begin
Dataform.FromMySQL80Connection.Close;
end;
end;
procedure TMainForm.setQueryConnections();
begin
if (Dataform.FromConnection.Connected) then
begin
DataForm.ScriptQuery0.SQLConnection := Dataform.FromConnection;
DataForm.ScriptQuery1.SQLConnection := Dataform.FromConnection;
DataForm.FromQuery1.SQLConnection := Dataform.FromConnection;
DataForm.TableandColumnsQuery.SQLConnection := Dataform.FromConnection;
DataForm.TablesQuery1.SQLConnection := Dataform.FromConnection;
DataForm.ColumnsQuery1.SQLConnection := Dataform.FromConnection;
DataForm.ColumnsQuery2.SQLConnection := Dataform.FromConnection;
DataForm.TempQuery1.SQLConnection := Dataform.FromConnection;
DataForm.ToQuery1.SQLConnection := Dataform.ToConnection;
DataForm.ToQuery2.SQLConnection := Dataform.ToConnection;
end
else
begin
DataForm.ScriptQuery0.SQLConnection := Dataform.FromMySQL80Connection;
DataForm.ScriptQuery1.SQLConnection := Dataform.FromMySQL80Connection;
DataForm.FromQuery1.SQLConnection := Dataform.FromMySQL80Connection;
DataForm.TableandColumnsQuery.SQLConnection := Dataform.FromMySQL80Connection;
DataForm.TablesQuery1.SQLConnection := Dataform.FromMySQL80Connection;
DataForm.ColumnsQuery1.SQLConnection := Dataform.FromMySQL80Connection;
DataForm.ColumnsQuery2.SQLConnection := Dataform.FromMySQL80Connection;
DataForm.TempQuery1.SQLConnection := Dataform.FromMySQL80Connection;
DataForm.ToQuery1.SQLConnection := Dataform.ToMySQL80Connection;
DataForm.ToQuery2.SQLConnection := Dataform.ToMySQL80Connection;
end;
end;
procedure TMainForm.FromDBComboSelect(Sender: TObject);
var
s: string;
begin
try
LastFromDB := FromDBCombo.ItemIndex;
s := FromDBCombo.Items[FromDBCombo.ItemIndex];
if (Dataform.FromConnection.Connected) then
begin
Dataform.FromConnection.Close;
Dataform.FromConnection.DatabaseName := s;
Dataform.FromConnection.Open;
end
else
begin
Dataform.FromMySQL80Connection.Close;
Dataform.FromMySQL80Connection.DatabaseName := s;
Dataform.FromMySQL80Connection.Open;
end;
Dataform.DBQuery1.Open;
FromDBCombo.ItemIndex := LastFromDB;
except
begin
ShowMessage('Unable to select DB , make sure the DB exist');
CloseConnections();
ConnecttoServerBtn.Caption := 'Connect';
Dataform.FromConnection.DatabaseName := '';
end;
raise;
end;
end;
procedure TMainForm.RefreshCSVColumnsBtnClick(Sender: TObject);
var
i:integer;
begin
FieldSelectList.Clear;
for i := 0 to Dataform.CSVDataset.Fields.Count - 1 do
begin
FieldSelectList.Items.Add(Dataform.CSVDataset.Fields[i].FieldName);
end;
end;
procedure TMainForm.ScriptGridEnter(Sender: TObject);
begin
If ResultsetEditableMnu.Checked = true then
begin
ScriptGrid.ReadOnly := False;
end
else
begin
ScriptGrid.ReadOnly := True;
end;
end;
procedure TMainForm.ScriptSQLEditResize(Sender: TObject);
begin
ScriptSQLHeightUpDown.Position := ScriptSQLEdit.Height;
ScriptSQLHeightEdt.Text := InttoStr(ScriptSQLEdit.Height);
end;
procedure TMainForm.ScriptSQLHeightEdtChange(Sender: TObject);
var
IntHeight: Integer;
WindowHeight: Integer;
begin
WindowHeight := Self.Height - 330;
if (ScriptSQLHeightEdt.Text = '') then
IntHeight := 180
else if (StrtoInt(ScriptSQLHeightEdt.Text) < 50) then
exit
else if (StrtoInt(ScriptSQLHeightEdt.Text) > WindowHeight) then
IntHeight := WindowHeight
else
IntHeight := StrtoInt(ScriptSQLHeightEdt.Text);
ScriptSQLHeightUpDown.Position := IntHeight;
ScriptSQLEdit.Height := IntHeight;
end;
procedure TMainForm.ScriptSQLHeightUpDownClick(Sender: TObject;
Button: TUDBtnType);
begin
ScriptSQLHeightEdt.Text := InttoStr(ScriptSQLHeightUpDown.Position);
ScriptSQLEdit.Height := ScriptSQLHeightUpDown.Position;
end;
procedure TMainForm.SetupGridDrawCell(Sender: TObject; aCol, aRow: Integer;
aRect: TRect; aState: TGridDrawState);
var
AGrid : TStringGrid;
begin
if ARow <> 0 then
begin
AGrid:=TStringGrid(Sender);
if gdFixed in aState then //if is fixed use the clBtnFace color
AGrid.Canvas.Brush.Color := clBtnFace
else
if gdSelected in aState then //if is selected use the clAqua color
begin
AGrid.Canvas.Brush.Color := clAqua;
AGrid.Canvas.Font.Color := clBlack;
end
else
begin
AGrid.Canvas.Brush.Color := clWindow;
AGrid.Canvas.Font.Color := clWindowText;
end;
AGrid.Canvas.FillRect(aRect);
AGrid.Canvas.TextOut(aRect.Left + 2, aRect.Top + 2, AGrid.Cells[ACol, ARow]);
end;
end;
procedure TMainForm.SpeedButton1Click(Sender: TObject);
begin
showmessage('Use in SQL [@FIELDS] as placeholder for fields. Use these fields if you are not comparing "*" in your SQL.');
end;
procedure TMainForm.TabControl1Change(Sender: TObject);
var
TabName, PreviousTabName: String;
TabIndex: Integer;
ConfigFilePath :String;
INI: TINIFile;
ScriptQuery: TComponent;
ScriptQuerySource: TComponent;
begin
ConfigFilePath := GetAppConfigFile(False);
INI := TINIFile.Create(ConfigFilePath);
try
TabIndex := TabControl1.TabIndex;
TabName := 'SQL' + InttoStr(TabIndex);
PreviousTabName := 'SQL' + InttoStr(PreviousScriptsTab);
INI.WriteString('SCRIPTS',PreviousTabName,EncodeStringBase64(ScriptSQLEdit.Text));
If INI.ReadString('SCRIPTS',TabName,'') <> '' then
begin
ScriptSQLEdit.Text := DecodeStringBase64(INI.ReadString('SCRIPTS',TabName,''));
end
else
begin
ScriptSQLEdit.Text := '';
end;
ScriptQuery := DataForm.FindComponent('ScriptQuery' + IntToStr(TabControl1.TabIndex));
if not Assigned(ScriptQuery) or not (ScriptQuery is TSQLQuery) then
begin
ShowMessage('Error: ScriptQuery' + IntToStr(TabControl1.TabIndex) + ' not found or invalid.');
Exit;
end;
ScriptQuerySource := DataForm.FindComponent('ScriptQuerySource' + IntToStr(TabControl1.TabIndex));
if not Assigned(ScriptQuerySource) or not (ScriptQuerySource is TDataSource) then
begin
ShowMessage('Error: ScriptQuerySource' + IntToStr(TabControl1.TabIndex) + ' not found or invalid.');
Exit;
end;
ScriptGrid.DataSource := TDataSource(ScriptQuerySource);
DBNavigator2.Datasource := TDataSource(ScriptQuerySource);
RowsCountLabel.Caption := InttoStr(TSQLQuery(ScriptQuery).RecordCount);
PreviousScriptsTab := TabIndex;
finally
INI.Free;
end;
end;
procedure TMainForm.TablesDirectoryBtnClick(Sender: TObject);
begin
TablesDirectoryMenuItem.Click;
end;
procedure TMainForm.TablesDirectoryMenuItemClick(Sender: TObject);
begin
TablesDirectoryForm.TargetEdit := MainForm.ScriptSQLEdit;
TablesDirectoryForm.showmodal;
end;
procedure TMainForm.TabSheetCompareDBsExit(Sender: TObject);
begin
dataform.FromQuery1.close;
dataform.ToQuery1.close;
end;
procedure TMainForm.XMLToCSVConvertBtnClick(Sender: TObject);
var
Doc: TXMLDocument;
Child, Child2: TDOMNode;
j, j2: Integer;
TempField: TField;
Col: TColumn;
S: String;
begin
OpenDialog1.FilterIndex := 3;
Opendialog1.DefaultExt := '.csv';
if Opendialog1.Execute then
begin
try
ReadXMLFile(Doc, OpenDialog1.filename);
LogMemo.Lines.Clear;
// using FirstChild and NextSibling properties
Child := Doc.DocumentElement.FirstChild;
while Assigned(Child) do
begin
// LogMemo.Lines.Add(Child.NodeName);
// using ChildNodes method
S := '';
with Child.FirstChild do
begin
with ChildNodes do
begin
for j2 := 0 to (Count - 1) do
begin
If S = '' then
S := '"' + Item[j2].NodeName + '"'
else
S := S + ',"' + Item[j2].NodeName + '"'
end;
end;
end;
LogMemo.Lines.Add(S);
with Child.ChildNodes do
try
for j := 0 to (Count - 1) do
begin
// LogMemo.Lines.Add(Item[j].NodeName);
with Item[j].ChildNodes do
begin
S := '';
for j2 := 0 to (Count - 1) do
begin
If S = '' then
S := '"' + RemoveCRLF(Item[j2].TextContent) + '"'
else
S := S + ',"' + RemoveCRLF(Item[j2].TextContent) + '"'
// LogMemo.Lines.Add(Item[j2].NodeName + ': ' + Item[j2].TextContent);
end;
LogMemo.Lines.Add(S);
end;
end;
finally
Free;
end;
Child := Child.NextSibling;
end;
finally
Doc.Free;
end;
end;
end;
procedure TMainForm.OpenProjectBtnClick(Sender: TObject);
begin
if Dataform.FromConnection.Connected = False then
begin
showmessage('Connect to SQL server first');
exit;
end;
OpenDialog1.FilterIndex := 2;
OpenDialog1.DefaultExt := '.csv';
if Opendialog1.Execute then
begin
SetupGrid.LoadFromCSVFile(OpenDialog1.FileName);
OpenProjectBtn.Hint := OpenDialog1.FileName;
OpenProjectBtn.ShowHint := True;
if LoadPrimaryTable = false then
begin
showmessage('No primary row found.');
end;
end;
end;
function TMainForm.LoadPrimaryTable(): Boolean;
var
I: Integer;
Found: Boolean;
begin
Found := false;
for I := 0 to SetupGrid.RowCount - 1 do
begin
If SetupGrid.Cells[0,I] = 'Primary' then
begin
PrimaryTableNameEdt.Text := SetupGrid.Cells[3,I];
PrimaryColumnEdt.Text := SetupGrid.Cells[4,I];
Dataform.ColumnsQuery2.Close;
Dataform.ColumnsQuery2.ParamByName('tablename').Value := PrimaryTableNameEdt.Text;
Dataform.ColumnsQuery2.Open;
Found := true;
end;
end;
LoadPrimaryTable := Found;
end;
procedure TMainForm.SaveProjectBtnClick(Sender: TObject);
begin
SaveDialog1.FilterIndex := 2;
SaveDialog1.DefaultExt := '.csv';
SaveDialog1.FileName := OpenProjectBtn.Hint;
if Savedialog1.Execute then
begin
SetupGrid.SaveToCSVFile(SaveDialog1.FileName);
end;
end;
procedure TMainForm.SetupGridClick(Sender: TObject);
begin
SetupGridSelectedRow := SetupGrid.Row;
end;
procedure TMainForm.TableListClick(Sender: TObject);
begin
LoadTableColumns(TableList.Items[TableList.ItemIndex]);
end;
procedure TMainForm.LoadTablesBtnClick(Sender: TObject);
begin
LoadDBTables();
end;
procedure TMainForm.AddLinkedColumnBtnClick(Sender: TObject);
begin
if FieldSelectList.ItemIndex = -1 then
begin
showmessage('Select CSV column');
exit;
end;
if PrimaryColumnsList.ItemIndex = -1 then
begin
showmessage('Select Primary column');
exit;
end;
if TableList.ItemIndex = -1 then
begin
showmessage('Select Link Table');
exit;
end;
if ColumnsList.ItemIndex = -1 then
begin
showmessage('Select Link Table Column');
exit;
end;
SetupGrid.RowCount := SetupGrid.RowCount + 1;
SetupGrid.Cells[0,SetupGrid.RowCount - 1] := 'Linked';
SetupGrid.Cells[1,SetupGrid.RowCount - 1] := FieldSelectList.Items[FieldSelectList.ItemIndex];
SetupGrid.Cells[2,SetupGrid.RowCount - 1] := PrimaryColumnsList.Items[PrimaryColumnsList.ItemIndex];
SetupGrid.Cells[3,SetupGrid.RowCount - 1] := TableList.Items[TableList.ItemIndex];
SetupGrid.Cells[4,SetupGrid.RowCount - 1] := ColumnsList.Items[ColumnsList.ItemIndex];
end;
procedure TMainForm.FormClose(Sender: TObject; var CloseAction: TCloseAction);
var
ConfigFilePath :String;
INI: TINIFile;
begin
CloseConnections();
ConfigFilePath := GetAppConfigFile(False);
INI := TINIFile.Create(ConfigFilePath);
try
INI.WriteString('DB','FromUserName',FromUserName.Text);
INI.WriteString('DB','FromPassword',encrypt(FromPassword.Text));
INI.WriteString('DB','FromServerName',FromServerName.Text);
INI.WriteString('DB','FromPort',FromPort.Text);
If SQLTypeSelect.ItemIndex <> -1 then
INI.WriteInteger('DB','SQLType',SQLTypeSelect.ItemIndex);
If LastFromDB <> -1 then
INI.WriteInteger('DB','FromDatabase',LastFromDB);
INI.WriteString('COMPARE','ToTableName',ToTableName.Text);
INI.WriteString('COMPARE','FromUniqueField',FromUniqueField.Text);
INI.WriteString('COMPARE','FromSQLEdit',EncodeStringBase64(FromSQLEdit.Text));
INI.WriteString('COMPARE','ToSQLEdit',EncodeStringBase64(ToSQLEdit.Text));
INI.WriteString('COMPARE','FromFieldsEdit',FromFieldsEdit.Text);
INI.WriteString('COMPARE','ToFieldsEdit',ToFieldsEdit.Text);
INI.WriteString('COMPARE','ToUserName',ToUserName.Text);
INI.WriteString('COMPARE','ToPassword',encrypt(ToPassword.Text));
INI.WriteString('COMPARE','ToServerName',ToServerName.Text);
INI.WriteString('COMPARE','ToPort',ToPort.Text);
INI.WriteString('COMPARE','ToDatabase',ToDatabase.Text);
INI.WriteString('FINDANDREPLACE','Find',FindEdt.Text);
INI.WriteString('FINDANDREPLACE','ReplaceWith',ReplaceWithEdt.Text);
INI.WriteString('FINDANDREPLACE','Prefix',PrefixEdt.Text);
INI.WriteString('SCRIPTS','SQL' + InttoStr(TabControl1.TabIndex),EncodeStringBase64(ScriptSQLEdit.Text));
INI.WriteInteger('FORM','PageControl1',Pagecontrol1.TabIndex);
finally
INI.Free;
end;
end;
procedure TMainForm.GenerateQueriesBtnClick(Sender: TObject);
var
button: TControl;
lowerLeft: TPoint;
begin
if Sender is TControl then
begin
button := TControl(Sender);
lowerLeft := Point(0, button.Height);
lowerLeft := button.ClientToScreen(lowerLeft);
CreateQueriesPopup.Popup(lowerLeft.X, lowerLeft.Y);
end;
end;
procedure TMainForm.GenerateInsertfromResultsetMNUClick(Sender: TObject);
var
I,ProgressI:Integer;
FieldsString,ValuesString,QueryString:WideString;
tempQueryString: WideString;
ScriptQuery: TSQLQuery;
begin
if (Dataform.FromConnection.Connected = False) and (Dataform.FromMySQL80Connection.Connected = False) then
begin
showmessage('Connect to SQL server first!');
exit;
end;
if POS(ScriptTableName.Text,ScriptSQLEdit.Text) = 0 then
begin
showmessage('Table name must be present in Query');
exit;
end;
ScriptSQL.Clear;
FieldsString := '';
ValuesString := '';
case TabControl1.TabIndex of
0 :
begin
ScriptQuery := DataForm.ScriptQuery0;
end;
1 :
begin
ScriptQuery := DataForm.ScriptQuery1;
end;
end;
ProgressBar1.Max := ScriptQuery.RecordCount;
ProgressBar1.Position := 0;
ScriptQuery.First;
ScriptQuery.DisableControls;
while not ScriptQuery.EOF do
begin
ProgressBar1.StepIt;
Application.processMessages;
for I := 0 to ScriptQuery.Fields.Count - 1 do
begin
if FieldsString = '' then
begin
FieldsString := ScriptQuery.Fields[I].FieldName;
ValuesString := ConvertFieldtoSQLString(ScriptQuery.Fields[I]);
end
else
begin
FieldsString := FieldsString + ',' + ScriptQuery.Fields[I].FieldName;
ValuesString := ValuesString + ',' + ConvertFieldtoSQLString(ScriptQuery.Fields[I]);
end;
end;
QueryString := 'insert into ' + ScriptTableName.Text + ' (' + FieldsString + ') Values (' + ValuesString + ');' + #13#10;
tempQueryString := tempQueryString + QueryString;
FieldsString := '';
ValuesString := '';
ScriptQuery.Next;
end;
ScriptSQL.Lines.Clear;
ScriptSQL.Lines.Add(tempQueryString);
ScriptQuery.EnableControls;
showmessage('Finished');
end;
procedure TMainForm.GenerateInsertsFromAllCSVRecordsMNUClick(Sender: TObject);
var
QueryString: String;
I: integer;
begin
if Dataform.FromConnection.Connected = False then
begin
showmessage('Connect from database first');
exit;
end;
if Dataform.CSVDataSet.Active = False then
begin
showmessage('Load CSV File First first');
exit;
end;
Dataform.CSVDataset.First;
Dataform.CSVDataset.DisableControls;
ProgressBar1.Max := Dataform.CSVDataset.RecordCount;
ProgressBar1.Position := 0;
LogMemo.Clear;
while not Dataform.CSVDataset.EOF do
begin
ProgressBar1.StepIt;
Application.processMessages;
FieldsString := '';
ValuesString := '';
QueryString := GenerateQueryLine('Insert');
LogMemo.Lines.Add(QueryString);
Dataform.CSVDataset.Next;
end;
Dataform.CSVDataset.EnableControls;
Dataform.FromQuery1.Close;
end;
procedure TMainForm.GenerateInsertsfromPrimaryCSVFieldNotFoundMNUClick(
Sender: TObject);
var
Found: boolean;
PrimaryCSVColumnStr, PrimaryKeyStr, PrimaryTableNameStr, PrimaryColumnStr: String;
I: Integer;
QueryString: String;
begin
if Dataform.FromConnection.Connected = False then
begin
showmessage('Connect from database first');
exit;
end;
if Dataform.CSVDataSet.Active = False then
begin
showmessage('Load CSV File First first');
exit;
end;
Found := false;
for I := 0 to SetupGrid.RowCount - 1 do
begin
If SetupGrid.Cells[0,I] = 'Primary' then
begin
PrimaryCSVColumnStr := SetupGrid.Cells[1,I];
PrimaryKeyStr := SetupGrid.Cells[2,I];
PrimaryTableNameStr := SetupGrid.Cells[3,I];
PrimaryColumnStr := SetupGrid.Cells[4,I];
Found := true;
end;
end;
If Found = false then
begin
showmessage('Primary setup entry not found');
exit;
end;
Dataform.CSVDataset.First;
Dataform.CSVDataset.DisableControls;
ProgressBar1.Max := Dataform.CSVDataset.RecordCount;
ProgressBar1.Position := 0;
LogMemo.Clear;
while not Dataform.CSVDataset.EOF do
begin
ProgressBar1.StepIt;
Application.processMessages;
Dataform.FromQuery1.Close;
with Dataform.FromQuery1.SQL do
begin
Clear;
Add('select ' + PrimaryColumnStr + ' from ' + PrimaryTableNameStr);
Add('where ' + PrimaryColumnStr + ' = ''' + FixSQLString(Dataform.CSVDataset.FieldByName(PrimaryCSVColumnStr).asString) + '''');
end;
try
Dataform.FromQuery1.Open;
except
on E : Exception do
begin
ShowMessage(E.ClassName + ' ' + E.Message + ' Error in Test');
exit;
end;
end;
If Dataform.FromQuery1.RecordCount = 0 then
begin
FieldsString := '';
ValuesString := '';
QueryString := GenerateQueryLine('Insert');