Skip to content

Commit

Permalink
SQL export: add drop down menu item for removing DEFINER clauses from…
Browse files Browse the repository at this point in the history
… triggers, procedures and functions
  • Loading branch information
ansgarbecker committed Nov 13, 2019
1 parent ed5a4ac commit be689ed
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
3 changes: 2 additions & 1 deletion source/apphelpers.pas
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
33 changes: 32 additions & 1 deletion source/dbconnection.pas
Expand Up @@ -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:
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions source/tabletools.dfm
Expand Up @@ -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
17 changes: 6 additions & 11 deletions source/tabletools.pas
Expand Up @@ -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);
Expand Down Expand Up @@ -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 +
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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
Expand All @@ -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 )
Expand Down

0 comments on commit be689ed

Please sign in to comment.