1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
unit GestionCompte;
interface
uses
SysUtils, Classes,Controls,Dialogs, UIB,ContNRS;
type
TGestion = class
private
FUIBDB : TUIBDataBase;
FUIBQR : TUIBQuery;
FUIBTS : TUIBTransaction;
protected
property UIBDB : TUIBDataBase read FUIBDB;
property UIBQR : TUIBQuery read FUIBQR;
property UIBTS : TUIBTransaction read FUIBTS;
public
constructor Create(aDBFile, aDBLibrary, aUserName, aPassWord: string); virtual;
destructor Destroy; override;
end;
TUtilisateur = class
private
FNum: Integer;
FNom: String;
FPrenom: string;
FAge: Integer;
public
property Num : Integer read FNum write FNum;
property Nom : string read FNom write FNom;
property Prenom : string read FPrenom write FPrenom;
property Age : Integer read FAge write FAge;
public
constructor Create; virtual;
destructor Destroy; override;
end;
TListeUtilisateur = class
private
FUtilisateur : TUtilisateur;
FLUtilisateur :TObjectList;
public
property Utilisateur : TUtilisateur read FUtilisateur write FUtilisateur;
property LUtilisateur : TObjectList read FLUtilisateur write FLUtilisateur;
procedure Voir;
procedure Ajouter; virtual; abstract;
procedure Modifier; virtual; abstract;
procedure Supprimer; virtual; abstract;
public
constructor Create; virtual;
destructor Destroy; override;
end;
TListeUtilisateurCustom = class (TListeUtilisateur)
private
FGestion :TGestion;
public
property Gestion : TGestion read FGestion write FGestion ;
procedure CallErrorMsg(aMessage: string);
procedure Voir;
procedure Ajouter(const aNeedConfirmation: boolean = false); override;
procedure Modifier(const aNeedConfirmation: boolean = false); override;
procedure Supprimer(const aNeedConfirmation: boolean = false); override;
public
constructor Create; virtual;
destructor Destroy; override;
end;
implementation
{ TGestion }
procedure TListeUtilisateurCustom.CallErrorMsg(aMessage: string);
begin
Gestion.FUIBTS.RollBack;
//MessageDlg(E.Message, mtError, [mbOK], 0);
end;
constructor TGestion.Create(aDBFile, aDBLibrary, aUserName, aPassWord: string);
begin
assert(fileExists(aDBFile), 'Oops! Fichier de données non trouvé.');
assert(fileExists(aDBLibrary), 'Epic Fail : Moteur de base de donnée absent.');
FUIBDB := TUIBDataBase.Create(Nil);
FUIBDB.Params.Add('sql_dialect=3');
FUIBDB.Params.Add('lc_ctype=UTF8');
FUIBDB.DatabaseName := aDBFile;
FUIBDB.UserName := aUserName;
FUIBDB.PassWord := aPassWord;
FUIBDB.LibraryName := aDBFile;
FUIBDB.Connected := True;
FUIBTS := TUIBTransaction.Create(nil);
FUIBTS.DataBase := FUIBDB;
FUIBQR := TUIBQuery.Create(nil);
FUIBQR.Transaction := FUIBTS;
end;
destructor TGestion.Destroy;
begin
FUIBQR.Free;
FUIBTS.Free;
FUIBDB.Free;
inherited;
end;
{ TUtilisateur }
constructor TUtilisateur.Create;
begin
FNum := -1;
FNom := '';
FPrenom := '';
FAge := -1;
end;
destructor TUtilisateur.Destroy;
begin
inherited;
end;
procedure TListeUtilisateurCustom.Voir;
begin
try
Gestion.FUIBQR.SQL.Add('SELECT NOM,PRENOM,AGE FROM UTILISATEUR WHERE NUM=:NUM');
Gestion.FUIBQR.Params.ByNameAsInteger['NUM'] := Utilisateur.FNum;
Gestion.FUIBQR.Open;
Utilisateur.FNom := Gestion.FUIBQR.Fields.ByNameAsString['NOM'];
Utilisateur.FPrenom := Gestion.FUIBQR.Fields.ByNameAsString['PRENOM'];
Utilisateur.FAge := Gestion.FUIBQR.Fields.ByNameAsInteger['AGE'];
Gestion.FUIBQR.Close(etmCommit);
except
on E: Exception do
CallErrorMsg(E.Message);
end;
end;
procedure TListeUtilisateurCustom.Ajouter(const aNeedConfirmation: boolean = false);
begin
if aNeedConfirmation then
if MessageDlg('Confirmer l''ajout de cet utilisateur ?', mtConfirmation, [mbYes, mbNo], 0) <> mrYes then
exit;
try
Gestion.FUIBQR.SQL.Add('INSERT INTO UTILISATEUR (NOM,PRENOM,AGE) VALUES(:NOM,:PRENOM,:AGE)');
Gestion.FUIBQR.Params.ByNameAsString['NOM'] := Utilisateur.FNom;
Gestion.FUIBQR.Params.ByNameAsString['PRENOM'] := Utilisateur.FPrenom;
Gestion.FUIBQR.Params.ByNameAsInteger['AGE'] := Utilisateur.FAge;
Gestion.FUIBQR.ExecSQL;
Gestion.FUIBTS.Commit;
except
on E: Exception do
CallErrorMsg(E.Message);
end;
end;
constructor TListeUtilisateurCustom.Create;
begin
FGestion := TGestion.Create(nil);
end;
destructor TListeUtilisateurCustom.Destroy;
begin
Gestion.Free;
inherited;
end;
procedure TListeUtilisateurCustom.Modifier(const aNeedConfirmation: boolean = false);
begin
if aNeedConfirmation then
if MessageDlg('Confirmer la modification de cet utilisateur ?', mtConfirmation, [mbYes, mbNo], 0) <> mrYes then
exit;
try
Gestion.FUIBQR.SQL.Add('UPDATE UTILISATEUR SET NOM=:NOM, PRENOM=:PRENOM, AGE=:AGE WHERE NUM=:NUM');
Gestion.FUIBQR.Params.ByNameAsInteger['NUM'] := Utilisateur.FNum;
Gestion.FUIBQR.Params.ByNameAsString['NOM'] := Utilisateur.FNom;
Gestion.FUIBQR.Params.ByNameAsString['PRENOM'] := Utilisateur.FPrenom;
Gestion.FUIBQR.Params.ByNameAsInteger['AGE'] := Utilisateur.FAge;
Gestion.FUIBQR.ExecSQL;
Gestion.FUIBTS.Commit;
except
on E: Exception do
CallErrorMsg(E.Message);
end;
end;
procedure TListeUtilisateurCustom.Supprimer(const aNeedConfirmation: boolean = false);
begin
if aNeedConfirmation then
if MessageDlg('Voulez vous supprimer cet utilisateur ?', mtConfirmation, [mbYes, mbNo], 0) <> mrYes then
exit;
try
Gestion.FUIBQR.SQL.Add('DELETE FROM UTILISATEUR WHERE NUM=:NUM');
Gestion.FUIBQR.Params.ByNameAsInteger['NUM'] := Utilisateur.FNum;
Gestion.FUIBQR.ExecSQL;
Gestion.FUIBTS.Commit;
except
on E: Exception do
CallErrorMsg(E.Message);
end;
end;
{ TListeUtilisateur }
procedure TListeUtilisateur.Ajouter;
begin
Utilisateur := TUtilisateur.Create;
Utilisateur.Nom := 'EGAR';
Utilisateur.Prenom := 'KEWIN';
Utilisateur.Age := 42;
LUtilisateur.add(Utilisateur);
Utilisateur.Free;
end;
constructor TListeUtilisateur.Create;
begin
FUtilisateur := TListeUtilisateur.Create;
end;
destructor TListeUtilisateur.Destroy;
begin
FLUtilisateur.Free;
inherited;
end;
procedure TListeUtilisateur.Modifier(const aNeedConfirmation: boolean);
begin
Utilisateur := TUtilisateur.Create;
Utilisateur.Nom := 'EGAR';
Utilisateur.Prenom := 'KEWIN';
Utilisateur.Age := 42;
LUtilisateur.Add(Utilisateur);
Utilisateur.Free;
end;
end. |
Partager