-
Notifications
You must be signed in to change notification settings - Fork 14
/
rpclientdataset.pas
148 lines (133 loc) · 3.86 KB
/
rpclientdataset.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
unit rpclientdataset;
interface
{$I rpconf.inc}
uses Classes,
DBCLient,
{$IFDEF DELPHI2006UP}
DbCommonTypes,
{$ENDIF}
DB;
type
TRpClientDataset = class(TClientDataSet)
protected
function LocateRecordUsingIndex(const KeyFields: string; const KeyValues:
Variant; Options: TLocateOptions; SyncCursor: Boolean; out CouldUseIndex:
Boolean): Boolean;
function MapsToIndex(Fields: TList; CaseInsensitive: Boolean): Boolean;
public
function Locate(const KeyFields: string; const KeyValues: Variant; Options:
TLocateOptions): Boolean; override;
function Lookup(const KeyFields: string; const KeyValues: Variant; const
ResultFields: string): Variant; override;
end;
implementation
{$IFDEF USEVARIANTS}
uses Variants; // not needed for Delphi 5
{$ENDIF}
function TRpClientDataset.Locate(const KeyFields: string; const KeyValues:
Variant; Options: TLocateOptions): Boolean;
var
CouldUseIndex: Boolean;
begin
Result := LocateRecordUsingIndex(KeyFields, KeyValues, Options, True, CouldUseIndex);
if not CouldUseIndex then
Result := inherited Locate(KeyFields, KeyValues, Options);
end;
function TRpClientDataset.LocateRecordUsingIndex(const KeyFields: string; const
KeyValues: Variant; Options: TLocateOptions; SyncCursor: Boolean; out
CouldUseIndex: Boolean): Boolean;
var
I: Integer;
Fields: TList;
SavedIndexName, SavedIndexFieldNames: string;
KeyIndex: TIndexDef;
begin
if (loPartialKey in Options) then
begin
CouldUseIndex := False;
Result := False;
Exit;
end;
KeyIndex := nil;
Fields := TList.Create;
try
GetFieldList(Fields, KeyFields);
if MapsToIndex(Fields, loCaseInsensitive in Options) then
CouldUseIndex := True
else
begin
KeyIndex := IndexDefs.GetIndexForFields(KeyFields, loCaseInsensitive in Options);
CouldUseIndex := KeyIndex <> nil;
end;
if CouldUseIndex then
begin
if Assigned(KeyIndex) then
begin
SavedIndexName := IndexName;
SavedIndexFieldNames := IndexFieldNames;
IndexName := KeyIndex.Name;
end;
try
SetKey;
if Fields.Count = 1 then
begin
if VarIsArray(KeyValues) then
TField(Fields.First).Value := KeyValues[0] else
TField(Fields.First).Value := KeyValues;
end
else
for I := 0 to Fields.Count - 1 do
TField(Fields[I]).Value := KeyValues[I];
{make sure that it only searches on first Fields.Count records, set FieldCount}
GetKeyBuffer(kiLookUp).FieldCount := Fields.Count;
Result := GotoKey;
finally
if Assigned(KeyIndex) then
begin
{Restore indexes, always restore at least one of them}
if SavedIndexFieldNames <> '' then
IndexFieldNames := SavedIndexFieldNames
else
IndexName := SavedIndexName;
end;
end;
end
else
Result := False; // if we can't use an index we always return False!
finally
Fields.Free;
end;
end;
function TRpClientDataset.Lookup(const KeyFields: string; const KeyValues:
Variant; const ResultFields: string): Variant;
var
CouldUseIndex: Boolean;
begin
if LocateRecordUsingIndex(KeyFields, KeyValues, [], False, CouldUseIndex) then
Result := FieldValues[ResultFields]
else
begin
if not CouldUseIndex then
Result := inherited Lookup(KeyFields, KeyValues, ResultFields)
else
Result := Null;
end;
end;
function TRpClientDataset.MapsToIndex(Fields: TList; CaseInsensitive: Boolean):
Boolean;
var
I: Integer;
begin
Result := False;
if (IndexFieldNames = '') then
Exit;
if Fields.Count > IndexFieldCount then Exit;
for I := 0 to Fields.Count - 1 do
if Fields[I] <> IndexFields[I] then Exit;
Result := True;
end;
//procedure Register;
//begin
// RegisterComponents('Data Access', [TRpClientDataset]);
//end;
end.