#1 2013-03-27 16:26:23

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,183
Website

Introducing TSQLTable.Step() method

The TSQLTable class has some new methods dedicated to direct cursor handling, as such:

procedure WriteBabiesStartingWith(const Letters: RawUTF8; Sex: TSex);
var aList: TSQLTableJSON;
begin
  aList := Client.MultiFieldValues(TSQLBaby,'ID,BirthDate',
    'Name LIKE ? AND Sex = ?',[Letters+'%',ord(Sex)]);
  try
    while aList.Step do
      writeln('ID=',aList.Field(0),' BirthDate=',aList.Field(1));
  finally
    aList.Free;
  end;
end;

By using the TSQLTable.Step method, you do not need to check that aList<>nil, since it will return false if aList is not assigned. And you do not need to access the RowCount property, nor specify the current row number.

We may have used not the field index, but the field name, within the loop:

writeln('ID=',aList.Field('ID'),' BirthDate=',aList.Field('BirthDate'));

You can also access the field values using late-binding and a local variant, which gives some perfectly readable code:

procedure WriteBabiesStartingWith(const Letters: RawUTF8; Sex: TSex);
var baby: variant;
begin
  with Client.MultiFieldValues(TSQLBaby,'ID,BirthDate',
    'Name LIKE ? AND Sex = ?',[Letters+'%',ord(Sex)]) do
  try
    while Step(false,@baby) do
      writeln('ID=',baby.ID,' BirthDate=',baby.BirthDate);
  finally
    Free;
  end;
end;

In the above code, late-binding will search for the "ID" and "BirthDate" fields at runtime. But the ability to write baby.ID and baby.BirthDate is very readable. Using a with ... do statement makes the code shorter, but should be avoided if it leads into confusion, e.g. in case of more complex process within the loop.

See http://blog.synopse.info/post/2013/03/2 … %29-method

Offline

#2 2013-03-28 07:27:11

h.hasenack
Member
From: Nijmegen, Netherlands
Registered: 2012-08-01
Posts: 173
Website

Re: Introducing TSQLTable.Step() method

Hi Arnaud

At a first glance, I would opt for a construction with an array of field names rather than a string with comma separated values.

...
  with Client.MultiFieldValues(TSQLBaby,['ID','BirthDate'],
      'Name LIKE ? AND Sex = ?',[Letters+'%',ord(Sex)])
...

I like the functionality though.

Last edited by h.hasenack (2013-03-28 07:27:53)

Offline

Board footer

Powered by FluxBB