Here's the iFieldList and the GetFieldList implementation used in test #4 (previous page).
unit FieldByNameHack;
interface
uses
DB;
type
iFieldList = interface
['{C62FA3F5-532E-4B05-8B39-48C1E7C40C5B}']
procedure Initialize(const aDataset: TDataSet);
function FindField(const FieldName: string): TField;
function FieldByName(const S: string): TField;
end;
function GetFieldList: iFieldList; overload;
function GetFieldList(const aDataset: TDataSet): iFieldList; overload;
implementation
uses
Classes, Sysutils, DBConsts;
type
TFieldList = class(TInterfacedObject, iFieldList)
private
FDataset : TDataset;
protected
type
TMyStringList = class(TStringList)
protected
function CompareStrings(const S1, S2: string): Integer; override;
end;
var
InternalList: TMyStringList;
public
procedure Initialize(const aDataset: TDataSet);
function FieldByName(const FieldName: string): TField;
function FindField(const FieldName: string): TField;
constructor Create; overload;
constructor Create(const aDataset: TDataSet); reintroduce; overload;
destructor Destroy; override;
end;
constructor TFieldList.Create(const aDataset: TDataSet);
begin
inherited Create;
InternalList := TMyStringList.Create;
FDataset := aDataset;
Initialize(FDataset);
end;
destructor TFieldList.Destroy;
begin
FreeAndNil(InternalList);
inherited;
end;
constructor TFieldList.Create;
begin
InternalList := TMyStringList.Create;
end;
function TFieldList.FieldByName(const FieldName: string): TField;
begin
Result := FindField(FieldName);
if Result = nil then
DatabaseErrorFmt(SFieldNotFound, [FieldName], FDataSet);
end;
function TFieldList.FindField(const FieldName: string): TField;
var
Index: Integer;
begin
Index := InternalList.IndexOf(FieldName);
if Index >= 0 then
Result := TField(InternalList.Objects[Index])
else
Result := nil;
end;
procedure TFieldList.Initialize(const aDataset: TDataSet);
var
aField: TField;
begin
InternalList.Clear;
for aField in aDataset.Fields do
InternalList.AddObject(aField.FieldName, aField);
InternalList.Sorted := True;
end;
{ TFieldList.TMyStringList }
function TFieldList.TMyStringList.CompareStrings(const S1, S2: string): Integer;
begin
Result := CompareText(S1, S2);
end;
function GetFieldList: iFieldList; overload;
begin
Result := TFieldList.Create;
end;
function GetFieldList(const aDataset: TDataSet): iFieldList; overload;
begin
Result := TFieldList.Create(aDataset);
end;
end.