[fpc-pascal] Suggestion: TDataSetEnumerator

silvioprog silvioprog at gmail.com
Thu Mar 5 15:58:19 CET 2015


On Thu, Mar 5, 2015 at 6:48 AM, Constantine Yannakopoulos <
alfasud.ti at gmail.com> wrote:

> On Thu, Mar 5, 2015 at 2:11 AM, silvioprog <silvioprog at gmail.com> wrote:
>
>> What do you think about a TDataSetEnumerator class in the DB unit?
>> Something like this:
>>
>> === begin code ===
>>
>>
> function TDataSetEnumerator.MoveNext: Boolean;
>> begin
>>   Inc(FPosition);
>>   if FPosition = FDataSet.RecordCount then
>>     Exit(False);
>>   FDataSet.MoveBy(FPosition);
>>   Result := True;
>> end;
>>
>
> I'm not sure for FPC but in Delphi there are datasets that do not fully
> implement the properties .RecNo and .RecordCount, server cursor datasets
> for example always return -1 if memory serves. IMHO the .MoveNext method
> should be written in such a way as to not use .RecordCount but only .Eof
> which is the only property that is guaranteed to always work correctly in
> all datasets.
>

Thanks for the information.

So we can change it to (FCL have no a .MoveNext, so I used the .Next
method):

=== begin code ===

  TDataSetEnumerator = class
  private
    FDataSet: TDataSet;
    FFirstDone: Boolean;
    function GetCurrent: TDataSet;
  public
    constructor Create(ADataSet: TDataSet);
    function MoveNext: Boolean;
    property Current: TDataSet read GetCurrent;
  end;

...

constructor TDataSetEnumerator.Create(ADataSet: TDataSet);
begin
  inherited Create;
  FDataSet := ADataSet;
  FDataSet.First;
end;

function TDataSetEnumerator.GetCurrent: TDataSet;
begin
  Result := FDataSet;
end;

function TDataSetEnumerator.MoveNext: Boolean;
begin
  if FFirstDone then
    FDataSet.Next
  else
    FFirstDone := True;
  Result := not FDataSet.EOF;
end;

=== end code ===

Thank you!

-- 
Silvio Cl├ęcio
My public projects - github.com/silvioprog
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20150305/bff496be/attachment.html>


More information about the fpc-pascal mailing list