-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathDataGrabber.Data.ResultSet.pas
327 lines (279 loc) · 7.98 KB
/
DataGrabber.Data.ResultSet.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
{
Copyright (C) 2013-2025 Tim Sinaeve tim.sinaeve@gmail.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
}
{$I DataGrabber.inc}
unit DataGrabber.Data.ResultSet;
{ A (decorator) class composed of a dataset with some extra information. }
interface
uses
System.SysUtils,
Data.DB,
FireDAC.Comp.DataSet, FireDAC.Comp.Client,
Spring.Collections, Spring.Collections.Lists,
DataGrabber.Interfaces;
type
TResultSet = class(TInterfacedObject, IResultSet)
private
FDataSet : TFDMemTable; // reference to a returned TFDMemTable instance
FData : IData;
FConstantFields : IList<TField>;
FEmptyFields : IList<TField>;
FNonEmptyFields : IList<TField>;
FHiddenFields : IList<TField>;
FFavoriteFields : IList<TField>;
FConstantFieldsVisible : Boolean;
FEmptyFieldsVisible : Boolean;
{$REGION 'property access methods'}
function GetConstantFields: IList<TField>;
function GetConstantFieldsVisible: Boolean;
function GetEmptyFields: IList<TField>;
function GetEmptyFieldsVisible: Boolean;
function GetHiddenFields: IList<TField>;
function GetNonEmptyFields: IList<TField>;
function GetShowFavoriteFieldsOnly: Boolean;
procedure SetConstantFieldsVisible(const Value: Boolean);
procedure SetEmptyFieldsVisible(const Value: Boolean);
procedure SetShowFavoriteFieldsOnly(const Value: Boolean);
function GetDataSet: TFDDataSet;
function GetData: IData;
{$ENDREGION}
procedure FDataBeforeExecute(Sender: TObject);
protected
procedure UpdateFieldLists;
function ShowAllFields: Boolean;
procedure InitFields(ADataSet: TDataSet);
procedure InitField(AField: TField);
public
constructor Create(
AData : IData;
AFDDataSetReference : IFDDataSetReference
);
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
property DataSet: TFDDataSet
read GetDataSet;
property ConstantFields: IList<TField>
read GetConstantFields;
property EmptyFields: IList<TField>
read GetEmptyFields;
property NonEmptyFields: IList<TField>
read GetNonEmptyFields;
property HiddenFields: IList<TField>
read GetHiddenFields;
property ConstantFieldsVisible: Boolean
read GetConstantFieldsVisible write SetConstantFieldsVisible;
property EmptyFieldsVisible: Boolean
read GetEmptyFieldsVisible write SetEmptyFieldsVisible;
property ShowFavoriteFieldsOnly: Boolean
read GetShowFavoriteFieldsOnly write SetShowFavoriteFieldsOnly;
end;
implementation
uses
Spring,
DDuce.Logger;
{$REGION 'construction and destruction'}
procedure TResultSet.AfterConstruction;
begin
inherited AfterConstruction;
FConstantFields := TCollections.CreateObjectList<TField>(False);
FEmptyFields := TCollections.CreateObjectList<TField>(False);
FNonEmptyFields := TCollections.CreateObjectList<TField>(False);
FFavoriteFields := TCollections.CreateObjectList<TField>(False);
FHiddenFields := TCollections.CreateObjectList<TField>(False);
FConstantFieldsVisible := True;
FEmptyFieldsVisible := True;
UpdateFieldLists;
end;
procedure TResultSet.BeforeDestruction;
begin
Logger.Track(Self, 'BeforeDestruction');
FData.OnBeforeExecute.Remove(FDataBeforeExecute);
FData := nil;
FreeAndNil(FDataSet);
inherited BeforeDestruction;
end;
constructor TResultSet.Create(AData: IData; AFDDataSetReference:
IFDDataSetReference);
begin
Logger.Track(Self, 'Create');
inherited Create;
Guard.CheckNotNull(AData, 'AData');
Guard.CheckNotNull(AFDDataSetReference, 'AFDDataSetReference');
FData := AData;
FData.OnBeforeExecute.Add(FDataBeforeExecute);
FDataSet := TFDMemTable.Create(nil);;
FDataSet.Data := AFDDataSetReference;
end;
{$ENDREGION}
{$REGION 'property access methods'}
function TResultSet.GetData: IData;
begin
Result := FData;
end;
function TResultSet.GetDataSet: TFDDataSet;
begin
Result := FDataSet;
end;
function TResultSet.GetConstantFields: IList<TField>;
begin
Result := FConstantFields;
end;
function TResultSet.GetEmptyFields: IList<TField>;
begin
Result := FEmptyFields;
end;
function TResultSet.GetHiddenFields: IList<TField>;
begin
Result := FHiddenFields;
end;
function TResultSet.GetNonEmptyFields: IList<TField>;
begin
Result := FNonEmptyFields;
end;
function TResultSet.GetEmptyFieldsVisible: Boolean;
begin
Result := FEmptyFieldsVisible;
end;
procedure TResultSet.SetEmptyFieldsVisible(const Value: Boolean);
begin
if Value <> EmptyFieldsVisible then
begin
FEmptyFieldsVisible := Value;
UpdateFieldLists;
InitFields(DataSet);
end;
end;
function TResultSet.GetShowFavoriteFieldsOnly: Boolean;
begin
Result := False;
end;
procedure TResultSet.SetShowFavoriteFieldsOnly(const Value: Boolean);
begin
// TODO
end;
function TResultSet.GetConstantFieldsVisible: Boolean;
begin
Result := FConstantFieldsVisible;
end;
procedure TResultSet.SetConstantFieldsVisible(const Value: Boolean);
begin
if Value <> ConstantFieldsVisible then
begin
FConstantFieldsVisible := Value;
UpdateFieldLists;
InitFields(DataSet);
end;
end;
{$ENDREGION}
{$REGION 'event handlers'}
procedure TResultSet.FDataBeforeExecute(Sender: TObject);
begin
FConstantFields.Clear;
FEmptyFields.Clear;
FNonEmptyFields.Clear;
FHiddenFields.Clear;
FFavoriteFields.Clear;
FConstantFieldsVisible := True;
FEmptyFieldsVisible := True;
end;
{$ENDREGION}
{$REGION 'protected methods'}
procedure TResultSet.InitField(AField: TField);
var
B : Boolean;
begin
B := True;
// if ShowFavoriteFieldsOnly then
// B := FFavoriteFields.Contains(AField);
if B and not ConstantFieldsVisible then
B := not FConstantFields.Contains(AField);
if B and not EmptyFieldsVisible then
B := not FEmptyFields.Contains(AField);
if not B and not FHiddenFields.Contains(AField) then
FHiddenFields.Add(AField);
B := not FHiddenFields.Contains(AField);
AField.Visible := B;
end;
procedure TResultSet.InitFields(ADataSet: TDataSet);
var
Field : TField;
begin
for Field in ADataSet.Fields do
begin
InitField(Field);
end;
end;
function TResultSet.ShowAllFields: Boolean;
var
F : TField;
begin
Result := False;
DataSet.DisableControls;
try
FConstantFieldsVisible := True;
FEmptyFieldsVisible := True;
for F in DataSet.Fields do
begin
if not F.Visible then
begin
F.Visible := True;
Result := True;
end;
end;
FHiddenFields.Clear;
finally
DataSet.EnableControls;
end;
end;
procedure TResultSet.UpdateFieldLists;
var
S : string;
T : string;
F : TField;
LIsEmpty : Boolean;
LIsConst : Boolean;
begin
Logger.Track(Self, 'UpdateFieldLists');
DataSet.DisableControls;
FConstantFields.Clear;
FEmptyFields.Clear;
FNonEmptyFields.Clear;
try
if DataSet.FindFirst then
begin
for F in DataSet.Fields do
begin
// constant fields
DataSet.FindFirst;
S := F.AsString;
LIsConst := True;
LIsEmpty := F.IsNull or F.AsString.IsEmpty;
while (LIsConst or LIsEmpty) and DataSet.FindNext do
begin
T := F.AsString;
LIsConst := LIsConst and (S = T);
LIsEmpty := LIsEmpty and (F.IsNull or T.IsEmpty);
end;
if LIsConst then
FConstantFields.Add(F);
if LIsEmpty then
FEmptyFields.Add(F)
else
FNonEmptyFields.Add(F);
end;
end;
finally
DataSet.EnableControls;
end;
end;
{$ENDREGION}
end.