Skip to content

Commit 5560454

Browse files
committed
feat: create SQL export option for wrapping DML commands in a BEGIN/COMMIT transaction
This also * removes the MyISAM-only ALTER TABLE .. DISABLE/ENABLE KEYS command, which in mid size tables costs more than it helps. * shows the number of checked export options on the button caption * saves settings when the user just closes the dialog, without having exported Closes #1262
1 parent 146044f commit 5560454

3 files changed

Lines changed: 36 additions & 10 deletions

File tree

source/apphelpers.pas

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ TWinControlHelper = class helper for TWinControl
191191
asSSLCert, asSSLCA, asSSLCipher, asSSLVerification, asSSLWarnUnused, asNetType, asCompressed, asLocalTimeZone, asQueryTimeout, asKeepAlive,
192192
asStartupScriptFilename, asDatabases, asComment, asDatabaseFilter, asTableFilter, asFilterVT, asExportSQLCreateDatabases,
193193
asExportSQLCreateTables, asExportSQLDataHow, asExportSQLDataInsertSize, asExportSQLFilenames, asExportZIPFilenames, asExportSQLDirectories,
194-
asExportSQLDatabase, asExportSQLServerDatabase, asExportSQLOutput, asExportSQLAddComments, asExportSQLRemoveAutoIncrement, asExportSQLRemoveDefiner,
194+
asExportSQLDatabase, asExportSQLServerDatabase, asExportSQLOutput, asExportSQLAddComments, asExportSQLTransactions, asExportSQLRemoveAutoIncrement, asExportSQLRemoveDefiner,
195195
asGridExportWindowWidth, asGridExportWindowHeight, asGridExportOutputCopy, asGridExportOutputFile,
196196
asGridExportFilename, asGridExportRecentFiles, asGridExportEncoding, asGridExportFormat, asGridExportSelection,
197197
asGridExportColumnNames, asGridExportIncludeAutoInc, asGridExportIncludeQuery, asGridExportRemoveLinebreaks, asGridExportOpenFile,
@@ -3682,6 +3682,7 @@ constructor TAppSettings.Create;
36823682
InitSetting(asExportSQLServerDatabase, 'ExportSQL_ServerDatabase', 0, False, '');
36833683
InitSetting(asExportSQLOutput, 'ExportSQL_Output', 0);
36843684
InitSetting(asExportSQLAddComments, 'ExportSQLAddComments', 0, True);
3685+
InitSetting(asExportSQLTransactions, 'ExportSQLTransactions', 0, False);
36853686
InitSetting(asExportSQLRemoveAutoIncrement, 'ExportSQLRemoveAutoIncrement', 0, False);
36863687
InitSetting(asExportSQLRemoveDefiner, 'ExportSQLRemoveDefiner', 0, True);
36873688
InitSetting(asGridExportWindowWidth, 'GridExportWindowWidth', 450);

source/tabletools.lfm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,14 +1061,22 @@ object frmTableTools: TfrmTableTools
10611061
object menuExportAddComments: TMenuItem
10621062
AutoCheck = True
10631063
Caption = 'Add comments'
1064+
OnClick = menuExportOptionClick
1065+
end
1066+
object menuExportTransactions: TMenuItem
1067+
AutoCheck = True
1068+
Caption = 'Wrap data DML in transactions'
1069+
OnClick = menuExportOptionClick
10641070
end
10651071
object menuExportRemoveAutoIncrement: TMenuItem
10661072
AutoCheck = True
10671073
Caption = 'Remove AUTO_INCREMENT clauses'
1074+
OnClick = menuExportOptionClick
10681075
end
10691076
object menuExportRemoveDefiner: TMenuItem
10701077
AutoCheck = True
10711078
Caption = 'Remove DEFINER clauses'
1079+
OnClick = menuExportOptionClick
10721080
end
10731081
object menuCopyMysqldumpCommand: TMenuItem
10741082
Caption = 'Copy mysqldump command'

