[fpc-pascal] Suggestion: TDataSetEnumerator

Michael Van Canneyt michael at freepascal.org
Fri Mar 6 11:49:47 CET 2015



On Thu, 5 Mar 2015, silvioprog wrote:

> 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;

You should check here for unidirectional datasets. They do not support .first.
For unidirectional datasets you can only go from the current point forward.

Michael.


More information about the fpc-pascal mailing list