Remove progress form for "Insert files" dialog. Instead, use the universal progress bar on main form's status bar and integrate existing code into the dialog's unit.

This commit is contained in:
Ansgar Becker
2009-11-21 17:22:40 +00:00
parent d6367a09fa
commit 6491160c99
5 changed files with 89 additions and 263 deletions

View File

@ -4,8 +4,7 @@ interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, ImgList, Buttons, ShellApi, Math, insertfiles_progress,
mysql_connection;
StdCtrls, ComCtrls, ImgList, Buttons, ShellApi, Math, mysql_connection;
type TCol = record
Name : String; // for displaying in lists
@ -60,10 +59,6 @@ type
procedure addfile(filename: String);
procedure ListViewFilesChange(Sender: TObject; Item: TListItem;
Change: TItemChange);
private
FProgressForm : TfrmInsertFilesProgress;
public
{ Public declarations }
cols : Array of TCol;
@ -349,9 +344,95 @@ end;
{ ok, let's rock! }
procedure TfrmInsertFiles.ButtonInsertClick(Sender: TObject);
const
ChunkSize = 131072;
var
i, j: Integer;
value, filename: String;
dt: TDateTime;
y, m, d, h, mi, s, ms: Word;
FileStream: TFileStream;
readBuf: String;
bytesRead: Integer;
sql, data: WideString;
begin
FProgressForm := TfrmInsertFilesProgress.Create(Self);
FProgressForm.ShowModal;
Screen.Cursor := crHourglass;
EnableProgressBar(ListViewFiles.Items.Count);
try
for i:=0 to ListViewFiles.Items.Count-1 do begin
filename := ListViewFiles.Items[i].Caption;
ListViewFiles.ClearSelection;
ListViewFiles.ItemFocused := ListViewFiles.Items[i];
ListViewFiles.Selected := ListViewFiles.ItemFocused;
ListViewFiles.ItemFocused.MakeVisible(False);
ListViewFiles.Repaint;
sql := 'INSERT INTO '+mainform.mask(ComboBoxDBs.Text)+'.'+mainform.mask(ComboBoxTables.Text) +
' (' + mainform.mask(ComboBoxColumns.Text);
for j:=0 to length(cols)-1 do begin
if cols[j].Name = ComboBoxColumns.Text then
Continue;
sql := sql + ', ' + mainform.mask(cols[j].Name);
end;
FileStream := TFileStream.Create( filename, fmShareDenyWrite );
try
data := '_binary 0x';
while FileStream.Position < FileStream.Size do begin
// Read characters from file.
// Data is imported as-is (byte for byte).
// How the server interprets the data is decided by the
// character set on the column that is imported into.
// Set the character set on the column before importing the file.
//
// TODO: Indicate this character set on the GUI.
//
SetLength(readBuf, ChunkSize div SizeOf(Char));
bytesRead := FileStream.Read(PChar(readBuf)^, ChunkSize);
SetLength(readBuf, bytesRead div SizeOf(Char));
data := data + BinToWideHex(readBuf);
end;
finally
FileStream.Free;
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
//Value := stringreplace(Value, '%filesize%', inttostr(size), [rfReplaceAll]);
Value := stringreplace(Value, '%filename%', ExtractFileName(filename), [rfReplaceAll]);
Value := stringreplace(Value, '%filepath%', ExtractFilePath(filename), [rfReplaceAll]);
FileAge(filename, dt);
DecodeDate(dt, y, m, d);
DecodeTime(dt, h, mi, s, ms);
Value := stringreplace(Value, '%filedate%', Format('%.4d-%.2d-%.2d', [y,m,d]), [rfReplaceAll]);
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
Value := esc(Value);
sql := sql + Value + ', ';
end;
// Strip last comma + space
sql := copy(sql, 1, length(sql)-2);
sql := sql + ')';
Mainform.Connection.Query(sql);
Mainform.ProgressBarStatus.StepIt;
Mainform.ProgressBarStatus.Repaint;
end;
Screen.Cursor := crDefault;
Mainform.ProgressBarStatus.Hide;
Close;
except
on E:Exception do begin
Screen.Cursor := crDefault;
MessageDlg(E.Message, mtError, [mbOK], 0);
Mainform.ProgressBarStatus.Hide;
end;
end;
end;
{ Execute selected file }