Colleagues need remote methods to transfer custom data types, he thought to write their own code will be very complex, in fact, Datasnap long ago for us to think of.
Datasnap's data sequences and restores are incredibly powerful, with remote methods that support custom object arguments, and Datasnap automatically use JSON sequences and restore custom objects.
1) Customizing an object
Type
Tmyinfo = Class (TObject)
Public
accountno:string;
sql:string;
params:string;
Recsmax:integer;
End
2) service-side remote method definition
function Tservermethods1.querysql4 (const myinfo:tmyinfo): Tfdjsondatasets;
Var
D:tfrmdb;
I, Ipos:integer;
Sl:tstringlist;
LName, LValue, lstr:string;
P:tfdparam;
Begin
Result: = nil;
If not Assigned (MyInfo) Then
Exit;
if (Myinfo.accountno = ") or (Myinfo.sql =") Then
Exit;
D: = Getdbpool (Myinfo.accountno). Lock;
If not Assigned (d) Then
Exit;
Try
Try
D.qryopen.close;
D.qryopen.sql.clear;
D.qryopen.sql.text: = Myinfo.sql;
If Myinfo.params <> ' then
Begin
SL: = Tstringlist.create;
Try
Sl. Delimiter: = '; ';
Sl. Delimitedtext: = Myinfo.params;
For I: = 0 to SL. Count-1 do
Begin
LSTR: = sl. Strings[i];
IPos: = Pos (': ', lstr);
LName: = Leftstr (Lstr, iPos-1);
LValue: = Rightstr (Lstr, Length (LSTR)-IPos);
P: = D.qryopen.findparam (LName);
If P <> Nil then
P.value: = LValue;
End
Finally
Sl. Free;
End
End
D.qryopen.open;
Result: = tfdjsondatasets.create;
Tfdjsondatasetswriter.listadd (Result, ' 1 ', d.qryopen);
Except
On E:exception do
Begin
Result: = nil;
Log.writelog (' tservermethods1.querysql2 ' + e.message);
End
End
Finally
D.qryopen.close;
Getdbpool (Myinfo.accountno). Unlock (d);
End
End
3) Client Calls
Procedure Tform1.button3click (Sender:tobject);
Var
Ldatasets:tfdjsondatasets;
Ldataset:tfddataset;
Myinfo:tmyinfo;
Begin
MyInfo: = tmyinfo.create;
Myinfo.accountno: = ' 0 ';
Myinfo.sql: = ' select * from t1 where c1=:c1 ';
Myinfo.params: = ' c1:55 ';
Ldatasets: = methods. QUERYSQL4 (MyInfo);
Ldataset: = Tfdjsondatasetsreader.getlistvaluebyname (Ldatasets, ' 1 ');
Fdmemtable1.close;
Fdmemtable1.data: = Ldataset;
End
Datasnap remote methods support custom Object parameters