From 4793d275cd984ee56089e7aed4ab78a50e67c29c Mon Sep 17 00:00:00 2001 From: Ansgar Becker Date: Mon, 10 Dec 2007 21:02:06 +0000 Subject: [PATCH] Add feature #1843670 "Multi-table Automated File Exporting" The output filename gets parsed and the eventually existing "" pattern gets replaced by either "header", "table1", "table2", ..., or "footer". That means, the single SQL files won't be complete in the end, as the header and footer is missing in the table-files. Not 100% sure if this is a good strategy. For me it seemed like the best/fast/lame way of working around the fact that the header potentially contains a CREATE DATABASE statement. --- source/exportsql.dfm | 2 + source/exportsql.pas | 87 ++++++++++++++++++++++++++++++++++++++------ 2 files changed, 77 insertions(+), 12 deletions(-) diff --git a/source/exportsql.dfm b/source/exportsql.dfm index ce478b46..e604855f 100644 --- a/source/exportsql.dfm +++ b/source/exportsql.dfm @@ -177,6 +177,8 @@ object ExportSQLForm: TExportSQLForm Width = 304 Height = 21 Anchors = [akLeft, akTop, akRight] + ParentShowHint = False + ShowHint = True TabOrder = 1 OnDblClick = btnFileBrowseClick end diff --git a/source/exportsql.pas b/source/exportsql.pas index f31a794e..4c09016b 100644 --- a/source/exportsql.pas +++ b/source/exportsql.pas @@ -94,6 +94,7 @@ type procedure SaveSettings; private { Private declarations } + function InitFileStream(TableName: String; OldStream: TFileStream = nil): TFileStream; public { Public declarations } end; @@ -135,6 +136,9 @@ const // Default output compatibility SQL_VERSION_DEFAULT = SQL_VERSION_ANSI; + // Pattern for use in creating destination files + TABLENAME_PATTERN = '
'; + var appHandles: array of THandle; cancelDialog: TForm = nil; @@ -279,6 +283,9 @@ begin if EditFileName.Text = '' then EditFileName.Text := ExtractFilePath(paramstr(0)) + 'export.sql'; + // Tell the user how to use the table pattern + EditFileName.Hint := 'Usage for generating one file per table: c:\foo\bar_'+TABLENAME_PATTERN+'.sql'; + validateControls(Sender); generateExampleSQL; end; @@ -426,6 +433,50 @@ begin end; +{** + Parse destination filename for variables like %table% and create the file + If an existing filestream is passed, check if it should be reused, depending on the "Part" + @return TFileStream|Nil +} +function TExportSQLForm.InitFileStream(TableName: String; OldStream: TFileStream = nil): TFileStream; +var + ParsedFileName, FileName, FilePath : String; +begin + Result := nil; + + // Parse filename + FilePath := ExtractFilePath(EditFileName.Text); + FileName := ExtractFileName(EditFileName.Text); + FileName := StringReplace(FileName, TABLENAME_PATTERN, TableName, [rfIgnoreCase]); + ParsedFileName := FilePath + GoodFileName(FileName); + + // Reuse the old stream if its filename has not changed + if (OldStream <> nil) and (OldStream.FileName = ParsedFileName) then + begin + Result := OldStream; + Exit; + end; + + // Filename has changed, so close the old file. + if OldStream <> nil then + OldStream.Free; + + // Warn about overwriting target file + if FileExists(ParsedFileName) then begin + if MessageDlg('Overwrite file "'+ParsedFileName+'" ?', mtConfirmation, [mbYes, mbCancel], 0 ) <> mrYes then + Exit; + end; + + // Create the file + try + Result := TFileStream.Create(ParsedFileName, fmCreate); + except + MessageDlg('File "'+ParsedFileName+'" could not be opened!' + CRLF + 'Maybe in use by another application?', mterror, [mbOK], 0); + Result.Free; + end; +end; + + procedure TExportSQLForm.btnExportClick(Sender: TObject); var f : TFileStream; @@ -487,12 +538,6 @@ begin todb := radioOtherDatabase.Checked; tohost := radioOtherHost.Checked; - // Warn about overwriting target file - if tofile and FileExists(EditFileName.Text) then begin - if MessageDlg('Overwrite file "'+EditFileName.Text+'" ?', mtConfirmation, [mbYes, mbCancel], 0 ) <> mrYes then - Exit; - end; - // export! pageControl1.ActivePageIndex := 0; Screen.Cursor := crHourGlass; @@ -523,13 +568,11 @@ begin // Extract name part of selected target version target_version := StrToIntDef( target_versions.Names[ comboTargetCompat.ItemIndex ], SQL_VERSION_DEFAULT ); max_allowed_packet := MakeInt( cwin.GetVar( 'SHOW VARIABLES LIKE ' + esc('max_allowed_packet'), 1 ) ); - try - f := TFileStream.Create(EditFileName.Text, fmCreate); - except - messagedlg('File "'+EditFileName.Text+'" could not be opened!' + crlf + 'Maybe in use by another application?', mterror, [mbOK], 0); - f.free; + f := InitFileStream('header'); + if f = nil then + begin Screen.Cursor := crDefault; - abort; + Abort; end; wfs(f, '# ' + APPNAME + ' Dump '); wfs(f, '#'); @@ -725,6 +768,16 @@ begin if exporttables then begin + if tofile then + begin + f := InitFileStream(checkListTables.Items[i], f); + if f = nil then + begin + Screen.Cursor := crDefault; + Abort; + end; + end; + dropquery := ''; if comboTables.ItemIndex = TAB_DROP_CREATE then begin if tofile then @@ -1131,6 +1184,16 @@ begin end; end; + if tofile then + begin + f := InitFileStream('footer', f); + if f = nil then + begin + Screen.Cursor := crDefault; + Abort; + end; + end; + // Restore old value for SQL_MODE if (tofile or tohost) and (target_version = SQL_VERSION_ANSI) then begin