4
février
2009
Persistance des objets par SQL : suppression
février
2009
Un article de erca57
Pas de commentaires
14e épisode
2.2.4 – Visiteur TSQLDeletePerson
Pour effacer un objet TPerson de la base de données, nous créons le visiteur TSQLDeletePerson. Il est construit sur le même modèle que TSQLUpdatePerson et ne fait que redéfinir les méthodes Init et SetupParams:
{
******************************* TSQLDeletePerson *******************************
}
procedure TSQLDeletePerson.Init;
begin
FQuery.SQL.Clear;
FQuery.SQL.Add('DELETE FROM PEOPLE');
FQuery.SQL.Add('WHERE OID = :OID;');
end;
procedure TSQLDeletePerson.SetupParams;
var
IData: TPerson;
begin
IData := TPerson(FVisited);
FQuery.Params.ParamByName('OID').AsInteger := IData.OID;
end;
******************************* TSQLDeletePerson *******************************
}
procedure TSQLDeletePerson.Init;
begin
FQuery.SQL.Clear;
FQuery.SQL.Add('DELETE FROM PEOPLE');
FQuery.SQL.Add('WHERE OID = :OID;');
end;
procedure TSQLDeletePerson.SetupParams;
var
IData: TPerson;
begin
IData := TPerson(FVisited);
FQuery.Params.ParamByName('OID').AsInteger := IData.OID;
end;
Le visiteur est appliqué à l’objet TPerson à effaçer comme suit:
procedure TFormRepert.aDeleteExecute(Sender: TObject);
var
aPerson: TPerson;
Visitor: TSQLDeletePerson;
begin
if ListBox1.ItemIndex < 0 then begin
MessageDlg('Sélectionnez une ligne', mtError, [mbOK], 0);
Exit;
end;
aPerson := TPerson(People.List.Items[ListBox1.ItemIndex]);
aPerson.State := bosDeleted;
if (MessageDlg('Effacer "'+ListBox1.Items[ListBox1.ItemIndex]+'" ?',
mtConfirmation, [mbYes,mbNo], 0) = mrYes)
then begin
if ExtractFileExt(CurrentFileName) = '.fdb' then begin
aPerson.State := bosDelete;
Visitor := TSQLDeletePerson.Create(CurrentFileName);
try
aPerson.AcceptBizObjVisitor(Visitor);
finally
Visitor.Free;
end;
end;
if aPerson.State = bosDeleted then
People.List.Delete(ListBox1.ItemIndex);
DisplayList;
end;
end;
var
aPerson: TPerson;
Visitor: TSQLDeletePerson;
begin
if ListBox1.ItemIndex < 0 then begin
MessageDlg('Sélectionnez une ligne', mtError, [mbOK], 0);
Exit;
end;
aPerson := TPerson(People.List.Items[ListBox1.ItemIndex]);
aPerson.State := bosDeleted;
if (MessageDlg('Effacer "'+ListBox1.Items[ListBox1.ItemIndex]+'" ?',
mtConfirmation, [mbYes,mbNo], 0) = mrYes)
then begin
if ExtractFileExt(CurrentFileName) = '.fdb' then begin
aPerson.State := bosDelete;
Visitor := TSQLDeletePerson.Create(CurrentFileName);
try
aPerson.AcceptBizObjVisitor(Visitor);
finally
Visitor.Free;
end;
end;
if aPerson.State = bosDeleted then
People.List.Delete(ListBox1.ItemIndex);
DisplayList;
end;
end;