-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrpdataset.pas
373 lines (350 loc) · 8.4 KB
/
rpdataset.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
{*******************************************************}
{ }
{ Report Manager }
{ }
{ rpdataset }
{ }
{ }
{ A client dataset with two record buffer }
{ can obtain large amounts of recors without }
{ consuming memory }
{ }
{ Copyright (c) 1994-2019 Toni Martir }
{ }
{ This file is under the MPL license }
{ If you enhace this file you must provide }
{ source code }
{ }
{ }
{*******************************************************}
unit rpdataset;
interface
{$I rpconf.inc}
uses Sysutils,Classes,
{$IFDEF FPC}
memds,
{$ENDIF}
{$IFNDEF FPC}
DBClient,
{$ENDIF}
db;
// A client dataset with two record buffer obtaining records from
resourcestring
SRpDatasetActive='Operation not allowed in a open dataset';
SRpInvOpRpdata='Invalid operation for TRpDataset';
type
{$IFDEF FPC}
TRpDataset=class(TMemDataset)
{$ENDIF}
{$IFNDEF FPC}
TRpDataset=class(TClientDataSet)
{$ENDIF}
private
{$IFDEF FPC}
FCopyDataset:TMemDataset;
{$ENDIF}
{$IFNDEF FPC}
FCopyDataset:TClientDataset;
{$ENDIF}
FDataset:TDataset;
procedure SetDataset(Value:TDataset);
protected
procedure DoAfterOpen;override;
procedure Notification(AComponent: TComponent;
Operation: TOperation);override;
public
procedure DoOpen;
procedure DoClose;
procedure DoNext;
procedure DoPrior;
constructor Create(AOwner:TComponent);override;
published
property Dataset:TDataset read FDataset write SetDataset;
end;
implementation
// Assign field source to destinaton (Assign not works well in Delphi 7)
// The bug appears when you edit a already assigned record and assign
// the blobfield, it clears the preceder field
// Sample4.rep, uses biolife.gdb and reproduce the bug in Delphi7
procedure AssignField(Source,Destination:TField);
{$IFDEF BLOBSTREAMBUG}
var
bwstream:TStream;
brstream:TStream;
memstream:TMemoryStream;
nvalue:string;
{$ENDIF}
begin
{$IFNDEF BLOBSTREAMBUG}
if Source.Datatype=ftLargeInt then
TLargeIntField(Destination).AsLargeInt:=TLargeIntField(Source).AsLargeInt
else
Destination.Assign(Source);
{$ENDIF}
{$IFDEF BLOBSTREAMBUG}
if (Destination is TMemoField) then
begin
nvalue := Source.AsString;
Destination.AsString := nvalue;
end
else
if (Destination is TBlobField) then
begin
Destination.Clear;
bwstream:=Destination.DataSet.CreateBlobStream(Destination,bmWrite);
try
brstream:=Source.DataSet.CreateBlobStream(Source,bmRead);
try
memstream:=TMemoryStream.Create;
try
memstream.SetSize(brStream.Size);
brStream.Read(memstream.memory^,memstream.Size);
memstream.Seek(0,soFromBeginning);
bwStream.Write(memstream.memory^,memstream.size);
finally
memstream.free;
end;
finally
brstream.Free;
end;
finally
bwstream.free;
end;
end
else
begin
if Source.Datatype=ftLargeInt then
TLargeIntField(Destination).AsLargeInt:=TLargeIntField(Source).AsLargeInt
else
Destination.Assign(Source);
end;
{$ENDIF}
end;
constructor TRpDataSet.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
{$IFDEF FPC}
FCopyDataset:=TMemDataset.Create(Self);
{$ENDIF}
{$IFNDEF FPC}
FCopyDataset:=TCLientDataset.Create(Self);
{$ENDIF}
end;
procedure TRpDataSet.DoOpen;
var
i:integer;
adef:TFieldDef;
asize:integer;
docreate:boolean;
adatatype:TFieldType;
begin
if Assigned(FDataset) then
begin
DisableControls;
try
// Compare field types and names
docreate:=false;
if ((not active) or (FieldDefs.Count<>FDataset.FieldCount)) then
docreate:=true
else
begin
for i:=0 to FDataset.FieldCount-1 do
begin
adef:=FieldDefs.Items[i];
if adef.Name<>FDataset.Fields[i].FieldName then
begin
docreate:=true;
break;
end;
adatatype:=FDataset.Fields[i].DataType;
if aDataType=ftAutoInc then
aDataType:=ftInteger;
if aDataType<>adef.DataType then
begin
docreate:=true;
break;
end;
aSize:=FDataset.Fields[i].Size;
{$IFNDEF USEVARIANTS}
// A bug in Dephi 5 about WideString and TClientDataset
if FDataset.Fields[i].DataType=ftWideString then
aSize:=aSize*2;
{$ENDIF}
if asize<>adef.Size then
begin
docreate:=true;
break;
end;
end;
end;
if docreate then
begin
Close;
// FDataset.FieldDefs.Update;
// FieldDefs.Assign(FDataset.FieldDefs);
FieldDefs.Clear;
for i:=0 to FDataset.FieldCount-1 do
begin
adef:=FieldDefs.AddFieldDef;
adef.Name:=FDataset.Fields[i].FieldName;
adef.DataType:=FDataset.Fields[i].DataType;
// A bug in Delphi 6- ADO Autoinc fields
if adef.DataType=ftAutoInc then
adef.DataType:=ftInteger;
adef.Size:=FDataset.Fields[i].Size;
{$IFNDEF USEVARIANTS}
// A bug in Dephi 5 about WideString and TClientDataset
if FDataset.Fields[i].DataType=ftWideString then
adef.Size:=adef.Size*2;
{$ENDIF}
{$IFDEF USEBCD}
if (FDataset.Fields[i] is TBCDField) then
adef.Precision:=TBCDField(FDataset.Fields[i]).Precision;
{$ENDIF}
end;
{$IFDEF FPC}
CreateTable;
{$ENDIF}
{$IFNDEF FPC}
CreateDataset;
{$ENDIF}
FCopyDataset.Close;
FCopyDataset.FieldDefs.Assign(FieldDefs);
{$IFDEF FPC}
FCopyDataset.CreateTable;
{$ENDIF}
{$IFNDEF FPC}
FCopyDataset.CreateDataSet;
FCopyDataset.LogChanges:=false;
{$ENDIF}
end
else
begin
First;
while Not Eof do
Delete;
FCopyDataset.First;
while Not FCopyDataset.Eof do
FCopyDataset.Delete;
DoAfterOpen;
end;
if Not FDataset.Eof then
begin
Append;
try
for i:=0 to FDataset.FieldCount-1 do
begin
AssignField(FDataset.Fields[i],Fields[i]);
end;
Post;
except
Cancel;
Raise;
end;
end;
finally
enablecontrols;
end;
end;
end;
procedure TRpDataSet.DoClose;
begin
FCopyDataset.Close;
Close;
end;
procedure TRpDataSet.DoNext;
var
i:integer;
doappend:boolean;
begin
if Assigned(FDataset) then
begin
if (Not ((recordcount>1) and (RecNo=1))) then
FDataset.Next;
// Copy the record to the copy
if Not FDataset.Eof then
begin
disablecontrols;
try
doappend:=true;
if recordcount>1 then
begin
if RecNo=1 then
begin
Next;
// Exit because a prior sentence has been done before
exit;
end;
doappend:=false;
FCopyDataset.Edit;
for i:=0 to FDataset.FieldCount-1 do
begin
AssignField(Fields[i],FCopyDataset.Fields[i]);
end;
FCopyDataset.Post;
First;
Edit;
for i:=0 to FDataset.FieldCount-1 do
begin
AssignField(FCopyDataset.Fields[i],Fields[i]);
end;
Post;
Last;
end;
if doappend then
Append
else
Edit;
try
for i:=0 to FDataset.FieldCount-1 do
begin
AssignField(FDataset.Fields[i],Fields[i]);
end;
Post;
except
Cancel;
Raise;
end;
finally
enablecontrols;
end;
end
else
begin
Next;
end;
end;
end;
procedure TRpDataSet.DoPrior;
begin
if RecordCount<2 then
Raise Exception.Create(SRpInvOpRpdata);
if Recno<>2 then
Raise Exception.Create(SRpInvOpRpdata);
Prior;
end;
procedure TRpDataset.SetDataset(Value:TDataset);
begin
// if Active then
// Raise Exception.Create(SRpDatasetActive);
FDataset:=Value;
if Not Assigned(FDataset) then
Active:=False;
end;
procedure TRpDataset.Notification(AComponent: TComponent; Operation: TOperation);
begin
if Assigned(FDataset) then
if Operation=opremove then
if AComponent=FDataset then
FDataset:=nil;
inherited Notification(AComponent,Operation);
end;
procedure TRpDataSet.DoAfterOpen;
begin
inherited DoAfterOpen;
{$IFNDEF FPC}
LogChanges:=false;
{$ENDIF}
end;
end.