Skip to content
Permalink
Browse files

SQL export: add drop down menu item for removing DEFINER clauses from…

… triggers, procedures and functions
  • Loading branch information
ansgarbecker committed Nov 13, 2019
1 parent ed5a4ac commit be689edaec600b209922e93734c5fbd795cff69e
Showing with 44 additions and 13 deletions.
  1. +2 −1 source/apphelpers.pas
  2. +32 −1 source/dbconnection.pas
  3. +4 −0 source/tabletools.dfm
  4. +6 −11 source/tabletools.pas
@@ -154,7 +154,7 @@ TQueryThread = class(TThread)
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 @@ constructor TAppSettings.Create;
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);
@@ -29,7 +29,6 @@ TDBObject = class(TPersistent)
function GetImageIndex: Integer;
function GetOverlayImageIndex: Integer;
function GetPath: String;
function GetCreateCode: String;
procedure SetCreateCode(Value: String);
public
// Table options:
@@ -50,6 +49,8 @@ TDBObject = class(TPersistent)
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 @@ function TDBObject.GetCreateCode: String;
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
@@ -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
@@ -82,6 +82,7 @@ TfrmTableTools = class(TExtForm)
menuExportRemoveAutoIncrement: TMenuItem;
comboMatchType: TComboBox;
lblMatchType: TLabel;
menuExportRemoveDefiner: TMenuItem;
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
@@ -257,6 +258,7 @@ procedure TfrmTableTools.FormCreate(Sender: TObject);
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 @@ procedure TfrmTableTools.SaveSettings(Sender: TObject);
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 @@ procedure TfrmTableTools.DoExport(DBObj: TDBObject);
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 @@ procedure TfrmTableTools.DoExport(DBObj: TDBObject);

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 @@ procedure TfrmTableTools.DoExport(DBObj: TDBObject);
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 )

0 comments on commit be689ed

Please sign in to comment.
You can’t perform that action at this time.