mirror of
https://github.com/HeidiSQL/HeidiSQL.git
synced 2025-08-06 18:24:26 +08:00
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:
@ -70,8 +70,6 @@ type
|
|||||||
procedure AcceptFiles( var msg : TMessage ); message WM_DROPFILES;
|
procedure AcceptFiles( var msg : TMessage ); message WM_DROPFILES;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function InsertFilesWindow (AOwner : TComponent; Flags : String = '') : Boolean;
|
|
||||||
|
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
@ -80,16 +78,6 @@ uses main, helpers, db;
|
|||||||
{$R *.DFM}
|
{$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 }
|
{ FormShow }
|
||||||
procedure TfrmInsertFiles.FormShow(Sender: TObject);
|
procedure TfrmInsertFiles.FormShow(Sender: TObject);
|
||||||
begin
|
begin
|
||||||
@ -363,7 +351,6 @@ end;
|
|||||||
procedure TfrmInsertFiles.ButtonInsertClick(Sender: TObject);
|
procedure TfrmInsertFiles.ButtonInsertClick(Sender: TObject);
|
||||||
begin
|
begin
|
||||||
FProgressForm := TfrmInsertFilesProgress.Create(Self);
|
FProgressForm := TfrmInsertFilesProgress.Create(Self);
|
||||||
FProgressForm.InsertFilesForm := Self;
|
|
||||||
FProgressForm.ShowModal;
|
FProgressForm.ShowModal;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
@ -40,27 +40,27 @@ object frmInsertFilesProgress: TfrmInsertFilesProgress
|
|||||||
Caption = 'Nr.'
|
Caption = 'Nr.'
|
||||||
end
|
end
|
||||||
object lblNumber: TLabel
|
object lblNumber: TLabel
|
||||||
Left = 112
|
Left = 131
|
||||||
Top = 16
|
Top = 16
|
||||||
Width = 47
|
Width = 47
|
||||||
Height = 13
|
Height = 13
|
||||||
Caption = 'lblNumber'
|
Caption = 'lblNumber'
|
||||||
end
|
end
|
||||||
object lblFilename: TLabel
|
object lblFilename: TLabel
|
||||||
Left = 112
|
Left = 131
|
||||||
Top = 32
|
Top = 32
|
||||||
Width = 52
|
Width = 52
|
||||||
Height = 13
|
Height = 13
|
||||||
Caption = 'lblFilename'
|
Caption = 'lblFilename'
|
||||||
end
|
end
|
||||||
object lblOperation: TLabel
|
object lblOperation: TLabel
|
||||||
Left = 112
|
Left = 131
|
||||||
Top = 48
|
Top = 48
|
||||||
Width = 58
|
Width = 58
|
||||||
Height = 13
|
Height = 13
|
||||||
Caption = 'lblOperation'
|
Caption = 'lblOperation'
|
||||||
end
|
end
|
||||||
object ProgressBar1: TProgressBar
|
object pbReadingFiles: TProgressBar
|
||||||
Left = 16
|
Left = 16
|
||||||
Top = 72
|
Top = 72
|
||||||
Width = 385
|
Width = 385
|
||||||
@ -68,7 +68,7 @@ object frmInsertFilesProgress: TfrmInsertFilesProgress
|
|||||||
Step = 1
|
Step = 1
|
||||||
TabOrder = 0
|
TabOrder = 0
|
||||||
end
|
end
|
||||||
object Button1: TButton
|
object btnCancel: TButton
|
||||||
Left = 168
|
Left = 168
|
||||||
Top = 104
|
Top = 104
|
||||||
Width = 75
|
Width = 75
|
||||||
@ -76,9 +76,9 @@ object frmInsertFilesProgress: TfrmInsertFilesProgress
|
|||||||
Caption = 'Cancel'
|
Caption = 'Cancel'
|
||||||
ModalResult = 2
|
ModalResult = 2
|
||||||
TabOrder = 1
|
TabOrder = 1
|
||||||
OnClick = Button1Click
|
OnClick = btnCancelClick
|
||||||
end
|
end
|
||||||
object Timer1: TTimer
|
object timerStartReading: TTimer
|
||||||
Enabled = False
|
Enabled = False
|
||||||
Interval = 1
|
Interval = 1
|
||||||
OnTimer = ProcessFiles
|
OnTimer = ProcessFiles
|
||||||
|
@ -8,38 +8,35 @@ uses
|
|||||||
|
|
||||||
type
|
type
|
||||||
TfrmInsertFilesProgress = class(TForm)
|
TfrmInsertFilesProgress = class(TForm)
|
||||||
ProgressBar1: TProgressBar;
|
pbReadingFiles: TProgressBar;
|
||||||
Label1: TLabel;
|
Label1: TLabel;
|
||||||
Label2: TLabel;
|
Label2: TLabel;
|
||||||
Label3: TLabel;
|
Label3: TLabel;
|
||||||
Button1: TButton;
|
btnCancel: TButton;
|
||||||
lblNumber: TLabel;
|
lblNumber: TLabel;
|
||||||
lblFilename: TLabel;
|
lblFilename: TLabel;
|
||||||
lblOperation: TLabel;
|
lblOperation: TLabel;
|
||||||
Timer1: TTimer;
|
timerStartReading: TTimer;
|
||||||
procedure FormClose(Sender: TObject; var Action: TCloseAction);
|
procedure FormClose(Sender: TObject; var Action: TCloseAction);
|
||||||
procedure Button1Click(Sender: TObject);
|
procedure btnCancelClick(Sender: TObject);
|
||||||
procedure ProcessFiles(Sender: TObject);
|
procedure ProcessFiles(Sender: TObject);
|
||||||
procedure FormShow(Sender: TObject);
|
procedure FormShow(Sender: TObject);
|
||||||
procedure FormCreate(Sender: TObject);
|
procedure FormCreate(Sender: TObject);
|
||||||
private
|
private
|
||||||
FInsertFilesForm : Pointer;
|
Canceled : Boolean;
|
||||||
canceled : Boolean;
|
|
||||||
public
|
|
||||||
property InsertFilesForm : Pointer read FInsertFilesForm write FInsertFilesForm;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
uses main, helpers,insertfiles;
|
uses main, helpers, insertfiles;
|
||||||
|
|
||||||
{$I const.inc}
|
{$I const.inc}
|
||||||
{$R *.DFM}
|
{$R *.DFM}
|
||||||
|
|
||||||
procedure TfrmInsertFilesProgress.Button1Click(Sender: TObject);
|
procedure TfrmInsertFilesProgress.btnCancelClick(Sender: TObject);
|
||||||
begin
|
begin
|
||||||
canceled := true;
|
Canceled := true;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TfrmInsertFilesProgress.ProcessFiles(Sender: TObject);
|
procedure TfrmInsertFilesProgress.ProcessFiles(Sender: TObject);
|
||||||
@ -55,69 +52,60 @@ var
|
|||||||
readBuf: String;
|
readBuf: String;
|
||||||
bytesRead: Integer;
|
bytesRead: Integer;
|
||||||
sql, data: WideString;
|
sql, data: WideString;
|
||||||
|
Caller: TfrmInsertFiles;
|
||||||
begin
|
begin
|
||||||
Timer1.Enabled := false;
|
timerStartReading.Enabled := false;
|
||||||
screen.Cursor := crHourglass;
|
Screen.Cursor := crHourglass;
|
||||||
ProgressBar1.Max := TfrmInsertFiles(FInsertFilesForm).ListViewFiles.Items.Count;
|
Caller := Owner as TfrmInsertFiles;
|
||||||
|
pbReadingFiles.Max := Caller.ListViewFiles.Items.Count;
|
||||||
|
|
||||||
TRY
|
try
|
||||||
|
for i:=0 to Caller.ListViewFiles.Items.Count-1 do begin
|
||||||
with TfrmInsertFiles(FInsertFilesForm) do
|
if Canceled then
|
||||||
begin
|
break;
|
||||||
for i:=0 to ListViewFiles.Items.Count-1 do
|
lblNumber.Caption := inttostr(i+1)+' of ' + inttostr(Caller.ListViewFiles.Items.Count);
|
||||||
begin
|
|
||||||
if self.canceled then break;
|
|
||||||
lblNumber.Caption := inttostr(i+1)+' of ' + inttostr(ListViewFiles.Items.Count);
|
|
||||||
lblNumber.Repaint;
|
lblNumber.Repaint;
|
||||||
filename := ListViewFiles.Items[i].Caption;
|
filename := Caller.ListViewFiles.Items[i].Caption;
|
||||||
lblFilename.Caption := mince(filename, 30) + ' ('+FormatNumber(ListViewFiles.Items[i].SubItems[0])+' KB)';
|
lblFilename.Caption := mince(filename, 30) + ' ('+FormatNumber(Caller.ListViewFiles.Items[i].SubItems[0])+' KB)';
|
||||||
lblFilename.Repaint;
|
lblFilename.Repaint;
|
||||||
sql := 'INSERT INTO '+mainform.mask(ComboBoxDBs.Text)+'.'+mainform.mask(ComboBoxTables.Text) +
|
sql := 'INSERT INTO '+mainform.mask(Caller.ComboBoxDBs.Text)+'.'+mainform.mask(Caller.ComboBoxTables.Text) +
|
||||||
' (' + mainform.mask(ComboBoxColumns.Text);
|
' (' + mainform.mask(Caller.ComboBoxColumns.Text);
|
||||||
lblOperation.caption := 'Inserting data ...';
|
lblOperation.caption := 'Inserting data ...';
|
||||||
lblOperation.Repaint;
|
lblOperation.Repaint;
|
||||||
for j:=0 to length(cols)-1 do
|
for j:=0 to length(Caller.cols)-1 do begin
|
||||||
begin
|
if Caller.cols[j].Name = Caller.ComboBoxColumns.Text then
|
||||||
if cols[j].Name = ComboBoxColumns.Text then
|
Continue;
|
||||||
continue;
|
sql := sql + ', ' + mainform.mask(Caller.cols[j].Name);
|
||||||
sql := sql + ', ' + mainform.mask(cols[j].Name);
|
|
||||||
end;
|
end;
|
||||||
|
lblOperation.caption := 'Reading file ...';
|
||||||
|
lblOperation.Repaint;
|
||||||
|
FileStream := TFileStream.Create( filename, fmShareDenyWrite );
|
||||||
try
|
try
|
||||||
lblOperation.caption := 'Reading file ...';
|
data := '_binary 0x';
|
||||||
lblOperation.Repaint;
|
while FileStream.Position < FileStream.Size do begin
|
||||||
FileStream := TFileStream.Create( filename, fmShareDenyWrite );
|
// Read characters from file.
|
||||||
try
|
// Data is imported as-is (byte for byte).
|
||||||
data := '_binary 0x';
|
// How the server interprets the data is decided by the
|
||||||
while FileStream.Position < FileStream.Size do begin
|
// character set on the column that is imported into.
|
||||||
// Read characters from file.
|
// Set the character set on the column before importing the file.
|
||||||
// Data is imported as-is (byte for byte).
|
//
|
||||||
// How the server interprets the data is decided by the
|
// TODO: Indicate this character set on the GUI.
|
||||||
// character set on the column that is imported into.
|
//
|
||||||
// Set the character set on the column before importing the file.
|
SetLength(readBuf, ChunkSize div SizeOf(Char));
|
||||||
//
|
bytesRead := FileStream.Read(PChar(readBuf)^, ChunkSize);
|
||||||
// TODO: Indicate this character set on the GUI.
|
SetLength(readBuf, bytesRead div SizeOf(Char));
|
||||||
//
|
data := data + BinToWideHex(readBuf);
|
||||||
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;
|
end;
|
||||||
except
|
finally
|
||||||
MessageDlg( 'Error reading file:' + CRLF + filename, mtError, [mbOK], 0 );
|
FileStream.Free;
|
||||||
break;
|
|
||||||
end;
|
end;
|
||||||
sql := sql + ') VALUES ('+data+', ';
|
sql := sql + ') VALUES ('+data+', ';
|
||||||
|
|
||||||
for j:=0 to length(cols)-1 do
|
for j:=0 to Length(Caller.cols)-1 do begin
|
||||||
begin
|
if Caller.cols[j].Name = Caller.ComboBoxColumns.Text then
|
||||||
if cols[j].Name = ComboBoxColumns.Text then
|
Continue;
|
||||||
continue;
|
Value := Caller.cols[j].Value;
|
||||||
Value := cols[j].Value;
|
if pos('%', Value) > 0 then begin
|
||||||
if pos('%', Value) > 0 then
|
|
||||||
begin
|
|
||||||
//Value := stringreplace(Value, '%filesize%', inttostr(size), [rfReplaceAll]);
|
//Value := stringreplace(Value, '%filesize%', inttostr(size), [rfReplaceAll]);
|
||||||
Value := stringreplace(Value, '%filename%', ExtractFileName(filename), [rfReplaceAll]);
|
Value := stringreplace(Value, '%filename%', ExtractFileName(filename), [rfReplaceAll]);
|
||||||
Value := stringreplace(Value, '%filepath%', ExtractFilePath(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, '%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]);
|
Value := stringreplace(Value, '%filetime%', Format('%.2d:%.2d:%.2d', [h,mi,s]), [rfReplaceAll]);
|
||||||
end;
|
end;
|
||||||
if cols[j].Quote then
|
if Caller.cols[j].Quote then
|
||||||
Value := esc(Value);
|
Value := esc(Value);
|
||||||
sql := sql + Value + ', ';
|
sql := sql + Value + ', ';
|
||||||
end;
|
end;
|
||||||
@ -136,17 +124,18 @@ begin
|
|||||||
sql := copy(sql, 1, length(sql)-2);
|
sql := copy(sql, 1, length(sql)-2);
|
||||||
sql := sql + ')';
|
sql := sql + ')';
|
||||||
Mainform.Connection.Query(sql);
|
Mainform.Connection.Query(sql);
|
||||||
lblOperation.caption := 'Freeing memory ...';
|
|
||||||
lblOperation.Repaint;
|
lblOperation.Repaint;
|
||||||
ProgressBar1.StepIt;
|
pbReadingFiles.StepIt;
|
||||||
ProgressBar1.Repaint;
|
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;
|
end;
|
||||||
|
|
||||||
FINALLY
|
|
||||||
screen.Cursor := crDefault;
|
|
||||||
Close();
|
|
||||||
END;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TfrmInsertFilesProgress.FormClose(Sender: TObject;
|
procedure TfrmInsertFilesProgress.FormClose(Sender: TObject;
|
||||||
@ -162,12 +151,12 @@ end;
|
|||||||
|
|
||||||
procedure TfrmInsertFilesProgress.FormShow(Sender: TObject);
|
procedure TfrmInsertFilesProgress.FormShow(Sender: TObject);
|
||||||
begin
|
begin
|
||||||
ProgressBar1.Position := 0;
|
pbReadingFiles.Position := 0;
|
||||||
lblNumber.Caption := '';
|
lblNumber.Caption := '';
|
||||||
lblFilename.Caption := '';
|
lblFilename.Caption := '';
|
||||||
lblOperation.Caption := '';
|
lblOperation.Caption := '';
|
||||||
Canceled := false;
|
Canceled := false;
|
||||||
Timer1.Enabled := true;
|
timerStartReading.Enabled := true;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ uses
|
|||||||
createdatabase, table_editor, SynRegExpr,
|
createdatabase, table_editor, SynRegExpr,
|
||||||
WideStrUtils, ExtActns, CommCtrl, routine_editor, options,
|
WideStrUtils, ExtActns, CommCtrl, routine_editor, options,
|
||||||
Contnrs, PngSpeedButton, connections, SynEditKeyCmds,
|
Contnrs, PngSpeedButton, connections, SynEditKeyCmds,
|
||||||
mysql_connection, mysql_api;
|
mysql_connection, mysql_api, insertfiles;
|
||||||
|
|
||||||
|
|
||||||
type
|
type
|
||||||
@ -791,6 +791,7 @@ type
|
|||||||
prefNullBG : TColor;
|
prefNullBG : TColor;
|
||||||
CreateDatabaseForm : TCreateDatabaseForm;
|
CreateDatabaseForm : TCreateDatabaseForm;
|
||||||
TableEditor : TfrmTableEditor;
|
TableEditor : TfrmTableEditor;
|
||||||
|
InsertFiles : TfrmInsertFiles;
|
||||||
FDataGridSelect : TWideStringList;
|
FDataGridSelect : TWideStringList;
|
||||||
FDataGridSort : TOrderColArray;
|
FDataGridSort : TOrderColArray;
|
||||||
DataGridCurrentSelect,
|
DataGridCurrentSelect,
|
||||||
@ -899,7 +900,7 @@ const
|
|||||||
implementation
|
implementation
|
||||||
|
|
||||||
uses
|
uses
|
||||||
About, loaddata, printlist, copytable, insertfiles,
|
About, loaddata, printlist, copytable,
|
||||||
mysql_structures, UpdateCheck, uVistaFuncs, runsqlfile, column_selection,
|
mysql_structures, UpdateCheck, uVistaFuncs, runsqlfile, column_selection,
|
||||||
data_sorting, grideditlinks, dataviewsave;
|
data_sorting, grideditlinks, dataviewsave;
|
||||||
|
|
||||||
@ -2164,7 +2165,9 @@ end;
|
|||||||
|
|
||||||
procedure TMainForm.actInsertFilesExecute(Sender: TObject);
|
procedure TMainForm.actInsertFilesExecute(Sender: TObject);
|
||||||
begin
|
begin
|
||||||
InsertFilesWindow(Self);
|
if not Assigned(InsertFiles) then
|
||||||
|
InsertFiles := TfrmInsertFiles.Create(Self);
|
||||||
|
InsertFiles.ShowModal;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// Drop Table(s)
|
// Drop Table(s)
|
||||||
|
Reference in New Issue
Block a user