Code cosmetic in "Insert files" dialog:

* Make sure the INSERT query is fired within a try/except block, and keep form open on any error
* Reasonably rename some component names
* Remove InsertFilesWindow(), instead cache the instance of InsertFiles in main form, like it is for most other dialogs
* Get rid of a with .. do block
* Ger rid of a weird form pointer, instead use the "Owner" property
This commit is contained in:
Ansgar Becker
2009-11-21 16:44:31 +00:00
parent 4b7ce24266
commit d6367a09fa
4 changed files with 75 additions and 96 deletions

View File

@ -70,8 +70,6 @@ type
procedure AcceptFiles( var msg : TMessage ); message WM_DROPFILES;
end;
function InsertFilesWindow (AOwner : TComponent; Flags : String = '') : Boolean;
implementation
@ -80,16 +78,6 @@ uses main, helpers, db;
{$R *.DFM}
function InsertFilesWindow (AOwner : TComponent; Flags : String = '') : Boolean;
var
f : TfrmInsertFiles;
begin
f := TfrmInsertFiles.Create(AOwner);
// todo: pass flags
Result := (f.ShowModal=mrOK);
FreeAndNil (f);
end;
{ FormShow }
procedure TfrmInsertFiles.FormShow(Sender: TObject);
begin
@ -363,7 +351,6 @@ end;
procedure TfrmInsertFiles.ButtonInsertClick(Sender: TObject);
begin
FProgressForm := TfrmInsertFilesProgress.Create(Self);
FProgressForm.InsertFilesForm := Self;
FProgressForm.ShowModal;
end;

View File

@ -40,27 +40,27 @@ object frmInsertFilesProgress: TfrmInsertFilesProgress
Caption = 'Nr.'
end
object lblNumber: TLabel
Left = 112
Left = 131
Top = 16
Width = 47
Height = 13
Caption = 'lblNumber'
end
object lblFilename: TLabel
Left = 112
Left = 131
Top = 32
Width = 52
Height = 13
Caption = 'lblFilename'
end
object lblOperation: TLabel
Left = 112
Left = 131
Top = 48
Width = 58
Height = 13
Caption = 'lblOperation'
end
object ProgressBar1: TProgressBar
object pbReadingFiles: TProgressBar
Left = 16
Top = 72
Width = 385
@ -68,7 +68,7 @@ object frmInsertFilesProgress: TfrmInsertFilesProgress
Step = 1
TabOrder = 0
end
object Button1: TButton
object btnCancel: TButton
Left = 168
Top = 104
Width = 75
@ -76,9 +76,9 @@ object frmInsertFilesProgress: TfrmInsertFilesProgress
Caption = 'Cancel'
ModalResult = 2
TabOrder = 1
OnClick = Button1Click
OnClick = btnCancelClick
end
object Timer1: TTimer
object timerStartReading: TTimer
Enabled = False
Interval = 1
OnTimer = ProcessFiles

View File

