forked from madorin/fibplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pFIBDataSet.pas
4664 lines (4231 loc) · 127 KB
/
pFIBDataSet.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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{***************************************************************}
{ 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 pFIBDataSet;
interface
{$I FIBPlus.inc}
uses
SysUtils, Classes, StdFuncs, DB, Ibase, IB_Intf, IB_Externals,
FIBPlatforms, Fib, FIBMiscellaneous, pFIBDatabase, FIBDataBase, FIBDataSet, FIBQuery,
pFIBLists, pFIBQuery, DSContainer, pFIBProps, IB_ErrorCodes, pFIBInterfaces,
pFIBDataInfo {, FIBSaveDataSetToXml }{$IFDEF D6+},FMTBcd, Variants {$ENDIF} ;
type
TLockStatus = (lsSuccess, lsDeadLock, lsNotExist, lsMultiply, lsUnknownError);
TLockErrorEvent =
procedure(DataSet: TDataSet; LockError: TLockStatus;
var ErrorMessage: string; var Action: TDataAction) of object;
TCachRefreshKind = (frkEdit, frkInsert);
TOnGetSQLTextProc = procedure(DataSet: TFIBDataSet; var SQLText: string) of object;
(* For generate SQL text*)
TIncludeFieldsToSQL=(ifsAllFields,ifsNoBlob,ifsOnlyBlob);
(* ********************)
TpFIBDataSet = class(TFIBDataSet)
private
vQryRecordCount: TFIBQuery;
vLockQry: TFIBQuery;
FDataSet_ID: integer;
FLoadedDataSet_ID: integer;
FDefaultFormats: TFormatFields;
FOnLockError: TLockErrorEvent;
vUserOnPostError: TDataSetErrorEvent;
vUserOnDeleteError: TDataSetErrorEvent;
FOnLockSQLText :TOnGetSQLTextProc;
// lists of UpdateObjects
vUpdates: TList;
vDeletes: TList;
vInserts: TList;
FReceiveEvents: TStrings;
{$IFDEF USE_DEPRECATE_METHODS2}
FOnUserEvent: TUserEvent;
{$ENDIF }
FHaveUncommitedChanges: boolean;
FHaveRollbackedChanges: boolean;
FDescription: string;
FOnAskRecordCount: TOnGetSQLTextProc;
FOnApplyDefaultValue:TOnApplyDefaultValue;
FSQLTextChanges: integer;
FBlobsUpdate: TpFIBUpdateObject;
FOnApplyFieldRepository:TOnApplyFieldRepository;
FContainer: TDataSetsContainer;
FDefaultsInited:boolean;
FParamsForFields: array of TFIBXSQLVAR;
FGeneratorBeUsed:boolean;
FMidasCommandText:Widestring;
FMidasOutParams: TParams;
procedure SetContainer(Value: TDataSetsContainer);
{$IFDEF USE_DEPRECATE_METHODS2}
procedure SetReceiveEvents(Value: TStrings);
{$ENDIF}
//Property access procedures
function GetSelectSQL: TStrings;
procedure SetSelectSQL(Value: TStrings);
// UpdateObjects support
function ListForUO(KindUpdate: TUpdateKind): TList;
procedure SynchroOrdersUO(List: TList);
//
procedure SetDataSet_ID(Value: Integer);
function GetFieldForTable(const Relation: string): TField;
function WillGenerateSQLs: boolean;
function GetFIBVersion: string;
procedure SetFIBVersion(const vs: string);
protected
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
function GetVisibleRecno: Integer;
procedure SetVisibleRecno(Value: Integer);
function GetRecNo: Integer; override;
procedure SetRecNo(Value: Integer); override;
procedure InternalPost; override;
procedure InternalOpen ;override;
procedure SetPrepareOptions(Value: TpPrepareOptions); override;
procedure DoOnPostError(DataSet: TDataSet; E: DB.EDatabaseError; var Action:
TDataAction); override;
procedure DoOnDeleteError(DataSet: TDataSet; E: DB.EDatabaseError; var
Action: TDataAction);
function CompareFieldValues(Field:TField;const S1,S2:variant):integer; override;
// IProviderSupport
protected
// FParams: TParams;
{$IFNDEF TWideDataSet}
function PSGetTableName: string; override;
function PSGetKeyFields: string; override;
function PSGetQuoteChar: string; override;
procedure PSSetCommandText(const CommandText: string); override;
{$ELSE}
procedure PSSetCommandText(const CommandText: Widestring); override;
function PSGetTableNameW: WideString; override;
function PSGetKeyFieldsW: WideString; override;
function PSGetQuoteCharW: Widestring; override;
function PSGetCommandTextW: WideString;override;
{$ENDIF}
function PSGetUpdateException(E: Exception; Prev: EUpdateError):
EUpdateError; override;
function PSInTransaction: Boolean; override;
function PSIsSQLBased: Boolean; override;
function PSIsSQLSupported: Boolean; override;
procedure PSStartTransaction; override;
procedure PSEndTransaction(Commit: Boolean); override;
procedure PSReset; override;
function PSUpdateRecord(UpdateKind: TUpdateKind; Delta: TDataSet): Boolean;
override;
function PSExecuteStatement(const ASQL: {$IFNDEF TWideDataSet} string{$ELSE} Widestring{$ENDIF}; AParams: TParams;
ResultSet: Pointer = nil): Integer; override;
procedure PSExecute; override;
function PSGetParams: TParams; override;
procedure PSSetParams(AParams: TParams); override;
function IsVisible(Buffer: TRecordBuffer): Boolean; override;
procedure InternalPostRecord(Qry: TFIBQuery; Buff: Pointer); override;
procedure InternalDeleteRecord(Qry: TFIBQuery; Buff: Pointer); override;
procedure AddedFilterRecord(DataSet: TDataSet; var Accept: Boolean);
virtual;
procedure InternalDoBeforeOpen; override;
procedure DoBeforeOpen; override;
procedure DoAfterOpen; override;
procedure DoAfterClose; override;
procedure DoBeforeInsert; override;
procedure DoAfterInsert; override;
procedure DoBeforeEdit; override;
procedure DoBeforePost; override;
procedure DoAfterPost; override;
procedure DoBeforeCancel; override;
procedure DoAfterCancel; override;
procedure DoBeforeDelete; override;
procedure DoOnNewRecord; override;
procedure DoAfterDelete; override;
procedure DoAfterEdit; override;
procedure DoAfterScroll; override;
procedure DoBeforeClose; override;
procedure DoBeforeScroll; override;
procedure DoOnCalcFields; override;
procedure DoBeforeRefresh; override;
procedure DoAfterRefresh; override;
procedure DoOnApplyDefaultValue(Field:TField; var Applied:boolean); dynamic;
function GetRecordCount: Integer; override;
procedure UpdateFieldsProps; virtual;
procedure DoAfterEndUpdateTransaction(EndingTR:TFIBTransaction;Action: TTransactionAction;
Force: Boolean); override;
procedure ClearModifFlags(Kind: byte; NeedRefreshFields:boolean=True);
procedure CloseProtect;
function RaiseLockError(LockError: TLockStatus; ExceptMessage: string):
TDataAction;
private
//
FBlockContextCount:integer;
FBlockSize:Integer;
FExecBlockStatement:TStrings;
function AddStatementToExecuteBlock(SK:TpSQLKind):boolean;
//=Succes add to execblock
protected
procedure LoadRepositoryInfo; override;
procedure SQLChanging(Sender: TObject); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function RecordCountFromSrv: integer; dynamic;
function VisibleRecordCount: Integer;
function VisibleRecnoToRecno(VisRN: integer): Integer;
procedure ParseParamToFieldsLinks(Dest: TStrings); override;
procedure Prepare; override;
function CanEdit: Boolean; override;
function CanInsert: Boolean; override;
function CanDelete: Boolean; override;
function IsSequenced: Boolean; override; // Scroll bar
function ExistActiveUO(KindUpdate: TUpdateKind): boolean;
// Exist active Update objects
function AddUpdateObject(Value: TpFIBUpdateObject): integer;
procedure RemoveUpdateObject(Value: TpFIBUpdateObject);
function ParamByName(const ParamName: string): TFIBXSQLVAR;
function FindParam(const ParamName: string): TFIBXSQLVAR;
function RecordStatus(RecNumber: integer): TUpdateStatus;
procedure CloneRecord(SrcRecord: integer; IgnoreFields: array of const);
procedure CloneCurRecord(IgnoreFields: array of const);
// Cached Routine
procedure CommitUpdToCach; // Clear CU buffers
procedure ApplyUpdToBase(DontChangeCacheFlags:boolean=True); // Send Updates to Base
procedure ApplyUpdates;
// SaveLoad Buffer functions
procedure SaveToStream(Stream: TStream; SeekBegin: boolean; AddInfo:Ansistring='');
procedure LoadFromStream(Stream: TStream; SeekBegin: boolean ); overload;
procedure LoadFromStream(Stream: TStream; SeekBegin: boolean; var AddInfo:Ansistring);overload;
// procedure SaveToXmlFile(const FileName:string; Format:TXmlDataSetFormat); overload;
// procedure SaveToXmlFile(const FileName:string; const FormatName:string); overload;
procedure SaveToFile(const FileName: string; AddInfo:Ansistring='');
procedure LoadFromFile(const FileName: string); overload;
procedure LoadFromFile(const FileName: string;var AddInfo:Ansistring); overload;
// End SaveLoad
function LockRecord(RaiseErr: boolean= True): TLockStatus;
function FieldByFieldNo(FieldNo: Integer): TField;
function ParamNameCount(const aParamName: string): integer;
function ParamCount: integer;
procedure ExecUpdateObjects(KindUpdate: TUpdateKind; Buff: TRecordBuffer;
aExecuteOrder: TFIBOrderExecUO);
{$IFDEF USE_DEPRECATE_METHODS2}
procedure DoUserEvent(Sender: TObject; const UDE: string; var Info: string);
dynamic; deprecated;
{$ENDIF}
procedure OpenWP(const ParamValues: array of Variant); overload;
procedure OpenWP(const ParamNames : string;const ParamValues: array of Variant); overload;
procedure OpenWPS(const ParamSources: array of ISQLObject);
procedure ReOpenWP(const ParamValues: array of Variant); overload;
procedure ReOpenWP(const ParamNames : string;const ParamValues: array of Variant); overload;
procedure ReOpenWPS(const ParamSources: array of ISQLObject);
procedure BatchRecordToQuery(ToQuery:TFIBQuery);
procedure BatchAllRecordsToQuery(ToQuery:TFIBQuery);
procedure AutoGenerateSQLText(ForState: TDataSetState);
function GenerateSQLText
(const TableName, KeyFieldNames: string; SK: TpSQLKind; IncludeFields:TIncludeFieldsToSQL=ifsAllFields;
ReturningFields: TSetReturningFields=[]
): string;
function GenerateSQLTextNoParams
(const TableName, KeyFieldNames: string; SK: TpSQLKind): string;
function GenerateSQLTextWA
(const TableName: string; SK: TpSQLKind; IncludeFields:TIncludeFieldsToSQL=ifsAllFields): string;
// Where All
procedure GenerateUpdateBlobsSQL;
procedure GenerateSQLs;
function CanGenerateSQLs: boolean;
//AutoUpdate operations
function KeyField: TField;
function SqlTextGenID: string;
procedure IncGenerator; virtual;
function AllKeyFields(const TableName: string): string;
procedure CacheModify(
aFields: array of integer; Values: array of Variant; KindModify: byte
);
procedure CacheEdit(aFields: array of integer; Values: array of Variant);
procedure CacheAppend(aFields: array of integer; Values: array of Variant);
overload;
procedure CacheAppend(Value: Variant; DoRefresh: boolean = False); overload;
procedure CacheInsert(aFields: array of integer; Values: array of Variant);
overload;
procedure CacheInsert(Value: Variant; DoRefresh: boolean = False); overload;
procedure CacheRefresh(FromDataSet: TDataSet; Kind: TCachRefreshKind
; FieldMap: TStrings
);
procedure CacheRefreshByArrMap(
FromDataSet: TDataSet; Kind: TCachRefreshKind;
const SourceFields, DestFields: array of string
);
procedure RefreshFromQuery(RefreshQuery:TFIBQuery;const KeyFields:string; IsDeletedRecords:boolean=False;
DoAdditionalRefreshRec:boolean=False
);
procedure RefreshFromDataSet(RefreshDataSet:TDataSet;const KeyFields:string;
IsDeletedRecords:boolean=False;
DoAdditionalRefreshRec:boolean=False
);
function RecordFieldAsFloat(Field: TField; RecNumber: integer;
IsVisibleRecordNum: boolean = True): Double;
public
property HasUncommitedChanges: boolean read FHaveUncommitedChanges;
property HaveRollbackedChanges: boolean read FHaveRollbackedChanges;
property AllRecordCount: integer read FAllRecordCount;
property VisibleRecno: integer read GetVisibleRecno write SetVisibleRecno;
published
//Added properties
property Filtered;
property OnFilterRecord;
property DeleteSQL: TStrings read GetDeleteSQL write SetDeleteSQL;
property UpdateSQL: TStrings read GetUpdateSQL write SetUpdateSQL;
property InsertSQL: TStrings read GetInsertSQL write SetInsertSQL;
property RefreshSQL: TStrings read GetRefreshSQL write SetRefreshSQL;
property SelectSQL: TStrings read GetSelectSQL write SetSelectSQL;
property DefaultFormats: TFormatFields read FDefaultFormats write
FDefaultFormats;
property OnPostError: TDataSetErrorEvent read vUserOnPostError write
vUserOnPostError;
property OnDeleteError: TDataSetErrorEvent read vUserOnDeleteError write
vUserOnDeleteError;
property OnLockError: TLockErrorEvent read FOnLockError write FOnLockError;
{$IFDEF USE_DEPRECATE_METHODS2}
property OnUserEvent: TUserEvent read FOnUserEvent write FOnUserEvent;
property ReceiveEvents: TStrings read FReceiveEvents write SetReceiveEvents;
{$ENDIF}
property DataSet_ID: integer read FDataSet_ID write SetDataSet_ID default 0;
property Description: string read FDescription write FDescription;
property Container:TDataSetsContainer read FContainer write SetContainer;
property OnAskRecordCount: TOnGetSQLTextProc read FOnAskRecordCount write
FOnAskRecordCount;
property About: string read GetFIBVersion write SetFIBVersion stored False;
property OnApplyDefaultValue:TOnApplyDefaultValue read FOnApplyDefaultValue write FOnApplyDefaultValue;
property OnApplyFieldRepository:TOnApplyFieldRepository read FOnApplyFieldRepository write FOnApplyFieldRepository;
property OnLockSQLText :TOnGetSQLTextProc read FOnLockSQLText write FOnLockSQLText;
end;
function FieldInArray(Field: TField; Arr: array of const): boolean;
implementation
uses
{$IFNDEF NO_MONITOR}
FIBSQLMonitor,
{$ENDIF}
{$IFDEF D_XE3}
System.Types, // for inline funcs
{$ENDIF}
StrUtil, DBConsts, SqlTxtRtns, FIBConsts,
pFIBFieldsDescr, pFIBCacheQueries,FIBCacheManage;
const
SQLKindNames: array[TpSQLKind] of string = (
'UpdateSQL',
'InsertSQL',
'DeleteSQL',
'RefreshSQL',
'UpdateOrInsert'
);
StreamSignature:AnsiString = 'FIB$DATASET';
function UseFormat(const sFormat: string; Scale: integer): string;
var
L, pD: integer;
begin
L := Length(sFormat);
if (L = 0) then
Result := sFormat
else
if (sFormat[L] = '.') then
Result := sFormat + MakeStr('0', Scale)
else
begin
pD := PosCh('.', sFormat);
if pD = 0 then
Result := sFormat
else
if (pD = L - 1) then
Result := FastCopy(sFormat, 1, Pred(pd)) + '.' + MakeStr(sFormat[L], Scale)
else
Result := sFormat
end;
end;
//
constructor TpFIBDataSet.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FDefaultFormats := TFormatFields.Create(Self);
inherited OnPostError := DoOnPostError;
inherited OnDeleteError := DoOnDeleteError;
vUpdates := TList.Create;
vDeletes := TList.Create;
vInserts := TList.Create;
FReceiveEvents := TStringList.Create;
vQryRecordCount := TFIBQuery.Create(Self);
vQryRecordCount.ParamCheck := True;
vLockQry := TFIBQuery.Create(Self);
vLockQry.ParamCheck := True;
end;
destructor TpFIBDataSet.Destroy;
begin
FDefaultFormats.Free;
vUpdates.Free;
vDeletes.Free;
vInserts.Free;
FReceiveEvents.Free;
if Assigned(FExecBlockStatement) then
FExecBlockStatement.Free;
if Assigned(FParams) then
FParams.Free;
if FGeneratorBeUsed then
FreeHandleCachedQuery(Database,
'select gen_id(' + FAutoUpdateOptions.GeneratorName + ', '
+ IntToStr(FAutoUpdateOptions.GeneratorStep) +') from RDB$DATABASE'
);
inherited Destroy;
end;
function TpFIBDataSet.GetVisibleRecno: Integer;
var
R: integer;
begin
R := inherited GetRecno;
FFilteredCacheInfo.NonVisibleRecords.Find(R, Result);
Result := R - Result;
end;
procedure TpFIBDataSet.SetVisibleRecno(Value: Integer);
var
R, R1: integer;
Diff, i: integer;
begin
if (FFilteredCacheInfo.NonVisibleRecords.Count = 0) or
(FFilteredCacheInfo.NonVisibleRecords[0] > Value)
then
inherited SetRecno(Value)
else
begin
R := FFilteredCacheInfo.NonVisibleRecords[0];
R1 := R - 1;
for i := 1 to Pred(FFilteredCacheInfo.NonVisibleRecords.Count) do
begin
Diff := FFilteredCacheInfo.NonVisibleRecords[i] - R - 1;
if Diff <> 0 then
begin
if R1 + Diff > Value then
Diff := Value - R1;
Inc(R1, Diff);
if R1 = Value then
begin
R := FFilteredCacheInfo.NonVisibleRecords[i - 1] + Diff;
Break
end
end;
R := FFilteredCacheInfo.NonVisibleRecords[i];
end;
if R1 < Value then
begin
R := FFilteredCacheInfo.NonVisibleRecords.LastItem + Value - R1
end;
inherited SetRecno(R)
end;
end;
function TpFIBDataSet.GetRecNo: Integer;
begin
if poVisibleRecno in Options then
Result := VisibleRecNo
else
Result := inherited GetRecNo
end;
procedure TpFIBDataSet.SetRecNo(Value: Integer);
begin
if poVisibleRecno in Options then
SetVisibleRecno(Value)
else
SetRealRecNo(Value);
end;
function TpFIBDataSet.VisibleRecordCount: Integer;
var
Buff: TRecordBuffer;
i: integer;
OldCurrentRec: integer;
begin
if not Filtered then
Result := RecordCount
else
begin
Result := 0;
Buff := AllocRecordBuffer;
OldCurrentRec := FCurrentRecord;
try
if FFilteredCacheInfo.AllRecords <> FRecordCount then
begin
for i := 0 to Pred(FRecordCount) do
begin
FCurrentRecord := i;
ReadRecordCache(FCurrentRecord, Buff, False);
if IsVisible(Buff) then
Inc(Result)
end;
FFilteredCacheInfo.AllRecords := FRecordCount;
FFilteredCacheInfo.FilteredRecords := Result
end
else
begin
Result := FFilteredCacheInfo.FilteredRecords
end
finally
FCurrentRecord := OldCurrentRec;
FreeRecordBuffer(Buff);
end;
end;
end;
function TpFIBDataSet.VisibleRecnoToRecno(VisRN: integer): Integer;
var
i: Integer;
VisCount: integer;
PredRN: integer;
begin
with FFilteredCacheInfo do
if NonVisibleRecords.Count = 0 then
Result := VisRN
else
begin
VisCount := 0;
PredRN := 0;
for i := 0 to NonVisibleRecords.Count - 1 do
begin
Inc(VisCount, NonVisibleRecords[i] - PredRN - 1);
if VisCount > VisRN then
begin
Result := VisRN + i;
Exit;
end
else
PredRN := NonVisibleRecords[i];
end;
Result := VisRN + NonVisibleRecords.Count;
end;
end;
procedure TpFIBDataSet.DoAfterEndUpdateTransaction(
EndingTR:TFIBTransaction;Action: TTransactionAction; Force: Boolean);
begin
if (Action in [TACommit, TACommitRetaining]) then
begin
if (poProtectedEdit in Options) and not CachedUpdates then
CloseProtect;
FHaveUncommitedChanges :=False
end
else
begin
FHaveRollbackedChanges :=FHaveUncommitedChanges or FHaveRollbackedChanges;
FHaveUncommitedChanges :=False
end;
inherited DoAfterEndUpdateTransaction(EndingTR,Action,Force);
end;
function TpFIBDataSet.CanEdit: Boolean; //override;
begin
Result := ((inherited CanEdit or ExistActiveUO(ukModify)
or WillGenerateSQLs
or (CachedUpdates and Assigned(OnUpdateRecord))
)
and (ukModify in FAllowedUpdateKinds)
)
or (drsInCacheRefresh in FRunState)
end;
function TpFIBDataSet.CanInsert: Boolean; //override;
begin
Result := ((inherited CanInsert or ExistActiveUO(ukInsert)
or WillGenerateSQLs
or (CachedUpdates and Assigned(OnUpdateRecord))
)
and (ukInsert in FAllowedUpdateKinds)
)
or (drsInCacheRefresh in FRunState)
end;
function TpFIBDataSet.CanDelete: Boolean;
begin
Result := (inherited CanDelete or ExistActiveUO(ukDelete)
or WillGenerateSQLs
or (CachedUpdates and Assigned(OnUpdateRecord))
)
and (ukDelete in FAllowedUpdateKinds)
end;
function TpFIBDataSet.IsSequenced: Boolean; // Scroll bar
begin
if (CacheModelOptions.CacheModelKind<>cmkStandard) then
Result := False
else
begin
Result := inherited IsSequenced;
if not Result then
if not Filtered or not (poVisibleRecno in Options) then
Result := FAllRecordCount <> 0
else
Result := AllFetched
end;
end;
// UpdateObjects support
function TpFIBDataSet.ListForUO(KindUpdate: TUpdateKind): TList;
begin
Result := nil;
case KindUpdate of
ukModify: Result := vUpdates;
ukInsert: Result := vInserts;
ukDelete: Result := vDeletes;
end;
end;
function TpFIBDataSet.ExistActiveUO(KindUpdate: TUpdateKind): boolean;
var
List: TList;
i, lc: integer;
begin
List := ListForUO(KindUpdate);
Result := False;
if List = nil then
Exit;
lc := Pred(List.Count);
for i := 0 to lc do
begin
Result := TpFIBUpdateObject(List[i]).Active;
if Result then
Exit;
end;
end;
procedure TpFIBDataSet.SynchroOrdersUO(List: TList);
var
i, lc: integer;
begin
lc := Pred(List.Count);
with List do
for i := 0 to lc do
begin
TpFIBUpdateObject(List[i]).ChangeOrderInList(i);
end;
end;
function TpFIBDataSet.AddUpdateObject(Value: TpFIBUpdateObject): integer;
var
List: TList;
OldPos: integer;
begin
Result := -1;
if Value = nil then
Exit;
List := ListForUO(Value.KindUpdate);
if List = nil then
Exit;
with List do
begin
OldPos := IndexOf(Value);
if OldPos = -1 then
begin
if Value.OrderInList < Count then
Insert(Value.OrderInList, Value)
else
Add(Value);
end
else
begin
if Value.OrderInList < Count then
Move(OldPos, Value.OrderInList)
else
Move(OldPos, Pred(Count))
end;
end;
SynchroOrdersUO(List);
Result := List.IndexOf(Value)
end;
procedure TpFIBDataSet.RemoveUpdateObject(Value: TpFIBUpdateObject);
var
List: TList;
begin
if Value = nil then
Exit;
List := ListForUO(Value.KindUpdate);
if List <> nil then
begin
List.Remove(Value);
SynchroOrdersUO(List);
end;
end;
/// Execute UpdateObjects
procedure TpFIBDataSet.ExecUpdateObjects(KindUpdate: TUpdateKind; Buff: TRecordBuffer;
aExecuteOrder: TFIBOrderExecUO
);
var
List: TList;
i: integer;
begin
List := ListForUO(KindUpdate);
if List.Count > 0 then
begin
for i := 0 to Pred(List.Count) do
with TpFIBUpdateObject(List[i]) do
if
Active and (ExecuteOrder = aExecuteOrder) and (not EmptyStrings(SQL))
and (SQL[0]<>SNoAction)
then
begin
AutoStartUpdateTransaction;
SetQueryParams(TpFIBUpdateObject(List[i]), Buff);
ExecQuery;
if SQLType in [SQLSelect, SQLExecProcedure, SQLSelectForUpdate] then
begin
WriteRecordCache(PRecordData(Buff)^.rdRecordNumber, Buff);
InternalRefreshRow(TpFIBUpdateObject(List[i]), Buff);
end;
FHaveUncommitedChanges:=True;
end;
end;
end;
procedure TpFIBDataSet.SetDataSet_ID(Value: Integer);
begin
if FDataSet_Id = Value then
Exit;
CheckDataSetClosed(' change DataSetID ');
FDataSet_Id := Value;
FPrepared := False;
if Value <> 0 then
PrepareOptions := PrepareOptions + [psApplyRepositary]
end;
procedure TpFIBDataSet.LoadRepositoryInfo;
begin
ListDataSetInfo.LoadDataSetInfo(Self);
end;
procedure TpFIBDataSet.SQLChanging(Sender: TObject);
begin
inherited;
FHaveRollbackedChanges := False;
FHaveUncommitedChanges := False;
end;
{$WARNINGS OFF}
procedure TpFIBDataSet.Prepare; //override;
var
iCurScreenState: Integer;
condApplied: boolean;
begin
ChangeScreenCursor(iCurScreenState);
try
FBase.CheckDatabase;
StartTransaction;
FBase.CheckTransaction;
if (psApplyRepositary in PrepareOptions)
and (DataSet_ID <> 0) and (FLoadedDataSet_ID <> FDataSet_Id)
then
begin
condApplied := Conditions.Applied;
ListDataSetInfo.LoadDataSetInfo(Self);
FLoadedDataSet_ID := FDataSet_Id;
if condApplied then
Conditions.Apply;
end
{ else
FLoadedDataSet_ID:=0;}
;
if not EmptyStrings(FQSelect.SQL) then
begin
if not FQSelect.Open then
FQSelect.Prepare;
if (csDesigning in ComponentState) then
begin
PrepareQuery(skModify);
PrepareQuery(FIBDataSet.skInsert);
PrepareQuery(FIBDataSet.skDelete);
end;
FPrepared := True;
InternalInitFieldDefs;
end
else
FIBError(feEmptyQuery, ['Prepare ' + CmpFullName(Self)]);
finally
RestoreScreenCursor(iCurScreenState);
end;
end;
//{$WARNINGS ON}
function TpFIBDataSet.GetRecordCount: Integer;
begin
Result := inherited GetRecordCount;
if Filtered and (poVisibleRecno in Options) then
Result := VisibleRecordCount
else
if Result < FAllRecordCount - FDeletedRecords then
Result := FAllRecordCount - FDeletedRecords
end;
procedure TpFIBDataSet.InternalPostRecord(Qry: TFIBQuery; Buff: Pointer);
begin
if Qry = QInsert then
ExecUpdateObjects(ukInsert, Buff, oeBeforeDefault)
else
ExecUpdateObjects(ukModify, Buff, oeBeforeDefault);
CheckDataSetOpen(' continue post ');
if not EmptyStrings(Qry.SQL) and (Qry.SQL[0]<>SNoAction) then
begin
AutoStartUpdateTransaction;
SetQueryParams(Qry, Buff);
if Qry.Open then
Qry.Close;
if not Qry.Prepared then
Qry.Prepare;
Qry.ExecQuery;
FHaveUncommitedChanges:=True;
if Qry.SQLType in [SQLSelect, SQLExecProcedure, SQLSelectForUpdate] then
begin
WriteRecordCache(PRecordData(Buff)^.rdRecordNumber, Buff);
InternalRefreshRow(Qry, Buff);
end;
end;
if Qry = QInsert then
begin
if Assigned(FBlobsUpdate) and (FBlobsUpdate.Active) then
FBlobsUpdate.KindUpdate :=ukInsert;
ExecUpdateObjects(ukInsert, Buff, oeAfterDefault)
end
else
begin
if Assigned(FBlobsUpdate) and (FBlobsUpdate.Active) then
FBlobsUpdate.KindUpdate :=ukModify;
ExecUpdateObjects(ukModify, Buff, oeAfterDefault);
end;
PRecordData(Buff)^.rdFlags := Byte(cusUnmodified);
SetModified(False);
WriteRecordCache(PRecordData(Buff)^.rdRecordNumber, Buff);
// if not CachedUpdates then
AutoCommitUpdateTransaction;
if not EmptyStrings(FQRefresh.SQL) and (poRefreshAfterPost in Options)
and (CacheModelOptions.CacheModelKind=cmkStandard)
then
begin
DoBeforeRefresh;
if InternalRefreshRow(FQRefresh, Buff) and (BlobFieldCount>0) then
begin
UpdateBlobInfo(Buff,ubiClearOldValue,False,False);
if CachedUpdates then
UpdateBlobInfo(Buff,ubiCheckIsNull,True,False)
else
UpdateBlobInfo(Buff,ubiPost,True,False);
WriteRecordCache(PRecordData(Buff).rdRecordNumber,Buff);
end;
DoAfterRefresh;
end;
UpdateBlobInfo(Buff,ubiClearOldValue,False,False);
end;
procedure TpFIBDataSet.InternalDeleteRecord(Qry: TFIBQuery; Buff: Pointer);
var
vNeedDeleteFromCache:boolean;
begin
if Qry.SQL.Count > 0 then
if Qry.SQL[0] = SNoAction then
Exit;
AutoStartUpdateTransaction;
ExecUpdateObjects(ukDelete, Buff, oeBeforeDefault);
if not EmptyStrings(Qry.SQL) then
begin
SetQueryParams(Qry, Buff);
if not Qry.Prepared then
Qry.Prepare;
Qry.ExecQuery;
if Qry.Open then
Qry.Next;
FHaveUncommitedChanges:=True;
end;
ExecUpdateObjects(ukDelete, Buff, oeAfterDefault);
if poRefreshAfterDelete in Options then
begin
vNeedDeleteFromCache:=not InternalRefreshRow(QRefresh,Buff);
end
else
vNeedDeleteFromCache:=True;
if vNeedDeleteFromCache then
PRecordData(Buff)^.rdFlags := Byte(cusDeletedApplied);
WriteRecordCache(PRecordData(Buff)^.rdRecordNumber, Buff);
if not FCachedUpdates then
AutoCommitUpdateTransaction;
end;
procedure TpFIBDataSet.DoOnDeleteError
(DataSet: TDataSet; E: DB.EDatabaseError; var Action: TDataAction);
begin
if (GlobalContainer<>nil) and (FContainer<>GlobalContainer) then
GlobalContainer.DataSetError(DataSet, deOnDeleteError, E, Action);
if FContainer <> nil then
FContainer.DataSetError(DataSet, deOnDeleteError, E, Action);
if Assigned(vUserOnDeleteError) then
vUserOnDeleteError(DataSet, E, Action);
end;
function TpFIBDataSet.CompareFieldValues(Field:TField;const S1,S2:variant):integer;
var
Compared:boolean;
begin
Compared:=False;
if (GlobalContainer<>nil) and (FContainer<>GlobalContainer) then
Compared:=GlobalContainer.DataSetCompareFieldValues(Self, Field,S1,S2,Result);
if FContainer <> nil then
Compared:=FContainer.DataSetCompareFieldValues(Self, Field,S1,S2,Result);
if not Compared then
Result:= inherited CompareFieldValues(Field,S1,S2);
end;
procedure TpFIBDataSet.DoOnPostError