[fpc-pascal] Suggestion: TDataSetEnumerator

silvioprog silvioprog at gmail.com
Thu Mar 5 01:11:00 CET 2015


Hello,

What do you think about a TDataSetEnumerator class in the DB unit?
Something like this:

=== begin code ===

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

  TSQLQuery = Class(TCustomSQLQuery)
  ...
  public
    function GetEnumerator: TDataSetEnumerator;
  end;
...

constructor TDataSetEnumerator.Create(ADataSet: TDataSet);
begin
  inherited Create;
  FDataSet := ADataSet;
  FPosition := -1;
  FDataSet.MoveBy(0);
end;

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

function TDataSetEnumerator.MoveNext: Boolean;
begin
  Inc(FPosition);
  if FPosition = FDataSet.RecordCount then
    Exit(False);
  FDataSet.MoveBy(FPosition);
  Result := True;
end;

...

function TSQLQuery.GetEnumerator: TDataSetEnumerator;
begin
  Result := TDataSetEnumerator.Create(Self);
end;

=== end code ===

So, after implementing this feature, you could use something like this:

=== begin code ===

    q.Open;
    for r in q do
      for f in r.Fields do
        WriteLn(f.AsString);

=== end code ===

Instead of this:

=== begin code ===

    q.Open;
    q.First;
    while not q.EOF do
    begin
      for f in q.Fields do
        WriteLn(f.AsString);
      q.Next;
    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/20150304/65358061/attachment.html>


More information about the fpc-pascal mailing list