-
Notifications
You must be signed in to change notification settings - Fork 5
/
Work.Table.History.pas
278 lines (251 loc) · 6.58 KB
/
Work.Table.History.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
unit Work.Table.History;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.Generics.Collections, Vcl.Grids,
HGM.Controls.VirtualTable, SQLLang, SQLiteTable3, Work.DB;
type
TTableHistory = class;
THistoryAction = (haAdd, haChange, haDelete);
TItemHistory = class
private
FModifed:Boolean;
FOwner:TTableHistory;
FEmpty:Boolean;
public
ID:Integer;
Action:THistoryAction;
TableName:string;
RecordID:Integer;
Desc:string;
Date:TDateTime;
property Modifed:Boolean read FModifed write FModifed;
property Empty:Boolean read FEmpty write FEmpty;
procedure Update;
procedure GetBack;
constructor Create(AOwner:TTableHistory);
end;
TTableHistory = class(TTableData<TItemHistory>)
const TableName = 'History';
const fnID = 'hyID';
const fnAction = 'hyAction';
const fnTableName = 'hyTableName';
const fnRecordID = 'hyRecordID';
const fnDesc = 'hyDesc';
const fnDate = 'hyDate';
private
FDB:TDatabaseCore;
FFilter:THistoryAction;
FUseFilter:Boolean;
FOrderBy: string;
FOrderByDESC:Boolean;
public
procedure Load;
procedure Save;
procedure GetBack(Index:Integer); overload;
procedure GetBack(Item:TItemHistory); overload;
procedure Update(Index:Integer); overload;
procedure Update(Item:TItemHistory); overload;
procedure Delete(Item:TItemHistory); overload;
procedure Delete(Index:Integer); overload;
procedure Clear; override;
function Add(AAction:THistoryAction; ATableName:string; ARecordID:Integer; ADesc:string):Integer; overload;
property Filter:THistoryAction read FFilter write FFilter;
property UseFilter:Boolean read FUseFilter write FUseFilter;
property LoadOrderBy:string read FOrderBy write FOrderBy;
property LoadOrderByDESC:Boolean read FOrderByDESC write FOrderByDESC;
constructor Create(ADB:TDatabaseCore); overload;
property DatabaseCore:TDatabaseCore read FDB write FDB;
end;
implementation
uses Work.Table.Customers;
{ TTableHistory }
function TTableHistory.Add(AAction: THistoryAction; ATableName: string; ARecordID: Integer; ADesc: string): Integer;
var Item:TItemHistory;
begin
Item:=TItemHistory.Create(Self);
Item.Action:=AAction;
Item.RecordID:=ARecordID;
Item.TableName:=ATableName;
Item.Desc:=ADesc;
Item.Update;
Insert(0, Item);
Result:=0;
end;
procedure TTableHistory.Clear;
var i:Integer;
begin
for i:= 0 to Count-1 do Items[i].Free;
inherited;
end;
constructor TTableHistory.Create(ADB:TDatabaseCore);
begin
inherited Create;
FOrderBy:=fnDate;
FOrderByDESC:=True;
FDB:=ADB;
if not FDB.SQL.TableExists(TableName) then
with SQL.CreateTable(TableName) do
begin
AddField(fnID, ftInteger, True, True);
AddField(fnAction, ftInteger);
AddField(fnTableName, ftString);
AddField(fnRecordID, ftInteger);
AddField(fnDesc, ftString);
AddField(fnDate, ftDateTime);
FDB.SQL.ExecSQL(GetSQL);
EndCreate;
end;
end;
procedure TTableHistory.Delete(Item: TItemHistory);
begin
with SQL.Delete(TableName) do
begin
WhereFieldEqual(fnID, Item.ID);
FDB.SQL.ExecSQL(GetSQL);
EndCreate;
end;
end;
procedure TTableHistory.Delete(Index: Integer);
begin
Delete(Items[Index]);
inherited;
end;
procedure TTableHistory.GetBack(Item: TItemHistory);
var RTable:TSQLiteTable;
begin
with SQL.Select(TableName) do
begin
AddField(fnAction);
AddField(fnTableName);
AddField(fnRecordID);
AddField(fnDesc);
AddField(fnDate);
WhereFieldEqual(fnID, Item.ID);
RTable:=FDB.SQL.GetTable(GetSQL);
if RTable.Count > 0 then
begin
Item.Action:=THistoryAction(RTable.FieldAsInteger(0));
Item.TableName:=RTable.FieldAsString(1);
Item.RecordID:=RTable.FieldAsInteger(2);
Item.Desc:=RTable.FieldAsString(3);
Item.Date:=RTable.FieldAsDateTime(4);
Item.Modifed:=False;
Item.Empty:=False;
end;
RTable.Free;
EndCreate;
end;
end;
procedure TTableHistory.Load;
var RTable:TSQLiteTable;
Item:TItemHistory;
begin
BeginUpdate;
Clear;
try
with SQL.Select(TableName) do
begin
AddField(fnID);
AddField(fnAction);
AddField(fnTableName);
AddField(fnRecordID);
AddField(fnDesc);
AddField(fnDate);
if UseFilter then WhereFieldEqual(fnAction, Ord(FFilter));
OrderBy(FOrderBy, FOrderByDESC);
WhereField(fnDate, '>', Now - 30);
RTable:=FDB.SQL.GetTable(GetSQL);
while not RTable.EOF do
begin
Item:=TItemHistory.Create(Self);
Item.ID:=RTable.FieldAsInteger(0);
Item.Action:=THistoryAction(RTable.FieldAsInteger(1));
Item.TableName:=RTable.FieldAsString(2);
Item.RecordID:=RTable.FieldAsInteger(3);
Item.Desc:=RTable.FieldAsString(4);
Item.Date:=RTable.FieldAsDateTime(5);
Item.Modifed:=False;
Item.Empty:=False;
Add(Item);
RTable.Next;
end;
RTable.Free;
EndCreate;
end;
finally
EndUpdate;
end;
end;
procedure TTableHistory.GetBack(Index: Integer);
begin
GetBack(Items[Index]);
end;
procedure TTableHistory.Save;
var i:Integer;
begin
for i:= 0 to Count-1 do if Items[i].Modifed then Update(i);
end;
procedure TTableHistory.Update(Item: TItemHistory);
var Res:Integer;
begin
with SQL.Select(TableName) do
begin
AddField(fnID);
WhereFieldEqual(fnID, Item.ID);
Res:=FDB.SQL.GetTableValue(GetSQL);
EndCreate;
end;
if Res < 0 then
begin
with SQL.InsertInto(TableName) do
begin
AddValue(fnAction, Ord(Item.Action));
AddValue(fnTableName, Item.TableName);
AddValue(fnRecordID, Item.RecordID);
AddValueAsParam(fnDesc, '?', True);
AddValue(fnDate, Item.Date);
FDB.SQL.ExecSQL(GetSQL, [PAnsiChar(AnsiString(Item.Desc))]);
Item.ID:=FDB.SQL.GetLastInsertRowID;
EndCreate;
end;
end
else
begin
with SQL.Update(TableName) do
begin
AddValue(fnAction, Ord(Item.Action));
AddValue(fnTableName, Item.TableName);
AddValue(fnRecordID, Item.RecordID);
AddValueAsParam(fnDesc, '?', True);
AddValue(fnDate, Item.Date);
WhereFieldEqual(fnID, Item.ID);
FDB.SQL.ExecSQL(GetSQL, [PAnsiChar(AnsiString(Item.Desc))]);
EndCreate;
end;
end;
Item.Modifed:=False;
Item.Empty:=False;
end;
procedure TTableHistory.Update(Index: Integer);
begin
Update(Items[Index]);
end;
{ TItemHistory }
constructor TItemHistory.Create(AOwner:TTableHistory);
begin
inherited Create;
FModifed:=True;
FEmpty:=True;
FOwner:=AOwner;
Date:=Now;
end;
procedure TItemHistory.GetBack;
begin
FOwner.GetBack(Self);
end;
procedure TItemHistory.Update;
begin
FOwner.Update(Self);
end;
end.