From 6491160c99655c4a951c652e1eb975e184bdb03a Mon Sep 17 00:00:00 2001 From: Ansgar Becker Date: Sat, 21 Nov 2009 17:22:40 +0000 Subject: [PATCH] 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. --- packages/delphi11/heidisql.dpr | 1 - packages/delphi11/heidisql.dproj | 3 - source/insertfiles.pas | 97 ++++++++++++++++-- source/insertfiles_progress.dfm | 88 ----------------- source/insertfiles_progress.pas | 163 ------------------------------- 5 files changed, 89 insertions(+), 263 deletions(-) delete mode 100644 source/insertfiles_progress.dfm delete mode 100644 source/insertfiles_progress.pas diff --git a/packages/delphi11/heidisql.dpr b/packages/delphi11/heidisql.dpr index 35684dc9..30672cdb 100644 --- a/packages/delphi11/heidisql.dpr +++ b/packages/delphi11/heidisql.dpr @@ -15,7 +15,6 @@ uses printlist in '..\..\source\printlist.pas' {printlistForm}, copytable in '..\..\source\copytable.pas' {CopyTableForm}, insertfiles in '..\..\source\insertfiles.pas' {frmInsertFiles}, - insertfiles_progress in '..\..\source\insertfiles_progress.pas' {frmInsertFilesProgress}, helpers in '..\..\source\helpers.pas', sqlhelp in '..\..\source\sqlhelp.pas' {frmSQLhelp}, mysql_structures in '..\..\source\mysql_structures.pas', diff --git a/packages/delphi11/heidisql.dproj b/packages/delphi11/heidisql.dproj index b7222ae3..de502b12 100644 --- a/packages/delphi11/heidisql.dproj +++ b/packages/delphi11/heidisql.dproj @@ -144,9 +144,6 @@
frmInsertFiles
- -
frmInsertFilesProgress
-
loaddataform
diff --git a/source/insertfiles.pas b/source/insertfiles.pas index 8c3fd99c..7642944e 100644 --- a/source/insertfiles.pas +++ b/source/insertfiles.pas @@ -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 } diff --git a/source/insertfiles_progress.dfm b/source/insertfiles_progress.dfm deleted file mode 100644 index 34c01b04..00000000 --- a/source/insertfiles_progress.dfm +++ /dev/null @@ -1,88 +0,0 @@ -object frmInsertFilesProgress: TfrmInsertFilesProgress - Left = 609 - Top = 147 - BorderStyle = bsDialog - Caption = 'Inserting files...' - ClientHeight = 142 - ClientWidth = 417 - Color = clBtnFace - Font.Charset = DEFAULT_CHARSET - Font.Color = clWindowText - Font.Height = -11 - Font.Name = 'Tahoma' - Font.Style = [] - OldCreateOrder = False - Position = poOwnerFormCenter - OnClose = FormClose - OnCreate = FormCreate - OnShow = FormShow - PixelsPerInch = 96 - TextHeight = 13 - object Label1: TLabel - Left = 16 - Top = 32 - Width = 46 - Height = 13 - Caption = 'Filename:' - end - object Label2: TLabel - Left = 16 - Top = 48 - Width = 90 - Height = 13 - Caption = 'Current operation:' - end - object Label3: TLabel - Left = 16 - Top = 16 - Width = 15 - Height = 13 - Caption = 'Nr.' - end - object lblNumber: TLabel - Left = 131 - Top = 16 - Width = 47 - Height = 13 - Caption = 'lblNumber' - end - object lblFilename: TLabel - Left = 131 - Top = 32 - Width = 52 - Height = 13 - Caption = 'lblFilename' - end - object lblOperation: TLabel - Left = 131 - Top = 48 - Width = 58 - Height = 13 - Caption = 'lblOperation' - end - object pbReadingFiles: TProgressBar - Left = 16 - Top = 72 - Width = 385 - Height = 17 - Step = 1 - TabOrder = 0 - end - object btnCancel: TButton - Left = 168 - Top = 104 - Width = 75 - Height = 25 - Caption = 'Cancel' - ModalResult = 2 - TabOrder = 1 - OnClick = btnCancelClick - end - object timerStartReading: TTimer - Enabled = False - Interval = 1 - OnTimer = ProcessFiles - Left = 24 - Top = 104 - end -end diff --git a/source/insertfiles_progress.pas b/source/insertfiles_progress.pas deleted file mode 100644 index b85101df..00000000 --- a/source/insertfiles_progress.pas +++ /dev/null @@ -1,163 +0,0 @@ -unit insertfiles_progress; - -interface - -uses - Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, - StdCtrls, ComCtrls, ExtCtrls, mysql_connection; - -type - TfrmInsertFilesProgress = class(TForm) - pbReadingFiles: TProgressBar; - Label1: TLabel; - Label2: TLabel; - Label3: TLabel; - btnCancel: TButton; - lblNumber: TLabel; - lblFilename: TLabel; - lblOperation: TLabel; - timerStartReading: TTimer; - procedure FormClose(Sender: TObject; var Action: TCloseAction); - procedure btnCancelClick(Sender: TObject); - procedure ProcessFiles(Sender: TObject); - procedure FormShow(Sender: TObject); - procedure FormCreate(Sender: TObject); - private - Canceled : Boolean; - end; - - -implementation - -uses main, helpers, insertfiles; - -{$I const.inc} -{$R *.DFM} - -procedure TfrmInsertFilesProgress.btnCancelClick(Sender: TObject); -begin - Canceled := true; -end; - -procedure TfrmInsertFilesProgress.ProcessFiles(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; - Caller: TfrmInsertFiles; -begin - timerStartReading.Enabled := false; - Screen.Cursor := crHourglass; - Caller := Owner as TfrmInsertFiles; - pbReadingFiles.Max := Caller.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 := 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(Caller.ComboBoxDBs.Text)+'.'+mainform.mask(Caller.ComboBoxTables.Text) + - ' (' + mainform.mask(Caller.ComboBoxColumns.Text); - lblOperation.caption := 'Inserting data ...'; - lblOperation.Repaint; - 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; - lblOperation.caption := 'Reading file ...'; - lblOperation.Repaint; - 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(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]); - 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 Caller.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); - lblOperation.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; -end; - -procedure TfrmInsertFilesProgress.FormClose(Sender: TObject; - var Action: TCloseAction); -begin - Action := caFree; -end; - -procedure TfrmInsertFilesProgress.FormCreate(Sender: TObject); -begin - InheritFont(Font); -end; - -procedure TfrmInsertFilesProgress.FormShow(Sender: TObject); -begin - pbReadingFiles.Position := 0; - lblNumber.Caption := ''; - lblFilename.Caption := ''; - lblOperation.Caption := ''; - Canceled := false; - timerStartReading.Enabled := true; -end; - - -end.