-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathYOTM.DB.Times.pas
267 lines (238 loc) · 6.21 KB
/
YOTM.DB.Times.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
unit YOTM.DB.Times;
interface
uses
HGM.SQLite, HGM.SQLang, System.Generics.Collections, HGM.Controls.VirtualTable,
YOTM.DB, Vcl.Graphics;
type
//Âðåìÿ ðàáîòû
TTimeItem = class;
TTimeItems = class;
TTimeItem = class(TObject)
private
FOwner: TTimeItems;
FID: Integer;
FDescription: string;
FTimeTo: TTime;
FTimeFrom: TTime;
FDate: TDate;
FTask: Integer;
FColor: TColor;
FDateEnd: TDate;
procedure SetOwner(const Value: TTimeItems);
procedure SetDescription(const Value: string);
procedure SetTimeFrom(const Value: TTime);
procedure SetTimeTo(const Value: TTime);
procedure SetID(const Value: Integer);
procedure SetDate(const Value: TDate);
procedure SetTask(const Value: Integer);
procedure SetColor(const Value: TColor);
procedure SetDateEnd(const Value: TDate);
public
constructor Create(AOwner: TTimeItems);
property Owner: TTimeItems read FOwner write SetOwner;
property Description: string read FDescription write SetDescription;
property TimeFrom: TTime read FTimeFrom write SetTimeFrom;
property TimeTo: TTime read FTimeTo write SetTimeTo;
property Date: TDate read FDate write SetDate;
property DateEnd: TDate read FDateEnd write SetDateEnd;
property Color: TColor read FColor write SetColor;
property ID: Integer read FID write SetID;
property Task: Integer read FTask write SetTask;
end;
TTimeItems = class(TTableData<TTimeItem>)
const
tnTable = 'TimeItems';
fnID = 'tiID';
fnTask = 'tiTask';
fnDesc = 'tiDesc';
fnTimeFrom = 'tiTimeFrom';
fnTimeTo = 'tiTimeTo';
fnDateStart = 'tiDate';
fnDateEnd = 'tiDateEnd';
private
FDataBase: TDB;
procedure SetDataBase(const Value: TDB);
public
constructor Create(ADataBase: TDB; ATableEx: TTableEx);
procedure Reload(Date: TDate);
procedure Update(Item: TTimeItem);
procedure Delete(Index: Integer); override;
procedure Clear; override;
procedure Save;
property DataBase: TDB read FDataBase write SetDataBase;
end;
implementation
uses
YOTM.DB.Tasks;
{ TTimeItem }
constructor TTimeItem.Create(AOwner: TTimeItems);
begin
inherited Create;
FID := -1;
FTask := -1;
FColor := clNone;
Owner := AOwner;
end;
procedure TTimeItem.SetColor(const Value: TColor);
begin
FColor := Value;
end;
procedure TTimeItem.SetDate(const Value: TDate);
begin
FDate := Value;
end;
procedure TTimeItem.SetDateEnd(const Value: TDate);
begin
FDateEnd := Value;
end;
procedure TTimeItem.SetDescription(const Value: string);
begin
FDescription := Value;
end;
procedure TTimeItem.SetID(const Value: Integer);
begin
FID := Value;
end;
procedure TTimeItem.SetOwner(const Value: TTimeItems);
begin
FOwner := Value;
end;
procedure TTimeItem.SetTask(const Value: Integer);
begin
FTask := Value;
end;
procedure TTimeItem.SetTimeFrom(const Value: TTime);
begin
FTimeFrom := Value;
end;
procedure TTimeItem.SetTimeTo(const Value: TTime);
begin
FTimeTo := Value;
end;
{ TTimeItems }
procedure TTimeItems.Clear;
var
i: Integer;
begin
for i := 0 to Count - 1 do
Items[i].Free;
inherited;
end;
constructor TTimeItems.Create(ADataBase: TDB; ATableEx: TTableEx);
begin
inherited Create(ATableEx);
FDataBase := ADataBase;
if not FDataBase.DB.TableExists(tnTable) then
with SQL.CreateTable(tnTable) do
begin
AddField(fnID, ftInteger, True, True);
AddField(fnDesc, ftString);
AddField(fnTimeFrom, ftDateTime);
AddField(fnTimeTo, ftDateTime);
AddField(fnDateStart, ftDateTime);
AddField(fnDateEnd, ftDateTime);
AddField(fnTask, ftInteger);
FDataBase.DB.ExecSQL(GetSQL);
EndCreate;
end;
end;
procedure TTimeItems.Delete(Index: Integer);
begin
with SQL.Delete(tnTable) do
begin
WhereFieldEqual(fnID, Items[Index].ID);
DataBase.DB.ExecSQL(GetSQL);
EndCreate;
end;
Items[Index].Free;
inherited;
end;
procedure TTimeItems.Reload;
var
Table: TSQLiteTable;
Item: TTimeItem;
begin
BeginUpdate;
Clear;
try
with SQL.Select(tnTable) do
begin
AddField(fnID);
AddField(fnTask);
AddField(fnDesc);
AddField(fnTimeFrom);
AddField(fnTimeTo);
AddField(fnDateStart);
AddField(TTaskItems.fnColor, '536870911');
AddField(fnDateEnd);
if Date <> 0 then
begin
WhereField(fnDateStart, ' >= ', Trunc(Date));
WhereField(fnDateEnd, '<=', Trunc(Date));
end;
OrderBy(fnTimeFrom, True);
LeftJoin(TTaskItems.tnTable, fnTask, TTaskItems.fnID);
Table := FDataBase.DB.GetTable(GetSQL);
EndCreate;
Table.MoveFirst;
while not Table.EOF do
begin
Item := TTimeItem.Create(Self);
Item.ID := Table.FieldAsInteger(0);
Item.Task := Table.FieldAsInteger(1);
Item.Description := Table.FieldAsString(2);
Item.TimeFrom := Frac(Table.FieldAsDateTime(3));
Item.TimeTo := Frac(Table.FieldAsDateTime(4));
Item.Date := Trunc(Table.FieldAsDateTime(5));
Item.Color := TColor(Table.FieldAsInteger(6));
Item.DateEnd := Table.FieldAsDateTime(7);
Add(Item);
Table.Next;
end;
Table.Free;
end;
finally
EndUpdate;
end;
end;
procedure TTimeItems.Update(Item: TTimeItem);
begin
if Item.ID < 0 then
with SQL.InsertInto(tnTable) do
begin
AddValue(fnTask, Item.Task);
AddValue(fnDesc, Item.Description);
AddValue(fnTimeFrom, Item.TimeFrom);
AddValue(fnTimeTo, Item.TimeTo);
AddValue(fnDateStart, Item.Date);
AddValue(fnDateEnd, Item.DateEnd);
DataBase.DB.ExecSQL(GetSQL);
Item.ID := DataBase.DB.LastInsertRowID;
EndCreate;
end
else
with SQL.Update(tnTable) do
begin
AddValue(fnTask, Item.Task);
AddValue(fnDesc, Item.Description);
AddValue(fnTimeFrom, Item.TimeFrom);
AddValue(fnTimeTo, Item.TimeTo);
AddValue(fnDateStart, Item.Date);
AddValue(fnDateEnd, Item.DateEnd);
WhereFieldEqual(fnID, Item.ID);
DataBase.DB.ExecSQL(GetSQL);
EndCreate;
end;
end;
procedure TTimeItems.Save;
var
i: Integer;
begin
for i := 0 to Count - 1 do
Update(Items[i]);
end;
procedure TTimeItems.SetDataBase(const Value: TDB);
begin
FDataBase := Value;
end;
end.