mirror of
https://github.com/HeidiSQL/HeidiSQL.git
synced 2025-08-26 20:50:20 +08:00
Reorganize code in table tools so the code paths get slightly simpler:
* Move code from ProcessTableNode() into Execute() * Consistently use one procedure per maintenance and find mode like it's already for export mode
This commit is contained in:
@ -94,11 +94,12 @@ type
|
|||||||
ExportStream: TStream;
|
ExportStream: TStream;
|
||||||
ExportLastDatabaseInFileMode: Widestring;
|
ExportLastDatabaseInFileMode: Widestring;
|
||||||
procedure SetToolMode(Value: TToolMode);
|
procedure SetToolMode(Value: TToolMode);
|
||||||
procedure ProcessTableNode(Sender: TObject; Node: PVirtualNode);
|
|
||||||
procedure AddResults(SQL: WideString);
|
procedure AddResults(SQL: WideString);
|
||||||
procedure AddNotes(Col1, Col2, Col3, Col4: WideString);
|
procedure AddNotes(Col1, Col2, Col3, Col4: WideString);
|
||||||
procedure UpdateResultGrid;
|
procedure UpdateResultGrid;
|
||||||
procedure DoExportObject(db, obj: WideString; NodeType: TListNodeType; RowsInTable, AvgRowLen: Int64);
|
procedure DoMaintenance(db, obj: WideString; NodeType: TListNodeType);
|
||||||
|
procedure DoFind(db, obj: WideString; NodeType: TListNodeType; RowsInTable: Int64);
|
||||||
|
procedure DoExport(db, obj: WideString; NodeType: TListNodeType; RowsInTable, AvgRowLen: Int64);
|
||||||
public
|
public
|
||||||
{ Public declarations }
|
{ Public declarations }
|
||||||
SelectedTables: TWideStringList;
|
SelectedTables: TWideStringList;
|
||||||
@ -321,6 +322,10 @@ end;
|
|||||||
procedure TfrmTableTools.Execute(Sender: TObject);
|
procedure TfrmTableTools.Execute(Sender: TObject);
|
||||||
var
|
var
|
||||||
DBNode, TableNode: PVirtualNode;
|
DBNode, TableNode: PVirtualNode;
|
||||||
|
Results: TMySQLQuery;
|
||||||
|
NodeType: TListNodeType;
|
||||||
|
db, table: WideString;
|
||||||
|
TableSize, RowsInTable, AvgRowLen: Int64;
|
||||||
begin
|
begin
|
||||||
Screen.Cursor := crHourGlass;
|
Screen.Cursor := crHourGlass;
|
||||||
if tabsTools.ActivePage = tabMaintenance then
|
if tabsTools.ActivePage = tabMaintenance then
|
||||||
@ -337,7 +342,31 @@ begin
|
|||||||
if not (DBNode.CheckState in [csUncheckedNormal, csUncheckedPressed]) then begin
|
if not (DBNode.CheckState in [csUncheckedNormal, csUncheckedPressed]) then begin
|
||||||
TableNode := TreeObjects.GetFirstChild(DBNode);
|
TableNode := TreeObjects.GetFirstChild(DBNode);
|
||||||
while Assigned(TableNode) do begin
|
while Assigned(TableNode) do begin
|
||||||
ProcessTableNode(Sender, TableNode);
|
if (csCheckedNormal in [TableNode.CheckState, DBNode.CheckState]) and (TableNode.CheckType <> ctNone) then begin
|
||||||
|
Results := Mainform.FetchDbTableList(TreeObjects.Text[DBNode, 0]);
|
||||||
|
Results.RecNo := TableNode.Index;
|
||||||
|
NodeType := GetDBObjectType(Results);
|
||||||
|
db := TreeObjects.Text[DBNode, 0];
|
||||||
|
table := TreeObjects.Text[TableNode, 0];
|
||||||
|
// Find table in cashed dataset and check its size - perhaps it has to be skipped
|
||||||
|
TableSize := GetTableSize(Results);
|
||||||
|
RowsInTable := MakeInt(Results.Col(DBO_ROWS));
|
||||||
|
AvgRowLen := MakeInt(Results.Col(DBO_AVGROWLEN));
|
||||||
|
if (udSkipLargeTables.Position = 0) or ((TableSize div SIZE_MB) < udSkipLargeTables.Position) then try
|
||||||
|
case FToolMode of
|
||||||
|
tmMaintenance: DoMaintenance(db, table, NodeType);
|
||||||
|
tmFind: DoFind(db, table, NodeType, RowsInTable);
|
||||||
|
tmSQLExport: DoExport(db, table, NodeType, RowsInTable, AvgRowLen);
|
||||||
|
end;
|
||||||
|
except
|
||||||
|
// The above SQL can easily throw an exception, e.g. if a table is corrupted.
|
||||||
|
// In such cases we create a dummy row, including the error message
|
||||||
|
on E:Exception do
|
||||||
|
AddNotes(db, table, 'error', E.Message);
|
||||||
|
end else begin
|
||||||
|
AddNotes(db, table, STRSKIPPED+FormatByteNumber(TableSize), '');
|
||||||
|
end;
|
||||||
|
end;
|
||||||
TableNode := TreeObjects.GetNextSibling(TableNode);
|
TableNode := TreeObjects.GetNextSibling(TableNode);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
@ -353,90 +382,50 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
procedure TfrmTableTools.ProcessTableNode(Sender: TObject; Node: PVirtualNode);
|
procedure TfrmTableTools.DoMaintenance(db, obj: WideString; NodeType: TListNodeType);
|
||||||
var
|
var
|
||||||
SQL, db, table, QuotedTable: WideString;
|
SQL: WideString;
|
||||||
TableSize, RowsInTable, AvgRowLen: Int64;
|
|
||||||
Results: TMySQLQuery;
|
|
||||||
i: Integer;
|
|
||||||
HasSelectedDatatype: Boolean;
|
|
||||||
NodeType: TListNodeType;
|
|
||||||
begin
|
begin
|
||||||
// Prepare SQL for one table node
|
SQL := UpperCase(comboOperation.Text) + ' TABLE ' + Mainform.mask(db) + '.' + Mainform.mask(obj);
|
||||||
if (csCheckedNormal in [Node.CheckState, Node.Parent.CheckState]) and (Node.CheckType <> ctNone) then begin
|
if chkQuick.Enabled and chkQuick.Checked then SQL := SQL + ' QUICK';
|
||||||
db := TreeObjects.Text[Node.Parent, 0];
|
if chkFast.Enabled and chkFast.Checked then SQL := SQL + ' FAST';
|
||||||
table := TreeObjects.Text[Node, 0];
|
if chkMedium.Enabled and chkMedium.Checked then SQL := SQL + ' MEDIUM';
|
||||||
QuotedTable := Mainform.mask(db)+'.'+Mainform.mask(table);
|
if chkExtended.Enabled and chkExtended.Checked then SQL := SQL + ' EXTENDED';
|
||||||
// Find table in cashed dataset and check its size - perhaps it has to be skipped
|
if chkChanged.Enabled and chkChanged.Checked then SQL := SQL + ' CHANGED';
|
||||||
TableSize := 0;
|
if chkUseFrm.Enabled and chkUseFrm.Checked then SQL := SQL + ' USE_FRM';
|
||||||
RowsInTable := 0;
|
AddResults(SQL);
|
||||||
AvgRowLen := 0;
|
end;
|
||||||
Results := Mainform.FetchDbTableList(db);
|
|
||||||
Results.RecNo := Node.Index;
|
|
||||||
NodeType := GetDBObjectType(Results);
|
procedure TfrmTableTools.DoFind(db, obj: WideString; NodeType: TListNodeType; RowsInTable: Int64);
|
||||||
if NodeType in [lntTable, lntCrashedTable] then begin
|
var
|
||||||
TableSize := GetTableSize(Results);
|
Results: TMySQLQuery;
|
||||||
RowsInTable := MakeInt(Results.Col(DBO_ROWS));
|
SQL: WideString;
|
||||||
AvgRowLen := MakeInt(Results.Col(DBO_AVGROWLEN));
|
HasSelectedDatatype: Boolean;
|
||||||
end;
|
i: Integer;
|
||||||
if (udSkipLargeTables.Position = 0) or ((TableSize div SIZE_MB) < udSkipLargeTables.Position) then try
|
begin
|
||||||
|
Results := Mainform.Connection.GetResults('SHOW COLUMNS FROM '+Mainform.mask(db)+'.'+Mainform.mask(obj));
|
||||||
case FToolMode of
|
SQL := '';
|
||||||
tmMaintenance: begin
|
while not Results.Eof do begin
|
||||||
SQL := UpperCase(comboOperation.Text) + ' TABLE ' + QuotedTable;
|
HasSelectedDatatype := comboDatatypes.ItemIndex = 0;
|
||||||
if chkQuick.Enabled and chkQuick.Checked then SQL := SQL + ' QUICK';
|
if not HasSelectedDatatype then for i:=Low(Datatypes) to High(Datatypes) do begin
|
||||||
if chkFast.Enabled and chkFast.Checked then SQL := SQL + ' FAST';
|
HasSelectedDatatype := (LowerCase(getFirstWord(Results.Col('Type'))) = LowerCase(Datatypes[i].Name))
|
||||||
if chkMedium.Enabled and chkMedium.Checked then SQL := SQL + ' MEDIUM';
|
and (Integer(Datatypes[i].Category)+1 = comboDatatypes.ItemIndex);
|
||||||
if chkExtended.Enabled and chkExtended.Checked then SQL := SQL + ' EXTENDED';
|
if HasSelectedDatatype then
|
||||||
if chkChanged.Enabled and chkChanged.Checked then SQL := SQL + ' CHANGED';
|
break;
|
||||||
if chkUseFrm.Enabled and chkUseFrm.Checked then SQL := SQL + ' USE_FRM';
|
|
||||||
AddResults(SQL);
|
|
||||||
end;
|
|
||||||
|
|
||||||
tmFind: begin
|
|
||||||
Results := Mainform.Connection.GetResults('SHOW COLUMNS FROM '+QuotedTable);
|
|
||||||
SQL := '';
|
|
||||||
while not Results.Eof do begin
|
|
||||||
HasSelectedDatatype := comboDatatypes.ItemIndex = 0;
|
|
||||||
if not HasSelectedDatatype then for i:=Low(Datatypes) to High(Datatypes) do begin
|
|
||||||
HasSelectedDatatype := (LowerCase(getFirstWord(Results.Col('Type'))) = LowerCase(Datatypes[i].Name))
|
|
||||||
and (Integer(Datatypes[i].Category)+1 = comboDatatypes.ItemIndex);
|
|
||||||
if HasSelectedDatatype then
|
|
||||||
break;
|
|
||||||
end;
|
|
||||||
if HasSelectedDatatype then
|
|
||||||
SQL := SQL + Mainform.mask(Results.Col('Field')) + ' LIKE ' + esc('%'+memoFindText.Text+'%') + ' OR ';
|
|
||||||
Results.Next;
|
|
||||||
end;
|
|
||||||
if SQL <> '' then begin
|
|
||||||
Delete(SQL, Length(SQL)-3, 3);
|
|
||||||
SQL := 'SELECT '''+db+''' AS `Database`, '''+table+''' AS `Table`, COUNT(*) AS `Found rows`, '
|
|
||||||
+ 'CONCAT(ROUND(100 / '+IntToStr(Max(RowsInTable,1))+' * COUNT(*), 1), ''%'') AS `Relevance` FROM '+QuotedTable+' WHERE '
|
|
||||||
+ SQL;
|
|
||||||
AddResults(SQL);
|
|
||||||
end else
|
|
||||||
AddNotes(db, table, STRSKIPPED+'table doesn''t have columns of selected type ('+comboDatatypes.Text+').', '');
|
|
||||||
end;
|
|
||||||
|
|
||||||
tmSQLExport: begin
|
|
||||||
AddResults('SELECT '+esc(db)+' AS '+Mainform.mask('Database')+', ' +
|
|
||||||
esc(table)+' AS '+Mainform.mask('Table')+', ' +
|
|
||||||
IntToStr(RowsInTable)+' AS '+Mainform.mask('Rows')+', '+
|
|
||||||
'0 AS '+Mainform.mask('Duration')
|
|
||||||
);
|
|
||||||
DoExportObject(db, table, NodeType, RowsInTable, AvgRowLen);
|
|
||||||
end;
|
|
||||||
|
|
||||||
end;
|
|
||||||
except
|
|
||||||
// The above SQL can easily throw an exception, e.g. if a table is corrupted.
|
|
||||||
// In such cases we create a dummy row, including the error message
|
|
||||||
on E:Exception do
|
|
||||||
AddNotes(db, table, 'error', E.Message);
|
|
||||||
end else begin
|
|
||||||
AddNotes(db, table, STRSKIPPED+FormatByteNumber(TableSize), '');
|
|
||||||
end;
|
end;
|
||||||
|
if HasSelectedDatatype then
|
||||||
|
SQL := SQL + Mainform.mask(Results.Col('Field')) + ' LIKE ' + esc('%'+memoFindText.Text+'%') + ' OR ';
|
||||||
|
Results.Next;
|
||||||
end;
|
end;
|
||||||
|
if SQL <> '' then begin
|
||||||
|
Delete(SQL, Length(SQL)-3, 3);
|
||||||
|
SQL := 'SELECT '''+db+''' AS `Database`, '''+obj+''' AS `Table`, COUNT(*) AS `Found rows`, '
|
||||||
|
+ 'CONCAT(ROUND(100 / '+IntToStr(Max(RowsInTable,1))+' * COUNT(*), 1), ''%'') AS `Relevance` FROM '+Mainform.mask(db)+'.'+Mainform.mask(obj)+' WHERE '
|
||||||
|
+ SQL;
|
||||||
|
AddResults(SQL);
|
||||||
|
end else
|
||||||
|
AddNotes(db, obj, STRSKIPPED+'table doesn''t have columns of selected type ('+comboDatatypes.Text+').', '');
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
@ -689,7 +678,7 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
procedure TfrmTableTools.DoExportObject(db, obj: WideString; NodeType: TListNodeType; RowsInTable, AvgRowLen: Int64);
|
procedure TfrmTableTools.DoExport(db, obj: WideString; NodeType: TListNodeType; RowsInTable, AvgRowLen: Int64);
|
||||||
var
|
var
|
||||||
ToFile, ToDir, ToDb, IsLastRowInChunk, NeedsDBStructure: Boolean;
|
ToFile, ToDir, ToDb, IsLastRowInChunk, NeedsDBStructure: Boolean;
|
||||||
Struc, Header, BaseInsert, Row, TargetDbAndObject, objtype: WideString;
|
Struc, Header, BaseInsert, Row, TargetDbAndObject, objtype: WideString;
|
||||||
@ -733,6 +722,11 @@ var
|
|||||||
end;
|
end;
|
||||||
begin
|
begin
|
||||||
// Handle one table, view or routine in SQL export mode
|
// Handle one table, view or routine in SQL export mode
|
||||||
|
AddResults('SELECT '+esc(db)+' AS '+Mainform.mask('Database')+', ' +
|
||||||
|
esc(obj)+' AS '+Mainform.mask('Table')+', ' +
|
||||||
|
IntToStr(RowsInTable)+' AS '+Mainform.mask('Rows')+', '+
|
||||||
|
'0 AS '+Mainform.mask('Duration')
|
||||||
|
);
|
||||||
ToFile := comboExportOutputType.Text = OUTPUT_FILE;
|
ToFile := comboExportOutputType.Text = OUTPUT_FILE;
|
||||||
ToDir := comboExportOutputType.Text = OUTPUT_DIR;
|
ToDir := comboExportOutputType.Text = OUTPUT_DIR;
|
||||||
ToDb := comboExportOutputType.Text = OUTPUT_DB;
|
ToDb := comboExportOutputType.Text = OUTPUT_DB;
|
||||||
|
Reference in New Issue
Block a user