Move detection of best table name to helpers unit, away from several methods where we make use of it. Fixes issue #1967.

This commit is contained in:
Ansgar Becker
2010-05-27 18:11:51 +00:00
parent 823269e2ca
commit 56ce17181f
2 changed files with 31 additions and 33 deletions

View File

@ -90,10 +90,11 @@ type
function encrypt(str: String): String;
function decrypt(str: String): String;
function htmlentities(str: String): String;
procedure GridToHtml(Grid: TVirtualStringTree; Title: String; S: TStream);
procedure GridToHtml(Grid: TVirtualStringTree; S: TStream);
procedure GridToCsv(Grid: TVirtualStringTree; Separator, Encloser, Terminator: String; S: TStream);
procedure GridToXml(Grid: TVirtualStringTree; root: String; S: TStream);
procedure GridToSql(Grid: TVirtualStringTree; Tablename: String; S: TStream);
procedure GridToXml(Grid: TVirtualStringTree; S: TStream);
procedure GridToSql(Grid: TVirtualStringTree; S: TStream);
function BestTableName(Data: TMySQLQuery): String;
function esc2ascii(str: String): String;
function urlencode(url: String): String;
procedure StreamWrite(S: TStream; Text: String = '');
@ -557,10 +558,10 @@ end;
@param Grid Object which holds data to export
@param string Text used in <title>
}
procedure GridToHtml(Grid: TVirtualStringTree; Title: String; S: TStream);
procedure GridToHtml(Grid: TVirtualStringTree; S: TStream);
var
i, MaxSize: Integer;
tmp, Data, Generator: String;
tmp, Data, Generator, Title: String;
Node: PVirtualNode;
GridData: TMySQLQuery;
SelectionOnly: Boolean;
@ -572,6 +573,7 @@ begin
Mainform.DataGridEnsureFullRows(Grid, SelectionOnly);
GridData := Mainform.GridResult(Grid);
Title := BestTableName(GridData);
MaxSize := GetRegValue(REGNAME_COPYMAXSIZE, DEFAULT_COPYMAXSIZE) * SIZE_MB;
@ -779,10 +781,10 @@ end;
@param Grid Object which holds data to export
@param string Text used as root-element
}
procedure GridToXml(Grid: TVirtualStringTree; root: String; S: TStream);
procedure GridToXml(Grid: TVirtualStringTree; S: TStream);
var
i, MaxSize: Integer;
tmp, Data: String;
tmp, Data, root: String;
Node: PVirtualNode;
GridData: TMySQLQuery;
SelectionOnly: Boolean;
@ -794,6 +796,7 @@ begin
Mainform.DataGridEnsureFullRows(Grid, SelectionOnly);
GridData := Mainform.GridResult(Grid);
root := BestTableName(GridData);
MaxSize := GetRegValue(REGNAME_COPYMAXSIZE, DEFAULT_COPYMAXSIZE) * SIZE_MB;
@ -860,10 +863,10 @@ end;
@param Grid Object which holds data to export
@param string Text used as tablename in INSERTs
}
procedure GridToSql(Grid: TVirtualStringTree; Tablename: String; S: TStream);
procedure GridToSql(Grid: TVirtualStringTree; S: TStream);
var
i, MaxSize: Integer;
tmp, Data: String;
tmp, Data, TableName: String;
Node: PVirtualNode;
GridData: TMySQLQuery;
SelectionOnly: Boolean;
@ -875,6 +878,7 @@ begin
Mainform.DataGridEnsureFullRows(Grid, SelectionOnly);
GridData := Mainform.GridResult(Grid);
TableName := BestTableName(GridData);
MaxSize := GetRegValue(REGNAME_COPYMAXSIZE, DEFAULT_COPYMAXSIZE) * SIZE_MB;
@ -937,6 +941,17 @@ begin
end;
function BestTableName(Data: TMySQLQuery): String;
begin
// Get table name from result if possible. Used by GridToXYZ() functions.
try
Result := Data.TableName;
except
Result := 'UnknownTable';
end;
end;
{***
Return ASCII-Values from MySQL-Escape-Sequences

View File

@ -2223,15 +2223,12 @@ end;
procedure TMainForm.actCopyAsHTMLExecute(Sender: TObject);
var
S: TMemoryStream;
Title: String;
begin
// Copy data in focused grid as HTML table
Screen.Cursor := crHourglass;
S := TMemoryStream.Create;
if ActiveGrid = DataGrid then Title := SelectedTable.Name
else Title := 'SQL query';
try
GridToHtml(ActiveGrid, Title, S);
GridToHtml(ActiveGrid, S);
StreamToClipboard(S, S, True);
finally
ShowStatusMsg('Freeing data...');
@ -2245,15 +2242,12 @@ end;
procedure TMainForm.actCopyAsXMLExecute(Sender: TObject);
var
S: TMemoryStream;
Root: String;
begin
// Copy data in focused grid as XML
Screen.Cursor := crHourglass;
S := TMemoryStream.Create;
if ActiveGrid = DataGrid then Root := SelectedTable.Name
else Root := 'SQL query';
try
GridToXml(ActiveGrid, Root, S);
GridToXml(ActiveGrid, S);
StreamToClipboard(S, nil, False);
finally
ShowStatusMsg('Freeing data...');
@ -2267,16 +2261,13 @@ end;
procedure TMainForm.actCopyAsSQLExecute(Sender: TObject);
var
S, HTML: TMemoryStream;
Tablename: String;
Content: AnsiString;
begin
// Copy data in focused grid as SQL
Screen.Cursor := crHourglass;
S := TMemoryStream.Create;
if ActiveGrid = DataGrid then Tablename := SelectedTable.Name
else Tablename := 'unknown';
try
GridToSql(ActiveGrid, Tablename, S);
GridToSql(ActiveGrid, S);
SetLength(Content, S.Size);
S.Position := 0;
S.Read(PAnsiChar(Content)^, S.Size);
@ -2297,27 +2288,19 @@ procedure TMainForm.actExportDataExecute(Sender: TObject);
var
Dialog: TSaveDialog;
FS: TFileStream;
Title: String;
begin
// Save data in current dataset as CSV, HTML or XML
Dialog := SaveDialogExportData;
if ActiveGrid = DataGrid then
Title := SelectedTable.Name
else
Title := 'SQL query';
Dialog.FileName := Title;
Dialog.FileName := BestTableName(GridResult(ActiveGrid));
Dialog.Title := 'Export result set from '+Dialog.Filename+'...';
if Dialog.Execute and (Dialog.FileName <> '') then try
Screen.Cursor := crHourGlass;
FS := TFileStream.Create(Dialog.FileName, fmCreate or fmOpenWrite);
case Dialog.FilterIndex of
1: GridToCsv(ActiveGrid, prefCSVSeparator, prefCSVEncloser, prefCSVTerminator, FS);
2: GridToHtml(ActiveGrid, Title, FS);
3: GridToXml(ActiveGrid, Title, FS);
4: GridToSql(ActiveGrid, Title, FS);
2: GridToHtml(ActiveGrid, FS);
3: GridToXml(ActiveGrid, FS);
4: GridToSql(ActiveGrid, FS);
end;
ShowStatusMsg('Freeing data...');
FS.Free;