Refactor: create and use self explanatory TDBObjectEditor.ObjectExists method

This commit is contained in:
Ansgar Becker
2022-04-22 12:42:37 +02:00
parent 3d55507881
commit e27d9c1b96
6 changed files with 25 additions and 19 deletions

View File

@@ -36,6 +36,7 @@ type
FModified: Boolean;
procedure SetModified(Value: Boolean);
protected
function ObjectExists: Boolean;
public
DBObject: TDBObject;
constructor Create(AOwner: TComponent); override;
@@ -1797,6 +1798,11 @@ begin
FModified := Value;
end;
function TDBObjectEditor.ObjectExists: Boolean;
begin
Result := not DBObject.Name.IsEmpty;
end;
procedure TDBObjectEditor.Init(Obj: TDBObject);
var
editName: TWinControl;

View File

@@ -109,7 +109,7 @@ begin
udEveryQuantity.Position := 1;
comboEveryInterval.ItemIndex := comboEveryInterval.Items.IndexOf('DAY');
tabALTERcode.TabVisible := False;
if DBObject.Name <> '' then begin
if ObjectExists then begin
// Edit mode
tabALTERcode.TabVisible := True;
editName.Text := DBObject.Name;
@@ -221,7 +221,7 @@ var
begin
// Create or alter table
Result := mrOk;
if DBObject.Name = '' then
if not ObjectExists then
sql := ComposeCreateStatement
else
sql := ComposeAlterStatement;
@@ -229,7 +229,7 @@ begin
MainForm.ActiveConnection.Query(sql);
DBObject.Name := editName.Text;
DBObject.UnloadDetails;
tabALTERcode.TabVisible := DBObject.Name <> '';
tabALTERcode.TabVisible := ObjectExists;
Mainform.UpdateEditorTab;
Mainform.RefreshTree(DBObject);
Modified := False;
@@ -296,7 +296,7 @@ begin
Result := Result + #9 + 'ON COMPLETION NOT PRESERVE'
else
Result := Result + #9 + 'ON COMPLETION PRESERVE';
if (DBObject.Name <> '') and (DBObject.Name <> editName.Text) then
if ObjectExists and (DBObject.Name <> editName.Text) then
Result := Result + CRLF + #9 + 'RENAME TO ' + DBObject.Connection.QuoteIdent(editName.Text);
Result := Result + CRLF + #9 + UpperCase(grpState.Items[grpState.ItemIndex]);
Result := Result + CRLF + #9 + 'COMMENT ' + DBObject.Connection.EscapeString(editComment.Text);

View File

