SQL export: add drop down menu item for removing DEFINER clauses from triggers, procedures and functions

This commit is contained in:
Ansgar Becker
2019-11-13 14:29:26 +01:00
parent ed5a4ace88
commit be689edaec
4 changed files with 44 additions and 13 deletions

View File

@@ -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);

View File

@@ -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

View File

@@ -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

View File

@@ -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 )