#1 2012-08-11 21:58:35

mvddoes
Member
Registered: 2012-08-11
Posts: 6

Sorting when CreateAndFillPrepare

Hello,

I am new to mORMot and have built some code to learn it. But I cannot figure out how to retrieve data sorted when using CreateAndFillPrepare (from a table by using an TSQLRecord).

How can I change the code below to retrieve my data sorted?

In the code below TSQLPatientsis just an TSQLRecord with some properties.

  patients := TSQLPatients.CreateAndFillPrepare(DatabaseClient, '');
  cbListPatients.Clear();
  try
    while patients.FillOne do
    begin
      Inc(iTemp);
      cbListPatients.AddItem(patients.name, Pointer(patients.id));
      cbListPatients.Checked[iTemp]:= (patients.active='y');
    end
  finally
    FreeAndNil(patients);
  end;

I am curious to read the answer...!

Regards,
Maurijn

Offline

#2 2012-08-14 07:55:18

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,007
Website

Re: Sorting when CreateAndFillPrepare

You have two possibilities, sorting either on the server side, either on the client side:
- You can specify an ORDER BY clause in the aSQLWhere parameter of the method, which will use the DB indexes (if any) on the server side;
- You can sort the internal TSQLTable instance containing the data on the client side, calling e.g. patients.FillTable.SortFields() method.

If there is a matching index in the DB for the field, I'd rather use ORDER BY on the server side.
But otherwise, client-side process is a good idea, since calling FillTable.SortFields() is very fast (see TUTF8QuickSort.QuickSort implementation) and will leave server CPU and memory resources free to process other requests.

Hope it helps.
wink

Offline

#3 2012-08-15 16:16:22

mvddoes
Member
Registered: 2012-08-11
Posts: 6

Re: Sorting when CreateAndFillPrepare

It surely helped! Thanks a lot!
Regards,
Maurijn

Offline

Board footer

Powered by FluxBB