-
Notifications
You must be signed in to change notification settings - Fork 17
/
CcmemDS.pas.cbk
2247 lines (2070 loc) · 60.5 KB
/
CcmemDS.pas.cbk
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
//CopyCat replication suite<p/>
//Copyright (c) 2014 Microtec Communications. For any questions or technical
//support, contact us at [email protected]<p/>
//<p/>
//Based on the RxMemDS unit from the Rx library<p/>
//<p/>
//The original Rx code is Copyright (c) 2001,2002 SGB Software Copyright (c) 1997,
//1998 Fedor Koshevnikov, Igor Pavluk and Serge Korolev
unit CcMemDS;
{$I CC.INC}
interface
uses
{$IFDEF MSWINDOWS} Windows,{$ENDIF}
SysUtils, Classes, CcProviders, DB{$IFDEF CC_D6}, Variants{$ENDIF} {$IFDEF CC_MEMDS_KBM}, kbmMemTable {$ENDIF}
{$IFDEF CC_MEMDS_CLIENTDATASET}, DBClient{$ENDIF}{$IFDEF CC_MEMDS_FIREDAC},FireDAC.Comp.Client{$ENDIF};
{ TCcMemoryData }
type
TLoadMode = (lmCopy, lmAppend);
{$IFNDEF CC_D6}
PByte = ^Byte;
{$ENDIF}
{ TCcWideStringField = class (TWideStringField)
end;}
{$IFDEF CC_MEMDS_INTERNAL}
TMemBlobData = ansistring;
TMemBlobArray = array[0..0] of TMemBlobData;
PMemBlobArray = ^TMemBlobArray;
TMemoryRecord = class;
TCompareRecords = function (Item1, Item2: TMemoryRecord): Integer of object;
{$IFDEF UNICODE}
PCcMemBuffer = TRecordBuffer;
{$ELSE}
PCcMemBuffer = PChar;
{$ENDIF UNICODE}
//Summary:
//In-memory TDataSet descendant.
//Description:
//TCcInternalMemoryData is based on the RxLib TRxMemoryData component.
TCcInternalMemoryData = class(TDataSet)
private
FRecordPos: Integer;
FRecordSize: Integer;
FBookmarkOfs: Integer;
FBlobOfs: Integer;
FRecBufSize: Integer;
FOffsets: PWordArray;
FLastID: Integer;
FAutoInc: Longint;
FActive: Boolean;
FRecords: TList;
FIndexList: TList;
FCaseInsensitiveSort: Boolean;
FDescendingSort: Boolean;
function AddRecord: TMemoryRecord;
function InsertRecord(Index: Integer): TMemoryRecord;
function FindRecordID(ID: Integer): TMemoryRecord;
procedure CreateIndexList(const FieldNames: string);
procedure FreeIndexList;
procedure QuickSort(L, R: Integer; Compare: TCompareRecords);
procedure Sort;
function CalcRecordSize: Integer;
function FindFieldData(Buffer: Pointer; Field: TField): Pointer;
function GetMemoryRecord(Index: Integer): TMemoryRecord;
function GetCapacity: Integer;
function RecordFilter: Boolean;
procedure SetCapacity(Value: Integer);
procedure ClearRecords;
procedure InitBufferPointers(GetProps: Boolean);
protected
procedure AssignMemoryRecord(Rec: TMemoryRecord; Buffer: PCcMemBuffer);
function GetActiveRecBuf(var RecBuf: PCcMemBuffer): Boolean; virtual;
procedure InitFieldDefsFromFields;
procedure RecordToBuffer(Rec: TMemoryRecord; Buffer: PCcMemBuffer);
procedure SetMemoryRecordData(Buffer: PCcMemBuffer; Pos: Integer); virtual;
procedure SetAutoIncFields(Buffer: PCcMemBuffer); virtual;
function CompareRecords(Item1, Item2: TMemoryRecord): Integer; virtual;
function GetBlobData(Field: TField; Buffer: PCcMemBuffer): TMemBlobData;
procedure SetBlobData(Field: TField; Buffer: PCcMemBuffer; Value: TMemBlobData);
function AllocRecordBuffer: PCcMemBuffer; override;
procedure FreeRecordBuffer(var Buffer: PCcMemBuffer); override;
procedure InternalInitRecord(Buffer: PCcMemBuffer); override;
procedure ClearCalcFields(Buffer: PCcMemBuffer); override;
function GetRecord(Buffer: PCcMemBuffer; GetMode: TGetMode;
DoCheck: Boolean): TGetResult; override;
function GetRecordSize: Word; override;
procedure SetFiltered(Value: Boolean); override;
procedure SetOnFilterRecord(const Value: TFilterRecordEvent); override;
procedure SetFieldData(Field: TField; Buffer: Pointer); override;
{$IFDEF CC_D2K13}
procedure SetFieldData(Field: TField; Buffer: TValueBuffer);overload;override;
{$ENDIF}
procedure CloseBlob(Field: TField); override;
procedure GetBookmarkData(Buffer: PCcMemBuffer; Data: Pointer); override;
function GetBookmarkFlag(Buffer: PCcMemBuffer): TBookmarkFlag; override;
{$IFDEF UNICODE}
procedure InternalGotoBookmark(Bookmark: Pointer); override;
{$ELSE}
procedure InternalGotoBookmark(Bookmark: TBookmark); override;
{$ENDIF}
procedure InternalSetToRecord(Buffer: PCcMemBuffer); override;
procedure SetBookmarkFlag(Buffer: PCcMemBuffer; Value: TBookmarkFlag); override;
procedure SetBookmarkData(Buffer: PCcMemBuffer; Data: Pointer); override;
function GetIsIndexField(Field: TField): Boolean; override;
procedure InternalFirst; override;
procedure InternalLast; override;
procedure InitRecord(Buffer: PCcMemBuffer); override;
procedure InternalAddRecord(Buffer: Pointer; Append: Boolean); override;
procedure InternalDelete; override;
procedure InternalPost; override;
procedure InternalClose; override;
procedure InternalHandleException; override;
procedure InternalInitFieldDefs; override;
procedure InternalOpen; override;
procedure OpenCursor(InfoQuery: Boolean); override;
function IsCursorOpen: Boolean; override;
function GetRecordCount: Integer; override;
function GetRecNo: Integer; override;
procedure SetRecNo(Value: Integer); override;
property Records[Index: Integer]: TMemoryRecord read GetMemoryRecord;
procedure DataConvert(Field: TField; Source, Dest: Pointer; ToNative: Boolean); override;
{$IFDEF CC_DXE6}
procedure DataConvert(Field: TField; Source: TValueBuffer; var Dest: TValueBuffer; ToNative: Boolean);override;
{$ENDIF}
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function BookmarkValid(Bookmark: TBookmark): Boolean; override;
function CompareBookmarks(Bookmark1, Bookmark2: TBookmark): Integer; override;
function CreateBlobStream(Field: TField; Mode: TBlobStreamMode): TStream; override;
function GetFieldData(Field: TField; Buffer: Pointer): Boolean; override;
{$IFDEF CC_D2K13}
function GetFieldData(Field: TField; {$IFDEF CC_D2K14}var {$ENDIF}Buffer: TValueBuffer): Boolean; overload; override;
{$ENDIF}
function GetCurrentRecord(Buffer: PCcMemBuffer): Boolean; override;
function IsSequenced: Boolean; override;
function Locate(const KeyFields: string; const KeyValues: Variant;
Options: TLocateOptions): Boolean; override;
procedure SortOnFields(const FieldNames: string;
CaseInsensitive: Boolean = True; Descending: Boolean = False);
procedure EmptyTable;
function SaveToDataSet(Dest: TDataSet; RecordCount: Integer): Integer;
property Capacity: Integer read GetCapacity write SetCapacity default 0;
property Active;
published
property BeforeOpen;
property AfterOpen;
property BeforeClose;
property AfterClose;
property BeforeInsert;
property AfterInsert;
property BeforeEdit;
property AfterEdit;
property BeforePost;
property AfterPost;
property BeforeCancel;
property AfterCancel;
property BeforeDelete;
property AfterDelete;
property BeforeScroll;
property AfterScroll;
property OnCalcFields;
property OnDeleteError;
property OnEditError;
property OnFilterRecord;
property OnNewRecord;
property OnPostError;
end;
{$ENDIF}
//Summary:
//In-memory TDataSet descendant.
//Description:
//TCcMemoryData is an in-memory TDataSet descendant, used internally as an ancestor
//for several CopyCat components.<p/>
//Please note that you can use the pre-compiler switches in CC.INC to determine which
//type of memory dataset is used internally, depending on the libraries you have available :
//set CC_MEMDS_INTERNAL to use the built-in memory dataset (default), CC_MEMDS_KBM to use
//the kbmMemTable, or CC_MEMDS_FIREDAC to use TFDMemTable from FireDAC. CopyCat must be recompiled
//after changing those settings.
TCcMemoryData = class(
{$IFDEF CC_MEMDS_INTERNAL}TCcInternalMemoryData{$ENDIF}
{$IFDEF CC_MEMDS_KBM}TkbmMemTable{$ENDIF}
{$IFDEF CC_MEMDS_CLIENTDATASET}TClientDataSet{$ENDIF}
{$IFDEF CC_MEMDS_FIREDAC}TFDMemTable{$ENDIF}
)
private
lFirst : Boolean;
procedure CreateDataSetFields;
procedure EmptyAndClose;
procedure CopyStructure(Source: TCCQuery);overload;
procedure CopyStructure(Source: TDataSet);overload;
protected
FAutoCreateFieldDefs: Boolean;
procedure DoBeforeClose;override;
procedure DoBeforeOpen;override;
procedure OpenCursor(InfoQuery: Boolean);override;
public
constructor Create(AOwner: TComponent); override;
procedure SortRows(const FieldNames: string; CaseInsensitive: Boolean);
function LoadFromDataSet(Source: TCCQuery; lTrimCharFields,
lCopyStructure: Boolean; lEmptyStringsToNull: Boolean): Integer;overload;
function LoadFromDataSet(Source: TCcQuery): Integer;overload;
function LoadFromDataSet(Source: TDataSet; RecordCount: Integer; Mode: TLoadMode): Integer;overload;
procedure AddField(Name: String; DataType: TFieldType; Size: Integer);
end;
//\ \
{$IFDEF CC_MEMDS_INTERNAL}
TMemBlobStream = class(TStream)
private
FField: TBlobField;
FDataSet: TCcInternalMemoryData;
FBuffer: PCcMemBuffer;
FMode: TBlobStreamMode;
FOpened: Boolean;
FModified: Boolean;
FPosition: Longint;
FCached: Boolean;
function GetBlobSize: Longint;
function GetBlobFromRecord(Field: TField): TMemBlobData;
public
constructor Create(Field: TBlobField; Mode: TBlobStreamMode);
destructor Destroy; override;
function Read(var Buffer; Count: Longint): Longint; override;
{$IFDEF CC_D2K13}
function Write(const Buffer: TBytes; Offset, Count: Longint): Longint; overload; override;
{$ENDIF}
function Write(const Buffer; Count: Longint): Longint; override;
{$IFDEF CC_D2K12}
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override;
{$ENDIF}
function Seek(Offset: Longint; Origin: Word): Longint; override;
procedure Truncate;
end;
//\ \
TMemoryRecord = class(TPersistent)
private
FMemoryData: TCcInternalMemoryData;
FID: Integer;
FData: Pointer;
FBlobs: Pointer;
function GetIndex: Integer;
procedure SetMemoryData(Value: TCcInternalMemoryData; UpdateParent: Boolean);
protected
procedure SetIndex(Value: Integer); virtual;
public
constructor Create(MemoryData: TCcInternalMemoryData); virtual;
constructor CreateEx(MemoryData: TCcInternalMemoryData; UpdateParent: Boolean); virtual;
destructor Destroy; override;
property MemoryData: TCcInternalMemoryData read FMemoryData;
property ID: Integer read FID write FID;
property Index: Integer read GetIndex write SetIndex;
property Data: Pointer read FData;
end;
{$IFDEF WIN32}
function DataSetLocateThrough(DataSet: TDataSet; const KeyFields: string;
const KeyValues: Variant; Options: TLocateOptions): Boolean;
{$ENDIF}
{$ENDIF}
procedure RxAssignRecord(Source, Dest: TDataSet; ByName: Boolean);
procedure _DBError(const Msg: string);
function Min(A, B: Longint): Longint;
implementation
uses
DbConsts{$IFDEF CC_MEMDS_INTERNAL},
{$IFDEF CC_D2K14}VCL.Forms{$ELSE} Forms{$ENDIF}, ComObj{$ENDIF};
resourcestring
SMemNoRecords = 'No data found';
const
ftBlobTypes = [ftBlob, ftMemo, ftGraphic, ftFmtMemo, ftParadoxOle,
ftDBaseOle, ftTypedBinary, ftOraBlob, ftOraClob
{$IFDEF CC_D2K6}, ftWideMemo{$ENDIF}];
ftSupported = [ftString, ftSmallint, ftInteger, ftWord, ftBoolean,
ftFloat, ftCurrency, ftDate, ftTime, ftDateTime, ftAutoInc, ftBCD,
{$IFDEF CC_D6}
ftTimestamp, ftFMTBcd,
{$ENDIF}
{$IFDEF CC_D2K6}
ftFixedWideChar,
{$ENDIF}
{$IFDEF CC_D2K9}
ftLongWord,
{$ENDIF}
ftBytes, ftVarBytes, ftADT, ftFixedChar, ftWideString, ftLargeint,
ftVariant, ftGuid] + ftBlobTypes;
fkStoredFields = [fkData];
GuidSize = 38;
{ Utility routines }
procedure RxAssignRecord(Source, Dest: TDataSet; ByName: Boolean);
var
I: Integer;
F, FSrc: TField;
begin
if not (Dest.State in dsEditModes) then _DBError(SNotEditing);
if ByName then begin
for I := 0 to Source.FieldCount - 1 do begin
F := Dest.FindField(Source.Fields[I].FieldName);
if F <> nil then begin
{$IFDEF WIN32}
F.Value := Source.Fields[I].Value;
{$ELSE}
if (F.DataType = Source.Fields[I].DataType) and
(F.DataSize = Source.Fields[I].DataSize) then
F.Assign(Source.Fields[I])
else F.AsString := Source.Fields[I].AsString;
{$ENDIF}
end;
end;
end
else begin
for I := 0 to Min(Source.FieldDefs.Count - 1, Dest.FieldDefs.Count - 1) do
begin
F := Dest.FindField(Dest.FieldDefs[I].Name);
FSrc := Source.FindField(Source.FieldDefs[I].Name);
if (F <> nil) and (FSrc <> nil) then begin
{$IFDEF WIN32}
F.Value := FSrc.Value;
{$ELSE}
if F.DataType = FSrc.DataType then F.Assign(FSrc)
else F.AsString := FSrc.AsString;
{$ENDIF}
end;
end;
end;
end;
procedure _DBError(const Msg: string);
begin
DatabaseError(Msg);
end;
{$IFDEF CC_MEMDS_INTERNAL}
{$IFDEF WIN32}
function DataSetLocateThrough(DataSet: TDataSet; const KeyFields: string;
const KeyValues: Variant; Options: TLocateOptions): Boolean;
var
FieldCount: Integer;
Fields: TList;
{$IFDEF UNICODE}
Bookmark: TBytes;
{$ELSE}
Bookmark: TBookmarkStr;
{$ENDIF}
function CompareField(Field: TField; Value: Variant): Boolean;
var
S: string;
begin
if Field.DataType = ftString then begin
S := Field.AsString;
if (loPartialKey in Options) then
Delete(S, Length(Value) + 1, MaxInt);
if (loCaseInsensitive in Options) then
Result := AnsiCompareText(S, Value) = 0
else
Result := AnsiCompareStr(S, Value) = 0;
end
else Result := (Field.Value = Value);
end;
function CompareRecord: Boolean;
var
I: Integer;
begin
if FieldCount = 1 then
Result := CompareField(TField(Fields.First), KeyValues)
else begin
Result := True;
for I := 0 to FieldCount - 1 do
Result := Result and CompareField(TField(Fields[I]), KeyValues[I]);
end;
end;
begin
Result := False;
with DataSet do begin
CheckBrowseMode;
if BOF and EOF then Exit;
end;
Fields := TList.Create;
try
DataSet.GetFieldList(Fields, KeyFields);
FieldCount := Fields.Count;
Result := CompareRecord;
if Result then Exit;
DataSet.DisableControls;
try
Bookmark := DataSet.Bookmark;
try
with DataSet do begin
First;
while not EOF do begin
Result := CompareRecord;
if Result then Break;
Next;
end;
end;
finally
{$IFDEF UNICODE}
if not Result and
DataSet.BookmarkValid( Bookmark) then
DataSet.Bookmark := Bookmark;
{$ELSE}
if not Result and
DataSet.BookmarkValid(PChar(Bookmark)) then
DataSet.Bookmark := Bookmark;
{$ENDIF}
end;
finally
DataSet.EnableControls;
end;
finally
Fields.Free;
end;
end;
{$ENDIF}
function CompareFields(Data1, Data2: Pointer; FieldType: TFieldType;
CaseInsensitive: Boolean): Integer;
begin
Result := 0;
case FieldType of
ftString:
if CaseInsensitive then
Result := AnsiCompareText(PChar(Data1), PChar(Data2))
else
Result := AnsiCompareStr(PChar(Data1), PChar(Data2));
ftSmallint:
if SmallInt(Data1^) > SmallInt(Data2^) then Result := 1
else if SmallInt(Data1^) < SmallInt(Data2^) then Result := -1;
ftInteger, ftDate, ftTime, ftAutoInc:
if Longint(Data1^) > Longint(Data2^) then Result := 1
else if Longint(Data1^) < Longint(Data2^) then Result := -1;
ftWord:
if Word(Data1^) > Word(Data2^) then Result := 1
else if Word(Data1^) < Word(Data2^) then Result := -1;
ftBoolean:
if WordBool(Data1^) and not WordBool(Data2^) then Result := 1
else if not WordBool(Data1^) and WordBool(Data2^) then Result := -1;
ftFloat, ftCurrency:
if Double(Data1^) > Double(Data2^) then Result := 1
else if Double(Data1^) < Double(Data2^) then Result := -1;
ftDateTime{$IFDEF CC_D6}, ftTimeStamp{$ENDIF}:
if TDateTime(Data1^) > TDateTime(Data2^) then Result := 1
else if TDateTime(Data1^) < TDateTime(Data2^) then Result := -1;
ftFixedChar:
if CaseInsensitive then
Result := AnsiCompareText(PChar(Data1), PChar(Data2))
else
Result := AnsiCompareStr(PChar(Data1), PChar(Data2));
ftWideString:
if CaseInsensitive then
Result := AnsiCompareText(WideCharToString(PWideChar(Data1)),
WideCharToString(PWideChar(Data2)))
else
Result := AnsiCompareStr(WideCharToString(PWideChar(Data1)),
WideCharToString(PWideChar(Data2)));
ftLargeint:
if Int64(Data1^) > Int64(Data2^) then Result := 1
else if Int64(Data1^) < Int64(Data2^) then Result := -1;
ftVariant:
Result := 0;
ftGuid:
Result := AnsiCompareText(PChar(Data1), PChar(Data2));
end;
end;
function CalcFieldLen(FieldType: TFieldType; Size: Word): Word;
begin
if not (FieldType in ftSupported) then
Result := 0
else
if FieldType in ftBlobTypes then
Result := SizeOf(Longint)
else
begin
Result := Size;
case FieldType of
ftString:
Inc(Result);
ftSmallint:
Result := SizeOf(Smallint);
ftInteger:
Result := SizeOf(Longint);
ftWord:
Result := SizeOf(Word);
{$IFDEF CC_D2K9}
ftLongWord:
Result := SizeOf(LongWord);
{$ENDIF}
ftBoolean:
Result := SizeOf(Wordbool);
ftFloat:
Result := SizeOf(Double);
ftCurrency:
Result := SizeOf(Double);
ftBCD:
Result := 34;
{$IFDEF CC_D6}
ftFmtBCD:
Result := 34;
{$ENDIF}
ftDate, ftTime:
Result := SizeOf(Longint);
ftDateTime{$IFDEF CC_D6}, ftTimeStamp{$ENDIF}:
Result := SizeOf(TDateTime);
ftBytes:
Result := Size;
ftVarBytes:
Result := Size + 2;
ftAutoInc:
Result := SizeOf(Longint);
ftADT:
Result := 0;
ftFixedChar:
Inc(Result);
ftWideString:
Result := (Result + 1) * SizeOf(WideChar);
ftLargeint:
Result := SizeOf(Int64);
ftVariant:
Result := SizeOf(Variant);
ftGuid:
Result := GuidSize + 1;
end;
end;
end;
procedure CalcDataSize(FieldDef: TFieldDef; var DataSize: Integer);
var
I: Integer;
begin
with FieldDef do
begin
if DataType in ftSupported - ftBlobTypes then
Inc(DataSize, CalcFieldLen(DataType, Size) + 1);
for I := 0 to ChildDefs.Count - 1 do
CalcDataSize(ChildDefs[I], DataSize);
end;
end;
procedure Error(const Msg: string);
begin
DatabaseError(Msg);
end;
procedure ErrorFmt(const Msg: string; const Args: array of const);
begin
DatabaseErrorFmt(Msg, Args);
end;
type
TBookmarkData = Integer;
PMemBookmarkInfo = ^TMemBookmarkInfo;
TMemBookmarkInfo = record
BookmarkData: TBookmarkData;
BookmarkFlag: TBookmarkFlag;
end;
{ TMemoryRecord }
constructor TMemoryRecord.Create(MemoryData: TCcInternalMemoryData);
begin
CreateEx(MemoryData, True);
end;
constructor TMemoryRecord.CreateEx(MemoryData: TCcInternalMemoryData; UpdateParent: Boolean);
begin
inherited Create;
SetMemoryData(MemoryData, UpdateParent);
end;
destructor TMemoryRecord.Destroy;
begin
SetMemoryData(nil, True);
inherited Destroy;
end;
function TMemoryRecord.GetIndex: Integer;
begin
if FMemoryData <> nil then
Result := FMemoryData.FRecords.IndexOf(Self)
else
Result := -1;
end;
procedure TMemoryRecord.SetMemoryData(Value: TCcInternalMemoryData; UpdateParent: Boolean);
var
I: Integer;
DataSize: Integer;
begin
if FMemoryData <> Value then
begin
if FMemoryData <> nil then
begin
FMemoryData.FRecords.Remove(Self);
if FMemoryData.BlobFieldCount > 0 then
Finalize(PMemBlobArray(FBlobs)[0], FMemoryData.BlobFieldCount);
ReallocMem(FBlobs, 0);
ReallocMem(FData, 0);
FMemoryData := nil;
end;
if Value <> nil then
begin
if UpdateParent then
begin
Value.FRecords.Add(Self);
Inc(Value.FLastID);
FID := Value.FLastID;
end;
FMemoryData := Value;
if Value.BlobFieldCount > 0 then
begin
ReallocMem(FBlobs, Value.BlobFieldCount * SizeOf(Pointer));
Initialize(PMemBlobArray(FBlobs)[0], Value.BlobFieldCount);
end;
DataSize := 0;
for I := 0 to Value.FieldDefs.Count - 1 do
CalcDataSize(Value.FieldDefs[I], DataSize);
ReallocMem(FData, DataSize);
end;
end;
end;
procedure TMemoryRecord.SetIndex(Value: Integer);
var
CurIndex: Integer;
begin
CurIndex := GetIndex;
if (CurIndex >= 0) and (CurIndex <> Value) then
FMemoryData.FRecords.Move(CurIndex, Value);
end;
{ TCcInternalMemoryData }
constructor TCcInternalMemoryData.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FRecordPos := -1;
FLastID := Low(Integer);
FAutoInc := 1;
FRecords := TList.Create;
end;
destructor TCcInternalMemoryData.Destroy;
begin
inherited Destroy;
FreeIndexList;
ClearRecords;
FRecords.Free;
ReallocMem(FOffsets, 0);
end;
{ Records Management }
function TCcInternalMemoryData.GetCapacity: Integer;
begin
if FRecords <> nil then
Result := FRecords.Capacity
else
Result := 0;
end;
procedure TCcInternalMemoryData.SetCapacity(Value: Integer);
begin
if FRecords <> nil then
FRecords.Capacity := Value;
end;
function TCcInternalMemoryData.AddRecord: TMemoryRecord;
begin
Result := TMemoryRecord.Create(Self);
end;
function TCcInternalMemoryData.FindRecordID(ID: Integer): TMemoryRecord;
var
I: Integer;
begin
for I := 0 to FRecords.Count - 1 do
begin
Result := TMemoryRecord(FRecords[I]);
if Result.ID = ID then
Exit;
end;
Result := nil;
end;
function TCcInternalMemoryData.InsertRecord(Index: Integer): TMemoryRecord;
begin
Result := AddRecord;
Result.Index := Index;
end;
function TCcInternalMemoryData.GetMemoryRecord(Index: Integer): TMemoryRecord;
begin
Result := TMemoryRecord(FRecords[Index]);
end;
{ Field Management }
procedure TCcInternalMemoryData.InitFieldDefsFromFields;
var
I: Integer;
Offset: Word;
begin
if FieldDefs.Count = 0 then
begin
for I := 0 to FieldCount - 1 do
begin
with Fields[I] do
if (FieldKind in fkStoredFields) and not (DataType in ftSupported) then
ErrorFmt(SUnknownFieldType, [DisplayName]);
end;
FreeIndexList;
end;
Offset := 0;
inherited InitFieldDefsFromFields;
{ Calculate fields offsets }
ReallocMem(FOffsets, FieldDefList.Count * SizeOf(Word));
for I := 0 to FieldDefList.Count - 1 do
begin
FOffsets^[I] := Offset;
with FieldDefList[I] do
if (DataType in ftSupported - ftBlobTypes) then
Inc(Offset, CalcFieldLen(DataType, Size) + 1);
end;
end;
function TCcInternalMemoryData.FindFieldData(Buffer: Pointer; Field: TField): Pointer;
var
Index: Integer; DataType: TFieldType;
begin
Result := nil ;
Index := FieldDefList.IndexOf (Field.FullName);
if (Index >= 0 ) and (Buffer <> nil ) then
begin
DataType := FieldDefList[Index].DataType;
if DataType in ftSupported then
if DataType in ftBlobTypes then
Result := Pointer (GetBlobData (Field, Buffer))
else
Result := (PCcMemBuffer(Buffer) + FOffsets [Index]);
end ;
end;
{ Buffer Manipulation }
function TCcInternalMemoryData.CalcRecordSize: Integer;
var
I: Integer;
begin
Result := 0;
for I := 0 to FieldDefs.Count - 1 do
CalcDataSize(FieldDefs[I], Result);
end;
procedure TCcInternalMemoryData.InitBufferPointers(GetProps: Boolean);
begin
if GetProps then
FRecordSize := CalcRecordSize;
FBookmarkOfs := FRecordSize + CalcFieldsSize;
FBlobOfs := FBookmarkOfs + SizeOf(TMemBookmarkInfo);
FRecBufSize := FBlobOfs + BlobFieldCount * SizeOf(Pointer);
end;
procedure TCcInternalMemoryData.ClearRecords;
begin
while FRecords.Count > 0 do
TObject(FRecords.Last).Free;
FLastID := Low(Integer);
FRecordPos := -1;
end;
function WideStrLen(AStr: PWideChar): Integer;
begin
Result := 0;
while (PChar(AStr)^ <> #0) or ((PChar(AStr) + 1)^ <> #0) do begin
Inc(Result);
AStr := PWideChar(PAnsiChar(AStr) + 2);
end;
end;
{$IFDEF CC_DXE6}
procedure TCcInternalMemoryData.DataConvert(Field: TField; Source: TValueBuffer; var Dest: TValueBuffer; ToNative: Boolean);
begin
{ if (Field.DataType = ftWideString) or (Field.DataType = ftFixedWideChar) then
StrLCopy(PChar(Dest), PChar(Source), WideStrLen(PWideChar(Source)))
else }
inherited DataConvert(Field, Source, Dest, ToNative);
end;
{$ENDIF}
procedure TCcInternalMemoryData.DataConvert(Field: TField; Source, Dest: Pointer; ToNative: Boolean);
{$IFNDEF CC_D2K6}
var
l: Integer;
p: PChar;
{$ENDIF}
begin
if (Field.DataType = ftWideString) then
{$IFNDEF CC_D2K6}
if Field.FieldKind in [fkCalculated, fkLookup] then
PLongWord(Dest)^ := PLongWord(Source)^
else if ToNative then begin
l := Length(WideString(Source^)) * sizeof(WideChar);
Move(WideString(Source^)[1], PWideChar(Dest)^, l);
p := PChar(Dest) + l;
p^ := #0;
(p + 1)^ := #0;
end
else
SetString(WideString(Dest^), PWideChar(Source), WideStrLen(PWideChar(Source)))
{$ELSE}
{$IFDEF CC_DXE6}
StrLCopy(PChar(Dest), PChar(Source), WideStrLen(PWideChar(Source)))
{$ELSE}
inherited DataConvert(Field, Source, Dest, ToNative)
{$ENDIF}
{$ENDIF}
else
inherited DataConvert(Field, Source, Dest, ToNative);
end;
function TCcInternalMemoryData.AllocRecordBuffer: PCcMemBuffer;
begin
{$IFDEF CC_D2K9}
GetMem(Result, FRecBufSize);
{$ELSE}
Result := StrAlloc(FRecBufSize);
{$ENDIF CC_D2K9}
if BlobFieldCount > 0 then
Initialize(PMemBlobArray(Result + FBlobOfs)[0], BlobFieldCount);
end;
procedure TCcInternalMemoryData.FreeRecordBuffer(var Buffer: PCcMemBuffer);
begin
if BlobFieldCount > 0 then
Finalize(PMemBlobArray(Buffer + FBlobOfs)[0], BlobFieldCount);
{$IFDEF CC_D2K9}
FreeMem(Buffer);
{$ELSE}
StrDispose(Buffer);
{$ENDIF CC_D2K9}
Buffer := nil;
end;
procedure TCcInternalMemoryData.ClearCalcFields(Buffer: PCcMemBuffer);
begin
FillChar(Buffer[FRecordSize], CalcFieldsSize, 0);
end;
procedure TCcInternalMemoryData.InternalInitRecord(Buffer: PCcMemBuffer);
var
I: Integer;
begin
FillChar(Buffer^, FBlobOfs, 0);
for I := 0 to BlobFieldCount - 1 do
PMemBlobArray(Buffer + FBlobOfs)[I] := '';
end;
procedure TCcInternalMemoryData.InitRecord(Buffer: PCcMemBuffer);
begin
inherited InitRecord(Buffer);
with PMemBookmarkInfo(Buffer + FBookmarkOfs)^ do
begin
BookmarkData := Low(Integer);
BookmarkFlag := bfInserted;
end;
end;
function TCcInternalMemoryData.GetCurrentRecord(Buffer: PCcMemBuffer): Boolean;
begin
Result := False;
if not IsEmpty and (GetBookmarkFlag(ActiveBuffer) = bfCurrent) then
begin
UpdateCursorPos;
if (FRecordPos >= 0) and (FRecordPos < RecordCount) then
begin
Move(Records[FRecordPos].Data^, Buffer^, FRecordSize);
Result := True;
end;
end;
end;
procedure TCcInternalMemoryData.RecordToBuffer(Rec: TMemoryRecord; Buffer: PCcMemBuffer);
var
I: Integer;
begin
Move(Rec.Data^, Buffer^, FRecordSize);
with PMemBookmarkInfo(Buffer + FBookmarkOfs)^ do
begin
BookmarkData := Rec.ID;
BookmarkFlag := bfCurrent;
end;
for I := 0 to BlobFieldCount - 1 do
PMemBlobArray(Buffer + FBlobOfs)[I] := PMemBlobArray(Rec.FBlobs)[I];
GetCalcFields(Buffer);
end;
function TCcInternalMemoryData.GetRecord(Buffer: PCcMemBuffer; GetMode: TGetMode;
DoCheck: Boolean): TGetResult;
var
Accept: Boolean;
begin
Result := grOk;
Accept := True;
case GetMode of
gmPrior:
if FRecordPos <= 0 then
begin
Result := grBOF;
FRecordPos := -1;
end
else
begin
repeat
Dec(FRecordPos);
if Filtered then
Accept := RecordFilter;
until Accept or (FRecordPos < 0);
if not Accept then
begin
Result := grBOF;
FRecordPos := -1;
end;
end;
gmCurrent:
if (FRecordPos < 0) or (FRecordPos >= RecordCount) then
Result := grError
else
if Filtered then
if not RecordFilter then
Result := grError;
gmNext:
if FRecordPos >= RecordCount - 1 then
Result := grEOF
else
begin
repeat
Inc(FRecordPos);
if Filtered then
Accept := RecordFilter;
until Accept or (FRecordPos > RecordCount - 1);
if not Accept then
begin
Result := grEOF;
FRecordPos := RecordCount - 1;
end;
end;
end;
if Result = grOk then
RecordToBuffer(Records[FRecordPos], Buffer)
else
if (Result = grError) and DoCheck then
Error(SMemNoRecords);
end;
function TCcInternalMemoryData.GetRecordSize: Word;
begin
Result := FRecordSize;
end;
function TCcInternalMemoryData.GetActiveRecBuf(var RecBuf: PCcMemBuffer): Boolean;
begin
case State of
dsBrowse:
if IsEmpty then
RecBuf := nil