-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathFIBDataSetLocate.inc
1218 lines (1145 loc) · 31.9 KB
/
FIBDataSetLocate.inc
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:gdeatz@hlmdd.com }
{ }
{ Copyright (c) 1998-2012 Devrace Ltd. }
{ Written by Serge Buzadzhy (buzz@devrace.com) }
{ }
{ ------------------------------------------------------------- }
{ FIBPlus home page: http://www.fibplus.com/ }
{ FIBPlus support : http://www.devrace.com/support/ }
{ ------------------------------------------------------------- }
{ }
{ Please see the file License.txt for full license information }
{***************************************************************}
{$IFDEF FIB_INTERFACE}
procedure GetFieldDataPointer(Field:TField;RecNumber:integer; var IsString:boolean;var OutRes:Pointer);
{$ENDIF}
{$IFDEF FIB_IMPLEMENT}
procedure TFIBCustomDataSet.GetFieldDataPointer(Field:TField;RecNumber:integer; var IsString:boolean;var OutRes:Pointer);
var
P1:Pointer;
fi:PFIBFieldDescr;
fn:Integer;
c:Currency;
OutRes1:Pointer;
begin
case FCacheModelOptions.CacheModelKind of
cmkStandard:
begin
if FRecordCount<=RecNumber then
FetchNext(RecNumber-FRecordCount+1);
if Unidirectional then
RecNumber:=RecNumber mod FCacheModelOptions.FBufferChunks
end;
cmkLimitedBufferSize:
begin
RecNumber:=RecNumber mod FCacheModelOptions.FBufferChunks
end;
end;
fn:=Field.FieldNo;
if
(fn<=0) and (not vCalcFieldsSavedCache
or not GetBit(PSavedRecordData(FRecordsCache.PRecBuffer(RecNumber+1,False))^.rdFlags,7))
then
begin
// NeedRecalcFields
vInspectRecno:=RecNumber;
try
vTypeDispositionField:=dfRRecNumber;
IsString:=False;
if not GetFieldData(Field,PAnsiChar(OutRes)) then // ForceCalculate
OutRes:=nil
else
case Field.DataType of
ftBCD:
begin
BCDToCurr(TBcd(OutRes^), c);
{$IFDEF D6+}
if TBCDField(Field).Size<=4 then
PInt64(OutRes)^:=(PInt64(@c)^ div IE10[4-TBCDField(Field).Size])
else
PInt64(OutRes)^:=PInt64(@c)^ *IE10[TBCDField(Field).Size-4];
{$ELSE} // D5
PCurrency(OutRes)^:=c
{$ENDIF}
end;
end;
finally
vTypeDispositionField:=dfNormal
end;
end;
if fn>0 then
begin
fi:=vFieldDescrList.List.List[fn-1];
IsString:=fi^.fdStrIndex>-1;
if IsString then
begin
OutRes:=FRecordsCache.GetStringFieldData(RecNumber,fi^.fdStrIndex);
if PAnsiString(OutRes)^='' then
begin
// May be null, may be empty str
P1:=FRecordsCache.PRecBuffer(RecNumber+1,False);
if PSavedRecordData(P1).rdFields[fn].fdIsNull then
OutRes:=nil;
end;
end
else
begin
OutRes:=FRecordsCache.GetNonStringFieldData(RecNumber,fi^.fdDataOfs-DiffSizesRecData,P1);
if PSavedRecordData(P1).rdFields[fn].fdIsNull then
OutRes:=nil;
end;
end
else
if vCalcFieldsSavedCache then
begin // Calc
OutRes1:=FRecordsCache.GetNonStringFieldData(RecNumber, FBlockReadSize+Field.Offset,P1);
if OutRes1<>nil then
if not PBoolean(OutRes1)^ then
OutRes:=nil
else
begin
Inc(PAnsiChar(OutRes1),SizeOf(Boolean));
IsString:=False;
case Field.DataType of
ftBCD:
begin
BCDToCurr(TBcd(OutRes1^), c);
{$IFDEF D6+}
if TBCDField(Field).Size<=4 then
PInt64(OutRes)^:=(PInt64(@c)^ div IE10[4-TBCDField(Field).Size])
else
PInt64(OutRes)^:=PInt64(@c)^ *IE10[TBCDField(Field).Size-4];
{$ELSE} // D5
PCurrency(OutRes)^:=c
{$ENDIF}
end;
else
OutRes:=OutRes1
end;
end
end;
end;
function TFIBCustomDataSet.InternalLocate(const KeyFields: string;
KeyValues:array of Variant; Options: TExtLocateOptions;FromBegin:boolean = False;
LocateKind:TLocateKind = lkStandard; ResyncToCenter:boolean =False
): Boolean;
type
TArrow=(arForward,arBackward);
var
fl: TFIBList;
fld_cnt: Integer;
rc,rc1 :Integer;
Arrow:TArrow;
vIgnoreRecChecked:boolean;
RecI: Integer;
vSortInfos : array of TSortFieldInfo;
FinishSearch:boolean;
NowInFetched:boolean;
vDateAdjusted:boolean;
WideValue:WideString;
WideKey:WideString;
vCalcBuffer:PAnsiChar;
function IsVisibleRecord:boolean;
var
vBuff:TRecordBuffer;
begin
if drsInMoveRecord in FRunState then
begin
Result := True;
Exit;
end;
if Filtered then
begin
vBuff:=AllocRecordBuffer;
try
FCurrentRecord:=RecI;
ReadRecordCache(RecI, vBuff, False);
Result:=IsVisible(vBuff);
finally
FreeMem(vBuff);
end;
end
else
if FCacheModelOptions.FCacheModelKind=cmkStandard then
begin
if UniDirectional then
vBuff :=FRecordsCache.PRecBuffer((RecI mod BufferChunks) +1,False)-DiffSizesRecData
else
vBuff :=FRecordsCache.PRecBuffer(RecI+1,False)-DiffSizesRecData;
Result:=IsVisibleStat(vBuff);
end
else
Result:=True;
end;
procedure AdjustOrders(L, R: Integer);
var
I, J: Integer;
P: Integer;
T: TSortFieldInfo;
P1:Pointer;
v :Variant;
begin
repeat
I := L;
J := R;
P := vSortInfos[(L + R) shr 1].InOrderIndex;
repeat
while vSortInfos[I].InOrderIndex< P do
Inc(I);
while vSortInfos[J].InOrderIndex> P do
Dec(J);
if I <= J then
begin
if I<>J then
begin
T := vSortInfos[I];
vSortInfos[I] := vSortInfos[J];
vSortInfos[J] := T;
P1:=fl.List^[I];
fl.List^[I]:=fl.List^[J];
fl.List^[J]:=P1;
v :=KeyValues[I];
KeyValues[I]:=KeyValues[J];
KeyValues[J]:=v;
end;
Inc(I);
Dec(J);
end;
until I > J;
if L < J then
AdjustOrders( L, J);
L := I;
until I >= R;
end;
function InitSortInfos:boolean;
var
i:integer;
NeedAdjustOrder:boolean;
vCallAsSorted:boolean;
begin
vCallAsSorted:=eloInSortedDS in Options;
if not vCallAsSorted then
begin
Result := False;
Exit;
end;
NeedAdjustOrder:=False;
SetLength(vSortInfos,fld_cnt);
for i:=0 to fld_cnt-1 do
begin
if IsSortedField(TField(fl.List^[i]),vSortInfos[i]) then
begin
if vSortInfos[i].InOrderIndex<>Succ(i) then
NeedAdjustOrder:=True;
end
else
begin
Result := False;
Exit;
end;
end;
if NeedAdjustOrder then
AdjustOrders(0,fld_cnt-1);
for i:=0 to fld_cnt-1 do
if (vSortInfos[i].InOrderIndex<>Succ(i)) or
((TField(fl.List^[i]).DataType in [ftString,ftWideString,ftGuid]) and
not (vCallAsSorted and not (eloCaseInsensitive in Options))
)
then
begin
Result := False;
Exit;
end;
Result:=True;
end;
procedure AdjustKeys;
var
i:integer;
CalcSize:integer;
fdt :TFieldType ;
begin
CalcSize:=0;
for i:=Pred(fl.Count) downto 0 do
begin
if TField(fl.List^[i]).FieldKind in [fkCalculated, fkLookup] then
begin
Inc(CalcSize,TField(fl.List^[i]).DataSize)
end;
if VarIsNull(KeyValues[i]) or VarIsEmpty(KeyValues[i]) then
begin
KeyValues[i]:=null;
Continue
end
else
if VarType(KeyValues[i])= varBoolean then
if KeyValues[i] then
KeyValues[i]:=1
else
KeyValues[i]:=0;
fdt :=TField(fl.List^[i]).DataType ;
{$IFDEF UNICODE_TO_STRING_FIELDS}
if fdt=ftString then
fdt:=ftWideString;
{$ENDIF}
case fdt of
ftString :
begin
if eloCaseInsensitive in Options then
begin
KeyValues[i]:=AnsiUpperCase(KeyValues[i]);
{$IFDEF D2009+}
KeyValues[i]:=VarAsType(KeyValues[i],varString);
{$ENDIF}
end
else
KeyValues[i]:=VarAsType(KeyValues[i],varString);
end;
ftSmallint: KeyValues[i]:=VarAsType(KeyValues[i],varSmallInt);
ftInteger : KeyValues[i]:=VarAsType(KeyValues[i],varInteger);
ftBoolean : KeyValues[i]:=VarAsType(KeyValues[i],varByte);
ftFloat : KeyValues[i]:=VarAsType(KeyValues[i],varDouble);
ftCurrency: KeyValues[i]:=VarAsType(KeyValues[i],varCurrency);
ftWideString :
begin
if Database.NeedUnicodeFieldsTranslation then
begin
if eloCaseInsensitive in Options then
begin
KeyValues[i]:=WideUpperCase(KeyValues[i]);
end
else
begin
KeyValues[i]:=VarAsType(KeyValues[i],varOleStr);
end
end
else
begin
if eloCaseInsensitive in Options then
KeyValues[i]:=AnsiUpperCase(KeyValues[i])
else
KeyValues[i]:=VarAsType(KeyValues[i],varString);
end
end;
{$IFDEF D6+}
ftLargeint : KeyValues[i]:=VarAsType(KeyValues[i],varInt64);
{$ELSE}
ftLargeint : KeyValues[i]:=VarAsType(KeyValues[i],varInteger);
{$ENDIF}
ftBCD :
{$IFDEF D6+}
if TField(fl.List^[i]).Size=0 then
KeyValues[i]:=VarAsType(KeyValues[i],varInt64)
else
KeyValues[i]:=VarAsType(KeyValues[i]*E10[TField(fl.List^[i]).Size],varInt64);
{$ELSE}
;
{$ENDIF}
ftTime:
begin
KeyValues[i]:= VarAsType(KeyValues[i],varDate);
KeyValues[i]:= DateTimeToTimeStamp(KeyValues[i]).Time
end ;
ftDate:
begin
KeyValues[i]:= VarAsType(KeyValues[i],varDate);
KeyValues[i]:= DateTimeToTimeStamp(KeyValues[i]).Date
end;
ftDateTime:
begin
if VarType(KeyValues[i])<>varDate then
begin
vDateAdjusted:=True;
KeyValues[i]:=VarAsType(KeyValues[i],varDate);
end
end;
ftGuid:
KeyValues[i]:=VarAsType(KeyValues[i],varString);
end;
end;
if CalcSize>0 then
GetMem(vCalcBuffer,CalcSize)
else
vCalcBuffer:=nil
end;
procedure IsFinishSearch;
begin
case LocateKind of
lkStandard,lkNext:
begin
if FCacheModelOptions.FCacheModelKind=cmkStandard then
begin
FinishSearch:=RecI>=FRecordCount;
if FinishSearch and not (eloInFetchedRecords in Options) then
FinishSearch:=FetchNext(1)=0;
end
else
begin
FinishSearch:=RecI>vPartition.EndPartRecordNo
end;
end;
lkPrior:
begin
if FCacheModelOptions.FCacheModelKind=cmkStandard then
FinishSearch:=RecI<0
else
FinishSearch:=RecI<vPartition.BeginPartRecordNo
end;
end;
end;
var
StrValue,sKey:Ansistring; // used in CompareValues
function CompareValues:boolean;
var
i:integer;
p:Pointer;
vIsString:boolean;
L:integer;
dt:TDateTime;
cResult:shortInt;
InitStrValue:boolean;
inWS:boolean;
ts:TTimeStamp;
fi:PFIBFieldDescr;
dv:Double;
fn:Integer;
{$IFNDEF D6+}
tempCurrency:Currency;
{$ENDIF}
function UDFCompStr(const s:Ansistring):integer;
begin
Result:=CompareFieldValues(TField(fl.List^[i]),KeyValues[i],s )
end;
function UDFCompWideStr(const s:Widestring):integer;
begin
Result:=CompareFieldValues(TField(fl.List^[i]),WideKey,s )
end;
procedure UDFUtf8DecodeA(var s:Ansistring);
begin
{$IFDEF D2009+}
s:=UTF8ToString(S)
{$ELSE}
s:=UTF8Decode(s)
{$ENDIF}
end;
procedure ChangeArrowInSorted;
begin
case TField(fl.List^[i]).DataType of
ftString :
begin
if not InitStrValue then
StrValue:=PAnsiString(p)^;
if eloCaseInsensitive in Options then
StrValue:=AnsiUppercase(StrValue);
Arrow:=TArrow(vSortInfos[i].Asc xor (UDFCompStr(StrValue)>0))
end;
ftWideString:
begin
if not InWs then
begin
DoUtf8Decode(KeyValues[i],WideKey);
if InitStrValue then
DoUtf8Decode(StrValue,WideValue)
else
DoUtf8Decode(PAnsiString(p)^,WideValue);
end;
if eloCaseInsensitive in Options then
DoWideUppercase(WideValue);
Arrow:=TArrow(vSortInfos[i].Asc xor (UDFCompWideStr(WideValue)>0))
end;
else
Arrow:=TArrow(vSortInfos[i].Asc xor (cResult< 0));
end ;
end;
function NonStrFieldCompare:boolean;
begin
// Non string fields compare
case TField(fl.List^[i]).DataType of
ftSmallInt:
begin
if PSmallInt(P)^=TVarData(KeyValues[i]).VSmallInt then
cResult:=0
else
if PSmallInt(P)^>TVarData(KeyValues[i]).VSmallInt then
cResult:=1
else
cResult:=-1;
end;
ftInteger,ftDate,ftTime:
begin
if (fi=nil) or (fi.fdDataSize=4) then
begin
if PInteger(P)^=TVarData(KeyValues[i]).VInteger then
cResult:=0
else
if PInteger(P)^>TVarData(KeyValues[i]).VInteger then
cResult:=1
else
cResult:=-1;
end
else
begin
if PSmallInt(P)^=TVarData(KeyValues[i]).VInteger then
cResult:=0
else
if PSmallInt(P)^>TVarData(KeyValues[i]).VInteger then
cResult:=1
else
cResult:=-1;
end
end;
ftFloat :
begin
if fi=nil then
begin
if Abs(PDouble(P)^-TVarData(KeyValues[i]).VDouble)<1E-11 then
cResult:=0
else
if PDouble(P)^>TVarData(KeyValues[i]).VDouble then
cResult:=1
else
cResult:=-1;
end
else
begin
case fi^.fdDataType of
SQL_FLOAT:
dv:=PSingle(P)^;
SQL_LONG :
dv:=PLong(P)^*E10[fi^.fdDataScale];
SQL_SHORT:
dv:=PShort(P)^*E10[fi^.fdDataScale]
else
dv:=PDouble(P)^
end;
if Abs(dv-TVarData(KeyValues[i]).VDouble)<1E-11 then
cResult:=0
else
if dv>TVarData(KeyValues[i]).VDouble then
cResult:=1
else
cResult:=-1;
end
end;
ftBCD :
begin
{$IFNDEF D6+}
// Keys don't adjust
cResult :=ComparePCurrencyAndVariant(p,KeyValues[i]);
{$ELSE}
if Int64(P^)=TVarData(KeyValues[i]).VInt64 then
cResult:=0
else
if Int64(P^)>TVarData(KeyValues[i]).VInt64 then
cResult:=1
else
cResult:=-1;
{$ENDIF}
end;
ftBoolean:
begin
if Byte(P^)=TVarData(KeyValues[i]).VByte then
cResult:=0
else
if Byte(P^)>TVarData(KeyValues[i]).VByte then
cResult:=1
else
cResult:=-1;
end;
ftDateTime :
begin
ts:=MSecsToTimeStamp(PDouble(P)^);
if vDateAdjusted then
ts.Time:= ts.Time - (ts.Time mod 1000);
// ts.Time:=(ts.Time div 1000)*1000; // TDateTime can't contain Msecs
dt:=TimeStampToDateTime(ts);
{ if (vDateAdjusted and (Abs(dt-TVarData(KeyValues[i]).VDate)<1E-5))
or (dt=TVarData(KeyValues[i]).VDate)}
if dt=TVarData(KeyValues[i]).VDate then
cResult:=0
else
if dt>TVarData(KeyValues[i]).VDate then
cResult:=1
else
cResult:=-1
end ;
ftLargeint:
{$IFDEF D6+}
if Int64(P^)=TVarData(KeyValues[i]).VInt64 then
cResult:=0
else
if Int64(P^)>TVarData(KeyValues[i]).VInt64 then
cResult:=1
else
cResult:=-1;
{$ELSE}
if Int64(P^)=TVarData(KeyValues[i]).VInteger then
cResult:=0
else
if Int64(P^)>TVarData(KeyValues[i]).VInteger then
cResult:=1
else
cResult:=-1;
{$ENDIF}
ftGuid:
begin
cResult :=CompareStrAndGuid(PGuid(P),AnsiString(TVarData(KeyValues[i]).VString));
end;
end;
Result:=cResult=0
end;
var
fdt :TFieldType ;
begin
i := 0; cResult:=0;
Result := True;
Arrow :=arForward;
while (Result and (i < fld_cnt)) do
begin
InitStrValue:=False;
inWS:=False;
p:=vCalcBuffer;
GetFieldDataPointer(TField(fl.List^[i]),RecI,vIsString,p);
fn:=TField(fl.List^[i]).FieldNo;
if fn>=1 then
fi:=vFieldDescrList[fn-1]
else
fi:=nil;
{$IFNDEF D6+}
if (TField(fl.List^[i]).DataType=ftBCD) and (fn>=1) then
if p<>nil then
begin
tempCurrency:=PComp(p)^*E10[-TBCDField(fl.List^[i]).Size];
p:=@tempCurrency;
end;
{$ENDIF}
if TVarData(KeyValues[i]).VType = varNull then // After cast. varByRef or varVariant can't be
begin
if p=nil then
begin
Inc(i);
Continue;
end
else
begin
Result := False;
if (eloInSortedDS in Options) then
if FIsClientSorting then
Arrow:=TArrow(vSortInfos[i].Asc)
else
Arrow:=TArrow(vSortInfos[i].NullsFirst);
Exit;
end;
end
else
begin
if p=nil then
begin
Result := False;
if (eloInSortedDS in Options) then
if FIsClientSorting then
Arrow:=TArrow(not vSortInfos[i].Asc)
else
Arrow:=TArrow(not vSortInfos[i].NullsFirst);
Exit;
end;
end;
fdt :=TField(fl.List^[i]).DataType;
{$IFDEF UNICODE_TO_STRING_FIELDS}
if fdt=ftString then
fdt:= ftWideString;
{$ENDIF}
if Result then
begin
if (Options-[eloInFetchedRecords]=[])
and ((fdt = ftString) or ((fdt =ftWideString) and not Database.NeedUnicodeFieldsTranslation))
then
begin
sKey:=AnsiString(TVarData(KeyValues[i]).VString);
if Options-[eloInFetchedRecords]=[] then
begin
if vIsString then
begin
if poTrimCharFields in Self.Options then
begin
L:=Length(PAnsiString(p)^);
if (L>0) and (PAnsiString(p)^[L]=' ') then
begin
while (L>0) and (PAnsiString(p)^[L]=' ') do
Dec(L);
Result :=(L=Length(sKey)) and CompareMem(@PAnsiString(p)^[1],@sKey[1],L);
end
else
begin
Result :=(L=Length(sKey)) and (sKey=PAnsiString(p)^)
end
end
else
begin
Result :=sKey=PAnsiString(p)^;
end
end
else
begin
L:=StrScan(PAnsiChar(p),#0)-PAnsiChar(p);
if L>TField(fl.List^[i]).Size then
L:=TField(fl.List^[i]).Size;
if poTrimCharFields in Self.Options then
while (L>0) and (PAnsiChar(p)[L-1]=' ') do
Dec(L);
Result :=(L=Length(sKey)) and CompareMem(PAnsiChar(p),@sKey[1],L);
end;
end
end
else
// case TField(fl.List^[i]).DataType of
case fdt of
ftString:
begin
sKey:=AnsiString(TVarData(KeyValues[i]).VString);
InitStrValue:=True;
if vIsString then
begin
StrValue:=PAnsiString(p)^;
UniqueString (StrValue);
if poTrimCharFields in Self.Options then
begin
L:=Length(StrValue) ;
while (L>0) and (PAnsiChar(p)[L-1]=' ') do
Dec(L);
SetLength(StrValue,L)
end;
// DoTrimRight(StrValue)
end
else
begin
L:=StrScan(PAnsiChar(p),#0)-PAnsiChar(p);
if L>TField(fl.List^[i]).Size then
L:=TField(fl.List^[i]).Size;
if poTrimCharFields in Self.Options then
while (L>0) and (PAnsiChar(p)[L-1]=' ') do
Dec(L);
SetString(StrValue,PAnsiChar(p),L);
end;
if (TField(fl.List^[i]) is TFIBStringField) and
Database.NeedUnicodeFieldTranslation(Byte(TFIBStringField(fl.List^[i]).SqlSubType))
then
UDFUtf8DecodeA(StrValue);
if (eloPartialKey in Options) then
begin
if (eloCaseInsensitive in Options) then
{$IFDEF D2009+}
DoAnsiUpperCase(StrValue) ;
{$ELSE}
StrValue:=AnsiUpperCase(StrValue);
{$ENDIF}
Result :=IsBeginPartStr(sKey,StrValue)
end
else
if (eloWildCards in Options) then
begin
if (eloCaseInsensitive in Options) then
{$IFDEF D2009+}
DoAnsiUpperCase(StrValue) ;
{$ELSE}
StrValue:=AnsiUpperCase(StrValue);
{$ENDIF}
Result:= SQLMaskCompareAA(StrValue,sKey)
end
else
if (eloCaseInsensitive in Options) then
begin //eloCaseInsensitive in Options
{$IFDEF D2009+}
Result:=AnsiCompareTextAA(sKey, StrValue)=0;
{$ELSE}
Result:=AnsiCompareText(sKey, StrValue)=0;
{$ENDIF}
end
else
// eloSorted
Result:=sKey = StrValue
end;
ftWideString:
begin
// Options<>[]
inWS:=True;
WideKey:=TVarData(KeyValues[i]).VOleStr;
begin
if vIsString then
begin
DoUtf8Decode(PAnsiString(p)^,WideValue);
if poTrimCharFields in Self.Options then
DoTrimRight(WideValue)
end
else
begin
if TField(fl.List^[i]).FieldNo<0 then
begin
{$IFDEF D2009+}
{$IFDEF D_XE}
WideValue:=PWideChar(p)
{$ELSE}
WideValue:=UTF8ToString(PAnsiChar(P))
{$ENDIF}
{$ELSE}
WideValue:=UTF8Decode(PChar(P))
{$ENDIF}
;
// WideValue:=PWideChar(p);
if poTrimCharFields in Self.Options then
DoTrimRight(WideValue)
end
else
begin
L:=StrScan(PAnsiChar(p),#0)-PAnsiChar(p);
if L>TField(fl.List^[i]).DataSize-1 then
L:=TField(fl.List^[i]).DataSize-1;
if poTrimCharFields in Self.Options then
while (L>0) and (PAnsiChar(p)[L-1]= ' ') do
Dec(L);
SetString(StrValue,PAnsiChar(p),L);
DoUtf8Decode(StrValue,WideValue);
end
end;
if (eloPartialKey in Options) then
begin
if (eloCaseInsensitive in Options) then
DoWideUpperCase(WideValue);
Result :=IsBeginPartStr(WideKey,WideValue)
end
else
if (eloWildCards in Options) then
begin
if (eloCaseInsensitive in Options) then
DoWideUpperCase(WideValue);
Result:= SQLMaskCompare(WideValue,WideKey)
end
else
if (eloCaseInsensitive in Options) then
{$IFDEF D6+}
Result:=WideCompareText(WideValue,WideKey)=0 //eloCaseInsensitive in Options
{$ELSE}
begin
DoWideUpperCase(WideValue);
Result:=WideValue=WideKey
end
{$ENDIF}
else
Result:=WideKey=WideValue
end
end // ftWideString *)
else
Result:=NonStrFieldCompare
end
end;
if not Result and (eloInSortedDS in Options) then
begin
// for sorted
ChangeArrowInSorted;
Exit;
end;
Inc(i);
end;
end;
procedure FindFirst;
var
GoodRec:Integer;
sRecI :Integer;
begin
// Already find record in sorted dataset
// Goto first suitable record
if (LocateKind<>lkStandard) or not (eloInSortedDS in Options) then
Exit;
sRecI:=RecI;
if IsVisibleRecord and (RecI+1<>vIgnoreLocRecno) then
GoodRec:=RecI
else
GoodRec:=-1;
while Result and (RecI>0) do
begin
Dec(RecI);
if IsVisibleRecord and (RecI+1<>vIgnoreLocRecno) then
begin
Result:=CompareValues;
if Result then
GoodRec:=RecI;
end;
end;
if GoodRec>-1 then
begin
RecI:=GoodRec;
Result := True;
Exit;
end
else
begin
RecI:=sRecI;
Result := True;
while Result and(RecI<FRecordCount-1) do
begin
Inc(RecI);
if IsVisibleRecord and (RecI+1<>vIgnoreLocRecno) then
begin
Result:=CompareValues;
if Result then
begin
GoodRec:=RecI;
Break
end;
end;
end;
if GoodRec>-1 then
begin
RecI:=GoodRec;
Result := True;
end
else
Result :=False
end;
end;
begin
CheckActive;
Result := False;
vCalcBuffer:=nil;
if IsEmpty then
Exit;
rc:=-1;
rc1:=-1;
if State in [dsEdit,dsInsert] then
Post;
NowInFetched:=True;
vIgnoreRecChecked :=False;
fl := TFIBList.Create;
try
GetFieldList(fl, KeyFields);
fld_cnt := fl.Count;
// SetLength(wsA,fl.Count);
if fl.Count=0 then
Exit;
vDateAdjusted:=False;
Result := False;
AdjustKeys;
if InitSortInfos then
Include(Options,eloInSortedDS) // Force use SortInfo
else
Exclude(Options,eloInSortedDS); // SortInfo not Valid
if FromBegin then
begin
if FCacheModelOptions.FCacheModelKind=cmkStandard then
RecI := 0
else