@@ -147,7 +147,7 @@ begin
comboDefiner.TextHint := f_('Current user (%s)', [Obj.Connection.CurrentUserHostCombination]);
comboDefiner.Hint := f_('Leave empty for current user (%s)', [Obj.Connection.CurrentUserHostCombination]);
SynMemoBody.Text := 'BEGIN'+CRLF+CRLF+'END';
if DBObject.Name <> '' then begin
if ObjectExists then begin
// Editing existing routine
DBObject.Connection.ParseRoutineStructure(Obj, Parameters);
comboReturns.Text := Obj.Returns;
@@ -177,7 +177,7 @@ begin
btnDiscard.Top := btnSave.Top;
btnRunProc.Top := btnSave.Top;
btnRunProc.Left := Width - btnRunProc.Width - 3;
Mainform.actRunRoutines.Enabled := DBObject.Name <> '';
Mainform.actRunRoutines.Enabled := ObjectExists;
Mainform.ShowStatusMsg;
Screen.Cursor := crDefault;
end;
@@ -471,7 +471,7 @@ begin
// Create a temp routine, check for syntax errors, then drop the old routine and create it.
// See also: http://dev.mysql.com/doc/refman/5.0/en/alter-procedure.html
try
if DBObject.Name <> '' then begin
if ObjectExists then begin
// Create temp name
i := 0;
allRoutineNames := DBObject.Connection.GetCol('SELECT ROUTINE_NAME FROM '+DBObject.Connection.QuoteIdent(DBObject.Connection.InfSch)+'.'+DBObject.Connection.QuoteIdent('ROUTINES')+

View File

@@ -292,7 +292,7 @@ begin
treeIndexes.Clear;
listForeignKeys.Clear;
listCheckConstraints.Clear;
tabALTERcode.TabVisible := DBObject.Name <> '';
tabALTERcode.TabVisible := ObjectExists;
// Clear value editors
memoComment.Text := '';
if Obj.Connection.ServerVersionInt < 50503 then
@@ -309,7 +309,7 @@ begin
comboInsertMethod.ItemIndex := -1;
SynMemoPartitions.Clear;
if DBObject.Name = '' then begin
if not ObjectExists then begin
// Creating new table
editName.Text := '';
if DBObject.Connection.Parameters.IsAnyMySQL then
@@ -460,7 +460,7 @@ begin
// Create or alter table
Result := mrOk;
if DBObject.Name = '' then
if not ObjectExists then
Batch := ComposeCreateStatement
else
Batch := ComposeAlterStatement;
@@ -468,11 +468,11 @@ begin
for Query in Batch do
DBObject.Connection.Query(Query.SQL);
// Rename table
if (DBObject.Name <> '') and (editName.Text <> DBObject.Name) then begin
if ObjectExists and (editName.Text <> DBObject.Name) then begin
Rename := DBObject.Connection.GetSQLSpecifity(spRenameTable, [DBObject.QuotedName, DBObject.Connection.QuoteIdent(editName.Text)]);
DBObject.Connection.Query(Rename);
end;
tabALTERcode.TabVisible := DBObject.Name <> '';
tabALTERcode.TabVisible := ObjectExists;
if chkCharsetConvert.Checked then begin
// Autoadjust column collations
for i:=0 to FColumns.Count-1 do begin
@@ -483,7 +483,7 @@ begin
// Set table name for altering if Apply was clicked
DBObject.Name := editName.Text;
DBObject.UnloadDetails;
tabALTERcode.TabVisible := DBObject.Name <> '';
tabALTERcode.TabVisible := ObjectExists;
Mainform.UpdateEditorTab;
MainForm.tabData.TabVisible := True;
Mainform.RefreshTree(DBObject);
@@ -2301,7 +2301,7 @@ end;
procedure TfrmTableEditor.chkCharsetConvertClick(Sender: TObject);
begin
chkCharsetConvert.Enabled := (DBObject.Name <> '') and (comboCollation.ItemIndex > -1);
chkCharsetConvert.Enabled := ObjectExists and (comboCollation.ItemIndex > -1);
listColumns.Repaint;
Modification(Sender);
end;

View File

@@ -104,7 +104,7 @@ begin
end;
if comboTable.Items.Count > 0 then
comboTable.ItemIndex := 0;
if DBObject.Name <> '' then begin
if ObjectExists then begin
// Edit mode
editName.Text := DBObject.Name;
Definitions := MainForm.ActiveConnection.GetResults('SHOW TRIGGERS FROM '+Obj.Connection.QuoteIdent(Mainform.ActiveDatabase));
@@ -203,7 +203,7 @@ end;
procedure TfrmTriggerEditor.comboChange(Sender: TObject);
begin
// Auto generate trigger name as long as it was not user-edited. See issue #3477.
if (DBObject.Name = '') and (not editName.Modified) then
if (not ObjectExists) and (not editName.Modified) then
editName.Text := comboTable.Text+'_'+LowerCase(comboTiming.Text)+'_'+LowerCase(comboEvent.Text);
Modification(Sender);
end;
@@ -226,7 +226,7 @@ begin
// So, we take the risk of loosing the trigger for cases in which the user has SQL errors in
// his statement. The user must fix such errors and re-press "Save" while we have them in memory,
// otherwise the trigger attributes are lost forever.
if DBObject.Name <> '' then try
if ObjectExists then try
DBObject.Connection.Query('DROP TRIGGER '+DBObject.Connection.QuoteIdent(DBObject.Name));
except
end;

View File

@@ -215,7 +215,7 @@ var
sql, ViewName, RenameView: String;
begin
// Create or Alter code
if DBObject.Name = '' then begin
if not ObjectExists then begin
sql := 'CREATE ';
ViewName := editName.Text;
end else begin
@@ -234,7 +234,7 @@ begin
sql := sql + 'WITH '+Uppercase(rgCheck.Items[rgCheck.ItemIndex])+' CHECK OPTION';
sql := sql + ';' + sLineBreak;
if (DBObject.Name <> '') and (DBObject.Name <> editName.Text) then begin
if ObjectExists and (DBObject.Name <> editName.Text) then begin
RenameView := DBObject.Connection.QuoteIdent(editName.Text);
sql := sql + 'RENAME TABLE '+ViewName + ' TO '+RenameView + ';' + sLineBreak;
end;