#1 2018-10-21 13:09:36

DKA
Member
Registered: 2016-07-05
Posts: 39

Serialize many records using CreateAndFillPrepare

Hello,

I build a server (interface based). For testing, i'm using this class:

TSQLBiolife = class(TSQLRecord)
  private
...
    fCommon_Name: RawUTF8;
    fSpecies_Name: RawUTF8;
...   
    fNotes: TSQLRawBlob;           

  published
....
  end;

Now I'm trying to achieve CRUD over AJAX.
My main objective is to exchange JSON between Client and Server and to mimic as much as possible the TclientDataset.Delta behavior. 
I spent time reading the documentation and some presentation (FROM SQl to ORM ...).
I saw many functions about serialization but don't know how to do.
I check also the examples. I found only one (Samples\ThirdPartyDemos\Migajek\synopse-sqlite-demo) where we are building a list after CreateAndFillPrepare. Not JSON list and maybe not relevant, I don't know...

For example about reading Data (I would like to retrieve many records with only 2 or 3 fields for example), I tried :

function TServiceBiolife.BiolifeReadFromName(Common_Name: RawUTF8): RawJSON;
var aBioLife: TSQLBiolife;   
    //tmpResult : ???;   What to do here?
begin;
  TAutoFree.Create (aBioLife,TSQLBiolife.CreateAndFillPrepare(aServer,'Common_Name LIKE ' + QuotedStr(Common_Name + '%')));
  While aBiolife.FillOne do
  begin
    Writeln (aBioLife.Common_Name);            //This works as expected
    //tmpResult.AddItem( with Only Needed Fields)  ;        //What to do here?
  end;
  //result := ObjectToJSON (tmpResult);            //What to do here?
end;


Please if you have some examples , it would help.

Offline

#2 2018-10-22 06:24:25

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

Re: Serialize many records using CreateAndFillPrepare

Just use aServer.RetrieveListJSON() with the needed fields in aCustomFieldsCSV, and aForceAJAX=true.

Offline

#3 2018-10-23 20:00:18

DKA
Member
Registered: 2016-07-05
Posts: 39

Re: Serialize many records using CreateAndFillPrepare

I wrote this and It works fine.

function TServiceBiolife.BiolifeReadFromName(Common_Name: RawUTF8): RawJSON;
begin;
  result:= aServer.RetrieveListJSON( TSQLBiolife, 'Common_Name LIKE ' + QuotedStr(Common_Name + '%') , 'Common_Name,Category', True);
end;

Thanks a lot ab.

Offline

Board footer

Powered by FluxBB