home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / full / delphi / DELPHI16 / TECHINFO / DELPHI / TIS / TI2837.FX < prev    next >
Encoding:
Text File  |  1995-08-24  |  6.3 KB  |  183 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.   PRODUCT  :  Delphi                                 NUMBER  :  2837
  8.   VERSION  :  All
  9.        OS  :  Windows
  10.      DATE  :  July 20, 1995                            PAGE  :  1/3
  11.  
  12.     TITLE  :  Cascading Deletes With Pdox Referential Integrity
  13.  
  14.  
  15.  
  16.  
  17. Paradox tables offer a Referential Integrity feature. This feature pre-
  18. vents adding records to a child table for which there is no matching
  19. record in the parent table. It will also cause the key field(s) in the
  20. child table to be changed when the corresponding key field(s) in the
  21. parent are changed (commonly referred to as a cascading update). These
  22. events occur automatically, requiring no intervention by a Delphi appli-
  23. cation using these tables. However, the Paradox Referential Integrity
  24. feature will not accommodate cascading deletes. That is, Delphi will not
  25. allow you to delete a record in the parent table while matching records
  26. exist in the child table. This would make "orphans" of the child records,
  27. losing referential integrity. Delphi raises an exception when an attempt
  28. is made to delete such a parent record.
  29.  
  30. To effect a cascading delete requires that the deletion of the matching
  31. child records be deleted programmatically -- before the parent record is
  32. deleted. In a Delphi application, this is done by interrupting the process
  33. of deleting the record in the parent table, deleting the matching records
  34. in the child table (if there are any), and then continuing with the dele-
  35. tion of the parent record.
  36.  
  37. A record in a table is deleted by a call to the Delete method of the
  38. TTable component, which deletes the current record in the associated
  39. table. Interrupting the this process to first perform some other opera-
  40. tions is a matter creating a procedure associated with the BeforeDelete
  41. event of the TTable. Any commands in a BeforeDelete event procedure are
  42. executed before the call actually goes out from the application to the
  43. Borland Database Engine (BDE) to physically remove the record from the
  44. table file.
  45.  
  46. To handle the deletion of one or more child records, in a BeforeDelete
  47. event procedure the Delete method for the TTable representing the child
  48. table is called in a loop. The loop is based on the condition of the
  49. record pointer in the table not being positioned at the end of the data
  50. set, as indicated by the Eof method of the TTable. This also accounts for
  51. there being no child records at all matching the parent record to be
  52. deleted: if there are no matching records, the record pointer will already
  53. be at the end of the data set, the loop condition will evaluate to False,
  54. and the Delete method in the loop nevers gets executed.
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.   PRODUCT  :  Delphi                                 NUMBER  :  2837
  69.   VERSION  :  All
  70.        OS  :  Windows
  71.      DATE  :  July 20, 1995                            PAGE  :  2/3
  72.  
  73.     TITLE  :  Cascading Deletes With Pdox Referential Integrity
  74.  
  75.  
  76.  
  77.  
  78.  
  79.   procedure TForm1.Table1BeforeDelete(DataSet: TDataset);
  80.   begin
  81.     with Table2 do begin
  82.       DisableControls;
  83.       First;
  84.       while not Eof do
  85.         Delete;
  86.       EnableControls;
  87.     end;
  88.   end;
  89.  
  90. In the above example, the parent table is represented by the TTable comp-
  91. onent Table1 and the child by Table2. The DisableControls and Enable-
  92. Controls methods are used as a cosmetic measure to freeze any data-aware
  93. components that might be displaying data from Table2 while the records
  94. are being deleted. These two methods make the process visually appear
  95. smoother, but are only optional and not essential to this process. The
  96. Next method need not be called within this loop. This is because the loop
  97. begins at the first record and, as each record is deleted, the record that
  98. previously followed the deleted record moves up in the data set, becoming
  99. both the first and the current record.
  100.  
  101. This example presumes that the parent and child tables are linked with a
  102. Master-Detail relationship, as is typical for tables for which such
  103. Referntial Integrity is configured. Linking the tables in this manner
  104. results in only those records in the child table that match the current
  105. record in the parent table being available. All other records in the child
  106. table are made unavailable through the Master-Detail filtering. If the
  107. tables are not so linked, there are two additional considerations that
  108. must be accounted for when deleting the child records. The first is that
  109. a call to the First method may or may not put the record pointer on a
  110. record that matches the current record in the parent table. This necessi-
  111. tates using a search method to manually move the record pointer to a
  112. matching record. The second consideration affects the condition for the
  113. loop. Because records other than those matching the current record in the
  114. parent table will be accessible, the condition for the loop must check
  115. that each record is a matching record before attempting to delete it. This
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.   PRODUCT  :  Delphi                                 NUMBER  :  2837
  130.   VERSION  :  All
  131.        OS  :  Windows
  132.      DATE  :  July 20, 1995                            PAGE  :  3/3
  133.  
  134.     TITLE  :  Cascading Deletes With Pdox Referential Integrity
  135.  
  136.  
  137.  
  138.  
  139. checking is in addition to querying the Eof method. Because the records
  140. will be ordered by this key field (from a primary or secondary index),
  141. all of the matching records will be contiguous. This leads to the given
  142. that, as soon as the first non-matching record is reached, it can be
  143. assumed that all matching records have been deleted. Thus, the previous
  144. example would be modified to:
  145.  
  146.   procedure TForm1.Table1BeforeDelete(DataSet: TDataset);
  147.   begin
  148.     with Table2 do begin
  149.       DisableControls;
  150.       FindKey([Table1.Fields[0].AsString])
  151.       while (Fields[0].AsString = Table1.Fields[0].AsString) and 
  152.         (not Eof) do
  153.         Delete;
  154.       EnableControls;
  155.     end;
  156.   end;
  157.  
  158. In the above, it is the first field in the parent table (Table1) upon
  159. which the Referential Integrity is based, and the first field in the
  160. child table (Table2) against which matching is judged.
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179. DISCLAIMER: You have the right to use this technical information
  180. subject to the terms of the No-Nonsense License Statement that
  181. you received with the Borland product to which this information
  182. pertains.
  183.