source/tabletools.pas

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ TfrmTableTools = class(TExtForm)
103103
lblGenerateDataNullAmount: TLabel;
104104
editGenerateDataNullAmount: TEdit;
105105
menuInvertCheck: TMenuItem;
106+
menuExportTransactions: TMenuItem;
106107
procedure FormCreate(Sender: TObject);
107108
procedure FormShow(Sender: TObject);
108109
procedure btnHelpMaintenanceClick(Sender: TObject);
@@ -128,7 +129,7 @@ TfrmTableTools = class(TExtForm)
128129
procedure ResultGridPaintText(Sender: TBaseVirtualTree; const TargetCanvas: TCanvas; Node: PVirtualNode;
129130
Column: TColumnIndex; TextType: TVSTTextType);
130131
procedure ValidateControls(Sender: TObject);
131-
procedure SaveSettings(Sender: TObject);
132+
procedure SaveSettings;
132133
procedure chkExportOptionClick(Sender: TObject);
133134
procedure btnExportOutputTargetSelectClick(Sender: TObject);
134135
procedure comboExportOutputTargetChange(Sender: TObject);
@@ -159,6 +160,7 @@ TfrmTableTools = class(TExtForm)
159160
Index: Integer; ARect: TRect; State: TOwnerDrawState);
160161
procedure comboExportOutputTypeMeasureItem(Control: TWinControl;
161162
Index: Integer; var AHeight: Integer);
163+
procedure menuExportOptionClick(Sender: TObject);
162164
const
163165
StatusMsg = '%s %s ...';
164166
private
@@ -270,6 +272,7 @@ procedure TfrmTableTools.FormCreate(Sender: TObject);
270272
comboExportData.ItemIndex := AppSettings.ReadInt(asExportSQLDataHow);
271273
editInsertSize.Text := AppSettings.ReadInt(asExportSQLDataInsertSize).ToString;
272274
menuExportAddComments.Checked := AppSettings.ReadBool(asExportSQLAddComments);
275+
menuExportTransactions.Checked := AppSettings.ReadBool(asExportSQLTransactions);
273276
menuExportRemoveAutoIncrement.Checked := AppSettings.ReadBool(asExportSQLRemoveAutoIncrement);
274277
menuExportRemoveDefiner.Checked := AppSettings.ReadBool(asExportSQLRemoveDefiner);
275278
// Add hardcoded output options and session names from registry
@@ -514,19 +517,34 @@ procedure TfrmTableTools.menuCopyMysqldumpCommandClick(Sender: TObject);
514517
end;
515518

516519

520+
procedure TfrmTableTools.menuExportOptionClick(Sender: TObject);
521+
var
522+
i: Integer;
523+
MenuItem: TMenuItem;
524+
begin
525+
// Display number of checked options in button caption
526+
i := 0;
527+
for MenuItem in popupExportOptions.Items do begin
528+
if MenuItem.Checked then
529+
Inc(i);
530+
end;
531+
btnExportOptions.Caption := _('Options') + ' (' + i.ToString + ')';
532+
end;
533+
517534
procedure TfrmTableTools.FormClose(Sender: TObject; var Action: TCloseAction);
518535
begin
519536
// Auto close temorary connection
520537
if Assigned(FTargetConnection) then
521538
FreeAndNil(FTargetConnection);
539+
SaveSettings;
522540
// Save GUI setup
523541
AppSettings.WriteInt(asTableToolsWindowWidth, Width);
524542
AppSettings.WriteInt(asTableToolsWindowHeight, Height);
525543
AppSettings.WriteInt(asTableToolsTreeWidth, pnlLeft.Width);
526544
end;
527545

528546

