-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathuInsulins.pas
213 lines (185 loc) · 4.77 KB
/
uInsulins.pas
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
unit uInsulins;
interface
uses
// Delphi
Generics.Collections,
// FireDACE
FireDAC.Comp.Client,
// Project
uDatabase;
type
TInsulin = class
private
FIdent : Integer;
FName : string;
FDuration: Integer;
public
constructor Create(AIdent: Integer; const AName: string; ADuration: Integer);
property Ident : Integer read FIdent;
property Name : string read FName;
property Duration: Integer read FDuration;
end;
TInsulins = class
private
FQuery: TFDQuery;
FDatabase: TDatabase;
function GetQuery: TFDQuery;
function GetDatabase: TDatabase;
procedure AfterConnect(Sender: TObject);
property Query: TFDQuery read GetQuery;
property Database: TDatabase read GetDatabase;
public
constructor Create;
destructor Destroy; override;
procedure Delete(AIndex: Integer);
function GetItems: TObjectList<TInsulin>;
function GetItemByIndex(Value: Integer): TInsulin;
function GetItemByIdent(Value: Integer): TInsulin;
procedure Add(const AName: string; ADuration: Integer);
procedure Edit(AIndex: Integer; const AName: string; ADuration: Integer);
end;
function Insulins: TInsulins;
implementation
uses
System.SysUtils;
var
varInsulins: TInsulins = nil;
function Insulins: TInsulins;
begin
if not Assigned(varInsulins) then
varInsulins := TInsulins.Create;
Result := varInsulins;
end;
{ TInsulin }
constructor TInsulin.Create(AIdent: Integer; const AName: string; ADuration: Integer);
begin
inherited Create;
FIdent := AIdent;
FName := AName;
FDuration := ADuration;
end;
{ TInsulins }
constructor TInsulins.Create;
begin
inherited;
FQuery := nil;
FDatabase := nil;
end;
destructor TInsulins.Destroy;
begin
if Assigned(FQuery) then
try
if FQuery.Active then
FQuery.Close;
finally
FQuery.Free;
end;
if Assigned(FDatabase) then
FDatabase.Free;
inherited Destroy;
end;
function TInsulins.GetDatabase: TDatabase;
begin
if not Assigned(FDatabase) then
begin
FDatabase := TDatabase.Create;
FDatabase.Connection.AfterConnect := AfterConnect;
end;
Result := FDatabase;
end;
function TInsulins.GetQuery: TFDQuery;
begin
if not Assigned(FQuery) then
begin
FQuery := TFDQuery.Create(nil);
FQuery.Connection := Database.Connection;
FQuery.Open('SELECT * FROM INSULINS');
end;
Result := FQuery;
end;
procedure TInsulins.AfterConnect;
begin
if not Database.TableExists('INSULINS') then
if Database.Execute('CREATE TABLE INSULINS (ID INTEGER PRIMARY KEY, NAME TEXT, DURATION INTEGER);') > -1 then
begin
Database.Execute('INSERT INTO INSULINS (NAME, DURATION) VALUES(''NovoRapid'', 180);');
Database.Execute('INSERT INTO INSULINS (NAME, DURATION) VALUES(''Actrapid'', 300);');
end;
end;
function TInsulins.GetItems: TObjectList<TInsulin>;
begin
Result := TObjectList<TInsulin>.Create;
Query.First;
while not Query.EOF do
begin
Result.Add(TInsulin.Create(Query.FieldByName('ID').AsInteger, Query.FieldByName('NAME').AsString, Query.FieldByName('DURATION').AsInteger));
Query.Next;
end;
end;
procedure TInsulins.Delete(AIndex: Integer);
var
IL: TObjectList<TInsulin>;
begin
if AIndex > -1 then
begin
IL := GetItems;
try
if AIndex < IL.Count then
if Database.Execute(Format('DELETE FROM INSULINS WHERE ID = %d;', [IL[AIndex].Ident])) > 0 then
Query.Refresh;
finally
IL.Free;
end;
end;
end;
function TInsulins.GetItemByIndex(Value: Integer): TInsulin;
var
IL: TObjectList<TInsulin>;
begin
if Value > -1 then
begin
IL := GetItems;
try
if Value < IL.Count then
Result := TInsulin.Create(IL[Value].Ident, IL[Value].Name, IL[Value].Duration);
finally
IL.Free;
end;
end;
end;
function TInsulins.GetItemByIdent(Value: Integer): TInsulin;
begin
Result := nil;
if Query.Locate('ID', Value) then
Result := TInsulin.Create(Query.FieldByName('ID').AsInteger, Query.FieldByName('NAME').AsString, Query.FieldByName('DURATION').AsInteger);
end;
procedure TInsulins.Add(const AName: string; ADuration: Integer);
begin
if Database.Execute(Format('INSERT INTO INSULINS (NAME, DURATION) VALUES(''%s'', %d);', [AName, ADuration])) > 0 then
Query.Refresh;
end;
procedure TInsulins.Edit(AIndex: Integer; const AName: string; ADuration: Integer);
var
IL: TObjectList<TInsulin>;
begin
if AIndex > -1 then
begin
IL := GetItems;
try
if AIndex < IL.Count then
if Query.Locate('ID', IL[AIndex].Ident, []) then
begin
Query.Edit;
try
Query.FieldByName('NAME').AsString := AName;
Query.FieldByName('DURATION').AsInteger := ADuration;
finally
Query.Post;
end;
end;
finally
IL.Free;
end;
end;
end;
end.