unit Finder;
interface
uses DB, DBTables, SysUtils;
function GrabMemoFieldAsPChar(TheField : TMemoField): PChar;
function DoFindIn(TheField : TField; SFor : String ): Boolean;
function FindIt(TheTable : TDataSet; TheFields : array of integer;
SearchBackward : Boolean; FromBeginning : Boolean; SFor : String ): Boolean;
implementation
function GrabMemoFieldAsPChar(TheField : TMemoField): PChar;
begin
with TBlobStream.Create(TheField, bmRead) do
begin
GetMem(Result, Size + 1 );
FillChar(Result^, Size + 1 , #0 );
Read(Result^, Size);
Free;
end ;
end ;
function DoFindIn(TheField : TField; SFor : String ): Boolean;
var
PChForMemo : PChar;
begin
Result := False;
case TheField.DataType of
ftString :
begin
if (Pos(SFor, UpperCase(TheField.AsString)) > 0 ) then
Result := True;
end ;
ftInteger :
begin
if (Pos(SFor, TheField.AsString) > 0 ) then Result := True;
end ;
ftBoolean :
begin
if SFor = UpperCase(TheField.AsString) then
Result := True;
end ;
ftFloat :
begin
if (Pos(SFor, TheField.AsString) > 0 ) then Result := True;
end ;
ftCurrency :
begin
if (Pos(SFor, TheField.AsString) > 0 ) then Result := True;
end ;
ftDate .. ftDateTime :
begin
if (Pos(SFor, TheField.AsString) > 0 ) then Result := True;
end ;
ftMemo :
begin
SFor[Ord(SFor[ 0 ]) + 1 ] := #0 ;
PChForMemo := GrabMemoFieldAsPChar(TMemoField(TheField));
StrUpper(PChForMemo);
if not (StrPos( PChForMemo, @SFor[ 1 ] ) = nil ) then Result :=
True; FreeMem(PChForMemo, StrLen(PChForMemo + 1 ));
end ;
end ;
end ;
function FindIt(TheTable : TDataSet; TheFields : array of integer;
SearchBackward : Boolean; FromBeginning : Boolean; SFor : String ): Boolean;
var
i, HighTheFields, LowTheFields : integer;
BM : TBookmark;
begin
TheTable.DisableControls;
BM := TheTable.GetBookmark;
try
LowTheFields := Low(TheFields);
HighTheFields := High(TheFields);
SFor := UpperCase(SFor);
Result := False;
if FromBeginning then TheTable.First;
if SearchBackward then
begin
TheTable.Prior;
while not TheTable.BOF do
begin
for i := LowTheFields to HighTheFields do
begin
if DoFindIn(TheTable.Fields[TheFields[i]], SFor) then
begin
Result := True;
Break;
end ;
end ;
if Result then Break else TheTable.Prior;
end ;
end else
begin
TheTable.Next;
while not TheTable.EOF do
begin
for i := LowTheFields to HighTheFields do
begin
if DoFindIn(TheTable.Fields[TheFields[i]], SFor) then
begin
Result := True;
Break;
end ;
end ;
if Result then Break else TheTable.Next;
end ;
end ;
finally
TheTable.EnableControls;
if not Result then
TheTable.GotoBookmark(BM);
TheTable.FreeBookmark(BM);
end ;
end ;
end . |