-
Notifications
You must be signed in to change notification settings - Fork 58
/
pFIBClientDataSet.pas
714 lines (617 loc) · 17.7 KB
/
pFIBClientDataSet.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
{***************************************************************}
{ 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 pFIBClientDataSet;
interface
{$I FIBPlus.inc}
uses
pFIBInterfaces, SysUtils, Classes,
Db, DbConsts, DBClient, Provider, DSIntf, MidConst
{$IFDEF D6+},FMTBcd, Variants{$ENDIF};
{$T-,H+,X+}
type
TOnApplyUpdateKind =(aukBefore,aukAfter);
TpFIBClientDataSet = class(TClientDataSet,ISQLObject)
protected
function GetFieldClass(FieldType: TFieldType): TFieldClass; override;
procedure DataConvert(Field: TField; Source, Dest: Pointer; ToNative: Boolean); override;
//ISQLObject
function ParamCount:integer;
function ParamName(ParamIndex:integer):string;
function FieldsCount:integer;
function FieldExist(const FieldName:string; var FieldIndex:integer):boolean;
function ParamExist(const ParamName:string; var ParamIndex:integer):boolean;
function FieldValue(const FieldName:string; Old:boolean):variant; overload;
function FieldValue(const FieldIndex:integer;Old:boolean):variant; overload;
function ParamValue(const ParamName:string):variant; overload;
function ParamValue(const ParamIndex:integer):variant; overload;
procedure SetParamValue(const ParamIndex:integer; aValue:Variant);
function FieldName(FieldIndex:integer):string;
function IEof:boolean;
procedure INext;
//
public
procedure OpenWP(ParamValues: array of Variant);
procedure Commit;
procedure RollBack;
function TransactionIsActive:boolean;
end;
TpFIBClientBCDField=class (TBCDField)
private
protected
function GetAsCurrency: Currency; override;
function GetAsFloat: Double; override;
{$IFNDEF NO_USE_COMP}
function GetAsComp : Comp ; virtual;
procedure SetAsComp(Value: comp); virtual;
{$ENDIF}
function GetAsExtended: Extended; {$IFNDEF D2009+} virtual{$ELSE} override {$ENDIF};
function GetAsString: string; override;
function GetAsInt64: Int64; virtual;
function GetAsVariant: Variant; override;
procedure SetAsFloat(Value: Double); override;
procedure SetAsExtended(Value: Extended); {$IFNDEF D2009+} virtual{$ELSE} override {$ENDIF};
procedure SetAsString(const Value: string); override;
procedure SetAsCurrency(Value: Currency); override;
procedure SetAsInt64(Value: Int64); virtual;
procedure SetVarValue(const Value: Variant); override;
procedure GetText(var Text: string; DisplayText: Boolean); override;
public
procedure Assign(Source: TPersistent); override;
property AsExtended :Extended read GetAsExtended write SetAsExtended;
property AsInt64 :Int64 read GetAsInt64 write SetAsInt64;
{$IFNDEF NO_USE_COMP}
property AsComp :Comp read GetAsComp write SetAsComp;
{$ENDIF}
property Value :Extended read GetAsExtended write SetAsExtended;
end;
TpFIBDataSetProvider = class(TDataSetProvider)
protected
function FindRecord(Source, Delta: TDataSet; UpdateMode: TUpdateMode): Boolean; {$IFDEF D2005+}override;{$ENDIF}
procedure UpdateRecord(Source, Delta: TDataSet; BlobsOnly, KeyOnly: Boolean); override;
function CreateResolver: TCustomResolver; override;
end;
procedure Register;
implementation
{$R fibplus_midas.dcr}
uses StdFuncs;
type
TpFIBDataSetResolver = class(TDataSetResolver)
private
procedure PutRecord(Tree: TUpdateTree);
protected
procedure InternalBeforeResolve(Tree: TUpdateTree); override;
procedure DoUpdate(Tree: TUpdateTree); override;
procedure DoDelete(Tree: TUpdateTree); override;
procedure DoInsert(Tree: TUpdateTree); override;
end;
TpFIBSQLResolver = class(TSQLResolver)
protected
end;
{ TpFIBClientDataSet }
procedure TpFIBClientDataSet.Commit;
var
DummyOwnerData: OleVariant;
DummyParams : OleVariant;
begin
if Assigned(AppServer) then
AppServer.AS_Execute(ProviderName, 'FIB$COMMIT', DummyParams, DummyOwnerData);
end;
procedure TpFIBClientDataSet.RollBack;
var
DummyOwnerData: OleVariant;
DummyParams : OleVariant;
begin
if Assigned(AppServer) then
AppServer.AS_Execute(ProviderName, 'FIB$ROLLBACK', DummyParams, DummyOwnerData);
end;
function TpFIBClientDataSet.TransactionIsActive: boolean;
var
DummyOwnerData: OleVariant;
Params : OleVariant;
v:Variant;
begin
if Assigned(AppServer) then
begin
AppServer.AS_Execute(ProviderName, 'FIB$GET_INTRANSACTION', Params, DummyOwnerData);
v:=Params[0];
Result:=v[1];
end
else
Result:=False
end;
procedure TpFIBClientDataSet.DataConvert(Field: TField; Source,
Dest: Pointer; ToNative: Boolean);
var
Scale :byte;
begin
if (Field.DataType<>ftBCD) or (Field.Size=4)
then
inherited DataConvert(Field,Source, Dest,ToNative)
else
if ToNative then
Int64ToBCD(Int64(Source^), Field.Size, TBcd(Dest^))
else
if not BCDToInt64WithScale(TBcd(Source^), Int64(Dest^),Scale ) then
raise EOverFlow.CreateFmt(SFieldOutOfRange, [Field.DisplayName]);
end;
function TpFIBClientDataSet.FieldExist(const FieldName: string;
var FieldIndex: integer): boolean;
var
tf:TField;
begin
tf:=FindField(FieldName);
Result:= Assigned(tf);
if Result then
FieldIndex:=tf.Index;
end;
function TpFIBClientDataSet.FieldName(FieldIndex: integer): string;
begin
Result:=Fields[FieldIndex].FieldName;
end;
function TpFIBClientDataSet.FieldsCount: integer;
begin
Result:=FieldCount
end;
function TpFIBClientDataSet.FieldValue(const FieldName: string;
Old: boolean): variant;
var
tf:TField;
begin
tf:=FieldByName(FieldName);
if Old then
Result:= tf.OldValue
else
Result:= tf.Value
end;
function TpFIBClientDataSet.FieldValue(const FieldIndex: integer;
Old: boolean): variant;
var
tf:TField;
begin
tf:=Fields[FieldIndex];
if Old and (State<>dsInsert) then
Result:= tf.OldValue
else
Result:= tf.Value
end;
function TpFIBClientDataSet.GetFieldClass(
FieldType: TFieldType): TFieldClass;
begin
case FieldType of
ftBCD : Result:=TpFIBClientBCDField;
else
Result:=inherited GetFieldClass( FieldType)
end
end;
function TpFIBClientDataSet.IEof: boolean;
begin
Result:=Eof;
end;
procedure TpFIBClientDataSet.INext;
begin
Next
end;
procedure TpFIBClientDataSet.OpenWP(ParamValues: array of Variant);
var i :integer;
pc:integer;
begin
// Exec Query with ParamValues
if High(ParamValues)<Pred(Params.Count) then
pc:=High(ParamValues)
else
pc:=Pred(Params.Count);
for i:=Low(ParamValues) to pc do
Params[i].Value:=ParamValues[i];
Open
end;
function TpFIBClientDataSet.ParamCount: integer;
begin
Result:=Params.Count
end;
function TpFIBClientDataSet.ParamExist(const ParamName: string;
var ParamIndex: integer): boolean;
var
par:TParam;
begin
par:=Params.FindParam(ParamName);
Result :=par<>nil;
if Result then
ParamIndex:=par.Index
end;
function TpFIBClientDataSet.ParamName(ParamIndex: integer): string;
begin
Result:=Params[ParamIndex].Name;
end;
function TpFIBClientDataSet.ParamValue(const ParamIndex: integer): variant;
begin
Result:=Params[ParamIndex].Value
end;
function TpFIBClientDataSet.ParamValue(const ParamName: string): variant;
begin
Result:=Params.ParamByName(ParamName).Value
end;
procedure TpFIBClientDataSet.SetParamValue(const ParamIndex: integer;
aValue: Variant);
begin
Params[ParamIndex].Value:=aValue;
end;
{ TpFIBClientBCDField }
procedure TpFIBClientBCDField.Assign(Source: TPersistent);
begin
if Source = nil then
Clear
else
if Source is TField then
Value := TField(Source).Value
else
inherited Assign(Source);
end;
{$IFNDEF NO_USE_COMP}
function TpFIBClientBCDField.GetAsComp: Comp;
begin
Result:=GetAsExtended
end;
procedure TpFIBClientBCDField.SetAsComp(Value: comp);
begin
SetData(@Value,False)
end;
{$ENDIF}
function TpFIBClientBCDField.GetAsCurrency: Currency;
begin
Result:=GetAsExtended
end;
function TpFIBClientBCDField.GetAsExtended: Extended;
var C:Int64;
begin
if GetData(@C, False) then
Result:=C*E10[-Size]
else
Result:=0
end;
function TpFIBClientBCDField.GetAsFloat: Double;
var C:Comp;
begin
if GetData(@C, False) then
Result:=C*E10[-Size]
else
Result:=0
end;
function TpFIBClientBCDField.GetAsInt64: Int64;
begin
if GetData(@Result, False) then
if Size<>0 then
Result:=Round(Result*E10[-Size])
else
else
Result:=0
end;
function TpFIBClientBCDField.GetAsString: string;
var C:Int64;
begin
{$IFDEF D_XE3}with FormatSettings do{$ENDIF}
if GetData(@C, False) then
Result := Int64WithScaleToStr(C,Size,DecimalSeparator) else Result := '';
end;
function TpFIBClientBCDField.GetAsVariant: Variant;
begin
case Size of
0:
{$IFDEF D6+}
Result:= asInt64;
{$ELSE}
{$IFDEF NO_USE_COMP}
Result:= asInteger ;
{$ELSE}
Result:= asComp ;
{$ENDIF}
{$ENDIF}
4:
Result:= asCurrency
else
Result:= asExtended
end
end;
procedure TpFIBClientBCDField.GetText(var Text: string;
DisplayText: Boolean);
var
Format: TFloatFormat;
Digits: Integer;
FmtStr: string;
C: Int64;
begin
{$IFDEF D_XE3}with FormatSettings do{$ENDIF}
try
if GetData(@C, False) then
begin
if DisplayText or (EditFormat = '') then
FmtStr := DisplayFormat else
FmtStr := EditFormat;
if FmtStr = '' then
begin
if currency then
begin
Digits := CurrencyDecimals;
if DisplayText then Format := ffCurrency else Format := ffFixed;
Text := CurrToStrF(C*E10[-Size], Format, Digits);
end
else
begin
Digits := Size;
Text := Int64WithScaleToStr(C,Digits,DecimalSeparator)
end;
end
else
if Size=4 then
Text := FormatCurr(FmtStr, C*E10[-Size])
else
begin
{$IFNDEF D6+}
Text := FormatFloat(FmtStr, RoundExtend(C*E10[-Size],Size));
{$ELSE}
Text := FormatNumericString(FmtStr,Int64WithScaleToStr(C,Size,DecimalSeparator));
{$ENDIF}
end;
end
else
Text := '';
except
on E: Exception do
Text := SBCDOverflow;
end;
end;
procedure TpFIBClientBCDField.SetAsCurrency(Value: Currency);
begin
SetAsExtended(Value)
end;
procedure TpFIBClientBCDField.SetAsExtended(Value: Extended);
var
RndComp :Comp;
begin
try
RndComp :=Value*E10[Size];
SetData(@RndComp,False);
except
raise
end;
end;
procedure TpFIBClientBCDField.SetAsFloat(Value: Double);
begin
SetAsExtended(Value);
end;
procedure TpFIBClientBCDField.SetAsInt64(Value: Int64);
begin
SetData(@Value,False)
end;
procedure TpFIBClientBCDField.SetAsString(const Value: string);
begin
if Value = '' then Clear else
if (Size=0) then
SetAsInt64(StrToInt64(Value))
else
SetAsExtended(StrToFloat(Value))
end;
procedure TpFIBClientBCDField.SetVarValue(const Value: Variant);
begin
case VarType(Value) of
varEmpty,varNull : Clear;
varString,varOleStr{$IFDEF D2009+},varUString{$ENDIF} : AsString :=Value;
varSmallint,varInteger{$IFDEF D6+},varWord, varLongWord{$ENDIF}: AsInteger:=Value;
{$IFDEF D6+}
varInt64 : AsInt64 :=Value
{$ENDIF}
else
AsExtended :=Value;
end
end;
{ TpFIBDataSetProvider }
function TpFIBDataSetProvider.CreateResolver: TCustomResolver;
begin
if ResolveToDataSet then
Result := TpFIBDataSetResolver.Create(Self) else
Result := TpFIBSQLResolver.Create(Self);
end;
type TUnprotectedPacketDataSet =class(TPacketDataSet);
function TpFIBDataSetProvider.FindRecord(Source, Delta: TDataSet;
UpdateMode: TUpdateMode): Boolean;
procedure GetFieldList(DataSet: TDataSet; UpdateMode: TUpdateMode; List: TList);
var
i: Integer;
begin
for i := 0 to DataSet.FieldCount - 1 do
with DataSet.Fields[i] do
begin
if (DataType in [ftBytes, ftVarBytes]) or IsBlob or
(DataSet.Fields[i] is TObjectField) then continue;
case UpdateMode of
upWhereKeyOnly:
if pfInKey in ProviderFlags then List.Add(DataSet.Fields[i]);
upWhereAll:
if pfInWhere in ProviderFlags then List.Add(DataSet.Fields[i]);
upWhereChanged:
if (pfInKey in ProviderFlags) or (not VarIsEmpty(NewValue)) then
List.Add(DataSet.Fields[i]);
end;
end;
end;
var
i: Integer;
KeyValues: Variant;
Fields: string;
FieldList: TList;
IsDelta: LongBool;
begin
Result := False;
TUnprotectedPacketDataSet(Delta).DSBase.GetProp(DSPROPISDELTA, @IsDelta);
FieldList := TList.Create;
try
GetFieldList(Delta, UpdateMode, FieldList);
if FieldList.Count > 1 then
begin
KeyValues := VarArrayCreate([0, FieldList.Count - 1], varVariant);
Fields := '';
for i := 0 to FieldList.Count - 1 do
with TField(FieldList[i]) do
begin
if (TField(FieldList[i]) is TBCDField) and
(TBCDField(FieldList[i]).Size<>4)
then
KeyValues[i] := BCDFieldAsString(TField(FieldList[i]),IsDelta)
else
if IsDelta then
KeyValues[i] := OldValue else
KeyValues[i] := Value;
if Fields <> '' then Fields := Fields + ';';
Fields := Fields + FieldName;
end;
Result := Source.Locate(Fields, KeyValues, []);
end
else
if FieldList.Count = 1 then
begin
with TField(FieldList[0]) do
if IsDelta then
Result := Source.Locate(FieldName, OldValue, [])
else
Result := Source.Locate(FieldName, Value, []);
end
else
DatabaseError(SNoKeySpecified);
finally
FieldList.Free;
end;
end;
procedure TpFIBDataSetProvider.UpdateRecord(Source, Delta: TDataSet;
BlobsOnly, KeyOnly: Boolean);
var
Field: TField;
i: Integer;
UseUpMode: TUpdateMode;
BcdValue:TBcd;
begin
if KeyOnly then
UseUpMode := upWhereKeyOnly
else
UseUpMode := UpdateMode;
if not FindRecord(Source, Delta, UseUpMode) then
DatabaseError(SRecordChanged);
begin
if not FindRecord(Source, Delta, upWhereKeyOnly) then
DatabaseError(SRecordChanged);
with Delta do
begin
Edit;
for i := 0 to FieldCount - 1 do
begin
Field := Source.FindField(Fields[i].FieldName);
if (Field <> nil) and (not BlobsOnly or (Field.IsBlob and VarIsNull(Fields[i].NewValue)))
then
if Fields[i].DataType<>ftBcd then
Fields[i].Assign(Field)
else
begin
// main changes:
GetBCDFieldData(Field,False,BcdValue);
Fields[i].SetData(@BcdValue)
end;
end;
Post;
end;
end;
end;
{ TpFIBDataSetResolver }
type TUnprotectedUpdateTree= class(TUpdateTree);
procedure TpFIBDataSetResolver.DoDelete(Tree: TUpdateTree);
begin
with TUnprotectedUpdateTree(Tree) do
begin
if TpFIBDataSetProvider(Provider).FindRecord(Source, Delta, Provider.UpdateMode) then
Source.Delete
else
DatabaseError(SRecordChanged);
end;
end;
procedure TpFIBDataSetResolver.DoInsert(Tree: TUpdateTree);
begin
TUnprotectedUpdateTree(Tree).Source.Append;
PutRecord(Tree);
end;
procedure TpFIBDataSetResolver.DoUpdate(Tree: TUpdateTree);
begin
with TUnprotectedUpdateTree(Tree) do
begin
if not TpFIBDataSetProvider(Provider).FindRecord(Source, Delta, Provider.UpdateMode) then
DatabaseError(SRecordChanged);
Source.Edit;
PutRecord(Tree);
end;
end;
procedure TpFIBDataSetResolver.InternalBeforeResolve(Tree: TUpdateTree);
begin
with TUnprotectedUpdateTree(Tree) do
TpFIBDataSetProvider(Provider).FindRecord(Source, Delta, Provider.UpdateMode);
end;
procedure TpFIBDataSetResolver.PutRecord(Tree: TUpdateTree);
procedure PutField(Src, Dest: TField); forward;
procedure PutObjectField(Src, Dest: TObjectField);
var
i: Integer;
begin
if VarIsNull(Src.NewValue) then
Dest.Clear else
for i := 0 to Src.FieldCount - 1 do
if (not VarIsEmpty(Src.Fields[i].NewValue)) and
(pfInUpdate in Src.Fields[i].ProviderFlags) then
PutField(Src.Fields[i], Dest.Fields[i]);
end;
procedure PutField(Src, Dest: TField);
begin
if (Src.DataType in [ftArray, ftADT]) then
PutObjectField(TObjectField(Src), TObjectField(Dest)) else
if (Src.DataType in [ftDataSet, ftReference]) then
raise Exception.CreateRes(Integer(@SNoDataSets)) else
if (not VarIsEmpty(Src.NewValue)) and
(pfInUpdate in Src.ProviderFlags) then
Dest.Assign(Src);
end;
var
i: Integer;
Field: TField;
begin
with TUnprotectedUpdateTree(Tree) do
try
for i := 0 to Delta.FieldCount - 1 do
begin
Field := Source.FindField(Delta.Fields[i].FieldName);
if (Field <> nil) then
PutField(Delta.Fields[i], Field);
end;
Source.Post;
except
Source.Cancel;
raise;
end;
end;
// Register;
procedure Register;
begin
RegisterClasses([TpFIBClientBCDField]);
RegisterComponents('FIBPlus', [TpFIBClientDataSet,TpFIBDataSetProvider]);
end;
end.