529-
procedure TfrmTableTools.SaveSettings(Sender: TObject);
547+
procedure TfrmTableTools.SaveSettings;
530548
var
531549
i: Integer;
532550
Items: TStringList;
@@ -548,6 +566,7 @@ procedure TfrmTableTools.SaveSettings(Sender: TObject);
548566
if comboExportData.ItemIndex > 0 then
549567
AppSettings.WriteInt(asExportSQLDataInsertSize, StrToInt64Def(editInsertSize.Text, 0));
550568
AppSettings.WriteBool(asExportSQLAddComments, menuExportAddComments.Checked);
569+
AppSettings.WriteBool(asExportSQLTransactions, menuExportTransactions.Checked);
551570
AppSettings.WriteBool(asExportSQLRemoveAutoIncrement, menuExportRemoveAutoIncrement.Checked);
552571
AppSettings.WriteBool(asExportSQLRemoveDefiner, menuExportRemoveDefiner.Checked);
553572

@@ -604,6 +623,7 @@ procedure TfrmTableTools.ValidateControls(Sender: TObject);
604623
TExtForm.PageControlTabHighlight(tabsTools);
605624
btnSeeResults.Visible := tabsTools.ActivePage = tabFind;
606625
lblCheckedSize.Caption := f_('Selected objects size: %s', [FormatByteNumber(FObjectSizes)]);
626+
menuExportOptionClick(Sender);
607627
if tabsTools.ActivePage = tabMaintenance then begin
608628
btnExecute.Caption := _('Execute');
609629
btnExecute.Enabled := (Pos(_(SUnsupported), comboOperation.Text) = 0) and SomeChecked;
@@ -1000,7 +1020,6 @@ procedure TfrmTableTools.Execute(Sender: TObject);
10001020
tabsTools.Enabled := True;
10011021
treeObjects.Enabled := True;
10021022
ValidateControls(Sender);
1003-
SaveSettings(Sender);
10041023
Screen.Cursor := crDefault;
10051024
end;
10061025

@@ -2013,6 +2032,8 @@ procedure TfrmTableTools.DoExport(DBObj: TDBObject);
20132032
tmp := '~'+tmp+' ('+_('approximately')+')';
20142033
if menuExportAddComments.Checked then
20152034
Output('-- '+f_('Dumping data for table %s.%s: %s', [DBObj.Database, DBObj.Name, tmp])+CRLF, False, True, True, False, False);
2035+
if menuExportTransactions.Checked then
2036+
Output('BEGIN', True, True, True, True, True);
20162037
TargetDbAndObject := Quoter.QuoteIdent(DBObj.Name);
20172038
if ToDb then
20182039
TargetDbAndObject := Quoter.QuoteIdent(FinalDbName) + '.' + TargetDbAndObject;
@@ -2035,9 +2056,6 @@ procedure TfrmTableTools.DoExport(DBObj: TDBObject);
20352056
Limit := Round(100 * SIZE_MB / IfThen(DBObj.AvgRowLen>0, DBObj.AvgRowLen, AssumedAvgRowLen));
20362057
if comboExportData.Text = DATA_REPLACE then
20372058
Output('DELETE FROM '+TargetDbAndObject, True, True, True, True, True);
2038-
if DBObj.Engine.ToLowerInvariant <> 'innodb' then begin
2039-
Output('/*!40000 ALTER TABLE '+TargetDbAndObject+' DISABLE KEYS */', True, True, True, True, True);
2040-
end;
20412059
while true do begin
20422060
Data := DBObj.Connection.GetResults(
20432061
DBObj.Connection.ApplyLimitClause(
@@ -2115,9 +2133,8 @@ procedure TfrmTableTools.DoExport(DBObj: TDBObject);
21152133
break;
21162134

21172135
end;
2118-
if DBObj.Engine.ToLowerInvariant <> 'innodb' then begin
2119-
Output('/*!40000 ALTER TABLE '+TargetDbAndObject+' ENABLE KEYS */', True, True, True, True, True);
2120-
end;
2136+
if menuExportTransactions.Checked then
2137+
Output('COMMIT', True, True, True, True, True);
21212138
Output(CRLF, False, True, True, True, True);
21222139
// Cosmetic fix for estimated InnoDB row count
21232140
DBObj.Rows := RowCount;

0 commit comments

Comments
 (0)