From be689edaec600b209922e93734c5fbd795cff69e Mon Sep 17 00:00:00 2001 From: Ansgar Becker Date: Wed, 13 Nov 2019 14:29:26 +0100 Subject: [PATCH] SQL export: add drop down menu item for removing DEFINER clauses from triggers, procedures and functions --- source/apphelpers.pas | 3 ++- source/dbconnection.pas | 33 ++++++++++++++++++++++++++++++++- source/tabletools.dfm | 4 ++++ source/tabletools.pas | 17 ++++++----------- 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/source/apphelpers.pas b/source/apphelpers.pas index b4cd3b29..71adfa92 100644 --- a/source/apphelpers.pas +++ b/source/apphelpers.pas @@ -154,7 +154,7 @@ type asSSLCert, asSSLCA, asSSLCipher, asNetType, asCompressed, asLocalTimeZone, asQueryTimeout, asKeepAlive, asStartupScriptFilename, asDatabases, asComment, asDatabaseFilter, asTableFilter, asExportSQLCreateDatabases, asExportSQLCreateTables, asExportSQLDataHow, asExportSQLDataInsertSize, asExportSQLFilenames, asExportZIPFilenames, asExportSQLDirectories, - asExportSQLDatabase, asExportSQLServerDatabase, asExportSQLOutput, asExportSQLAddComments, asExportSQLRemoveAutoIncrement, + asExportSQLDatabase, asExportSQLServerDatabase, asExportSQLOutput, asExportSQLAddComments, asExportSQLRemoveAutoIncrement, asExportSQLRemoveDefiner, asGridExportWindowWidth, asGridExportWindowHeight, asGridExportOutputCopy, asGridExportOutputFile, asGridExportFilename, asGridExportRecentFiles, asGridExportEncoding, asGridExportFormat, asGridExportSelection, asGridExportColumnNames, asGridExportIncludeAutoInc, asGridExportIncludeQuery, @@ -3555,6 +3555,7 @@ begin InitSetting(asExportSQLOutput, 'ExportSQL_Output', 0); InitSetting(asExportSQLAddComments, 'ExportSQLAddComments', 0, True); InitSetting(asExportSQLRemoveAutoIncrement, 'ExportSQLRemoveAutoIncrement', 0, False); + InitSetting(asExportSQLRemoveDefiner, 'ExportSQLRemoveDefiner', 0, True); InitSetting(asGridExportWindowWidth, 'GridExportWindowWidth', 400); InitSetting(asGridExportWindowHeight, 'GridExportWindowHeight', 460); InitSetting(asGridExportOutputCopy, 'GridExportOutputCopy', 0, True); diff --git a/source/dbconnection.pas b/source/dbconnection.pas index e427570a..d3e14265 100644 --- a/source/dbconnection.pas +++ b/source/dbconnection.pas @@ -29,7 +29,6 @@ type function GetImageIndex: Integer; function GetOverlayImageIndex: Integer; function GetPath: String; - function GetCreateCode: String; procedure SetCreateCode(Value: String); public // Table options: @@ -50,6 +49,8 @@ type function QuotedDbAndTableName(AlwaysQuote: Boolean=True): String; function QuotedColumn(AlwaysQuote: Boolean=True): String; function RowCount: Int64; + function GetCreateCode: String; overload; + function GetCreateCode(RemoveAutoInc, RemoveDefiner: Boolean): String; overload; property ObjType: String read GetObjType; property ImageIndex: Integer read GetImageIndex; property OverlayImageIndex: Integer read GetOverlayImageIndex; @@ -7542,6 +7543,36 @@ begin Result := FCreateCode; end; +function TDBObject.GetCreateCode(RemoveAutoInc, RemoveDefiner: Boolean): String; + + procedure RemovePattern(RegExp: String); + var + rx: TRegExpr; + begin + // Remove first occurrence of pattern from result + rx := TRegExpr.Create; + rx.Expression := RegExp; + rx.ModifierI := True; + if rx.Exec(Result) then begin + Delete(Result, rx.MatchPos[0], rx.MatchLen[0]-1); + end; + rx.Free; + end; +begin + Result := GetCreateCode; + + if RemoveAutoInc then begin + // Remove AUTO_INCREMENT clause + RemovePattern('\sAUTO_INCREMENT\s*\=\s*\d+\s'); + end; + + if RemoveDefiner then begin + // Remove DEFINER clause + RemovePattern('\sDEFINER\s*\=\s*\S+\s'); + end; + +end; + procedure TDBObject.SetCreateCode(Value: String); begin // When manually clearing CreateCode from outside, also reset indicator for fetch attempt diff --git a/source/tabletools.dfm b/source/tabletools.dfm index 9a638b0c..f59eb339 100644 --- a/source/tabletools.dfm +++ b/source/tabletools.dfm @@ -633,5 +633,9 @@ object frmTableTools: TfrmTableTools AutoCheck = True Caption = 'Remove AUTO_INCREMENT clauses' end + object menuExportRemoveDefiner: TMenuItem + AutoCheck = True + Caption = 'Remove DEFINER clauses' + end end end diff --git a/source/tabletools.pas b/source/tabletools.pas index e02a6ff5..c04bf9e2 100644 --- a/source/tabletools.pas +++ b/source/tabletools.pas @@ -82,6 +82,7 @@ type menuExportRemoveAutoIncrement: TMenuItem; comboMatchType: TComboBox; lblMatchType: TLabel; + menuExportRemoveDefiner: TMenuItem; procedure FormDestroy(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormShow(Sender: TObject); @@ -257,6 +258,7 @@ begin updownInsertSize.Position := AppSettings.ReadInt(asExportSQLDataInsertSize); menuExportAddComments.Checked := AppSettings.ReadBool(asExportSQLAddComments); menuExportRemoveAutoIncrement.Checked := AppSettings.ReadBool(asExportSQLRemoveAutoIncrement); + menuExportRemoveDefiner.Checked := AppSettings.ReadBool(asExportSQLRemoveDefiner); // Add hardcoded output options and session names from registry comboExportOutputType.Items.Text := OUTPUT_FILE + CRLF + @@ -402,6 +404,7 @@ begin AppSettings.WriteInt(asExportSQLDataInsertSize, updownInsertSize.Position); AppSettings.WriteBool(asExportSQLAddComments, menuExportAddComments.Checked); AppSettings.WriteBool(asExportSQLRemoveAutoIncrement, menuExportRemoveAutoIncrement.Checked); + AppSettings.WriteBool(asExportSQLRemoveDefiner, menuExportRemoveDefiner.Checked); if not StartsStr(OUTPUT_SERVER, comboExportOutputType.Text) then AppSettings.WriteInt(asExportSQLOutput, comboExportOutputType.ItemIndex); @@ -1539,15 +1542,7 @@ begin try case DBObj.NodeType of lntTable: begin - Struc := DBObj.CreateCode; - // Remove AUTO_INCREMENT clause - if menuExportRemoveAutoIncrement.Checked then begin - rx := TRegExpr.Create; - rx.ModifierI := True; - rx.Expression := '\sAUTO_INCREMENT\s*\=\s*\d+\s'; - Struc := rx.Replace(Struc, ' ', false); - rx.Free; - end; + Struc := DBObj.GetCreateCode(menuExportRemoveAutoIncrement.Checked, False); Insert('IF NOT EXISTS ', Struc, Pos('TABLE', Struc) + 6); if ToDb then Insert(Quoter.QuoteIdent(FinalDbName)+'.', Struc, Pos('EXISTS', Struc) + 7 ); @@ -1601,7 +1596,7 @@ begin lntTrigger: begin StrucResult := DBObj.Connection.GetResults('SHOW TRIGGERS FROM '+DBObj.QuotedDatabase+' WHERE `Trigger`='+esc(DBObj.Name)); - Struc := DBObj.CreateCode; + Struc := DBObj.GetCreateCode(False, menuExportRemoveDefiner.Checked); if ToDb then Insert(Quoter.QuoteIdent(FinalDbName)+'.', Struc, Pos('TRIGGER', Struc) + 8 ); if ToFile or ToClipboard or ToDir then begin @@ -1614,7 +1609,7 @@ begin end; lntFunction, lntProcedure: begin - Struc := DBObj.CreateCode; + Struc := DBObj.GetCreateCode(False, menuExportRemoveDefiner.Checked); if ToDb then begin if DBObj.NodeType = lntProcedure then Insert(Quoter.QuoteIdent(FinalDbName)+'.', Struc, Pos('PROCEDURE', Struc) + 10 )