Skip to content

Commit 9fb90d1

Browse files
committed
feat: prevent loading an SQL file multiple times into an editor
Shows an information dialog when a loaded file is being selected again through the various load-file mechanism: load-file dialog, recent files menu, dropping on editor, passing on command line Refs #1403
1 parent 4016741 commit 9fb90d1

File tree

1 file changed

+52
-41
lines changed

1 file changed

+52
-41
lines changed

source/main.pas

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,7 @@ TMainForm = class(TExtForm)
13091309
procedure SetSnippetFilenames;
13101310
function TreeClickHistoryPrevious(MayBeNil: Boolean=False): PVirtualNode;
13111311
procedure OperationRunning(Runs: Boolean);
1312+
procedure OpenQueryFiles(Filenames: TStrings; Encoding: TEncoding; ForceRun: Boolean);
13121313
function RunQueryFiles(Filenames: TStrings; Encoding: TEncoding; ForceRun: Boolean): Boolean;
13131314
function RunQueryFile(Filename: String; Encoding: TEncoding; Conn: TDBConnection;
13141315
ProgressDialog: IProgressDialog; FilesizeSum: Int64; var CurrentPosition: Int64): Boolean;
@@ -2215,7 +2216,6 @@ procedure TMainForm.AfterFormCreate;
22152216
StatsCall: THttpDownload;
22162217
SessionPaths: TStringlist;
22172218
DlgResult: TModalResult;
2218-
Tab: TQueryTab;
22192219
SessionManager: TConnForm;
22202220
begin
22212221
if AppSettings.ReadBool(asUpdatecheck) then begin
@@ -2350,14 +2350,7 @@ procedure TMainForm.AfterFormCreate;
23502350
end;
23512351

23522352
// Load SQL file(s) by command line
2353-
if not RunQueryFiles(FileNames, nil, false) then begin
2354-
for i:=0 to FileNames.Count-1 do begin
2355-
Tab := GetOrCreateEmptyQueryTab(False);
2356-
Tab.LoadContents(FileNames[i], True, nil);
2357-
if i = FileNames.Count-1 then
2358-
SetMainTab(Tab.TabSheet);
2359-
end;
2360-
end;
2353+
OpenQueryFiles(FileNames, nil, False);
23612354

23622355
MainFormAfterCreateDone := True;
23632356
end;
@@ -3980,10 +3973,9 @@ procedure TMainForm.actLaunchCommandlineExecute(Sender: TObject);
39803973
// Load SQL-file, make sure that SheetQuery is activated
39813974
procedure TMainForm.actLoadSQLExecute(Sender: TObject);
39823975
var
3983-
i, ProceedResult: Integer;
3976+
ProceedResult: Integer;
39843977
Dialog: TExtFileOpenDialog;
39853978
Encoding: TEncoding;
3986-
Tab: TQueryTab;
39873979
begin
39883980
AppSettings.ResetPath;
39893981
Dialog := TExtFileOpenDialog.Create(Self);
@@ -4005,21 +3997,59 @@ procedure TMainForm.actLoadSQLExecute(Sender: TObject);
40053997
end;
40063998

40073999
if ProceedResult = mrYes then begin
4008-
if not RunQueryFiles(Dialog.Files, Encoding, Sender=actRunSQL) then begin
4009-
for i:=0 to Dialog.Files.Count-1 do begin
4010-
Tab := GetOrCreateEmptyQueryTab(False);
4011-
Tab.LoadContents(Dialog.Files[i], True, Encoding);
4012-
if i = Dialog.Files.Count-1 then
4013-
SetMainTab(Tab.TabSheet);
4014-
end;
4015-
end;
4000+
OpenQueryFiles(Dialog.Files, Encoding, Sender=actRunSQL);
40164001
end;
40174002
AppSettings.WriteInt(asFileDialogEncoding, Dialog.EncodingIndex, Self.Name);
40184003
end;
40194004
Dialog.Free;
40204005
end;
40214006

40224007