@ -8,25 +8,22 @@ uses
type
TfrmInsertFilesProgress = class(TForm)
ProgressBar1: TProgressBar;
pbReadingFiles: TProgressBar;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Button1: TButton;
btnCancel: TButton;
lblNumber: TLabel;
lblFilename: TLabel;
lblOperation: TLabel;
Timer1: TTimer;
timerStartReading: TTimer;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
procedure btnCancelClick(Sender: TObject);
procedure ProcessFiles(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
FInsertFilesForm : Pointer;
canceled : Boolean;
public
property InsertFilesForm : Pointer read FInsertFilesForm write FInsertFilesForm;
Canceled : Boolean;
end;
@ -37,9 +34,9 @@ uses main, helpers,insertfiles;
{$I const.inc}
{$R *.DFM}
procedure TfrmInsertFilesProgress.Button1Click(Sender: TObject);
procedure TfrmInsertFilesProgress.btnCancelClick(Sender: TObject);
begin
canceled := true;
Canceled := true;
end;
procedure TfrmInsertFilesProgress.ProcessFiles(Sender: TObject);
@ -55,34 +52,31 @@ var
readBuf: String;
bytesRead: Integer;
sql, data: WideString;
Caller: TfrmInsertFiles;
begin
Timer1.Enabled := false;
screen.Cursor := crHourglass;
ProgressBar1.Max := TfrmInsertFiles(FInsertFilesForm).ListViewFiles.Items.Count;
timerStartReading.Enabled := false;
Screen.Cursor := crHourglass;
Caller := Owner as TfrmInsertFiles;
pbReadingFiles.Max := Caller.ListViewFiles.Items.Count;
TRY
with TfrmInsertFiles(FInsertFilesForm) do
begin
for i:=0 to ListViewFiles.Items.Count-1 do
begin
if self.canceled then break;
lblNumber.Caption := inttostr(i+1)+' of ' + inttostr(ListViewFiles.Items.Count);
try
for i:=0 to Caller.ListViewFiles.Items.Count-1 do begin
if Canceled then
break;
lblNumber.Caption := inttostr(i+1)+' of ' + inttostr(Caller.ListViewFiles.Items.Count);
lblNumber.Repaint;
filename := ListViewFiles.Items[i].Caption;
lblFilename.Caption := mince(filename, 30) + ' ('+FormatNumber(ListViewFiles.Items[i].SubItems[0])+' KB)';
filename := Caller.ListViewFiles.Items[i].Caption;
lblFilename.Caption := mince(filename, 30) + ' ('+FormatNumber(Caller.ListViewFiles.Items[i].SubItems[0])+' KB)';
lblFilename.Repaint;
sql := 'INSERT INTO '+mainform.mask(ComboBoxDBs.Text)+'.'+mainform.mask(ComboBoxTables.Text) +
' (' + mainform.mask(ComboBoxColumns.Text);
sql := 'INSERT INTO '+mainform.mask(Caller.ComboBoxDBs.Text)+'.'+mainform.mask(Caller.ComboBoxTables.Text) +
' (' + mainform.mask(Caller.ComboBoxColumns.Text);
lblOperation.caption := 'Inserting data ...';
lblOperation.Repaint;
for j:=0 to length(cols)-1 do
begin
if cols[j].Name = ComboBoxColumns.Text then
continue;
sql := sql + ', ' + mainform.mask(cols[j].Name);
for j:=0 to length(Caller.cols)-1 do begin
if Caller.cols[j].Name = Caller.ComboBoxColumns.Text then
Continue;
sql := sql + ', ' + mainform.mask(Caller.cols[j].Name);
end;
try
lblOperation.caption := 'Reading file ...';
lblOperation.Repaint;
FileStream := TFileStream.Create( filename, fmShareDenyWrite );
@ -105,19 +99,13 @@ begin
finally
FileStream.Free;
end;
except
MessageDlg( 'Error reading file:' + CRLF + filename, mtError, [mbOK], 0 );
break;
end;
sql := sql + ') VALUES ('+data+', ';
for j:=0 to length(cols)-1 do
begin
if cols[j].Name = ComboBoxColumns.Text then
continue;
Value := cols[j].Value;
if pos('%', Value) > 0 then
begin
for j:=0 to Length(Caller.cols)-1 do begin
if Caller.cols[j].Name = Caller.ComboBoxColumns.Text then
Continue;
Value := Caller.cols[j].Value;
if pos('%', Value) > 0 then begin
//Value := stringreplace(Value, '%filesize%', inttostr(size), [rfReplaceAll]);
Value := stringreplace(Value, '%filename%', ExtractFileName(filename), [rfReplaceAll]);
Value := stringreplace(Value, '%filepath%', ExtractFilePath(filename), [rfReplaceAll]);
@ -128,7 +116,7 @@ begin
Value := stringreplace(Value, '%filedatetime%', Format('%.4d-%.2d-%.2d %.2d:%.2d:%.2d', [y,m,d,h,mi,s]), [rfReplaceAll]);
Value := stringreplace(Value, '%filetime%', Format('%.2d:%.2d:%.2d', [h,mi,s]), [rfReplaceAll]);
end;
if cols[j].Quote then
if Caller.cols[j].Quote then
Value := esc(Value);
sql := sql + Value + ', ';
end;
@ -136,17 +124,18 @@ begin
sql := copy(sql, 1, length(sql)-2);
sql := sql + ')';
Mainform.Connection.Query(sql);
lblOperation.caption := 'Freeing memory ...';
lblOperation.Repaint;
ProgressBar1.StepIt;
ProgressBar1.Repaint;
pbReadingFiles.StepIt;
pbReadingFiles.Repaint;
end;
Screen.Cursor := crDefault;
Close;
except
on E:Exception do begin
Screen.Cursor := crDefault;
MessageDlg(E.Message, mtError, [mbOK], 0);
end;
end;
FINALLY
screen.Cursor := crDefault;
Close();
END;
end;
procedure TfrmInsertFilesProgress.FormClose(Sender: TObject;
@ -162,12 +151,12 @@ end;
procedure TfrmInsertFilesProgress.FormShow(Sender: TObject);
begin
ProgressBar1.Position := 0;
pbReadingFiles.Position := 0;
lblNumber.Caption := '';
lblFilename.Caption := '';
lblOperation.Caption := '';
Canceled := false;
Timer1.Enabled := true;
timerStartReading.Enabled := true;
end;

View File

@ -22,7 +22,7 @@ uses
createdatabase, table_editor, SynRegExpr,
WideStrUtils, ExtActns, CommCtrl, routine_editor, options,
Contnrs, PngSpeedButton, connections, SynEditKeyCmds,
mysql_connection, mysql_api;
mysql_connection, mysql_api, insertfiles;
type
@ -791,6 +791,7 @@ type
prefNullBG : TColor;
CreateDatabaseForm : TCreateDatabaseForm;
TableEditor : TfrmTableEditor;
InsertFiles : TfrmInsertFiles;
FDataGridSelect : TWideStringList;
FDataGridSort : TOrderColArray;
DataGridCurrentSelect,
@ -899,7 +900,7 @@ const
implementation
uses
About, loaddata, printlist, copytable, insertfiles,
About, loaddata, printlist, copytable,
mysql_structures, UpdateCheck, uVistaFuncs, runsqlfile, column_selection,
data_sorting, grideditlinks, dataviewsave;
@ -2164,7 +2165,9 @@ end;
procedure TMainForm.actInsertFilesExecute(Sender: TObject);
begin
InsertFilesWindow(Self);
if not Assigned(InsertFiles) then
InsertFiles := TfrmInsertFiles.Create(Self);
InsertFiles.ShowModal;
end;
// Drop Table(s)