-
Notifications
You must be signed in to change notification settings - Fork 58
/
DSContainer.pas
338 lines (295 loc) · 11.7 KB
/
DSContainer.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
328
329
330
331
332
333
334
335
336
337
{***************************************************************}
{ FIBPlus - component library for direct access to Firebird and }
{ InterBase databases }
{ }
{ FIBPlus is based in part on the product }
{ Free IB Components, written by Gregory H. Deatz for }
{ Hoagland, Longo, Moran, Dunst & Doukas Company. }
{ mailto:[email protected] }
{ }
{ Copyright (c) 1998-2007 Devrace Ltd. }
{ Written by Serge Buzadzhy ([email protected]) }
{ }
{ ------------------------------------------------------------- }
{ FIBPlus home page: http://www.fibplus.com/ }
{ FIBPlus support : http://www.devrace.com/support/ }
{ ------------------------------------------------------------- }
{ }
{ Please see the file License.txt for full license information }
{***************************************************************}
unit DSContainer;
interface
{$I FIBPlus.inc}
uses
SysUtils, Classes, DB,pFIBDataInfo,FIBDataSet;
type
TKindDataSetEvent = ( deBeforeOpen , deAfterOpen,deBeforeClose ,
deAfterClose,deBeforeInsert,deAfterInsert,deBeforeEdit,deAfterEdit,
deBeforePost,deAfterPost,deBeforeCancel,deAfterCancel,deBeforeDelete,
deAfterDelete,deBeforeScroll,deAfterScroll,deOnNewRecord,deOnCalcFields,
deBeforeRefresh,deAfterRefresh
);
TKindDataSetError=(
deOnEditError,deOnPostError,deOnDeleteError
);
TOnApplyDefaultValue = procedure(DataSet:TDataSet; Field:TField; var Applied:boolean) of object;
TOnApplyFieldRepository=procedure(DataSet:TDataSet;Field:TField;FieldInfo:TpFIBFieldInfo) of object;
TOnDataSetEvent =procedure (DataSet:TDataSet;Event:TKindDataSetEvent) of object;
TOnDataSetError =procedure (DataSet:TDataSet;Event:TKindDataSetError;
E: EDatabaseError; var Action: TDataAction
) of object;
TOnCompareFieldValues=
function (Field:TField;const S1,S2:variant;var Compared:boolean ):integer of object;
{$IFDEF USE_DEPRECATE_METHODS2}
TUserEvent =procedure (Sender:TObject;Receiver:TDataSet;const EventName:string;
var Info :string
) of object;
{$ENDIF}
TDataSetsContainer = class;
TDataSetsContainer = class(TComponent)
private
FActive :boolean;
FMasterContainer:TDataSetsContainer;
FOnDataSetEvent:TOnDataSetEvent;
FOnApplyDefaultValue:TOnApplyDefaultValue;
FOnApplyFieldRepository:TOnApplyFieldRepository;
FOnDataSetError:TOnDataSetError;
FOnCompareFieldValues:TOnCompareFieldValues;
FOnReadBlobField:TOnBlobFieldProcessing;
FOnWriteBlobField:TOnBlobFieldProcessing;
{$IFDEF USE_DEPRECATE_METHODS2}
FOnUserEvent :TUserEvent;
{$ENDIF}
vDataSetsList :TList;
FIsGlobalContainer:boolean;
procedure SetMasterContainer(Value:TDataSetsContainer);
procedure CheckCircularMaster(Value:TDataSetsContainer);
procedure SetIsGlobalContainer(const Value: boolean);
protected
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
public
constructor Create(AOwner:Tcomponent);override;
destructor Destroy;override;
procedure AddDataSet(Value:TDataSet);
procedure RemoveDataSet(Value:TDataSet);
{$IFDEF USE_DEPRECATE_METHODS2}
procedure NotifyDataSets(Sender:TObject;const UDA,UDE:string;var Info :string); dynamic; deprecated;
// (UDA - user defined address)
// (UDE - user defined Event)
{$ENDIF}
procedure DataSetEvent(DataSet:TDataSet;Event:TKindDataSetEvent);
procedure DoOnApplyDefaultValue(DataSet:TDataSet;Field:TField; var Applied:boolean);
procedure DoOnApplyFieldRepository(DataSet:TDataSet;Field:TField;FieldInfo:TpFIBFieldInfo);
procedure DataSetError(DataSet:TDataSet;Event:TKindDataSetError;
E: EDatabaseError; var Action: TDataAction
);
function DataSetCompareFieldValues(DataSet:TDataSet;
Field:TField;const S1,S2:variant;var Res:integer
):boolean;
procedure DoOnReadBlobField(Field:TBlobField;BlobSize:integer;Progress:integer;var Stop:boolean);
procedure DoOnWriteBlobField(Field:TBlobField;BlobSize:integer;Progress:integer;var Stop:boolean);
{$IFDEF USE_DEPRECATE_METHODS2}
procedure UserEvent(Sender:TObject;Receiver:TDataSet;const EventName:string; var Info :string); deprecated;
{$ENDIF}
function DataSetCount:integer;
function DataSet(Index:integer):TDataSet;
published
property Active:boolean read FActive write FActive default true;
property OnDataSetEvent :TOnDataSetEvent read FOnDataSetEvent write FOnDataSetEvent;
property OnDataSetError :TOnDataSetError read FOnDataSetError write FOnDataSetError;
{$IFDEF USE_DEPRECATE_METHODS2}
property OnUserEvent :TUserEvent read FOnUserEvent write FOnUserEvent;
{$ENDIF}
property OnApplyDefaultValue:TOnApplyDefaultValue read FOnApplyDefaultValue write FOnApplyDefaultValue;
property OnApplyFieldRepository:TOnApplyFieldRepository read FOnApplyFieldRepository write FOnApplyFieldRepository;
property MasterContainer:TDataSetsContainer read FMasterContainer write SetMasterContainer;
property OnCompareFieldValues:TOnCompareFieldValues read FOnCompareFieldValues write FOnCompareFieldValues;
property IsGlobalContainer:boolean read FIsGlobalContainer write SetIsGlobalContainer default false;
property OnReadBlobField :TOnBlobFieldProcessing read FOnReadBlobField write FOnReadBlobField;
property OnWriteBlobField :TOnBlobFieldProcessing read FOnWriteBlobField write FOnWriteBlobField;
end;
function GlobalContainer:TDataSetsContainer;
implementation
uses
{$IFDEF D_XE3}
System.Types, // for inline funcs
{$ENDIF}
StrUtil,FIBConsts;
threadvar
vGlobalContainer:TDataSetsContainer;
function GlobalContainer:TDataSetsContainer;
begin
Result:=vGlobalContainer
end;
constructor TDataSetsContainer.Create(AOwner:Tcomponent);//override;
begin
inherited Create(AOwner);
FActive :=true;
FMasterContainer :=nil;
vDataSetsList :=TList.Create;
end;
destructor TDataSetsContainer.Destroy;//override;
begin
inherited Destroy;
vDataSetsList.Free;
end;
procedure TDataSetsContainer.Notification(AComponent: TComponent; Operation: TOperation); //override;
begin
if (Operation=opRemove) then
begin
if AComponent<>FMasterContainer then vDataSetsList.Remove(AComponent);
if AComponent=FMasterContainer then FMasterContainer:=nil;
end;
inherited Notification(AComponent,Operation)
end;
procedure TDataSetsContainer.DataSetEvent(DataSet:TDataSet;Event:TKindDataSetEvent);
begin
if Assigned(FMasterContainer) then FMasterContainer.DataSetEvent(DataSet,Event);
if Active then
begin
if Assigned(FOnDataSetEvent) then FOnDataSetEvent(DataSet,Event)
end;
end;
procedure TDataSetsContainer.DoOnApplyDefaultValue(DataSet:TDataSet;Field:TField; var Applied:boolean);
begin
if Assigned(FMasterContainer) then FMasterContainer.DoOnApplyDefaultValue(DataSet,Field,Applied);
if Active then
begin
if Assigned(FOnApplyDefaultValue) then FOnApplyDefaultValue(DataSet,Field,Applied)
end;
end;
procedure TDataSetsContainer.DataSetError(DataSet:TDataSet;Event:TKindDataSetError;
E: EDatabaseError; var Action: TDataAction);
begin
if Assigned(FMasterContainer) then FMasterContainer.DataSetError(DataSet,Event,E,Action);
if Active then
begin
if Assigned(FOnDataSetError) then FOnDataSetError(DataSet,Event,E,Action)
end;
end;
{$IFDEF USE_DEPRECATE_METHODS2}
procedure TDataSetsContainer.UserEvent(Sender:TObject;Receiver:TDataSet;const EventName:string;var Info:string);
begin
if Assigned(FMasterContainer) then FMasterContainer.UserEvent(Sender,Receiver,EventName,Info);
if Active then
begin
if Assigned(FOnUserEvent) then FOnUserEvent(Sender,Receiver,EventName,Info)
end;
end;
{$ENDIF}
procedure TDataSetsContainer.SetMasterContainer(Value:TDataSetsContainer);
var i:integer;
begin
CheckCircularMaster(Value);
if FMasterContainer<>nil then
for i:=0 to Pred(vDataSetsList.Count) do
FMasterContainer.RemoveDataSet(TDataSet(vDataSetsList[i]));
FMasterContainer:=Value;
if Value<>nil then
begin
Value.FreeNotification(Self);
for i:=0 to Pred(vDataSetsList.Count) do
Value.AddDataSet(TDataSet(vDataSetsList[i]))
end;
end;
procedure TDataSetsContainer.CheckCircularMaster(Value:TDataSetsContainer);
var CurContainer:TDataSetsContainer;
begin
if Value = Self then raise Exception.Create(SFIBErrorCircularLinks);
CurContainer:=Value;
while CurContainer<>nil do
if CurContainer.MasterContainer=Self then raise Exception.Create(SFIBErrorCircularLinks)
else CurContainer:=CurContainer.MasterContainer;
end;
procedure TDataSetsContainer.AddDataSet(Value:TDataSet);
begin
if Value<>nil then
begin
if vDataSetsList.IndexOf(Value)=-1 then vDataSetsList.Add(Value);
if MasterContainer<>nil then MasterContainer.AddDataSet(Value);
Value.FreeNotification(Self)
end;
end;
procedure TDataSetsContainer.RemoveDataSet(Value:TDataSet);
begin
if vDataSetsList.IndexOf(Value)<>-1 then vDataSetsList.Remove(Value);
if MasterContainer<>nil then MasterContainer.RemoveDataSet(Value);
end;
{$IFDEF USE_DEPRECATE_METHODS2}
procedure TDataSetsContainer.NotifyDataSets(Sender:TObject;const UDA,UDE:string;var Info :string);
var i:integer;
begin
for i:=0 to Pred(vDataSetsList.Count) do
begin
if (UDA='') or
WildStringCompare(
FastUpperCase(TDataSet(vDataSetsList[i]).Owner.Name+'.'+TDataSet(vDataSetsList[i]).Name),
FastUpperCase(UDA)
)
then
if (TDataSet(vDataSetsList[i]) is TpFibDataSet) then
with TpFibDataSet(vDataSetsList[i]) do
if (ReceiveEvents.Count=0) or (WldIndexOf(ReceiveEvents,UDE,false)<>-1 )then
DoUserEvent(Sender,UDE,Info)
end;
end;
{$ENDIF}
function TDataSetsContainer.DataSetCount:integer;
begin
Result :=vDataSetsList.Count
end;
function TDataSetsContainer.DataSet(Index:integer):TDataSet;
begin
if (Index<vDataSetsList.Count) and (Index>-1) then
Result :=TDataSet(vDataSetsList[Index])
else
Result :=nil
end;
function TDataSetsContainer.DataSetCompareFieldValues(DataSet: TDataSet;
Field: TField; const S1, S2: variant; var Res:integer): boolean;
begin
if Assigned(FOnCompareFieldValues) then
Res:=FOnCompareFieldValues(Field,S1, S2,Result)
else
Result := False;
end;
procedure TDataSetsContainer.SetIsGlobalContainer(const Value: boolean);
begin
FIsGlobalContainer := Value;
if not (csDesigning in ComponentState)then
begin
if Value then
begin
if Assigned(vGlobalContainer) and (vGlobalContainer<>Self) then
vGlobalContainer.FIsGlobalContainer:=False;
vGlobalContainer:=Self
end
else
if not Value and (vGlobalContainer=Self) then
vGlobalContainer:=nil
end;
end;
procedure TDataSetsContainer.DoOnApplyFieldRepository(DataSet: TDataSet;
Field: TField; FieldInfo: TpFIBFieldInfo);
begin
if Assigned(FMasterContainer) then FMasterContainer.DoOnApplyFieldRepository(DataSet,Field,FieldInfo);
if Active then
begin
if Assigned(FOnApplyFieldRepository) then FOnApplyFieldRepository(DataSet,Field,FieldInfo)
end;
end;
procedure TDataSetsContainer.DoOnReadBlobField(Field: TBlobField; BlobSize,
Progress: integer; var Stop: boolean);
begin
if Assigned(FOnReadBlobField) then
FOnReadBlobField(Field,BlobSize,Progress,Stop)
end;
procedure TDataSetsContainer.DoOnWriteBlobField(Field: TBlobField;
BlobSize, Progress: integer; var Stop: boolean);
begin
if Assigned(FOnWriteBlobField) then
FOnWriteBlobField(Field,BlobSize,Progress,Stop)
end;
initialization
vGlobalContainer:=nil;
end.