Kbmmw provides the queryservice, which allows us to directly use clientquery for SQL query and commit on the client, just like traditional database applications. It can also submit multiple datasets in the form of transactions on the client, it should be said that queryservice provides us with great convenience to quickly convert two-tier applications into multi-tier applications. If Asta is used, it will be found that queryservice is converting two-tier applications, it is more perfect than its implementation, with a clearer hierarchy. However, in actual projects, it is not enough to only use queryservice if you consider the application server's support for multiple platforms and the centralized implementation of business logic, we may also need to use other services provided by kbmmw, such as WebService. This involves a problem, such as how to control the streaming of datasets, just like queryservice, by streaming a query on the server, call the received stream to a clientquery on the client, and vice versa.
Kbmmw also provides a perfect mechanism for us to transfer data sets between servers and clients using different formats of streams. Let's take a look at the server implementation:
Place a tkbmmwunidacquery in a service and name it Q. Set the connectionpool and sessionname attributes;
Place the required kbmmwxxxstreamformat, add the method sum1 to the service, and set the first parameter of the method to the stream format required by the client. Now, you can see the following implementation code.
// Functions hed by the Service.
//------------------------------------
Function twsyh. performsum1 (clientident: tkbmmwclientidentity; const
ARGs: array of variant): variant;
VaR
St: tkbmmwmemorystream;
Begin
// Enter code here to perform function sum1
Q. close;
Q. SQL. Add ('select * From t1 ');
Q. open;
St: = tkbmmwmemorystream. Create;
Case ARGs [0]
0: Q. savetostreamviaformat (St, kbmmwbinarystreamformat1 );
1: Q. savetostreamviaformat (St, kbmmwjsonstreamformat1 );
2: Q. savetostreamviaformat (St, kbmmwxmlstreamformat1 );
End;
Resultstream. loadfromstream (ST );
St. Free;
End;
The implementation is very simple. Set the SQL statement, execute the query, generate the stream in the format required by the client, and write it to resultstream. The opposite is true for client implementation:
Procedure tform6.button5click (Sender: tobject );
VaR
Sfbin: tkbmmwbinarystreamformat;
Sfjson: tkbmmwjsonstreamformat;
Sfxml: tkbmmwxmlstreamformat;
Begin
Sfbin: = tkbmmwbinarystreamformat. Create (Self );
Sfjson: = tkbmmwjsonstreamformat. Create (Self );
Sfxml: = tkbmmwxmlstreamformat. Create (Self );
Try
Label1.caption: = kbmmwSimpleclient1.request('Httpservice', 'kbmmw _ 000000', 'sum1', [rgselectstreamformat. itemindex]);
//
Label1.caption: = kbmmwsimpleclient1.request ('queryservice', '1. 0', 'sum1', [rgselectstreamformat. itemindex]);
Kbmmwclientquery2.close;
Case rgselectstreamformat. itemindex
0: kbmmwclientquery2.loadfromstreamviaformat (kbmmwsimpleclient1.Resultstream, Sfbin );
1: kbmmwclientquery2.loadfromstreamviaformat (kbmmwsimpleclient1.Resultstream, Sfjson );
2: kbmmwclientquery2.loadfromstreamviaformat (kbmmwsimpleclient1.Resultstream, Sfxml );
End;
Finally
Sfbin. Free;
Sfjson. Free;
Sfxml. Free;
End;
End;
On the client side, we use simpleclient to send a request to the specified service, and transfer the resultstream returned by the request to a clientquery in the request format.
During the above test code, a strange problem occurs: When binarystreamformat is selected, clientquery fails to load the stream, while the other two formats are positive.
. Ask xalion. His first judgment is that this is not a bug, because kbmmw first uses the bin stream. Therefore, we also specially write the stream into a file to compare the server and client generation.
Xalion also tests whether the files are different. It loads the files into a clientquery. The test is normal, but an error occurs here. A waste of hours
Xalion found that this sentence was added to my code, and the problem was solved:
Kbmmwbinarystreamformat1.sfdef: = [sfloaddef]
Find the cause and check the cause. It turns out that when we use a stream to operate the query, the streamformat control used cannot be referenced by the clientquery. transportstreamformat attribute (and clienttransactionresolve). Why?
Procedure
Tkbmmwcustomclientcursor.Settransportstreamformat(Aformat: tkbmmwcustomstreamformat );
Begin
If aformat = ftransportstreamformat then exit;
If aformat <> nil then
With aformat do
Begin
Sfdef: = [];// Clientquery clears the sfdef of streamformat!
Sfdeltas: = [sfsavedeltas];
Sfdata: = [sfloaddata];
End;
Clienttransactionresolve also has the same code, clearing the sfdef attribute value of streamformat.
So far, we have gained an experience:
That is, when streaming query, do not use the streamformat referenced by the transportstreamformat attribute of other queries!