4008+
procedure TMainForm.OpenQueryFiles(Filenames: TStrings; Encoding: TEncoding; ForceRun: Boolean);
4009+
var
4010+
Tab, FileInTab: TQueryTab;
4011+
FileHints: TStringList;
4012+
i: Integer;
4013+
begin
4014+
// Decides whether to run or load files, prevents duplicates etc.
4015+
if RunQueryFiles(Filenames, Encoding, ForceRun) then
4016+
Exit;
4017+
4018+
FileHints := TStringList.Create;
4019+
4020+
for i:=0 to Filenames.Count-1 do begin
4021+
4022+
FileInTab := nil;
4023+
for Tab in QueryTabs do begin
4024+
if Tab.MemoFilename = Filenames[i] then begin
4025+
FileInTab := Tab;
4026+
FileHints.Add(f_('This file is already open in query tab #%d.', [FileInTab.Number]) + ' ' + ExtractFileName(Filenames[i]));
4027+
if i = Filenames.Count-1 then
4028+
SetMainTab(FileInTab.TabSheet);
4029+
Break;
4030+
end;
4031+
end;
4032+
4033+
if not Assigned(FileInTab) then begin
4034+
Tab := GetOrCreateEmptyQueryTab(False);
4035+
Tab.LoadContents(Filenames[i], True, Encoding);
4036+
if i = Filenames.Count-1 then
4037+
SetMainTab(Tab.TabSheet);
4038+
end;
4039+
end;
4040+
4041+
if not FileHints.IsEmpty then begin
4042+
if MainFormAfterCreateDone then
4043+
MessageDialog(FileHints.Text, mtInformation, [mbOK])
4044+
else begin
4045+
for i:=0 to FileHints.Count-1 do
4046+
LogSQL(FileHints[i]);
4047+
end;
4048+
end;
4049+
FileHints.Free;
4050+
end;
4051+
4052+
40234053
function TMainForm.RunQueryFiles(Filenames: TStrings; Encoding: TEncoding; ForceRun: Boolean): Boolean;
40244054
var
40254055
i, FilesProcessed: Integer;
@@ -5245,7 +5275,6 @@ procedure TMainform.popupQueryLoadClick(Sender: TObject);
52455275
Filename: String;
52465276
FileList: TStringList;
52475277
p: Integer;
5248-
Tab: TQueryTab;
52495278
begin
52505279
// Click on the popupQueryLoad
52515280
Filename := (Sender as TMenuItem).Caption;
@@ -5258,10 +5287,7 @@ procedure TMainform.popupQueryLoadClick(Sender: TObject);
52585287
end;
52595288
FileList := TStringList.Create;
52605289
FileList.Add(Filename);
5261-
if not RunQueryFiles(FileList, nil, false) then begin
5262-
Tab := GetOrCreateEmptyQueryTab(True);
5263-
Tab.LoadContents(Filename, True, nil);
5264-
end;
5290+
OpenQueryFiles(FileList, nil, False);
52655291
FileList.Free;
52665292
end;
52675293

@@ -7698,18 +7724,10 @@ procedure TMainForm.SynMemoQueryDragDrop(Sender, Source: TObject; X,
76987724

76997725
procedure TMainForm.SynMemoQueryDropFiles(Sender: TObject; X, Y: Integer;
77007726
AFiles: TUnicodeStrings);
7701-
var
7702-
i: Integer;
7703-
Tab: TQueryTab;
77047727
begin
77057728
// One or more files from explorer or somewhere else was dropped onto the
77067729
// query-memo - load their contents into seperate tabs
7707-
if not RunQueryFiles(AFiles, nil, False) then begin
7708-
for i:=0 to AFiles.Count-1 do begin
7709-
Tab := GetOrCreateEmptyQueryTab(True);
7710-
Tab.LoadContents(AFiles[i], False, nil);
7711-
end;
7712-
end;
7730+
OpenQueryFiles(AFiles, nil, False);
77137731
end;
77147732

77157733

@@ -13822,9 +13840,7 @@ procedure TMainForm.actDataResetSortingExecute(Sender: TObject);
1382213840

1382313841
procedure TMainForm.WMCopyData(var Msg: TWMCopyData);
1382413842
var
13825-
i: Integer;
1382613843
Connection: TDBConnection;
13827-
Tab: TQueryTab;
1382813844
ConnectionParams: TConnectionParameters;
1382913845
FileNames: TStringList;
1383013846
RunFrom: String;
@@ -13834,12 +13850,7 @@ procedure TMainForm.WMCopyData(var Msg: TWMCopyData);
1383413850
LogSQL(f_('Preventing second application instance - disabled in %s > %s > %s.', [_('Tools'), _('Preferences'), _('General')]), lcInfo);
1383513851
ConnectionParams := nil;
1383613852
ParseCommandLine(ParamBlobToStr(Msg.CopyDataStruct.lpData), ConnectionParams, FileNames, RunFrom);
13837-
if not RunQueryFiles(FileNames, nil, False) then begin
13838-
for i:=0 to FileNames.Count-1 do begin
13839-
Tab := GetOrCreateEmptyQueryTab(True);
13840-
Tab.LoadContents(FileNames[i], True, nil);
13841-
end;
13842-
end;
13853+
OpenQueryFiles(FileNames, nil, False);
1384313854
if ConnectionParams <> nil then
1384413855
InitConnection(ConnectionParams, True, Connection);
1384513856
end else

0 commit comments

Comments
 (0)