Delphi - TUpdateObject versus OnUpdateRecord for a

2019-06-09 05:43发布

问题:

I have a pFibdataset(which is working similar to BDEDataset) in which I need to make the following join selection

select table.Name as name,
table1.Name as name_1,
table2.Name as name_2
from table 
left join table table_1 on table.id=table_1.id
left join table table_2 on table.id=table_2.id

Fields name, name_1 and name_2 are linked to some data-aware edits. Now, I want after I'm modifying(update,delete,insert operations) the name,name_1 and name_2 fields to be updated in the tables. Based on the wiki Using_Multiple_Update_Objects_Index I can use UpdateObjects, or OnUpdateRecord event.

The problem is that I don't understand how this need to be implemented. I have the join select on the query, how I need to define and work with name_1 and name_2 fields. Can someone provide me an example for this?

I know how to use subqueries in order to accomplish this. I need to see how can I can make it by using UpdateObjects or OnUpdateRecord.

回答1:

TpFibUpdateObject works like a trigger on client side. To make it work, set the following properties:

DataSet - dataset (master) to monitor
KindUpdate - Insert/Update/Delete - action to monitor
SQL - command to execute when action is fired, params are taken from DataSet
ExecuteOrder - AfterDefault/BeforeDefault - probably you need after / master

BUT, instead using a lot of UpdateObject components and such tangled approach, I recommend two alternative (read better) ways:

  1. Updatable view. It will work like a "virtual table". Create a view that joins these theee tables and write Before Insert/Update/Delete triggers. In Delphi use it as a regular table: select from view / insert into view / update view and delete from view. Anyway I suppose you need in many places these tables linked toghether.

  2. Use EXECUTE BLOCK statements in your TpFIBDataSet SQLs. Insert / Update / Delete in a batch all tables.



回答2:

Solution : OnUpdateRecord it must be created an TUpdateObject for each field from the joined table.

 UpdateObjectvariable.DataSet := Dataset;
 fill the SQL text
 Apply.

After all update objects are set, UpdateAction := uaApplied; must be called.