Forum > Databases
is there any solution like clentdataset-DataSetProvider-query on delphi
jianwang:
Hello everyone:
i always develop n-tier application on delphi. and is used to code by ClientDataSet on client side and DataSetProvider on server side. is there any solution like above on lazarus?
I attach some example code that i worked on delphi:
1. get data from database:
1.1server-side code:
Query.Connection:=DBConnect; //DBConnection connected to the database and Query connected to the DBconnection
Query.SQL.Text:='select * from tablename'; //manully set sql statement
result:=DataSetProvider .GetRecords(-1,i,MetaDataOption+ResetOption); // DataSetProvider linked to the query and
1.2 return search result by networt transfom like web service
1.3 Client-side code:
ClientDataSet.data :=search result returned by server-side web service
2. commit clientdataset changed record to server side database:
2.1 Client-side code:
send ClientDataSet.Delta to server side by web service
2.2server-side code:
Query.Connection:=DBConnect; //DBConnection connected to the database and Query connected to the DBconnection
Query.SQL.Text:= 'select * from tablename where 1=2';
DataSetProvider.ApplyUpdates(Deltadata,0,i); // Deltadata is from ClientDataSet.Delta
2.3 on DataSetProviderBeforeUpdateRecord event to write code for some sql manully like below:
procedure TdmSysManage.dspDepartMentBeforeUpdateRecord(Sender: TObject;
SourceDS: TDataSet; DeltaDS: TCustomClientDataSet;
UpdateKind: TUpdateKind; var Applied: Boolean);
begin
if SourceDS =qryDepartMent then
begin
//DeltaDS.FieldByName('ID').ProviderFlags := [];
case UpdateKind of
ukInsert:
begin
DeltaDS.FieldByName('ID').NewValue:=newID ;
Applied :=False;
end;
ukModify:
begin
Applied :=False;
DeltaDS.FieldByName('ID').ProviderFlags := [pfInKey];
DeltaDS.FieldByName('Update_Time').NewValue:=now;
end;
ukDelete:
begin
Applied :=true;
DBConnect.Execute('delete from tablename where ID = ' + DeltaDS.FieldByName('ID').oldvalue);
end;
end;
end;
end;
this is very convenient way to process changed record, I can save changed record to database automatic or manully if needed.
in lazarus is there some similar way?
thanks a lot!
Thaddy:
It is the exact same on Freepascal/Lazarus as it is in Delphi..
jianwang:
Hello, Thaddy
is it exact same on Freepascal/Lazarus as it is in Delphi ? can you attch some example code for refrence?
thank you!
Thaddy:
Like your own code? Which is not complete. But Your own code should work if it works in Delphi.
Otherwise report back.
egsuh:
I couldn't find anything like either TDataSetProvider or TClientDataSet in Lazarus.
Currently I'm using TBufDataSet instead of TClientDataSet.
1. get data from database:
When there is a request frrom client,
1) Execute SQL statement in server. This is TSQLQuery. I copy it to TBufDataSet,
using TBufDataSet.CopyFromDataSet(SQLQuery1), save it as TStream,
and server returns the TStream to the client.
2) Client gets the stream, and a copy of TBufDataSet is made --- using TBufDataSet.LoadFromStream.
2. Update changes to the server
As there are no TDataSetProvider, there are no Delta neither.
There are two options.
1) For a table with large amount of modifications,
- First I pack the new table as a TStream, and send it to server.
- Server first delete all records of the specific condition.
- Server inserts all records received from the client.
2) Small changes : I try to keep both table (server and client) as the same at record basis ---
treated in the event handlers of beforepost, beforeinsert, beforedelete, etc.
of client's local dataset (TBufDataSet).
When there is a deletion, for example, the client first request the server to delete a record
with the key fields in the server DB, and if successful then delete client's dataset record
but calls DataSet.Cancel if not successful.
Insertions and updates are treated within beforepost event.
Client sends one records data in the TStrings.Text format (fieldname=value...),
and server call InsertOrUpdate based on keys.
Hope anybody suggests better idea.
Navigation
[0] Message Index
[#] Next page