-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathYOTM.DB.Notes.pas
231 lines (207 loc) · 4.96 KB
/
YOTM.DB.Notes.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
unit YOTM.DB.Notes;
interface
uses
HGM.SQLite, HGM.SQLang, System.Generics.Collections, SysUtils, System.Classes,
HGM.Controls.VirtualTable, YOTM.DB, Vcl.Graphics;
type
//Ìåòêè çàäà÷
TNoteItem = class;
TNoteItem = class(TObject)
const
tnTable = 'NoteItems';
fnID = 'niID';
fnText = 'niText';
fnDate = 'niDate';
fnModify = 'niModify';
private
FID: Integer;
FLoaded: Boolean;
FText: TStringStream;
FDateModify: TDateTime;
FDate: TDate;
FDataBase: TDB;
procedure SetText(const Value: TStringStream);
procedure SetID(const Value: Integer);
procedure SetDateModify(const Value: TDateTime);
procedure SetDate(const Value: TDate);
procedure SetDataBase(const Value: TDB);
public
constructor Create(ADataBase: TDB);
destructor Destroy; override;
procedure Load(ADate: TDate);
procedure Save;
property ID: Integer read FID write SetID;
property Text: TStringStream read FText write SetText;
property Date: TDate read FDate write SetDate;
property DateModify: TDateTime read FDateModify write SetDateModify;
property DataBase: TDB read FDataBase write SetDataBase;
property Loaded: Boolean read FLoaded;
end;
TNoteRecord = record
ID: Integer;
Date: TDate;
DateModify: TDateTime;
end;
TActualNotes = class(TTableData<TNoteRecord>)
private
FDataBase: TDB;
public
procedure Reload(ADate: TDate);
constructor Create(ADataBase: TDB; ATableEx: TTableEx);
destructor Destroy; override;
end;
implementation
uses
DateUtils;
{ TNoteItem }
constructor TNoteItem.Create;
begin
inherited Create;
FDataBase := ADataBase;
FLoaded := False;
if not FDataBase.DB.TableExists(tnTable) then
with SQL.CreateTable(tnTable) do
begin
AddField(fnID, ftInteger, True, True);
AddField(fnText, ftBlob);
AddField(fnDate, ftDateTime);
AddField(fnModify, ftDateTime);
FDataBase.DB.ExecSQL(GetSQL);
EndCreate;
end;
FID := -1;
FText := TStringStream.Create;
FDateModify := Now;
FDate := DateOf(Now);
end;
destructor TNoteItem.Destroy;
begin
FText.Free;
inherited;
end;
procedure TNoteItem.Load(ADate: TDate);
var
Table: TSQLiteTable;
begin
with SQL.Select(tnTable) do
begin
AddField(fnID);
AddField(fnText);
AddField(fnDate);
AddField(fnModify);
WhereFieldEqual(fnDate, DateOf(ADate));
Table := FDataBase.DB.Query(GetSQL);
EndCreate;
Text.Clear;
if Table.RowCount > 0 then
begin
ID := Table.FieldAsInteger(0);
Text.WriteString(Table.FieldAsBlobText(1));
Date := Table.FieldAsDateTime(2);
DateModify := Table.FieldAsDateTime(3);
FLoaded := True;
end
else
begin
with SQL.InsertInto(tnTable) do
begin
Date := DateOf(ADate);
DateModify := Now;
AddValue(fnDate, Date);
AddValue(fnModify, DateModify);
AddValue(fnText, '');
FDataBase.DB.ExecSQL(GetSQL);
ID := FDataBase.DB.LastInsertRowID;
FLoaded := True;
EndCreate;
end;
end;
Table.Free;
end;
end;
procedure TNoteItem.Save;
begin
if FLoaded then
begin
with SQL.Update(tnTable) do
begin
DateModify := Now;
AddValue(fnModify, DateModify);
WhereFieldEqual(fnID, ID);
FDataBase.DB.ExecSQL(GetSQL);
EndCreate;
end;
Text.Position := 0;
with SQL.UpdateBlob(tnTable) do
begin
BlobField := fnText;
WhereFieldEqual(fnID, ID);
FDataBase.DB.UpdateBlob(GetSQL, Text);
EndCreate;
end;
end;
end;
procedure TNoteItem.SetDataBase(const Value: TDB);
begin
FDataBase := Value;
end;
procedure TNoteItem.SetDate(const Value: TDate);
begin
FDate := Value;
end;
procedure TNoteItem.SetDateModify(const Value: TDateTime);
begin
FDateModify := Value;
end;
procedure TNoteItem.SetText(const Value: TStringStream);
begin
FText := Value;
end;
procedure TNoteItem.SetID(const Value: Integer);
begin
FID := Value;
end;
{ TActualNotes }
constructor TActualNotes.Create(ADataBase: TDB; ATableEx: TTableEx);
begin
inherited Create(ATableEx);
FDataBase := ADataBase;
end;
destructor TActualNotes.Destroy;
begin
Clear;
inherited;
end;
procedure TActualNotes.Reload(ADate: TDate);
var
Table: TSQLiteTable;
Item: TNoteRecord;
begin
BeginUpdate;
Clear;
with SQL.Select(TNoteItem.tnTable) do
begin
AddField(TNoteItem.fnID);
AddField(TNoteItem.fnDate);
AddField(TNoteItem.fnModify);
AddField(TNoteItem.fnText);
WhereFieldBetween(TNoteItem.fnDate, ADate - 30, ADate + 30);
WhereFieldIsNotNull(TNoteItem.fnText);
Table := FDataBase.DB.GetTable(GetSQL);
EndCreate;
while not Table.EOF do
begin
if not Table.FieldAsBlobText(3).IsEmpty then
begin
Item.ID := Table.FieldAsInteger(0);
Item.Date := Table.FieldAsDateTime(1);
Item.DateModify := Table.FieldAsDateTime(2);
Add(Item);
end;
Table.Next;
end;
Table.Free;
end;
EndUpdate;
end;
end.