Refetching Data on Inserts and Updates
ASTAClientDataSets can refetch values from the server on inserts and updates. Use your down arrow in this grid to add a row or two but only put something in the SomeField column. Leave the id and default field fields alone. For master/detail there is also autoncrement support in the SendMasterDetailAutoIncTransaction method of the AstaClientSocket.
1. Set up your select query and set your edit mode to cached. Refetch on insert action can only work within a transaction so your database must support transactions.
2. Set the AutoIncrementField Property
3. Set the fields that you want to refetch from the RefetchFieldsOnInsert Property
4. Run your project and call apply updates after you have inserted a row or 2. In this demo the auto increment value and the default field value are streamed back to the client with NO code needed by you!
What do you need to do? Not much. Just code one event on the AstaServerSocket.
procedure TForm1.AstaServerSocket1InsertAutoIncrementFetch(Sender:
TObject;
ClientSocket: TCustomWinSocket; AQuery: TComponent; TheDatabase,
TheTable, TheAutoIncField: String; var AutoIncrementValue: Integer);
begin
//put your code in here.
//Something like
with aQuery as TQuery do begin
sql.add('Select max('+TheAutoIncField+') from '+TheTable);
Open;
TheAutoIncrementValue:=FieldByName(TheAutoIncField).AsInteger;
end;
end;
The above code is specific to your database. For access, paradox and Interbase 5.X the above code should work in calling MAX to get the last value used by the server for an autoinc field. For MS SQL Server, Sybase, and SQL Anywhere you would need to write code to access the @@identify variable. The AstaOdbc server is already coded to identify these databases and make
the appropriate call.
For Oracle you would need to get the last sequence value.
Here's what happens on the server.
1. an insert statement comes in either alone or within a transaction
2. it is executed then then the AstaServer socket calls the OnInsertAutoIncrementFetch which
returns a Var Integer of the autoinc value just used for the insert statement. of course
the server must support transactions to make this work as the value returned is only valid
to your session in a transaction.
3. If the autoinc is the only field requested to be refetched it is streamed back to the asta
clientdatset if the transaction is successful. If you have tagged other values in the
AstaClientDatSet.RefetchOnInsert property, an additional select statement is fired that retrieves
any fields that you have choosen (default server values, values changed by triggers etc)
and all the values are streamed back.