-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.pas
294 lines (246 loc) · 6.78 KB
/
models.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
unit Models;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, fgl, Utils;
type
{ TTableEntry }
TTableEntry {TModel}= class
public
Id: Integer;
end;
TTask = class;
TTasks = specialize TFPGObjectList<TTask>;
{ TTask }
TTask = class(TTableEntry)
public
Name, Description: String;
Created, Modified: TDateTime;
Done: Boolean;
function IsActive: Boolean;
procedure Start;
class procedure Stop; static;
class function HasActive: Boolean; static;
class function GetActive: TTask; static;
class function GetById(anId: Integer): TTask; static;
class function GetLastActive(ACount: Integer; AOnlyUnfinished: Boolean = False): TTasks;
private
class procedure RefreshSQLQuery;
end;
{ TPeriod }
TPeriod = class(TTableEntry)
private
function GetDuration: TDuration;
public
BeginTime, EndTime: TDateTime;
TaskId: Integer;
property Duration: TDuration read GetDuration;
function GetTask: TTask;
function IsActive: Boolean;
class function GetActive: TPeriod; static;
class function GetById(anId: Integer): TPeriod; static;
end;
implementation
uses
DatabaseDM, DB, StrUtils;
{ TPeriod }
function TPeriod.GetDuration: TDuration;
begin
if IsActive then
Result := Now - BeginTime
else
Result := EndTime - BeginTime;
end;
function TPeriod.GetTask: TTask;
begin
Result := TTask.GetById(Self.TaskId);
end;
function TPeriod.IsActive: Boolean;
begin
Result := EndTime = 0;
end;
class function TPeriod.GetActive: TPeriod;
begin
Result := Nil;
with DatabaseDataModule.CustomSQLQuery do
begin
Close;
SQL.Text := 'SELECT `id` FROM `periods` WHERE `is_active` == TRUE;';
Open;
if RecordCount > 0 then
begin
First;
Result := TPeriod.GetById(FieldByName('id').AsInteger);
end;
end;
end;
class function TPeriod.GetById(anId: Integer): TPeriod;
begin
Result := Nil;
with DatabaseDataModule.CustomSQLQuery do
begin
Close;
SQL.Text := 'SELECT * FROM `periods` WHERE `id` == :id;';
ParamByName('id').AsInteger := anId;
Open;
if RecordCount > 0 then
begin
First;
Result := TPeriod.Create;
Result.Id := FieldByName('id').AsInteger;
Result.BeginTime := FieldByName('begin').AsDateTime;
//if not FieldByName('end').IsNull then
//try
if FieldByName('end').DataType = ftDateTime then
Result.EndTime := FieldByName('end').AsDateTime
else
//except
Result.EndTime := 0;
//end;
Result.TaskId := FieldByName('task_id').AsInteger;
end;
end;
end;
{ TTask }
function TTask.IsActive: Boolean;
begin
with DatabaseDataModule.CustomSQLQuery do
begin
Close;
SQL.Text := 'SELECT COUNT(*) AS `cnt` FROM `periods` WHERE `is_active` == TRUE AND `task_id` == :task_id;';
ParamByName('task_id').AsInteger := Self.Id;
Open;
First;
Result := FieldByName('cnt').AsInteger > 0;
end;
end;
procedure TTask.Start;
begin
with DatabaseDataModule.PeriodsSQLQuery do
begin
// ToDo: Check if no unfinished periods
Append;
FieldByName('task_id').AsInteger := Self.Id;
FieldByName('begin').AsDateTime := Now;
Post;
ApplyUpdates;
end;
DatabaseDataModule.SQLTransaction1.CommitRetaining;
RefreshSQLQuery;
end;
class procedure TTask.Stop;
begin
with DatabaseDataModule.CustomSQLQuery do
begin
Close;
SQL.Text := 'UPDATE `_periods` SET `end`=:end WHERE /*`is_active` = TRUE*/ `end` IS NULL';
// ToDo: Check if only one result
ParamByName('end').AsDateTime := Now;
ExecSQL;
end;
DatabaseDataModule.SQLTransaction1.CommitRetaining;
//PeriodsSQLQuery.Refresh;
//RefreshStartStopBtnsVisibility;
RefreshSQLQuery;
end;
class function TTask.HasActive: Boolean;
begin
Result:=False;
//if (DatabaseDataModule <> nil) and (DataModule1.CustomSQLQuery <> nil) then
//begin
with DatabaseDataModule.CustomSQLQuery do
begin
Close;
SQL.Text := 'SELECT COUNT(*) AS `cnt` FROM `periods` WHERE `is_active` = TRUE;';
Open;
First;
Result := FieldByName('cnt').AsInteger > 0;
end;
//end;
end;
class function TTask.GetActive: TTask;
begin
Result := Nil;
with DatabaseDataModule.CustomSQLQuery do
begin
Close;
SQL.Text := 'SELECT `task_id` FROM `periods` WHERE `is_active` == TRUE;';
Open;
if RecordCount > 0 then
begin
First;
Result := TTask.GetById(FieldByName('task_id').AsInteger);
end;
end;
end;
class function TTask.GetById(anId: Integer): TTask;
begin
Result := Nil;
with DatabaseDataModule.CustomSQLQuery do
begin
Close;
SQL.Text := 'SELECT * FROM `tasks` WHERE `id` == :id;';
ParamByName('id').AsInteger := anId;
Open;
if RecordCount > 0 then
begin
First;
Result := TTask.Create;
Result.Id := FieldByName('id').AsInteger;
Result.Name := FieldByName('name').AsString;
Result.Description := FieldByName('description').AsString;
Result.Created := FieldByName('created').AsDateTime;
Result.Modified := FieldByName('modified').AsDateTime;
Result.Done := FieldByName('done').AsBoolean;
end;
end;
end;
class function TTask.GetLastActive(ACount: Integer; AOnlyUnfinished: Boolean = False): TTasks;
var
Task: TTask;
begin
Result := TTasks.Create();
with DatabaseDataModule.CustomSQLQuery do
begin
Close;
SQL.Text := 'SELECT `tasks`.*, MAX(`end`) AS `last_active` FROM `periods` '
+ '/*LEFT*/ INNER JOIN `tasks` ON `periods`.`task_id` = `tasks`.`id` '
+ IfThen(AOnlyUnfinished, 'where done == FALSE ')
+ 'GROUP BY `task_id` '
+ 'ORDER BY `last_active` DESC '
+ 'LIMIT :limit ';
ParamByName('limit').AsInteger := ACount;
Open;
First;
while not EOF do
begin
Task := TTask.Create;
// ToDo: Code duplicate
with Task do
begin
Id := FieldByName('id').AsInteger;
Name := FieldByName('name').AsString;
Description := FieldByName('description').AsString;
Created := FieldByName('created').AsDateTime;
Modified := FieldByName('modified').AsDateTime;
Done := FieldByName('done').AsBoolean;
end;
//////////////////
Result.Add(Task);
Next;
end;
Close;
end;
end;
class procedure TTask.RefreshSQLQuery;
var
r: Integer;
begin
with DatabaseDataModule.TasksSQLQuery do
begin
r := RecNo;
Refresh;
RecNo := r; // Restore previous selected row
end;
end;
end.