* * *

Author Topic: Post in master calls AfterPost in detail  (Read 833 times)

kapibara

  • Sr. Member
  • ****
  • Posts: 459
Post in master calls AfterPost in detail
« on: March 13, 2017, 11:26:16 am »
qryDetail has qryMaster as DataSource.

Is it expected behaviour that qryMaster.Post triggers AfterPost in qryDetail?
Lazarus trunk / fpc 3.0 / Debian Stretch 64-bit

Thaddy

  • Hero Member
  • *****
  • Posts: 4278
Re: Post in master calls AfterPost in detail
« Reply #1 on: March 13, 2017, 11:35:15 am »
Why would you expect otherwise? You may have to update the detail to reflect the current status.
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

kapibara

  • Sr. Member
  • ****
  • Posts: 459
Re: Post in master calls AfterPost in detail
« Reply #2 on: March 13, 2017, 12:22:00 pm »
Still learning about database events. Why is qryDetail.AfterPost triggered by qryMaster?

I have a settings dialog where fields in qryMaster are edited and posted first, and if qryDetail is modified too it is also posted. But it never gets posted since qryMaster triggered qryDetail's afterpost and qryDetail.Modified is no longer true:

Code: Pascal  [Select]
  1. function TfrmDataset.SaveDataset: Boolean;
  2. begin
  3.   Result:=False;
  4.   if ValidateInput then
  5.   try
  6.     try
  7.       DM.qryDataset.Post;
  8.       DM.qryDataset.ApplyUpdates;
  9.  
  10.       if DM.qryLinks.Modified then
  11.       begin
  12.         DM.qryLinks.FieldByName('dataset_id').AsInteger:=
  13.         DM.qryDataset.FieldByName('dataset_id').AsInteger;
  14.  
  15.         DM.qryLinks.Post;
  16.         DM.qryLinks.ApplyUpdates;
  17.       end;
  18.       DM.TX.CommitRetaining;
  19.       Result:=True;
  20.     except
  21.       DM.TX.RollbackRetaining;
  22.     end;
  23.   finally
  24.     DM.qryLinks.Close;
  25.     DM.qryDataset.Close;
  26.   end;
  27. end;

I supply the dataset_id manually since UpdateSQL/InsertSQL etc doesn't make use of parameters.
« Last Edit: March 13, 2017, 01:07:10 pm by kapibara »
Lazarus trunk / fpc 3.0 / Debian Stretch 64-bit

goodname

  • Sr. Member
  • ****
  • Posts: 285
Re: Post in master calls AfterPost in detail
« Reply #3 on: March 13, 2017, 06:09:43 pm »
Not really what you were asking but try not to use the CommitRetaining or RollbackRetaining in transactions. It leads to long running idle transactions which is generally considered to be a program bug. Instead use TSQLQuery option sqlAutoCommit or sqlKeepOpenOnCommit with Commit.

kapibara

  • Sr. Member
  • ****
  • Posts: 459
Re: Post in master calls AfterPost in detail
« Reply #4 on: March 15, 2017, 08:19:55 pm »
@goodname Good to know, I changed to sqlKeepOpenOnCommit and it seems to work fine.

When it was clear that Post of detail is triggered by Post of master, I assigned the dataset_id value in detail's BeforePost and called ApplyUpdates in detail's AfterPost.
Lazarus trunk / fpc 3.0 / Debian Stretch 64-bit

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus